NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
plugin_random.cpp
Go to the documentation of this file.
1/*****************************************************************************
2 NumeRe: Framework fuer Numerische Rechnungen
3 Copyright (C) 2014 Erik Haenel et al.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17******************************************************************************/
18
19
20/*
21 * Realisierung des Random-Plugins
22 */
23
24#include <random>
25#include <ctime>
26
27#include "plugins.hpp"
28#include "../kernel.hpp"
30#include "structures.hpp"
31
45static double getParameterValue(const std::string& sCmd, const std::string& sLongVersion, const std::string& sShortVersion, Parser& _parser, double defaultVal)
46{
47 if (findParameter(sCmd, sLongVersion, '=') || findParameter(sCmd, sShortVersion, '='))
48 {
49 int nPos = 0;
50
51 if (findParameter(sCmd, sLongVersion, '='))
52 nPos = findParameter(sCmd, sLongVersion, '=')+sLongVersion.length();
53 else
54 nPos = findParameter(sCmd, sShortVersion, '=')+sShortVersion.length();
55
56 _parser.SetExpr(getArgAtPos(sCmd, nPos));
57 return _parser.Eval().real();
58 }
59
60 return defaultVal;
61}
62
63
69{
76};
77
78
87void plugin_random(std::string& sCmd)
88{
89 // Get all necessary references
93 Indices _idx;
94
96 static double dSeedBase = 1.0;
97 std::string sDistrib = _lang.get("RANDOM_DISTRIB_TYPE_GAUSS");
98 std::string sTarget = evaluateTargetOptionInCommand(sCmd, "table", _idx, _parser, _data, _option);
99 double dRandomNumber = 0.0;
100 std::default_random_engine randomGenerator(dSeedBase * time(0)); // Zufallszahlengenerator initialisieren
101
102 // Get all parameter values (or use default ones)
103 long long int nDataPoints = intCast(getParameterValue(sCmd, "lines", "l", _parser, 0.0));
104 long long int nDataRows = intCast(getParameterValue(sCmd, "cols", "c", _parser, 0.0));
105 double dDistributionMean = getParameterValue(sCmd, "mean", "m", _parser, 0.0);
106 double dDistributionWidth = fabs(getParameterValue(sCmd, "width", "w", _parser, 1.0));
107 double dShape = fabs(getParameterValue(sCmd, "shape", "sh", _parser, 1.0));
108 double dScale = fabs(getParameterValue(sCmd, "scale", "sc", _parser, 1.0));
109 double dProbability = fabs(getParameterValue(sCmd, "prob", "p", _parser, 0.5));
110 unsigned int nUpperBound = abs(intCast(getParameterValue(sCmd, "ubound", "ub", _parser, 1.0)));
111 unsigned int nFreedoms = abs(intCast(getParameterValue(sCmd, "freedoms", "f", _parser, 1.0)));
112
113 // Find the type of random distribution
114 if (findParameter(sCmd, "distrib", '=') || findParameter(sCmd, "d", '='))
115 {
116 int nPos = 0;
117
118 if (findParameter(sCmd, "distrib", '='))
119 nPos = findParameter(sCmd, "distrib", '=')+7;
120 else
121 nPos = findParameter(sCmd, "d", '=')+1;
122
123 sDistrib = getArgAtPos(sCmd, nPos);
124
125 if (sDistrib == "gauss" || sDistrib == "normal")
126 {
127 sDistrib = _lang.get("RANDOM_DISTRIB_TYPE_GAUSS");
128 nDistribution = NORMAL_DISTRIBUTION;
129 }
130 else if (sDistrib == "poisson")
131 {
132 sDistrib = _lang.get("RANDOM_DISTRIB_TYPE_POISSON");
133 nDistribution = POISSON_DISTRIBUTION;
134 }
135 else if (sDistrib == "gamma")
136 {
137 sDistrib = _lang.get("RANDOM_DISTRIB_TYPE_GAMMA");
138 nDistribution = GAMMA_DISTRIBUTION;
139 }
140 else if (sDistrib == "uniform")
141 {
142 sDistrib = _lang.get("RANDOM_DISTRIB_TYPE_UNIFORM");
143 nDistribution = UNIFORM_DISTRIBUTION;
144 }
145 else if (sDistrib == "binomial")
146 {
147 sDistrib = _lang.get("RANDOM_DISTRIB_TYPE_BINOMIAL");
148 nDistribution = BINOMIAL_DISTRIBUTION;
149 }
150 else if (sDistrib == "student")
151 {
152 sDistrib = _lang.get("RANDOM_DISTRIB_TYPE_STUDENT");
153 nDistribution = STUDENT_DISTRIBUTION;
154 }
155 else
156 {
157 sDistrib = _lang.get("RANDOM_DISTRIB_TYPE_GAUSS");
158 nDistribution = NORMAL_DISTRIBUTION;
159 }
160 }
161
162 if (!nDataRows)
164
165 if (!nDataPoints)
167
168 // Create random distributions
169 std::normal_distribution<double> normalDistribution(dDistributionMean, dDistributionWidth);
170 std::poisson_distribution<int> poissonDistribution(dDistributionMean);
171 std::gamma_distribution<double> gammaDistribution(dShape, dScale);
172 std::uniform_real_distribution<double> uniformDistribution(dDistributionMean-0.5*dDistributionWidth, dDistributionMean+0.5*dDistributionWidth);
173 std::binomial_distribution<int> binomialDistribution(nUpperBound, dProbability);
174 std::student_t_distribution<double> studentTDistribution(nFreedoms);
175
176 // Fill the table with the newly created random numbers
177 for (long long int i = 0; i < nDataPoints; i++)
178 {
179 for (long long int j = 0; j < nDataRows; j++)
180 {
181 switch (nDistribution)
182 {
184 dRandomNumber = normalDistribution(randomGenerator);
185 break;
187 dRandomNumber = poissonDistribution(randomGenerator);
188 break;
190 dRandomNumber = gammaDistribution(randomGenerator);
191 break;
193 dRandomNumber = uniformDistribution(randomGenerator);
194 break;
196 dRandomNumber = binomialDistribution(randomGenerator);
197 break;
199 dRandomNumber = studentTDistribution(randomGenerator);
200 break;
201 }
202
203 if (i < _idx.row.size() && j < _idx.col.size())
204 _data.writeToTable(_idx.row[i], _idx.col[j], sTarget, dRandomNumber);
205
206 if ((!i && !j) || dSeedBase == 0.0)
207 {
208 if (dSeedBase == dRandomNumber)
209 dSeedBase = 0.0;
210 else
211 dSeedBase = dRandomNumber;
212 }
213 }
214 }
215
216 NumeReKernel::print(_lang.get("RANDOM_SUCCESS", toString(nDataRows*nDataPoints), sDistrib, sTarget + "()"));
217}
218
219
std::string get(const std::string &sMessage, const std::vector< std::string > &vTokens) const
This member function returns the language string for the passed language identifier and replaces all ...
Definition: language.cpp:292
This class represents the central memory managing instance. It will handle all tables and clusters,...
void writeToTable(int _nLine, int _nCol, const std::string &_sCache, const mu::value_type &_dData)
static NumeReKernel * getInstance()
This static member function returns a a pointer to the singleton instance of the kernel.
Definition: kernel.hpp:221
mu::Parser & getParser()
Definition: kernel.hpp:281
MemoryManager & getMemoryManager()
Definition: kernel.hpp:263
static void print(const std::string &__sLine, bool printingEnabled=true)
This member function appends the passed string as a new output line to the buffer and informs the ter...
Definition: kernel.cpp:2636
Settings & getSettings()
Definition: kernel.hpp:296
This class manages the setting values of the internal (kernel) settings of this application.
Definition: settings.hpp:663
Common exception class for all exceptions thrown in NumeRe.
Definition: error.hpp:32
static size_t invalid_position
Definition: error.hpp:235
size_t size() const
This member function returns the size of the indices stored in this class.
Definition: structures.hpp:314
void SetExpr(StringView a_sExpr)
Set the expression. Triggers first time calculation thus the creation of the bytecode and scanning of...
value_type Eval()
Single-value wrapper around the vectorized overload of this member function.
Mathematical expressions parser.
Definition: muParser.h:51
Language _lang
Definition: kernel.cpp:39
CONSTCD11 std::chrono::duration< Rep, Period > abs(std::chrono::duration< Rep, Period > d)
Definition: date.h:1317
string evaluateTargetOptionInCommand(string &sCmd, const string &sDefaultTarget, Indices &_idx, Parser &_parser, MemoryManager &_data, const Settings &_option)
This function evaluates the "target=TABLE()" expression and creates the target table,...
void plugin_random(std::string &sCmd)
This function is the implementation of the random command.
RandomDistribution
This enumeration defines all available random distributions.
@ STUDENT_DISTRIBUTION
@ UNIFORM_DISTRIBUTION
@ BINOMIAL_DISTRIBUTION
@ NORMAL_DISTRIBUTION
@ GAMMA_DISTRIBUTION
@ POISSON_DISTRIBUTION
static double getParameterValue(const std::string &sCmd, const std::string &sLongVersion, const std::string &sShortVersion, Parser &_parser, double defaultVal)
This static function unifies the parameter detection of the random command implementation.
int findParameter(const std::string &sCmd, const std::string &sParam, const char cFollowing)
This function searches the passed parameter in the passed command string. If something is found,...
Definition: tools.cpp:113
This structure is central for managing the indices of a table or cluster read or write data access....
VectorIndex col
VectorIndex row
long long int intCast(const std::complex< double > &)
Casts the real part of the complex number to an integer and avoids rounding errors.
Definition: tools.cpp:1824
std::string toString(int)
Converts an integer to a string without the Settings bloat.
string getArgAtPos(const string &sCmd, unsigned int nPos, int extraction)
Extracts a options value at the selected position and applies automatic parsing, if necessary.
Definition: tools.cpp:1598