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)
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;
105 unsigned int id = i *
d_nCols + j;
112 unsigned int id = i *
d_nCols + j;
122 TYPE *data =
d_data.get();
123 memcpy(
static_cast<void *
>(rData),
static_cast<void *
>(&data[
id]),
133 TYPE *data =
d_data.get();
134 for (j = 0; j <
d_nRows; j++) {
152 "Num rows mismatch in matrix copying");
154 "Num cols mismatch in matrix copying");
155 const TYPE *otherData = other.
getData();
156 TYPE *data =
d_data.get();
157 memcpy(
static_cast<void *
>(data),
static_cast<const void *
>(otherData),
167 "Num rows mismatch in matrix addition");
169 "Num cols mismatch in matrix addition");
170 const TYPE *oData = other.
getData();
172 TYPE *data =
d_data.get();
184 "Num rows mismatch in matrix addition");
186 "Num cols mismatch in matrix addition");
187 const TYPE *oData = other.
getData();
189 TYPE *data =
d_data.get();
199 TYPE *data =
d_data.get();
209 TYPE *data =
d_data.get();
225 unsigned int tRows =
transpose.numRows();
226 unsigned int tCols =
transpose.numCols();
230 unsigned int idA, idAt, idT;
232 TYPE *data =
d_data.get();
233 for (i = 0; i <
d_nRows; i++) {
235 for (j = 0; j <
d_nCols; j++) {
238 tData[idT] = data[idAt];
270 unsigned int aRows = A.
numRows();
271 unsigned int aCols = A.
numCols();
272 unsigned int cRows = C.numRows();
273 unsigned int cCols = C.numCols();
274 unsigned int bRows = B.
numRows();
275 unsigned int bCols = B.
numCols();
276 CHECK_INVARIANT(aCols == bRows,
"Size mismatch during multiplication");
277 CHECK_INVARIANT(aRows == cRows,
"Size mismatch during multiplication");
278 CHECK_INVARIANT(bCols == cCols,
"Size mismatch during multiplication");
281 TYPE *cData = C.getData();
282 const TYPE *bData = B.
getData();
283 const TYPE *aData = A.
getData();
284 unsigned int i, j, k;
285 unsigned int idA, idAt, idB, idC, idCt;
286 for (i = 0; i < aRows; i++) {
289 for (j = 0; j < cCols; j++) {
291 cData[idCt] = (TYPE)0.0;
292 for (k = 0; k < aCols; k++) {
295 cData[idCt] += (aData[idAt] * bData[idB]);
317 unsigned int aRows = A.
numRows();
318 unsigned int aCols = A.
numCols();
319 unsigned int xSiz = x.
size();
320 unsigned int ySiz = y.
size();
324 unsigned int idA, idAt;
325 const TYPE *xData = x.
getData();
326 const TYPE *aData = A.
getData();
328 for (i = 0; i < aRows; i++) {
330 yData[i] = (TYPE)(0.0);
331 for (j = 0; j < aCols; j++) {
333 yData[i] += (aData[idAt] * xData[j]);
346 unsigned int nr = mat.
numRows();
347 unsigned int nc = mat.
numCols();
348 target <<
"Rows: " << mat.
numRows() <<
" Columns: " << mat.
numCols() <<
"\n";
351 for (i = 0; i < nr; i++) {
352 for (j = 0; j < nc; j++) {
353 target << std::setfill(
' ') << std::setw(7) << std::setprecision(3)
354 << 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
returns the number of rows
virtual Matrix< TYPE > & operator*=(TYPE scale)
Multiplication by a scalar.
unsigned int numCols() const
returns the number of columns
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.
unsigned int size() const
return the size (dimension) of the vector
TYPE * getData()
returns a pointer to our data array
Matrix< TYPE > & multiply(const Matrix< TYPE > &A, const Matrix< TYPE > &B, Matrix< TYPE > &C)
Matrix multiplication.
Matrix< double > DoubleMatrix