[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

*Subject*: Re: multiplication*From*: Carsten Dominik <dominik(at)astro.uva.nl>*Date*: 28 Mar 2000 14:35:43 +0200*Distribution*: world*Newsgroups*: comp.lang.idl-pvwave*Organization*: Faculty of Science, University of Amsterdam*References*: <38E03BDC.868B8396@hotmail.com> <f9XD4.136$i3.1947@uchinews> <38E0A379.34ADB7F7@wizard.net>*Xref*: news.doit.wisc.edu comp.lang.idl-pvwave:19068

>>>>> "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 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 -- 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|____/ ~~ \___/ ~~~~~

**Follow-Ups**:**Re: multiplication***From:*Craig Markwardt

**References**:**multiplication***From:*marc

**Re: multiplication***From:*meron

**Re: multiplication***From:*James Kuyper

- Prev by Date:
**Re: multiplication** - Next by Date:
**Re: Colormaps (a favorite subject!)** - Prev by thread:
**Re: multiplication** - Next by thread:
**Re: multiplication** - Index(es):