"...MDAlgorithms/git@code.ornl.gov:mantidproject/mantid.git" did not exist on "7360ca58f8fd95c49801f4c1f58ca66cb14ef01c"
Newer
Older
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/FunctionValues.h"
#include <algorithm>
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
namespace Mantid {
namespace API {
/**
* Constructs a set of output values based on the given domain.
* @param domain :: A reference to the domain over which the function is
* calculated
*/
FunctionValues::FunctionValues(const FunctionDomain &domain) { reset(domain); }
/** Copy constructor.
* @param values :: Values to copy from.
*/
FunctionValues::FunctionValues(const FunctionValues &values)
: m_calculated(values.m_calculated), m_data(values.m_data),
m_weights(values.m_weights) {}
/// Destructor
FunctionValues::~FunctionValues() {}
/// Reset the values to match a new domain.
void FunctionValues::reset(const FunctionDomain &domain) {
if (domain.size() == 0) {
throw std::invalid_argument("FunctionValues cannot have zero size.");
}
m_calculated.resize(domain.size());
}
/**
* Expand to a new size. Preserve old values. Do not contract.
* @param n :: A new size, must be greater than the current size.
*/
void FunctionValues::expand(size_t n) {
if (n < size()) {
throw std::invalid_argument("Cannot make FunctionValues smaller");
}
m_calculated.resize(n);
if (!m_data.empty()) {
m_data.resize(n);
}
if (!m_weights.empty()) {
m_weights.resize(n);
}
}
/** Set all calculated values to same number.
* @param value :: A new value.
*/
void FunctionValues::setCalculated(double value) {
std::fill(m_calculated.begin(), m_calculated.end(), value);
}
/**
* Get a pointer to calculated data at index i
* @param i :: Index.
*/
double *FunctionValues::getPointerToCalculated(size_t i) {
if (i < size()) {
return &m_calculated[i];
}
throw std::out_of_range("FunctionValue index out of range.");
}
/// Set all calculated values to zero
void FunctionValues::zeroCalculated() { setCalculated(0.0); }
/**
* Copy calculated values to a buffer
* @param to :: Pointer to the buffer
*/
void FunctionValues::copyTo(double *to) const {
std::copy(m_calculated.begin(), m_calculated.end(), to);
}
/** Add calculated values to values in a buffer and save result to the buffer
* @param to :: Pointer to the buffer, it must be large enough
*/
void FunctionValues::add(double *to) const {
std::transform(m_calculated.begin(), m_calculated.end(), to, to,
std::plus<double>());
}
/** Multiply calculated values by values in a buffer and save result to the
* buffer
* @param to :: Pointer to the buffer, it must be large enough
*/
void FunctionValues::multiply(double *to) const {
std::transform(m_calculated.begin(), m_calculated.end(), to, to,
std::multiplies<double>());
}
/**
* Add other calculated values to this.
* @param values :: An instance of FunctionValues to be added to this. Must have
* the same size to this.
* @return A reference to this values.
*/
FunctionValues &FunctionValues::operator+=(const FunctionValues &values) {
if (size() != values.size()) {
throw std::runtime_error("Cannot add values: sizes do not match");
}
values.add(getPointerToCalculated(0));
return *this;
}
/**
* Multiply by other calculated values.
* @param values :: An instance of FunctionValues to multiply to this. Must have
* the same size to this.
* @return A reference to this values.
*/
FunctionValues &FunctionValues::operator*=(const FunctionValues &values) {
if (size() != values.size()) {
throw std::runtime_error("Cannot multiply values: sizes do not match");
}
values.multiply(getPointerToCalculated(0));
return *this;
}
/**
* Add other calculated values to these values starting from some index.
* @param start :: A starting index for addition
* @param values :: An instance of FunctionValues to be added to this. The size
* mustn't exceed the number of
* values in this from start to the end.
* @return A reference to this values.
*/
void FunctionValues::addToCalculated(size_t start,
const FunctionValues &values) {
if (start + size() < values.size()) {
throw std::runtime_error("Cannot add values: sizes do not match");
}
values.add(getPointerToCalculated(start));
}
/**
* Set a fitting data value.
* @param i :: A value index
* @param value :: A new value to set.
*/
void FunctionValues::setFitData(size_t i, double value) {
if (m_data.size() != m_calculated.size()) {
m_data.resize(m_calculated.size());
}
m_data[i] = value;
}
/**
* Set all fitting data values.
* @param values :: A vector of values for fitting. Must have the same size as
* this.
*/
void FunctionValues::setFitData(const std::vector<double> &values) {
if (values.size() != this->size()) {
throw std::invalid_argument("Setting data of a wrong size");
}
m_data.assign(values.begin(), values.end());
}
/**
* Get a fitting data value
* @param i :: A value index
*/
double FunctionValues::getFitData(size_t i) const {
if (m_data.size() != m_calculated.size()) {
throw std::runtime_error("Fitting data was not set");
}
return m_data[i];
}
/**
* Set a fitting weight
* @param i :: A value index
* @param value :: A new value for the weight.
*/
void FunctionValues::setFitWeight(size_t i, double value) {
if (m_weights.size() != m_calculated.size()) {
m_weights.resize(m_calculated.size());
}
m_weights[i] = value;
}
/**
* Set all fitting weights.
* @param values :: A vector of fitting weights. Must have the same size as
* this.
*/
void FunctionValues::setFitWeights(const std::vector<double> &values) {
if (values.size() != this->size()) {
throw std::invalid_argument("Setting data of a wrong size");
}
m_weights.assign(values.begin(), values.end());
}
/**
* Set all weights to the same value.
* @param value :: The value to set.
*/
void FunctionValues::setFitWeights(const double &value) {
m_weights.resize(m_calculated.size(), value);
}
/**
* Get a fitting weight.
* @param i :: A value index.
*/
double FunctionValues::getFitWeight(size_t i) const {
if (m_weights.size() != m_calculated.size()) {
throw std::runtime_error("Fitting weights was not set");
}
return m_weights[i];
}
/**
* Set fitting data copied from other FunctionValues' calculated values.
* @param values :: An instance of FunctionValues to copy the data from.
*/
void FunctionValues::setFitDataFromCalculated(const FunctionValues &values) {
m_data.assign(values.m_calculated.begin(), values.m_calculated.end());
}
} // namespace API
} // namespace Mantid