Nemo  2.3.46
TMatrix Class Reference

A class to handle matrix in params, coerces matrix into a vector of same total size. More...

#include <tmatrix.h>

+ Collaboration diagram for TMatrix:

Public Member Functions

 TMatrix ()
 
 TMatrix (const TMatrix &mat)
 copy constructor. More...
 
 TMatrix (unsigned int rows, unsigned int cols)
 Creates an array of doubles of size = rows*cols. More...
 
 ~TMatrix ()
 
void copy (const TMatrix &mat)
 Copy a matrix. More...
 
void set (unsigned int i, unsigned int j, double val)
 Sets element at row i and column j to value val. More...
 
void assign (double val)
 Assigns a value to all element of the matrix. More...
 
void reset (unsigned int rows, unsigned int cols)
 Re-allocate the existing matrix with assigned rows and cols dimensions. More...
 
void reset (unsigned int rows, unsigned int cols, double *array)
 Reset the existing matrix to the new dimensions and copies the array. More...
 
void reset ()
 Reset members to zero state. More...
 
double get (unsigned int i, unsigned int j)
 Accessor to element at row i and column j. More...
 
double * get () const
 Accessor to the whole array. More...
 
double * getValArray () const
 
unsigned int get_dims (unsigned int *dims)
 Accessor to the matrix dimensions. More...
 
unsigned int getNbRows ()
 Gives the number of rows. More...
 
unsigned int nrows ()
 
unsigned int getNbCols ()
 Gives the number of columns. More...
 
unsigned int ncols ()
 
unsigned int length ()
 Returns the number of elements in the matrix. More...
 
void getColumnView (unsigned int col, unsigned int n, double *array)
 Gives access to a column of the matrix. More...
 
void getRowView (unsigned int row, unsigned int n, double *array)
 Gives access to a row of the matrix. More...
 
void plus (unsigned int i, unsigned int j, double value)
 Adds a value to an element of the matrix. More...
 
void minus (unsigned int i, unsigned int j, double value)
 Substracts a value from an element of the matrix. More...
 
void multi (unsigned int i, unsigned int j, double value)
 Multiply an element of the matrix by a value. More...
 
void divide (unsigned int i, unsigned int j, double value)
 Divide an element of the matrix by a value. More...
 
void transpose ()
 Transpose the matrix, swaps columns for rows. More...
 
double colSum (unsigned int col)
 Sum all elements in a column. More...
 
double rowSum (unsigned int row)
 Sum all elements in a row. More...
 
void show_up ()
 
string to_string ()
 Writes the matrix into a string in Nemo's matrix input format. More...
 

Private Attributes

unsigned int _rows
 
unsigned int _cols
 
unsigned int _length
 
double * _val
 

Detailed Description

A class to handle matrix in params, coerces matrix into a vector of same total size.

Constructor & Destructor Documentation

TMatrix::TMatrix ( )
inline
58 : _rows(0), _cols(0), _length(0), _val(0) { }
unsigned int _rows
Definition: tmatrix.h:52
unsigned int _cols
Definition: tmatrix.h:52
double * _val
Definition: tmatrix.h:54
unsigned int _length
Definition: tmatrix.h:52
TMatrix::TMatrix ( const TMatrix mat)
inline

copy constructor.

60  : _rows(0), _cols(0), _length(0), _val(0)
61  {
62  copy(mat);
63  }
unsigned int _rows
Definition: tmatrix.h:52
unsigned int _cols
Definition: tmatrix.h:52
double * _val
Definition: tmatrix.h:54
unsigned int _length
Definition: tmatrix.h:52
void copy(const TMatrix &mat)
Copy a matrix.
Definition: tmatrix.h:75
TMatrix::TMatrix ( unsigned int  rows,
unsigned int  cols 
)
inline

Creates an array of doubles of size = rows*cols.

65  : _rows(0), _cols(0), _length(0), _val(0)
66  {
67  _length = rows*cols;
68  _val = new double [_length];
69  _rows = rows; _cols = cols;
70  }
unsigned int _rows
Definition: tmatrix.h:52
unsigned int _cols
Definition: tmatrix.h:52
double * _val
Definition: tmatrix.h:54
unsigned int _length
Definition: tmatrix.h:52
TMatrix::~TMatrix ( )
inline
72 {if(_val != NULL) delete [] _val;}
double * _val
Definition: tmatrix.h:54

Member Function Documentation

double TMatrix::colSum ( unsigned int  col)
inline

Sum all elements in a column.

227  {
228  assert(col < _cols); //has to be [0, _cols-1]
229  double sum = 0;
230  for (unsigned int i = 0; i < _rows; ++i) {
231  sum += _val[i*_cols + col];
232  }
233  return sum;
234  }
unsigned int _rows
Definition: tmatrix.h:52
unsigned int _cols
Definition: tmatrix.h:52
double * _val
Definition: tmatrix.h:54
void TMatrix::copy ( const TMatrix mat)
inline

Copy a matrix.

References _cols, _rows, and _val.

Referenced by LCE_Breed_Wolbachia::setParameters().

76  {
77  _rows = mat._rows;
78  _cols = mat._cols;
80  if(_val) delete [] _val;
81  _val = new double [_length];
82  memcpy(_val,mat._val,_length*sizeof(double));
83  }
unsigned int _rows
Definition: tmatrix.h:52
unsigned int _cols
Definition: tmatrix.h:52
double * _val
Definition: tmatrix.h:54
unsigned int _length
Definition: tmatrix.h:52
void TMatrix::divide ( unsigned int  i,
unsigned int  j,
double  value 
)
inline

Divide an element of the matrix by a value.

References error().

Referenced by TTNeutralGenesSH::setAlleleTables().

208  {
209  if( i*j < _length)
210  _val[i*_cols + j] /= value;
211  else
212  error("TMatrix::set overflow!\n");
213  }
unsigned int _cols
Definition: tmatrix.h:52
double * _val
Definition: tmatrix.h:54
int error(const char *str,...)
Definition: output.cc:73
unsigned int _length
Definition: tmatrix.h:52
double TMatrix::get ( unsigned int  i,
unsigned int  j 
)
inline

Accessor to element at row i and column j.

References fatal().

Referenced by Metapop::buildPatchArray(), LCE_Selection_base::changeLocalOptima(), LCE_Disperse_base::checkBackwardDispersalMatrix(), LCE_Disperse_base::checkForwardDispersalMatrix(), LCE_Patch_Extinction::do_remove(), LCE_Patch_Extinction::execute(), LCE_Patch_Extinction::get_harvest_size(), TProtoQuanti::get_trait_var(), TTNeutralGenesSH::getCoa(), LCE_Breed_base::getFecundity(), LCE_Selection_base::getFitnessMultivariateGaussian(), LCE_Selection_base::getFitnessMultivariateGaussian_VE(), LCE_Selection_base::getFitnessUnivariateGaussian(), LCE_Selection_base::getFitnessUnivariateGaussian_VE(), LCE_Selection_base::getFitnessUnivariateQuadratic(), TTNeutralGenesSH::getFst_ij(), TProtoBDMI::getGenoFitnessDiplo(), TProtoBDMI::getGenoFitnessHaplo(), TTNeutralGenesSH::getGlobalAlleleFreq(), LCE_Breed_base::getMeanFecundity(), TTNeutralGenesSH::getNeiGeneticDistance(), Metapop::getPatchCapacity(), LCE_Breed_Wolbachia::inoculate_wolbachia(), LCE_Resize::removeDesignatedPatch(), FileHandler::set_OccMatrix(), LCE_Selection_base::set_param_rate_of_change(), LCE_Selection_base::set_sel_model(), LCE_Selection_base::set_std_rate_of_change(), LCE_Disperse_base::setBasicLatticeMatrix(), TProtoQuanti::setContinuousMutationModel(), TProtoQuanti::setDiallelicMutationModel(), LCE_Breed_base::setFecundity(), TTNeutralGenesSH::setFst_li(), TTNeutralGenesSH::setFstatWeirCockerham(), TTNeutralGenesSH::setFstatWeirCockerham_MS(), TTNeutralGenesSH::setFstMatrix(), TTProtoWithMap::setGeneticMapParameters(), TTNeutralGenesSH::setHt(), TTNeutralGenesSH::setHt2(), LCE_Disperse_base::setLatticeAbsorbingMatrix(), LCE_Disperse_base::setLatticeReflectingMatrix(), LCE_Disperse_base::setLatticeTorrusMatrix(), TTNeutralGenesSH::setLociDivCounter(), TTProtoWithMap::setNumLociPerChromosome(), LCE_StatServiceNotifier::setOccurence(), TProtoQuanti::setParameters(), LCE_Breed_Disperse::setParameters(), LCE_Resize::setParameters(), Metapop::setPatchCapacities(), LCE_Init_BDMI::setPatchFreq(), LCE_Selection_base::setSelectionMatrix(), setSpatialMatrix(), LCE_Init_BDMI::setSpatialPattern(), TProtoBDMI::showGenoTable(), transpose(), LCE_Resize::updatePatchCapacities(), Metapop::updatePatchState(), and TTNeutralGenesFH::write_varcompWC().

120  {
121  if( !((i+1)*(j+1) > _length) )
122  return _val[i*_cols + j];
123  else
124  fatal("TMatrix::get overflow!\n");
125  return 0;
126  }
unsigned int _cols
Definition: tmatrix.h:52
void fatal(const char *str,...)
Definition: output.cc:90
double * _val
Definition: tmatrix.h:54
unsigned int _length
Definition: tmatrix.h:52
double* TMatrix::get ( ) const
inline

Accessor to the whole array.

128 {return _val;}
double * _val
Definition: tmatrix.h:54
unsigned int TMatrix::get_dims ( unsigned int dims)
inline

Accessor to the matrix dimensions.

Parameters
dimsan array of at least 2 elements to store the row [0] and column [1] numbers. May be NULL.
Returns
the total size of the matrix

Referenced by TProtoQuanti::setContinuousMutationModel().

134  {
135  if(dims != NULL) { dims[0] = _rows; dims[1] = _cols; }
136  return _length;
137  }
unsigned int _rows
Definition: tmatrix.h:52
unsigned int _cols
Definition: tmatrix.h:52
unsigned int _length
Definition: tmatrix.h:52
void TMatrix::getColumnView ( unsigned int  col,
unsigned int  n,
double *  array 
)
inline

Gives access to a column of the matrix.

Parameters
colindex of col to view
nsize of the storing array passed, must be equal to no. of rows
arrayarray where the column values will be stored

References error().

152  {
153  if(col > _cols-1) {
154  error("TMatrix::getColumnView: not that many columns in matrix\n");
155  return;
156  }
157  if(n != _rows) {
158  error("TMatrix::getColumnView: array size not equal to number of rows in matrix\n");
159  return;
160  }
161  for(unsigned int i = 0; i < _rows; ++i)
162  array[i] = _val[i*_cols + col];
163  }
unsigned int _rows
Definition: tmatrix.h:52
unsigned int _cols
Definition: tmatrix.h:52
double * _val
Definition: tmatrix.h:54
int error(const char *str,...)
Definition: output.cc:73
void TMatrix::getRowView ( unsigned int  row,
unsigned int  n,
double *  array 
)
inline

Gives access to a row of the matrix.

Parameters
rowindex of row to view
nsize of the storing array passed, must be equal to no. of columns
arrayarray where the row values will be stored

References error().

Referenced by LCE_QuantiInit::execute(), LCE_NtrlInit::execute(), and LCE_Init_BDMI::execute().

170  {
171  if(row > _rows-1) {
172  error("TMatrix::getRowView: not that many rows in matrix\n");
173  return;
174  }
175  if(n != _cols) {
176  error("TMatrix::getRowView: array size not equal to number of columns in matrix\n");
177  return;
178  }
179  for(unsigned int i = 0, stride = row*_cols; i < _cols; ++i)
180  array[i] = _val[stride + i];
181  }
unsigned int _rows
Definition: tmatrix.h:52
unsigned int _cols
Definition: tmatrix.h:52
double * _val
Definition: tmatrix.h:54
int error(const char *str,...)
Definition: output.cc:73
double* TMatrix::getValArray ( ) const
inline

Referenced by FileHandler::set_OccMatrix().

129 {return _val;}
double * _val
Definition: tmatrix.h:54
unsigned int TMatrix::length ( )
inline

Returns the number of elements in the matrix.

Referenced by TTNeutralGenesSH::setFstMatrix(), TTNeutralGenesSH::setNeiGeneticDistance(), and Metapop::setPatchCapacities().

145 {return _length;}
unsigned int _length
Definition: tmatrix.h:52
void TMatrix::minus ( unsigned int  i,
unsigned int  j,
double  value 
)
inline

Substracts a value from an element of the matrix.

References error().

192  {
193  if( i*j < _length)
194  _val[i*_cols + j] -= value;
195  else
196  error("TMatrix::set overflow!\n");
197  }
unsigned int _cols
Definition: tmatrix.h:52
double * _val
Definition: tmatrix.h:54
int error(const char *str,...)
Definition: output.cc:73
unsigned int _length
Definition: tmatrix.h:52
void TMatrix::multi ( unsigned int  i,
unsigned int  j,
double  value 
)
inline

Multiply an element of the matrix by a value.

References error().

Referenced by LCE_Selection_base::set_std_rate_of_change().

200  {
201  if( i*j < _length)
202  _val[i*_cols + j] *= value;
203  else
204  error("TMatrix::set overflow!\n");
205  }
unsigned int _cols
Definition: tmatrix.h:52
double * _val
Definition: tmatrix.h:54
int error(const char *str,...)
Definition: output.cc:73
unsigned int _length
Definition: tmatrix.h:52
void TMatrix::plus ( unsigned int  i,
unsigned int  j,
double  value 
)
inline

Adds a value to an element of the matrix.

References error().

Referenced by LCE_Selection_base::changeLocalOptima(), TTNeutralGenesSH::setAlleleTables(), and LCE_Disperse_base::setLatticeAbsorbingMatrix().

184  {
185  if( i*j < _length)
186  _val[i*_cols + j] += value;
187  else
188  error("TMatrix::set overflow!\n");
189  }
unsigned int _cols
Definition: tmatrix.h:52
double * _val
Definition: tmatrix.h:54
int error(const char *str,...)
Definition: output.cc:73
unsigned int _length
Definition: tmatrix.h:52
void TMatrix::reset ( unsigned int  rows,
unsigned int  cols 
)
inline

Re-allocate the existing matrix with assigned rows and cols dimensions.

Referenced by LCE_Disperse_base::allocateDispMatrix(), TTNeutralGenesSH::allocateTables(), Param::parse_matrix(), LCE_Selection_base::set_local_optima(), LCE_Patch_Extinction::set_matrix_param(), FileHandler::set_OccMatrix(), LCE_Selection_base::set_param_rate_of_change(), LCE_Selection_base::set_std_rate_of_change(), LCE_Breed_base::setFecundity(), TTNeutralGenesSH::setFstMatrix(), TTNeutralGenesSH::setNeiGeneticDistance(), LCE_Init_BDMI::setParameters(), Metapop::setPatchCapacities(), LCE_Selection_base::setSelectionMatrix(), setSpatialMatrix(), and LCE_Resize::updateParameters().

97  {
98  if(_val != NULL) delete [] _val;
99  _length = rows * cols;
100  _val = new double [_length];
101  memset(_val, 0, _length*sizeof(double));
102  _rows = rows; _cols = cols;
103  }
unsigned int _rows
Definition: tmatrix.h:52
unsigned int _cols
Definition: tmatrix.h:52
double * _val
Definition: tmatrix.h:54
unsigned int _length
Definition: tmatrix.h:52
void TMatrix::reset ( unsigned int  rows,
unsigned int  cols,
double *  array 
)
inline

Reset the existing matrix to the new dimensions and copies the array.

105  {
106  reset(rows, cols);
107  memcpy(_val, array, _length * sizeof(double));
108  }
double * _val
Definition: tmatrix.h:54
unsigned int _length
Definition: tmatrix.h:52
void reset()
Reset members to zero state.
Definition: tmatrix.h:110
void TMatrix::reset ( )
inline

Reset members to zero state.

111  {
112  _rows = 0;
113  _cols = 0;
114  _length = 0;
115  if(_val != NULL) delete [] _val;
116  _val = NULL;
117  }
unsigned int _rows
Definition: tmatrix.h:52
unsigned int _cols
Definition: tmatrix.h:52
double * _val
Definition: tmatrix.h:54
unsigned int _length
Definition: tmatrix.h:52
double TMatrix::rowSum ( unsigned int  row)
inline

Sum all elements in a row.

Referenced by LCE_Disperse_base::setLatticeReflectingMatrix().

237  {
238  assert(row < _rows); // has to be [0, _rows-1]
239  double sum = 0;
240  for (unsigned int i = 0; i < _cols; ++i) {
241  sum += _val[row*_cols + i];
242  }
243  return sum;
244  }
unsigned int _rows
Definition: tmatrix.h:52
unsigned int _cols
Definition: tmatrix.h:52
double * _val
Definition: tmatrix.h:54
void TMatrix::show_up ( )
inline

References message().

Referenced by TProtoQuanti::setContinuousMutationModel(), LCE_Disperse_base::setReducedDispMatrix(), and Metapop::show_up().

247  {
248  message("TMatrix dimensions: rows = %i, columns = %i\n",_rows,_cols);
249  for(unsigned int i = 0; i < _rows; i++) {
250  for(unsigned int j = 0; j < _cols; j++)
251  message("%.3f ",_val[i*_cols + j]);
252  message("\n");
253  }
254  }
void message(const char *message,...)
Definition: output.cc:40
unsigned int _rows
Definition: tmatrix.h:52
unsigned int _cols
Definition: tmatrix.h:52
double * _val
Definition: tmatrix.h:54
string TMatrix::to_string ( )
inline

Writes the matrix into a string in Nemo's matrix input format.

Referenced by LCE_Selection_base::set_std_rate_of_change().

257  {
258  ostringstream OUT;
259 
260  OUT << "{";
261  for(unsigned int i = 0; i < _rows; i++) {
262  OUT<<"{";
263  for(unsigned int j = 0; j < _cols-1; j++) {
264  OUT<<_val[i*_cols + j]<<",";
265  }
266  OUT<<_val[i*_cols + _cols-1]<<"}";
267  }
268  OUT << "}";
269 
270  return OUT.str();
271  }
unsigned int _rows
Definition: tmatrix.h:52
unsigned int _cols
Definition: tmatrix.h:52
double * _val
Definition: tmatrix.h:54
void TMatrix::transpose ( )
inline

Transpose the matrix, swaps columns for rows.

References get(), and set().

Referenced by LCE_Disperse_base::setLatticeReflectingMatrix(), and LCE_Disperse_base::setSteppingStone1DMatrix().

216  {
217  TMatrix tmp(_cols, _rows);
218 
219  for(unsigned int i = 0; i < _rows; i++)
220  for(unsigned int j = 0; j < _cols; j++)
221  tmp.set(j, i, get(i, j));
222 
223  reset(_cols, _rows, tmp.get());
224  }
unsigned int _rows
Definition: tmatrix.h:52
unsigned int _cols
Definition: tmatrix.h:52
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:49
void reset()
Reset members to zero state.
Definition: tmatrix.h:110

Member Data Documentation

unsigned int TMatrix::_cols
private

Referenced by copy().

unsigned int TMatrix::_length
private
unsigned int TMatrix::_rows
private

Referenced by copy().

double* TMatrix::_val
private

Referenced by copy().


The documentation for this class was generated from the following file:

Generated for Nemo v2.3.0 by  doxygen 1.8.8 --
Catalogued on GSR