root/trunk/ProjectFortress/demos/Integrate2.fss

Revision 3621, 2.1 KB (checked in by jmaessen, 8 months ago)

[demos] Numerical integration code based on the code provided by Doug
Lea. This code had a data-dependent recursion depth that keeps
computations levels from being too predictable. We will eventually
want to turn this into Compiled6; at that point we ought to be able to
dial down the error tolerance. The interpreter is just too slow for
that treatment.

Line 
1(*******************************************************************************
2    Copyright 2009 Sun Microsystems, Inc.,
3    4150 Network Circle, Santa Clara, California 95054, U.S.A.
4    All rights reserved.
5
6    U.S. Government Rights - Commercial software.
7    Government users are subject to the Sun Microsystems, Inc. standard
8    license agreement and applicable provisions of the FAR and its supplements.
9
10    Use is subject to license terms.
11
12    This distribution may include materials developed by third parties.
13
14    Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
15    trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
16 ******************************************************************************)
17
18(************************************************************
19 * Sample program using Gaussian Quadrature for numerical integration.
20 *
21 * Essentially a translation of Java code by Doug Lea, in turn
22 * inspired by a <A href="http://www.cs.uga.edu/~dkl/filaments/dist.html"> Filaments</A>
23 * demo program.
24 ************************************************************)
25
26component Integrate2
27export Executable
28
29start : RR64 = 0.0
30finish : RR64 = 1536.0
31errorTolerance : RR64 = 10.0^(-4) (* should be 10^(-12) *)
32reps : ZZ32 = 10
33expect = 1.391570583 10.0^12
34
35computeArea(l: RR64, r: RR64): RR64 =
36    recEval(l, r, l l + 1.0, (r r + 1.0) r, 0)
37
38recEval(l: RR64, r: RR64, fl: RR64, fr: RR64, a: RR64): RR64 = do
39    h = 0.5 (r-l)
40    c = l+h
41    fc = (c c + 1.0) c
42    hh = 0.5 h
43    al = (fl + fc) hh
44    ar = (fr + fc) hh
45    alr = al + ar
46    if |alr - a| <= errorTolerance then
47        alr
48    else
49        recEval(c,r,fc,fr,ar) + recEval(l,c,fl,fc,al)
50    end
51  end
52
53elapsedSec(init:ZZ64, final:ZZ64): RR64 =
54    (final - init) / (10.0^9)
55
56run(): () = do
57    println("Time(s)\tArea")
58    for _ <- 0#reps do
59        startTime = nanoTime()
60        a = computeArea(start,finish)
61        endTime = nanoTime()
62        println(elapsedSec(startTime,endTime) "\t" a)
63        if |a-expect| > errorTolerance a then
64            println("    FAIL: Bad error tolerance!")
65        end
66    end
67  end
68
69end Integrate2
Note: See TracBrowser for help on using the browser.