Nemo  2.3.56
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
utils.cc File Reference
#include "utils.h"
#include "Uniform.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)
 
vector< vector< int > > nChooseKVec (int n, int k)
 

Detailed Description

Id
utils.cc,v 1.7 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 
)
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:96

References fatal().

Referenced by nChooseKVec().

+ Here is the caller graph for this function:

◆ nChooseKVec()

vector< vector< int > > nChooseKVec ( int  n,
int  k 
)
198{
199 vector< vector<int> > results;
200 vector<int> result(k,0);
201
202 unsigned long ncombs = nChooseK(n, k);
203
204 for (int x = 0 ; x < ncombs ; x++) {
205 int i = x; int K = k; int N = n;
206 double c = ncombs * K / N;
207 int counter = 0;
208 for (int element = 0; element < n; element++){
209 if (i < c) {
210 //take this element, and K-1 from the remaining
211 result[counter] = element;
212 ++counter;
213 K = K-1;
214 if (K == 0) {
215 break;
216 }
217 //have c == nChooseK(N-1,K), want nChooseK(N-2,K-1)
218 c = c * K / (N-1);
219 }
220 else {
221 //skip this element, and take all from the remaining
222 i = i-c;
223 //have c == nChooseK(N-1,K-1), want nChooseK(N-2,K-1)
224 c = c * (N-K) / (N-1);
225 }
226 N = N-1;
227 }
228// cout << "{ ";
229// for (int x = 0 ; x < k-1 ; x++)
230// cout << result[x] << " ";
231// cout << result[k-1] << " }" << endl;
232
233 counter = 0;
234 results.push_back(result);
235 }
236 return results;
237}
unsigned long nChooseK(int n, int k)
Definition: utils.cc:173

References nChooseK().

◆ 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:125
void reset(unsigned int rows, unsigned int cols)
Re-allocate the existing matrix with assigned rows and cols dimensions.
Definition: tmatrix.h:116
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)
Accessor to element at row i and column j.
Definition: tmatrix.h:147
unsigned int getNbRows()
Gives the number of rows.
Definition: tmatrix.h:166
unsigned int getNbCols()
Gives the number of columns.
Definition: tmatrix.h:169
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_Selection_base::set_local_optima(), 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.3.56 by  doxygen 1.9.0 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR