| 1 |
#include <stdlib.h> |
| 2 |
#include <math.h> |
| 3 |
#include <iostream> |
| 4 |
#include "Matrix.h" |
| 5 |
|
| 6 |
// The following macro defines a family of functions that work with 2D |
| 7 |
// arrays with the forms |
| 8 |
// |
| 9 |
// TYPE SNAMEDet( TYPE matrix[2][2]); |
| 10 |
// TYPE SNAMEMax( TYPE * matrix, int rows, int cols); |
| 11 |
// TYPE SNAMEMin( int rows, int cols, TYPE * matrix); |
| 12 |
// void SNAMEScale( TYPE matrix[3][3]); |
| 13 |
// void SNAMEFloor( TYPE * array, int rows, int cols, TYPE floor); |
| 14 |
// void SNAMECeil( int rows, int cols, TYPE * array, TYPE ceil); |
| 15 |
// void SNAMELUSplit(TYPE in[3][3], TYPE lower[3][3], TYPE upper[3][3]); |
| 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 |
// * 2D input arrays, hard-coded length |
| 24 |
// * 2D input arrays |
| 25 |
// * 2D input arrays, data last |
| 26 |
// * 2D in-place arrays, hard-coded lengths |
| 27 |
// * 2D in-place arrays |
| 28 |
// * 2D in-place arrays, data last |
| 29 |
// * 2D argout arrays, hard-coded length |
| 30 |
// |
| 31 |
#define TEST_FUNCS(TYPE, SNAME) \ |
| 32 |
\ |
| 33 |
TYPE SNAME ## Det(TYPE matrix[2][2]) { \ |
| 34 |
return matrix[0][0]*matrix[1][1] - matrix[0][1]*matrix[1][0]; \ |
| 35 |
} \ |
| 36 |
\ |
| 37 |
TYPE SNAME ## Max(TYPE * matrix, int rows, int cols) { \ |
| 38 |
int i, j, index; \ |
| 39 |
TYPE result = matrix[0]; \ |
| 40 |
for (j=0; j<cols; ++j) { \ |
| 41 |
for (i=0; i<rows; ++i) { \ |
| 42 |
index = j*rows + i; \ |
| 43 |
if (matrix[index] > result) result = matrix[index]; \ |
| 44 |
} \ |
| 45 |
} \ |
| 46 |
return result; \ |
| 47 |
} \ |
| 48 |
\ |
| 49 |
TYPE SNAME ## Min(int rows, int cols, TYPE * matrix) { \ |
| 50 |
int i, j, index; \ |
| 51 |
TYPE result = matrix[0]; \ |
| 52 |
for (j=0; j<cols; ++j) { \ |
| 53 |
for (i=0; i<rows; ++i) { \ |
| 54 |
index = j*rows + i; \ |
| 55 |
if (matrix[index] < result) result = matrix[index]; \ |
| 56 |
} \ |
| 57 |
} \ |
| 58 |
return result; \ |
| 59 |
} \ |
| 60 |
\ |
| 61 |
void SNAME ## Scale(TYPE array[3][3], TYPE val) { \ |
| 62 |
for (int i=0; i<3; ++i) \ |
| 63 |
for (int j=0; j<3; ++j) \ |
| 64 |
array[i][j] *= val; \ |
| 65 |
} \ |
| 66 |
\ |
| 67 |
void SNAME ## Floor(TYPE * array, int rows, int cols, TYPE floor) { \ |
| 68 |
int i, j, index; \ |
| 69 |
for (j=0; j<cols; ++j) { \ |
| 70 |
for (i=0; i<rows; ++i) { \ |
| 71 |
index = j*rows + i; \ |
| 72 |
if (array[index] < floor) array[index] = floor; \ |
| 73 |
} \ |
| 74 |
} \ |
| 75 |
} \ |
| 76 |
\ |
| 77 |
void SNAME ## Ceil(int rows, int cols, TYPE * array, TYPE ceil) { \ |
| 78 |
int i, j, index; \ |
| 79 |
for (j=0; j<cols; ++j) { \ |
| 80 |
for (i=0; i<rows; ++i) { \ |
| 81 |
index = j*rows + i; \ |
| 82 |
if (array[index] > ceil) array[index] = ceil; \ |
| 83 |
} \ |
| 84 |
} \ |
| 85 |
} \ |
| 86 |
\ |
| 87 |
void SNAME ## LUSplit(TYPE matrix[3][3], TYPE lower[3][3], TYPE upper[3][3]) { \ |
| 88 |
for (int i=0; i<3; ++i) { \ |
| 89 |
for (int j=0; j<3; ++j) { \ |
| 90 |
if (i >= j) { \ |
| 91 |
lower[i][j] = matrix[i][j]; \ |
| 92 |
upper[i][j] = 0; \ |
| 93 |
} else { \ |
| 94 |
lower[i][j] = 0; \ |
| 95 |
upper[i][j] = matrix[i][j]; \ |
| 96 |
} \ |
| 97 |
} \ |
| 98 |
} \ |
| 99 |
} |
| 100 |
|
| 101 |
TEST_FUNCS(signed char , schar ) |
| 102 |
TEST_FUNCS(unsigned char , uchar ) |
| 103 |
TEST_FUNCS(short , short ) |
| 104 |
TEST_FUNCS(unsigned short , ushort ) |
| 105 |
TEST_FUNCS(int , int ) |
| 106 |
TEST_FUNCS(unsigned int , uint ) |
| 107 |
TEST_FUNCS(long , long ) |
| 108 |
TEST_FUNCS(unsigned long , ulong ) |
| 109 |
TEST_FUNCS(long long , longLong ) |
| 110 |
TEST_FUNCS(unsigned long long, ulongLong) |
| 111 |
TEST_FUNCS(float , float ) |
| 112 |
TEST_FUNCS(double , double ) |