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 <stdexcept>
16#include <vector>
17
18namespace Mantid {
19namespace Kernel {
20/*
21 A collection of functions for use with vectors
22
23 @author Laurent C Chapon, Rutherford Appleton Laboratory
24 @date 16/12/2008
25 */
26namespace VectorHelper {
27int MANTID_KERNEL_DLL createAxisFromRebinParams(const std::vector<double> &params, std::vector<double> &xnew,
28 const bool resize_xnew = true, const bool full_bins_only = false,
29 const double xMinHint = std::nan(""),
30 const double xMaxHint = std::nan(""),
31 const bool useReverseLogarithmic = false, const double power = -1);
32
33void MANTID_KERNEL_DLL rebin(const std::vector<double> &xold, const std::vector<double> &yold,
34 const std::vector<double> &eold, const std::vector<double> &xnew,
35 std::vector<double> &ynew, std::vector<double> &enew, bool distribution,
36 bool addition = false);
37
38// New method to rebin Histogram data, should be faster than previous one
39void MANTID_KERNEL_DLL rebinHistogram(const std::vector<double> &xold, const std::vector<double> &yold,
40 const std::vector<double> &eold, const std::vector<double> &xnew,
41 std::vector<double> &ynew, std::vector<double> &enew, bool addition);
42
44void MANTID_KERNEL_DLL convertToBinCentre(const std::vector<double> &bin_edges, std::vector<double> &bin_centres);
45
47void MANTID_KERNEL_DLL convertToBinBoundary(const std::vector<double> &bin_centers, std::vector<double> &bin_edges);
48
50size_t MANTID_KERNEL_DLL indexOfValueFromCenters(const std::vector<double> &bin_centers, const double value);
51
53int MANTID_KERNEL_DLL indexOfValueFromCentersNoThrow(const std::vector<double> &bin_centers, const double value);
54
56size_t MANTID_KERNEL_DLL indexOfValueFromEdges(const std::vector<double> &bin_edges, const double value);
57
58bool MANTID_KERNEL_DLL isConstantValue(const std::vector<double> &arra);
59
69template <typename T> std::vector<T> flattenVector(const std::vector<std::vector<T>> &v) {
70 std::vector<T> flattened;
71
72 for (const auto &subVector : v) {
73 flattened.insert(flattened.end(), subVector.begin(), subVector.end());
74 }
75
76 return flattened;
77}
78
79template <typename NumT> MANTID_KERNEL_DLL std::vector<NumT> splitStringIntoVector(std::string listString);
80
81MANTID_KERNEL_DLL int getBinIndex(const std::vector<double> &bins, const double value);
82
83// Do running average of input vector within specified range, considering
84// heterogeneous bin-boundaries
85// if such boundaries are provided
86MANTID_KERNEL_DLL void smoothInRange(const std::vector<double> &input, std::vector<double> &output, double avrgInterval,
87 std::vector<double> const *const binBndrs = nullptr, size_t startIndex = 0,
88 size_t endIndex = 0, std::vector<double> *const outBins = nullptr);
89
90//-------------------------------------------------------------------------------------
97template <typename T> T lengthVector(const std::vector<T> &x) {
98 T total = 0;
99 for (size_t i = 0; i < x.size(); i++)
100 total += x[i] * x[i];
101 // Length is sqrt
102 total = sqrt(total);
103 return total;
104}
105// Scalar product of two vectors
106template <typename T> T scalar_prod(const std::vector<T> &v1, const std::vector<T> &v2) {
107 if (v1.size() != v2.size())
108 throw std::invalid_argument(" scalar product is defined only for the "
109 "vectors of the equivalent length");
110 T total = 0;
111 for (size_t i = 0; i < v1.size(); i++)
112 total += v1[i] * v2[i];
113
114 return total;
115}
116// Scalar product of two different type vectors which allow static cast to
117// double
118template <typename T, typename U> double scalar_prod(const std::vector<T> &v1, const std::vector<U> &v2) {
119 if (v1.size() != v2.size())
120 throw std::invalid_argument(" scalar product is defined only for the "
121 "vectors of the equivalient length");
122 double total = 0;
123 for (size_t i = 0; i < v1.size(); i++)
124 total += double(v1[i]) * double(v2[i]);
125
126 return total;
127}
128
129//-------------------------------------------------------------------------------------
136template <typename T> std::vector<T> normalizeVector(const std::vector<T> &x) {
137 // Ignore 0-sized vectors
138 if (x.size() == 0)
139 return x;
140 std::vector<T> out(x.size(), 0);
141 // Length is sqrt
142 T length = lengthVector(x);
143 for (size_t i = 0; i < x.size(); i++)
144 out[i] = x[i] / length;
145 return out;
146}
147
150template <class T> struct SumGaussError {
151 SumGaussError() = default;
153 inline T operator()(const T &l, const T &r) const { return sqrt(l * l + r * r); }
154};
155
162template <class T> struct AddVariance {
163 AddVariance() = default;
166 T operator()(const T &r, const T &x) const { return sqrt(r * r + x); }
167};
168
170template <class T> struct SumSquares {
171 SumSquares() = default;
173 T operator()(const T &r, const T &x) const { return (r + x * x); }
174};
175
177template <class T> struct TimesSquares {
178 TimesSquares() = default;
180 T operator()(const T &l, const T &r) const { return (r * r * l * l); }
181};
182
184template <class T> struct Squares {
185 Squares() = default;
187 T operator()(const T &x) const { return (x * x); }
188};
189
191template <class T> struct Log {
192 Log() = default;
195 T operator()(const T &x) const {
196 if (x <= 0)
197 throw std::range_error("Attempt to take logarithm of zero or negative number.");
198 return std::log(x);
199 }
200};
201
202// Non-throwing version of the Log functor
203template <class T> struct LogNoThrow {
204 LogNoThrow() = default;
205 // Returns the logarithm of the argument
206 T operator()(const T &x) const { return std::log(x); }
207};
208
210template <class T> struct DividesNonNull {
211 DividesNonNull() = default;
213 T operator()(const T &l, const T &r) const {
214 if (std::fabs(r) < 1e-12)
215 return l;
216 return l / r;
217 }
218};
219
221template <class T> struct SimpleAverage {
222 SimpleAverage() = default;
224 T operator()(const T &x, const T &y) const { return static_cast<T>(0.5) * (x + y); }
225};
226
227} // namespace VectorHelper
228} // namespace Kernel
229} // namespace Mantid
double value
The value of the point.
Definition: FitMW.cpp:51
MANTID_KERNEL_DLL void smoothInRange(const std::vector< double > &input, std::vector< double > &output, double avrgInterval, std::vector< double > const *const binBndrs=nullptr, 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 > flattenVector(const std::vector< std::vector< T > > &v)
A convenience function to "flatten" the given vector of vectors into a single vector.
Definition: VectorHelper.h:69
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 convertToBinCentre(const std::vector< double > &bin_edges, std::vector< double > &bin_centres)
Convert an array of bin boundaries to bin center values.
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.
Definition: VectorHelper.h:136
bool MANTID_KERNEL_DLL isConstantValue(const std::vector< double > &arra)
Assess if all the values in the vector are equal or if there are some different values.
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...
Definition: VectorHelper.h:97
MANTID_KERNEL_DLL int getBinIndex(const std::vector< double > &bins, const double value)
Return the index into a vector of bin boundaries for a particular X value.
size_t MANTID_KERNEL_DLL indexOfValueFromEdges(const std::vector< double > &bin_edges, const double value)
Gets the bin of a value from a vector of bin edges.
T scalar_prod(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelper.h:106
int 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.
void MANTID_KERNEL_DLL rebin(const std::vector< double > &xold, const std::vector< double > &yold, const std::vector< double > &eold, const std::vector< double > &xnew, std::vector< double > &ynew, std::vector< double > &enew, bool distribution, bool addition=false)
Rebins data according to a new output X array.
void MANTID_KERNEL_DLL rebinHistogram(const std::vector< double > &xold, const std::vector< double > &yold, const std::vector< double > &eold, const std::vector< double > &xnew, std::vector< double > &ynew, std::vector< double > &enew, bool addition)
Rebins histogram data according to a new output X array.
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.
int MANTID_KERNEL_DLL indexOfValueFromCentersNoThrow(const std::vector< double > &bin_centers, const double value)
Gets the bin of a value from a vector of bin centers and returns -1 if out of range.
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.
Definition: VectorHelper.h:162
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
Definition: VectorHelper.h:166
Divide functor with result reset to 0 if the denominator is null.
Definition: VectorHelper.h:210
T operator()(const T &l, const T &r) const
Returns l/r if r is non-zero, otherwise returns l.
Definition: VectorHelper.h:213
T operator()(const T &x) const
Returns the logarithm of the argument.
Definition: VectorHelper.h:195
A binary functor to compute the simple average of 2 numbers.
Definition: VectorHelper.h:221
T operator()(const T &x, const T &y) const
Return the average of the two arguments.
Definition: VectorHelper.h:224
T operator()(const T &x) const
Returns the square of the argument.
Definition: VectorHelper.h:187
Functor used for computing the sum of the square values of a vector, using the accumulate algorithm.
Definition: VectorHelper.h:150
T operator()(const T &l, const T &r) const
Sums the arguments in quadrature.
Definition: VectorHelper.h:153
Functor to accumulate a sum of squares.
Definition: VectorHelper.h:170
T operator()(const T &r, const T &x) const
Adds the square of the right-hand argument to the left hand one.
Definition: VectorHelper.h:173
Functor giving the product of the squares of the arguments.
Definition: VectorHelper.h:177
T operator()(const T &l, const T &r) const
Multiplies the squares of the arguments.
Definition: VectorHelper.h:180