| 1 |
#include <stdlib.h> |
| 2 |
#include <math.h> |
| 3 |
#include <iostream> |
| 4 |
#include "SuperTensor.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 supertensor[2][2][2][2]); |
| 10 |
// TYPE SNAMEMax( TYPE * supertensor, int cubes, int slices, int rows, int cols); |
| 11 |
// TYPE SNAMEMin( int cubes, int slices, int rows, int cols, TYPE * supertensor); |
| 12 |
// void SNAMEScale( TYPE supertensor[3][3][3][3]); |
| 13 |
// void SNAMEFloor( TYPE * array, int cubes, int slices, int rows, int cols, TYPE floor); |
| 14 |
// void SNAMECeil( int slices, int cubes, int slices, int rows, int cols, TYPE * array, TYPE ceil); |
| 15 |
// void SNAMELUSplit(TYPE in[2][2][2][2], TYPE lower[2][2][2][2], TYPE upper[2][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 |
// * 4D input arrays, hard-coded length |
| 24 |
// * 4D input arrays |
| 25 |
// * 4D input arrays, data last |
| 26 |
// * 4D in-place arrays, hard-coded lengths |
| 27 |
// * 4D in-place arrays |
| 28 |
// * 4D in-place arrays, data last |
| 29 |
// * 4D argout arrays, hard-coded length |
| 30 |
// |
| 31 |
#define TEST_FUNCS(TYPE, SNAME) \ |
| 32 |
\ |
| 33 |
TYPE SNAME ## Norm(TYPE supertensor[2][2][2][2]) { \ |
| 34 |
double result = 0; \ |
| 35 |
for (int l=0; l<2; ++l) \ |
| 36 |
for (int k=0; k<2; ++k) \ |
| 37 |
for (int j=0; j<2; ++j) \ |
| 38 |
for (int i=0; i<2; ++i) \ |
| 39 |
result += supertensor[l][k][j][i] * supertensor[l][k][j][i]; \ |
| 40 |
return (TYPE)sqrt(result/16); \ |
| 41 |
} \ |
| 42 |
\ |
| 43 |
TYPE SNAME ## Max(TYPE * supertensor, int cubes, int slices, int rows, int cols) { \ |
| 44 |
int i, j, k, l, index; \ |
| 45 |
TYPE result = supertensor[0]; \ |
| 46 |
for (l=0; l<cubes; ++l) { \ |
| 47 |
for (k=0; k<slices; ++k) { \ |
| 48 |
for (j=0; j<rows; ++j) { \ |
| 49 |
for (i=0; i<cols; ++i) { \ |
| 50 |
index = l*slices*rows*cols + k*rows*cols + j*cols + i; \ |
| 51 |
if (supertensor[index] > result) result = supertensor[index]; \ |
| 52 |
} \ |
| 53 |
} \ |
| 54 |
} \ |
| 55 |
} \ |
| 56 |
return result; \ |
| 57 |
} \ |
| 58 |
\ |
| 59 |
TYPE SNAME ## Min(int cubes, int slices, int rows, int cols, TYPE * supertensor) { \ |
| 60 |
int i, j, k, l, index; \ |
| 61 |
TYPE result = supertensor[0]; \ |
| 62 |
for (l=0; l<cubes; ++l) { \ |
| 63 |
for (k=0; k<slices; ++k) { \ |
| 64 |
for (j=0; j<rows; ++j) { \ |
| 65 |
for (i=0; i<cols; ++i) { \ |
| 66 |
index = l*slices*rows*cols + k*rows*cols + j*cols + i; \ |
| 67 |
if (supertensor[index] < result) result = supertensor[index]; \ |
| 68 |
} \ |
| 69 |
} \ |
| 70 |
} \ |
| 71 |
} \ |
| 72 |
return result; \ |
| 73 |
} \ |
| 74 |
\ |
| 75 |
void SNAME ## Scale(TYPE array[3][3][3][3], TYPE val) { \ |
| 76 |
for (int l=0; l<3; ++l) \ |
| 77 |
for (int k=0; k<3; ++k) \ |
| 78 |
for (int j=0; j<3; ++j) \ |
| 79 |
for (int i=0; i<3; ++i) \ |
| 80 |
array[l][k][j][i] *= val; \ |
| 81 |
} \ |
| 82 |
\ |
| 83 |
void SNAME ## Floor(TYPE * array, int cubes, int slices, int rows, int cols, TYPE floor) { \ |
| 84 |
int i, j, k, l, index; \ |
| 85 |
for (l=0; l<cubes; ++l) { \ |
| 86 |
for (k=0; k<slices; ++k) { \ |
| 87 |
for (j=0; j<rows; ++j) { \ |
| 88 |
for (i=0; i<cols; ++i) { \ |
| 89 |
index = l*slices*rows*cols + k*rows*cols + j*cols + i; \ |
| 90 |
if (array[index] < floor) array[index] = floor; \ |
| 91 |
} \ |
| 92 |
} \ |
| 93 |
} \ |
| 94 |
} \ |
| 95 |
} \ |
| 96 |
\ |
| 97 |
void SNAME ## Ceil(int cubes, int slices, int rows, int cols, TYPE * array, TYPE ceil) { \ |
| 98 |
int i, j, k, l, index; \ |
| 99 |
for (l=0; l<cubes; ++l) { \ |
| 100 |
for (k=0; k<slices; ++k) { \ |
| 101 |
for (j=0; j<rows; ++j) { \ |
| 102 |
for (i=0; i<cols; ++i) { \ |
| 103 |
index = l*slices*rows*cols + k*rows*cols + j*cols + i; \ |
| 104 |
if (array[index] > ceil) array[index] = ceil; \ |
| 105 |
} \ |
| 106 |
} \ |
| 107 |
} \ |
| 108 |
} \ |
| 109 |
} \ |
| 110 |
\ |
| 111 |
void SNAME ## LUSplit(TYPE supertensor[2][2][2][2], TYPE lower[2][2][2][2], \ |
| 112 |
TYPE upper[2][2][2][2]) { \ |
| 113 |
int sum; \ |
| 114 |
for (int l=0; l<2; ++l) { \ |
| 115 |
for (int k=0; k<2; ++k) { \ |
| 116 |
for (int j=0; j<2; ++j) { \ |
| 117 |
for (int i=0; i<2; ++i) { \ |
| 118 |
sum = i + j + k + l; \ |
| 119 |
if (sum < 2) { \ |
| 120 |
lower[l][k][j][i] = supertensor[l][k][j][i]; \ |
| 121 |
upper[l][k][j][i] = 0; \ |
| 122 |
} else { \ |
| 123 |
upper[l][k][j][i] = supertensor[l][k][j][i]; \ |
| 124 |
lower[l][k][j][i] = 0; \ |
| 125 |
} \ |
| 126 |
} \ |
| 127 |
} \ |
| 128 |
} \ |
| 129 |
} \ |
| 130 |
} |
| 131 |
|
| 132 |
TEST_FUNCS(signed char , schar ) |
| 133 |
TEST_FUNCS(unsigned char , uchar ) |
| 134 |
TEST_FUNCS(short , short ) |
| 135 |
TEST_FUNCS(unsigned short , ushort ) |
| 136 |
TEST_FUNCS(int , int ) |
| 137 |
TEST_FUNCS(unsigned int , uint ) |
| 138 |
TEST_FUNCS(long , long ) |
| 139 |
TEST_FUNCS(unsigned long , ulong ) |
| 140 |
TEST_FUNCS(long long , longLong ) |
| 141 |
TEST_FUNCS(unsigned long long, ulongLong) |
| 142 |
TEST_FUNCS(float , float ) |
| 143 |
TEST_FUNCS(double , double ) |
| 144 |
|