Mantid
Loading...
Searching...
No Matches
VectorHelper.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2007 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#pragma once
8
9//----------------------------------------------------------------------
10// Includes
11//----------------------------------------------------------------------
12#include "MantidKernel/DllConfig.h"
13#include <cmath>
14#include <functional>
15#include <numeric>
16#include <span>
17#include <stdexcept>
18#include <vector>
19
20namespace Mantid {
21namespace Kernel {
22/*
23 A collection of functions for use with vectors
24
25 @author Laurent C Chapon, Rutherford Appleton Laboratory
26 @date 16/12/2008
27 */
28namespace VectorHelper {
29
30void MANTID_KERNEL_DLL validateRebinParameters(std::vector<double> const &, bool const = false);
31
32std::size_t MANTID_KERNEL_DLL estimateNumberOfBins(std::vector<double> const &params, double const power = -1);
33
34std::size_t MANTID_KERNEL_DLL createAxisFromRebinParams(
35 const std::vector<double> &params, std::vector<double> &xnew, const bool resize_xnew = true,
36 const bool full_bins_only = false, const double xMinHint = std::nan(""), const double xMaxHint = std::nan(""),
37 const bool useReverseLogarithmic = false, const double power = -1);
38
42void MANTID_KERNEL_DLL rebin(std::span<double const> xold, std::span<double const> yold, std::span<double const> eold,
43 std::span<double const> xnew, std::span<double> ynew, std::span<double> enew,
44 bool distribution, bool addition = false);
45
46// New method to rebin Histogram data, should be faster than previous one
47void MANTID_KERNEL_DLL rebinHistogram(std::span<double const> xold, std::span<double const> yold,
48 std::span<double const> eold, std::span<double const> xnew,
49 std::span<double> ynew, std::span<double> enew, bool addition);
50
52void MANTID_KERNEL_DLL convertToBinCentre(std::span<double const> bin_edges, std::vector<double> &bin_centres);
53
55void MANTID_KERNEL_DLL convertToBinBoundary(std::span<double const> bin_centers, std::vector<double> &bin_edges);
56
58size_t MANTID_KERNEL_DLL indexOfValueFromCenters(std::span<double const> bin_centers, const double value);
59
61int MANTID_KERNEL_DLL indexOfValueFromCentersNoThrow(std::span<double const> bin_centers, const double value);
62
64size_t MANTID_KERNEL_DLL indexOfValueFromEdges(std::span<double const> bin_edges, const double value);
65
66bool MANTID_KERNEL_DLL isConstantValue(std::span<double const> arra);
67
77template <typename T> std::vector<T> flattenVector(const std::vector<std::vector<T>> &v) {
78 std::vector<T> flattened;
79
80 for (const auto &subVector : v) {
81 flattened.insert(flattened.end(), subVector.begin(), subVector.end());
82 }
83
84 return flattened;
85}
86
87template <typename NumT>
88MANTID_KERNEL_DLL std::vector<NumT> splitStringIntoVector(std::string listString, const std::string &separators = ", ");
89
90MANTID_KERNEL_DLL int getBinIndex(std::span<double const> bins, const double value);
91
92// Do running average of input vector within specified range, considering
93// heterogeneous bin-boundaries
94// if such boundaries are provided. An empty binBndrs means no boundaries were provided.
95MANTID_KERNEL_DLL void smoothInRange(std::span<double const> input, std::vector<double> &output, double avrgInterval,
96 std::span<double const> binBndrs = {}, size_t startIndex = 0, size_t endIndex = 0,
97 std::vector<double> *const outBins = nullptr);
98
99// Forward declare SumSquares to use it in lengthVector
100template <class T> struct SumSquares;
101
102//-------------------------------------------------------------------------------------
109template <typename T> T lengthVector(const std::vector<T> &x) {
110 T total = std::accumulate(x.cbegin(), x.cend(), static_cast<T>(0), SumSquares<T>());
111 // Length is sqrt
112 total = sqrt(total);
113 return total;
114}
115// Scalar product of two vectors
116template <typename T> T scalar_prod(const std::vector<T> &v1, const std::vector<T> &v2) {
117 if (v1.size() != v2.size())
118 throw std::invalid_argument(" scalar product is defined only for the "
119 "vectors of the equivalent length");
120 T total = 0;
121 for (size_t i = 0; i < v1.size(); i++)
122 total += v1[i] * v2[i];
123
124 return total;
125}
126// Scalar product of two different type vectors which allow static cast to
127// double
128template <typename T, typename U> double scalar_prod(const std::vector<T> &v1, const std::vector<U> &v2) {
129 if (v1.size() != v2.size())
130 throw std::invalid_argument(" scalar product is defined only for the "
131 "vectors of the equivalient length");
132 double total = 0;
133 for (size_t i = 0; i < v1.size(); i++)
134 total += double(v1[i]) * double(v2[i]);
135
136 return total;
137}
138
139//-------------------------------------------------------------------------------------
146template <typename T> std::vector<T> normalizeVector(const std::vector<T> &x) {
147 // Ignore 0-sized vectors
148 if (x.size() == 0)
149 return x;
150 std::vector<T> out(x.size(), 0);
151 // Length is sqrt
152 T length = lengthVector(x);
153 for (size_t i = 0; i < x.size(); i++)
154 out[i] = x[i] / length;
155 return out;
156}
157
160template <class T> struct SumGaussError {
161 SumGaussError() = default;
163 inline T operator()(const T &l, const T &r) const { return sqrt(l * l + r * r); }
164};
165
172template <class T> struct AddVariance {
173 AddVariance() = default;
176 T operator()(const T &r, const T &x) const { return sqrt(r * r + x); }
177};
178
180template <class T> struct SumSquares {
181 SumSquares() = default;
183 T operator()(const T &r, const T &x) const { return (r + x * x); }
184};
185
187template <class T> struct TimesSquares {
188 TimesSquares() = default;
190 T operator()(const T &l, const T &r) const { return (r * r * l * l); }
191};
192
194template <class T> struct Squares {
195 Squares() = default;
197 T operator()(const T &x) const { return (x * x); }
198};
199
201template <class T> struct Log {
202 Log() = default;
205 T operator()(const T &x) const {
206 if (x <= 0)
207 throw std::range_error("Attempt to take logarithm of zero or negative number.");
208 return std::log(x);
209 }
210};
211
212// Non-throwing version of the Log functor
213template <class T> struct LogNoThrow {
214 LogNoThrow() = default;
215 // Returns the logarithm of the argument
216 T operator()(const T &x) const { return std::log(x); }
217};
218
220template <class T> struct DividesNonNull {
221 DividesNonNull() = default;
223 T operator()(const T &l, const T &r) const {
224 if (std::fabs(r) < 1e-12)
225 return l;
226 return l / r;
227 }
228};
229
231template <class T> struct SimpleAverage {
232 SimpleAverage() = default;
234 T operator()(const T &x, const T &y) const { return static_cast<T>(0.5) * (x + y); }
235};
236
237} // namespace VectorHelper
238} // namespace Kernel
239} // namespace Mantid
double value
The value of the point.
Definition FitMW.cpp:51
std::vector< T > flattenVector(const std::vector< std::vector< T > > &v)
A convenience function to "flatten" the given vector of vectors into a single vector.
MANTID_KERNEL_DLL void smoothInRange(std::span< double const > input, std::vector< double > &output, double avrgInterval, std::span< double const > binBndrs={}, size_t startIndex=0, size_t endIndex=0, std::vector< double > *const outBins=nullptr)
Basic running average of input vector within specified range, considering variable bin-boundaries if ...
std::vector< T > normalizeVector(const std::vector< T > &x)
Normalize a vector of any size to unity, using the sum of the squares of the components.
void MANTID_KERNEL_DLL rebin(std::span< double const > xold, std::span< double const > yold, std::span< double const > eold, std::span< double const > xnew, std::span< double > ynew, std::span< double > enew, bool distribution, bool addition=false)
The input and output ranges are taken as spans so that the size-checked histogram data types,...
std::size_t MANTID_KERNEL_DLL estimateNumberOfBins(std::vector< double > const &params, double const power=-1)
Returns a size_t with the estimated number of bins that would be needed for a rebinning operation.
T lengthVector(const std::vector< T > &x)
Return the length of the vector (in the physical sense), the sqrt of the sum of the squares of the co...
int MANTID_KERNEL_DLL indexOfValueFromCentersNoThrow(std::span< double const > bin_centers, const double value)
Gets the bin of a value from a vector of bin centers and returns -1 if out of range.
MANTID_KERNEL_DLL int getBinIndex(std::span< double const > bins, const double value)
Return the index into a vector of bin boundaries for a particular X value.
void MANTID_KERNEL_DLL rebinHistogram(std::span< double const > xold, std::span< double const > yold, std::span< double const > eold, std::span< double const > xnew, std::span< double > ynew, std::span< double > enew, bool addition)
Rebins histogram data according to a new output X array.
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.
T scalar_prod(const std::vector< T > &v1, const std::vector< T > &v2)
bool MANTID_KERNEL_DLL isConstantValue(std::span< double const > arra)
Assess if all the values in the vector are equal or if there are some different values.
void MANTID_KERNEL_DLL convertToBinCentre(std::span< double const > bin_edges, std::vector< double > &bin_centres)
Convert an array of bin boundaries to bin center values.
void MANTID_KERNEL_DLL validateRebinParameters(std::vector< double > const &, bool const =false)
Validate rebinning parameters, throwing an error if any assumptions are invalidated.
size_t MANTID_KERNEL_DLL indexOfValueFromEdges(std::span< double const > bin_edges, const double value)
Gets the bin of a value from a vector of bin edges.
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.
std::size_t MANTID_KERNEL_DLL createAxisFromRebinParams(const std::vector< double > &params, std::vector< double > &xnew, const bool resize_xnew=true, const bool full_bins_only=false, const double xMinHint=std::nan(""), const double xMaxHint=std::nan(""), const bool useReverseLogarithmic=false, const double power=-1)
Creates a new output X array given a 'standard' set of rebinning parameters.
Helper class which provides the Collimation Length for SANS instruments.
Functor to deal with the increase in the error when adding (or subtracting) a number of counts.
T operator()(const T &r, const T &x) const
adds the square of the left-hand argument to the right hand argument and takes the square root
Divide functor with result reset to 0 if the denominator is null.
T operator()(const T &l, const T &r) const
Returns l/r if r is non-zero, otherwise returns l.
T operator()(const T &x) const
Returns the logarithm of the argument.
A binary functor to compute the simple average of 2 numbers.
T operator()(const T &x, const T &y) const
Return the average of the two arguments.
T operator()(const T &x) const
Returns the square of the argument.
Functor used for computing the sum of the square values of a vector, using the accumulate algorithm.
T operator()(const T &l, const T &r) const
Sums the arguments in quadrature.
Functor to accumulate a sum of squares.
T operator()(const T &r, const T &x) const
Adds the square of the right-hand argument to the left hand one.
Functor giving the product of the squares of the arguments.
T operator()(const T &l, const T &r) const
Multiplies the squares of the arguments.