Polynomial. I kinda have function which creates basic polynomial. But I need to form polynomial (𝑥 − 𝑎0 )(𝑥 − 𝑎1 ) … (𝑥 − 𝑎𝑛 )where a is double.What should I change?
If you want the reverse, then just multiply them (convolution).
If you want to work directly in that form
1 2 3 4 5 6 7 8 9 10
class poly_atom{ //x-a_0
//...
double coeff; //a_0
//note: no a_1
};
class polynomial{
std::vector<poly_atom> factors;
double gain; //this multiplies all the factors, it is missing in your formula
};
Sorry, I'm quite rusty on the nomenclature. The idea is to have 2 classes:
- ones that represent (x-a)
- the polynomial, that's a collection of (x-a) and also has a multiply factor.
Well this program makes basic polynomial. But I have to form polynomial according to this formula (𝑥 − 𝑎0 )(𝑥 − 𝑎1 ) … (𝑥 − 𝑎𝑛 ), a1, a2 are the different numbers and by multiplicating it should form polynomial.
(x + a)(x + b) = x^2 + (a + b)x + ab // not using the multiplication operator explicitly
(x + a)(x + b)(x + c) = x^3 + (a + b + c)x^2 + (ab + bc + ac) x + abc
(x + a)(x + b)(x + c)(x + d) = x^4 + (a + b + c + d) x^3 + (ab + bc + ca + ad + bd + cd) x^2
+ (abc + abd + bcd + cad) x + abcd
....
(x + a)(x + b)(x + c) ... (x + n) = x^n + (a + b + ... + n) x^(n-1) + (all combinations of 2
from the n coefficients, multiply elements of each combination, then sum) x^(n-2)
+ (all combinations of 3 from the n coefficients, multiply each combination, then sum) x^(n-3)
+ ... + (abcd...n)
From here on, if you or anyone else have any solutions then I'd be very interested in seeing that. I've tried a few things but not being able to quite pin it down. Most of the stuff out there works on the first n integers 1, 2, ... , 3 while your co-efficients vector could have integers in any order. I'm guessing we'll need to sort this vector first but what happens then?
There is nothing stopping you having a separate class with polynomial stored as the product form.
- Do you want to convert from the normal power series form to product form (i.e. find roots - problem is that some of these may be complex)?
- Do you want to convert from the product form to the power-series form? (Probably most easily done recursively, multiplying a bracket at a time).
- Or do you simply want to create a "product polynomial" class? In this case, you probably only need to change the evaluateAt( x ) and print() member functions of your existing class (and you will also have to be careful about how many coefficients you have: a[0] ... a[N] is N+1 coefficients).
I borrowed your code and tweaked it so that I have polynomial classes in both power series and product form. It is a bit different from yours in that my polynomials have N+1 coefficients, as I prefer xN to be the highest degree term. This code is at the bottom.
If you want to create a polynomial by multiplying brackets then I suggest that you do it recursively by multiplying one bracket at a time, for which you could write a member function of your original polynomial class; then: (x-c) * (Nth order polynomial) = (N+1)th-order polynomial
Say, (x-c) * (a0 + a1x + ... aNxN) = b0 + b1x + b2x2 + ... bN+1xN+1
where, by considering the ith term, bi=ai-1-cai (in a for() loop on i)
(I have not included this is not in my code here.)