Mantid
Loading...
Searching...
No Matches
Minus.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 +
9
10using namespace Mantid::API;
11using namespace Mantid::Kernel;
12
13namespace Mantid::Algorithms {
14// Register the class into the algorithm factory
16
17const std::string Minus::alias() const { return "Subtract"; }
18
19void Minus::performBinaryOperation(const HistogramData::Histogram &lhs, const HistogramData::Histogram &rhs,
20 HistogramData::HistogramY &YOut, HistogramData::HistogramE &EOut) {
21 std::transform(lhs.y().begin(), lhs.y().end(), rhs.y().begin(), YOut.begin(), std::minus<>());
22 std::transform(lhs.e().begin(), lhs.e().end(), rhs.e().begin(), EOut.begin(), VectorHelper::SumGaussError<double>());
23}
24
25void Minus::performBinaryOperation(const HistogramData::Histogram &lhs, const double rhsY, const double rhsE,
26 HistogramData::HistogramY &YOut, HistogramData::HistogramE &EOut) {
27 using std::placeholders::_1;
28 std::transform(lhs.y().begin(), lhs.y().end(), YOut.begin(), [rhsY](double l) { return l - rhsY; });
29 // Only do E if non-zero, otherwise just copy
30 if (rhsE != 0) {
31 double rhsE2 = rhsE * rhsE;
32 std::transform(lhs.e().begin(), lhs.e().end(), EOut.begin(),
33 [rhsE2](double l) { return std::sqrt(l * l + rhsE2); });
34 } else
35 EOut = lhs.e();
36}
37
38// ===================================== EVENT LIST BINARY OPERATIONS
39// ==========================================
48 // Easy, no? :) - This appends the event lists, with the rhs being negatively
49 // weighted.
50 lhs -= rhs;
51}
52
62 const MantidVec &rhsE) {
63 (void)lhs; // Avoid compiler warnings
64 (void)rhsX;
65 (void)rhsY;
66 (void)rhsE;
67 throw Exception::NotImplementedError("Plus::performEventBinaryOperation() cannot subtract a histogram from an "
68 "event list in an EventWorkspace. Try switching to a Workspace2D before "
69 "using Minus.");
70}
71
80void Minus::performEventBinaryOperation(DataObjects::EventList &lhs, const double &rhsY, const double &rhsE) {
81 (void)lhs; // Avoid compiler warnings
82 (void)rhsY;
83 (void)rhsE;
84 throw Exception::NotImplementedError("Plus::performEventBinaryOperation() cannot subtract a number from an "
85 "event list in an EventWorkspace. Try switching to a Workspace2D before "
86 "using Minus.");
87}
88
89//---------------------------------------------------------------------------------------------
97 if (m_erhs && m_elhs) {
98 // Two EventWorkspaces! They can be concatenated.
99 // Output will be EW
101 // Histogram sizes need not match
102 m_matchXSize = false;
103 // Can't flip - this is non-commutative
104 m_flipSides = false;
105 // Special case for plus/minus: if there is only one bin on the RHS, use the
106 // 2D method (appending event lists)
107 // so that the single bin is not treated as a scalar
109 } else {
110 // either or both workspace are "other"
111 // Use the default behaviour
113 }
114}
115
116//---------------------------------------------------------------------------------------------
126 if (lhs->size() > 1 && rhs->size() > 1) {
127 if (lhs->YUnit() != rhs->YUnit()) {
128 g_log.error("The two workspaces are not compatible because they have "
129 "different units for the data (Y).");
130 return false;
131 }
132 if (lhs->isDistribution() != rhs->isDistribution()) {
133 g_log.error("The two workspaces are not compatible because one is "
134 "flagged as a distribution.");
135 return false;
136 }
137 }
138 return true;
139}
140
141//---------------------------------------------------------------------------------------------
151 // Are we allowing the subtraction of different # of spectra, using detector
152 // IDs to match up?
154 return true;
155 }
156
157 if (!checkUnitCompatibility(lhs, rhs))
158 return false;
159
160 // Keep checking more generally.
162}
163
164//--------------------------------------------------------------------------------------------
177 if (m_erhs && m_elhs) {
178
179 if (lhs->getNumberHistograms() == rhs->getNumberHistograms()) {
180 return "";
181 } else {
182 return "Number of histograms not identical.";
183 }
184 } else
186}
187} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
const std::vector< double > & rhs
Kernel::Logger & g_log
Definition: Algorithm.h:451
bool m_do2D_even_for_SingleColumn_on_rhs
Special case for plus/minus: if there is only one bin on the RHS, use the 2D method (appending event ...
bool m_keepEventWorkspace
Variable set to true if the operation allows the output to stay as an EventWorkspace.
virtual bool checkCompatibility(const API::MatrixWorkspace_const_sptr lhs, const API::MatrixWorkspace_const_sptr rhs) const
Checks the compatibility of the two workspaces.
virtual std::string checkSizeCompatibility(const API::MatrixWorkspace_const_sptr lhs, const API::MatrixWorkspace_const_sptr rhs) const
Checks the overall size compatibility of two workspaces.
bool m_AllowDifferentNumberSpectra
The property value.
bool m_matchXSize
matchXSize set to true if the X sizes of histograms must match.
bool m_flipSides
flipSides set to true if the rhs and lhs operands should be flipped - for commutative binary operatio...
DataObjects::EventWorkspace_const_sptr m_erhs
Right-hand side EventWorkspace.
DataObjects::EventWorkspace_const_sptr m_elhs
Left-hand side EventWorkspace.
virtual void checkRequirements()
Check what operation will be needed in order to apply the operation to these two types of workspaces.
Minus performs the difference of two input workspaces.
Definition: Minus.h:28
bool checkUnitCompatibility(const API::MatrixWorkspace_const_sptr &lhs, const API::MatrixWorkspace_const_sptr &rhs) const
Return true if the units and distribution-type of the workspaces make them compatible.
Definition: Minus.cpp:124
void performBinaryOperation(const HistogramData::Histogram &lhs, const HistogramData::Histogram &rhs, HistogramData::HistogramY &YOut, HistogramData::HistogramE &EOut) override
Carries out the binary operation on a single spectrum, with another spectrum as the right-hand operan...
Definition: Minus.cpp:19
void checkRequirements() override
Check what operation will be needed in order to apply the operation to these two types of workspaces.
Definition: Minus.cpp:96
std::string checkSizeCompatibility(const API::MatrixWorkspace_const_sptr lhs, const API::MatrixWorkspace_const_sptr rhs) const override
Performs a simple check to see if the sizes of two workspaces are compatible for a binary operation I...
Definition: Minus.cpp:175
bool checkCompatibility(const API::MatrixWorkspace_const_sptr lhs, const API::MatrixWorkspace_const_sptr rhs) const override
Check the given workspaces for unit, distribution and binary operation compatibility.
Definition: Minus.cpp:149
void performEventBinaryOperation(DataObjects::EventList &lhs, const DataObjects::EventList &rhs) override
Carries out the binary operation IN-PLACE on a single EventList, with another EventList as the right-...
Definition: Minus.cpp:47
A class for holding :
Definition: EventList.h:56
Marks code as not implemented yet.
Definition: Exception.h:138
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::vector< double > MantidVec
typedef for the data storage used in Mantid matrix workspaces
Definition: cow_ptr.h:172
STL namespace.
Functor used for computing the sum of the square values of a vector, using the accumulate algorithm.
Definition: VectorHelper.h:150