Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
NormalDistribution.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2022 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* NormalDistribution.h (C) 2000-2022 */
9/* */
10/* Randomiser 'NormalDistribution'. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13#ifndef ARCANE_RANDOM_NORMALDISTRIBUTION_H
14#define ARCANE_RANDOM_NORMALDISTRIBUTION_H
15/*---------------------------------------------------------------------------*/
16/*---------------------------------------------------------------------------*/
17
18#include "arcane/utils/FatalErrorException.h"
19#include "arcane/utils/Math.h"
20
21#include "arcane/random/RandomGlobal.h"
22
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
26namespace Arcane::random
27{
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
32/*
33 * Copyright Jens Maurer 2000-2001
34 * Permission to use, copy, modify, sell, and distribute this software
35 * is hereby granted without fee provided that the above copyright notice
36 * appears in all copies and that both that copyright notice and this
37 * permission notice appear in supporting documentation,
38 *
39 * Jens Maurer makes no representations about the suitability of this
40 * software for any purpose. It is provided "as is" without express or
41 * implied warranty.
42 *
43 * See http://www.boost.org for most recent version including documentation.
44 *
45 * $Id: NormalDistribution.h 3932 2004-08-23 08:45:03Z grospelx $
46 *
47 * Revision history
48 * 2001-02-18 moved to individual header files
49 */
50
51/*---------------------------------------------------------------------------*/
52/*---------------------------------------------------------------------------*/
53
54// deterministic polar method, uses trigonometric functions
55template<class UniformRandomNumberGenerator>
57{
58 public:
59
61
62 explicit NormalDistribution(base_type & rng,Real mean = 0.0,Real sigma = 1.0)
63 : _rng(rng), _mean(mean), _sigma(sigma), _valid(false)
64 {
65 }
66
67 // compiler-generated copy constructor is NOT fine, need to purge cache
69 : _rng(other._rng), _mean(other._mean), _sigma(other._sigma), _valid(false)
70 {
71 }
72 // uniform_01 cannot be assigned, neither can this class
73
74 Real operator()()
75 {
76 if (!_valid) {
77 _r1 = _rng();
78 _r2 = _rng();
79 _cached_rho = math::sqrt(-2 * math::log(1.0-_r2));
80 _valid = true;
81 }
82 else
83 _valid = false;
84 // Can we have a boost::mathconst please?
85 const double pi = 3.14159265358979323846;
86
87 return _cached_rho * (_valid ? cos(2*pi*_r1) : sin(2*pi*_r1)) * _sigma + _mean;
88 }
89
90 private:
91
93 const Real _mean, _sigma;
94 Real _r1 = 0.0;
95 Real _r2 = 0.0;
96 Real _cached_rho = 0.0;
97 bool _valid;
98};
99
100/*---------------------------------------------------------------------------*/
101/*---------------------------------------------------------------------------*/
102
103} // End namespace Arcane::random
104
105/*---------------------------------------------------------------------------*/
106/*---------------------------------------------------------------------------*/
107
108#endif
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120
ARCCORE_HOST_DEVICE double log(double v)
Logarithme népérien de v.
Definition Math.h:40
ARCCORE_HOST_DEVICE double sqrt(double v)
Racine carrée de v.
Definition Math.h:135