x016.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "x013.h"
4 
5 namespace nmx::apps::x016 {
6 
8 
13 class QPolynom : public Polynom<double, 2>
14 {
15  using BaseT = Polynom<double, 2>;
16 
17 public:
18  using BaseT::BaseT; // erbe Konstruktoren
19 
25  auto real_roots() const {
26  double x0, x1; //die Nullstellen
27  int countroots = gsl_poly_solve_quadratic(c(2), c(1), c(0), &x0, &x1);
28 
29  if (countroots == 0) { //keine reelle Nullstellen, leerer Vektor
30  return std::vector<double>{};
31  } else if (countroots == 1) { //eine reelle Nullstelle
32  return std::vector{ x0 };
33  } else { //zwei reelle Nullstelle
34  return std::vector{ x0, x1 };
35  }
36  }
37 
42  auto complex_roots() const {
43  using CVector = std::vector<std::complex<double>>;
44  gsl_complex z0, z1;
45  gsl_poly_complex_solve_quadratic(c(2), c(1), c(0), &z0, &z1);
46  return CVector{ { GSL_REAL(z0), GSL_IMAG(z0) }, //
47  { GSL_REAL(z1), GSL_IMAG(z1) } };
48  }
49 };
50 
55 inline void ex1() {
56  //lambda Funktion zur Ausgabe eines Polynoms und seiner Nullstellen
57  auto showResults = [](const QPolynom &p) {
58  std::cout << p << std::endl;
59  //suche reelle Nullstellen
60  auto roots = p.real_roots();
61  if (roots.empty()) { //keine reelle Nullstellen
62  std::cout << "no real roots" << std::endl;
63  auto croots = p.complex_roots();
64  for (const auto &z : croots) {
65  std::cout << z << std::endl;
66  }
67  } else {
68  std::cout << "real roots" << std::endl;
69  for (const auto &x : roots) {
70  std::cout << x << std::endl;
71  }
72  }
73  };
74 
75  //3*x^2+2*x+1
76  QPolynom p1{ { 1, 2, 3 } };
77  auto roots = p1.real_roots();
78  showResults(p1);
79  std::cout << "-------------------" << std::endl;
80  //x^2-3*x+1
81  QPolynom p2{ { 1, -3, 1 } };
82  showResults(p2);
83 }
84 
85 } // namespace nmx::apps::x016
void ex1()
cppx4 Beispiel Berechnung von Nullstellen eines quadratischen Polynoms
Definition: x016.h:55
The Polynom class Darstellung von Polynomen.
Definition: x013.h:28
T c(size_t idx) const
c lese Koeffizienten
Definition: x013.h:48
The QPolynom class Nullstellen eines quadratischen Polynoms mit reellen Koeffizienten.
Definition: x016.h:13
auto real_roots() const
real_roots berechne reelle Nullstellen
Definition: x016.h:25
auto complex_roots() const
complex_roots berechne komplexe Nullstellen
Definition: x016.h:42