Re: Need a function to multiply the elements of an array [message #4052] |
Thu, 27 April 1995 00:00 |
barsam
Messages: 2 Registered: April 1995
|
Junior Member |
|
|
In article <3nm2uk$qqq@pinot.umd.edu>,
I wrote:
>
> Hi,
>
> I am looking for a routine to multiply the elements of an array.
> Is there a faster way of doing it than the following?
>
>
> function prod, x
> n = n_elements(x)-1
> z = 1.d0
> for i = 0, n do z = z * x(i)
> return, z
> end
>
>
>
> BM
>
Thanks for the replies.
z = exp(total(alog(x))) gives me about 60% savings. Not terribly
fast but obviously better than the "for" loop.
BM
|
|
|
Re: Need a function to multiply the elements of an array [message #4056 is a reply to message #4052] |
Thu, 27 April 1995 00:00  |
adorf
Messages: 4 Registered: August 1994
|
Junior Member |
|
|
In article <3nm2uk$qqq@pinot.umd.edu>, barsam@Glue.umd.edu (Barsam
Marasli) wrote:
> Hi,
>
> I am looking for a routine to multiply the elements of an array.
> Is there a faster way of doing it than the following?
>
>
> function prod, x
> n = n_elements(x)-1
> z = 1.d0
> for i = 0, n do z = z * x(i)
> return, z
> end
>
>
>
> BM
You can try
z = exp(total(alog(x)))
Whether it is faster than your function, I do not know. On my Sparcstation
2 it took 2 seconds to multiply 1 million numbers.
Regards
Hans-Martin
PS: I think that apart from the function 'total' (which might more
appropriately be called 'sum') there should be a native IDL function
'prod' doing what you do by hand.
--
Hans-Martin Adorf
ST-ECF/ESO
Karl-Schwarzschild-Str. 2
D-85748 Garching b. Muenchen
Germany
Tel: +49-89-32006-261
Fax: +49-89-32006-480
Internet: adorf@eso.org
|
|
|
Re: Need a function to multiply the elements of an array [message #4062 is a reply to message #4052] |
Wed, 26 April 1995 00:00  |
dan
Messages: 27 Registered: March 1993
|
Junior Member |
|
|
In article <3nm2uk$qqq@pinot.umd.edu>, barsam@Glue.umd.edu (Barsam Marasli) writes:
|>
|> Hi,
|>
|> I am looking for a routine to multiply the elements of an array.
|> Is there a faster way of doing it than the following?
|>
|>
|> function prod, x
|> n = n_elements(x)-1
|> z = 1.d0
|> for i = 0, n do z = z * x(i)
|> return, z
|> end
|>
|>
|>
|> BM
|>
How about
z = exp(total(alog(x)))
Which only works if all x's greater than zero.
--
************************************************************ ***
** Dan Bergmann dbergmann@llnl.gov **
** Global Climate Research fax (510) 422-5844 **
** Lawrence Livermore National Lab human (510) 423-6765 **
|
|
|