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),
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;
104 unsigned int id = i *
d_nCols + j;
111 unsigned int id = i *
d_nCols + j;
121 TYPE *data =
d_data.get();
122 memcpy(
static_cast<void *
>(rData),
static_cast<void *
>(&data[
id]),
132 TYPE *data =
d_data.get();
133 for (j = 0; j <
d_nRows; j++) {
151 "Num rows mismatch in matrix copying");
153 "Num cols mismatch in matrix copying");
154 const TYPE *otherData = other.
getData();
155 TYPE *data =
d_data.get();
156 memcpy(
static_cast<void *
>(data),
static_cast<const void *
>(otherData),
166 "Num rows mismatch in matrix addition");
168 "Num cols mismatch in matrix addition");
169 const TYPE *oData = other.
getData();
171 TYPE *data =
d_data.get();
183 "Num rows mismatch in matrix addition");
185 "Num cols mismatch in matrix addition");
186 const TYPE *oData = other.
getData();
188 TYPE *data =
d_data.get();
198 TYPE *data =
d_data.get();
208 TYPE *data =
d_data.get();
224 unsigned int tRows =
transpose.numRows();
225 unsigned int tCols =
transpose.numCols();
229 unsigned int idA, idAt, idT;
231 TYPE *data =
d_data.get();
232 for (i = 0; i <
d_nRows; i++) {
234 for (j = 0; j <
d_nCols; j++) {
237 tData[idT] = data[idAt];
269 unsigned int aRows = A.
numRows();
270 unsigned int aCols = A.
numCols();
271 unsigned int cRows = C.numRows();
272 unsigned int cCols = C.numCols();
273 unsigned int bRows = B.
numRows();
274 unsigned int bCols = B.
numCols();
275 CHECK_INVARIANT(aCols == bRows,
"Size mismatch during multiplication");
276 CHECK_INVARIANT(aRows == cRows,
"Size mismatch during multiplication");
277 CHECK_INVARIANT(bCols == cCols,
"Size mismatch during multiplication");
280 TYPE *cData = C.getData();
281 const TYPE *bData = B.
getData();
282 const TYPE *aData = A.
getData();
283 unsigned int i, j, k;
284 unsigned int idA, idAt, idB, idC, idCt;
285 for (i = 0; i < aRows; i++) {
288 for (j = 0; j < cCols; j++) {
290 cData[idCt] = (TYPE)0.0;
291 for (k = 0; k < aCols; k++) {
294 cData[idCt] += (aData[idAt] * bData[idB]);
316 unsigned int aRows = A.
numRows();
317 unsigned int aCols = A.
numCols();
318 unsigned int xSiz = x.
size();
319 unsigned int ySiz = y.
size();
323 unsigned int idA, idAt;
324 const TYPE *xData = x.
getData();
325 const TYPE *aData = A.
getData();
327 for (i = 0; i < aRows; i++) {
329 yData[i] = (TYPE)(0.0);
330 for (j = 0; j < aCols; j++) {
332 yData[i] += (aData[idAt] * xData[j]);
345 unsigned int nr = mat.
numRows();
346 unsigned int nc = mat.
numCols();
347 target <<
"Rows: " << mat.
numRows() <<
" Columns: " << mat.
numCols() <<
"\n";
350 for (i = 0; i < nr; i++) {
351 for (j = 0; j < nc; j++) {
352 target << std::setfill(
' ') << std::setw(7) << std::setprecision(3)
353 << 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.
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
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(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