Polynomial parameters

Hi,
Please, how to use the ROOT::Math::Polynomial or another function to get the polynomial parameters: P(x) = p[0] + p[1]*x + p[2] *x 2 + … + p[n] x n . I have a polynomial of n degree and i want to extract the parameters p[0], p[1] , p[2] … I need a simple example, please.

Thank you in advance,

Hi,

The ROOT::Math::Polynomial class defines a function with n parameters representing a polynomial
p[0] + p[1]*x + p[2] *x 2 + … + p[n] *xn

n is passed when constructing the class. The parameter values can be set with Polynomial::SetParameters and retrieved with Polynomial::Parameters.

For example:

ROOT::Math::Polynomial p(4); 
double params[] = {1,2,3,4};
// set parameter values 
p.SetParameters(params); 
//evaluate polynomial for x = 2 
std::cout << p(2) << std::endl;
// retrieve parameter values 
for (int i = 0; i < 4 ; ++i) 
     std::cout << "param " << i << " = " << p.Parameters()[i] << std::endl;

Thank you very much for your answer, I made a mistake in my question. Indeed, after introducing the parameters (SetParameters(params), how to extract the solutions (x) with ROOT::Math::Polynomial of my P(n) and not the parameters.

Thank you in advance

For evaluating the function you can just call the operator() as shown in the example above.
You can also pass the parameter pointer to the operator() ,


double x = 2; 
double params = {1,2,3,4};
// evaluare polynomial for x  with stored parameter values 
std::cout << p(x) << std::endl;
// evaluare polynomial for x with given  parameter values 
double par = {4,3.2.1};
std::cout << p(x, par) << std::endl;

I know that my question is very stupid; in your example you fixed the x=2. but if I have this equation (where I know the value of n, and the parameters):
p[0] + p[1]*x + p[2] *x * 2 + … + p[n] x n = 10 for example
and I am looking for the solution x (unknown). so you know p[0] , p[1], p[2] … p[n] and you look for the solutions of your polynomial P(n). what is the solution, please?

Hi,
You can use the Polynomial::FindRoots() function, which returns a std::vectorstd::complex > since not all solutions are real

For example to find the roots for x^3 - 7x + 6 = 0, you do :

ROOT::Math::Polynomial p(3);
// the number of parameters is n (polynomial degree ) + 1 
double par[] = { 6,-7,0,1};  
p.SetParameters(par); 
auto r = p.FindRoots(); 
for (size_t i = 0; i < r.size(); ++i) { std::cout << r[i] << std::endl; } 

and the output will be :

(-3,0)
(1,0)
(2,0)

Cheers

Lorenzo

1 Like

thank you very much Lorenzo :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.