[082b02]: src / modules / bugs / functions / InterpLin.cc Maximize Restore History

Download this file

InterpLin.cc    65 lines (55 with data), 1.4 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
#include <config.h>
#include "InterpLin.h"
#include <util/nainf.h>
#include <util/dim.h>
using std::vector;
namespace jags {
namespace bugs {
InterpLin::InterpLin() : VectorFunction("interp.lin", 3)
{}
void InterpLin::evaluate(double *value, vector<double const *> const &args,
vector<unsigned int> const &lengths) const
{
double const *x = args[1];
double const *y = args[2];
double xnew = args[0][0];
unsigned int N = lengths[1];
if (xnew < x[0])
value[0] = y[0];
else if (xnew >= x[N-1])
value[0] = y[N-1];
else {
unsigned int i = 0;
for (; i < N-1; ++i) {
if (xnew >= x[i] && xnew < x[i+1]) {
break;
}
}
if (i == N-1) {
/* This shouldn't happen, but we must guard against an
attempt to access an illegal element of x or y */
value[0] = JAGS_NAN;
}
else {
value[0] = y[i] +
(xnew - x[i]) * (y[i+1] - y[i]) / (x[i+1] - x[i]);
}
}
}
bool InterpLin::checkParameterLength(vector<unsigned int> const &lengths)
const
{
return (lengths[0] == 1) && (lengths[1] == lengths[2]);
}
bool
InterpLin::checkParameterValue(vector <double const *> const &args,
vector <unsigned int> const &lengths) const
{
unsigned int N = lengths[1];
for (unsigned int i = 1; i < N; ++i) {
if (args[1][i] <= args[1][i-1])
return false;
}
return true;
}
}}