11 #ifndef __RD_MATRIX_H__ 12 #define __RD_MATRIX_H__ 19 #include <boost/smart_ptr.hpp> 34 Matrix(
unsigned int nRows,
unsigned int nCols)
37 memset(static_cast<void *>(data), 0,
d_dataSize *
sizeof(TYPE));
42 Matrix(
unsigned int nRows,
unsigned int nCols, TYPE val)
57 Matrix(
unsigned int nRows,
unsigned int nCols, DATA_SPTR data)
70 const TYPE *otherData = other.
getData();
71 memcpy(static_cast<void *>(data), static_cast<const void *>(otherData),
87 inline virtual TYPE
getVal(
unsigned int i,
unsigned int j)
const {
90 unsigned int id = i *
d_nCols + j;
95 inline virtual void setVal(
unsigned int i,
unsigned int j, TYPE val) {
98 unsigned int id = i *
d_nCols + j;
109 TYPE *data =
d_data.get();
110 memcpy(static_cast<void *>(rData), static_cast<void *>(&data[
id]),
111 d_nCols *
sizeof(TYPE));
120 TYPE *data =
d_data.get();
121 for (j = 0; j <
d_nRows; j++) {
139 "Num rows mismatch in matrix copying");
141 "Num cols mismatch in matrix copying");
142 const TYPE *otherData = other.
getData();
143 TYPE *data =
d_data.get();
144 memcpy(static_cast<void *>(data), static_cast<const void *>(otherData),
154 "Num rows mismatch in matrix addition");
156 "Num cols mismatch in matrix addition");
157 const TYPE *oData = other.
getData();
159 TYPE *data =
d_data.get();
171 "Num rows mismatch in matrix addition");
173 "Num cols mismatch in matrix addition");
174 const TYPE *oData = other.
getData();
176 TYPE *data =
d_data.get();
186 TYPE *data =
d_data.get();
196 TYPE *data =
d_data.get();
212 unsigned int tRows = transpose.
numRows();
213 unsigned int tCols = transpose.
numCols();
217 unsigned int idA, idAt, idT;
218 TYPE *tData = transpose.
getData();
219 TYPE *data =
d_data.get();
220 for (i = 0; i <
d_nRows; i++) {
222 for (j = 0; j <
d_nCols; j++) {
225 tData[idT] = data[idAt];
254 template <
class TYPE>
257 unsigned int aRows = A.
numRows();
258 unsigned int aCols = A.
numCols();
259 unsigned int cRows = C.
numRows();
260 unsigned int cCols = C.
numCols();
261 unsigned int bRows = B.
numRows();
262 unsigned int bCols = B.
numCols();
263 CHECK_INVARIANT(aCols == bRows,
"Size mismatch during multiplication");
264 CHECK_INVARIANT(aRows == cRows,
"Size mismatch during multiplication");
265 CHECK_INVARIANT(bCols == cCols,
"Size mismatch during multiplication");
269 const TYPE *bData = B.
getData();
270 const TYPE *aData = A.
getData();
271 unsigned int i, j, k;
272 unsigned int idA, idAt, idB, idC, idCt;
273 for (i = 0; i < aRows; i++) {
276 for (j = 0; j < cCols; j++) {
278 cData[idCt] = (TYPE)0.0;
279 for (k = 0; k < aCols; k++) {
282 cData[idCt] += (aData[idAt] * bData[idB]);
301 template <
class TYPE>
304 unsigned int aRows = A.
numRows();
305 unsigned int aCols = A.
numCols();
306 unsigned int xSiz = x.
size();
307 unsigned int ySiz = y.
size();
311 unsigned int idA, idAt;
312 const TYPE *xData = x.
getData();
313 const TYPE *aData = A.
getData();
315 for (i = 0; i < aRows; i++) {
317 yData[i] = (TYPE)(0.0);
318 for (j = 0; j < aCols; j++) {
320 yData[i] += (aData[idAt] * xData[j]);
330 template <
class TYPE>
333 unsigned int nr = mat.
numRows();
334 unsigned int nc = mat.
numCols();
335 target <<
"Rows: " << mat.
numRows() <<
" Columns: " << mat.
numCols() <<
"\n";
338 for (i = 0; i < nr; i++) {
339 for (j = 0; j < nc; j++) {
340 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