RWalib C Array Library User Guide > Searches and Intersections > alibIntersect
  

alibIntersect
Row-wise intersection and complement for two matrices (or column vectors). Although there is no prototype for complex data, the example section shows how complex data is easily handled with casts to real data of the same precision. The PV-WAVE API for this routine is the WHEREINVEC Function and WHEREIN Function.
Prototypes
wvlong alibIntersectb( wvlong h, wvlong m, wvlong n, UCHAR *p, UCHAR *q, wvlong *i, wvlong *j, UCHAR *d, UCHAR *s, UCHAR *r )
wvlong alibIntersects( wvlong h, wvlong m, wvlong n, short *p, short *q, wvlong *i, wvlong *j, short *d, UCHAR *s, UCHAR *r )
wvlong alibIntersecti( wvlong h, wvlong m, wvlong n, int *p, int *q, wvlong *i, wvlong *j, int *d, UCHAR *s, UCHAR *r )
wvlong alibIntersectl( wvlong h, wvlong m, wvlong n, wvlong *p, wvlong *q, wvlong *i, wvlong *j, wvlong *d, UCHAR *s, UCHAR *r )
wvlong alibIntersectf( wvlong h, wvlong m, wvlong n, float *p, float *q, wvlong *i, wvlong *j, float *d, UCHAR *s, UCHAR *r )
wvlong alibIntersectd( wvlong h, wvlong m, wvlong n, double *p, double *q, wvlong *i, wvlong *j, double *d, UCHAR *s, UCHAR *r )
Parameters
h — (Input) The number of columns in the matrices (h=1 for column vectors).
m — (Input) The number of rows in the first matrix (or column vector).
n — (Input) The number of rows in the second matrix (or column vector).
*p — (Input) The (m,h) source array.
*q — (Input) The (n,h) source array.
*i — (Input/Output) The k-element array where k is the greater of m and h. On output, array i contains the indices of the rows of p which are also in q.
*j — (Input/Output) The k-element array where k is the greater of m and h. On output, array j contains the indices of the rows of p which are not in q.
*d — (Input/Output) The m*h-element array. On output, array d contains the rows of p which are also in q. These rows are in the original order and may contain repetitions if p has repetitions. To obtain the sorted unique rows, see alibUnique.
*s — (Input/Output) The m-element array.
*r — (Input/Output) The m-element array. On return, array r contains only 0's and one's, where r[i] is one if and only if the ith row of p appears in q.
Return value
The number of rows in p which also appear in q, i.e., the number one's in r.
Example
Here we show an example for vectors, an example for matrices, and an example for complex data. Single/double precision (*,k) complex arrays can be handled with alibIntersectf/alibIntersectd by casting p, q, and d to float/double, and by inputting h = 2k.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "alib.h"
void main() {
   /* make some data to use for a 1d, a 2d, and a 1d complex 
      example */
   short    ps[10]={6,0,5,2,1,5,3,1,4,3}, qs[7]={1,7,1,5,2,4,4};
   short    ps0[20]={6,0,0,1,5,2,1,3,5,2,1,3,4,4,2,5,3,6,3,7};
   DCOMPLEX pz0[10]={6,0,0,1,5,2,1,3,5,2,1,3,4,4,2,5,3,6,3,7};
   short    qs0[14]={1,3,6,1,1,3,5,2,2,1,4,5,4,4};
   DCOMPLEX qz0[ 7]={1,3,6,1,1,3,5,2,2,1,4,5,4,4};
   /* work arrays and output arrays big enough to use for all the 
      examples */
   short    ds[20];
   wvlong   ks, kd;
   DCOMPLEX dz[10];
   UCHAR  ss[10], sd[10];
   UCHAR  rs[10], rd[10];
   wvlong is[10], id[10];
   wvlong js[10], jd[10];
   printf( "\n\n show 10-element array ps and 7-element array qs" );
      alibinit( NULL, NULL, NULL, NULL );
      alibPrintArrays( 1, 1, 1, 10, ps, NULL );
      alibPrintArrays( 1, 1, 1,  7, qs, NULL );
   printf( "\n\n show the mask for intersection and complement of qs in ps" );
   ks = alibIntersects( 1, 10, 7, ps, qs, is, js, ds, ss, rs );
      alibPrintArrayb( 1, 1, 1, 10, rs, NULL );
   printf( "\n\n show the indices of the intersection" );
      alibPrintArrayl( 1, 1, 1, ks, is, NULL );
   printf( "\n\n show the indices of the complement" );
      alibPrintArrayl( 1, 1, 1, 10-ks, js, NULL );
   printf( "\n\n show the intersection itself" );
      alibPrintArrays( 1, 1, 1, ks, ds, NULL );
   printf( "\n\n show (10,2) matrix ps0 and (7,2) matrix qs0" );
   alibPrintArrays( 1, 1, 10, 2, ps0, NULL );
   alibPrintArrays( 1, 1,  7, 2, qs0, NULL );
   printf( "\n\n mask for row-wise intersection & complement of qs0 in ps0" );
   ks = alibIntersects( 2, 10, 7, ps0, qs0, is, js, ds, ss, rs );
      alibPrintArrayb( 1, 1, 1, 10, rs, NULL );
   printf( "\n\n show the indices of the intersection" );
      alibPrintArrayl( 1, 1, 1, ks, is, NULL );
   printf( "\n\n show the indices of the complement" );
      alibPrintArrayl( 1, 1, 1, 10-ks, js, NULL );
   printf( "\n\n show the intersection itself" );
      alibPrintArrays( 1, 1, ks, 2, ds, NULL );
   printf( "\n\n show 10-element and 7-element complex vectors pz0 and qz0" );
      alibPrintArrayz( 1, 1, 10, 1, pz0, NULL );
      alibPrintArrayz( 1, 1,  7, 1, qz0, NULL );
   printf( "\n\n show mask for intersection and complement of qz0 in pz0" );
   kd = alibIntersectd( 2, 10, 7, (double*)pz0, (double*)qz0,
                        id, jd, (double*)dz, sd, rd );
      alibPrintArrayb( 1, 1, 1, 10, rd, NULL );
   printf( "\n\n show the indices of the intersection" );
      alibPrintArrayl( 1, 1, 1, kd, id, NULL );
   printf( "\n\n show the indices of the complement" );
      alibPrintArrayl( 1, 1, 1, 10-kd, jd, NULL );
   printf( "\n\n show the intersection itself" );
      alibPrintArrayz( 1, 1, kd, 1, dz, NULL );
}
 
Output:
 
 show 10-element array ps and 7-element array qs
 
       6       0       5       2       1       5       3       1       4       3
 
       1       7       1       5       2       4       4
 
 show the mask for intersection and complement of qs in ps
 
   0   0   1   1   1   1   0   1   1   0
 
 show the indices of the intersection
 
                    2                    3                    4                    5                    7                    8
 
 show the indices of the complement
 
                    0                    1                    6                    9
 
 show the intersection itself
 
       5       2       1       5       1       4
 
 show (10,2) matrix ps0 and (7,2) matrix qs0
 
       6       0
       0       1
       5       2
       1       3
       5       2
       1       3
       4       4
       2       5
       3       6
       3       7
 
       1       3
       6       1
       1       3
       5       2
       2       1
       4       5
       4       4
 
 mask for row-wise intersection & complement of qs0 in ps0
 
   0   0   1   1   1   1   1   0   0   0
 
 show the indices of the intersection
 
                    2                    3                    4                    5                    6
 
 show the indices of the complement
 
                    0                    1                    7                    8                    9
 
 show the intersection itself
 
       5       2
       1       3
       5       2
       1       3
       4       4
 
 show 10-element and 7-element complex vectors pz0 and qz0
 
(  6.000e+00,  0.000e+00) 
(  0.000e+00,  1.000e+00) 
(  5.000e+00,  2.000e+00) 
(  1.000e+00,  3.000e+00) 
(  5.000e+00,  2.000e+00) 
(  1.000e+00,  3.000e+00) 
(  4.000e+00,  4.000e+00) 
(  2.000e+00,  5.000e+00) 
(  3.000e+00,  6.000e+00) 
(  3.000e+00,  7.000e+00) 
 
(  1.000e+00,  3.000e+00) 
(  6.000e+00,  1.000e+00) 
(  1.000e+00,  3.000e+00) 
(  5.000e+00,  2.000e+00) 
(  2.000e+00,  1.000e+00) 
(  4.000e+00,  5.000e+00) 
(  4.000e+00,  4.000e+00) 
 
 show mask for intersection and complement of qz0 in pz0
 
   0   0   1   1   1   1   1   0   0   0
 
 show the indices of the intersection
 
                    2                    3                    4                    5                    6
 
 show the indices of the complement
 
                    0                    1                    7                    8                    9
 
 show the intersection itself
 
(  5.000e+00,  2.000e+00) 
(  1.000e+00,  3.000e+00) 
(  5.000e+00,  2.000e+00) 
(  1.000e+00,  3.000e+00) 
(  4.000e+00,  4.000e+00) 

Version 2017.1
Copyright © 2019, Rogue Wave Software, Inc. All Rights Reserved.