# Re: multiplication

```>>>>> "JK" == James Kuyper <kuyper@wizard.net> writes:

JK> meron@cars3.uchicago.edu wrote:
>>
>> In article <38E03BDC.868B8396@hotmail.com>, marc
>> <m_schellens@hotmail.com> writes:
>> >Is there a function like TOTAL but for multiplication.  Like the
>> >big PI symbol in mathematical notation.  Or this really something
>> >for the for loop?
>> >
>> >I.E.
>> >
>> >a=[1,2,3,...]
>> >
>> >result=a[1]*a[2]*a[3]...
>> >
>> if all the elements of a are positive then you can simply do
>>
>> result = exp(total(alog(a)))
JK> ...
>> If some of the elements are negative, you can still handle it.  do
>>
>> dum = where(a lt 0, ndum) sig = (-1)^ndum result =
>> sig*exp(total(alog(abs(a))))

JK> You can't honestly be suggesting that this is a good technique?
JK> Ignore for a momement what happens if any element of 'a' is
JK> 0. That code performs two transcendental function evaluations per
JK> element of 'a'. IDL would have to be very badly engineered (which
JK> I suppose is possible), for a 'for' loop to execute more slowly

Well, it depends very much on the size of the array.  Loops in IDL are
indeed very slow.  Try the following: Set N to a large number
(e.g. 10 000 000) and execute the following lines:

x=fltarr(n)*0.+1.000001 & p=1 & for i=0.,1.*n_elements(x)-1 do p=p*x[i] & print,p

x=fltarr(n)*0.+1.000001 &  p=exp(total(alog(x)))&print,p

You'll get a surprise, I promise.

- Carsten

--
Carsten Dominik <dominik@astro.uva.nl>           \ _ /
Sterrenkundig Instituut "Anton Pannekoek"         |X|                _
Kruislaan 403; NL-1098 SJ Amsterdam              /| |\    _  _     _/ \
phone +31 (20) 525-7477; FAX +31 (20) 525-7484 ___|o|____/ ~~ \___/    ~~~~~

```