commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Luc Maisonobe <>
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,


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."
> ( )
> 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,

> Thanx for any help,
> Gabriele.
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message