RWalib C Array Library User Guide > Linear Algebra > alibTrace
  

alibTrace
Returns the tensor contraction along two dimensions of an array. For two-dimensional arrays this is the matrix trace. The PV-WAVE API for this routine is the ARRAYTRACE Function.
Prototypes
void alibTraceb( wvlong n, wvlong *d, wvlong *e, UCHAR *p, float *q )
void alibTraces( wvlong n, wvlong *d, wvlong *e, short *p, float *q )
void alibTracei( wvlong n, wvlong *d, wvlong *e, int *p, float *q )
void alibTracel( wvlong n, wvlong *d, wvlong *e, wvlong *p, float *q )
void alibTracef( wvlong n, wvlong *d, wvlong *e, float *p, float *q )
void alibTraced( wvlong n, wvlong *d, wvlong *e, double *p, double *q )
void alibTracec( wvlong n, wvlong *d, wvlong *e, COMPLEX *p, COMPLEX *q )
void alibTracez( wvlong n, wvlong *d, wvlong *e, DCOMPLEX *p, DCOMPLEX *q )
Parameters
n — (Input) The number of dimensions in the array.
*d — (Input) The n-element array of dimensions for the array.
*e — (Input) The 2-element array designating the two contracted dimensions. For convenience, the pair {0,1} can be designated by inputting e as NULL.
*p — (Input) The source array, i.e., the n-dimensional array of dimensions d.
*q — (Input/Output) The destination array. On return, array q contains the result of the tensor contraction. For n=2, this is the matrix trace, i.e., the sum of the main diagonal of the matrix. For n>2, e[0] and e[1] define the two-dimensional subarrays whose traces are stored in an array of the n–2 dimensions obtained by excluding dimensions e[0] and e[1] from d. If the input is of integer type, computations and output are of type float. Otherwise, computations and output are of the same type as that of the input.
Example
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "alib.h"
void main() {
   /* array of dimensions to use for all the examples */
   wvlong dm[4]={3,3,3,3}, e[2];
   /* source data for all the examples */
   UCHAR b0[81];
   /* output array to use for all the examples */
   float b[9];
   /* initialize the source data */
   alibinit( NULL, NULL, NULL, NULL );
   alibIndexb( 81, 1, 1, b0 );
   printf( "\n\n show (3,3) matrix b0" );
      alibPrintArrayb( 1, 1, 3, 3, b0, NULL );
   printf( "\n\n print the trace of matrix b0" );
      alibTraceb( 2, dm, NULL, b0, b );
      alibPrintArrayf( 1, 1, 1, 1, b, NULL );
   printf( "\n\n show (3,3,3) array b0" );
      alibPrintArrayb( 1, 3, 3, 3, b0, NULL );
   printf( "\n\n contract b0 over dimensions 0 and 1" );
      alibTraceb( 3, dm, NULL, b0, b );
      alibPrintArrayf( 1, 1, 1, 3, b, NULL );
   printf( "\n\n show (3,3,3) array b0" );
      alibPrintArrayb( 1, 3, 3, 3, b0, NULL );
   printf( "\n\n contract b0 over dimensions 0 and 2" );
      e[0] = 0;
      e[1] = 2;
      alibTraceb( 3, dm, e, b0, b );
      alibPrintArrayf( 1, 1, 3, 1, b, NULL );
   printf( "\n\n show (3,3,3) array b0" );
      alibPrintArrayb( 1, 3, 3, 3, b0, NULL );
   printf( "\n\n contract b0 over dimensions 1 and 2" );
      e[0] = 1;  
      e[1] = 2;
      alibTraceb( 3, dm, e, b0, b );
      alibPrintArrayf( 1, 3, 1, 1, b, NULL );
   printf( "\n\n show (3,3,3,3) array b0" );
      alibPrintArrayb( 3, 3, 3, 3, b0, NULL );
   printf( "\n\n contract b0 over dimensions 0 and 1" );
      alibTraceb( 4, dm, NULL, b0, b );
      alibPrintArrayf( 1, 1, 3, 3, b, NULL );
   printf( "\n\n show (3,3,3,3) array b0" );
      alibPrintArrayb( 3, 3, 3, 3, b0, NULL );
   printf( "\n\n contract b0 over dimensions 0 and 2" );
      e[0] = 0;
      e[1] = 2;
      alibTraceb( 4, dm, e, b0, b );
      alibPrintArrayf( 1, 3, 1, 3, b, NULL );
   printf( "\n\n show (3,3,3,3) array b0" );
      alibPrintArrayb( 3, 3, 3, 3, b0, NULL );
   printf( "\n\n contract b0 over dimensions 0 and 3" );
      e[0] = 0;
      e[1] = 3;
      alibTraceb( 4, dm, e, b0, b );
      alibPrintArrayf( 1, 3, 3, 1, b, NULL );
   printf( "\n\n show (3,3,3,3) array b0" );
      alibPrintArrayb( 3, 3, 3, 3, b0, NULL );
   printf( "\n\n contract b0 over dimensions 1 and 2" );
      e[0] = 1;
      e[1] = 2;
      alibTraceb( 4, dm, e, b0, b );
      alibPrintArrayf( 3, 1, 1, 3, b, NULL );
   printf( "\n\n show (3,3,3,3) array b0" );
      alibPrintArrayb( 3, 3, 3, 3, b0, NULL );
   printf( "\n\n contract b0 over dimensions 1 and 3" );
      e[0] = 1;
      e[1] = 3;
      alibTraceb( 4, dm, e, b0, b );
      alibPrintArrayf( 3, 1, 3, 1, b, NULL );
   printf( "\n\n show (3,3,3,3) array b0" );
      alibPrintArrayb( 3, 3, 3, 3, b0, NULL );
   printf( "\n\n contract b0 over dimensions 2 and 3" );
      e[0] = 2;
      e[1] = 3;
      alibTraceb( 4, dm, e, b0, b );
      alibPrintArrayf( 3, 3, 1, 1, b, NULL );
}
 
Output:
 
 show (3,3) matrix b0
 
   1   2   3
   4   5   6
   7   8   9
 
 print the trace of matrix b0
 
   1.500e+01
 
 show (3,3,3) array b0
 
   1   2   3
   4   5   6
   7   8   9
 
  10  11  12
  13  14  15
  16  17  18
 
  19  20  21
  22  23  24
  25  26  27
 
 contract b0 over dimensions 0 and 1
 
   3.900e+01   4.200e+01   4.500e+01
 
 show (3,3,3) array b0
 
   1   2   3
   4   5   6
   7   8   9
 
  10  11  12
  13  14  15
  16  17  18
 
  19  20  21
  22  23  24
  25  26  27
 
 contract b0 over dimensions 0 and 2
 
   3.300e+01
   4.200e+01
   5.100e+01
 
 show (3,3,3) array b0
 
   1   2   3
   4   5   6
   7   8   9
 
  10  11  12
  13  14  15
  16  17  18
 
  19  20  21
  22  23  24
  25  26  27
 
 contract b0 over dimensions 1 and 2
 
   1.500e+01
 
   4.200e+01
 
   6.900e+01
 
 show (3,3,3,3) array b0
 
   1   2   3
   4   5   6
   7   8   9
 
  10  11  12
  13  14  15
  16  17  18
 
  19  20  21
  22  23  24
  25  26  27
 
  28  29  30
  31  32  33
  34  35  36
 
  37  38  39
  40  41  42
  43  44  45
 
  46  47  48
  49  50  51
  52  53  54
 
  55  56  57
  58  59  60
  61  62  63
 
  64  65  66
  67  68  69
  70  71  72
 
  73  74  75
  76  77  78
  79  80  81
 
 contract b0 over dimensions 0 and 1
 
   1.110e+02   1.140e+02   1.170e+02
   1.200e+02   1.230e+02   1.260e+02
   1.290e+02   1.320e+02   1.350e+02
 
 show (3,3,3,3) array b0
 
   1   2   3
   4   5   6
   7   8   9
 
  10  11  12
  13  14  15
  16  17  18
 
  19  20  21
  22  23  24
  25  26  27
 
  28  29  30
  31  32  33
  34  35  36
 
  37  38  39
  40  41  42
  43  44  45
 
  46  47  48
  49  50  51
  52  53  54
 
  55  56  57
  58  59  60
  61  62  63
 
  64  65  66
  67  68  69
  70  71  72
 
  73  74  75
  76  77  78
  79  80  81
 
 contract b0 over dimensions 0 and 2
 
   9.300e+01   9.600e+01   9.900e+01
 
   1.200e+02   1.230e+02   1.260e+02
 
   1.470e+02   1.500e+02   1.530e+02
 
 show (3,3,3,3) array b0
 
   1   2   3
   4   5   6
   7   8   9
 
  10  11  12
  13  14  15
  16  17  18
 
  19  20  21
  22  23  24
  25  26  27
 
  28  29  30
  31  32  33
  34  35  36
 
  37  38  39
  40  41  42
  43  44  45
 
  46  47  48
  49  50  51
  52  53  54
 
  55  56  57
  58  59  60
  61  62  63
 
  64  65  66
  67  68  69
  70  71  72
 
  73  74  75
  76  77  78
  79  80  81
 
 contract b0 over dimensions 0 and 3
 
   8.700e+01
   9.600e+01
   1.050e+02
 
   1.140e+02
   1.230e+02
   1.320e+02
 
   1.410e+02
   1.500e+02
   1.590e+02
 
 show (3,3,3,3) array b0
 
   1   2   3
   4   5   6
   7   8   9
 
  10  11  12
  13  14  15
  16  17  18
 
  19  20  21
  22  23  24
  25  26  27
 
  28  29  30
  31  32  33
  34  35  36
 
  37  38  39
  40  41  42
  43  44  45
 
  46  47  48
  49  50  51
  52  53  54
 
  55  56  57
  58  59  60
  61  62  63
 
  64  65  66
  67  68  69
  70  71  72
 
  73  74  75
  76  77  78
  79  80  81
 
 contract b0 over dimensions 1 and 2
 
   3.900e+01   4.200e+01   4.500e+01
 
   1.200e+02   1.230e+02   1.260e+02
 
   2.010e+02   2.040e+02   2.070e+02
 
 show (3,3,3,3) array b0
 
   1   2   3
   4   5   6
   7   8   9
 
  10  11  12
  13  14  15
  16  17  18
 
  19  20  21
  22  23  24
  25  26  27
 
  28  29  30
  31  32  33
  34  35  36
 
  37  38  39
  40  41  42
  43  44  45
 
  46  47  48
  49  50  51
  52  53  54
 
  55  56  57
  58  59  60
  61  62  63
 
  64  65  66
  67  68  69
  70  71  72
 
  73  74  75
  76  77  78
  79  80  81
 
 contract b0 over dimensions 1 and 3
 
   3.300e+01
   4.200e+01
   5.100e+01
 
   1.140e+02
   1.230e+02
   1.320e+02
 
   1.950e+02
   2.040e+02
   2.130e+02
 
 show (3,3,3,3) array b0
 
   1   2   3
   4   5   6
   7   8   9
 
  10  11  12
  13  14  15
  16  17  18
 
  19  20  21
  22  23  24
  25  26  27
 
  28  29  30
  31  32  33
  34  35  36
 
  37  38  39
  40  41  42
  43  44  45
 
  46  47  48
  49  50  51
  52  53  54
 
  55  56  57
  58  59  60
  61  62  63
 
  64  65  66
  67  68  69
  70  71  72
 
  73  74  75
  76  77  78
  79  80  81
 
 contract b0 over dimensions 2 and 3
 
   1.500e+01
 
   4.200e+01
 
   6.900e+01
 
   9.600e+01
 
   1.230e+02
 
   1.500e+02
 
   1.770e+02
 
   2.040e+02
 
   2.310e+02
 

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