commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Luc Maisonobe <Luc.Maison...@free.fr>
Subject [math] Re: Math Polynomial from specified roots
Date Fri, 03 Aug 2012 18:16:53 GMT
Le 03/08/2012 09:38, Gabriele Bulfon a écrit :
> Hi,

Hello,

Please note that I have changed the subject of your message to add the
[math] marker. This list is shared among several components and we ask
users to set such a marker to help filtering.

> while converting a Matlab source, I encountered the poly(r) function.
> The Matlab documentation, says :
> 
> "p = poly(r) where r is a vector returns a row vector whose elements are
> the coefficients of the polynomial whose roots are the elements of r."
> ( http://www.mathworks.it/help/techdoc/ref/poly.html )
> 
> I tried to find a similar function in Apache Math, but I could only find
> the opposite: build a polynomial
> from coefficients, and then find the roots.
> 
> How can I implement the Matlab poly functions using Apache Math?
> I need it only in the second form, passing a vector of roots, not a matrix.

You can try something along these lines (beware, I just write the code
in this mail, so it probably has lots of typos and would need some
adjustment to compile).

  PolynomialFunction buildFromRoots(double[] roots) {

     // start with constant polynomial
     PolynomialFunction p =
       new Polynomialfunction(new double[] { 1.0 });

     for (double r : roots) {
         double[] coeffs = new double[] { r, -1.0 };
         p = p.multiply(new Polynomialfunction(coeffs));
     }

     return p;

  }


You can retrieve the coefficients from the returned PolynomialFunction
using method getCoefficients().


best regards,
Luc

> 
> Thanx for any help,
> Gabriele.
> 
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message