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

Home » Public Forums » archive » Need a function to multiply the elements of an array
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
Need a function to multiply the elements of an array [message #4065] Wed, 26 April 1995 00:00 Go to next message
barsam is currently offline  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 #4127 is a reply to message #4065] Mon, 01 May 1995 00:00 Go to previous message
VUKOVIC is currently offline  VUKOVIC
Messages: 6
Registered: December 1994
Junior Member
In <CHASE.95Apr28164307@retro.jhuapl.edu> chase@retro.jhuapl.edu writes:

> 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

yesss!

and since we are on the topic of arrays etc, it would be nice to have a
symbol that points to the last element of the array

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 #4139 is a reply to message #4065] Fri, 28 April 1995 00:00 Go to previous message
isaacman is currently offline  isaacman
Messages: 20
Registered: June 1992
Junior Member
In article <adorf-2704951634260001@st53.hq.eso.org> adorf@eso.org (Hans-Martin Adorf) writes:
> In article <3nm2uk$qqq@pinot.umd.edu>, barsam@Glue.umd.edu (Barsam

>> I am looking for a routine to multiply the elements of an array.

> You can try
>
> z = exp(total(alog(x)))

Cute, but there better not be any negative or zero elements in x!

Rich Isaacman
General Sciences Corp.
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 Go to previous message
chase is currently offline  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 Go to previous message
VUKOVIC is currently offline  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 Go to previous message
rlw is currently offline  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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: ARMA (was Re: Need a function to multiply the elements of an array)
Next Topic: Re: strart up file in idl

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

Current Time: Wed Oct 08 16:01:26 PDT 2025

Total time taken to generate the page: 0.01263 seconds