Re: simpson.ex: The Simpson Rule
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> -=})
({<=----------------------------------------=>})
|
Not Categorized, Please Help
|
|