[GiNaC-list] Polynomial companion matrix

James Jackson james.jackson at cern.ch
Wed Feb 18 13:22:05 CET 2009


Hi,

As far as I can see, there is no method available to compute the  
companion matrix of a polynomial. May I suggest the addition of a  
method along these lines?:

// Creates the Companion matrix for a given polynomial
GiNaC::matrix CreateCompanion(const GiNaC::ex &poly, const GiNaC::ex  
&symbol)
{
    if(poly.is_polynomial(symbol))
    {
       int rank = poly.degree(symbol);
       GiNaC::matrix mat(rank, rank);
       GiNaC::ex majorCoeff = poly.coeff(symbol, rank);
       for(int i = 0; i < rank; ++i)
       {
          for(int j = 0; j < rank; ++j)
          {
             if(i - j == 1)
             {
                mat(i, j) = 1;
             }
             else if(i == 0)
             {
                mat(i, j) = -1 * poly.coeff(symbol, rank - j - 1) /  
majorCoeff;
             }
          }
       }
       return mat;
    }
    else
    {
       // Throw exception or return empty matrix
    }
}

Regards,
James.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cebix.net/pipermail/ginac-list/attachments/20090218/2fec00b9/attachment.htm>


More information about the GiNaC-list mailing list