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)
69 const TYPE *otherData = other.
getData();
70 memcpy(
static_cast<void *
>(data),
static_cast<const void *
>(otherData),
94 inline virtual TYPE
getVal(
unsigned int i,
unsigned int j)
const {
97 unsigned int id = i *
d_nCols + j;
102 inline virtual void setVal(
unsigned int i,
unsigned int j, TYPE val) {
105 unsigned int id = i *
d_nCols + j;
112 unsigned int id = i *
d_nCols + j;
119 unsigned int id = i *
d_nCols + j;
129 TYPE *data =
d_data.get();
130 memcpy(
static_cast<void *
>(rData),
static_cast<void *
>(&data[
id]),
140 TYPE *data =
d_data.get();
141 for (j = 0; j <
d_nRows; j++) {
159 "Num rows mismatch in matrix copying");
161 "Num cols mismatch in matrix copying");
162 const TYPE *otherData = other.
getData();
163 TYPE *data =
d_data.get();
164 memcpy(
static_cast<void *
>(data),
static_cast<const void *
>(otherData),
174 "Num rows mismatch in matrix addition");
176 "Num cols mismatch in matrix addition");
177 const TYPE *oData = other.
getData();
179 TYPE *data =
d_data.get();
191 "Num rows mismatch in matrix addition");
193 "Num cols mismatch in matrix addition");
194 const TYPE *oData = other.
getData();
196 TYPE *data =
d_data.get();
206 TYPE *data =
d_data.get();
216 TYPE *data =
d_data.get();
232 unsigned int tRows =
transpose.numRows();
233 unsigned int tCols =
transpose.numCols();
237 unsigned int idA, idAt, idT;
239 TYPE *data =
d_data.get();
240 for (i = 0; i <
d_nRows; i++) {
242 for (j = 0; j <
d_nCols; j++) {
245 tData[idT] = data[idAt];
274 unsigned int aRows = A.
numRows();
275 unsigned int aCols = A.
numCols();
276 unsigned int cRows = C.numRows();
277 unsigned int cCols = C.numCols();
278 unsigned int bRows = B.
numRows();
279 unsigned int bCols = B.
numCols();
280 CHECK_INVARIANT(aCols == bRows,
"Size mismatch during multiplication");
281 CHECK_INVARIANT(aRows == cRows,
"Size mismatch during multiplication");
282 CHECK_INVARIANT(bCols == cCols,
"Size mismatch during multiplication");
285 TYPE *cData = C.getData();
286 const TYPE *bData = B.
getData();
287 const TYPE *aData = A.
getData();
288 unsigned int i, j, k;
289 unsigned int idA, idAt, idB, idC, idCt;
290 for (i = 0; i < aRows; i++) {
293 for (j = 0; j < cCols; j++) {
295 cData[idCt] = (TYPE)0.0;
296 for (k = 0; k < aCols; k++) {
299 cData[idCt] += (aData[idAt] * bData[idB]);
321 unsigned int aRows = A.
numRows();
322 unsigned int aCols = A.
numCols();
323 unsigned int xSiz = x.
size();
324 unsigned int ySiz = y.
size();
328 unsigned int idA, idAt;
329 const TYPE *xData = x.
getData();
330 const TYPE *aData = A.
getData();
332 for (i = 0; i < aRows; i++) {
334 yData[i] = (TYPE)(0.0);
335 for (j = 0; j < aCols; j++) {
337 yData[i] += (aData[idAt] * xData[j]);
350 unsigned int nr = mat.
numRows();
351 unsigned int nc = mat.
numCols();
352 target <<
"Rows: " << mat.
numRows() <<
" Columns: " << mat.
numCols() <<
"\n";
355 for (i = 0; i < nr; i++) {
356 for (j = 0; j < nc; j++) {
357 target << std::setfill(
' ') << std::setw(7) << std::setprecision(3)
358 << mat.
getVal(i, j) <<
" ";
#define CHECK_INVARIANT(expr, mess)
#define PRECONDITION(expr, mess)
std::ostream & operator<<(std::ostream &target, const RDNumeric::Matrix< TYPE > &mat)
ostream operator for Matrix's
A matrix class for general, non-square matrices.
virtual TYPE getValUnchecked(unsigned int i, unsigned int j) const
returns a particular element of the matrix
Matrix(unsigned int nRows, unsigned int nCols)
Initialize with a size.
virtual TYPE getVal(unsigned int i, unsigned int j) const
returns a particular element of the matrix
virtual Matrix< TYPE > & transpose(Matrix< TYPE > &transpose) const
copies the transpose of this Matrix into another, returns the result
virtual Matrix< TYPE > & operator+=(const Matrix< TYPE > &other)
Matrix addition.
Matrix(unsigned int nRows, unsigned int nCols, DATA_SPTR data)
Initialize from a pointer.
Matrix(Matrix< TYPE > &&other)=default
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
unsigned int getDataSize() const
virtual void setValUnchecked(unsigned int i, unsigned int j, TYPE val)
sets a particular element of the matrix
TYPE * getData()
returns a pointer to our data array
unsigned int numRows() const
virtual Matrix< TYPE > & operator*=(TYPE scale)
Multiplication by a scalar.
unsigned int numCols() const
Matrix(const Matrix< TYPE > &other)
copy constructor
Matrix< TYPE > & operator=(Matrix< TYPE > &&other)=default
boost::shared_array< TYPE > DATA_SPTR
virtual Matrix< TYPE > & operator-=(const Matrix< TYPE > &other)
Matrix subtraction.
const TYPE * getData() const
returns a const pointer to our data array
Matrix< TYPE > & operator=(const Matrix< TYPE > &other)
Matrix(unsigned int nRows, unsigned int nCols, TYPE val)
Initialize with a size and default value.
virtual void setVal(unsigned int i, unsigned int j, TYPE val)
sets a particular element of the matrix
virtual void getCol(unsigned int i, Vector< TYPE > &col) const
returns a copy of a column of the matrix
Matrix< TYPE > & assign(const Matrix< TYPE > &other)
Copy operator.
A class to represent vectors of numbers.
constexpr TYPE * getData()
returns a pointer to our data array
constexpr unsigned int size() const
return the size (dimension) of the vector
Matrix< TYPE > & multiply(const Matrix< TYPE > &A, const Matrix< TYPE > &B, Matrix< TYPE > &C)
Matrix multiplication.
Matrix< double > DoubleMatrix