-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathf_random.cc
189 lines (170 loc) · 5.4 KB
/
f_random.cc
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* -*- C++ -*-
* Copyright (C) 2016 Felix Salfelder
* Author: same
*
* This file is part of "Gnucap", the Gnu Circuit Analysis Package
*
* 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 3, 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
// implements (a)gauss, (a)unif and limit as per ngspice manual.
/*--------------------------------------------------------------------------*/
#include <u_parameter.h>
#include <u_function.h>
#include <globals.h>
#include <gsl/gsl_randist.h>
// -uf compat hack
#ifndef _U_FUNC
typedef std::string fun_t;
#define to_fun_t to_string
#endif
/*--------------------------------------------------------------------------*/
namespace{
/*--------------------------------------------------------------------------*/
struct myrng{
myrng(){
const gsl_rng_type * T;
T = gsl_rng_default;
gsl_rng_env_setup();
_rng = gsl_rng_alloc(T);
}
gsl_rng *_rng;
} rng;
/*--------------------------------------------------------------------------*/
class agauss : public FUNCTION {
public:
fun_t eval(CS& Cmd, const CARD_LIST* Scope)const
{
PARAMETER<double> mean;
PARAMETER<double> abs_dev_in; // standard deviation. NOT variance
PARAMETER<double> sigma; // some weird scaling. NOT standard deviation
Cmd >> mean >> abs_dev_in;
if(!Cmd.more()){ untested();
}else{ untested();
Cmd >> sigma;
sigma.e_val(NOT_INPUT, Scope);
}
mean.e_val(NOT_INPUT, Scope);
abs_dev_in.e_val(NOT_INPUT, Scope);
double abs_dev = abs_dev_in;
if(!sigma.has_hard_value()){ untested();
}else if(double(sigma)==0){
incomplete();
}else{ untested();
abs_dev = abs_dev/sigma;
}
return to_fun_t(gsl_ran_gaussian(rng._rng, abs_dev) + mean);
}
private:
} p_gauss;
DISPATCHER<FUNCTION>::INSTALL d_gauss(&function_dispatcher, "agauss", &p_gauss);
/*--------------------------------------------------------------------------*/
class rgauss : public FUNCTION {
public:
fun_t eval(CS& Cmd, const CARD_LIST* Scope)const
{
PARAMETER<double> mean;
PARAMETER<double> rel_dev; // relative std deviation. NOT variance
PARAMETER<double> sigma; // some weird scaling. NOT standard deviation
Cmd >> mean >> rel_dev;
mean.e_val(NOT_INPUT, Scope);
rel_dev.e_val(NOT_INPUT, Scope);
if(!Cmd.more()){ untested();
}else{ untested();
Cmd >> sigma;
sigma.e_val(NOT_INPUT, Scope);
}
mean.e_val(NOT_INPUT, Scope);
double abs_dev = 0;
if(!mean.has_hard_value()){
incomplete();
}else{ untested();
abs_dev = rel_dev*mean;
}
if(!sigma.has_hard_value()){ untested();
}else if(double(sigma)==0){
incomplete();
}else{
abs_dev = abs_dev/sigma;
}
return to_fun_t(gsl_ran_gaussian(rng._rng, abs_dev) + mean);
}
private:
} p_rgauss;
DISPATCHER<FUNCTION>::INSTALL d_rgauss(&function_dispatcher, "gauss|rgauss", &p_rgauss);
/*--------------------------------------------------------------------------*/
class runif : public FUNCTION {
public:
fun_t eval(CS& Cmd, const CARD_LIST* Scope)const
{
PARAMETER<double> mean;
PARAMETER<double> rel_dev; // relative std deviation. NOT variance
Cmd >> mean >> rel_dev;
mean.e_val(NOT_INPUT, Scope);
rel_dev.e_val(NOT_INPUT, Scope);
double abs_dev = 0;
if(!mean.has_hard_value()){
incomplete();
}else{ untested();
abs_dev = rel_dev*mean;
}
double rnd=gsl_rng_uniform_pos (rng._rng);
rnd *= 2;
rnd -= 1;
return to_fun_t( rnd*abs_dev + mean);
}
private:
} p_runif;
DISPATCHER<FUNCTION>::INSTALL d_runif(&function_dispatcher, "unif|runif", &p_runif);
/*--------------------------------------------------------------------------*/
class aunif : public FUNCTION {
public:
fun_t eval(CS& Cmd, const CARD_LIST* Scope)const
{
PARAMETER<double> mean;
PARAMETER<double> abs_dev; // relative std deviation. NOT variance
Cmd >> mean >> abs_dev;
mean.e_val(NOT_INPUT, Scope);
abs_dev.e_val(NOT_INPUT, Scope);
double rnd=gsl_rng_uniform_pos (rng._rng);
rnd *= 2;
rnd -= 1;
return to_fun_t( rnd*abs_dev + mean);
}
private:
} p_aunif;
DISPATCHER<FUNCTION>::INSTALL d_aunif(&function_dispatcher, "aunif", &p_aunif);
/*--------------------------------------------------------------------------*/
class limit : public FUNCTION {
public:
fun_t eval(CS& Cmd, const CARD_LIST* Scope)const
{
PARAMETER<double> mean;
PARAMETER<double> abs_dev; // relative std deviation. NOT variance
Cmd >> mean >> abs_dev;
mean.e_val(NOT_INPUT, Scope);
abs_dev.e_val(NOT_INPUT, Scope);
if(gsl_rng_uniform_int(rng._rng, 2)){
abs_dev =- abs_dev;
}
return to_fun_t( abs_dev + mean);
}
private:
} p_limit;
DISPATCHER<FUNCTION>::INSTALL d_limit(&function_dispatcher, "limit", &p_limit);
/*--------------------------------------------------------------------------*/
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/