[ff1121]: src / lib / function / testfun.cc  Maximize  Restore  History

Download this file

339 lines (297 with data), 8.6 kB

  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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include <function/testfun.h>
#include <util/nainf.h>
#include <util/logical.h>
#include <cppunit/extensions/HelperMacros.h>
using jags::ScalarFunction;
using jags::VectorFunction;
using jags::Function;
#include <climits>
#include <cmath>
#include <algorithm>
using std::vector;
using std::string;
using std::copy;
/* All functions */
bool isdiscrete(Function const *f, bool mask1)
{
CPPUNIT_ASSERT(checkNPar(f, 1));
return f->isDiscreteValued(vector<bool>(1, mask1));
}
bool isdiscrete(Function const *f, bool mask1, bool mask2)
{
CPPUNIT_ASSERT(checkNPar(f, 2));
vector<bool> arg(2);
arg[0] = mask1;
arg[1] = mask2;
return f->isDiscreteValued(arg);
}
bool isdiscrete(Function const *f, bool mask1, bool mask2, bool mask3)
{
CPPUNIT_ASSERT(checkNPar(f, 3));
vector<bool> arg(3);
arg[0] = mask1;
arg[1] = mask2;
arg[2] = mask3;
return f->isDiscreteValued(arg);
}
class BoolIterator : public std::vector<bool>
{
public:
bool atEnd;
BoolIterator(unsigned int n) : vector<bool>(n, false), atEnd(false) {};
void next() {
bool bump = true;
for (unsigned int i = 0; i < size(); ++i) {
if (bump) {
bool x = operator[](i); //current value
bump = x;
operator[](i) = !x;
}
else return;
}
if (bump) atEnd=true;
}
};
bool isdiscrete(Function const *f, unsigned int npar,
bool (*predicate) (vector<bool> const &))
{
CPPUNIT_ASSERT(checkNPar(f, npar));
BoolIterator mask(npar);
for(BoolIterator mask(npar); !mask.atEnd; mask.next()) {
if (f->isDiscreteValued(mask) != predicate(mask)) {
return false;
}
}
return true;
}
bool always(vector<bool> const &mask) { return true; }
bool never(vector<bool> const &mask) { return false; }
bool all(vector<bool> const &mask) { return allTrue(mask); }
bool any(vector<bool> const &mask) { return anyTrue(mask); }
bool neverlinear(Function const *f, unsigned int npar)
{
CPPUNIT_ASSERT_MESSAGE(f->name(), checkNPar(f, npar));
for(BoolIterator mask(npar); !mask.atEnd; mask.next()) {
if (f->isLinear(mask, vector<bool>())) return false;
for(BoolIterator fixed(npar); !fixed.atEnd; fixed.next()) {
if (f->isLinear(mask, fixed)) return false;
}
}
return true;
}
bool neverscale(Function const *f, unsigned int npar)
{
CPPUNIT_ASSERT_MESSAGE(f->name(), checkNPar(f, npar));
for(BoolIterator mask(npar); !mask.atEnd; mask.next()) {
if (f->isScale(mask, vector<bool>())) return false;
for(BoolIterator fixed(npar); !fixed.atEnd; fixed.next()) {
if (f->isScale(mask, fixed)) return false;
}
}
return true;
}
bool neverpow(Function const *f, unsigned int npar)
{
CPPUNIT_ASSERT_MESSAGE(f->name(), checkNPar(f, npar));
for(BoolIterator mask(npar); !mask.atEnd; mask.next()) {
if (f->isPower(mask, vector<bool>())) return false;
for(BoolIterator fixed(npar); !fixed.atEnd; fixed.next()) {
if (f->isPower(mask, fixed)) return false;
}
}
return true;
}
bool neverslp(Function const *f, unsigned int npar)
{
return neverscale(f, npar) && neverlinear(f, npar) && neverpow(f, npar);
}
/* Scalar functions */
void checkLimits(ScalarFunction const *f, double lower, double upper)
{
CPPUNIT_ASSERT(lower < upper);
CPPUNIT_ASSERT_MESSAGE(f->name(), checkval(f, lower));
CPPUNIT_ASSERT_MESSAGE(f->name(), checkval(f, upper));
CPPUNIT_ASSERT_MESSAGE(f->name(), !jags_isnan(eval(f, lower)));
CPPUNIT_ASSERT_MESSAGE(f->name(), !jags_isnan(eval(f, upper)));
if (jags_finite(upper)) {
if (upper > 1) {
upper *= (1.0 + DBL_EPSILON);
}
else if (upper < -1) {
upper *= (1.0 - DBL_EPSILON);
}
else {
upper += DBL_EPSILON;
}
CPPUNIT_ASSERT_MESSAGE(f->name(), !checkval(f, upper));
}
if (jags_finite(lower)) {
if (lower > 1) {
lower *= (1.0 - DBL_EPSILON);
}
else if (lower < -1) {
lower *= (1.0 + DBL_EPSILON);
}
else {
lower -= DBL_EPSILON;
}
CPPUNIT_ASSERT_MESSAGE(f->name(), !checkval(f, lower));
}
}
/*
Evaluate a scalar function that takes a single argument
*/
double eval(ScalarFunction const *f, double x)
{
CPPUNIT_ASSERT_MESSAGE(f->name(), checkNPar(f, 1));
vector<double const *> arg(1, &x);
CPPUNIT_ASSERT_MESSAGE(f->name(), f->checkParameterValue(arg));
return f->evaluate(arg);
}
/*
Evaluate a scalar function that takes two arguments
*/
double eval(ScalarFunction const *f, double x, double y)
{
CPPUNIT_ASSERT_MESSAGE(f->name(), checkNPar(f, 2));
vector<double const *> args(2);
args[0] = &x;
args[1] = &y;
CPPUNIT_ASSERT_MESSAGE(f->name(), f->checkParameterValue(args));
return f->evaluate(args);
}
/*
Evaluate a scalar function that takes three arguments
*/
double eval(ScalarFunction const *f, double x, double y, double z)
{
CPPUNIT_ASSERT_MESSAGE(f->name(), checkNPar(f, 3));
vector<double const *> args(3);
args[0] = &x;
args[1] = &y;
args[2] = &z;
CPPUNIT_ASSERT_MESSAGE(f->name(), f->checkParameterValue(args));
return f->evaluate(args);
}
/*
Check validity of argument to a scalar function that takes a single
scalar argument
*/
bool checkval(ScalarFunction const *f, double x)
{
CPPUNIT_ASSERT_MESSAGE(f->name(), checkNPar(f, 1));
vector<double const *> arg(1, &x);
return f->checkParameterValue(arg);
}
bool checkval(ScalarFunction const *f, double x, double y)
{
CPPUNIT_ASSERT_MESSAGE(f->name(), checkNPar(f, 2));
vector<double const *> args(2);
args[0] = &x;
args[1] = &y;
return f->checkParameterValue(args);
}
bool checkval(ScalarFunction const *f, double x, double y, double z)
{
CPPUNIT_ASSERT_MESSAGE(f->name(), checkNPar(f, 3));
vector<double const *> args(3);
args[0] = &x;
args[1] = &y;
args[2] = &z;
return f->checkParameterValue(args);
}
vector<double> mkVec(double const *x, unsigned int N)
{
vector<double> y(N);
copy(x, x + N, y.begin());
return y;
}
static vector<double> mkVec(double x)
{
return vector<double>(1, x);
}
/* Evaluate a VectorFunction that takes a single argument */
vector<double>
veval(VectorFunction const *f, vector<double> const &x)
{
CPPUNIT_ASSERT_MESSAGE(f->name(), checkNPar(f, 1));
vector<double const *> arg(1, &x[0]);
vector<unsigned int> arglen(1, x.size());
CPPUNIT_ASSERT_MESSAGE(f->name(), f->checkParameterLength(arglen));
CPPUNIT_ASSERT_MESSAGE(f->name(), f->checkParameterValue(arg, arglen));
vector<double> ans(f->length(arglen));
f->evaluate(&ans[0], arg, arglen);
return ans;
}
//Evaluate a VectorFunction that takes two arguments
vector<double>
veval(VectorFunction const *f, vector<double> const &x, vector<double> const &y)
{
CPPUNIT_ASSERT_MESSAGE(f->name(), checkNPar(f, 2));
vector<double const *> arg(2);
arg[0] = &x[0];
arg[1] = &y[0];
vector<unsigned int> arglen(2);
arglen[0] = x.size();
arglen[1] = y.size();
vector<double> ans(f->length(arglen));
f->evaluate(&ans[0], arg, arglen);
return ans;
}
/*
Evaluate a VectorFunction that takes a single argument and returns a
scalar
*/
double eval(VectorFunction const *f, vector<double> const &x)
{
vector<double> y = veval(f, x);
unsigned int N = y.size();
CPPUNIT_ASSERT_EQUAL(1U, N);
return y[0];
}
double eval(VectorFunction const *f, vector<double> const &x,
vector<double> const &y)
{
vector<double> z = veval(f, x, y);
unsigned int N = z.size();
CPPUNIT_ASSERT_EQUAL(1U, N);
return z[0];
}
/*
Evaluate a VectorFunction that takes two arguments and returns a
scalar. The first argument is also a scalar
*/
double eval(VectorFunction const *f, double x, vector<double> const &y)
{
return eval(f, mkVec(x), y);
}
/*
Evaluate a VectorFunction that takes two arguments and returns a
scalar. The first argument is also a scalar
*/
double eval(VectorFunction const *f, vector<double> const &x, double y)
{
return eval(f, x, mkVec(y));
}
/*
Check that VectorFunction taking a single argument has valid
parameter length
*/
bool checkparlen(VectorFunction const *f, unsigned int n)
{
vector<unsigned int> arglen(1, n);
CPPUNIT_ASSERT(checkNPar(f, 1));
return f->checkParameterLength(arglen);
}
/*
Check that VectorFunction taking two arguments has valid
parameter length
*/
bool checkparlen(VectorFunction const *f, unsigned int n1, unsigned int n2)
{
vector<unsigned int> arglen(2);
arglen[0] = n1;
arglen[1] = n2;
CPPUNIT_ASSERT(checkNPar(f, 2));
return f->checkParameterLength(arglen);
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks