x018.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <array>
4 #include <chrono>
5 #include <cmath>
6 #include <iomanip>
7 #include <iostream>
8 
9 namespace nmx::apps::x018 {
10 
15 template<size_t N = 10>
16 class Factorial
17 {
18 private:
19  //reserviere Speicher für n!
20  inline static std::array<double, N> _data;
21  // Daten berechnet?
22  inline static bool _init = false;
23 
24 public:
25  //berechne und speichere n! (Hilfsfunktion)
26  inline static void init() {
27  if (!_init) {
28  _data[0] = 1;
29  for (size_t idx = 1; idx < N; idx++) {
30  _data[idx] = idx * _data[idx - 1];
31  }
32  _init = true;
33  }
34  }
35 
36  //Es dürfen keine Instanzen dieser Klasse erzeugt werden
37  Factorial() = delete;
38  Factorial(const Factorial &) = delete;
39  Factorial &operator=(const Factorial &) = delete;
40 
41  //berechne n!
42  inline static double get(size_t n) { //
43  return n < N ? _data[n] : (n * get(n - 1));
44  }
45 };
46 
52 template<class T = double, size_t N = 15>
54 {
55 public:
57 
58 protected:
59  size_t _n;
60  //Variable speichert die Potenzen von x
61  mutable double xval = 1;
62 
63 protected:
68  inline PowerSeries(size_t n)
69  : _n{ n } {
71  }
72 
79  inline double term(double x, size_t n) const { //
80  return static_cast<const T *>(this)->term(x, n);
81  }
82 
88  inline double sum(double x) const {
89  xval = 1;
90  double val = 0;
91  for (size_t idx = 0; idx < _n + 1; idx++) {
92  val += term(x, idx);
93  }
94  return val;
95  }
96 
102  inline int sign(size_t n) const { return n % 2 == 0 ? 1 : -1; }
103 
104 public:
110  inline double operator()(double x) const { return sum(x); }
111 }; //PowerSeries
112 
116 class Exp : public PowerSeries<Exp>
117 {
118  friend PowerSeries; //Zugriff auf private Elemente
119 
120 protected:
127  inline double term(double x, size_t n) const { //
128  xval *= n == 0 ? 1 : x;
129  return xval / Factorial::get(n);
130  }
131 
132 public:
137  Exp(size_t n)
138  : PowerSeries(n) {}
139 }; //Exp
140 
144 class Sin : public PowerSeries<Sin>
145 {
146  friend PowerSeries; //Zugriff auf private Elemente
147 
148 protected:
155  inline double term(double x, size_t n) const { //
156  const size_t npw = 2 * n + 1;
157  xval *= n == 0 ? x : (x * x);
158  return sign(n) * xval / Factorial::get(npw);
159  }
160 
161 public:
166  Sin(size_t n)
167  : PowerSeries(n) {}
168 }; //Sin
169 
173 class Cos : public PowerSeries<Cos>
174 {
175  friend PowerSeries; //Zugriff auf private Elemente
176 
177 protected:
184  inline double term(double x, size_t n) const { //
185  const size_t npw = 2 * n;
186  xval *= n == 0 ? 1 : (x * x);
187  return sign(n) * xval / Factorial::get(npw);
188  }
189 
190 public:
195  Cos(size_t n)
196  : PowerSeries(n) {}
197 }; //Cos
198 
202 inline void ex1() {
203  const Exp myexp(5);
204  const Sin mysin(5);
205  const Cos mycos(5);
206  //berechne Funktionswert an dieser Stelle
207  const double x0 = 0.2;
208  std::cout << myexp(x0) << std::setw(10) << std::exp(x0) << std::endl;
209  std::cout << mysin(x0) << std::setw(10) << std::sin(x0) << std::endl;
210  std::cout << mycos(x0) << std::setw(10) << std::cos(x0) << std::endl;
211 }
212 
216 inline void ex2() {
217  using namespace std::chrono;
218  constexpr size_t N = 3;
219  const Exp myexp(N);
220  const Sin mysin(N);
221  const Cos mycos(N);
222  //berechne Funktionswert an dieser Stelle
223  const double x0 = 0.2;
224 
225  double y1 = 0.0, y2 = 0.0, y3 = 0.0, y4 = 0.0, y5 = 0.0, y6 = 0.0;
226 
227  auto start1 = steady_clock::now();
228  for (double idx = 0; idx < 100000; idx++) {
229  y1 = std::exp(x0);
230  y2 = std::sin(x0);
231  y3 = std::cos(x0);
232  }
233  auto end1 = steady_clock::now();
234  auto diff1 = duration<double, std::milli>(end1 - start1).count();
235 
236  auto start2 = steady_clock::now();
237  for (double idx = 0; idx < 100000; idx++) {
238  y4 = myexp(x0);
239  y5 = mysin(x0);
240  y6 = mycos(x0);
241  }
242  auto end2 = steady_clock::now();
243  auto diff2 = duration<double, std::milli>(end2 - start2).count();
244 
245  std::cout << std::setprecision(4) << std::scientific;
246 
247  std::cout << "dt1=" << diff1 << " ms" << std::endl;
248  std::cout << "dt2=" << diff2 << " ms" << std::endl;
249  std::cout << "dt1/dt2=" << diff1 / diff2 << std::endl;
250  std::cout << "---------------------------------" << std::endl;
251  const size_t w = 15;
252  std::cout << "exp(x)" << std::setw(w) << "sin(x)" << std::setw(w) << "cos(x)" << std::endl;
253  std::cout << y1 << std::setw(w) << y2 << std::setw(w) << y3 << std::endl;
254  std::cout << y4 << std::setw(w) << y5 << std::setw(w) << y6 << std::endl;
255 }
256 } // namespace nmx::apps::x018
The Cos class Die Funktion cos(x) als Taylor-Polynom.
Definition: x018.h:173
double term(double x, size_t n) const
term allgemeiner Term (Funktionsspezifisch)
Definition: x018.h:79
The Exp class Die Funktion e^x als Taylor-Polynom.
Definition: x018.h:116
The Factorial class Berechnung von Fakultäten.
Definition: x018.h:16
double term(double x, size_t n) const
term Term des Taylor-Polynoms
Definition: x018.h:127
The PowerSeries class Schnittstelle zur Entwicklung von Funktionen in Potenzreihen Parameter...
Definition: x018.h:53
Exp(size_t n)
Exp Konstruktor.
Definition: x018.h:137
Factorial< 15 > Factorial
Definition: x018.h:56
void ex2()
ex2
Definition: x018.h:216
double term(double x, size_t n) const
term Term des Taylor-Polynoms
Definition: x018.h:155
double term(double x, size_t n) const
term Term des Taylor-Polynoms
Definition: x018.h:184
Cos(size_t n)
Cos Konstruktor.
Definition: x018.h:195
double sum(double x) const
sum Partialsumme ...
Definition: x018.h:88
void ex1()
run_power_series Beispielanwendung
Definition: x018.h:202
PowerSeries(size_t n)
PowerSeries.
Definition: x018.h:68
Sin(size_t n)
Sin Konstruktor.
Definition: x018.h:166
static double get(size_t n)
Definition: x018.h:42
The Sin class Die Funktion sin(x) als Taylor-Polynom.
Definition: x018.h:144
int sign(size_t n) const
sign Berechne (-1)^n
Definition: x018.h:102
Factorial & operator=(const Factorial &)=delete
double operator()(double x) const
operator () berechne Funktionswert
Definition: x018.h:110
static void init()
Definition: x018.h:26