32 #include <gsl/gsl_vector.h>
33 #include <gsl/gsl_matrix.h>
34 #include <gsl/gsl_permutation.h>
35 #include <gsl/gsl_linalg.h>
51 unsigned int _rows,
_cols, _length;
57 TMatrix () : _rows(0), _cols(0), _length(0), _val(0) { }
66 TMatrix (
unsigned int rows,
unsigned int cols ) : _rows(0), _cols(0), _length(0), _val(0)
69 _val =
new double [_length];
70 _rows = rows; _cols = cols;
80 _length = _rows*_cols;
81 if(_val)
delete [] _val;
82 _val =
new double [_length];
83 memcpy(_val,mat.
_val,_length*
sizeof(
double));
92 else if(_val == NULL || _length == 0)
93 error(
"TMatrix::copy_recycle::matrix must be allocated before call\n");
95 for(
unsigned int i = 0; i < _rows; ++i)
96 for(
unsigned int j = 0; j < _cols; ++j)
101 void set (
unsigned int i,
unsigned int j,
double val) {
103 _val[i*_cols + j] = val;
105 error(
"TMatrix::set overflow, i*j > length!\n");
111 for(
unsigned int j = 0, stride = i*_cols; j < _cols; ++j)
112 _val[stride + j] = val;
114 error(
"TMatrix::set_row overflow, i > num rows!\n");
118 void set_row (
unsigned int i, vector<double> vec) {
120 if(vec.size() < _cols)
121 fatal(
"TMatrix::set_row copy from vector too small to fit\n");
124 for(
unsigned int j = 0, stride = i*_cols; j < _cols; ++j)
125 _val[stride + j] = vec[j];
127 error(
"TMatrix::set_row overflow, i > num rows!\n");
133 for(
unsigned int j = 0; j < _rows; ++j)
134 _val[j*_cols + i] = val;
136 error(
"TMatrix::set_col overflow, i > num cols!\n");
140 void set_col (
unsigned int i, vector<double> vec) {
142 if(vec.size() < _rows)
143 fatal(
"TMatrix::set_col copy from vector too small to fit\n");
146 for(
unsigned int j = 0; j < _rows; ++j)
147 _val[j*_cols + i] = vec[j];
149 error(
"TMatrix::set_col overflow, i > num cols!\n");
155 for(
unsigned int i = 0; i < _length; ++i) _val[i] = val;
159 void reset (
unsigned int rows,
unsigned int cols) {
160 _length = rows * cols;
161 if(_length == 0)
error(
"TMatrix::attempt to reset a matrix with size = 0!\n");
162 if(_val != NULL)
delete [] _val;
163 _val =
new double [_length];
164 memset(_val, 0, _length*
sizeof(
double));
165 _rows = rows; _cols = cols;
169 void reset (
unsigned int rows,
unsigned int cols,
double value) {
175 void reset (
unsigned int rows,
unsigned int cols,
const double* array) {
177 memcpy(_val, array, _length *
sizeof(
double));
186 if(_val != NULL)
delete [] _val;
191 double get (
unsigned int i,
unsigned int j)
const {
192 if( !((i+1)*(j+1) > _length) )
193 return _val[i*_cols + j];
195 fatal(
"TMatrix::get overflow!\n");
199 double*
get ()
const {
return _val;}
206 if(dims != NULL) { dims[0] = _rows; dims[1] = _cols; }
211 unsigned int nrows ()
const {
return _rows;}
214 unsigned int ncols ()
const {
return _cols;}
216 unsigned int length ( )
const {
return _length;}
225 error(
"TMatrix::getColumnView: not that many columns in matrix\n");
229 error(
"TMatrix::getColumnView: array size not equal to number of rows in matrix\n");
232 for(
unsigned int i = 0; i < _rows; ++i)
233 array[i] = _val[i*_cols + col];
240 void getRowView (
unsigned int row,
unsigned int n,
double* array)
243 error(
"TMatrix::getRowView: not that many rows in matrix\n");
247 error(
"TMatrix::getRowView: array size not equal to number of columns in matrix\n");
250 for(
unsigned int i = 0, stride = row*_cols; i < _cols; ++i)
251 array[i] = _val[stride + i];
254 void plus (
unsigned int i,
unsigned int j,
double value)
257 _val[i*_cols + j] += value;
259 error(
"TMatrix::plus overflow!\n");
264 for(
unsigned int i = 0; i < _rows; ++i){
265 for(
unsigned int j = 0; j < _cols; ++j){
273 for(
unsigned int i = 0; i < _rows; ++i){
274 for(
unsigned int j = 0; j < _cols; ++j){
280 void minus (
unsigned int i,
unsigned int j,
double value)
283 _val[i*_cols + j] -= value;
285 error(
"TMatrix::minus overflow!\n");
290 for(
unsigned int i = 0; i < _rows; ++i){
291 for(
unsigned int j = 0; j < _cols; ++j){
297 void multi (
unsigned int i,
unsigned int j,
double value)
300 _val[i*_cols + j] *= value;
302 error(
"TMatrix::multi overflow!\n");
307 for(
unsigned int i = 0; i < _rows; ++i){
308 for(
unsigned int j = 0; j < _cols; ++j){
314 void divide (
unsigned int i,
unsigned int j,
double value)
317 _val[i*_cols + j] /= value;
319 error(
"TMatrix::divide overflow!\n");
324 for(
unsigned int i = 0; i < _rows; ++i){
325 for(
unsigned int j = 0; j < _cols; ++j){
335 for(
unsigned int i = 0; i < _rows; i++)
336 for(
unsigned int j = 0; j < _cols; j++)
337 tmp.
set(j, i, get(i, j));
339 reset(_cols, _rows, tmp.
get());
346 for (
unsigned int i = 0; i < _rows; ++i) {
347 sum += _val[i*_cols + col];
356 for (
unsigned int i = 0; i < _cols; ++i) {
357 sum += _val[row*_cols + i];
364 message(
"TMatrix dimensions: \nrows = %i, columns = %i, length = %i\n",_rows,_cols, _length);
365 for(
unsigned int i = 0; i < _rows; i++) {
366 for(
unsigned int j = 0; j < _cols; j++)
367 message(
"%.3f ",_val[i*_cols + j]);
377 for(
unsigned int i = 0; i < _rows; i++) {
379 for(
unsigned int j = 0; j < _cols-1; j++) {
380 OUT<<_val[i*_cols + j]<<
",";
382 OUT<<_val[i*_cols + _cols-1]<<
"}";
393 void get_gsl_matrix(gsl_matrix* M)
395 if(M->size1 != _rows)
396 fatal(
"TMatrix::get_gsl_matrix row size of input matrix doesn't match!\n");
397 if(M->size2 != _cols)
398 fatal(
"TMatrix::get_gsl_matrix col size of input matrix doesn't match!\n");
399 for(
unsigned int i = 0; i < _rows; ++i)
400 for(
unsigned int j = 0; j < _cols; ++j)
401 gsl_matrix_set(M,i,j,_val[i*_cols + j]);
406 void set_from_gsl_matrix (gsl_matrix* M)
408 if(_val != 0)
delete [] _val;
411 _length = _rows*_cols;
412 _val =
new double [_length];
413 memcpy(_val, M->data, _length*
sizeof(
double));
419 error(
"TMatrix::inverse matrix must be square to invert!\n");
423 gsl_matrix *S = gsl_matrix_alloc(_rows,_cols);
425 gsl_matrix *LU = gsl_matrix_alloc(_rows,_cols);
426 gsl_permutation *P = gsl_permutation_alloc(_rows);
429 gsl_matrix_memcpy(LU,S);
430 gsl_linalg_LU_decomp(LU,P,&snum);
431 gsl_linalg_LU_invert(LU,P,S);
433 set_from_gsl_matrix(S);
437 gsl_permutation_free(P);
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:48
void sweep_multiply(double value)
multiply each element by a value.
Definition: tmatrix.h:305
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:159
unsigned int getNbRows() const
Gives the number of rows.
Definition: tmatrix.h:210
unsigned int _cols
Definition: tmatrix.h:51
double * get() const
Accessor to the whole array.
Definition: tmatrix.h:199
double * getValArray() const
Definition: tmatrix.h:200
double colSum(unsigned int col)
Sum all elements in a column.
Definition: tmatrix.h:342
void set_col(unsigned int i, vector< double > vec)
Sets element at column i to values stored in vector vec.
Definition: tmatrix.h:140
void set_row(unsigned int i, double val)
Sets all elements at row i to value val.
Definition: tmatrix.h:109
void show_up()
Definition: tmatrix.h:362
unsigned int ncols() const
Definition: tmatrix.h:214
TMatrix()
Definition: tmatrix.h:57
void copy(const TMatrix &mat)
Copy a matrix.
Definition: tmatrix.h:76
void sweep_minus(double value)
Substracts a value to all elements in a matrix.
Definition: tmatrix.h:288
unsigned int getNbCols() const
Gives the number of columns.
Definition: tmatrix.h:213
void matrix_increment(double value)
Adds a value to all elements in a matrix.
Definition: tmatrix.h:262
void set(unsigned int i, unsigned int j, double val)
Sets element at row i and column j to value val.
Definition: tmatrix.h:101
double get(unsigned int i, unsigned int j) const
Accessor to element at row i and column j.
Definition: tmatrix.h:191
void getColumnView(unsigned int col, unsigned int n, double *array)
Gives access to a column of the matrix.
Definition: tmatrix.h:222
void set_col(unsigned int i, double val)
Sets element at column i to value val.
Definition: tmatrix.h:131
void assign(double val)
Assigns a value to all element of the matrix.
Definition: tmatrix.h:153
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:88
unsigned int _rows
Definition: tmatrix.h:51
void divide(unsigned int i, unsigned int j, double value)
Divide an element of the matrix by a value.
Definition: tmatrix.h:314
void sweep_plus(double value)
Adds a value to all elements in a matrix.
Definition: tmatrix.h:271
void reset()
Reset members to zero state.
Definition: tmatrix.h:181
void transpose()
Transpose the matrix, swaps columns for rows.
Definition: tmatrix.h:331
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:175
double rowSum(unsigned int row)
Sum all elements in a row.
Definition: tmatrix.h:352
void minus(unsigned int i, unsigned int j, double value)
Substracts a value from an element of the matrix.
Definition: tmatrix.h:280
void set_row(unsigned int i, vector< double > vec)
Sets elements at row i to values stored in vector vec.
Definition: tmatrix.h:118
void multi(unsigned int i, unsigned int j, double value)
Multiply an element of the matrix by a value.
Definition: tmatrix.h:297
void plus(unsigned int i, unsigned int j, double value)
Adds a value to an element of the matrix.
Definition: tmatrix.h:254
TMatrix(const TMatrix &mat)
copy constructor.
Definition: tmatrix.h:60
double * _val
Definition: tmatrix.h:53
unsigned int get_dims(unsigned int *dims) const
Accessor to the matrix dimensions.
Definition: tmatrix.h:205
~TMatrix()
Definition: tmatrix.h:73
unsigned int length() const
Returns the number of elements in the matrix.
Definition: tmatrix.h:216
void sweep_divide(double value)
Divide all elements of the matrix by a value.
Definition: tmatrix.h:322
string to_string()
Writes the matrix into a string in Nemo's matrix input format.
Definition: tmatrix.h:372
void getRowView(unsigned int row, unsigned int n, double *array)
Gives access to a row of the matrix.
Definition: tmatrix.h:240
TMatrix(unsigned int rows, unsigned int cols)
Creates an array of doubles of size = rows*cols.
Definition: tmatrix.h:66
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:169
unsigned int nrows() const
Definition: tmatrix.h:211
void fatal(const char *str,...)
Definition: output.cc:98
int error(const char *str,...)
Definition: output.cc:77
void message(const char *message,...)
Definition: output.cc:38