alibVecOverArr
Broadcasts a vector operation over an array. This routine applies a binary vector operation to an
n-element vector and each vector along an
n-element dimension of an array, resulting in a second array of the same dimensions. For example, it can be used to add a vector to each row or column of a matrix. More generally, it can be used to apply a binary vector operation to an
m-dimensional array and each one of a set of parallel
m-dimensional slices through a second array, e.g., to add a matrix to each one of a stack of matrices. The only restriction in this more general context is that each slice must be contiguous in memory, i.e., must occupy contiguous dimensions within the higher dimensional array. The PV-WAVE API for this routine is the
vecOverArr Functions.
Prototypes
void alibVecOverArrb( void (*f)(), wvlong k, wvlong m, wvlong n, wvlong *d, UCHAR *p, UCHAR *q, UCHAR *r, UCHAR *s )
void alibVecOverArrs( void (*f)(), wvlong k, wvlong m, wvlong n, wvlong *d, short *p, short *q, short *r, UCHAR *s )
void alibVecOverArri( void (*f)(), wvlong k, wvlong m, wvlong n, wvlong *d, int *p, int *q, int *r, UCHAR *s )
void alibVecOverArrl( void (*f)(), wvlong k, wvlong m, wvlong n, wvlong *d, wvlong *p, wvlong *q, wvlong *r, UCHAR *s )
void alibVecOverArrf( void (*f)(), wvlong k, wvlong m, wvlong n, wvlong *d, float *p, float *q, float *r, UCHAR *s )
void alibVecOverArrd( void (*f)(), wvlong k, wvlong m, wvlong n, wvlong *d, double *p, double *q, double *r, UCHAR *s )
void alibVecOverArrc( void (*f)(), wvlong k, wvlong m, wvlong n, wvlong *d, COMPLEX *p, COMPLEX *q, COMPLEX *r, UCHAR *s )
void alibVecOverArrz( void (*f)(), wvlong k, wvlong m, wvlong n, wvlong *d, DCOMPLEX *p, DCOMPLEX *q, DCOMPLEX *r, UCHAR *s )
Parameters
(*f)() — (Input) The name of one of the alib functions:
where the asterisk represents the letter-code for a supported data-type. The data-type for f() must of course match that of alibVecOverArr.
k — (Input) The leading dimension (>=0) of the slices on which f operates. If the slices are one-dimensional (vectors), k is the dimension parallel to the vectors. If the slices are m-dimensional, they occupy dimensions k through k + m – 1.
m — (Input) The flag 0/1 designating that the first/second source array is interpreted as the higher dimensional of the two. The flag is needed to fully accommodate non-commutative operations, e.g., to allow for A-x as well as x-A.
n — (Input) The number of dimensions in the higher dimensional array minus number of dimensions in a slice, plus one. So if the slices are one-dimensional (vectors) then n is the number of dimensions in the array containing the vectors, and more generally, for m-dimensional slices in a p-dimensional array, n = p – m + 1.
*d — (Input) The n-element array of dimensions for the higher dimensional array but with slice dimensions collapsed into a single dimension. So if the slices are one-dimensional (vectors) then the d[i] are just the dimensions of the array, and more generally, for m-dimensional slices in an array of p dimensions D,
d = { D(0), ..., D(k-1), D(k)*...*D(k+m-1), D(k+m), ..., D(p-1) }
where D(k)*...*D(k+m–1) is the product of the slice dimensions.
*p — (Input) The pointer to the first source array. This array, or part of it, is the first operand in each call to f().
*q — (Input) The pointer to the second source array. This array, or part of it, is the second operand in each call to f().
*r — (Input/Output) NULL if f() is one of alibEq*, alibNe*, alibLe*, alibGe*, alibLt*, or alibGt*. Otherwise, r is a destination array with the same dimensions as the higher dimensional source array. On return, array r contains the result of applying f() to the lower dimensional source array and each slice of the higher dimensional source array.
*s — (Input/Output) NULL if r is not NULL. Otherwise, s is a destination array with same dimensions as the higher dimensional source array. On return, array s holds the result of applying f() to the lower dimensional source array and each slice of the higher dimensional source array.
Example
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "alib.h"
void main() {
/* array of dimensions to use for 2d and 3d examples */
wvlong dim2[2]={2,3}, dim3[3]={2,4,3}, dim4[2]={8,3}, dim5[2]={2,12};
/* input and output arrays for the examples */
UCHAR sb[24];
short s[24], s0[24], s1[12];
/* make up some data for the input arrays */
alibinit( NULL, NULL, NULL, NULL );
alibIndexs( 24, 0, 1, s0 );
alibIndexs( 12, 2, 1, s1 );
printf( "\n\n show (2,3) matrix s0 and 2-element column vector s1" );
alibPrintArrays( 1, 1, 2, 3, s0, NULL );
alibPrintArrays( 1, 1, 2, 1, s1, NULL );
printf( "\n\n apply alibLe to each column of matrix s0 and vector s1" );
alibVecOverArrs( alibLes, 0, 0, 2, dim2, s0, s1, NULL, sb );
alibPrintArrayb( 1, 1, 2, 3, sb, NULL );
printf( "\n\n subtract vector s1 from each column of matrix s0" );
alibVecOverArrs( alibSubts, 0, 0, 2, dim2, s0, s1, s, NULL );
alibPrintArrays( 1, 1, 2, 3, s, NULL );
printf( "\n\n apply alibLe to vector s1 and each column of matrix s0" );
alibVecOverArrs( alibLes, 0, 1, 2, dim2, s1, s0, NULL, sb );
alibPrintArrayb( 1, 1, 2, 3, sb, NULL );
printf( "\n\n subtract each column of matrix s0 from vector s1" );
alibVecOverArrs( alibSubts, 0, 1, 2, dim2, s1, s0, s, NULL );
alibPrintArrays( 1, 1, 2, 3, s, NULL );
printf( "\n\n show (2,3) matrix s0 and 3-element row vector s1" );
alibPrintArrays( 1, 1, 2, 3, s0, NULL );
alibPrintArrays( 1, 1, 1, 3, s1, NULL );
printf( "\n\n apply alibLe to each row of matrix s0 and vector s1" );
alibVecOverArrs( alibLes, 1, 0, 2, dim2, s0, s1, NULL, sb );
alibPrintArrayb( 1, 1, 2, 3, sb, NULL );
printf( "\n\n subtract vector s1 from each row of matrix s0" );
alibVecOverArrs( alibSubts, 1, 0, 2, dim2, s0, s1, s, NULL );
alibPrintArrays( 1, 1, 2, 3, s, NULL );
printf( "\n\n apply alibLe to vector s1 and each row of matrix s0" );
alibVecOverArrs( alibLes, 1, 1, 2, dim2, s1, s0, NULL, sb );
alibPrintArrayb( 1, 1, 2, 3, sb, NULL );
printf( "\n\n subtract each row of matrix s0 from vector s1" );
alibVecOverArrs( alibSubts, 1, 1, 2, dim2, s1, s0, s, NULL );
alibPrintArrays( 1, 1, 2, 3, s, NULL );
printf( "\n\n show (2,4,3) array s0 and (2,1,1) vector s1" );
alibPrintArrays( 1, 2, 4, 3, s0, NULL );
alibPrintArrays( 1, 2, 1, 1, s1, NULL );
printf( "\n\n subtract vector s1 from each column along dim-0 of s0" );
alibVecOverArrs( alibSubts, 0, 0, 3, dim3, s0, s1, s, NULL );
alibPrintArrays( 1, 2, 4, 3, s, NULL );
printf( "\n\n subtract each column along dim-0 of s0 from vector s1" );
alibVecOverArrs( alibSubts, 0, 1, 3, dim3, s1, s0, s, NULL );
alibPrintArrays( 1, 2, 4, 3, s, NULL );
printf( "\n\n show (2,4,3) array s0 and (1,4,1) vector s1" );
alibPrintArrays( 1, 2, 4, 3, s0, NULL );
alibPrintArrays( 1, 1, 4, 1, s1, NULL );
printf( "\n\n subtract vector s1 from each column along dim-1 of s0" );
alibVecOverArrs( alibSubts, 1, 0, 3, dim3, s0, s1, s, NULL );
alibPrintArrays( 1, 2, 4, 3, s, NULL );
printf( "\n\n subtract each column along dim-1 of s0 from vector s1" );
alibVecOverArrs( alibSubts, 1, 1, 3, dim3, s1, s0, s, NULL );
alibPrintArrays( 1, 2, 4, 3, s, NULL );
printf( "\n\n show (2,4,3) array s0 and (1,1,3) vector s1" );
alibPrintArrays( 1, 2, 4, 3, s0, NULL );
alibPrintArrays( 1, 1, 1, 3, s1, NULL );
printf( "\n\n subtract vector s1 from each row along dim-2 of s0" );
alibVecOverArrs( alibSubts, 2, 0, 3, dim3, s0, s1, s, NULL );
alibPrintArrays( 1, 2, 4, 3, s, NULL );
printf( "\n\n subtract each row along dim-2 of s0 from vector s1" );
alibVecOverArrs( alibSubts, 2, 1, 3, dim3, s1, s0, s, NULL );
alibPrintArrays( 1, 2, 4, 3, s, NULL );
printf( "\n\n show (2,4,3) array s0 and (2,4,1) matrix s1" );
alibPrintArrays( 1, 2, 4, 3, s0, NULL );
alibPrintArrays( 1, 2, 4, 1, s1, NULL );
printf( "\n\n subtract matrix s1 from each dims-0-1 slice of s0" );
alibVecOverArrs( alibSubts, 0, 0, 2, dim4, s0, s1, s, NULL );
alibPrintArrays( 1, 2, 4, 3, s, NULL );
printf( "\n\n show (2,4,3) array s0 and (1,4,3) matrix s1" );
alibPrintArrays( 1, 2, 4, 3, s0, NULL );
alibPrintArrays( 1, 1, 4, 3, s1, NULL );
printf( "\n\n subtract matrix s1 from each dims-1-2 slice of s0" );
alibVecOverArrs( alibSubts, 1, 0, 2, dim5, s0, s1, s, NULL );
alibPrintArrays( 1, 2, 4, 3, s, NULL );
}
Output:
show (2,3) matrix s0 and 2-element column vector s1
0 1 2
3 4 5
2
3
apply alibLe to each column of matrix s0 and vector s1
1 1 1
1 0 0
subtract vector s1 from each column of matrix s0
-2 -1 0
0 1 2
apply alibLe to vector s1 and each column of matrix s0
0 0 1
1 1 1
subtract each column of matrix s0 from vector s1
2 1 0
0 -1 -2
show (2,3) matrix s0 and 3-element row vector s1
0 1 2
3 4 5
2 3 4
apply alibLe to each row of matrix s0 and vector s1
1 1 1
0 0 0
subtract vector s1 from each row of matrix s0
-2 -2 -2
1 1 1
apply alibLe to vector s1 and each row of matrix s0
0 0 0
1 1 1
subtract each row of matrix s0 from vector s1
2 2 2
-1 -1 -1
show (2,4,3) array s0 and (2,1,1) vector s1
0 1 2
3 4 5
6 7 8
9 10 11
12 13 14
15 16 17
18 19 20
21 22 23
2
3
subtract vector s1 from each column along dim-0 of s0
-2 -1 0
1 2 3
4 5 6
7 8 9
9 10 11
12 13 14
15 16 17
18 19 20
subtract each column along dim-0 of s0 from vector s1
2 1 0
-1 -2 -3
-4 -5 -6
-7 -8 -9
-9 -10 -11
-12 -13 -14
-15 -16 -17
-18 -19 -20
show (2,4,3) array s0 and (1,4,1) vector s1
0 1 2
3 4 5
6 7 8
9 10 11
12 13 14
15 16 17
18 19 20
21 22 23
2
3
4
5
subtract vector s1 from each column along dim-1 of s0
-2 -1 0
0 1 2
2 3 4
4 5 6
10 11 12
12 13 14
14 15 16
16 17 18
subtract each column along dim-1 of s0 from vector s1
2 1 0
0 -1 -2
-2 -3 -4
-4 -5 -6
-10 -11 -12
-12 -13 -14
-14 -15 -16
-16 -17 -18
show (2,4,3) array s0 and (1,1,3) vector s1
0 1 2
3 4 5
6 7 8
9 10 11
12 13 14
15 16 17
18 19 20
21 22 23
2 3 4
subtract vector s1 from each row along dim-2 of s0
-2 -2 -2
1 1 1
4 4 4
7 7 7
10 10 10
13 13 13
16 16 16
19 19 19
subtract each row along dim-2 of s0 from vector s1
2 2 2
-1 -1 -1
-4 -4 -4
-7 -7 -7
-10 -10 -10
-13 -13 -13
-16 -16 -16
-19 -19 -19
show (2,4,3) array s0 and (2,4,1) matrix s1
0 1 2
3 4 5
6 7 8
9 10 11
12 13 14
15 16 17
18 19 20
21 22 23
2
3
4
5
6
7
8
9
subtract matrix s1 from each dims-0-1 slice of s0
-2 -1 0
0 1 2
2 3 4
4 5 6
6 7 8
8 9 10
10 11 12
12 13 14
show (2,4,3) array s0 and (1,4,3) matrix s1
0 1 2
3 4 5
6 7 8
9 10 11
12 13 14
15 16 17
18 19 20
21 22 23
2 3 4
5 6 7
8 9 10
11 12 13
subtract matrix s1 from each dims-1-2 slice of s0
-2 -2 -2
-2 -2 -2
-2 -2 -2
-2 -2 -2
10 10 10
10 10 10
10 10 10
10 10 10
Version 2017.0
Copyright © 2017, Rogue Wave Software, Inc. All Rights Reserved.