34#include <gsl/gsl_vector.h>
35#include <gsl/gsl_matrix.h>
36#include <gsl/gsl_permutation.h>
37#include <gsl/gsl_linalg.h>
94 error(
"TMatrix::copy_recycle::matrix must be allocated before call\n");
96 for(
unsigned int i = 0; i <
_rows; ++i)
97 for(
unsigned int j = 0; j <
_cols; ++j)
102 void set (
unsigned int i,
unsigned int j,
double val) {
106 error(
"TMatrix::set overflow!\n");
112 for(
unsigned int i = 0; i <
_length; ++i)
_val[i] = val;
116 void reset (
unsigned int rows,
unsigned int cols) {
125 void reset (
unsigned int rows,
unsigned int cols,
double value) {
131 void reset (
unsigned int rows,
unsigned int cols,
double* array) {
147 double get (
unsigned int i,
unsigned int j) {
151 fatal(
"TMatrix::get overflow!\n");
162 if(dims != NULL) { dims[0] =
_rows; dims[1] =
_cols; }
181 error(
"TMatrix::getColumnView: not that many columns in matrix\n");
185 error(
"TMatrix::getColumnView: array size not equal to number of rows in matrix\n");
188 for(
unsigned int i = 0; i <
_rows; ++i)
196 void getRowView (
unsigned int row,
unsigned int n,
double* array)
199 error(
"TMatrix::getRowView: not that many rows in matrix\n");
203 error(
"TMatrix::getRowView: array size not equal to number of columns in matrix\n");
206 for(
unsigned int i = 0, stride = row*
_cols; i <
_cols; ++i)
207 array[i] =
_val[stride + i];
210 void plus (
unsigned int i,
unsigned int j,
double value)
215 error(
"TMatrix::plus overflow!\n");
220 for(
unsigned int i = 0; i <
_rows; ++i){
221 for(
unsigned int j = 0; j <
_cols; ++j){
227 void minus (
unsigned int i,
unsigned int j,
double value)
232 error(
"TMatrix::minus overflow!\n");
235 void multi (
unsigned int i,
unsigned int j,
double value)
240 error(
"TMatrix::multi overflow!\n");
243 void divide (
unsigned int i,
unsigned int j,
double value)
248 error(
"TMatrix::divide overflow!\n");
255 for(
unsigned int i = 0; i <
_rows; i++)
256 for(
unsigned int j = 0; j <
_cols; j++)
266 for (
unsigned int i = 0; i <
_rows; ++i) {
276 for (
unsigned int i = 0; i <
_cols; ++i) {
285 for(
unsigned int i = 0; i <
_rows; i++) {
286 for(
unsigned int j = 0; j <
_cols; j++)
297 for(
unsigned int i = 0; i <
_rows; i++) {
299 for(
unsigned int j = 0; j <
_cols-1; j++) {
313 void get_gsl_matrix(gsl_matrix* M)
315 if(M->size1 !=
_rows)
316 fatal(
"TMatrix::get_gsl_matrix row size of input matrix doesn't match!\n");
317 if(M->size2 !=
_cols)
318 fatal(
"TMatrix::get_gsl_matrix col size of input matrix doesn't match!\n");
319 for(
unsigned int i = 0; i <
_rows; ++i)
320 for(
unsigned int j = 0; j <
_cols; ++j)
326 void set_from_gsl_matrix (gsl_matrix* M)
339 error(
"TMatrix::inverse matrix must be square to invert!\n");
343 gsl_matrix *S = gsl_matrix_alloc(
_rows,
_cols);
345 gsl_matrix *LU = gsl_matrix_alloc(
_rows,
_cols);
346 gsl_permutation *P = gsl_permutation_alloc(
_rows);
349 gsl_matrix_memcpy(LU,S);
350 gsl_linalg_LU_decomp(LU,P,&snum);
351 gsl_linalg_LU_invert(LU,P,S);
353 set_from_gsl_matrix(S);
357 gsl_permutation_free(P);
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:49
void reset(unsigned int rows, unsigned int cols)
Re-allocate the existing matrix with assigned rows and cols dimensions.
Definition: tmatrix.h:116
unsigned int _cols
Definition: tmatrix.h:52
unsigned int length()
Returns the number of elements in the matrix.
Definition: tmatrix.h:172
double * getValArray() const
Definition: tmatrix.h:156
double colSum(unsigned int col)
Sum all elements in a column.
Definition: tmatrix.h:262
void show_up()
Definition: tmatrix.h:282
TMatrix()
Definition: tmatrix.h:58
unsigned int ncols()
Definition: tmatrix.h:170
void copy(const TMatrix &mat)
Copy a matrix.
Definition: tmatrix.h:77
void matrix_increment(double value)
Adds a value to all elements in a matrix.
Definition: tmatrix.h:218
void set(unsigned int i, unsigned int j, double val)
Sets element at row i and column j to value val.
Definition: tmatrix.h:102
unsigned int get_dims(unsigned int *dims)
Accessor to the matrix dimensions.
Definition: tmatrix.h:161
void getColumnView(unsigned int col, unsigned int n, double *array)
Gives access to a column of the matrix.
Definition: tmatrix.h:178
void reset(unsigned int rows, unsigned int cols, double *array)
Reset the existing matrix to the new dimensions and copies the array.
Definition: tmatrix.h:131
void assign(double val)
Assigns a value to all element of the matrix.
Definition: tmatrix.h:110
void copy_recycle(const TMatrix &mat)
Copy elements of 'mat', recycling elements of 'mat' if its size is smaller than current matrix.
Definition: tmatrix.h:89
unsigned int _rows
Definition: tmatrix.h:52
void divide(unsigned int i, unsigned int j, double value)
Divide an element of the matrix by a value.
Definition: tmatrix.h:243
void reset()
Reset members to zero state.
Definition: tmatrix.h:137
void transpose()
Transpose the matrix, swaps columns for rows.
Definition: tmatrix.h:251
double rowSum(unsigned int row)
Sum all elements in a row.
Definition: tmatrix.h:272
void minus(unsigned int i, unsigned int j, double value)
Substracts a value from an element of the matrix.
Definition: tmatrix.h:227
double * get() const
Accessor to the whole array.
Definition: tmatrix.h:155
double get(unsigned int i, unsigned int j)
Accessor to element at row i and column j.
Definition: tmatrix.h:147
unsigned int getNbRows()
Gives the number of rows.
Definition: tmatrix.h:166
unsigned int nrows()
Definition: tmatrix.h:167
void multi(unsigned int i, unsigned int j, double value)
Multiply an element of the matrix by a value.
Definition: tmatrix.h:235
void plus(unsigned int i, unsigned int j, double value)
Adds a value to an element of the matrix.
Definition: tmatrix.h:210
TMatrix(const TMatrix &mat)
copy constructor.
Definition: tmatrix.h:61
double * _val
Definition: tmatrix.h:54
unsigned int _length
Definition: tmatrix.h:52
~TMatrix()
Definition: tmatrix.h:74
string to_string()
Writes the matrix into a string in Nemo's matrix input format.
Definition: tmatrix.h:292
void getRowView(unsigned int row, unsigned int n, double *array)
Gives access to a row of the matrix.
Definition: tmatrix.h:196
TMatrix(unsigned int rows, unsigned int cols)
Creates an array of doubles of size = rows*cols.
Definition: tmatrix.h:67
unsigned int getNbCols()
Gives the number of columns.
Definition: tmatrix.h:169
void reset(unsigned int rows, unsigned int cols, double value)
Reset the existing matrix to the new dimensions and copies the value to all elements.
Definition: tmatrix.h:125
void fatal(const char *str,...)
Definition: output.cc:96
int error(const char *str,...)
Definition: output.cc:77
void message(const char *message,...)
Definition: output.cc:40