11#ifndef __RD_SYMM_MATRIX_H__
12#define __RD_SYMM_MATRIX_H__
17#include <boost/smart_ptr.hpp>
36 memset(
static_cast<void *
>(data), 0,
d_dataSize *
sizeof(TYPE));
58 const TYPE *otherData = other.
getData();
60 memcpy(
static_cast<void *
>(data),
static_cast<const void *
>(otherData),
77 memset(
static_cast<void *
>(data), 0,
d_dataSize *
sizeof(TYPE));
78 for (
unsigned int i = 0; i <
d_size; i++) {
79 data[i * (i + 3) / 2] = (TYPE)1.0;
83 TYPE
getVal(
unsigned int i,
unsigned int j)
const {
88 id = i * (i + 1) / 2 + j;
90 id = j * (j + 1) / 2 + i;
95 void setVal(
unsigned int i,
unsigned int j, TYPE val) {
100 id = i * (i + 1) / 2 + j;
102 id = j * (j + 1) / 2 + i;
110 TYPE *data =
d_data.get();
111 for (
unsigned int j = 0; j <
d_size; j++) {
114 id = i * (i + 1) / 2 + j;
116 id = j * (j + 1) / 2 + i;
125 TYPE *data =
d_data.get();
126 for (
unsigned int j = 0; j <
d_size; j++) {
129 id = j * (j + 1) / 2 + i;
131 id = i * (i + 1) / 2 + j;
144 TYPE *data =
d_data.get();
145 for (
unsigned int i = 0; i <
d_dataSize; i++) {
152 TYPE *data =
d_data.get();
153 for (
unsigned int i = 0; i <
d_dataSize; i++) {
161 "Sizes don't match in the addition");
162 const TYPE *oData = other.
getData();
163 TYPE *data =
d_data.get();
164 for (
unsigned int i = 0; i <
d_dataSize; i++) {
172 "Sizes don't match in the addition");
173 const TYPE *oData = other.
getData();
174 TYPE *data =
d_data.get();
175 for (
unsigned int i = 0; i <
d_dataSize; i++) {
184 "Size mismatch during multiplication");
186 const TYPE *bData = B.
getData();
187 TYPE *data =
d_data.get();
188 for (
unsigned int i = 0; i <
d_size; i++) {
189 unsigned int idC = i * (i + 1) / 2;
190 for (
unsigned int j = 0; j < i + 1; j++) {
191 unsigned int idCt = idC + j;
192 cData[idCt] = (TYPE)0.0;
193 for (
unsigned int k = 0; k <
d_size; k++) {
194 unsigned int idA, idB;
196 idA = i * (i + 1) / 2 + k;
198 idA = k * (k + 1) / 2 + i;
201 idB = j * (j + 1) / 2 + k;
203 idB = k * (k + 1) / 2 + j;
205 cData[idCt] += (data[idA] * bData[idB]);
210 for (
unsigned int i = 0; i <
d_dataSize; i++) {
221 "Size mismatch during transposing");
223 TYPE *data =
d_data.get();
224 for (
unsigned int i = 0; i <
d_dataSize; i++) {
264 unsigned int aSize = A.
numRows();
266 "Size mismatch in matric multiplication");
268 "Size mismatch in matric multiplication");
269 TYPE *cData = C.getData();
270 const TYPE *aData = A.
getData();
271 const TYPE *bData = B.
getData();
272 for (
unsigned int i = 0; i < aSize; i++) {
273 unsigned int idC = i * (i + 1) / 2;
274 for (
unsigned int j = 0; j < i + 1; j++) {
275 unsigned int idCt = idC + j;
276 cData[idCt] = (TYPE)0.0;
277 for (
unsigned int k = 0; k < aSize; k++) {
278 unsigned int idA, idB;
280 idA = i * (i + 1) / 2 + k;
282 idA = k * (k + 1) / 2 + i;
285 idB = j * (j + 1) / 2 + k;
287 idB = k * (k + 1) / 2 + j;
289 cData[idCt] += (aData[idA] * bData[idB]);
315 unsigned int aSize = A.
numRows();
318 const TYPE *xData = x.
getData();
319 const TYPE *aData = A.
getData();
321 for (
unsigned int i = 0; i < aSize; i++) {
322 yData[i] = (TYPE)(0.0);
323 unsigned int idA = i * (i + 1) / 2;
324 for (
unsigned int j = 0; j < i + 1; j++) {
326 yData[i] += (aData[idA] * xData[j]);
330 for (
unsigned int j = i + 1; j < aSize; j++) {
333 yData[i] += (aData[idA] * xData[j]);
348 unsigned int nr = mat.
numRows();
349 unsigned int nc = mat.
numCols();
350 target <<
"Rows: " << mat.
numRows() <<
" Columns: " << mat.
numCols() <<
"\n";
352 for (
unsigned int i = 0; i < nr; i++) {
353 for (
unsigned int j = 0; j < nc; j++) {
354 target << std::setw(7) << std::setprecision(3) << mat.
getVal(i, j);
#define CHECK_INVARIANT(expr, mess)
#define URANGE_CHECK(x, hi)
std::ostream & operator<<(std::ostream &target, const RDNumeric::SymmMatrix< TYPE > &mat)
ostream operator for Matrix's
A symmetric matrix class.
SymmMatrix(unsigned int N)
SymmMatrix< TYPE > & transposeInplace()
SymmMatrix< TYPE > & transpose(SymmMatrix< TYPE > &transpose) const
const TYPE * getData() const
returns a const pointer to our data array
SymmMatrix< TYPE > & operator/=(TYPE scale)
SymmMatrix< TYPE > & operator*=(TYPE scale)
void getCol(unsigned int i, Vector< TYPE > &col)
void setVal(unsigned int i, unsigned int j, TYPE val)
void getRow(unsigned int i, Vector< TYPE > &row)
unsigned int numCols() const
returns the number of columns
SymmMatrix(unsigned int N, DATA_SPTR data)
unsigned int numRows() const
returns the number of rows
unsigned int getDataSize() const
TYPE getVal(unsigned int i, unsigned int j) const
SymmMatrix< TYPE > & operator+=(const SymmMatrix< TYPE > &other)
SymmMatrix< TYPE > & operator*=(const SymmMatrix< TYPE > &B)
in-place matrix multiplication
SymmMatrix(const SymmMatrix< TYPE > &other)
TYPE * getData()
returns a pointer to our data array
SymmMatrix< TYPE > & operator-=(const SymmMatrix< TYPE > &other)
boost::shared_array< TYPE > DATA_SPTR
SymmMatrix(unsigned int N, TYPE val)
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
SymmMatrix< unsigned int > UintSymmMatrix
SymmMatrix< double > DoubleSymmMatrix
Matrix< TYPE > & multiply(const Matrix< TYPE > &A, const Matrix< TYPE > &B, Matrix< TYPE > &C)
Matrix multiplication.
SymmMatrix< int > IntSymmMatrix