Mantid
Loading...
Searching...
No Matches
FunctionValues.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
11#include <algorithm>
12#include <functional>
13
14namespace Mantid::API {
15
19 if (n > 0) {
20 m_calculated.resize(n);
21 }
22}
23
30
33 if (domain.size() == 0) {
34 throw std::invalid_argument("FunctionValues cannot have zero size.");
35 }
36 m_calculated.resize(domain.size());
37}
38
44 if (n < size()) {
45 throw std::invalid_argument("Cannot make FunctionValues smaller");
46 }
47 m_calculated.resize(n);
48 if (!m_data.empty()) {
49 m_data.resize(n);
50 }
51 if (!m_weights.empty()) {
52 m_weights.resize(n);
53 }
54}
55
59void FunctionValues::setCalculated(double value) { std::fill(m_calculated.begin(), m_calculated.end(), value); }
60
66 if (i < size()) {
67 return &m_calculated[i];
68 }
69 throw std::out_of_range("FunctionValue index out of range.");
70}
71
74
79void FunctionValues::copyTo(double *to) const { std::copy(m_calculated.begin(), m_calculated.end(), to); }
80
84void FunctionValues::add(double *to) const {
85 std::transform(m_calculated.begin(), m_calculated.end(), to, to, std::plus<double>());
86}
87
92void FunctionValues::multiply(double *to) const {
93 std::transform(m_calculated.begin(), m_calculated.end(), to, to, std::multiplies<double>());
94}
95
103 if (size() != values.size()) {
104 throw std::runtime_error("Cannot add values: sizes do not match");
105 }
106 values.add(getPointerToCalculated(0));
107 return *this;
108}
109
117 if (size() != values.size()) {
118 throw std::runtime_error("Cannot multiply values: sizes do not match");
119 }
121 return *this;
122}
123
132void FunctionValues::addToCalculated(size_t start, const FunctionValues &values) {
133 if (start + size() < values.size()) {
134 throw std::runtime_error("Cannot add values: sizes do not match");
135 }
136 values.add(getPointerToCalculated(start));
137}
138
144void FunctionValues::setFitData(size_t i, double value) {
145 if (m_data.size() != m_calculated.size()) {
146 m_data.resize(m_calculated.size());
147 }
148 m_data[i] = value;
149}
150
156void FunctionValues::setFitData(const std::vector<double> &values) {
157 if (values.size() != this->size()) {
158 throw std::invalid_argument("Setting data of a wrong size");
159 }
160 m_data.assign(values.begin(), values.end());
161}
162
167double FunctionValues::getFitData(size_t i) const {
168 if (m_data.size() != m_calculated.size()) {
169 throw std::runtime_error("Fitting data was not set");
170 }
171 return m_data[i];
172}
173
179void FunctionValues::setFitWeight(size_t i, double value) {
180 if (m_weights.size() != m_calculated.size()) {
181 m_weights.resize(m_calculated.size());
182 }
183 m_weights[i] = value;
184}
185
191void FunctionValues::setFitWeights(const std::vector<double> &values) {
192 if (values.size() != this->size()) {
193 throw std::invalid_argument("Setting data of a wrong size");
194 }
195 m_weights.assign(values.begin(), values.end());
196}
197
202void FunctionValues::setFitWeights(const double &value) { m_weights.resize(m_calculated.size(), value); }
203
208double FunctionValues::getFitWeight(size_t i) const {
209 if (m_weights.size() != m_calculated.size()) {
210 throw std::runtime_error("Fitting weights was not set");
211 }
212 return m_weights[i];
213}
214
220 m_data.assign(values.m_calculated.begin(), values.m_calculated.end());
221}
222
223} // namespace Mantid::API
double value
The value of the point.
Definition: FitMW.cpp:51
Base class that represents the domain of a function.
virtual size_t size() const =0
Return the number of points in the domain.
A class to store values calculated by a function.
void addToCalculated(size_t i, double value)
Add a number to a calculated value.
std::vector< double > m_calculated
buffer for calculated values
void zeroCalculated()
Set all calculated values to zero.
FunctionValues & operator*=(const FunctionValues &values)
Multiply by other calculated values.
double * getPointerToCalculated(size_t i)
Get a pointer to calculated data at index i.
double getFitWeight(size_t i) const
Get a fitting weight.
void setFitWeight(size_t i, double value)
Set a fitting weight.
double getFitData(size_t i) const
Get a fitting data value.
std::vector< double > m_weights
buffer for fitting weights (reciprocal errors)
size_t size() const
Return the number of values.
void expand(size_t n)
Expand values to a new size, preserve stored values.
std::vector< double > m_data
buffer for fit data
FunctionValues(size_t n=0)
Constructor.
void copyTo(double *to) const
Copy calculated values to a buffer.
void setFitData(size_t i, double value)
Set a fitting data value.
void setCalculated(double value)
set all calculated values to same number
void add(double *to) const
Add calculated values to values in a buffer and save result to the buffer.
void setFitWeights(const std::vector< double > &values)
Set all fitting weights.
void reset(const FunctionDomain &domain)
Reset the values to match a new domain.
void multiply(double *to) const
Multiply calculated values by values in a buffer and save result to the buffer.
FunctionValues & operator+=(const FunctionValues &values)
Add other calculated values.
void setFitDataFromCalculated(const FunctionValues &values)
Set all calculated values by copying them from another FunctionValues instance.