Mantid
Loading...
Searching...
No Matches
NumericAxis.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//----------------------------------------------------------------------
14
15#include <boost/format.hpp>
16#include <cmath>
17#include <utility>
18
19#include "MantidKernel/Logger.h"
20namespace {
21Mantid::Kernel::Logger g_log("NumericAxis");
22
23class EqualWithinTolerance {
24public:
25 explicit EqualWithinTolerance(double tolerance) : m_tolerance(tolerance) {};
31 bool operator()(double a, double b) {
32 if (std::isnan(a) && std::isnan(b))
33 return true;
34 if (std::isinf(a) && std::isinf(b))
35 return true;
36 return Mantid::Kernel::withinAbsoluteDifference(a, b, m_tolerance);
37 }
38
39private:
40 double m_tolerance;
41};
42} // namespace
43
44namespace Mantid::API {
45
46//------------------------------------------------------------------------------
47// public members
48//------------------------------------------------------------------------------
49
57
61NumericAxis::NumericAxis(const std::size_t &length) : Axis(), m_values(length) {}
62
67NumericAxis::NumericAxis(std::vector<double> centres) : Axis(), m_values(std::move(centres)) {}
68
69NumericAxis::NumericAxis(std::span<double const> centres) : Axis(), m_values(centres.begin(), centres.end()) {}
70
76Axis *NumericAxis::clone(const MatrixWorkspace *const parentWorkspace) {
77 UNUSED_ARG(parentWorkspace)
78 return new NumericAxis(*this);
79}
80
81Axis *NumericAxis::clone(const std::size_t length, const MatrixWorkspace *const parentWorkspace) {
82 UNUSED_ARG(parentWorkspace)
83 auto newAxis = new NumericAxis(*this);
84 newAxis->m_values.clear();
85 newAxis->m_values.resize(length);
86 return newAxis;
87}
88
96double NumericAxis::operator()(const std::size_t &index, const std::size_t &verticalIndex) const {
97 UNUSED_ARG(verticalIndex)
98 if (index >= length()) {
99 throw Kernel::Exception::IndexError(index, length() - 1, "NumericAxis: Index out of range.");
100 }
101
102 return m_values[index];
103}
104
110void NumericAxis::setValue(const std::size_t &index, const double &value) {
111 if (index >= length()) {
112 throw Kernel::Exception::IndexError(index, length() - 1, "NumericAxis: Index out of range.");
113 }
114
116}
117
122bool NumericAxis::operator==(const NumericAxis &axis2) const { return equalWithinTolerance(axis2, 1e-15); }
123
128bool NumericAxis::operator==(const Axis &axis2) const { return equalWithinTolerance(axis2, 1e-15); }
129
135bool NumericAxis::equalWithinTolerance(const Axis &axis2, const double tolerance) const {
136 if (length() != axis2.length()) {
137 return false;
138 }
139 const auto *spec2 = dynamic_cast<const NumericAxis *>(&axis2);
140 if (!spec2) {
141 return false;
142 }
143 // Check each value is within tolerance
144 EqualWithinTolerance comparison(tolerance);
145 return std::equal(m_values.begin(), m_values.end(), spec2->m_values.begin(), comparison);
146}
147
152std::string NumericAxis::label(const std::size_t &index) const { return formatLabel((*this)(index)); }
153
159std::string NumericAxis::formatLabel(const double value) const {
160 std::string numberLabel = boost::str(boost::format("%.13f") % value);
161
162 // Remove all zeros up to the decimal place or a non zero value
163 auto it = numberLabel.end() - 1;
164 for (; it != numberLabel.begin(); --it) {
165 if (*it == '0') {
166 it = numberLabel.erase(it);
167 } else if (*it == '.') {
168 numberLabel.erase(it);
169 break;
170 } else {
171 break;
172 }
173 }
174
175 return numberLabel;
176}
177
182std::vector<double> NumericAxis::createBinBoundaries() const {
183 std::vector<double> result;
184 result.reserve(m_values.size());
186 return result;
187}
188
193const std::vector<double> &NumericAxis::getValues() const { return m_values; }
194
195//------------------------------------------------------------------------------
196// Protected members
197//------------------------------------------------------------------------------
198
202NumericAxis::NumericAxis() = default;
203
204} // namespace Mantid::API
double value
The value of the point.
Definition FitMW.cpp:51
std::map< DeltaEMode::Type, std::string > index
double tolerance
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition System.h:44
Class to represent the axis of a workspace.
Definition Axis.h:30
virtual std::size_t length() const =0
Get the length of the axis.
Base MatrixWorkspace Abstract Class.
Class to represent a numeric axis of a workspace.
Definition NumericAxis.h:30
virtual bool equalWithinTolerance(const Axis &axis2, const double tolerance) const
Check if two numeric axis are equivalent to a given tolerance.
void setValue(const std::size_t &index, const double &value) override
Set the value at a specific index.
bool operator==(const NumericAxis &) const
Check if two NumericAxis are equivalent.
std::string label(const std::size_t &index) const override
Returns a text label which shows the value corresponding to a bin index.
std::string formatLabel(const double value) const
Get number label.
virtual std::vector< double > createBinBoundaries() const
Create bin boundaries from the point values.
virtual const std::vector< double > & getValues() const
Return a const reference to the values.
std::size_t length() const override
Get the length of the axis.
Definition NumericAxis.h:42
std::vector< double > m_values
A vector holding the centre values.
Definition NumericAxis.h:66
NumericAxis()
Default constructor.
double operator()(const std::size_t &index, const std::size_t &verticalIndex=0) const override
Get a value at the specified index.
Axis * clone(const MatrixWorkspace *const parentWorkspace) override
Virtual constructor.
size_t indexOfValue(const double value) const override
Returns the index of the value wrt bin edge representation of the axis.
Exception for index errors.
Definition Exception.h:284
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition Logger.h:51
Kernel::Logger g_log("DetermineSpinStateOrder")
void MANTID_KERNEL_DLL convertToBinBoundary(std::span< double const > bin_centers, std::vector< double > &bin_edges)
Convert an array of bin centers to bin boundary values.
size_t MANTID_KERNEL_DLL indexOfValueFromCenters(std::span< double const > bin_centers, const double value)
Gets the bin of a value from a vector of bin centers and throws exception if out of range.
MANTID_KERNEL_DLL bool withinAbsoluteDifference(T const x, T const y, S const tolerance)
Test whether x, y are within absolute tolerance tol.
STL namespace.