33 #include <gsl/gsl_vector.h>
34 #include <gsl/gsl_matrix.h>
35 #include <gsl/gsl_permutation.h>
36 #include <gsl/gsl_linalg.h>
52 unsigned int _rows,
_cols, _length;
58 TMatrix () : _rows(0), _cols(0), _length(0), _val(0) { }
67 TMatrix (
unsigned int rows,
unsigned int cols ) : _rows(0), _cols(0), _length(0), _val(0)
70 _val =
new double [_length];
71 _rows = rows; _cols = cols;
81 _length = _rows*_cols;
82 if(_val)
delete [] _val;
83 _val =
new double [_length];
84 memcpy(_val,mat.
_val,_length*
sizeof(
double));
93 else if(_val == NULL || _length == 0)
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) {
104 _val[i*_cols + j] = val;
106 error(
"TMatrix::set overflow, i*j > length!\n");
112 for(
unsigned int j = 0, stride = i*_cols; j < _cols; ++j)
113 _val[stride + j] = val;
115 error(
"TMatrix::set_row overflow, i > num rows!\n");
119 void set_row (
unsigned int i, vector<double> vec) {
121 if(vec.size() < _cols)
122 fatal(
"TMatrix::set_row copy from vector too small to fit\n");
125 for(
unsigned int j = 0, stride = i*_cols; j < _cols; ++j)
126 _val[stride + j] = vec[j];
128 error(
"TMatrix::set_row overflow, i > num rows!\n");
134 for(
unsigned int j = 0; j < _rows; ++j)
135 _val[j*_cols + i] = val;
137 error(
"TMatrix::set_col overflow, i > num cols!\n");
141 void set_col (
unsigned int i, vector<double> vec) {
143 if(vec.size() < _rows)
144 fatal(
"TMatrix::set_col copy from vector too small to fit\n");
147 for(
unsigned int j = 0; j < _rows; ++j)
148 _val[j*_cols + i] = vec[j];
150 error(
"TMatrix::set_col overflow, i > num cols!\n");
156 for(
unsigned int i = 0; i < _length; ++i) _val[i] = val;
160 void reset (
unsigned int rows,
unsigned int cols) {
161 _length = rows * cols;
162 if(_length == 0)
error(
"TMatrix::attempt to reset a matrix with size = 0!\n");
163 if(_val != NULL)
delete [] _val;
164 _val =
new double [_length];
165 memset(_val, 0, _length*
sizeof(
double));
166 _rows = rows; _cols = cols;
170 void reset (
unsigned int rows,
unsigned int cols,
double value) {
176 void reset (
unsigned int rows,
unsigned int cols,
const double* array) {
178 memcpy(_val, array, _length *
sizeof(
double));
187 if(_val != NULL)
delete [] _val;
192 double get (
unsigned int i,
unsigned int j)
const {
193 if( !((i+1)*(j+1) > _length) )
194 return _val[i*_cols + j];
196 fatal(
"TMatrix::get overflow!\n");
200 double*
get ()
const {
return _val;}
207 if(dims != NULL) { dims[0] = _rows; dims[1] = _cols; }
212 unsigned int nrows ()
const {
return _rows;}
215 unsigned int ncols ()
const {
return _cols;}
217 unsigned int length ( )
const {
return _length;}
226 error(
"TMatrix::getColumnView: not that many columns in matrix\n");
230 error(
"TMatrix::getColumnView: array size not equal to number of rows in matrix\n");
233 for(
unsigned int i = 0; i < _rows; ++i)
234 array[i] = _val[i*_cols + col];
241 void getRowView (
unsigned int row,
unsigned int n,
double* array)
244 error(
"TMatrix::getRowView: not that many rows in matrix\n");
248 error(
"TMatrix::getRowView: array size not equal to number of columns in matrix\n");
251 for(
unsigned int i = 0, stride = row*_cols; i < _cols; ++i)
252 array[i] = _val[stride + i];
255 void plus (
unsigned int i,
unsigned int j,
double value)
258 _val[i*_cols + j] += value;
260 error(
"TMatrix::plus overflow!\n");
265 for(
unsigned int i = 0; i < _rows; ++i){
266 for(
unsigned int j = 0; j < _cols; ++j){
274 for(
unsigned int i = 0; i < _rows; ++i){
275 for(
unsigned int j = 0; j < _cols; ++j){
281 void minus (
unsigned int i,
unsigned int j,
double value)
284 _val[i*_cols + j] -= value;
286 error(
"TMatrix::minus overflow!\n");
291 for(
unsigned int i = 0; i < _rows; ++i){
292 for(
unsigned int j = 0; j < _cols; ++j){
298 void multi (
unsigned int i,
unsigned int j,
double value)
301 _val[i*_cols + j] *= value;
303 error(
"TMatrix::multi overflow!\n");
308 for(
unsigned int i = 0; i < _rows; ++i){
309 for(
unsigned int j = 0; j < _cols; ++j){
315 void divide (
unsigned int i,
unsigned int j,
double value)
318 _val[i*_cols + j] /= value;
320 error(
"TMatrix::divide overflow!\n");
325 for(
unsigned int i = 0; i < _rows; ++i){
326 for(
unsigned int j = 0; j < _cols; ++j){
336 for(
unsigned int i = 0; i < _rows; i++)
337 for(
unsigned int j = 0; j < _cols; j++)
338 tmp.
set(j, i, get(i, j));
340 reset(_cols, _rows, tmp.
get());
347 for (
unsigned int i = 0; i < _rows; ++i) {
348 sum += _val[i*_cols + col];
357 for (
unsigned int i = 0; i < _cols; ++i) {
358 sum += _val[row*_cols + i];
365 message(
"TMatrix dimensions: \nrows = %i, columns = %i, length = %i\n",_rows,_cols, _length);
366 for(
unsigned int i = 0; i < _rows; i++) {
367 for(
unsigned int j = 0; j < _cols; j++)
368 message(
"%.3f ",_val[i*_cols + j]);
378 for(
unsigned int i = 0; i < _rows; i++) {
380 for(
unsigned int j = 0; j < _cols-1; j++) {
381 OUT<<_val[i*_cols + j]<<
",";
383 OUT<<_val[i*_cols + _cols-1]<<
"}";
394 void get_gsl_matrix(gsl_matrix* M)
396 if(M->size1 != _rows)
397 fatal(
"TMatrix::get_gsl_matrix row size of input matrix doesn't match!\n");
398 if(M->size2 != _cols)
399 fatal(
"TMatrix::get_gsl_matrix col size of input matrix doesn't match!\n");
400 for(
unsigned int i = 0; i < _rows; ++i)
401 for(
unsigned int j = 0; j < _cols; ++j)
402 gsl_matrix_set(M,i,j,_val[i*_cols + j]);
407 void set_from_gsl_matrix (gsl_matrix* M)
409 if(_val != 0)
delete [] _val;
412 _length = _rows*_cols;
413 _val =
new double [_length];
414 memcpy(_val, M->data, _length*
sizeof(
double));
420 error(
"TMatrix::inverse matrix must be square to invert!\n");
424 gsl_matrix *S = gsl_matrix_alloc(_rows,_cols);
426 gsl_matrix *LU = gsl_matrix_alloc(_rows,_cols);
427 gsl_permutation *P = gsl_permutation_alloc(_rows);
430 gsl_matrix_memcpy(LU,S);
431 gsl_linalg_LU_decomp(LU,P,&snum);
432 gsl_linalg_LU_invert(LU,P,S);
434 set_from_gsl_matrix(S);
438 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 sweep_multiply(double value)
multiply each element by a value.
Definition: tmatrix.h:306
void reset(unsigned int rows, unsigned int cols)
Re-allocate the existing matrix with assigned rows and cols dimensions and all elements to 0.
Definition: tmatrix.h:160
unsigned int getNbRows() const
Gives the number of rows.
Definition: tmatrix.h:211
unsigned int _cols
Definition: tmatrix.h:52
double * get() const
Accessor to the whole array.
Definition: tmatrix.h:200
double * getValArray() const
Definition: tmatrix.h:201
double colSum(unsigned int col)
Sum all elements in a column.
Definition: tmatrix.h:343
void set_col(unsigned int i, vector< double > vec)
Sets element at column i to values stored in vector vec.
Definition: tmatrix.h:141
void set_row(unsigned int i, double val)
Sets all elements at row i to value val.
Definition: tmatrix.h:110
void show_up()
Definition: tmatrix.h:363
unsigned int ncols() const
Definition: tmatrix.h:215
TMatrix()
Definition: tmatrix.h:58
void copy(const TMatrix &mat)
Copy a matrix.
Definition: tmatrix.h:77
void sweep_minus(double value)
Substracts a value to all elements in a matrix.
Definition: tmatrix.h:289
unsigned int getNbCols() const
Gives the number of columns.
Definition: tmatrix.h:214
void matrix_increment(double value)
Adds a value to all elements in a matrix.
Definition: tmatrix.h:263
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
double get(unsigned int i, unsigned int j) const
Accessor to element at row i and column j.
Definition: tmatrix.h:192
void getColumnView(unsigned int col, unsigned int n, double *array)
Gives access to a column of the matrix.
Definition: tmatrix.h:223
void set_col(unsigned int i, double val)
Sets element at column i to value val.
Definition: tmatrix.h:132
void assign(double val)
Assigns a value to all element of the matrix.
Definition: tmatrix.h:154
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:315
void sweep_plus(double value)
Adds a value to all elements in a matrix.
Definition: tmatrix.h:272
void reset()
Reset members to zero state.
Definition: tmatrix.h:182
void transpose()
Transpose the matrix, swaps columns for rows.
Definition: tmatrix.h:332
void reset(unsigned int rows, unsigned int cols, const double *array)
Reset the existing matrix to the new dimensions and copies the array, which has to be of the same tot...
Definition: tmatrix.h:176
double rowSum(unsigned int row)
Sum all elements in a row.
Definition: tmatrix.h:353
void minus(unsigned int i, unsigned int j, double value)
Substracts a value from an element of the matrix.
Definition: tmatrix.h:281
void set_row(unsigned int i, vector< double > vec)
Sets elements at row i to values stored in vector vec.
Definition: tmatrix.h:119
void multi(unsigned int i, unsigned int j, double value)
Multiply an element of the matrix by a value.
Definition: tmatrix.h:298
void plus(unsigned int i, unsigned int j, double value)
Adds a value to an element of the matrix.
Definition: tmatrix.h:255
TMatrix(const TMatrix &mat)
copy constructor.
Definition: tmatrix.h:61
double * _val
Definition: tmatrix.h:54
unsigned int get_dims(unsigned int *dims) const
Accessor to the matrix dimensions.
Definition: tmatrix.h:206
~TMatrix()
Definition: tmatrix.h:74
unsigned int length() const
Returns the number of elements in the matrix.
Definition: tmatrix.h:217
void sweep_divide(double value)
Divide all elements of the matrix by a value.
Definition: tmatrix.h:323
string to_string()
Writes the matrix into a string in Nemo's matrix input format.
Definition: tmatrix.h:373
void getRowView(unsigned int row, unsigned int n, double *array)
Gives access to a row of the matrix.
Definition: tmatrix.h:241
TMatrix(unsigned int rows, unsigned int cols)
Creates an array of doubles of size = rows*cols.
Definition: tmatrix.h:67
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:170
unsigned int nrows() const
Definition: tmatrix.h:212
void fatal(const char *str,...)
Definition: output.cc:99
int error(const char *str,...)
Definition: output.cc:78
void message(const char *message,...)
Definition: output.cc:39