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.
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
Yes I suppose you're correct! I'm just using it to compute eigenvalues so using this transposed matrix works fine. The eigenvectors would of course be different. Just swapping is and js should put it into 'normal' format. 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@ginac.de https://www.cebix.net/mailman/listinfo/ginac-list
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@ginac.de https://www.cebix.net/mailman/listinfo/ginac-list
participants (2)
-
James Jackson
-
Jens Vollinga