x013.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <array>
4 #include <cmath>
5 #include <complex>
6 #include <gsl/gsl_poly.h>
7 #include <iostream>
8 #include <vector>
9 
10 namespace nmx::apps::x013 {
11 
18 inline std::ostream &operator<<(std::ostream &os, const gsl_complex &c) {
19  return os << "(" << GSL_REAL(c) << "," << GSL_IMAG(c) << ")";
20 }
21 
27 template<class T, size_t N>
28 class Polynom
29 {
30 protected:
31  // speichere Koeffizienten, Anzahl Grad des Polynoms+1
32  std::array<T, N + 1> _c;
33 
34 public:
39  Polynom(std::array<T, N + 1> clst) {
40  std::copy(std::begin(clst), std::end(clst), std::begin(_c));
41  }
42 
48  T c(size_t idx) const {
49  //Zugriff mit Index-Überprüfung
50  return _c.at(idx);
51  }
52 
58  auto eval(gsl_complex z) const {
59  //komplexe Koeffizienten, komplexe Variable
60  if constexpr (std::is_same_v<T, gsl_complex>) {
61  return gsl_complex_poly_complex_eval( //
62  _c.data(),
63  N + 1,
64  z);
65  } else {
66  //reelle Koeffizienten, komplexe Variable
67  return gsl_poly_complex_eval(_c.data(), N, z);
68  }
69  }
70 
76  auto eval(double x) const {
77  //komplexe Koeffizienten, reelle Variable
78  if constexpr (std::is_same_v<T, gsl_complex>) {
79  return gsl_complex_poly_complex_eval( //
80  _c.data(),
81  N + 1,
82  gsl_complex{ x, 0 });
83  } else {
84  //reelle Koeffizienten, reelle Variable
85  return gsl_poly_eval(_c.data(), N + 1, x);
86  }
87  }
88 
94  template<class R>
95  auto operator()(R x) const {
96  return eval(x);
97  }
98 
106  friend inline std::ostream &operator<<(std::ostream &os, const Polynom &p) {
107  os << "{" << p.c(0);
108  for (size_t idx = 1; idx < p._c.size(); idx++) {
109  os << "," << p.c(idx);
110  }
111  return os << "}";
112  }
113 
118  inline constexpr size_t degree() const { return N; }
119 }; //Polynom
120 
124 inline void ex1() {
125  //Polynom 2 Grades mit reellen Koeffizienten
126  Polynom<double, 2> p1{ { 1., 2., 3. } };
127  std::cout << "p1=" << p1 << std::endl;
128  std::cout << "p1(-0.5)=" << p1(-0.5) << std::endl;
129  std::cout << "----------------" << std::endl;
130  //Polynom 3 Grades mit reellen Koeffizienten
131  Polynom<double, 3> p2{ { 1., -2., 3., 4. } };
132  std::cout << "p2=" << p2 << std::endl;
133  std::cout << "p2(-0.5)=" << p2(-0.5) << std::endl;
134  std::cout << "----------------" << std::endl;
135  //Polynom 1 Grades mit komplexen Koeffizienten
136  Polynom<gsl_complex, 1> p3{ { gsl_complex{ 1., 1. }, //
137  gsl_complex{ 2., 2. } } };
138 
139  //Ausgabe
140  std::cout << "p3=" << p3 << std::endl;
141  std::cout << "p3(1)=" << p3(1) << std::endl;
142  std::cout << "p3({1,2})=" << p3(gsl_complex{ 1., 2. }) << std::endl;
143 }
144 
145 } // namespace nmx::apps::x013
auto eval(gsl_complex z) const
eval Auswertung des Polynoms
Definition: x013.h:58
std::array< T, N+1 > _c
Definition: x013.h:32
std::ostream & operator<<(std::ostream &os, const gsl_complex &c)
operator <<
Definition: x013.h:18
auto operator()(R x) const
operator () Auswertung des Polynoms
Definition: x013.h:95
constexpr size_t degree() const
degree
Definition: x013.h:118
The Polynom class Darstellung von Polynomen.
Definition: x013.h:28
friend std::ostream & operator<<(std::ostream &os, const Polynom &p)
operator << Ausgabe der Koeffizienten des Polynoms {c0,c1,c2,...,cn}
Definition: x013.h:106
T c(size_t idx) const
c lese Koeffizienten
Definition: x013.h:48
Polynom(std::array< T, N+1 > clst)
Polynom Konstruktor.
Definition: x013.h:39
void ex1()
cppx4a Beispiel Auswertung von Polynomen
Definition: x013.h:124
auto eval(double x) const
eval Auswertung des Polynoms
Definition: x013.h:76