# [GiNaC-list] Polynomial companion matrix

James Jackson james.jackson at cern.ch
Wed Feb 18 16:26:14 CET 2009

```Actually, it was just wrong... Must not write code at 2AM... Try the
following!:

// 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(j == rank - 1)
{
mat(i, j) = -1 * poly.coeff(symbol, i) / majorCoeff;
}
}
}
return mat;
}
else
{
return GiNaC::matrix();
}
}

Regards,
James.

On 18 Feb 2009, at 14:59, Jens Vollinga wrote:

> Hi James,
>
> James Jackson schrieb:
>> 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?:
>
> sure, we can include such a function.
>
> I am just a bit confused about the conventions here:
>
>>    mat(i, j) = -1 * poly.coeff(symbol, rank - j - 1) / majorCoeff;
>
> Shouldn't coefficient c_j go in row j?
>
> Regards,
> Jens
>
> _______________________________________________
> GiNaC-list mailing list
> GiNaC-list at ginac.de
> https://www.cebix.net/mailman/listinfo/ginac-list

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cebix.net/pipermail/ginac-list/attachments/20090218/9a73caaf/attachment.htm>
```