Nemo  2.4.2
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
utils.cc File Reference

Nemo2. More...

#include "utils.h"
#include "Uniform.h"
#include "tmatrix.h"
#include <string>

Functions

double my_mean (double *data, unsigned int size)
 
double my_mean_no_nan (double *data, unsigned int size)
 
double my_variance_with_fixed_mean (double *data, unsigned int size, double mean)
 
double my_variance_with_fixed_mean_no_nan (double *data, unsigned int size, double mean)
 
double my_covariance_no_nan (double *data1, double *data2, unsigned int size1, unsigned int size2)
 
bool setSpatialMatrix (string param, string numColCondition, TMatrix *inMat, TMatrix *outMat, unsigned int nVal, unsigned int patchNbr, bool doRandomize)
 
unsigned long nChooseK (int n, int k)
 
TMatrix nChooseKVec (int n, int k)
 

Detailed Description

Nemo2.

Copyright (C) 2013-2026 The Authors

This file is part of Nemo

Nemo is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

Nemo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.

created on

Date
02.05.2013
Author
fred

Function Documentation

◆ my_covariance_no_nan()

double my_covariance_no_nan ( double *  data1,
double *  data2,
unsigned int  size1,
unsigned int  size2 
)
88 {
89  assert(size1 == size2);
90 
91  double var = 0, cnt = 0;
92 
93  for (unsigned int i = 0; i < size1; ++i) {
94 
95  if( !isnan(data1[i]) && !isnan(data2[i])) {
96 
97  for (unsigned int j = i+1; j < size1; ++j) {
98 
99  if( !isnan(data1[j]) && !isnan(data2[j])) {
100  var += ( data1[i] - data1[j])*(data2[i] - data2[j]);
101  cnt++;
102  }
103 
104  }
105  }
106  }
107  cnt *= cnt;
108  return var/cnt;
109 }

Referenced by TTQuantiSH::setStats().

+ Here is the caller graph for this function:

◆ my_mean()

double my_mean ( double *  data,
unsigned int  size 
)
37 {
38  double mean = 0;
39  for (unsigned int i = 0; i < size; ++i) {
40  mean += data[i];
41  }
42  return mean/size;
43 }

Referenced by LCE_Selection_base::addPhenotypicSD(), and TTQuantiSH::setStats().

+ Here is the caller graph for this function:

◆ my_mean_no_nan()

double my_mean_no_nan ( double *  data,
unsigned int  size 
)
48 {
49  double mean = 0, cnt = 0;
50  for (unsigned int i = 0; i < size; ++i) {
51  if( !isnan(data[i]) ) { //exclude NaN values (happens for empty patches)
52  mean += data[i];
53  cnt++;
54  }
55  }
56  return mean/cnt;
57 }

Referenced by TTQuantiSH::setStats().

+ Here is the caller graph for this function:

◆ my_variance_with_fixed_mean()

double my_variance_with_fixed_mean ( double *  data,
unsigned int  size,
double  mean 
)
62 {
63  double var = 0;
64  for (unsigned int i = 0; i < size; ++i) {
65  var += pow( data[i] - mean, 2 );
66  }
67  return var/size;
68 }

Referenced by LCE_Selection_base::addPhenotypicSD(), TTQuantiSH::getVaNoDominance(), TTQuantiSH::getVaWithDominance(), and TTQuantiSH::setStats().

+ Here is the caller graph for this function:

◆ my_variance_with_fixed_mean_no_nan()

double my_variance_with_fixed_mean_no_nan ( double *  data,
unsigned int  size,
double  mean 
)
73 {
74  double var = 0, cnt = 0;
75  for (unsigned int i = 0; i < size; ++i) {
76  if( !isnan(data[i]) ) {
77  var += pow( data[i] - mean, 2 );
78  cnt++;
79  }
80  }
81  return var/cnt;
82 }

Referenced by TTQuantiSH::setStats().

+ Here is the caller graph for this function:

◆ nChooseK()

unsigned long nChooseK ( int  n,
int  k 
)
173 {
174  if (!(n >= k && k >= 0)) {
175  fatal("nChooseK() requires (n >= k && k >= 0)\n");
176  }
177 
178  if ((k == 0) || (n == k)) {
179  return 1;
180  }
181 
182  unsigned long result = n;
183  int i = 2;
184  n = n-1; k = k-1;
185  while (k != 0){
186  result = result * n / i;
187  i = i+1;
188  k = k-1;
189  n = n-1;
190  }
191  return result;
192 }
void fatal(const char *str,...)
Definition: output.cc:98

References fatal().

Referenced by nChooseKVec(), and TProtoQuanti::setEpistasisParameters().

+ Here is the caller graph for this function:

◆ nChooseKVec()

TMatrix nChooseKVec ( int  n,
int  k 
)
197 {
198  vector<double> result(k,0);
199 
200  unsigned long ncombs = nChooseK(n, k);
201 
202  TMatrix results(ncombs , k);
203 
204  int i, K, N, counter;
205  double c;
206 
207  for (int x = 0 ; x < ncombs ; ++x) {
208 
209  i = x; K = k; N = n;
210  c = ncombs * K / N;
211  counter = 0;
212 
213  for (int element = 0; element < n; ++element){
214  if (i < c) {
215  //take this element, and K-1 from the remaining
216  result[counter] = element;
217  ++counter;
218  K = K-1;
219  if (K == 0) {
220  break;
221  }
222  //have c == nChooseK(N-1,K), want nChooseK(N-2,K-1)
223  c = c * K / (N-1);
224  }
225  else {
226  //skip this element, and take all from the remaining
227  i = i-c;
228  //have c == nChooseK(N-1,K-1), want nChooseK(N-2,K-1)
229  c = c * (N-K) / (N-1);
230  }
231  N = N-1;
232  }
233 // cout << "{ ";
234 // for (int x = 0 ; x < k-1 ; x++)
235 // cout << result[x] << " ";
236 // cout << result[k-1] << " }" << endl;
237 
238  counter = 0;
239  results. set_row(x, result);
240  }
241  return results;
242 }
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:48
unsigned long nChooseK(int n, int k)
Definition: utils.cc:172

References nChooseK().

Referenced by TTNOhtaStats::FHwrite(), TTQOhtaStats::FHwrite(), and TProtoQuanti::setEpistasisParameters().

+ Here is the caller graph for this function:

◆ setSpatialMatrix()

bool setSpatialMatrix ( string  param,
string  numColCondition,
TMatrix inMat,
TMatrix outMat,
unsigned int  nVal,
unsigned int  patchNbr,
bool  doRandomize 
)
115 {
116  unsigned int ncol = inMat->getNbCols(); //nbr of traits/loci = nbr of col
117  unsigned int npat = inMat->getNbRows(); //nbr of patches, may be <= patchNbr
118 
119  //we have two possible configurations:
120  // 1. ncol == 1; same value for all 'traits' in a patch but varies among patches, with possible repetition of a pattern
121  // 2. ncol == no traits && npat <= no patches; trait values change in each patch following a pattern (same if npat == 1)
122  if(npat > patchNbr) {
123  error("The number of rows in \"%s\" is greater than the number of patches, must be at least equal to it.", param.c_str());
124  return false;
125  }
126  if(ncol != 1 && ncol != nVal) {
127  error("The number of columns in \"%s\" not properly set.\n", param.c_str());
128  error("It is expected to be equal to 1 or equal to %s (%i).\n", numColCondition.c_str(), nVal);
129  return false;
130  }
131 
132  outMat->reset(patchNbr, nVal);
133 
134  if(doRandomize) {
135 
136  for(unsigned int i = 0; i < patchNbr; ++i)
137  for(unsigned int j = 0; j < nVal; j++)
138  outMat->set(i, j, inMat->get( RAND::Uniform(npat), j));
139 
140  } else {
141 
142  for(unsigned int i = 0; i < patchNbr; ++i) {
143 
144  if(npat < patchNbr) {//repetition of a pattern
145 
146  if(ncol == 1)
147  for(unsigned int j = 0; j < nVal; j++)
148  outMat->set(i, j, inMat->get(i % npat, 0)) ;
149  else
150  for(unsigned int j = 0; j < nVal; j++)
151  outMat->set(i, j, inMat->get(i % npat, j));
152 
153  } else {//different values for each Patch
154 
155  if(ncol == 1)
156  for(unsigned int j = 0; j < nVal; j++)
157  outMat->set(i, j, inMat->get(i, 0));
158  else
159  for(unsigned int j = 0; j < nVal; j++)
160  outMat->set(i, j, inMat->get(i, j));
161  }
162  }
163  }
164 
165 
166 
167  return true;
168 }
static double Uniform()
Generates a random number from [0.0, 1.0[ uniformly distributed.
Definition: Uniform.h:125
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 getNbCols() const
Gives the number of columns.
Definition: tmatrix.h:213
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
int error(const char *str,...)
Definition: output.cc:77

References error(), TMatrix::get(), TMatrix::getNbCols(), TMatrix::getNbRows(), TMatrix::reset(), TMatrix::set(), and RAND::Uniform().

Referenced by LCE_PhenotypeExpression::set_env_cue(), LCE_Selection_base::set_local_optima(), LCE_PhenotypeExpression::setParameters(), LCE_QuantiInit::setParameters(), LCE_NtrlInit::setParameters(), LCE_Breed_Wolbachia::setParameters(), and LCE_Selection_base::setSelectionMatrix().

+ Here is the caller graph for this function:

Generated for Nemo v2.4.2 by  doxygen 1.9.1

Catalogued on GSR