10 #ifndef __RD_MATRIX_H__ 11 #define __RD_MATRIX_H__ 18 #include <boost/smart_ptr.hpp> 33 Matrix(
unsigned int nRows,
unsigned int nCols)
36 memset(static_cast<void *>(data), 0,
d_dataSize *
sizeof(TYPE));
41 Matrix(
unsigned int nRows,
unsigned int nCols, TYPE val)
56 Matrix(
unsigned int nRows,
unsigned int nCols, DATA_SPTR data)
69 const TYPE *otherData = other.
getData();
70 memcpy(static_cast<void *>(data), static_cast<const void *>(otherData),
86 inline virtual TYPE
getVal(
unsigned int i,
unsigned int j)
const {
89 unsigned int id = i *
d_nCols + j;
94 inline virtual void setVal(
unsigned int i,
unsigned int j, TYPE val) {
97 unsigned int id = i *
d_nCols + j;
108 TYPE *data =
d_data.get();
109 memcpy(static_cast<void *>(rData), static_cast<void *>(&data[
id]),
110 d_nCols *
sizeof(TYPE));
119 TYPE *data =
d_data.get();
120 for (j = 0; j <
d_nRows; j++) {
138 "Num rows mismatch in matrix copying");
140 "Num cols mismatch in matrix copying");
141 const TYPE *otherData = other.
getData();
142 TYPE *data =
d_data.get();
143 memcpy(static_cast<void *>(data), static_cast<const void *>(otherData),
153 "Num rows mismatch in matrix addition");
155 "Num cols mismatch in matrix addition");
156 const TYPE *oData = other.
getData();
158 TYPE *data =
d_data.get();
170 "Num rows mismatch in matrix addition");
172 "Num cols mismatch in matrix addition");
173 const TYPE *oData = other.
getData();
175 TYPE *data =
d_data.get();
185 TYPE *data =
d_data.get();
195 TYPE *data =
d_data.get();
211 unsigned int tRows = transpose.
numRows();
212 unsigned int tCols = transpose.
numCols();
216 unsigned int idA, idAt, idT;
217 TYPE *tData = transpose.
getData();
218 TYPE *data =
d_data.get();
219 for (i = 0; i <
d_nRows; i++) {
221 for (j = 0; j <
d_nCols; j++) {
224 tData[idT] = data[idAt];
253 template <
class TYPE>
256 unsigned int aRows = A.
numRows();
257 unsigned int aCols = A.
numCols();
258 unsigned int cRows = C.
numRows();
259 unsigned int cCols = C.
numCols();
260 unsigned int bRows = B.
numRows();
261 unsigned int bCols = B.
numCols();
262 CHECK_INVARIANT(aCols == bRows,
"Size mismatch during multiplication");
263 CHECK_INVARIANT(aRows == cRows,
"Size mismatch during multiplication");
264 CHECK_INVARIANT(bCols == cCols,
"Size mismatch during multiplication");
268 const TYPE *bData = B.
getData();
269 const TYPE *aData = A.
getData();
270 unsigned int i, j, k;
271 unsigned int idA, idAt, idB, idC, idCt;
272 for (i = 0; i < aRows; i++) {
275 for (j = 0; j < cCols; j++) {
277 cData[idCt] = (TYPE)0.0;
278 for (k = 0; k < aCols; k++) {
281 cData[idCt] += (aData[idAt] * bData[idB]);
300 template <
class TYPE>
303 unsigned int aRows = A.
numRows();
304 unsigned int aCols = A.
numCols();
305 unsigned int xSiz = x.
size();
306 unsigned int ySiz = y.
size();
310 unsigned int idA, idAt;
311 const TYPE *xData = x.
getData();
312 const TYPE *aData = A.
getData();
314 for (i = 0; i < aRows; i++) {
316 yData[i] = (TYPE)(0.0);
317 for (j = 0; j < aCols; j++) {
319 yData[i] += (aData[idAt] * xData[j]);
329 template <
class TYPE>
332 unsigned int nr = mat.
numRows();
333 unsigned int nc = mat.
numCols();
334 target <<
"Rows: " << mat.
numRows() <<
" Columns: " << mat.
numCols() <<
"\n";
337 for (i = 0; i < nr; i++) {
338 for (j = 0; j < nc; j++) {
339 target << std::setw(7) << std::setprecision(3) << mat.
getVal(i, j);
Matrix(unsigned int nRows, unsigned int nCols)
Initialize with a size.
virtual Matrix< TYPE > & operator-=(const Matrix< TYPE > &other)
Matrix subtraction.
unsigned int numRows() const
returns the number of rows
std::ostream & operator<<(std::ostream &target, const RDNumeric::Matrix< TYPE > &mat)
ostream operator for Matrix's
unsigned int size() const
return the size (dimension) of the vector
#define CHECK_INVARIANT(expr, mess)
virtual Matrix< TYPE > & operator*=(TYPE scale)
Multiplication by a scalar.
virtual void setVal(unsigned int i, unsigned int j, TYPE val)
sets a particular element of the matrix
Matrix(unsigned int nRows, unsigned int nCols, TYPE val)
Initialize with a size and default value.
Matrix< TYPE > & assign(const Matrix< TYPE > &other)
Copy operator.
virtual Matrix< TYPE > & transpose(Matrix< TYPE > &transpose) const
copies the transpose of this Matrix into another, returns the result
unsigned int getDataSize() const
const TYPE * getData() const
returns a const pointer to our data array
virtual TYPE getVal(unsigned int i, unsigned int j) const
returns a particular element of the matrix
unsigned int numCols() const
returns the number of columns
Matrix(unsigned int nRows, unsigned int nCols, DATA_SPTR data)
Initialize from a pointer.
virtual void getCol(unsigned int i, Vector< TYPE > &col) const
returns a copy of a column of the matrix
A matrix class for general, non-square matrices.
boost::shared_array< TYPE > DATA_SPTR
Matrix< double > DoubleMatrix
Matrix< TYPE > & multiply(const Matrix< TYPE > &A, const Matrix< TYPE > &B, Matrix< TYPE > &C)
Matrix multiplication.
virtual Matrix< TYPE > & operator+=(const Matrix< TYPE > &other)
Matrix addition.
virtual Matrix< TYPE > & operator/=(TYPE scale)
division by a scalar
virtual void getRow(unsigned int i, Vector< TYPE > &row) const
returns a copy of a row of the matrix
#define PRECONDITION(expr, mess)
TYPE * getData()
returns a pointer to our data array
A class to represent vectors of numbers.
TYPE * getData()
returns a pointer to our data array
Matrix(const Matrix< TYPE > &other)
copy constructor