Changeset 136 for trunk/SRC/Matrix/cmset_op.pro
- Timestamp:
- 07/10/06 17:20:19 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SRC/Matrix/cmset_op.pro
r134 r136 2 2 ; @hidden 3 3 ; 4 ; @file_comments 5 ; Simplified version of CMSET_OP_UNIQ which sorts, and takes the 6 ; "first" value, whatever that may mean. 7 ; 4 8 ; @todo seb 5 9 ;- 6 10 ; 7 ;; Simplified version of CMSET_OP_UNIQ which sorts, and takes the8 ;; "first" value, whatever that may mean.9 11 function cmset_op_uniq, a 10 12 ; … … 22 24 ;+ 23 25 ; 24 ; @file_comments 26 ; @file_comments 25 27 ; Performs an AND, OR, or XOR operation between two sets 26 28 ; 27 ; Description: SET_OP performs three common operations between two sets.The28 ; 29 ; Description: SET_OP performs three common operations between two sets. The 30 ; three supported functions of OP are: 29 31 ; 30 32 ; OP Meaning 31 33 ; 'AND' - to find the intersection of A and B; 32 34 ; 'OR' - to find the union of A and B; 33 ; 'XOR' - to find the those elements who are members of A or B 35 ; 'XOR' - to find the those elements who are members of A or B 34 36 ; but not both; 35 37 ; 36 38 ; Sets as defined here are one dimensional arrays composed of 37 ; numeric or string types. 39 ; numeric or string types. Comparisons of equality between elements 38 40 ; are done using the IDL EQ operator. 39 41 ; 40 42 ; The complements of either set can be taken as well, by using the 41 ; NOT1 and NOT2 keywords. 43 ; NOT1 and NOT2 keywords. For example, it may be desireable to find 42 44 ; the elements in A but not B, or B but not A (they are different!). 43 45 ; The following IDL expressions achieve each of those effects: … … 52 54 ; 53 55 ; NOT1 and NOT2 can only be set for the 'AND' operator, and never 54 ; simultaneously. 56 ; simultaneously. This is because the results of an operation with 55 57 ; 'OR' or 'XOR' and any combination of NOTs -- or with 'AND' and 56 58 ; both NOTs -- formally cannot produce a defined result. 57 59 ; 58 ; The implementation depends on the type of operands. 59 ; types, a fast technique using HISTOGRAM is used. 60 ; The implementation depends on the type of operands. For integer 61 ; types, a fast technique using HISTOGRAM is used. However, this 60 62 ; algorithm becomes inefficient when the dynamic range in the data 61 ; is large. 62 ; based on SORT() is used. 63 ; is large. For those cases, and for other data types, a technique 64 ; based on SORT() is used. Thus the compute time should scale 63 65 ; roughly as (A+B)*ALOG(A+B) or better, rather than (A*B) for the 64 ; brute force approach. 66 ; brute force approach. For large arrays this is a significant 65 67 ; benefit. 66 68 ; 67 69 ; @categories array 68 70 ; 69 ; @param A {in}{required} The two sets to be operated on. A one dimensional array of 70 ; either numeric or string type. A and B must be of the same 71 ; type. Empty sets are permitted, and are either represented 72 ; as an undefined variable, or by setting EMPTY1 or EMPTY2. 73 ; 74 ; @param B {in}{required} See A 75 ; 76 ; @param OP {in}{required} a string, the operation to be performed. Must be one of 77 ; 'AND', 'OR' or 'XOR' (lower or mixed case is permitted). 78 ; Other operations will cause an error message to be produced. 79 ; 80 ; @keyword NOT1 If set and OP is 'AND', then the complement of A (for 81 ; NOT1) or B (for NOT2) will be used in the operation. 82 ; NOT1 and NOT2 cannot be set simultaneously. 83 ; 84 ; @keyword NOT2 See NOT1 85 ; 86 ; @keyword EMPTY1 If set, then A (for EMPTY1) or B (for EMPTY2) are 87 ; assumed to be the empty set. The actual values 88 ; passed as A or B are then ignored. 89 ; 90 ; @keyword EMPTY2 See EMPTY1 91 ; 92 ; @keyword INDEX if set, then return a list of indices instead of the array 93 ; values themselves. The "slower" set operations are always 94 ; performed in this case. 95 ; 96 ; The indices refer to the *combined* array [A,B]. To 97 ; clarify, in the following call: I = CMSET_OP(..., /INDEX); 98 ; returned values from 0 to NA-1 refer to A[I], and values 99 ; from NA to NA+NB-1 refer to B[I-NA]. 100 ; 101 ; @keyword COUNT upon return, the number of elements in the result set. 102 ; This is only important when the result set is the empty 103 ; set, in which case COUNT is set to zero. 104 ; 105 ; @returns The resulting set as a one-dimensional array. The set may be 106 ; represented by either an array of data values (default), or an 107 ; array of indices (if INDEX is set). Duplicate elements, if any, 108 ; are removed, and element order may not be preserved. 109 ; 110 ; The empty set is represented as a return value of -1L, and COUNT 111 ; is set to zero. Note that the only way to recognize the empty set 112 ; is to examine COUNT. 71 ; @param A {in}{required} 72 ; The two sets to be operated on. A one dimensional array of 73 ; either numeric or string type. A and B must be of the same 74 ; type. Empty sets are permitted, and are either represented 75 ; as an undefined variable, or by setting EMPTY1 or EMPTY2. 76 ; 77 ; @param B {in}{required} 78 ; See A 79 ; 80 ; @param OP0 {in}{required} 81 ; a string, the operation to be performed. Must be one of 82 ; 'AND', 'OR' or 'XOR' (lower or mixed case is permitted). 83 ; Other operations will cause an error message to be produced. 84 ; 85 ; @keyword NOT1 86 ; If set and OP is 'AND', then the complement of A (for 87 ; NOT1) or B (for NOT2) will be used in the operation. 88 ; NOT1 and NOT2 cannot be set simultaneously. 89 ; 90 ; @keyword NOT2 91 ; See NOT1 92 ; 93 ; @keyword EMPTY1 94 ; If set, then A (for EMPTY1) or B (for EMPTY2) are 95 ; assumed to be the empty set. The actual values 96 ; passed as A or B are then ignored. 97 ; 98 ; @keyword EMPTY2 99 ; See EMPTY1 100 ; 101 ; @keyword INDEX 102 ; if set, then return a list of indices instead of the array 103 ; values themselves. The "slower" set operations are always 104 ; performed in this case. 105 ; 106 ; The indices refer to the *combined* array [A,B]. To 107 ; clarify, in the following call: I = CMSET_OP(..., /INDEX); 108 ; returned values from 0 to NA-1 refer to A[I], and values 109 ; from NA to NA+NB-1 refer to B[I-NA]. 110 ; 111 ; @keyword COUNT 112 ; upon return, the number of elements in the result set. 113 ; This is only important when the result set is the empty 114 ; set, in which case COUNT is set to zero. 115 ; 116 ; @returns 117 ; The resulting set as a one-dimensional array. The set may be 118 ; represented by either an array of data values (default), or an 119 ; array of indices (if INDEX is set). Duplicate elements, if any, 120 ; are removed, and element order may not be preserved. 121 ; 122 ; The empty set is represented as a return value of -1L, and COUNT 123 ; is set to zero. Note that the only way to recognize the empty set 124 ; is to examine COUNT. 113 125 ; 114 126 ; SEE ALSO: 115 127 ; 116 ; 128 ; SET_UTILS.PRO by RSI 117 129 ; 118 130 ; @history Written, CM, 23 Feb 2000 … … 145 157 ; 146 158 ; Author: Craig B. Markwardt, NASA/GSFC Code 662, Greenbelt, MD 20770 147 ; craigm@lheamail.gsfc.nasa.gov 148 ; 149 ; @version $Id: cmset_op.pro,v 1.6 2006/01/16 19:45:22 craigm Exp $ 150 ; 151 ; @examples Utility function, similar to UNIQ, but allowing choice of taking 159 ; craigm\@lheamail.gsfc.nasa.gov 160 ; 161 ; @version $Id$ 162 ; 163 ; @examples 164 ; Utility function, similar to UNIQ, but allowing choice of taking 152 165 ; first or last unique element, or non-unique elements. 153 166 ; Unfortunately this doesn't work because of implementation dependent … … 235 248 if n2 GT 0 then b1 = cmset_op_uniq(b) 236 249 n1 = n_elements(a1) < n1 & n2 = n_elements(b1) < n2 237 case op of 250 case op of 238 251 'OR': if n1 EQ 0 then goto, RET_A1 else goto, RET_B1 239 252 'XOR': if n1 EQ 0 then goto, RET_B1 else goto, RET_A1 … … 255 268 if kind then begin 256 269 if count GT 0 then return, b1+n1 else return, -1L 257 endif 270 endif 258 271 if count GT 0 then return, b[b1] else return, -1L 259 272 endif … … 294 307 ;; String and real types, or large int arrays 295 308 SLOW_SET_OP: 296 case op of 309 case op of 297 310 'OR': begin 298 311 uu = [a,b] ;; OR is simple; just take unique values … … 394 407 395 408 ;; Compute NOT cases 396 if keyword_set(not1) then ha = 1b - ha 409 if keyword_set(not1) then ha = 1b - ha 397 410 if keyword_set(not2) then hb = 1b - hb 398 case op of 411 case op of 399 412 ;; Boolean operations 400 'AND': mask = temporary(ha) AND temporary(hb) 413 'AND': mask = temporary(ha) AND temporary(hb) 401 414 'OR': mask = temporary(ha) OR temporary(hb) 402 415 'XOR': mask = temporary(ha) XOR temporary(hb) … … 405 418 wh = where(temporary(mask), count) 406 419 if count EQ 0 then return, -1L 407 420 408 421 result = temporary(wh+minn) 409 422 if tp1 NE tp2 then return, result
Note: See TracChangeset
for help on using the changeset viewer.