comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: TOTAL gives totally different result on identical array
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: TOTAL gives totally different result on identical array [message #76896 is a reply to message #76892] Fri, 08 July 2011 08:44 Go to previous messageGo to previous message
Foldy Lajos is currently offline  Foldy Lajos
Messages: 268
Registered: October 2001
Senior Member
On Fri, 8 Jul 2011, Liam Gumley wrote:

> function kahansum, input
> sum = 0.0
> c = 0.0
> for i = 0L, n_elements(input) - 1 do begin
> y = input[i] - c
> t = sum + y
> c = (t - sum) - y
> sum = t
> endfor
> return, sum
> end


It will be very slow. But it's IDL, vectorize it!

; test.pro begin

function kahan_sum, input
sum=0.0
c=0.0
for i=0l, n_elements(input)-1 do begin
y=input[i]-c
t=sum+y
c=(t-sum)-y
sum=t
endfor
return, sum
end

function kahan_sum_1000, input
sum=fltarr(1000)
c=fltarr(1000)
for i=0l, n_elements(input)-1,1000 do begin
y=input[i:i+999]-c
t=sum+y
c=(t-sum)-y
sum=t
endfor
return, kahan_sum(sum)
end


pro test
vec=findgen(100000)

t1=systime(1)
s1=kahan_sum(vec)
t1=systime(1)-t1
print, 't1:', t1

t2=systime(1)
s2=kahan_sum_1000(vec)
t2=systime(1)-t2
print, 't2:', t2
print, 'ratio:', t1/t2
print, 'sums and diff:', s1, s2, s2-s1

end

; test.pro end


IDL> test
t1: 0.056817055
t2: 0.0012319088
ratio: 46.121153
sums and diff: 4.99995e+09 4.99995e+09 0.00000
IDL>


(The general case is left to the reader.)

regards,
Lajos
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Variable is undefined: Actually a function
Next Topic: Weighted correlation

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Sat Oct 11 06:30:09 PDT 2025

Total time taken to generate the page: 0.15740 seconds