Laguerre polynomials
Loading...
Searching...
No Matches
Laguerre.h
Go to the documentation of this file.
1// Александр, Дмитрий
2
3#ifndef LAGUERRE
4#define LAGUERRE
5
6#include <vector>
7#include <limits>
8#include <numbers>
9#include <complex>
10#include <iostream>
11#include <algorithm>
12
13#include "BaseSolver.h"
14#include "ExtendedFunctions.h"
15
16namespace Laguerre{
17using std::complex;
18using std::vector;
19
20template<typename T>
21class Original : public BaseSolver<T>{
22private:
23 //
24 static constexpr T eps = std::numeric_limits<T>::epsilon();
25
31 inline void laguer(const std::vector<std::complex<T>>& a, std::complex<T>& x, int& converged, int itmax){
32 converged = 1;
33 std::complex<T> dx, x1, b, d, f, g, h, sq, gp, gm, g2;
34 T err, abx, abp, abm;
35 int m = a.size() - 1;
36
37 for (int iter = 1; iter <= itmax; iter++) {
38 b = std::complex<T>(1.0, 0.0);
39 err = std::abs(b);
40 d = f = std::complex<T>(0.0, 0.0);
41 abx = std::abs(x);
42
43 for (int j = m - 1; j >= 0; j--) {
44 f = fma(x, f, d);
45 d = fma(x, d, b);
46 b = fma(x, b, a[j]);
47 err = fma(err, abx, std::abs(b));
48 }
49
50 if (std::abs(b) <= err * eps)
51 return; // We are on the root.
52
53 g = d / b;
54 g2 = g * g;
55 h = fma(f / b, -static_cast<T>(2), g2);
56 sq = std::sqrt(static_cast<T>(m - 1) * (fma(h, static_cast<T>(m), -g2)));
57 gp = g + sq;
58 gm = g - sq;
59 abp = std::abs(gp);
60 abm = std::abs(gm);
61
62 gp = abp < abm ? gm : gp;
63 dx = (std::max(abp, abm) > static_cast<T>(0.0)) ? (static_cast<T>(m) / gp) : std::polar(static_cast<T>(1) + abx, static_cast<T>(iter));
64 x1 = x - dx;
65 if (x == x1)
66 return; // Converged.
67 x = x1;
68 }
69 converged = -1;
70 // throw "Too many iterations!";
71 }
72
73
74public:
76 //
77 }
78
86 void operator()(std::vector<T>& poly, std::vector<std::complex<T>>& roots, std::vector<int>& conv, int itmax=80) override{
87 std::complex<T> x, _b, _c;
88 int m = poly.size() - 1;
89 // std::cout << "M SIZE " << m << "\n";
90 std::vector<std::complex<T>> ad(m + 1);
91
92 // Copy of coefficients for successive deflation.
93 for (int i = 0; i <= m; i++)
94 ad[i] = poly[i];
95
96 std::vector<std::complex<T>> ad_v;
97 for (int j = m - 1; j > 0; --j) {
98 // std::cout << "j:" << j << "\n";
99 x = std::complex<T>(0.0, 0.0);
100
101 // Start at zero to favor convergence to the smallest remaining root.
102 ad_v = std::vector<std::complex<T>>(ad.cbegin(), ad.cbegin() + j + 2);
103 laguer(ad_v, x, conv[j], itmax);
104
105 x.imag(std::abs(imag(x)) <= std::abs(real(x)) * eps ? static_cast<T>(0) : x.imag());
106 roots[j] = x;
107 _b = ad[j + 1];
108 for (int jj = j; jj >= 0; jj--) {
109 _c = ad[jj];
110 ad[jj] = _b;
111 _b = fma(x, _b, _c);
112 }
113 }
114 roots[0] = -ad[0];
115 conv[0] = conv[1];
116
117 // Polishing
118 if (anycomplex(roots)) {
119 std::cout << "has complex!\n";
120 return;
121 int i;
122 for (int j = 1; j < m; ++j) {
123 x = roots[j];
124 for (i = j - 1; i>=0; --i) {
125 if (real(roots[i]) <= real(x))
126 break;
127 roots[i + 1] = roots[i];
128 }
129 roots[i + 1] = x;
130 }
131 }
132 // std::cout << "SOLVED EVERYTHING\n";
133 }
134
135};
136
137};
138#endif
std::vector< double > err(double q, double x)
Definition Laguerre15m.h:96
p2 de j *z j
Definition Laguerre15m_or.h:23
cohn_schur ra local b
Definition Laguerre15m_or.h:3
Definition BaseSolver.h:7
Definition Laguerre.h:21
void operator()(std::vector< T > &poly, std::vector< std::complex< T > > &roots, std::vector< int > &conv, int itmax=80) override
Find the roots of a polynomial using Laguerre's method.
Definition Laguerre.h:86
static constexpr T eps
Definition Laguerre.h:24
void laguer(const std::vector< std::complex< T > > &a, std::complex< T > &x, int &converged, int itmax)
Laguerre's method to find a root of a polynomial.
Definition Laguerre.h:31
Original()
Definition Laguerre.h:75
Definition ExtendedFunctions.h:14
complex< number > fma(complex< number > a, complex< number > b, complex< number > c)
Fused multiply-add for complex numbers.
Definition ExtendedFunctions.h:82
bool anycomplex(T &&... t)
Check if at least one complex number has imaginary part.
Definition ExtendedFunctions.h:39