amiro-os / include / Matrix.h @ 1b3adcdd
History | View | Annotate | Download (4.831 KB)
| 1 |
#ifndef MATRIX_H_
|
|---|---|
| 2 |
#define MATRIX_H_
|
| 3 |
|
| 4 |
#include <algorithm> // std::fill, std::copy |
| 5 |
|
| 6 |
namespace Matrix {
|
| 7 |
|
| 8 |
typedef unsigned int uint; |
| 9 |
|
| 10 |
/**
|
| 11 |
* Multiplication of two matrices Z=X*Y
|
| 12 |
*
|
| 13 |
* @param *X Pointer to first element of first matrix
|
| 14 |
* @param Xrows Rows of X
|
| 15 |
* @param Xcols Cols of X
|
| 16 |
* @param *Y Pointer to first element of second matrix
|
| 17 |
* @param Yrows Rows of Y
|
| 18 |
* @param Ycols Cols of Y
|
| 19 |
* @param *Z Pointer to first element of result matrix (needs to be init with 0)
|
| 20 |
* @param Zrows Rows of Z
|
| 21 |
* @param Zcols Cols of Z
|
| 22 |
*/
|
| 23 |
template <typename T> |
| 24 |
void XdotY(T* X,uint Xrows,uint Xcols,T* Y,uint Yrows, uint Ycols,T* Z, uint Zrows,uint Zcols) {
|
| 25 |
|
| 26 |
// Check if the multiplication will work
|
| 27 |
// if (Zrows != Xrows || Zcols != Ycols || Xcols != Yrows) {
|
| 28 |
// return 1;
|
| 29 |
// }
|
| 30 |
|
| 31 |
// Do the multiplication
|
| 32 |
// Z_{i,k} = sum_{n} X_{i,n} * Y_{n,k}
|
| 33 |
for (uint i = 0; i < Zrows; i++) { |
| 34 |
for (uint k = 0; k < Zcols; k++) { |
| 35 |
for (uint n = 0; n < Xcols; n++) { |
| 36 |
Z[i*Zcols+k] += X[i*Xcols+n] * Y[n*Ycols+k]; |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
// return 0;
|
| 41 |
}; |
| 42 |
|
| 43 |
/**
|
| 44 |
* Multiplication of two matrices Z=X*Y^{T},
|
| 45 |
* while Y will be transposed during calculation
|
| 46 |
*
|
| 47 |
* @param *X Pointer to first element of first matrix
|
| 48 |
* @param Xrows Rows of X
|
| 49 |
* @param Xcols Cols of X
|
| 50 |
* @param *Y Pointer to first element of second matrix
|
| 51 |
* @param Yrows Rows of Y
|
| 52 |
* @param Ycols Cols of Y
|
| 53 |
* @param *Z Pointer to first element of result matrix (needs to be init with 0)
|
| 54 |
* @param Zrows Rows of Z
|
| 55 |
* @param Zcols Cols of Z
|
| 56 |
*/
|
| 57 |
template <typename T> |
| 58 |
void XdotYtrans(T* X,uint Xrows,uint Xcols,T* Y,uint Yrows, uint Ycols,T* Z, uint Zrows,uint Zcols) {
|
| 59 |
|
| 60 |
// Check if the multiplication will work
|
| 61 |
// if (Zrows != Xrows || Zcols != Yrows || Xcols != Ycols) {
|
| 62 |
// return 1;
|
| 63 |
// }
|
| 64 |
|
| 65 |
// Do the multiplication
|
| 66 |
// Z_{i,k} = sum_{n} X_{i,n} * Y_{k,n}
|
| 67 |
for (uint i = 0; i < Zrows; i++) { |
| 68 |
for (uint k = 0; k < Zcols; k++) { |
| 69 |
for (uint n = 0; n < Xcols; n++) { |
| 70 |
Z[i*Zcols+k] += X[i*Xcols+n] * Y[n+k*Ycols]; |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
// return 0;
|
| 75 |
}; |
| 76 |
|
| 77 |
/**
|
| 78 |
* Multiplication of two matrices Z=X^{T}*Y,
|
| 79 |
* while X will be transposed during calculation
|
| 80 |
*
|
| 81 |
* @param *X Pointer to first element of first of matrix
|
| 82 |
* @param Xrows Rows of X
|
| 83 |
* @param Xcols Cols of X
|
| 84 |
* @param *Y Pointer to first element of second matrix
|
| 85 |
* @param Yrows Rows of Y
|
| 86 |
* @param Ycols Cols of Y
|
| 87 |
* @param *Z Pointer to first element of result matrix (needs to be init with 0)
|
| 88 |
* @param Zrows Rows of Z
|
| 89 |
* @param Zcols Cols of Z
|
| 90 |
*/
|
| 91 |
template <typename T> |
| 92 |
void XtransDotY(T* X, uint Xrows,uint Xcols,T* Y,uint Yrows, uint Ycols,T* Z, uint Zrows,uint Zcols) {
|
| 93 |
|
| 94 |
// Check if the multiplication will work
|
| 95 |
// if (Zrows != Xrows || Zcols != Yrows || Xcols != Ycols) {
|
| 96 |
// return 1;
|
| 97 |
// }
|
| 98 |
|
| 99 |
// Do the multiplication
|
| 100 |
// Z_{i,k} = sum_{n} X_{n,i} * Y_{k,n}
|
| 101 |
for (uint i = 0; i < Zrows; i++) { |
| 102 |
for (uint k = 0; k < Zcols; k++) { |
| 103 |
for (uint n = 0; n < Xcols; n++) { |
| 104 |
Z[i*Zcols+k] += X[n*Xcols+i] * Y[n+k*Ycols]; |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
// return 0;
|
| 109 |
}; |
| 110 |
|
| 111 |
/**
|
| 112 |
* Addition of two matrices Z=X+Y,
|
| 113 |
*
|
| 114 |
* @param *X Pointer to first element of first matrix
|
| 115 |
* @param Xrows Rows of X
|
| 116 |
* @param Xcols Cols of X
|
| 117 |
* @param *Y Pointer to first element of second matrix
|
| 118 |
* @param Yrows Rows of Y
|
| 119 |
* @param Ycols Cols of Y
|
| 120 |
* @param *Z Pointer to first element of result matrix (needs to be init with 0)
|
| 121 |
* @param Zrows Rows of Z
|
| 122 |
* @param Zcols Cols of Z
|
| 123 |
*/
|
| 124 |
template <typename T> |
| 125 |
void XplusY(T* X,uint Xrows,uint Xcols,T* Y,uint Yrows, uint Ycols,T* Z, uint Zrows,uint Zcols) {
|
| 126 |
|
| 127 |
// Check if the multiplication will work
|
| 128 |
// if (Zrows != Xrows || Xrows != Yrows || Zcols != Ycols || Ycols != Xcols {
|
| 129 |
// return 1;
|
| 130 |
// }
|
| 131 |
|
| 132 |
// Do the summation
|
| 133 |
for (uint idx = 0; idx < Xrows*Xcols; idx++) { |
| 134 |
Z[idx] += X[idx] + Y[idx]; |
| 135 |
} |
| 136 |
// return 0;
|
| 137 |
}; |
| 138 |
|
| 139 |
/**
|
| 140 |
* Initializing matrix with any element
|
| 141 |
*
|
| 142 |
* @param *X Pointer to first element of first matrix
|
| 143 |
* @param Xrows Rows of X
|
| 144 |
* @param Xcols Cols of X
|
| 145 |
* @param value value of all elements
|
| 146 |
*/
|
| 147 |
template <typename T> |
| 148 |
void init(T* X, uint Xrows, uint Xcols, T value) {
|
| 149 |
std::fill (X,X+Xrows*Xcols,value); |
| 150 |
}; |
| 151 |
|
| 152 |
/**
|
| 153 |
* Copy a matrix Y=X
|
| 154 |
*
|
| 155 |
* @param *X Pointer to first element of source matrix
|
| 156 |
* @param Xrows Rows of X
|
| 157 |
* @param Xcols Cols of X
|
| 158 |
* @param *Y Pointer to first element of result matrix
|
| 159 |
* @param Yrows Rows of Y
|
| 160 |
* @param Ycols Cols of Y
|
| 161 |
*
|
| 162 |
*/
|
| 163 |
template <typename T> |
| 164 |
void copy(T* X, uint Xrows, uint Xcols, T* Y, uint Yrows, uint Ycols) {
|
| 165 |
// if (Xrows != Yrows || Xcols != Ycols ) {
|
| 166 |
// return 1;
|
| 167 |
// }
|
| 168 |
std::copy (X,X+Xrows*Xcols,Y); |
| 169 |
// return 0;
|
| 170 |
}; |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
#endif /* MATRIX_ */ |