source: trunk/SRC/Matrix/inter.pro @ 157

Last change on this file since 157 was 157, checked in by navarro, 18 years ago

header improvements + xxx doc

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.1 KB
Line 
1;------------------------------------------------------------
2;------------------------------------------------------------
3;------------------------------------------------------------
4;+
5;
6; @file_comments
7; calculate the intersection between 2 matrixes of whole numbers
8;
9; @categories
10; Calculation
11;
12; @param a {in}{required} arrays of positive integers, which need not to be
13; sorted. Duplicate elements are ignored, as they have noeffect on the
14; result
15;
16; @param b {in}{required} see a
17;
18; @returns tableau
19;
20; @restrictions The empty set is denoted by an array with the first element equal to
21; -1.
22;
23; @restrictions These functions will not be efficient on sparse sets with wide
24; ranges, as they trade memory for efficiency. The HISTOGRAM function
25; is used, which creates arrays of size equal to the range of the
26; resulting set.
27;
28; @examples  a = [2,4,6,8]
29;   b = [6,1,3,2]
30;   inter(a,b) = [ 2, 6]       ; Common elements
31;
32; @history  http://www.dfanning.com/tips/set_operations.html
33;
34; @version $Id$
35;
36;-
37;------------------------------------------------------------
38;------------------------------------------------------------
39;------------------------------------------------------------
40
41FUNCTION inter, a, b
42;
43;
44  compile_opt idl2, strictarrsubs
45;
46   case 1 of
47      n_elements(a) EQ 0:return,  -1
48      n_elements(b) EQ 0:return,  -1
49      n_elements(a) EQ 1 AND n_elements(b) NE 1: $
50       if (where(b EQ a[0]))[0] EQ -1 then return, -1 ELSE return,  a[0]
51      n_elements(b) EQ 1 AND n_elements(a) NE 1: $
52       if (where(a EQ b[0]))[0] EQ -1 then return, -1 ELSE return,  b[0]
53      n_elements(a) EQ 1 AND n_elements(b) EQ 1: $
54       if (where(a[0] EQ b[0]))[0] EQ -1 then return, -1 ELSE return,  a[0]
55      ELSE:
56   ENDCASE
57;
58minab = Min(a, Max=maxa) > Min(b, Max=maxb) ;Only need intersection of ranges
59maxab = maxa < maxb
60
61   ; If either set is empty, or their ranges don't intersect: result = NULL.
62
63IF maxab LT minab OR maxab LT 0 THEN RETURN, -1
64r = Where(Histogram(a, Min=minab, Max=maxab) $
65          *Histogram(b, Min=minab, Max=maxab), count)
66
67IF count EQ 0 THEN RETURN, -1 ELSE RETURN, r + minab
68END
Note: See TracBrowser for help on using the repository browser.