Mantid
Loading...
Searching...
No Matches
FunctionDomain1D.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
12namespace Mantid::API {
13
15FunctionDomain1D::FunctionDomain1D(const double *x, size_t n) : m_data(x), m_n(n), m_peakRadius(0) {}
16
18std::vector<double> FunctionDomain1D::toVector() const {
19 std::vector<double> res;
20 if (m_n > 0) {
21 res.assign(m_data, m_data + m_n);
22 }
23 return res;
24}
25
30void FunctionDomain1D::setPeakRadius(int radius) { m_peakRadius = radius; }
31
36
41FunctionDomain1DVector::FunctionDomain1DVector(const std::vector<double> &xvalues) : FunctionDomain1D(nullptr, 0) {
42 if (xvalues.empty()) {
43 throw std::invalid_argument("FunctionDomain1D cannot have zero size.");
44 }
45 m_X.assign(xvalues.begin(), xvalues.end());
46 resetData(&m_X[0], m_X.size());
47}
48
53FunctionDomain1DVector::FunctionDomain1DVector(std::span<double const> xvalues) : FunctionDomain1D(nullptr, 0) {
54 if (xvalues.empty()) {
55 throw std::invalid_argument("FunctionDomain1D cannot have zero size.");
56 }
57 m_X.assign(xvalues.begin(), xvalues.end());
58 resetData(&m_X[0], m_X.size());
59}
60
65FunctionDomain1DVector::FunctionDomain1DVector(std::vector<double> &&xvalues) : FunctionDomain1D(nullptr, 0) {
66 if (xvalues.empty()) {
67 throw std::invalid_argument("FunctionDomain1D cannot have zero size.");
68 }
69 m_X = std::move(xvalues);
70 /* clear the invalidated object */
71 xvalues.clear();
72 resetData(&m_X[0], m_X.size());
73}
74
80FunctionDomain1DVector::FunctionDomain1DVector(std::vector<double>::const_iterator from,
81 std::vector<double>::const_iterator to)
82 : FunctionDomain1D(nullptr, 0) {
83 if (from == to) {
84 throw std::invalid_argument("FunctionDomain1D cannot have zero size.");
85 }
86 m_X.assign(from, to);
87 resetData(&m_X[0], m_X.size());
88}
89
98FunctionDomain1DVector::FunctionDomain1DVector(const double startX, const double endX, const size_t n)
99 : FunctionDomain1D(nullptr, 0) {
100 if (n == 0) {
101 throw std::invalid_argument("FunctionDomain1D cannot have zero size.");
102 }
103 m_X.resize(n);
104 if (n == 1) {
105 m_X[0] = (startX + endX) / 2;
106 } else {
107 const double dx = (endX - startX) / double(n - 1);
108 for (size_t i = 0; i < n; ++i) {
109 m_X[i] = startX + dx * double(i);
110 }
111 }
112 resetData(&m_X[0], m_X.size());
113}
114
120 m_X.resize(1);
121 m_X[0] = x;
122 resetData(&m_X[0], m_X.size());
123}
124
132
138 if (right.m_X.empty()) {
139 throw std::invalid_argument("FunctionDomain1D cannot have zero size.");
140 }
141 m_X.assign(right.m_X.begin(), right.m_X.end());
142 resetData(&m_X[0], m_X.size());
143 return *this;
144}
145
151FunctionDomain1DSpectrum::FunctionDomain1DSpectrum(size_t wi, const std::vector<double> &xvalues)
152 : FunctionDomain1DVector(xvalues), m_workspaceIndex(wi) {}
153
160FunctionDomain1DSpectrum::FunctionDomain1DSpectrum(size_t wi, std::vector<double>::const_iterator from,
161 std::vector<double>::const_iterator to)
162 : FunctionDomain1DVector(from, to), m_workspaceIndex(wi) {}
163
167 : FunctionDomain1DHistogram(bins.begin(), bins.end()) {}
168
174FunctionDomain1DHistogram::FunctionDomain1DHistogram(std::vector<double>::const_iterator from,
175 std::vector<double>::const_iterator to)
176 : FunctionDomain1D(nullptr, 0), m_bins(from, to) {
177 if (m_bins.size() < 2) {
178 throw std::runtime_error("Cannot initialize FunctionDomain1DHistogram with "
179 "less than 2 bin boundaries.");
180 }
181 resetData(&m_bins[1], m_bins.size() - 1);
182}
183
185double FunctionDomain1DHistogram::leftBoundary() const { return m_bins.front(); }
186
187} // namespace Mantid::API
double right
const std::vector< Type > & m_data
Implements FunctionDomain1D as a set of bins for a histogram.
double leftBoundary() const
Get the leftmost boundary.
std::vector< double > m_bins
vector of bin boundaries
FunctionDomain1DHistogram(const std::vector< double > &bins)
Constructor.
FunctionDomain1DSpectrum(size_t wi, const std::vector< double > &xvalues)
Constructor.
Implements FunctionDomain1D with its own storage in form of a std::vector.
FunctionDomain1DVector(const double x)
Constructor.
FunctionDomain1DVector & operator=(const FunctionDomain1DVector &)
Copy assignment operator.
std::vector< double > m_X
vector of function arguments
Represent a domain for functions of one real argument.
int getPeakRadius() const
Get the peak radius.
void setPeakRadius(int radius)
Set a peak radius to pass to peak functions.
size_t m_n
size of the data
int m_peakRadius
A peak radius that IPeakFunctions should use.
const double * m_data
pointer to the start of the domain data
std::vector< double > toVector() const
Convert to a vector.
FunctionDomain1D(const FunctionDomain1D &right)=delete
copying is not allowed.
void resetData(const double *x, size_t n)
Reset the pointer and size of the domain.