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

Generated for Nemo v2.4.0 by  doxygen 1.9.1 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR