Nemo  2.4.2
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
tmatrix.h
Go to the documentation of this file.
1 
28 #ifndef _T_MATRIX_H
29 #define _T_MATRIX_H
30 
31 #ifdef HAS_GSL
32 #include <gsl/gsl_vector.h>
33 #include <gsl/gsl_matrix.h>
34 #include <gsl/gsl_permutation.h>
35 #include <gsl/gsl_linalg.h>
36 #endif
37 
38 #include <string.h>
39 #include <sstream>
40 #include <string>
41 #include <assert.h>
42 #include <vector>
43 #include "output.h"
44 
45 using namespace std;
46 
48 class TMatrix {
49 private:
50 
51  unsigned int _rows, _cols, _length;
52 
53  double* _val;
54 
55 public:
56 
57  TMatrix () : _rows(0), _cols(0), _length(0), _val(0) { }
58 
60  TMatrix (const TMatrix& mat) : _rows(0), _cols(0), _length(0), _val(0)
61  {
62  copy(mat);
63  }
64 
66  TMatrix ( unsigned int rows, unsigned int cols ) : _rows(0), _cols(0), _length(0), _val(0)
67  {
68  _length = rows*cols;
69  _val = new double [_length];
70  _rows = rows; _cols = cols;
71  }
72 
73  ~TMatrix () {if(_val != NULL) delete [] _val;}
74 
76  void copy (const TMatrix& mat)
77  {
78  _rows = mat._rows;
79  _cols = mat._cols;
80  _length = _rows*_cols;
81  if(_val) delete [] _val;
82  _val = new double [_length];
83  memcpy(_val,mat._val,_length*sizeof(double));
84  }
85 
88  void copy_recycle (const TMatrix& mat)
89  {
90  if(_rows == mat._rows && _cols == mat._cols)
91  copy(mat);
92  else if(_val == NULL || _length == 0)
93  error("TMatrix::copy_recycle::matrix must be allocated before call\n");
94  else {
95  for(unsigned int i = 0; i < _rows; ++i)
96  for(unsigned int j = 0; j < _cols; ++j)
97  set(i, j, mat._val[ (i%mat._rows)*mat._cols + (j%mat._cols) ]); //mat.get( i%nrow, j%ncol )); _val[i*_cols + j]
98  }
99  }
101  void set (unsigned int i, unsigned int j, double val) {
102  if( i*j < _length)
103  _val[i*_cols + j] = val;
104  else
105  error("TMatrix::set overflow, i*j > length!\n");
106  }
107 
109  void set_row (unsigned int i, double val) {
110  if( i < _rows)
111  for(unsigned int j = 0, stride = i*_cols; j < _cols; ++j)
112  _val[stride + j] = val;
113  else
114  error("TMatrix::set_row overflow, i > num rows!\n");
115  }
116 
118  void set_row (unsigned int i, vector<double> vec) {
119 
120  if(vec.size() < _cols)
121  fatal("TMatrix::set_row copy from vector too small to fit\n");
122 
123  if( i < _rows)
124  for(unsigned int j = 0, stride = i*_cols; j < _cols; ++j)
125  _val[stride + j] = vec[j];
126  else
127  error("TMatrix::set_row overflow, i > num rows!\n");
128  }
129 
131  void set_col (unsigned int i, double val) {
132  if( i < _cols)
133  for(unsigned int j = 0; j < _rows; ++j)
134  _val[j*_cols + i] = val;
135  else
136  error("TMatrix::set_col overflow, i > num cols!\n");
137  }
138 
140  void set_col (unsigned int i, vector<double> vec) {
141 
142  if(vec.size() < _rows)
143  fatal("TMatrix::set_col copy from vector too small to fit\n");
144 
145  if( i < _cols)
146  for(unsigned int j = 0; j < _rows; ++j)
147  _val[j*_cols + i] = vec[j];
148  else
149  error("TMatrix::set_col overflow, i > num cols!\n");
150  }
151 
153  void assign (double val)
154  {
155  for(unsigned int i = 0; i < _length; ++i) _val[i] = val;
156  }
157 
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;
166  }
167 
169  void reset (unsigned int rows, unsigned int cols, double value) {
170  reset(rows, cols);
171  assign(value);
172  }
173 
175  void reset (unsigned int rows, unsigned int cols, const double* array) {
176  reset(rows, cols);
177  memcpy(_val, array, _length * sizeof(double));
178  }
179 
181  void reset ( )
182  {
183  _rows = 0;
184  _cols = 0;
185  _length = 0;
186  if(_val != NULL) delete [] _val;
187  _val = NULL;
188  }
189 
191  double get (unsigned int i, unsigned int j) const {
192  if( !((i+1)*(j+1) > _length) )
193  return _val[i*_cols + j];
194  else
195  fatal("TMatrix::get overflow!\n");
196  return 0;
197  }
199  double* get () const {return _val;}
200  double* getValArray () const {return _val;}
205  unsigned int get_dims (unsigned int* dims) const {
206  if(dims != NULL) { dims[0] = _rows; dims[1] = _cols; }
207  return _length;
208  }
210  unsigned int getNbRows ( ) const {return _rows;}
211  unsigned int nrows () const {return _rows;}
213  unsigned int getNbCols ( ) const {return _cols;}
214  unsigned int ncols () const {return _cols;}
216  unsigned int length ( ) const {return _length;}
222  void getColumnView (unsigned int col, unsigned int n, double* array)
223  {
224  if(col > _cols-1) {
225  error("TMatrix::getColumnView: not that many columns in matrix\n");
226  return;
227  }
228  if(n != _rows) {
229  error("TMatrix::getColumnView: array size not equal to number of rows in matrix\n");
230  return;
231  }
232  for(unsigned int i = 0; i < _rows; ++i)
233  array[i] = _val[i*_cols + col];
234  }
240  void getRowView (unsigned int row, unsigned int n, double* array)
241  {
242  if(row > _rows-1) {
243  error("TMatrix::getRowView: not that many rows in matrix\n");
244  return;
245  }
246  if(n != _cols) {
247  error("TMatrix::getRowView: array size not equal to number of columns in matrix\n");
248  return;
249  }
250  for(unsigned int i = 0, stride = row*_cols; i < _cols; ++i)
251  array[i] = _val[stride + i];
252  }
254  void plus (unsigned int i, unsigned int j, double value)
255  {
256  if( i*j < _length)
257  _val[i*_cols + j] += value;
258  else
259  error("TMatrix::plus overflow!\n");
260  }
262  void matrix_increment (double value)
263  {
264  for(unsigned int i = 0; i < _rows; ++i){
265  for(unsigned int j = 0; j < _cols; ++j){
266  plus(i,j,value);
267  }
268  }
269  }
271  void sweep_plus (double value)
272  {
273  for(unsigned int i = 0; i < _rows; ++i){
274  for(unsigned int j = 0; j < _cols; ++j){
275  plus(i,j,value);
276  }
277  }
278  }
280  void minus (unsigned int i, unsigned int j, double value)
281  {
282  if( i*j < _length)
283  _val[i*_cols + j] -= value;
284  else
285  error("TMatrix::minus overflow!\n");
286  }
288  void sweep_minus (double value)
289  {
290  for(unsigned int i = 0; i < _rows; ++i){
291  for(unsigned int j = 0; j < _cols; ++j){
292  minus(i,j,value);
293  }
294  }
295  }
297  void multi (unsigned int i, unsigned int j, double value)
298  {
299  if( i*j < _length)
300  _val[i*_cols + j] *= value;
301  else
302  error("TMatrix::multi overflow!\n");
303  }
305  void sweep_multiply (double value)
306  {
307  for(unsigned int i = 0; i < _rows; ++i){
308  for(unsigned int j = 0; j < _cols; ++j){
309  multi(i,j,value);
310  }
311  }
312  }
314  void divide (unsigned int i, unsigned int j, double value)
315  {
316  if( i*j < _length)
317  _val[i*_cols + j] /= value;
318  else
319  error("TMatrix::divide overflow!\n");
320  }
322  void sweep_divide (double value)
323  {
324  for(unsigned int i = 0; i < _rows; ++i){
325  for(unsigned int j = 0; j < _cols; ++j){
326  divide(i,j,value);
327  }
328  }
329  }
331  void transpose ()
332  {
333  TMatrix tmp(_cols, _rows);
334 
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));
338 
339  reset(_cols, _rows, tmp.get());
340  }
342  double colSum (unsigned int col)
343  {
344  assert(col < _cols); //has to be [0, _cols-1]
345  double sum = 0;
346  for (unsigned int i = 0; i < _rows; ++i) {
347  sum += _val[i*_cols + col];
348  }
349  return sum;
350  }
352  double rowSum (unsigned int row)
353  {
354  assert(row < _rows); // has to be [0, _rows-1]
355  double sum = 0;
356  for (unsigned int i = 0; i < _cols; ++i) {
357  sum += _val[row*_cols + i];
358  }
359  return sum;
360  }
361 
362  void show_up()
363  {
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]);
368  message("\n");
369  }
370  }
372  string to_string ()
373  {
374  ostringstream OUT;
375 
376  OUT << "{";
377  for(unsigned int i = 0; i < _rows; i++) {
378  OUT<<"{";
379  for(unsigned int j = 0; j < _cols-1; j++) {
380  OUT<<_val[i*_cols + j]<<",";
381  }
382  OUT<<_val[i*_cols + _cols-1]<<"}";
383  }
384  OUT << "}";
385 
386  return OUT.str();
387  }
388 
389 #ifdef HAS_GSL
393  void get_gsl_matrix(gsl_matrix* M)
394  {
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]);
402  }
406  void set_from_gsl_matrix (gsl_matrix* M)
407  {
408  if(_val != 0) delete [] _val;
409  _rows = M->size1;
410  _cols = M->size2;
411  _length = _rows*_cols;
412  _val = new double [_length];
413  memcpy(_val, M->data, _length*sizeof(double));
414  }
416  void inverse ()
417  {
418  if(_rows != _cols) {
419  error("TMatrix::inverse matrix must be square to invert!\n");
420  return;
421  }
422 
423  gsl_matrix *S = gsl_matrix_alloc(_rows,_cols);
424  get_gsl_matrix(S);
425  gsl_matrix *LU = gsl_matrix_alloc(_rows,_cols);
426  gsl_permutation *P = gsl_permutation_alloc(_rows);
427  int snum;
428 
429  gsl_matrix_memcpy(LU,S);
430  gsl_linalg_LU_decomp(LU,P,&snum);
431  gsl_linalg_LU_invert(LU,P,S);
432 
433  set_from_gsl_matrix(S);
434 
435  gsl_matrix_free(S);
436  gsl_matrix_free(LU);
437  gsl_permutation_free(P);
438  }
439 #endif
440 };
441 
442 #endif
443 
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
Nemo2.

Generated for Nemo v2.4.2 by  doxygen 1.9.1

Catalogued on GSR