Nemo  2.3.56
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
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 copy_recycle (const TMatrix &mat)
 Copy elements of 'mat', recycling elements of 'mat' if its size is smaller than current 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 value)
 Reset the existing matrix to the new dimensions and copies the value to all elements. 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 matrix_increment (double value)
 Adds a value to all elements in a 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() [1/3]

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

◆ TMatrix() [2/3]

TMatrix::TMatrix ( const TMatrix mat)
inline

copy constructor.

61 : _rows(0), _cols(0), _length(0), _val(0)
62 {
63 copy(mat);
64 }
void copy(const TMatrix &mat)
Copy a matrix.
Definition: tmatrix.h:77

References copy().

◆ TMatrix() [3/3]

TMatrix::TMatrix ( unsigned int  rows,
unsigned int  cols 
)
inline

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

67 : _rows(0), _cols(0), _length(0), _val(0)
68 {
69 _length = rows*cols;
70 _val = new double [_length];
71 _rows = rows; _cols = cols;
72 }

References _cols, _length, _rows, and _val.

◆ ~TMatrix()

TMatrix::~TMatrix ( )
inline
74{if(_val != NULL) delete [] _val;}

References _val.

Member Function Documentation

◆ assign()

◆ colSum()

double TMatrix::colSum ( unsigned int  col)
inline

Sum all elements in a column.

263 {
264 assert(col < _cols); //has to be [0, _cols-1]
265 double sum = 0;
266 for (unsigned int i = 0; i < _rows; ++i) {
267 sum += _val[i*_cols + col];
268 }
269 return sum;
270 }

References _cols, _rows, and _val.

◆ copy()

void TMatrix::copy ( const TMatrix mat)
inline

Copy a matrix.

78 {
79 _rows = mat._rows;
80 _cols = mat._cols;
82 if(_val) delete [] _val;
83 _val = new double [_length];
84 memcpy(_val,mat._val,_length*sizeof(double));
85 }

References _cols, _length, _rows, and _val.

Referenced by copy_recycle(), LCE_Breed_Wolbachia::setParameters(), Metapop::setPatchSizes(), and TMatrix().

+ Here is the caller graph for this function:

◆ copy_recycle()

void TMatrix::copy_recycle ( const TMatrix mat)
inline

Copy elements of 'mat', recycling elements of 'mat' if its size is smaller than current matrix.

Will only copy as many elements as necessary given _rows and _cols of current matrix.

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 }
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
int error(const char *str,...)
Definition: output.cc:77

References _cols, _length, _rows, _val, copy(), error(), and set().

Referenced by TProtoQuanti::setDominanceParameters().

+ Here is the caller graph for this function:

◆ divide()

void TMatrix::divide ( unsigned int  i,
unsigned int  j,
double  value 
)
inline

Divide an element of the matrix by a value.

244 {
245 if( i*j < _length)
246 _val[i*_cols + j] /= value;
247 else
248 error("TMatrix::divide overflow!\n");
249 }

References _cols, _length, _val, and error().

Referenced by TTNeutralGenesSH::setAlleleTables(), and TTNeutralGenesSH::setCoaMatrix().

+ Here is the caller graph for this function:

◆ get() [1/2]

double * TMatrix::get ( ) const
inline

Accessor to the whole array.

155{return _val;}

References _val.

Referenced by transpose().

+ Here is the caller graph for this function:

◆ get() [2/2]

double TMatrix::get ( unsigned int  i,
unsigned int  j 
)
inline

Accessor to element at row i and column j.

147 {
148 if( !((i+1)*(j+1) > _length) )
149 return _val[i*_cols + j];
150 else
151 fatal("TMatrix::get overflow!\n");
152 return 0;
153 }
void fatal(const char *str,...)
Definition: output.cc:96

References _cols, _length, _val, and 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_QuantiModifier::execute(), TProtoQuanti::get_dominance(), 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(), TTDispersal::init_sequence(), LCE_Breed_Wolbachia::inoculate_wolbachia(), LCE_Breed_base::NonWrightFisherPopulation(), LCE_Breed_Quanti::NonWrightFisherPopulation(), 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(), TTNeutralGenesSH::setCoaMatrix(), TProtoQuanti::setContinuousMutationModel(), TProtoQuanti::setDiallelicMutationModel(), TProtoQuanti::setDominanceParameters(), TProtoDeletMutations_bitstring::setEffectsFromInput(), 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(), LCE_Breed_Disperse::setParameters(), LCE_Resize::setParameters(), TProtoQuanti::setParameters(), Metapop::setPatchCapacities(), LCE_Init_BDMI::setPatchFreq(), LCE_Selection_base::setSelectionMatrix(), setSpatialMatrix(), LCE_Init_BDMI::setSpatialPattern(), TProtoBDMI::showGenoTable(), ParamsParser::sym_matrix(), transpose(), LCE_Resize::updatePatchCapacities(), Metapop::updatePatchState(), and TTNeutralGenesFH::write_varcompWC().

◆ get_dims()

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
161 {
162 if(dims != NULL) { dims[0] = _rows; dims[1] = _cols; }
163 return _length;
164 }

References _cols, _length, and _rows.

Referenced by TProtoQuanti::setContinuousMutationModel().

+ Here is the caller graph for this function:

◆ getColumnView()

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
179 {
180 if(col > _cols-1) {
181 error("TMatrix::getColumnView: not that many columns in matrix\n");
182 return;
183 }
184 if(n != _rows) {
185 error("TMatrix::getColumnView: array size not equal to number of rows in matrix\n");
186 return;
187 }
188 for(unsigned int i = 0; i < _rows; ++i)
189 array[i] = _val[i*_cols + col];
190 }

References _cols, _rows, _val, and error().

◆ getNbCols()

◆ getNbRows()

◆ getRowView()

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
197 {
198 if(row > _rows-1) {
199 error("TMatrix::getRowView: not that many rows in matrix\n");
200 return;
201 }
202 if(n != _cols) {
203 error("TMatrix::getRowView: array size not equal to number of columns in matrix\n");
204 return;
205 }
206 for(unsigned int i = 0, stride = row*_cols; i < _cols; ++i)
207 array[i] = _val[stride + i];
208 }

References _cols, _rows, _val, and error().

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

+ Here is the caller graph for this function:

◆ getValArray()

double * TMatrix::getValArray ( ) const
inline
156{return _val;}

References _val.

Referenced by FileHandler::set_OccMatrix().

+ Here is the caller graph for this function:

◆ length()

unsigned int TMatrix::length ( )
inline

Returns the number of elements in the matrix.

172{return _length;}

References _length.

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

+ Here is the caller graph for this function:

◆ matrix_increment()

void TMatrix::matrix_increment ( double  value)
inline

Adds a value to all elements in a matrix.

219 {
220 for(unsigned int i = 0; i < _rows; ++i){
221 for(unsigned int j = 0; j < _cols; ++j){
222 plus(i,j,value);
223 }
224 }
225 }
void plus(unsigned int i, unsigned int j, double value)
Adds a value to an element of the matrix.
Definition: tmatrix.h:210

References _cols, _rows, and plus().

◆ minus()

void TMatrix::minus ( unsigned int  i,
unsigned int  j,
double  value 
)
inline

Substracts a value from an element of the matrix.

228 {
229 if( i*j < _length)
230 _val[i*_cols + j] -= value;
231 else
232 error("TMatrix::minus overflow!\n");
233 }

References _cols, _length, _val, and error().

◆ multi()

void TMatrix::multi ( unsigned int  i,
unsigned int  j,
double  value 
)
inline

Multiply an element of the matrix by a value.

236 {
237 if( i*j < _length)
238 _val[i*_cols + j] *= value;
239 else
240 error("TMatrix::multi overflow!\n");
241 }

References _cols, _length, _val, and error().

Referenced by LCE_Selection_base::set_std_rate_of_change().

+ Here is the caller graph for this function:

◆ ncols()

◆ nrows()

◆ plus()

void TMatrix::plus ( unsigned int  i,
unsigned int  j,
double  value 
)
inline

Adds a value to an element of the matrix.

211 {
212 if( i*j < _length)
213 _val[i*_cols + j] += value;
214 else
215 error("TMatrix::plus overflow!\n");
216 }

References _cols, _length, _val, and error().

Referenced by LCE_Selection_base::changeLocalOptima(), matrix_increment(), TTNeutralGenesSH::setAlleleTables(), TTNeutralGenesSH::setCoaMatrix(), TProtoQuanti::setDominanceParameters(), and LCE_Disperse_base::setLatticeAbsorbingMatrix().

+ Here is the caller graph for this function:

◆ reset() [1/4]

void TMatrix::reset ( )
inline

Reset members to zero state.

138 {
139 _rows = 0;
140 _cols = 0;
141 _length = 0;
142 if(_val != NULL) delete [] _val;
143 _val = NULL;
144 }

References _cols, _length, _rows, and _val.

Referenced by reset(), and transpose().

+ Here is the caller graph for this function:

◆ reset() [2/4]

◆ reset() [3/4]

void TMatrix::reset ( unsigned int  rows,
unsigned int  cols,
double *  array 
)
inline

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

131 {
132 reset(rows, cols);
133 memcpy(_val, array, _length * sizeof(double));
134 }
void reset()
Reset members to zero state.
Definition: tmatrix.h:137

References _length, _val, and reset().

◆ reset() [4/4]

void TMatrix::reset ( unsigned int  rows,
unsigned int  cols,
double  value 
)
inline

Reset the existing matrix to the new dimensions and copies the value to all elements.

125 {
126 reset(rows, cols);
127 assign(value);
128 }
void assign(double val)
Assigns a value to all element of the matrix.
Definition: tmatrix.h:110

References assign(), and reset().

◆ rowSum()

double TMatrix::rowSum ( unsigned int  row)
inline

Sum all elements in a row.

273 {
274 assert(row < _rows); // has to be [0, _rows-1]
275 double sum = 0;
276 for (unsigned int i = 0; i < _cols; ++i) {
277 sum += _val[row*_cols + i];
278 }
279 return sum;
280 }

References _cols, _rows, and _val.

Referenced by LCE_Disperse_base::setLatticeReflectingMatrix().

+ Here is the caller graph for this function:

◆ set()

◆ show_up()

void TMatrix::show_up ( )
inline
283 {
284 message("TMatrix dimensions: rows = %i, columns = %i\n",_rows,_cols);
285 for(unsigned int i = 0; i < _rows; i++) {
286 for(unsigned int j = 0; j < _cols; j++)
287 message("%.3f ",_val[i*_cols + j]);
288 message("\n");
289 }
290 }
void message(const char *message,...)
Definition: output.cc:40

References _cols, _rows, _val, and message().

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

+ Here is the caller graph for this function:

◆ to_string()

string TMatrix::to_string ( )
inline

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

293 {
294 ostringstream OUT;
295
296 OUT << "{";
297 for(unsigned int i = 0; i < _rows; i++) {
298 OUT<<"{";
299 for(unsigned int j = 0; j < _cols-1; j++) {
300 OUT<<_val[i*_cols + j]<<",";
301 }
302 OUT<<_val[i*_cols + _cols-1]<<"}";
303 }
304 OUT << "}";
305
306 return OUT.str();
307 }

References _cols, _rows, and _val.

Referenced by LCE_Selection_base::set_std_rate_of_change(), and ParamsParser::sym_matrix().

+ Here is the caller graph for this function:

◆ transpose()

void TMatrix::transpose ( )
inline

Transpose the matrix, swaps columns for rows.

252 {
253 TMatrix tmp(_cols, _rows);
254
255 for(unsigned int i = 0; i < _rows; i++)
256 for(unsigned int j = 0; j < _cols; j++)
257 tmp.set(j, i, get(i, j));
258
259 reset(_cols, _rows, tmp.get());
260 }
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:49
double * get() const
Accessor to the whole array.
Definition: tmatrix.h:155

References _cols, _rows, get(), reset(), and set().

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

+ Here is the caller graph for this function:

Member Data Documentation

◆ _cols

◆ _length

unsigned int TMatrix::_length
private

◆ _rows

◆ _val


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

Generated for Nemo v2.3.56 by  doxygen 1.9.0 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR