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>
80MANTID_KERNEL_DLL std::vector<NumT> splitStringIntoVector(std::string listString, const std::string &separators = ", ");
81
82MANTID_KERNEL_DLL int getBinIndex(const std::vector<double> &bins, const double value);
83
84// Do running average of input vector within specified range, considering
85// heterogeneous bin-boundaries
86// if such boundaries are provided
87MANTID_KERNEL_DLL void smoothInRange(const std::vector<double> &input, std::vector<double> &output, double avrgInterval,
88 std::vector<double> const *const binBndrs = nullptr, size_t startIndex = 0,
89 size_t endIndex = 0, std::vector<double> *const outBins = nullptr);
90
91//-------------------------------------------------------------------------------------
98template <typename T> T lengthVector(const std::vector<T> &x) {
99 T total = 0;
100 for (size_t i = 0; i < x.size(); i++)
101 total += x[i] * x[i];
102 // Length is sqrt
103 total = sqrt(total);
104 return total;
105}
106// Scalar product of two vectors
107template <typename T> T scalar_prod(const std::vector<T> &v1, const std::vector<T> &v2) {
108 if (v1.size() != v2.size())
109 throw std::invalid_argument(" scalar product is defined only for the "
110 "vectors of the equivalent length");
111 T total = 0;
112 for (size_t i = 0; i < v1.size(); i++)
113 total += v1[i] * v2[i];
114
115 return total;
116}
117// Scalar product of two different type vectors which allow static cast to
118// double
119template <typename T, typename U> double scalar_prod(const std::vector<T> &v1, const std::vector<U> &v2) {
120 if (v1.size() != v2.size())
121 throw std::invalid_argument(" scalar product is defined only for the "
122 "vectors of the equivalient length");
123 double total = 0;
124 for (size_t i = 0; i < v1.size(); i++)
125 total += double(v1[i]) * double(v2[i]);
126
127 return total;
128}
129
130//-------------------------------------------------------------------------------------
137template <typename T> std::vector<T> normalizeVector(const std::vector<T> &x) {
138 // Ignore 0-sized vectors
139 if (x.size() == 0)
140 return x;
141 std::vector<T> out(x.size(), 0);
142 // Length is sqrt
143 T length = lengthVector(x);
144 for (size_t i = 0; i < x.size(); i++)
145 out[i] = x[i] / length;
146 return out;
147}
148
151template <class T> struct SumGaussError {
152 SumGaussError() = default;
154 inline T operator()(const T &l, const T &r) const { return sqrt(l * l + r * r); }
155};
156
163template <class T> struct AddVariance {
164 AddVariance() = default;
167 T operator()(const T &r, const T &x) const { return sqrt(r * r + x); }
168};
169
171template <class T> struct SumSquares {
172 SumSquares() = default;
174 T operator()(const T &r, const T &x) const { return (r + x * x); }
175};
176
178template <class T> struct TimesSquares {
179 TimesSquares() = default;
181 T operator()(const T &l, const T &r) const { return (r * r * l * l); }
182};
183
185template <class T> struct Squares {
186 Squares() = default;
188 T operator()(const T &x) const { return (x * x); }
189};
190
192template <class T> struct Log {
193 Log() = default;
196 T operator()(const T &x) const {
197 if (x <= 0)
198 throw std::range_error("Attempt to take logarithm of zero or negative number.");
199 return std::log(x);
200 }
201};
202
203// Non-throwing version of the Log functor
204template <class T> struct LogNoThrow {
205 LogNoThrow() = default;
206 // Returns the logarithm of the argument
207 T operator()(const T &x) const { return std::log(x); }
208};
209
211template <class T> struct DividesNonNull {
212 DividesNonNull() = default;
214 T operator()(const T &l, const T &r) const {
215 if (std::fabs(r) < 1e-12)
216 return l;
217 return l / r;
218 }
219};
220
222template <class T> struct SimpleAverage {
223 SimpleAverage() = default;
225 T operator()(const T &x, const T &y) const { return static_cast<T>(0.5) * (x + y); }
226};
227
228} // namespace VectorHelper
229} // namespace Kernel
230} // 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.
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.
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...
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)
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.
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.