Changeset 133 for trunk/SRC/Matrix/cmapply.pro
- Timestamp:
- 07/07/06 11:57:27 (18 years ago)
- Location:
- trunk/SRC/Matrix
- Files:
-
- 1 added
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/SRC/Matrix/cmapply.pro
r132 r133 1 ;; Utility function, adapted from CMPRODUCT 1 2 ;+ 2 ; NAME: 3 ; CMAPPLY 4 ; 5 ; AUTHOR: 6 ; Craig B. Markwardt, NASA/GSFC Code 662, Greenbelt, MD 20770 7 ; craigm@lheamail.gsfc.nasa.gov 8 ; 9 ; PURPOSE: 10 ; Applies a function to specified dimensions of an array 11 ; 12 ; MAJOR TOPICS: 13 ; Arrays 14 ; 15 ; CALLING SEQUENCE: 16 ; XX = CMAPPLY(OP, ARRAY, DIMS, [/DOUBLE], [TYPE=TYPE]) 17 ; 18 ; DESCRIPTION: 19 ; CMAPPLY will apply one of a few select functions to specified 3 ; @todo seb 4 ;- 5 ; 6 function cmapply_product, x 7 ; 8 compile_opt idl2, strictarrsubs 9 ; 10 sz = size(x) 11 n = sz[1] 12 13 while n GT 1 do begin 14 if (n mod 2) EQ 1 then x[0,*] = x[0,*] * x[n-1,*] 15 n2 = floor(n/2) 16 x = x[0:n2-1,*] * x[n2:*,*] 17 n = n2 18 endwhile 19 return, reform(x[0,*], /overwrite) 20 end 21 22 ;; Utility function, used to collect collaped dimensions 23 ;+ 24 ; @todo seb 25 ;- 26 ; 27 pro cmapply_redim, newarr, dimapply, dimkeep, nkeep, totcol, totkeep 28 ; 29 compile_opt idl2, strictarrsubs 30 ; 31 sz = size(newarr) 32 ;; First task: rearrange dimensions so that the dimensions 33 ;; that are "kept" (ie, uncollapsed) are at the back 34 dimkeep = where(histogram(dimapply,min=1,max=sz[0]) ne 1, nkeep) 35 if nkeep EQ 0 then return 36 37 newarr = transpose(temporary(newarr), [dimapply-1, dimkeep]) 38 ;; totcol is the total number of collapsed elements 39 totcol = sz[dimapply[0]] 40 for i = 1, n_elements(dimapply)-1 do totcol = totcol * sz[dimapply[i]] 41 totkeep = sz[dimkeep[0]+1] 42 for i = 1, n_elements(dimkeep)-1 do totkeep = totkeep * sz[dimkeep[i]+1] 43 44 ;; this new array has two dimensions: 45 ;; * the first, all elements that will be collapsed 46 ;; * the second, all dimensions that will be preserved 47 ;; (the ordering is so that all elements to be collapsed are 48 ;; adjacent in memory) 49 newarr = reform(newarr, [totcol, totkeep], /overwrite) 50 end 51 52 ;; Main function 53 ;+ 54 ; 55 ; @file_comments 56 ; Applies a function to specified dimensions of an array 57 ; 58 ; Description: 59 ; 60 ; CMAPPLY will apply one of a few select functions to specified 20 61 ; dimensions of an array. Unlike some IDL functions, you *do* have 21 62 ; a choice of which dimensions that are to be "collapsed" by this … … 59 100 ; elements is large. 60 101 ; 61 ; 62 ; INPUTS: 63 ; 64 ; OP - The operation to perform, as a string. May be upper or lower 102 ; @categories Arrays 103 ; 104 ; @param OP {in}{required} The operation to perform, as a string. May be upper or lower 65 105 ; case. 66 106 ; … … 69 109 ; the user-defined function. 70 110 ; 71 ; ARRAY -An array of values to be operated on. Must not be of type111 ; @param ARRAY {in}{required} An array of values to be operated on. Must not be of type 72 112 ; STRING (7) or STRUCTURE (8). 73 113 ; 74 ; OPTIONAL INPUTS: 75 ; 76 ; DIMS - An array of dimensions that are to be "collapsed", where 114 ; @param DIMS {in}{optional}{default=1 (ie, first dimension)} 115 ; An array of dimensions that are to be "collapsed", where 77 116 ; the the first dimension starts with 1 (ie, same convention 78 117 ; as IDL function TOTAL). Whereas TOTAL only allows one … … 84 123 ; DEFAULT: 1 (ie, first dimension) 85 124 ; 86 ; KEYWORDS: 87 ; 88 ; DOUBLE - Set this if you wish the internal computations to be done 125 ; @keyword DOUBLE Set this if you wish the internal computations to be done 89 126 ; in double precision if necessary. If ARRAY is double 90 127 ; precision (real or complex) then DOUBLE=1 is implied. 91 128 ; DEFAULT: not set 92 129 ; 93 ; TYPE -Set this to the IDL code of the desired output type (refer130 ; @keyword TYPE Set this to the IDL code of the desired output type (refer 94 131 ; to documentation of SIZE()). Internal results will be 95 132 ; rounded to the nearest integer if the output type is an … … 97 134 ; DEFAULT: same is input type 98 135 ; 99 ; FUNCTARGS -If OP is 'USER:...', then the contents of this keyword136 ; @keyword FUNCTARGS If OP is 'USER:...', then the contents of this keyword 100 137 ; are passed to the user function using the _EXTRA 101 138 ; mechanism. This way you can pass additional data to … … 104 141 ; DEFAULT: undefined (i.e., no keywords passed by _EXTRA) 105 142 ; 106 ; RETURN VALUE: 107 ; 108 ; An array of the required TYPE, whose elements are the result of 143 ; @returns An array of the required TYPE, whose elements are the result of 109 144 ; the requested operation. Depending on the operation and number of 110 145 ; elements in the input array, the result may be vulnerable to 111 146 ; overflow or underflow. 112 147 ; 113 ; EXAMPLES: 114 ; Shows how CMAPPLY can be used to total the second dimension of the 148 ; @examples 149 ; 150 ; First example: Shows how CMAPPLY can be used to total the second dimension of the 115 151 ; array called IN. This is equivalent to OUT = TOTAL(IN, 2) 116 152 ; … … 120 156 ; OUT INT = Array[5] 121 157 ; 122 ; Second example .Input is assumed to be an 5x100 array of 1's and158 ; Second example: Input is assumed to be an 5x100 array of 1's and 123 159 ; 0's indicating the status of 5 detectors at 100 points in time. 124 160 ; The desired output is an array of 100 values, indicating whether … … 134 170 ; although there would have been more loop iterations). 135 171 ; 136 ; Third example .Shows sum over first and third dimensions in an172 ; Third example: Shows sum over first and third dimensions in an 137 173 ; array with dimensions 4x4x4: 138 174 ; … … 142 178 ; 408 472 536 600 143 179 ; 144 ; Fourth example .A user-function (MEDIAN) is used:180 ; Fourth example: A user-function (MEDIAN) is used: 145 181 ; 146 182 ; IDL> IN = RANDOMN(SEED,10,10,5) … … 151 187 ; (OUT[i,j] is the median value of IN[i,j,*]) 152 188 ; 153 ; MODIFICATION HISTORY: 154 ; Mar 1998, Written, CM 189 ; @history Mar 1998, Written, CM 155 190 ; Changed usage message to not bomb, 24 Mar 2000, CM 156 191 ; Signficant rewrite for *, MIN and MAX (inspired by Todd Clements … … 163 198 ; Correct bug in MAX/MIN initialization of RESULT, CM, 05 Dec 2002 164 199 ; 165 ; $Id$ 200 ; Author: Craig B. Markwardt, NASA/GSFC Code 662, Greenbelt, MD 20770 201 ; craigm@lheamail.gsfc.nasa.gov 202 ; 203 ; @version $Id$ 166 204 ; 167 205 ;- 168 ; Copyright (C) 1998, 2000, 2002, Craig Markwardt169 ; This software is provided as is without any warranty whatsoever.170 ; Permission to use, copy, modify, and distribute modified or171 ; unmodified copies is granted, provided this copyright and disclaimer172 ; are included unchanged.173 ;-174 175 ;; Utility function, adapted from CMPRODUCT176 function cmapply_product, x177 ;178 compile_opt idl2, strictarrsubs179 ;180 sz = size(x)181 n = sz[1]182 183 while n GT 1 do begin184 if (n mod 2) EQ 1 then x[0,*] = x[0,*] * x[n-1,*]185 n2 = floor(n/2)186 x = x[0:n2-1,*] * x[n2:*,*]187 n = n2188 endwhile189 return, reform(x[0,*], /overwrite)190 end191 192 ;; Utility function, used to collect collaped dimensions193 pro cmapply_redim, newarr, dimapply, dimkeep, nkeep, totcol, totkeep194 ;195 compile_opt idl2, strictarrsubs196 ;197 sz = size(newarr)198 ;; First task: rearrange dimensions so that the dimensions199 ;; that are "kept" (ie, uncollapsed) are at the back200 dimkeep = where(histogram(dimapply,min=1,max=sz[0]) ne 1, nkeep)201 if nkeep EQ 0 then return202 203 newarr = transpose(temporary(newarr), [dimapply-1, dimkeep])204 ;; totcol is the total number of collapsed elements205 totcol = sz[dimapply[0]]206 for i = 1, n_elements(dimapply)-1 do totcol = totcol * sz[dimapply[i]]207 totkeep = sz[dimkeep[0]+1]208 for i = 1, n_elements(dimkeep)-1 do totkeep = totkeep * sz[dimkeep[i]+1]209 210 ;; this new array has two dimensions:211 ;; * the first, all elements that will be collapsed212 ;; * the second, all dimensions that will be preserved213 ;; (the ordering is so that all elements to be collapsed are214 ;; adjacent in memory)215 newarr = reform(newarr, [totcol, totkeep], /overwrite)216 end217 218 ;; Main function219 206 function cmapply, op, array, dimapply, double=dbl, type=type, $ 220 207 functargs=functargs, nocatch=nocatch
Note: See TracChangeset
for help on using the changeset viewer.