forked from physimals/fabber_core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfwdmodel_poly.cc
More file actions
80 lines (65 loc) · 1.83 KB
/
Copy pathfwdmodel_poly.cc
File metadata and controls
80 lines (65 loc) · 1.83 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
/* fwdmodel_poly.cc - Implements a polynomial model
Martin Craig
Copyright (C) 2016 University of Oxford */
/* CCOPYRIGHT */
#include "fwdmodel_poly.h"
#include "dist_mvn.h"
#include "rundata.h"
#include "version.h"
#include <newmatio.h>
#include <string>
#include <vector>
using namespace std;
static OptionSpec OPTIONS[]
= { { "degree", OPT_INT, "Maximum power in the polynomial function", OPT_REQ, "" }, { "" } };
FwdModel *PolynomialFwdModel::NewInstance()
{
return new PolynomialFwdModel();
}
void PolynomialFwdModel::GetOptions(vector<OptionSpec> &opts) const
{
for (int i = 0; OPTIONS[i].name != ""; i++)
{
opts.push_back(OPTIONS[i]);
}
}
string PolynomialFwdModel::GetDescription() const
{
return "Model which fits data to a simple polynomial function: c0 + c1x + "
"c2x^2 ... etc";
}
string PolynomialFwdModel::ModelVersion() const
{
return fabber_version();
}
void PolynomialFwdModel::Initialize(FabberRunData &args)
{
FwdModel::Initialize(args);
m_degree = convertTo<int>(args.GetString("degree"));
}
void PolynomialFwdModel::GetParameterDefaults(std::vector<Parameter> ¶ms) const
{
for (int i = 0; i < m_degree + 1; i++)
{
params.push_back(
Parameter(i, "c" + stringify(i), DistParams(0, 1e12), DistParams(0, 1e12)));
}
}
void PolynomialFwdModel::EvaluateModel(
const NEWMAT::ColumnVector ¶ms, NEWMAT::ColumnVector &result, const std::string &key) const
{
assert(params.Nrows() == m_degree + 1);
result.ReSize(data.Nrows());
// LOG << "Evaluating for : " << params.t() << endl;
for (int i = 1; i <= result.Nrows(); i++)
{
double res = 0;
int p = 1;
for (int n = 0; n <= m_degree; n++)
{
res += params(n + 1) * p;
p *= i;
}
result(i) = res;
}
}