Nemo  2.3.46
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)
 

Detailed Description

Id
utils.h,v 1.4 2015-07-13 08:52:59 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

double my_mean ( double *  data,
unsigned int  size 
)

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

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

Referenced by TTQuantiSH::setStats().

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 }
double my_variance_with_fixed_mean ( double *  data,
unsigned int  size,
double  mean 
)

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

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 }
double my_variance_with_fixed_mean_no_nan ( double *  data,
unsigned int  size,
double  mean 
)

Referenced by TTQuantiSH::setStats().

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 }
bool setSpatialMatrix ( string  param,
string  numColCondition,
TMatrix inMat,
TMatrix outMat,
unsigned int  nVal,
unsigned int  patchNbr,
bool  doRandomize = false 
)

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().

89 {
90  unsigned int ncol = inMat->getNbCols(); //nbr of traits/loci = nbr of col
91  unsigned int npat = inMat->getNbRows(); //nbr of patches, may be <= patchNbr
92 
93  //we have two possible configurations:
94  // 1. ncol == 1; same value for all 'traits' in a patch but varies among patches, with possible repetition of a pattern
95  // 2. ncol == no traits && npat <= no patches; trait values change in each patch following a pattern (same if npat == 1)
96  if(npat > patchNbr) {
97  error("The number of rows in \"%s\" is greater than the number of patches, must be at least equal to it.", param.c_str());
98  return false;
99  }
100  if(ncol != 1 && ncol != nVal) {
101  error("The number of columns in \"%s\" not properly set.\n", param.c_str());
102  error("It is expected to be equal to 1 or equal to %s (%i).\n", numColCondition.c_str(), nVal);
103  return false;
104  }
105 
106  outMat->reset(patchNbr, nVal);
107 
108  if(doRandomize) {
109 
110  for(unsigned int i = 0; i < patchNbr; ++i)
111  for(unsigned int j = 0; j < nVal; j++)
112  outMat->set(i, j, inMat->get( RAND::Uniform(npat), j));
113 
114  } else {
115 
116  for(unsigned int i = 0; i < patchNbr; ++i) {
117 
118  if(npat < patchNbr) {//repetition of a pattern
119 
120  if(ncol == 1)
121  for(unsigned int j = 0; j < nVal; j++)
122  outMat->set(i, j, inMat->get(i % npat, 0)) ;
123  else
124  for(unsigned int j = 0; j < nVal; j++)
125  outMat->set(i, j, inMat->get(i % npat, j));
126 
127  } else {//different values for each Patch
128 
129  if(ncol == 1)
130  for(unsigned int j = 0; j < nVal; j++)
131  outMat->set(i, j, inMat->get(i, 0));
132  else
133  for(unsigned int j = 0; j < nVal; j++)
134  outMat->set(i, j, inMat->get(i, j));
135  }
136  }
137  }
138 
139 
140 
141  return true;
142 }
unsigned int getNbCols()
Gives the number of columns.
Definition: tmatrix.h:142
unsigned int getNbRows()
Gives the number of rows.
Definition: tmatrix.h:139
double get(unsigned int i, unsigned int j)
Accessor to element at row i and column j.
Definition: tmatrix.h:120
int error(const char *str,...)
Definition: output.cc:73
void set(unsigned int i, unsigned int j, double val)
Sets element at row i and column j to value val.
Definition: tmatrix.h:85
static double Uniform()
Generates a random number from [0.0, 1.0[ uniformly distributed.
Definition: Uniform.h:101
void reset(unsigned int rows, unsigned int cols)
Re-allocate the existing matrix with assigned rows and cols dimensions.
Definition: tmatrix.h:97

Generated for Nemo v2.3.0 by  doxygen 1.8.8 --
Catalogued on GSR