-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ex13_PolynomialRoots.cpp
158 lines (131 loc) · 3.55 KB
/
Ex13_PolynomialRoots.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//Author: Ksymena Poradzisz
#include <iostream>
#include <cstdio>
#include <cmath>
#include <complex>
#include <iterator>
using namespace std;
complex<double> P1(size_t n, complex<double> a[],complex<double> z)
{
complex<double> p = (0,0);
for(int i =0; i<n+1; i++)
{
p = p+ a[i]*pow(z,i);
}
return p;
}
complex<double> deriv(size_t n, complex<double> a[], complex<double> z)
{
complex<double> temp = 0;
for (int i = 0; i<n+1; i++)
{
double dumb = i;
temp += a[i]*dumb*pow(z,i-1);
}
return temp;
}
complex<double> lag(size_t n,complex<double> poly[],complex<double> z)
{
//rzad wielomianu
double n_d = n;
complex<double> dpoly_coeff[n]= {0};
for (int j =1; j<n+1; j++)
{
double dumbo = j;
dpoly_coeff[j-1]=poly[j]*dumbo; // tablica ze wspolczynnikammi pierwszej pochodnej
}
while(true)
{
complex<double> war = sqrt( (n_d-1) * ( (n_d-1) * pow(deriv(n, poly, z),2) - n_d * P1(n,poly,z) * deriv(n-1, dpoly_coeff,z) ) );
complex<double> m1 = deriv(n, poly, z) + war;
complex<double> m2 = deriv(n, poly, z) - war;
complex<double> m = 0;
if(abs(m1)>abs(m2))
{
m = m1;
}
else
{
m = m2;
}
complex<double> z1 = z - ( n_d * P1(n, poly, z ) )/(m);
if(abs(z1-z) < pow(0.1,8))
{
return z;
break;
}
else
{
z = z1;
}
}
}
void give_me_all_ROOTS (size_t psize, complex<double> poly[]) // psize - rzad wielomianu
{
complex<double> b[psize]= {0,0};
if(psize == 0)
{
cout << "Brak pierwiastków" << endl;
}
else if(psize == 1)
{
cout << -poly[0]/poly[1] << endl;
}
complex<double> r[psize] = {0};
for(int i =0; i<psize+1; i++)
{
r[i] = poly[i];
}
complex<double> roots[psize+1] = {0};
int ex = 2;
int rsize = psize;
for(int krok = 0; krok <100; krok ++)
{
if (rsize == 2)
{
//delta
complex<double> delta = r[1]*r[1] - 4.0*r[0]*r[2];
complex<double> sqrtDelta = sqrt(delta);
roots[0] = (-r[1]+sqrtDelta)/(2.0*r[2]);
roots[1] = (-r[1]-sqrtDelta)/(2.0*r[2]);
break;
}
else
{
complex<double> z410 (1,0);
complex<double> guess = lag(rsize,r,z410);
complex<double> root = lag(psize,poly, guess);
roots[ex] = root;
ex++;
//dzielenie wielomianu
b[rsize-1] = r[rsize];
for(int i = 1; i<rsize; i++)
{
b[rsize-i-1] =r[rsize-i] + root *b[rsize-i]; // wyliczam wspolczynniki wielomianu o rzedzie n-1
}
for(int i = 0; i<rsize; i++)
{
r[i] = {0,0}; //zeruje poprzedni wielomian i wypelnie te tablice nowym wielomianem
}
for(int i = 0; i< rsize; i++)
{
r[i] = b[i];
}
rsize--;
}
}
for(int i = 0; i< psize; i++)
{
cout<< roots[i] << endl;
}
}
int main() {
complex<double> P1_a[8] = {16,-72,-28,558,-990,783,-486,243};
complex<double> P1_b[11] = {-4,-4, -12, -8, -11, -3, -1, 2, 3,1,1};
complex<double> P1_c[5] = {{1,0},{0,-1},{-1,0},{0,1},{1,0}};
give_me_all_ROOTS(7,P1_a);
cout << endl;
give_me_all_ROOTS(10,P1_b);
cout << endl;
give_me_all_ROOTS(4,P1_c);
}