;+ ; NAME: ; geo_mean ; ; ; PURPOSE: ; compute the geometric mean of an array ; ; ; INPUTS: ; in: array to compute geometric mean of ; ; ; OPTIONAL INPUTS: ; none ; ; ; KEYWORD PARAMETERS: ; DOUBLE: do the calulation in double precision ; ; ; OUTPUTS: ; the geometric mean ; ; ; ; RESTRICTIONS: ; this is only valid if all values are positive (geometric mean) ; ; ; PROCEDURE: ; plays with mean and log to make sure you are not multiplying huge ; numbers together ; ; ; EXAMPLE: ; b=[1,2,3,4,5,6] ; print, geo_mean(b, /double) ; 2.9937952 ; print, geo_mean(b) ; 2.99380 ; ; ; ; MODIFICATION HISTORY: ; ; Tue Jan 22 14:18:13 2008, Brian Larsen ; changed to use message, added zero warning, and should ; handle NaN now ; Wed Dec 21 18:37:41 2005, Brian Larsen ; written and tested ; ;- FUNCTION geo_mean, in, DOUBLE=double IF size(in, /n_dim) NE 1 THEN $ message, /ioerror, 'Input must be 1-D array' IF min(in, /nan) LT 0.0 THEN $ message, /ioerror, 'Array must be positive' IF min(in, /nan) EQ 0.0 THEN $ message, /information, 'Zero element in array, answer is zero' IF KEYWORD_SET(double) THEN BEGIN ind = double(in) ENDIF ELSE ind = in inl = alog(ind) RETURN, exp(mean(inl, /nan)) END