-
Notifications
You must be signed in to change notification settings - Fork 122
/
IFunctionAutoDiff.h
139 lines (103 loc) · 3.57 KB
/
IFunctionAutoDiff.h
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
#ifndef MANTID_API_IFUNCTIONAUTODIFF_H_
#define MANTID_API_IFUNCTIONAUTODIFF_H_
#include "MantidKernel/System.h"
#include "MantidAPI/IFunction.h"
#include "MantidAPI/Jacobian.h"
#include "MantidAPI/ParamFunction.h"
#include <adept.h>
namespace Mantid
{
namespace API
{
/** IFunctionAutoDiff : TODO: DESCRIPTION
Copyright © 2014 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid 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 of the License, or
(at your option) any later version.
Mantid 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, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class AutoDiffJacobian : public Jacobian
{
public:
AutoDiffJacobian() : Jacobian(),
m_nParams(0), m_nValues(0), m_jacobian(), m_isValid(false)
{ }
virtual ~AutoDiffJacobian() { }
void setSize(size_t nParams, size_t nValues) {
m_nParams = nParams;
m_nValues = nValues;
m_jacobian.resize(nParams * nValues);
}
double *rawValues() {
return &m_jacobian[0];
}
virtual void set(size_t iY, size_t iP, double value) {
m_jacobian[iY + iP * m_nValues] = value;
}
virtual double get(size_t iY, size_t iP) {
return m_jacobian[iY + iP * m_nValues];
}
void setValid() {
m_isValid = true;
}
void setInvalid() {
m_isValid = false;
}
bool isValid() {
return m_isValid;
}
protected:
size_t m_nParams;
size_t m_nValues;
std::vector<double> m_jacobian;
bool m_isValid;
};
class AutoDiffParameters
{
public:
AutoDiffParameters(const IFunction *fromFunction)
: m_names(), m_parameters()
{
size_t np = fromFunction->nParams();
m_parameters.resize(np);
for(size_t i = 0; i < np; ++i) {
m_names[fromFunction->parameterName(i)] = i;
m_parameters[i].set_value(fromFunction->getParameter(i));
}
}
size_t size() const { return m_parameters.size(); }
adept::adouble &operator[](size_t i) { return m_parameters[i]; }
adept::adouble getParameter(size_t i) const
{
return m_parameters[i];
}
adept::adouble getParameter(std::string &name) const
{
return m_parameters[m_names.at(name)];
}
private:
std::map<std::string, size_t> m_names;
std::vector<adept::adouble> m_parameters;
};
class DLLExport IFunctionAutoDiff : virtual public IFunction
{
public:
IFunctionAutoDiff();
virtual ~IFunctionAutoDiff();
void function(const FunctionDomain &domain, FunctionValues &values) const;
void functionDeriv(const FunctionDomain &domain, Jacobian &jacobian);
void functionWrapper(const FunctionDomain &domain, FunctionValues &values, AutoDiffJacobian *jacobian) const;
virtual void functionAutoDiff(std::vector<adept::adouble> &x, std::vector<adept::adouble> &y, const AutoDiffParameters ¶meters) const = 0;
};
} // namespace API
} // namespace Mantid
#endif /* MANTID_API_IFUNCTIONAUTODIFF_H_ */