34 #include <gsl/gsl_vector.h>
35 #include <gsl/gsl_matrix.h>
36 #include <gsl/gsl_permutation.h>
37 #include <gsl/gsl_linalg.h>
53 unsigned int _rows,
_cols, _length;
59 TMatrix () : _rows(0), _cols(0), _length(0), _val(0) { }
68 TMatrix (
unsigned int rows,
unsigned int cols ) : _rows(0), _cols(0), _length(0), _val(0)
71 _val =
new double [_length];
72 _rows = rows; _cols = cols;
82 _length = _rows*_cols;
83 if(_val)
delete [] _val;
84 _val =
new double [_length];
85 memcpy(_val,mat.
_val,_length*
sizeof(
double));
94 else if(_val == NULL || _length == 0)
95 error(
"TMatrix::copy_recycle::matrix must be allocated before call\n");
97 for(
unsigned int i = 0; i < _rows; ++i)
98 for(
unsigned int j = 0; j < _cols; ++j)
103 void set (
unsigned int i,
unsigned int j,
double val) {
105 _val[i*_cols + j] = val;
107 error(
"TMatrix::set overflow, i*j > length!\n");
113 for(
unsigned int j = 0, stride = i*_cols; j < _cols; ++j)
114 _val[stride + j] = val;
116 error(
"TMatrix::set_row overflow, i > num rows!\n");
120 void set_row (
unsigned int i, vector<double> vec) {
122 if(vec.size() < _cols)
123 fatal(
"TMatrix::set_row copy from vector too small to fit\n");
126 for(
unsigned int j = 0, stride = i*_cols; j < _cols; ++j)
127 _val[stride + j] = vec[j];
129 error(
"TMatrix::set_row overflow, i > num rows!\n");
135 for(
unsigned int j = 0; j < _rows; ++j)
136 _val[j*_cols + i] = val;
138 error(
"TMatrix::set_col overflow, i > num cols!\n");
142 void set_col (
unsigned int i, vector<double> vec) {
144 if(vec.size() < _rows)
145 fatal(
"TMatrix::set_col copy from vector too small to fit\n");
148 for(
unsigned int j = 0; j < _rows; ++j)
149 _val[j*_cols + i] = vec[j];
151 error(
"TMatrix::set_col overflow, i > num cols!\n");
157 for(
unsigned int i = 0; i < _length; ++i) _val[i] = val;
161 void reset (
unsigned int rows,
unsigned int cols) {
162 _length = rows * cols;
163 if(_length == 0)
error(
"TMatrix::attempt to reset a matrix with size = 0!\n");
164 if(_val != NULL)
delete [] _val;
165 _val =
new double [_length];
166 memset(_val, 0, _length*
sizeof(
double));
167 _rows = rows; _cols = cols;
171 void reset (
unsigned int rows,
unsigned int cols,
double value) {
177 void reset (
unsigned int rows,
unsigned int cols,
const double* array) {
179 memcpy(_val, array, _length *
sizeof(
double));
188 if(_val != NULL)
delete [] _val;
193 double get (
unsigned int i,
unsigned int j)
const {
194 if( !((i+1)*(j+1) > _length) )
195 return _val[i*_cols + j];
197 fatal(
"TMatrix::get overflow!\n");
201 double*
get ()
const {
return _val;}
208 if(dims != NULL) { dims[0] = _rows; dims[1] = _cols; }
213 unsigned int nrows ()
const {
return _rows;}
216 unsigned int ncols ()
const {
return _cols;}
218 unsigned int length ( )
const {
return _length;}
227 error(
"TMatrix::getColumnView: not that many columns in matrix\n");
231 error(
"TMatrix::getColumnView: array size not equal to number of rows in matrix\n");
234 for(
unsigned int i = 0; i < _rows; ++i)
235 array[i] = _val[i*_cols + col];
242 void getRowView (
unsigned int row,
unsigned int n,
double* array)
245 error(
"TMatrix::getRowView: not that many rows in matrix\n");
249 error(
"TMatrix::getRowView: array size not equal to number of columns in matrix\n");
252 for(
unsigned int i = 0, stride = row*_cols; i < _cols; ++i)
253 array[i] = _val[stride + i];
256 void plus (
unsigned int i,
unsigned int j,
double value)
259 _val[i*_cols + j] += value;
261 error(
"TMatrix::plus overflow!\n");
266 for(
unsigned int i = 0; i < _rows; ++i){
267 for(
unsigned int j = 0; j < _cols; ++j){
273 void minus (
unsigned int i,
unsigned int j,
double value)
276 _val[i*_cols + j] -= value;
278 error(
"TMatrix::minus overflow!\n");
281 void multi (
unsigned int i,
unsigned int j,
double value)
284 _val[i*_cols + j] *= value;
286 error(
"TMatrix::multi overflow!\n");
289 void divide (
unsigned int i,
unsigned int j,
double value)
292 _val[i*_cols + j] /= value;
294 error(
"TMatrix::divide overflow!\n");
301 for(
unsigned int i = 0; i < _rows; i++)
302 for(
unsigned int j = 0; j < _cols; j++)
303 tmp.
set(j, i, get(i, j));
305 reset(_cols, _rows, tmp.
get());
312 for (
unsigned int i = 0; i < _rows; ++i) {
313 sum += _val[i*_cols + col];
322 for (
unsigned int i = 0; i < _cols; ++i) {
323 sum += _val[row*_cols + i];
330 message(
"TMatrix dimensions: \nrows = %i, columns = %i, length = %i\n",_rows,_cols, _length);
331 for(
unsigned int i = 0; i < _rows; i++) {
332 for(
unsigned int j = 0; j < _cols; j++)
333 message(
"%.3f ",_val[i*_cols + j]);
343 for(
unsigned int i = 0; i < _rows; i++) {
345 for(
unsigned int j = 0; j < _cols-1; j++) {
346 OUT<<_val[i*_cols + j]<<
",";
348 OUT<<_val[i*_cols + _cols-1]<<
"}";
359 void get_gsl_matrix(gsl_matrix* M)
361 if(M->size1 != _rows)
362 fatal(
"TMatrix::get_gsl_matrix row size of input matrix doesn't match!\n");
363 if(M->size2 != _cols)
364 fatal(
"TMatrix::get_gsl_matrix col size of input matrix doesn't match!\n");
365 for(
unsigned int i = 0; i < _rows; ++i)
366 for(
unsigned int j = 0; j < _cols; ++j)
367 gsl_matrix_set(M,i,j,_val[i*_cols + j]);
372 void set_from_gsl_matrix (gsl_matrix* M)
374 if(_val != 0)
delete [] _val;
377 _length = _rows*_cols;
378 _val =
new double [_length];
379 memcpy(_val, M->data, _length*
sizeof(
double));
385 error(
"TMatrix::inverse matrix must be square to invert!\n");
389 gsl_matrix *S = gsl_matrix_alloc(_rows,_cols);
391 gsl_matrix *LU = gsl_matrix_alloc(_rows,_cols);
392 gsl_permutation *P = gsl_permutation_alloc(_rows);
395 gsl_matrix_memcpy(LU,S);
396 gsl_linalg_LU_decomp(LU,P,&snum);
397 gsl_linalg_LU_invert(LU,P,S);
399 set_from_gsl_matrix(S);
403 gsl_permutation_free(P);
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:50
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:161
unsigned int getNbRows() const
Gives the number of rows.
Definition: tmatrix.h:212
unsigned int _cols
Definition: tmatrix.h:53
double * get() const
Accessor to the whole array.
Definition: tmatrix.h:201
double * getValArray() const
Definition: tmatrix.h:202
double colSum(unsigned int col)
Sum all elements in a column.
Definition: tmatrix.h:308
void set_col(unsigned int i, vector< double > vec)
Sets element at column i to values stored in vector vec.
Definition: tmatrix.h:142
void set_row(unsigned int i, double val)
Sets all elements at row i to value val.
Definition: tmatrix.h:111
void show_up()
Definition: tmatrix.h:328
unsigned int ncols() const
Definition: tmatrix.h:216
TMatrix()
Definition: tmatrix.h:59
void copy(const TMatrix &mat)
Copy a matrix.
Definition: tmatrix.h:78
unsigned int getNbCols() const
Gives the number of columns.
Definition: tmatrix.h:215
void matrix_increment(double value)
Adds a value to all elements in a matrix.
Definition: tmatrix.h:264
void set(unsigned int i, unsigned int j, double val)
Sets element at row i and column j to value val.
Definition: tmatrix.h:103
double get(unsigned int i, unsigned int j) const
Accessor to element at row i and column j.
Definition: tmatrix.h:193
void getColumnView(unsigned int col, unsigned int n, double *array)
Gives access to a column of the matrix.
Definition: tmatrix.h:224
void set_col(unsigned int i, double val)
Sets element at column i to value val.
Definition: tmatrix.h:133
void assign(double val)
Assigns a value to all element of the matrix.
Definition: tmatrix.h:155
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:90
unsigned int _rows
Definition: tmatrix.h:53
void divide(unsigned int i, unsigned int j, double value)
Divide an element of the matrix by a value.
Definition: tmatrix.h:289
void reset()
Reset members to zero state.
Definition: tmatrix.h:183
void transpose()
Transpose the matrix, swaps columns for rows.
Definition: tmatrix.h:297
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:177
double rowSum(unsigned int row)
Sum all elements in a row.
Definition: tmatrix.h:318
void minus(unsigned int i, unsigned int j, double value)
Substracts a value from an element of the matrix.
Definition: tmatrix.h:273
void set_row(unsigned int i, vector< double > vec)
Sets elements at row i to values stored in vector vec.
Definition: tmatrix.h:120
void multi(unsigned int i, unsigned int j, double value)
Multiply an element of the matrix by a value.
Definition: tmatrix.h:281
void plus(unsigned int i, unsigned int j, double value)
Adds a value to an element of the matrix.
Definition: tmatrix.h:256
TMatrix(const TMatrix &mat)
copy constructor.
Definition: tmatrix.h:62
double * _val
Definition: tmatrix.h:55
unsigned int get_dims(unsigned int *dims) const
Accessor to the matrix dimensions.
Definition: tmatrix.h:207
~TMatrix()
Definition: tmatrix.h:75
unsigned int length() const
Returns the number of elements in the matrix.
Definition: tmatrix.h:218
string to_string()
Writes the matrix into a string in Nemo's matrix input format.
Definition: tmatrix.h:338
void getRowView(unsigned int row, unsigned int n, double *array)
Gives access to a row of the matrix.
Definition: tmatrix.h:242
TMatrix(unsigned int rows, unsigned int cols)
Creates an array of doubles of size = rows*cols.
Definition: tmatrix.h:68
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:171
unsigned int nrows() const
Definition: tmatrix.h:213
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