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//----------------------------------------------------------------------
13
14#include <boost/format.hpp>
15#include <cmath>
16#include <utility>
17
18#include "MantidKernel/Logger.h"
19namespace {
20Mantid::Kernel::Logger g_log("NumericAxis");
21
22class EqualWithinTolerance {
23public:
24 explicit EqualWithinTolerance(double tolerance) : m_tolerance(tolerance){};
25 bool operator()(double a, double b) {
26 if (std::isnan(a) && std::isnan(b))
27 return true;
28 if (std::isinf(a) && std::isinf(b))
29 return true;
30 return std::abs(a - b) <= m_tolerance;
31 }
32
33private:
34 double m_tolerance;
35};
36} // namespace
37
38namespace Mantid::API {
39
40//------------------------------------------------------------------------------
41// public members
42//------------------------------------------------------------------------------
43
48size_t NumericAxis::indexOfValue(const double value) const {
50}
51
55NumericAxis::NumericAxis(const std::size_t &length) : Axis(), m_values(length) {}
56
61NumericAxis::NumericAxis(std::vector<double> centres) : Axis(), m_values(std::move(centres)) {}
62
68Axis *NumericAxis::clone(const MatrixWorkspace *const parentWorkspace) {
69 UNUSED_ARG(parentWorkspace)
70 return new NumericAxis(*this);
71}
72
73Axis *NumericAxis::clone(const std::size_t length, const MatrixWorkspace *const parentWorkspace) {
74 UNUSED_ARG(parentWorkspace)
75 auto newAxis = new NumericAxis(*this);
76 newAxis->m_values.clear();
77 newAxis->m_values.resize(length);
78 return newAxis;
79}
80
88double NumericAxis::operator()(const std::size_t &index, const std::size_t &verticalIndex) const {
89 UNUSED_ARG(verticalIndex)
90 if (index >= length()) {
91 throw Kernel::Exception::IndexError(index, length() - 1, "NumericAxis: Index out of range.");
92 }
93
94 return m_values[index];
95}
96
102void NumericAxis::setValue(const std::size_t &index, const double &value) {
103 if (index >= length()) {
104 throw Kernel::Exception::IndexError(index, length() - 1, "NumericAxis: Index out of range.");
105 }
106
108}
109
114bool NumericAxis::operator==(const Axis &axis2) const { return equalWithinTolerance(axis2, 1e-15); }
115
121bool NumericAxis::equalWithinTolerance(const Axis &axis2, const double tolerance) const {
122 if (length() != axis2.length()) {
123 return false;
124 }
125 const auto *spec2 = dynamic_cast<const NumericAxis *>(&axis2);
126 if (!spec2) {
127 return false;
128 }
129 // Check each value is within tolerance
130 EqualWithinTolerance comparison(tolerance);
131 return std::equal(m_values.begin(), m_values.end(), spec2->m_values.begin(), comparison);
132}
133
138std::string NumericAxis::label(const std::size_t &index) const { return formatLabel((*this)(index)); }
139
145std::string NumericAxis::formatLabel(const double value) const {
146 std::string numberLabel = boost::str(boost::format("%.13f") % value);
147
148 // Remove all zeros up to the decimal place or a non zero value
149 auto it = numberLabel.end() - 1;
150 for (; it != numberLabel.begin(); --it) {
151 if (*it == '0') {
152 it = numberLabel.erase(it);
153 } else if (*it == '.') {
154 it = numberLabel.erase(it);
155 break;
156 } else {
157 break;
158 }
159 }
160
161 return numberLabel;
162}
163
168std::vector<double> NumericAxis::createBinBoundaries() const {
169 std::vector<double> result;
170 result.reserve(m_values.size());
172 return result;
173}
174
179const std::vector<double> &NumericAxis::getValues() const { return m_values; }
180
181//------------------------------------------------------------------------------
182// Protected members
183//------------------------------------------------------------------------------
184
188NumericAxis::NumericAxis() = default;
189
190} // namespace Mantid::API
double value
The value of the point.
Definition: FitMW.cpp:51
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
double tolerance
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition: System.h:64
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.
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:61
NumericAxis()
Default constructor.
bool operator==(const Axis &) const override
Check if two axis defined as spectra or numeric axis are equivalent.
double operator()(const std::size_t &index, const std::size_t &verticalIndex=0) const override
Get a value at the specified index.
Definition: NumericAxis.cpp:88
Axis * clone(const MatrixWorkspace *const parentWorkspace) override
Virtual constructor.
Definition: NumericAxis.cpp:68
size_t indexOfValue(const double value) const override
Returns the index of the value wrt bin edge representation of the axis.
Definition: NumericAxis.cpp:48
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:52
Kernel::Logger g_log("ExperimentInfo")
static logger object
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.
STL namespace.