[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
>>>>> "JK" == James Kuyper <email@example.com> writes:
JK> firstname.lastname@example.org wrote:
>> In article <38E03BDC.868B8396@hotmail.com>, marc
>> <email@example.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?
>> if all the elements of a are positive then you can simply do
>> result = exp(total(alog(a)))
>> If some of the elements are negative, you can still handle it. do
>> dum = where(a lt 0, ndum) sig = (-1)^ndum result =
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
JK> than your code.
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 Dominik <firstname.lastname@example.org> \ _ /
Sterrenkundig Instituut "Anton Pannekoek" |X| _
Kruislaan 403; NL-1098 SJ Amsterdam /| |\ _ _ _/ \
phone +31 (20) 525-7477; FAX +31 (20) 525-7484 ___|o|____/ ~~ \___/ ~~~~~