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
74Axis *NumericAxis::clone(const MatrixWorkspace *const parentWorkspace) {
75 UNUSED_ARG(parentWorkspace)
76 return new NumericAxis(*this);
77}
78
79Axis *NumericAxis::clone(const std::size_t length, const MatrixWorkspace *const parentWorkspace) {
80 UNUSED_ARG(parentWorkspace)
81 auto newAxis = new NumericAxis(*this);
82 newAxis->m_values.clear();
83 newAxis->m_values.resize(length);
84 return newAxis;
85}
86
94double NumericAxis::operator()(const std::size_t &index, const std::size_t &verticalIndex) const {
95 UNUSED_ARG(verticalIndex)
96 if (index >= length()) {
97 throw Kernel::Exception::IndexError(index, length() - 1, "NumericAxis: Index out of range.");
98 }
99
100 return m_values[index];
101}
102
108void NumericAxis::setValue(const std::size_t &index, const double &value) {
109 if (index >= length()) {
110 throw Kernel::Exception::IndexError(index, length() - 1, "NumericAxis: Index out of range.");
111 }
112
114}
115
120bool NumericAxis::operator==(const NumericAxis &axis2) const { return equalWithinTolerance(axis2, 1e-15); }
121
126bool NumericAxis::operator==(const Axis &axis2) const { return equalWithinTolerance(axis2, 1e-15); }
127
133bool NumericAxis::equalWithinTolerance(const Axis &axis2, const double tolerance) const {
134 if (length() != axis2.length()) {
135 return false;
136 }
137 const auto *spec2 = dynamic_cast<const NumericAxis *>(&axis2);
138 if (!spec2) {
139 return false;
140 }
141 // Check each value is within tolerance
142 EqualWithinTolerance comparison(tolerance);
143 return std::equal(m_values.begin(), m_values.end(), spec2->m_values.begin(), comparison);
144}
145
150std::string NumericAxis::label(const std::size_t &index) const { return formatLabel((*this)(index)); }
151
157std::string NumericAxis::formatLabel(const double value) const {
158 std::string numberLabel = boost::str(boost::format("%.13f") % value);
159
160 // Remove all zeros up to the decimal place or a non zero value
161 auto it = numberLabel.end() - 1;
162 for (; it != numberLabel.begin(); --it) {
163 if (*it == '0') {
164 it = numberLabel.erase(it);
165 } else if (*it == '.') {
166 numberLabel.erase(it);
167 break;
168 } else {
169 break;
170 }
171 }
172
173 return numberLabel;
174}
175
180std::vector<double> NumericAxis::createBinBoundaries() const {
181 std::vector<double> result;
182 result.reserve(m_values.size());
184 return result;
185}
186
191const std::vector<double> &NumericAxis::getValues() const { return m_values; }
192
193//------------------------------------------------------------------------------
194// Protected members
195//------------------------------------------------------------------------------
196
200NumericAxis::NumericAxis() = default;
201
202} // 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:48
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:29
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:38
std::vector< double > m_values
A vector holding the centre values.
Definition NumericAxis.h:62
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")
size_t MANTID_KERNEL_DLL indexOfValueFromCenters(const std::vector< double > &bin_centers, const double value)
Gets the bin of a value from a vector of bin centers and throws exception if out of range.
void MANTID_KERNEL_DLL convertToBinBoundary(const std::vector< double > &bin_centers, std::vector< double > &bin_edges)
Convert an array of bin centers to bin boundary values.
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.