Need a function to multiply the elements of an array [message #4065] |
Wed, 26 April 1995 00:00  |
barsam
Messages: 2 Registered: April 1995
|
Junior Member |
|
|
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
|
|
|
|
|
Re: Need a function to multiply the elements of an array [message #4141 is a reply to message #4065] |
Fri, 28 April 1995 00:00  |
chase
Messages: 62 Registered: May 1993
|
Member |
|
|
>>>> > "Rick" == Rick White <rlw@sundog.stsci.edu> writes:
In article <3nr9e0$2dp@sundog.stsci.edu> rlw@sundog.stsci.edu (Rick White) writes:
Rick> In article <3nm2uk$qqq@pinot.umd.edu>, barsam@Glue.umd.edu (Barsam Marasli) writes:
Rick> |> I am looking for a routine to multiply the elements of an array.
Rick> Here is one more approach for this problem. I did some tests and found
Rick> it to be roughly the same speed as the previous approaches (using logarithms)
Rick> for short vectors (< 100 elements), but it is faster by about a factor
Rick> of 3 for long vectors. It is about 15 times faster than a direct loop
Rick> for long vectors.
It would be nice if IDL had the equivalent of the Scan operator in the
APL language. This is equivalent to partial sums for a vector using
an arbitrary binary operator (+, x, min, max, etc.).
A related recursive operation that I have wanted on occasion is an
auto-regressive (AR) filter. IDL can easily implement moving-average
filters (via convol) but it does not have an efficient function for
impelmenting AR (or ARMA) filters. The most useful generalization of
this would be implementations of multi-input, multi-output linear
time-invariant (or time-varying) state space equations like those in
the matlab signal processing and image processing packages.
Native functions for these types of operations would be an order of
magnitude faster over IDL loop implementations.
Just wishing,
Chris
--
===============================
Bldg 24-E188
The Applied Physics Laboratory
The Johns Hopkins University
Laurel, MD 20723-6099
(301)953-6000 x8529
chris.chase@jhuapl.edu
|
|
|
Re: Need a function to multiply the elements of an array [message #4142 is a reply to message #4065] |
Fri, 28 April 1995 00:00  |
VUKOVIC
Messages: 6 Registered: December 1994
|
Junior Member |
|
|
In <3nohst$sun@pinot.umd.edu% barsam@Glue.umd.edu writes:
% 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
%
%
That is strange (or I am strange), but I would think naively
expect that in the "for" loop you have only N multiplications, while
taking log of each number gives you M*N multiplications, where M is the
number of multiplications used to evaluate the log of a number.
I guess if going for speed, it would be fairly simple to write a fortran/c
routine to do it
Mirko
Mirko Vukovic | vukovic@uwmfe.neep.wisc.edu
Dept. of Nucl. Eng. and Eng. Phys. | mvukovic@wiscmacc.bitnet
U of Wisconsin -- Madison | phone: (608) 265-4069
1500 Johnson Drive; Madison WI 53706| fax: (608) 265-2364
|
|
|
Re: Need a function to multiply the elements of an array [message #4143 is a reply to message #4065] |
Fri, 28 April 1995 00:00  |
rlw
Messages: 1 Registered: April 1995
|
Junior Member |
|
|
In article <3nm2uk$qqq@pinot.umd.edu>, barsam@Glue.umd.edu (Barsam Marasli) writes:
|> I am looking for a routine to multiply the elements of an array.
Here is one more approach for this problem. I did some tests and found
it to be roughly the same speed as the previous approaches (using logarithms)
for short vectors (< 100 elements), but it is faster by about a factor
of 3 for long vectors. It is about 15 times faster than a direct loop
for long vectors.
function prod,x
; Compute product of elements of x. Returns result as double.
; R. White, 1995 April 28
n = n_elements(x)
y = double(x)
mm = 1.0d0
; takes about log3(n) passes through this loop
; 20 elements is about the right break point where the direct loop becomes
; fastest
while (n GT 20) do begin
mval = n MOD 3
if mval eq 1 then begin
mm = mm*y(n-1)
endif else if mval eq 2 then begin
mm = mm*y(n-1)*y(n-2)
endif
n = n/3
y = y(0:n-1)*y(n:2*n-1)*y(2*n:3*n-1)
endwhile
for i=0,n-1 do mm=mm*y(i)
return, mm
end
|
|
|