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

Home » Public Forums » archive » Re: Cumulative total
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Cumulative total [message #12828] Fri, 18 September 1998 00:00
bowman is currently offline  bowman
Messages: 121
Registered: September 1991
Senior Member
In article <3602B1F3.210@cdc.noaa.gov>, Andrew Loughe <afl@cdc.noaa.gov> wrote:

> Nice, elegant solution, Eddie.

>> here is the way i do it:
>>
>> IDL> A = findgen(10)
>> IDL> N = n_elements(A)
>> IDL> result = A # (lindgen(N,N) ge transpose(lindgen(N,N)))
>> IDL> print,result
>> 0.00000
>> 1.00000
>> 3.00000
>> 6.00000
>> 10.0000
>> 15.0000
>> 21.0000
>> 28.0000
>> 36.0000
>> 45.0000

In my opinion this is a baroque construction that reveals a shortcoming in
IDL. (Hey, not a major shortcoming. I'm not committing heresy here!)

To compute the cumulative sum of a vector of length N, i.e.,

x_cum[0] = x[0]
FOR i = 1, N-1 DO x_cum[i] = x_cum[i-1] + x[i]

should require N loads, N flops (adds), and N stores. This may not
optimize well, since each result depends on the previous one, but I
suspect most modern Fortran or C compilers would do pretty well.

The IDL approach above requires creating an N^2 matrix filled with
integers, performing an if test on every element of that array and its
transpose, and then performing a matrix-vector multiply (N^2 multiplies
and N^2 adds). What do you do when N = 100,000? It seem a silly way to
do a simple task.

I'm not trying to pick on Eddie. It works for him. I wonder which method
is faster?

Maybe I should write a Fortran function to do it ... yuck -- highly
non-portable.

So, note to RSI: Add CUMULATIVE function to next release of IDL and make
it a good IDL function so that one can specify which dimension of a
possibly multidimensional array to accumulate over, etc. It should be
quite useful with HISTOGRAM.

Ken

--
Kenneth P. Bowman, Professor 409-862-4060
Department of Meteorology 409-862-4466 fax
Texas A&M University bowmanATcsrp.tamu.edu
College Station, TX 77843-3150 Change the AT to @
Re: Cumulative total [message #12838 is a reply to message #12828] Fri, 18 September 1998 00:00 Go to previous message
Andy Loughe is currently offline  Andy Loughe
Messages: 174
Registered: November 1995
Senior Member
Nice, elegant solution, Eddie.

P.S. Weren't you the kid on... Nah!

eddie haskell wrote:
>
>> Can anyone suggest a more efficient way to compute the
>> running cumulative total of a 1-D array than using FOR loops?
>
> here is the way i do it:
>
> IDL> A = findgen(10)
> IDL> N = n_elements(A)
> IDL> result = A # (lindgen(N,N) ge transpose(lindgen(N,N)))
> IDL> print,result
> 0.00000
> 1.00000
> 3.00000
> 6.00000
> 10.0000
> 15.0000
> 21.0000
> 28.0000
> 36.0000
> 45.0000
>
> cheers,
> eddie
Re: Cumulative total [message #12840 is a reply to message #12828] Fri, 18 September 1998 00:00 Go to previous message
eddie haskell is currently offline  eddie haskell
Messages: 29
Registered: September 1998
Junior Member
> Can anyone suggest a more efficient way to compute the running cumulative
> total of a 1-D array than using FOR loops?

here is the way i do it:

IDL> A = findgen(10)
IDL> N = n_elements(A)
IDL> result = A # (lindgen(N,N) ge transpose(lindgen(N,N)))
IDL> print,result
0.00000
1.00000
3.00000
6.00000
10.0000
15.0000
21.0000
28.0000
36.0000
45.0000

cheers,
eddie

__^__
__^__
( ___ )----------------------------------------------------------- -( ___
)
| / | A. G. Edward Haskell | \
|
| / | Center for Coastal Physical Oceanography | \
|
| / | Old Dominion University, Norfolk VA 23529 | \
|
| / | Voice 757.683.4816 Fax 757.683.5550 | \
|
|___| e-mail haskell*ccpo.odu.edu
|___|
(_____)----------------------------------------------------- -------(_____)
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Help with pick menu widget code
Next Topic: Re: Search routines

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

Current Time: Wed Oct 08 19:35:44 PDT 2025

Total time taken to generate the page: 0.00423 seconds