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 <stdexcept>
17#include <vector>
18
19namespace Mantid {
20namespace Kernel {
21/*
22 A collection of functions for use with vectors
23
24 @author Laurent C Chapon, Rutherford Appleton Laboratory
25 @date 16/12/2008
26 */
27namespace VectorHelper {
28
29void MANTID_KERNEL_DLL validateRebinParameters(std::vector<double> const &, bool const = false);
30
31std::size_t MANTID_KERNEL_DLL estimateNumberOfBins(std::vector<double> const &params, double const power = -1);
32
33std::size_t MANTID_KERNEL_DLL createAxisFromRebinParams(
34 const std::vector<double> &params, std::vector<double> &xnew, const bool resize_xnew = true,
35 const bool full_bins_only = false, const double xMinHint = std::nan(""), const double xMaxHint = std::nan(""),
36 const bool useReverseLogarithmic = false, const double power = -1);
37
38void MANTID_KERNEL_DLL rebin(const std::vector<double> &xold, const std::vector<double> &yold,
39 const std::vector<double> &eold, const std::vector<double> &xnew,
40 std::vector<double> &ynew, std::vector<double> &enew, bool distribution,
41 bool addition = false);
42
43// New method to rebin Histogram data, should be faster than previous one
44void MANTID_KERNEL_DLL rebinHistogram(const std::vector<double> &xold, const std::vector<double> &yold,
45 const std::vector<double> &eold, const std::vector<double> &xnew,
46 std::vector<double> &ynew, std::vector<double> &enew, bool addition);
47
49void MANTID_KERNEL_DLL convertToBinCentre(const std::vector<double> &bin_edges, std::vector<double> &bin_centres);
50
52void MANTID_KERNEL_DLL convertToBinBoundary(const std::vector<double> &bin_centers, std::vector<double> &bin_edges);
53
55size_t MANTID_KERNEL_DLL indexOfValueFromCenters(const std::vector<double> &bin_centers, const double value);
56
58int MANTID_KERNEL_DLL indexOfValueFromCentersNoThrow(const std::vector<double> &bin_centers, const double value);
59
61size_t MANTID_KERNEL_DLL indexOfValueFromEdges(const std::vector<double> &bin_edges, const double value);
62
63bool MANTID_KERNEL_DLL isConstantValue(const std::vector<double> &arra);
64
74template <typename T> std::vector<T> flattenVector(const std::vector<std::vector<T>> &v) {
75 std::vector<T> flattened;
76
77 for (const auto &subVector : v) {
78 flattened.insert(flattened.end(), subVector.begin(), subVector.end());
79 }
80
81 return flattened;
82}
83
84template <typename NumT>
85MANTID_KERNEL_DLL std::vector<NumT> splitStringIntoVector(std::string listString, const std::string &separators = ", ");
86
87MANTID_KERNEL_DLL int getBinIndex(const std::vector<double> &bins, const double value);
88
89// Do running average of input vector within specified range, considering
90// heterogeneous bin-boundaries
91// if such boundaries are provided
92MANTID_KERNEL_DLL void smoothInRange(const std::vector<double> &input, std::vector<double> &output, double avrgInterval,
93 std::vector<double> const *const binBndrs = nullptr, size_t startIndex = 0,
94 size_t endIndex = 0, std::vector<double> *const outBins = nullptr);
95
96// Forward declare SumSquares to use it in lengthVector
97template <class T> struct SumSquares;
98
99//-------------------------------------------------------------------------------------
106template <typename T> T lengthVector(const std::vector<T> &x) {
107 T total = std::accumulate(x.cbegin(), x.cend(), static_cast<T>(0), SumSquares<T>());
108 // Length is sqrt
109 total = sqrt(total);
110 return total;
111}
112// Scalar product of two vectors
113template <typename T> T scalar_prod(const std::vector<T> &v1, const std::vector<T> &v2) {
114 if (v1.size() != v2.size())
115 throw std::invalid_argument(" scalar product is defined only for the "
116 "vectors of the equivalent length");
117 T total = 0;
118 for (size_t i = 0; i < v1.size(); i++)
119 total += v1[i] * v2[i];
120
121 return total;
122}
123// Scalar product of two different type vectors which allow static cast to
124// double
125template <typename T, typename U> double scalar_prod(const std::vector<T> &v1, const std::vector<U> &v2) {
126 if (v1.size() != v2.size())
127 throw std::invalid_argument(" scalar product is defined only for the "
128 "vectors of the equivalient length");
129 double total = 0;
130 for (size_t i = 0; i < v1.size(); i++)
131 total += double(v1[i]) * double(v2[i]);
132
133 return total;
134}
135
136//-------------------------------------------------------------------------------------
143template <typename T> std::vector<T> normalizeVector(const std::vector<T> &x) {
144 // Ignore 0-sized vectors
145 if (x.size() == 0)
146 return x;
147 std::vector<T> out(x.size(), 0);
148 // Length is sqrt
149 T length = lengthVector(x);
150 for (size_t i = 0; i < x.size(); i++)
151 out[i] = x[i] / length;
152 return out;
153}
154
157template <class T> struct SumGaussError {
158 SumGaussError() = default;
160 inline T operator()(const T &l, const T &r) const { return sqrt(l * l + r * r); }
161};
162
169template <class T> struct AddVariance {
170 AddVariance() = default;
173 T operator()(const T &r, const T &x) const { return sqrt(r * r + x); }
174};
175
177template <class T> struct SumSquares {
178 SumSquares() = default;
180 T operator()(const T &r, const T &x) const { return (r + x * x); }
181};
182
184template <class T> struct TimesSquares {
185 TimesSquares() = default;
187 T operator()(const T &l, const T &r) const { return (r * r * l * l); }
188};
189
191template <class T> struct Squares {
192 Squares() = default;
194 T operator()(const T &x) const { return (x * x); }
195};
196
198template <class T> struct Log {
199 Log() = default;
202 T operator()(const T &x) const {
203 if (x <= 0)
204 throw std::range_error("Attempt to take logarithm of zero or negative number.");
205 return std::log(x);
206 }
207};
208
209// Non-throwing version of the Log functor
210template <class T> struct LogNoThrow {
211 LogNoThrow() = default;
212 // Returns the logarithm of the argument
213 T operator()(const T &x) const { return std::log(x); }
214};
215
217template <class T> struct DividesNonNull {
218 DividesNonNull() = default;
220 T operator()(const T &l, const T &r) const {
221 if (std::fabs(r) < 1e-12)
222 return l;
223 return l / r;
224 }
225};
226
228template <class T> struct SimpleAverage {
229 SimpleAverage() = default;
231 T operator()(const T &x, const T &y) const { return static_cast<T>(0.5) * (x + y); }
232};
233
234} // namespace VectorHelper
235} // namespace Kernel
236} // 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.
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.
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)
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.
void MANTID_KERNEL_DLL validateRebinParameters(std::vector< double > const &, bool const =false)
Validate rebinning parameters, throwing an error if any assumptions are invalidated.
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.