Debian Codegraph @

tools/swig/test/Tensor.cxx

raw
1 #include <stdlib.h>
2 #include <math.h>
3 #include <iostream>
4 #include "Tensor.h"
5
6 // The following macro defines a family of functions that work with 3D
7 // arrays with the forms
8 //
9 // TYPE SNAMENorm( TYPE tensor[2][2][2]);
10 // TYPE SNAMEMax( TYPE * tensor, int slices, int rows, int cols);
11 // TYPE SNAMEMin( int slices, int rows, int cols TYPE * tensor);
12 // void SNAMEScale( TYPE tensor[3][3][3]);
13 // void SNAMEFloor( TYPE * array, int slices, int rows, int cols, TYPE floor);
14 // void SNAMECeil( int slices, int rows, int cols, TYPE * array, TYPE ceil);
15 // void SNAMELUSplit(TYPE in[2][2][2], TYPE lower[2][2][2], TYPE upper[2][2][2]);
16 //
17 // for any specified type TYPE (for example: short, unsigned int, long
18 // long, etc.) with given short name SNAME (for example: short, uint,
19 // longLong, etc.). The macro is then expanded for the given
20 // TYPE/SNAME pairs. The resulting functions are for testing numpy
21 // interfaces, respectively, for:
22 //
23 // * 3D input arrays, hard-coded length
24 // * 3D input arrays
25 // * 3D input arrays, data last
26 // * 3D in-place arrays, hard-coded lengths
27 // * 3D in-place arrays
28 // * 3D in-place arrays, data last
29 // * 3D argout arrays, hard-coded length
30 //
31 #define TEST_FUNCS(TYPE, SNAME) \
32 \
33 TYPE SNAME ## Norm(TYPE tensor[2][2][2]) { \
34 double result = 0; \
35 for (int k=0; k<2; ++k) \
36 for (int j=0; j<2; ++j) \
37 for (int i=0; i<2; ++i) \
38 result += tensor[k][j][i] * tensor[k][j][i]; \
39 return (TYPE)sqrt(result/8); \
40 } \
41 \
42 TYPE SNAME ## Max(TYPE * tensor, int slices, int rows, int cols) { \
43 int i, j, k, index; \
44 TYPE result = tensor[0]; \
45 for (k=0; k<slices; ++k) { \
46 for (j=0; j<rows; ++j) { \
47 for (i=0; i<cols; ++i) { \
48 index = k*rows*cols + j*cols + i; \
49 if (tensor[index] > result) result = tensor[index]; \
50 } \
51 } \
52 } \
53 return result; \
54 } \
55 \
56 TYPE SNAME ## Min(int slices, int rows, int cols, TYPE * tensor) { \
57 int i, j, k, index; \
58 TYPE result = tensor[0]; \
59 for (k=0; k<slices; ++k) { \
60 for (j=0; j<rows; ++j) { \
61 for (i=0; i<cols; ++i) { \
62 index = k*rows*cols + j*cols + i; \
63 if (tensor[index] < result) result = tensor[index]; \
64 } \
65 } \
66 } \
67 return result; \
68 } \
69 \
70 void SNAME ## Scale(TYPE array[3][3][3], TYPE val) { \
71 for (int k=0; k<3; ++k) \
72 for (int j=0; j<3; ++j) \
73 for (int i=0; i<3; ++i) \
74 array[k][j][i] *= val; \
75 } \
76 \
77 void SNAME ## Floor(TYPE * array, int slices, int rows, int cols, TYPE floor) { \
78 int i, j, k, index; \
79 for (k=0; k<slices; ++k) { \
80 for (j=0; j<rows; ++j) { \
81 for (i=0; i<cols; ++i) { \
82 index = k*rows*cols + j*cols + i; \
83 if (array[index] < floor) array[index] = floor; \
84 } \
85 } \
86 } \
87 } \
88 \
89 void SNAME ## Ceil(int slices, int rows, int cols, TYPE * array, TYPE ceil) { \
90 int i, j, k, index; \
91 for (k=0; k<slices; ++k) { \
92 for (j=0; j<rows; ++j) { \
93 for (i=0; i<cols; ++i) { \
94 index = k*rows*cols + j*cols + i; \
95 if (array[index] > ceil) array[index] = ceil; \
96 } \
97 } \
98 } \
99 } \
100 \
101 void SNAME ## LUSplit(TYPE tensor[2][2][2], TYPE lower[2][2][2], \
102 TYPE upper[2][2][2]) { \
103 int sum; \
104 for (int k=0; k<2; ++k) { \
105 for (int j=0; j<2; ++j) { \
106 for (int i=0; i<2; ++i) { \
107 sum = i + j + k; \
108 if (sum < 2) { \
109 lower[k][j][i] = tensor[k][j][i]; \
110 upper[k][j][i] = 0; \
111 } else { \
112 upper[k][j][i] = tensor[k][j][i]; \
113 lower[k][j][i] = 0; \
114 } \
115 } \
116 } \
117 } \
118 }
119
120 TEST_FUNCS(signed char , schar )
121 TEST_FUNCS(unsigned char , uchar )
122 TEST_FUNCS(short , short )
123 TEST_FUNCS(unsigned short , ushort )
124 TEST_FUNCS(int , int )
125 TEST_FUNCS(unsigned int , uint )
126 TEST_FUNCS(long , long )
127 TEST_FUNCS(unsigned long , ulong )
128 TEST_FUNCS(long long , longLong )
129 TEST_FUNCS(unsigned long long, ulongLong)
130 TEST_FUNCS(float , float )
131 TEST_FUNCS(double , double )