1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
@*
GaussianBoxMuellerRandomVariable.tmpl
Created by Joe Hope on 2009-08-20.
Copyright (c) 2009-2012, Joe Hope
This program 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.
This program 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, see <http://www.gnu.org/licenses/>.
*@
@extends xpdeint.Stochastic.RandomVariables.GaussianRandomVariable
@def boxMuellerAlgorithm
@*doc:
Return the code to generate two gaussian noises
using the Box-Mueller algorithm
*@
@#
real _v1, _v2, _rsq;
do {
_v1 = ${generator.minusOneToOneRandomNumber};
_v2 = ${generator.minusOneToOneRandomNumber};
_rsq = _v1*_v1 + _v2*_v2;
} while(_rsq >= 1.0 || _rsq == 0.0);
const real _fac = sqrt(-2.0*_var*log(_rsq)/_rsq);
@#
@end def
@def makeFixedVarianceNoises
@*doc:
Return the code for the contents of the makeNoises function for
a gaussian noise generated with the Box Mueller algorithm
*@
@set noiseVector = $parent
const ptrdiff_t _evenNoises = _vector_size & ~1;
for (ptrdiff_t _i0 = 0; _i0 < _evenNoises; _i0 += 2) {
${boxMuellerAlgorithm, autoIndent=True}@slurp
reinterpret_cast<real*>(_active_${noiseVector.id})[_i0 + 0] = _v1*_fac;
reinterpret_cast<real*>(_active_${noiseVector.id})[_i0 + 1] = _v2*_fac;
}
// If _n is odd, we need to generate the last random number
if (_vector_size & 1) {
static real _spareNoise = 0.0;
static bool _spareNoiseAvailable = false;
static real _old_var = 0.0;
if (_spareNoiseAvailable && _old_var == _var) {
reinterpret_cast<real*>(_active_${noiseVector.id})[_vector_size - 1] = _spareNoise;
_spareNoiseAvailable = false;
} else {
${boxMuellerAlgorithm, autoIndent=True}@slurp
reinterpret_cast<real*>(_active_${noiseVector.id})[_vector_size - 1] = _v1*_fac;
_spareNoise = _v2*_fac;
_spareNoiseAvailable = true;
_old_var = _var;
}
}
@#
@end def
|