| 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 | |
|---|
| 26 | component Integrate2 |
|---|
| 27 | export Executable |
|---|
| 28 | |
|---|
| 29 | start : RR64 = 0.0 |
|---|
| 30 | finish : RR64 = 1536.0 |
|---|
| 31 | errorTolerance : RR64 = 10.0^(-4) (* should be 10^(-12) *) |
|---|
| 32 | reps : ZZ32 = 10 |
|---|
| 33 | expect = 1.391570583 10.0^12 |
|---|
| 34 | |
|---|
| 35 | computeArea(l: RR64, r: RR64): RR64 = |
|---|
| 36 | recEval(l, r, l l + 1.0, (r r + 1.0) r, 0) |
|---|
| 37 | |
|---|
| 38 | recEval(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 | |
|---|
| 53 | elapsedSec(init:ZZ64, final:ZZ64): RR64 = |
|---|
| 54 | (final - init) / (10.0^9) |
|---|
| 55 | |
|---|
| 56 | run(): () = 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 | |
|---|
| 69 | end Integrate2 |
|---|