x015.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cmath>
4 #include <functional>
5 #include <iomanip>
6 #include <iostream>
7 #include <numeric>
8 #include <sstream>
9 #include <string>
10 #include <vector>
11 #include <xmath.h>
12 
13 namespace nmx::apps::x015 {
14 
20 {
21 protected:
22  //Anzahl der Terme des Polynoms
23  size_t _n;
24 
25  //Funktion : berechnet einzelne Terme
26  std::function<double(size_t, double)> _fn;
27 
28  //Zwischenspeicher n!
29  inline static std::vector<double> cache;
30 
35  inline PowerSeries(size_t n)
36  : _n{ n } {}
37 
43  inline double sum(double x) const {
44  double val = 0;
45  for (size_t idx = 0; idx <= _n + 1; idx++) {
46  //wende das intern gespeicherte Funktionsobjekt an
47  val += _fn(idx, x);
48  }
49  return val;
50  }
51 
57  static inline double factorial(size_t n) {
58  //maximale Anzahl der gespeicherten Werte für n!
59  const size_t cachesize = 11;
60  if (cache.empty()) {
61  //n! wird berechnet und gespeichert
62  double val = 1;
63  cache.push_back(val);
64  for (size_t idx = 1; idx < cachesize; idx++) {
65  val *= idx;
66  cache.push_back(val);
67  }
68  }
69  if (n < cache.size()) {
70  return cache[n]; //lese gespeicherten Wert
71  }
72  //Stirling-Formel
73  return sqrt(2 * Math::PI * n) * std::pow(n / Math::E, n);
74  }
75 
81  static inline int sign(size_t n) { return n % 2 == 0 ? 1 : -1; }
82 
83 public:
89  inline double operator()(double x) const { return sum(x); }
90 }; //PowerSeries
91 
95 class Exp : public PowerSeries
96 {
97 private:
98  //speichere den zuletzt berechneten Term: x^n
99  double _powterm;
100 
101 public:
106  Exp(size_t n)
107  : PowerSeries(n) {
108  //lambda-Funktion ruft Elementfunktion der Klasse auf
109  _fn = [this](size_t np, double x) { return term(np, x); };
110  }
111 
118  double term(size_t n, double x) {
119  _powterm = n == 0 ? 1 : _powterm *= x;
120  return _powterm / factorial(n);
121  }
122 }; //Exp
123 
127 class Sin : public PowerSeries
128 {
129 private:
130  double _powterm;
131 
132 public:
137  Sin(size_t n)
138  : PowerSeries(n) {
139  _fn = [this](size_t np, double x) { return term(np, x); };
140  }
141 
148  double term(size_t n, double x) {
149  size_t npow = 2 * n + 1;
150  _powterm = n == 0 ? x : (_powterm *= x * x);
151  return sign(n) / factorial(npow) * _powterm;
152  }
153 }; //Sin
154 
158 class Cos : public PowerSeries
159 {
160 private:
161  double _powterm;
162 
163 public:
168  Cos(size_t n)
169  : PowerSeries(n) {
170  _fn = [this](size_t np, double x) { return term(np, x); };
171  }
172 
179  double term(size_t n, double x) {
180  size_t npow = 2 * n;
181  _powterm = n == 0 ? 1 : (_powterm *= x * x);
182  return sign(n) / factorial(npow) * _powterm;
183  }
184 }; //Cos
185 
190 inline void ex1() {
191  const size_t nmax = 10; //Anzahl der Terme
192 
193  const Exp myexp(nmax);
194  const Sin mysin(nmax);
195  const Cos mycos(nmax);
196 
197  double x0 = 0.25;
198 
199  //Berechnung und Ausgabe
200  std::cout << std::setprecision(7) << std::scientific;
201  std::cout << "N=" << nmax << std::endl;
202  std::cout << "x0=" << x0 << std::endl;
203  std::cout << myexp(x0) << "\t" << std::exp(x0) << std::endl;
204  std::cout << mysin(x0) << "\t" << std::sin(x0) << std::endl;
205  std::cout << mycos(x0) << "\t" << std::cos(x0) << std::endl;
206 }
207 
208 } // namespace nmx::apps::x015
void ex1()
run_power_series Berechnung von Näherungswerten für e^x, sin(x), cos(x) 10 Terme
Definition: x015.h:190
The Cos class Taylorpolynom für cos(x)
Definition: x015.h:158
The Exp class Taylorpolynom für e^x.
Definition: x015.h:95
Sin(size_t n)
Sin Konstruktor.
Definition: x015.h:137
Exp(size_t n)
Exp Konstruktor.
Definition: x015.h:106
static constexpr double E
Definition: xmath.h:13
double sum(double x) const
sum Summe aller Terme
Definition: x015.h:43
PowerSeries(size_t n)
PowerSeries Konstruktor.
Definition: x015.h:35
The PowerSeries class Basisklasse Berechnung von Taylorpolynomen von Funktionen.
Definition: x015.h:19
static int sign(size_t n)
sign
Definition: x015.h:81
double term(size_t n, double x)
term Term des Taylor-Polynoms
Definition: x015.h:118
std::function< double(size_t, double)> _fn
Definition: x015.h:26
Cos(size_t n)
Cos Konstruktor.
Definition: x015.h:168
static std::vector< double > cache
Definition: x015.h:29
double term(size_t n, double x)
term Term des Taylor-Polynoms
Definition: x015.h:148
double term(size_t n, double x)
term Term des Taylor-Polynoms
Definition: x015.h:179
static constexpr double PI
Definition: xmath.h:11
The Sin class Taylorpolynom für sin(x)
Definition: x015.h:127
double operator()(double x) const
operator ()
Definition: x015.h:89
static double factorial(size_t n)
factorial Fakultät
Definition: x015.h:57