Changeset 26621
- Timestamp:
- 11/13/21 07:25:07 (3 years ago)
- Location:
- issm/trunk-jpl
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/c/shared/Random/random.cpp
r26483 r26621 9 9 #include <math.h> 10 10 #include <float.h> /* DBL_EPSILON */ 11 #include <chrono>12 11 #include <cstdarg> 13 12 #include <iostream> 14 #include <random>15 13 16 14 #include "../Matrix/matrix.h" … … 18 16 #include "../MemOps/MemOps.h" 19 17 #include "../io/io.h" 18 #include "./randomgenerator.h" 20 19 /*}}}*/ 21 20 22 void univariateNormal(IssmPDouble* prand, IssmPDouble mean, IssmPDouble sdev, int seed fixed=-1) { /*{{{*/21 void univariateNormal(IssmPDouble* prand, IssmPDouble mean, IssmPDouble sdev, int seed=-1) { /*{{{*/ 23 22 24 unsigned seed; 25 /*Random seed using time_since_epoch*/ 26 if(seedfixed==-1) seed = std::chrono::steady_clock::now().time_since_epoch().count(); 27 /*Seed fixed by input argument*/ 28 else seed = seedfixed; 29 std::default_random_engine generator(seed); 30 /*Normal Probability Distribution*/ 31 std::normal_distribution<IssmPDouble> normdistri(mean,sdev); 32 *prand = normdistri(generator); 23 /*Seed the pseudo-random number generator*/ 24 rnd::linear_congruential_engine randomengine; 25 randomengine.seed(seed); 26 /*Normal distribution*/ 27 rnd::normal_distribution distriNormal(mean,sdev); 28 *prand = distriNormal.generator(randomengine); 33 29 } /*}}}*/ 34 void multivariateNormal(IssmDouble** prand, int dim, IssmDouble mean, IssmDouble* covariancematrix, int seed fixed=-1) { /*{{{*/30 void multivariateNormal(IssmDouble** prand, int dim, IssmDouble mean, IssmDouble* covariancematrix, int seed=-1) { /*{{{*/ 35 31 36 32 IssmPDouble* sampleStandardNormal = xNew<IssmPDouble>(dim); … … 38 34 IssmDouble* Lchol = xNewZeroInit<IssmDouble>(dim*dim); 39 35 40 /*True randomness if seedfixed==-1, otherwise random seed is fixed at seedfixed*/ 41 for(int i=0;i<dim;i++) univariateNormal(&(sampleStandardNormal[i]),0.0,1.0,seedfixed); 36 /*True randomness if seed<0, otherwise random seed is fixed at seed*/ 37 /*Seed the pseudo-random number generator, repeatedly calling univariateNormal does not ensure randomness*/ 38 rnd::linear_congruential_engine randomengine; 39 randomengine.seed(seed); 40 /*Normal distribution*/ 41 rnd::normal_distribution distriNormal(0.0,1.0); 42 for(int i=0;i<dim;i++){ 43 sampleStandardNormal[i] = distriNormal.generator(randomengine); 44 } 45 46 /*Cholsesky decomposition of the covariance matrix*/ 42 47 CholeskyRealPositiveDefinite(Lchol,covariancematrix,dim); 43 48 … … 55 60 xDelete<IssmDouble>(Lchol); 56 61 } /*}}}*/ 57 void multivariateNormal(IssmDouble** prand, int dim, IssmDouble* mean, IssmDouble* covariancematrix, int seed fixed=-1) { /*{{{*/62 void multivariateNormal(IssmDouble** prand, int dim, IssmDouble* mean, IssmDouble* covariancematrix, int seed=-1) { /*{{{*/ 58 63 59 64 IssmPDouble* sampleStandardNormal = xNew<IssmPDouble>(dim); 60 65 IssmDouble* sampleMultivariateNormal = xNew<IssmDouble>(dim); 61 66 IssmDouble* Lchol = xNewZeroInit<IssmDouble>(dim*dim); 67 68 /*True randomness if seed<0, otherwise random seed is fixed at seed*/ 69 /*Seed the pseudo-random number generator, repeatedly calling univariateNormal does not ensure randomness*/ 70 rnd::linear_congruential_engine randomengine; 71 randomengine.seed(seed); 72 /*Normal distribution*/ 73 rnd::normal_distribution distriNormal(0.0,1.0); 74 for(int i=0;i<dim;i++){ 75 sampleStandardNormal[i] = distriNormal.generator(randomengine); 76 } 62 77 63 /*True randomness if seedfixed==-1, otherwise random seed is fixed at seedfixed*/ 64 for(int i=0;i<dim;i++) univariateNormal(&(sampleStandardNormal[i]),0.0,1.0,seedfixed); 78 /*Cholsesky decomposition of the covariance matrix*/ 65 79 CholeskyRealPositiveDefinite(Lchol,covariancematrix,dim); 66 80 -
issm/trunk-jpl/src/c/shared/Random/random.h
r26483 r26621 6 6 #define _RANDOM_H_ 7 7 8 void univariateNormal(IssmDouble* prand, IssmDouble mean, IssmDouble sdev, int seed fixed=-1);9 void multivariateNormal(IssmDouble** prand, int dim, IssmDouble mean, IssmDouble* covariancematrix, int seed fixed=-1);10 void multivariateNormal(IssmDouble** prand, int dim, IssmDouble* mean, IssmDouble* covariancematrix, int seed fixed=-1);8 void univariateNormal(IssmDouble* prand, IssmDouble mean, IssmDouble sdev, int seed=-1); 9 void multivariateNormal(IssmDouble** prand, int dim, IssmDouble mean, IssmDouble* covariancematrix, int seed=-1); 10 void multivariateNormal(IssmDouble** prand, int dim, IssmDouble* mean, IssmDouble* covariancematrix, int seed=-1); 11 11 12 12 #endif //ifndef _RANDOM_H_
Note:
See TracChangeset
for help on using the changeset viewer.