xerus
a general purpose tensor library
simpleNumerics.h
Go to the documentation of this file.
1 // Xerus - A General Purpose Tensor Library
2 // Copyright (C) 2014-2017 Benjamin Huber and Sebastian Wolf.
3 //
4 // Xerus is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published
6 // by the Free Software Foundation, either version 3 of the License,
7 // or (at your option) any later version.
8 //
9 // Xerus is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with Xerus. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // For further information on Xerus visit https://libXerus.org
18 // or contact us at contact@libXerus.org.
19 
25 #pragma once
26 
27 #include "standard.h"
28 
29 #include <vector>
30 #include <limits>
31 #include <functional>
32 
33 #include <boost/math/tools/polynomial.hpp>
34 
35 namespace xerus { namespace misc {
36 
38  double integrate(const std::function<double(double)>& _f, double _a, double _b, double _eps = std::numeric_limits<double>::epsilon(), uint _minIter = 4, uint _maxIter = 6, uint _branchFactor = 7, uint _maxRecursion = 10, bool _relativeError = true);
39 
40  double integrate_segmented(const std::function<double(double)> &_f, double _a, double _b, double _segmentation, double _eps = 1e-8, uint _minIter = 4, uint _maxIter = 6, uint _branchFactor = 8, uint _maxRecursion = 10);
41 
42  double find_root_bisection(const std::function<double(double)> &_f, double _min, double _max, double _epsilon = 1e-14);
43 
44  template<class T>
45  __attribute__((const)) T difference(T _a, T _b) {
46  if (_a > _b) {
47  return (_a-_b);
48  } else {
49  return (_b-_a);
50  }
51  }
52 
54  struct Polynomial {
55  std::vector<double> coefficients;
56 
57  Polynomial();
58 
59  Polynomial(std::vector<double> _coeff);
60 
61  size_t terms() const;
62 
63  Polynomial& operator+=(const Polynomial &_rhs);
64 
65  Polynomial& operator-=(const Polynomial &_rhs);
66 
67  Polynomial& operator*=(double _rhs);
68 
69  Polynomial& operator/=(double _rhs);
70 
71  Polynomial operator*(const Polynomial &_rhs) const;
72 
73  Polynomial operator*(double _rhs) const;
74 
75  double operator()(double x) const;
76 
77  double scalar_product(const Polynomial &_rhs, const std::function<double (double)> &_weight, double _minX, double _maxX) const;
78 
79  double norm(const std::function<double (double)> &_weight, double _minX, double _maxX) const;
80 
82  Polynomial &orthogonolize(const std::vector<Polynomial> &_orthoBase, const std::function<double (double)> &_weight, double _minX, double _maxX);
83 
85  static std::vector<Polynomial> build_orthogonal_base(size_t _N, const std::function<double (double)> &_weight, double _minX, double _maxX);
86  };
87 
88 
90  template<class ft_type>
92  public:
93  virtual void push_back(ft_type _val) = 0;
94  virtual ft_type best_estimate() const = 0;
95  virtual ft_type error_approximate() const = 0;
96  virtual void reset() = 0;
97 
98  virtual ~LimitExtractor() {};
99  };
100 
105  template<class ft_type>
106  class ShanksTransformation : public LimitExtractor<ft_type> {
107  public:
108  static constexpr ft_type epsilon = std::numeric_limits<ft_type>::epsilon();
109  public:
110  std::vector<ft_type> values;
111 
112  static ft_type shanks(ft_type x1, ft_type x2, ft_type x3);
113 
114  void push_back(ft_type _val) override;
115 
116  ft_type best_estimate() const override;
117 
118  ft_type error_approximate() const override;
119 
120  void reset() override;
121 
122  virtual ~ShanksTransformation() {};
123  };
124 
125 
126  //TODO the following is crap. implement Levin-t, Levin-u Levin-v
131  template<class ft_type>
132  class RichardsonExtrapolation : public LimitExtractor<ft_type> {
133  public:
134  static constexpr ft_type epsilon = std::numeric_limits<ft_type>::epsilon();
135  public:
136  std::vector<ft_type> values;
137 
138  static ft_type richard(size_t n, ft_type x1, ft_type x2);
139 
140  void push_back(ft_type _val) override;
141 
142  ft_type best_estimate() const override;
143 
144  ft_type error_approximate() const override;
145 
146  void reset() override;
147 
149  };
150 
151 } } // namespaces
double integrate_segmented(const std::function< double(double)> &_f, double _a, double _b, double _segmentation, double _eps=1e-8, uint _minIter=4, uint _maxIter=6, uint _branchFactor=8, uint _maxRecursion=10)
The main namespace of xerus.
Definition: basic.h:37
Tensor operator*(const value_t _factor, Tensor _tensor)
Calculates the entrywise multiplication of the Tensor _tensor with the constant _factor.
Definition: tensor.cpp:1233
unsigned int uint
Definition: standard.h:51
std::vector< ft_type > values
double find_root_bisection(const std::function< double(double)> &_f, double _min, double _max, double _epsilon=1e-14)
Limit extraction using the shanks transformation aka Aitken process.
__attribute__((const)) T difference(T _a
Limit extraction using the richardson extrapolation.
Classes that can extract an estimate of the limit of a sequence.
double integrate(const std::function< double(double)> &_f, double _a, double _b, double _eps=std::numeric_limits< double >::epsilon(), uint _minIter=4, uint _maxIter=6, uint _branchFactor=7, uint _maxRecursion=10, bool _relativeError=true)
Performs a Romberg Integration (richardson extrapolation of regular riemannian sum) + adaptive refine...
Header file for global shorthand notations of elementary integer types and attribute lists...