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

*Subject*: Re: multiplication*From*: Craig Markwardt <craigmnet(at)cow.physics.wisc.edu>*Date*: 28 Mar 2000 11:30:41 -0600*Newsgroups*: comp.lang.idl-pvwave*Organization*: U. Wisc. Madison Physics -- Compact Objects*References*: <38E03BDC.868B8396@hotmail.com> <f9XD4.136$i3.1947@uchinews> <38E0A379.34ADB7F7@wizard.net> <qzug0tbi868.fsf@antares.astro.uva.nl>*Reply-To*: craigmnet(at)cow.physics.wisc.edu*Xref*: news.doit.wisc.edu comp.lang.idl-pvwave:19071

Carsten Dominik <dominik@astro.uva.nl> writes: > > 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. One way to speed things up is to use some sort of a divide and conquer algorithm. Which is to say, divide the array into two segments and multiply them element-by-element. Keep doing this until you get down to a single element. FUNCTION CMPRODUCT, ARRAY X = ARRAY N = N_ELEMENTS(X) WHILE N GT 1 DO BEGIN IF (N MOD 2) EQ 1 THEN X(0) = X(0) * X(N-1) ;; When N is odd!! N2 = FLOOR(N/2) X = X(0:N2-1) * X(N2:*) ;; Don't worry if N is odd here. ;; X keeps shrinking by a factor of two each time N = N2 ENDWHILE RETURN,X(0) END Disadvantages are that it may be slower when n_elements(array) is small. Also, the round-off error can grow to significance, as I think Carsten was trying to say, but this will happen with most approaches unfortunately. Double precision can help. Craig -- -------------------------------------------------------------------------- Craig B. Markwardt, Ph.D. EMAIL: craigmnet@cow.physics.wisc.edu Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response --------------------------------------------------------------------------

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

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

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

**Re: multiplication***From:*Carsten Dominik

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