1. simpson.ex: The Simpson Rule
- Posted by Alan Tu <ATU5713 at COMPUSERVE.COM> Dec 17, 1998
- 327 views
- Last edited Dec 18, 1998
For all you calculists or whatever, the Simpson Rule for finding integrals in Euphoria! SIMPSON.EX will ask for A, B, and N (N must be even). There is one catch: the function f(atom x). This must be a global function in function.e. And it must be, well, euphoric. For example, BASIC recognizes the standard way of doing exponents on computer x^2 is x squared. But Euphoria does not. To write x squared, you must do power(x,2). x cubed would be power(x,3). Order of operations are consistent with standard algebraic practice. sin, cos, and tan are self-explanatory; Euphoria has these functions. All those who are interested in RAM programs (LRAM, MRAM, and RRAM), I've written those too. So, without further waiting, this is simpson.ex Last thing, speed does matter, so suggestions to improve speed and all other suggestions are welcome! Alan include get.e include function.e type even(integer x) return integer(0.5*x) end type atom delta, coeff, solution, real, answer object a, b, n even subd real = 0 clear_screen() puts(1,"Enter A: ") a = get(0) a = a[2] puts(1,"\nEnter B: ") b = get(0) b = b[2] puts(1,"\nEnter N: ") n = get(0) subd = n[2] delta = (b-a)/subd coeff = (1/3)*delta puts(1,"\n") for i = 0 to subd do solution = f(a+i*delta) if i = 0 or i = subd then real = real+solution elsif 0.5*i != floor(0.5*i) then real = real+4*solution elsif 0.5*i = floor(0.5*i) then real = real+2*solution end if end for answer = coeff*real puts(1,"The estimated integral is: ") print(1,answer) puts(1,"\nNote: Simpson's Rule often provides exact answers.") --begin sample function.e global function f(atom x) return 1/x end function
2. Re: simpson.ex: The Simpson Rule
- Posted by Hawke' <mdeland at GEOCITIES.COM> Dec 17, 1998
- 311 views
Alan Tu wrote: > Last thing, speed does matter, so suggestions to improve speed and all > other suggestions are welcome! > include get.e > include function.e > type even(integer x) > return integer(0.5*x) at this point, i'm not sure, but, since you are calling it so often, return remainder(x,2) = 0 may be considerably faster... > end type > atom delta, coeff, solution, real, answer, current, temp, test > object a, b, n > even subd > real = 0 > clear_screen() > puts(1,"Enter A: ") > a = get(0) > a = a[2] > puts(1,"\nEnter B: ") > b = get(0) > b = b[2] > puts(1,"\nEnter N: ") > n = get(0) > subd = n[2] > delta = (b-a)/subd > coeff = (1/3)*delta coeff = delta/3 would be faster, one less operation and one less set of () to parse... --add this: current = 0 > puts(1,"\n") > for i = 0 to subd do > solution = f(a+i*delta) change this to solution = f(a+current) > if i = 0 or i = subd then > real = real+solution > elsif 0.5*i != floor(0.5*i) then > real = real+4*solution > elsif 0.5*i = floor(0.5*i) then > real = real+2*solution > end if --change the compound if..then to: if i = 0 or i = subd then real = real + solution else test = 0.5*i temp = solution + solution real = real + temp if test != floor(test) then real = real + temp end if end if --add this: current = current + delta > end for > answer = coeff*real > puts(1,"The estimated integral is: ") > print(1,answer) > puts(1,"\nNote: Simpson's Rule often provides exact answers.") > a straight up typing of the newly modified function/program is thusly: include get.e include function.e type even(integer x) return remainder(x,2) = 0 end type atom delta, coeff, solution, real, answer, current, temp, test object a, b, n even subd real = 0 clear_screen() puts(1,"Enter A: ") a = get(0) a = a[2] puts(1,"\nEnter B: ") b = get(0) b = b[2] puts(1,"\nEnter N: ") n = get(0) subd = n[2] delta = (b-a)/subd coeff = delta/3 current = 0 puts(1,"\n") for i = 0 to subd do solution = f(a+current) if i = 0 or i = subd then real = real + solution else test = 0.5*i temp = solution + solution real = real + temp if test != floor(test) then real = real + temp end if end if current = current + delta end for answer = coeff*real puts(1,"The estimated integral is: ") print(1,answer) puts(1,"\nNote: Simpson's Rule often provides exact answers.") and the sample function is the 'same' of course.... hope this shows some speed improvements... everyone knows i'm a sucker for tweaking :) the benchmark program i posted for testing reverse() seemed to provide everyone with a (seemingly?) stable platform for testing functions across different processors and you are welcome of course to use that to time the two versions :) take care and care of, _/ _/ _/_/_/ _/ _/ _/ _/ _/_/_/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/_/_/ _/_/_/ _/ _/ _/_/ _/_/ _/ _/ _/ _/ _/_/_/ _/ _/ _/ _/ _/ _/ _/ _/_/_/ _/ _/ _/_/_/ ({<=----------------------------------------=>}) ({<=- http://members.xoom.com/Hawkes_Hovel> -=}) ({<=----------------------------------------=>})
3. Re: simpson.ex: The Simpson Rule
- Posted by Alan Tu <ATU5713 at COMPUSERVE.COM> Dec 17, 1998
- 313 views
- Last edited Dec 18, 1998
Thanks, Hawke, for all the input! See my other message "Using Euphoria to Solve Real World Problems". It'll inspire you, hopefully. Alan
4. Re: simpson.ex: The Simpson Rule
- Posted by Lucius Hilley III <lhilley at CDC.NET> Dec 18, 1998
- 325 views
- Last edited Dec 19, 1998
On Thu, 17 Dec 1998 17:56:35 -0800, Hawke' <mdeland at GEOCITIES.COM> wrote: Alan Tu wrote: > Last thing, speed does matter, so suggestions to improve speed and all > other suggestions are welcome! > > type even(integer x) > return integer(0.5*x) > end type Hawke' wrote: >type even(integer x) > return remainder(x,2) = 0 >end type I, Lucius, write: > I believe that the following use of and_bits() will be even faster > than remainder. > >type even(integer x) > return and_bits(x, 1) = 0 >end type _________________________ Lucius L. Hilley III lhilley at cdc.net http://www.cdc.net/~lhilley http://www.americanantiques.com http://www.dragonvet.com _________________________