Nemo  2.4.0
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 2 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, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

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

Referenced by TTQuantiSH::setStats().

+ Here is the caller graph for this function:

◆ my_mean()

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

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 
)
49 {
50  double mean = 0, cnt = 0;
51  for (unsigned int i = 0; i < size; ++i) {
52  if( !isnan(data[i]) ) { //exclude NaN values (happens for empty patches)
53  mean += data[i];
54  cnt++;
55  }
56  }
57  return mean/cnt;
58 }

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 
)
63 {
64  double var = 0;
65  for (unsigned int i = 0; i < size; ++i) {
66  var += pow( data[i] - mean, 2 );
67  }
68  return var/size;
69 }

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 
)
74 {
75  double var = 0, cnt = 0;
76  for (unsigned int i = 0; i < size; ++i) {
77  if( !isnan(data[i]) ) {
78  var += pow( data[i] - mean, 2 );
79  cnt++;
80  }
81  }
82  return var/cnt;
83 }

Referenced by TTQuantiSH::setStats().

+ Here is the caller graph for this function:

◆ nChooseK()

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

References fatal().

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

+ Here is the caller graph for this function:

◆ nChooseKVec()

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

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

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.0 by  doxygen 1.9.1 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR