Nemo  2.4.0b
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
utils.h File Reference
#include "types.h"
#include "tmatrix.h"
#include <string>

Go to the source code of this file.

Functions

bool setSpatialMatrix (string param, string numColCondition, TMatrix *inMat, TMatrix *outMat, unsigned int nVal, unsigned int patchNbr, bool doRandomize=false)
 
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)
 
unsigned long nChooseK (int n, int k)
 
TMatrix nChooseKVec (int n, int k)
 

Detailed Description

Id
utils.h,v 1.5 2018/09/28 16:14:00 fred Exp

Nemo2

Copyright (C) 2013 Frederic Guillaume frede.nosp@m.ric..nosp@m.guill.nosp@m.aume.nosp@m.@ieu..nosp@m.uzh..nosp@m.ch

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

Referenced by TTQuantiSH::setStats().

◆ my_mean()

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

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

◆ my_mean_no_nan()

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

Referenced by TTQuantiSH::setStats().

◆ my_variance_with_fixed_mean()

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

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

◆ my_variance_with_fixed_mean_no_nan()

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

Referenced by TTQuantiSH::setStats().

◆ nChooseK()

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

References fatal().

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

◆ nChooseKVec()

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

References nChooseK().

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

◆ setSpatialMatrix()

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


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

Locations of visitors to this page
Catalogued on GSR