[GiNaC-list] Polynomial Factoring

Vladimir V. Kisil V.Kisil at leeds.ac.uk
Fri May 5 09:32:36 CEST 2023


	Dear Charles,

	In ginsh you can get factors by op() function:

> rat = pow(x,6)+1;
1+x^6
> e = factor(rat);
-(1+x^2)*(-1+x^2-x^4)
> a = op(e, 0);
1+x^2
> a = op(e, 1);
-1+x^2-x^4

  More automated C++ code to get factors (note the scalar as well) is:

#include <fstream>
#include <ginac/ginac.h>                                                       

using namespace GiNaC;                                                         
using namespace std;

int main () {

	symbol x("x");
	ex rat = 2*(pow(x,6)+1);
	ex fac = factor(rat);
	for (int i =0; i < fac.nops(); ++i)
		cout << fac.op(i) << endl;

	return 0;
}

  The output is:
-1+x^2-x^4
1+x^2
-2

  Alternatively you may use pyGiNaC (https://moebinv.sourceforge.net/pyGiNaC.html):

from ginac import *

x = symbol("x")
rat = 2*(pow(x,6)+1)
fac = factor(rat)
for i  in range(fac.nops()):
    print(fac.op(i))

  Best wishes,
  Vladimir
-- 
Vladimir V. Kisil                 http://www1.maths.leeds.ac.uk/~kisilv/
  Book:      Geometry of Mobius Maps       https://doi.org/10.1142/p835
  Soft:      Geometry of cycles         http://moebinv.sourceforge.net/
  Jupyter notebooks:        https://github.com/vvkisil?tab=repositories
>>>>> On Thu, 4 May 2023 19:09:42 -1000, Charles White <whitece6 at hawaii.edu> said:

    CW> Hi GiNaC!  I love the library! Keep up the good work!

    CW> Currently I am trying to find exact eigenvalues. My solution so
    CW> far is to factor the characteristic equation then I can try to
    CW> find roots of the factored polynomials. But, I need a list of
    CW> expressions that store the factored polynomials.

    CW> For example in the ginsh I have: ```
    >> rat = pow(x,6)+1;
    CW> 1+x^6
    >> factor(rat);
    CW> (1+x^4-x^2)*(1+x^2) ``` Then I need a list with the expressions
    CW> (1+x^4-x^2) and (1+x^2); from there I can calculate the roots
    CW> exactly.

    CW> Is this possible? Is there a better way to find exact
    CW> eigenvalues?

    CW> Thanks!  Charles


More information about the GiNaC-list mailing list