83 typename std::vector<T>::iterator
data;
95 Matrix(
bool ownData_ =
true) : nrows(0), ncols(0), ownData(ownData_) {}
103 Matrix(
unsigned int nrows_,
bool ownData_ =
true) {
108 _data.assign(nrows * ncols, T(0.0));
109 data = _data.begin();
120 Matrix(
unsigned int nrows_,
unsigned int ncols_,
bool ownData_ =
true) {
125 _data.assign(nrows * ncols, T(0.0));
126 data = _data.begin();
136 Matrix(
unsigned int nrows_,
unsigned int ncols_,
137 typename std::vector<T>::iterator data_it) {
150 void resize(
unsigned int nrows_,
unsigned int ncols_) {
153 _data.resize(nrows * ncols);
161 if (nrows != ncols)
throw std::runtime_error(
"Matrix is not square");
165 _data.resize(nrows * ncols);
174 for (
unsigned int i = 0; i < nrows *
ncols; i++) sum_ += data[i];
182 for (
unsigned int i = 0; i <
nrows; i++) {
183 for (
unsigned int j = 0; j <
ncols; j++) {
184 std::cout << data[i * ncols + j] <<
" ";
186 std::cout << std::endl;
197 for (
unsigned int i = 0; i <
ncols; i++) {
198 for (
unsigned int j = 0; j <
nrows; j++) {
199 out->
data[i * nrows + j] = data[j * ncols + i];
212 if (ncols != mat->
nrows)
213 throw std::runtime_error(
"Wrong dimensions for matrix product");
216 for (
unsigned int i = 0; i <
nrows; i++) {
217 for (
unsigned int j = 0; j < mat->
ncols; j++) {
219 for (
unsigned int k = 0; k <
ncols; k++) {
221 data[i * ncols + k] * mat->
data[k * mat->
ncols + j];
236 if (nrows == ncols) {
246 if (nrows >= ncols) {
249 inverse = dst->
product(transp);
253 inverse = transp->
product(dst);
271 if (nrows != ncols) {
272 throw std::runtime_error(
273 "Gauss-Jordan inversion: Can't invert Non-square matrix");
279 unsigned int n =
nrows;
282 for (
unsigned int i = 0; i < n; i++) {
283 for (
unsigned int j = 0; j < n; j++) {
284 mat.
_data[i * 2 * n + j] = data[i * n + j];
286 mat.
_data[i * 2 * n + n + i] = 1;
289 for (
unsigned int k = 0; k < n; k++) {
291 while (std::fabs(mat.
_data[i * 2 * n + k]) <
295 throw std::runtime_error(
"Non-invertible matrix");
298 *det *= mat.
_data[i * 2 * n + k];
307 for (
unsigned int j = 0; j < 2 * n; j++) {
308 new_mat.
_data[k * 2 * n + j] /= mat.
_data[k * 2 * n + k];
310 for (i = 0; i < n; i++) {
312 for (
unsigned int j = 0; j < 2 * n; j++) {
313 new_mat.
_data[i * 2 * n + j] -=
314 mat.
_data[i * 2 * n + k] *
315 new_mat.
_data[k * 2 * n + j];
323 for (
unsigned int i = 0; i < n; i++)
324 for (
unsigned int j = 0; j < n; j++)
325 dst->
_data[i * n + j] = mat.
_data[i * 2 * n + n + j];
336 for (
unsigned int k = 0; k <
ncols; k++) {
337 tmp = data[i * ncols + k];
338 data[i * ncols + k] = data[j * ncols + k];
339 data[j * ncols + k] = tmp;
350 for (
unsigned int k = 0; k <
nrows; k++) {
351 tmp = data[k * ncols + i];
352 data[k * ncols + i] = data[k * ncols + j];
353 data[k * ncols + j] = tmp;
Dirty and very incomplete Matrix Class.
Definition: xmmMatrix.hpp:55
void swap_columns(unsigned int i, unsigned int j)
Swap 2 columns of the matrix.
Definition: xmmMatrix.hpp:348
std::vector< T >::iterator data
Data iterator.
Definition: xmmMatrix.hpp:83
Matrix(unsigned int nrows_, unsigned int ncols_, bool ownData_=true)
Constructor.
Definition: xmmMatrix.hpp:120
void resize(unsigned int nrows_, unsigned int ncols_)
Resize the matrix.
Definition: xmmMatrix.hpp:150
float sum() const
Compute the Sum of the matrix.
Definition: xmmMatrix.hpp:172
Matrix< T > * product(Matrix const *mat) const
Compute the product of matrices.
Definition: xmmMatrix.hpp:211
Matrix(bool ownData_=true)
Default Constructor.
Definition: xmmMatrix.hpp:95
Matrix< T > * transpose() const
Compute the transpose matrix.
Definition: xmmMatrix.hpp:195
unsigned int ncols
number of columns of the matrix
Definition: xmmMatrix.hpp:71
Matrix< T > * pinv(double *det) const
Compute the Pseudo-Inverse of a Matrix.
Definition: xmmMatrix.hpp:234
Matrix(unsigned int nrows_, bool ownData_=true)
Square Matrix Constructor.
Definition: xmmMatrix.hpp:103
static const double kEpsilonPseudoInverse()
Epsilon value for Matrix inversion.
Definition: xmmMatrix.hpp:61
Matrix< T > * gauss_jordan_inverse(double *det) const
Compute the Gauss-Jordan Inverse of a Square Matrix.
Definition: xmmMatrix.hpp:270
unsigned int nrows
number of rows of the matrix
Definition: xmmMatrix.hpp:66
Definition: xmmAttribute.hpp:42
Matrix(unsigned int nrows_, unsigned int ncols_, typename std::vector< T >::iterator data_it)
Constructor from vector (shared data)
Definition: xmmMatrix.hpp:136
bool ownData
Defines if the matrix has its own data.
Definition: xmmMatrix.hpp:88
void print() const
Print the matrix.
Definition: xmmMatrix.hpp:181
std::vector< T > _data
Matrix Data if not shared.
Definition: xmmMatrix.hpp:76
void resize(unsigned int nrows_)
Resize a Square Matrix.
Definition: xmmMatrix.hpp:160
void swap_lines(unsigned int i, unsigned int j)
Swap 2 lines of the matrix.
Definition: xmmMatrix.hpp:334