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) :
67 const TYPE *otherData = other.
getData();
68 memcpy(static_cast<void *>(data), static_cast<const void *>(otherData),
91 inline virtual TYPE
getVal(
unsigned int i,
unsigned int j)
const {
94 unsigned int id = i*
d_nCols + j;
99 inline virtual void setVal(
unsigned int i,
unsigned int j, TYPE val) {
102 unsigned int id = i*
d_nCols + j;
113 TYPE *data =
d_data.get();
114 memcpy(static_cast<void *>(rData), static_cast<void *>(&data[
id]), d_nCols*
sizeof(TYPE));
124 TYPE *data =
d_data.get();
125 for (j = 0; j <
d_nRows; j++) {
149 const TYPE *otherData = other.
getData();
150 TYPE *data =
d_data.get();
151 memcpy(static_cast<void *>(data), static_cast<const void *>(otherData),
d_dataSize*
sizeof(TYPE));
161 const TYPE *oData = other.
getData();
163 TYPE *data =
d_data.get();
176 const TYPE *oData = other.
getData();
178 TYPE *data =
d_data.get();
188 TYPE *data =
d_data.get();
198 TYPE *data =
d_data.get();
214 unsigned int tRows = transpose.
numRows();
215 unsigned int tCols = transpose.
numCols();
219 unsigned int idA, idAt, idT;
220 TYPE *tData = transpose.
getData();
221 TYPE *data =
d_data.get();
222 for (i = 0; i <
d_nRows; i++) {
224 for (j = 0; j <
d_nCols; j++) {
227 tData[idT] = data[idAt];
256 template <
class TYPE>
259 unsigned int aRows = A.
numRows();
260 unsigned int aCols = A.
numCols();
261 unsigned int cRows = C.
numRows();
262 unsigned int cCols = C.
numCols();
263 unsigned int bRows = B.
numRows();
264 unsigned int bCols = B.
numCols();
265 CHECK_INVARIANT(aCols == bRows,
"Size mismatch during multiplication");
266 CHECK_INVARIANT(aRows == cRows,
"Size mismatch during multiplication");
267 CHECK_INVARIANT(bCols == cCols,
"Size mismatch during multiplication");
271 const TYPE *bData = B.
getData();
272 const TYPE *aData = A.
getData();
273 unsigned int i, j, k;
274 unsigned int idA, idAt, idB, idC, idCt;
275 for (i = 0; i < aRows; i++) {
278 for (j = 0; j < cCols; j++) {
280 cData[idCt] = (TYPE)0.0;
281 for (k = 0; k < aCols; k++) {
284 cData[idCt] += (aData[idAt]*bData[idB]);
303 template <
class TYPE>
306 unsigned int aRows = A.
numRows();
307 unsigned int aCols = A.
numCols();
308 unsigned int xSiz = x.
size();
309 unsigned int ySiz = y.
size();
313 unsigned int idA, idAt;
314 const TYPE *xData = x.
getData();
315 const TYPE *aData = A.
getData();
317 for (i = 0; i < aRows; i++) {
319 yData[i] = (TYPE)(0.0);
320 for (j = 0; j < aCols; j++) {
322 yData[i] += (aData[idAt]*xData[j]);
332 template <
class TYPE > std::ostream &
operator<<(std::ostream& target,
334 unsigned int nr = mat.
numRows();
335 unsigned int nc = mat.
numCols();
336 target <<
"Rows: " << mat.
numRows() <<
" Columns: " << mat.
numCols() <<
"\n";
339 for (i = 0; i < nr; i++) {
340 for (j = 0; j < nc; j++) {
341 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
#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 size() const
return the size (dimension) of the vector
Matrix(unsigned int nRows, unsigned int nCols, DATA_SPTR data)
Initialize from a pointer.
A matrix class for general, non-square matrices.
boost::shared_array< TYPE > DATA_SPTR
const TYPE * getData() const
returns a const pointer to our data array
unsigned int numCols() const
returns the number of columns
Matrix< double > DoubleMatrix
Matrix< TYPE > & multiply(const Matrix< TYPE > &A, const Matrix< TYPE > &B, Matrix< TYPE > &C)
Matrix multiplication.
virtual TYPE getVal(unsigned int i, unsigned int j) const
returns a particular element of the matrix
virtual Matrix< TYPE > & operator+=(const Matrix< TYPE > &other)
Matrix addition.
virtual Matrix< TYPE > & operator/=(TYPE scale)
division by a scalar
#define PRECONDITION(expr, mess)
virtual void getCol(unsigned int i, Vector< TYPE > &col) const
returns a copy of a column of the matrix
virtual void getRow(unsigned int i, Vector< TYPE > &row) const
returns a copy of a row of the matrix
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
unsigned int getDataSize() const
Matrix(const Matrix< TYPE > &other)
copy constructor