## Giacpy and some symbolic+numerical computations

jocaps
Messages : 118
Inscription : lun. avr. 17, 2017 4:32 pm

### Giacpy and some symbolic+numerical computations

Hi,

I have made a series of long computations. One computation is faster but requires me to solve the numerical root of a polynomial and another computation is slower but more exact and the computations are mostly symbolic. To compare these computation I subtract the result to see if it becomes zero. Giacpy seems to have trouble with it (Xcas has only trouble with it if I use the "simplify" command, but writing the whole subtracted expression will yield 0). You can reproduce the problem with the following short code

Code : Tout sélectionner

``````from giacpy import simplify

giac("printpow(1)")  # trick to force the ^ printing with giac
print simplify("x0^6+2.0*x1^6+(4.0+3.5527136788e-015*i)*x2^6+(-1.0-1.42108547152e-014*i)*x0*x1^5+1.0*x0*x2^5+(-2.0-2.84217094304e-014*i)*x1*x2^5+(4.0-1.7763568394e-015*i)*x0^2*x2^4+(-4.0-7.1054273576e-015*i)*x0^3*x2^3+(1.0+1.42108547152e-014*i)*x0^4*x1^2+(1.0+1.42108547152e-014*i)*x0^4*x2^2-2.0*x0^5*x2+(2.0-2.84217094304e-014*i)*x1^4*x2^2-8.0*x0*x1^2*x2^3-1.0*x0*x1^3*x2^2+(-12.0-7.1054273576e-015*i)*x0*x1^4*x2+(3.0-4.97379915032e-014*i)*x0^2*x1*x2^3+(18.0+3.5527136788e-014*i)*x0^2*x1^2*x2^2+(5.0+3.5527136788e-014*i)*x0^2*x1^3*x2-5.0*x0^3*x1*x2^2-x0^6+2*x0^5*x2-x0^4*x2^2-x0^4*x1^2+4*x0^3*x2^3+5*x0^3*x2^2*x1-4*x0^2*x2^4-3*x0^2*x2^3*x1-18*x0^2*x2^2*x1^2-5*x0^2*x2*x1^3-x0*x2^5+8*x0*x2^3*x1^2+x0*x2^2*x1^3+12*x0*x2*x1^4+x0*x1^5-4*x2^6+2*x2^5*x1-2*x2^2*x1^4-2*x1^6")
``````
One easily checks that for instance the monomial term x0^6 should vanish (I also tried evalf or simply initiate the whole expression as a new giac pygen variable, but I get the same result). Trying simplify("x0^6-x0^6") will give me 0. But not with this long expression which has the monomials "x0^6" and "-x0^6".

In most situations I do only need symbolic computations but there are instances when I can only solve my problem numerically (through some roots of a high degree polynomial) and to assure myself that I have the correct result is important.

Edit: I just want to report that putting the expression in "expand" will work.

Jose

frederic han
Messages : 1113
Inscription : dim. mai 20, 2007 7:09 am
Localisation : Paris
Contact :

### Re: Giacpy and some symbolic+numerical computations

Expand is very slow.
I don't know what should simplify do because it is a very sophisticated instruction.

To compare polynomials or rational fractions without algebraic simplifications by just expanding and cancel there is:
ratnormal

also normal will try algrebraic simplifications but as you have non exact coefficients it won't happen:

Code : Tout sélectionner

``````In [17]: A=giac("x0^6+2.0*x1^6+(4.0+3.5527136788e-015*i)*x2^6+(-1.0-1.42108547152e-014*i)*x0*x1^5+1.0*x0*x2^5+(-2.0-2.84217094304e-014*i)*x1*x2^5+(4.0-1.7763568394e-015*i)*x0^2*x2^4+(-4.0-7.1054273576e-015*i)*x0^3*x2^3+(1.0+1.42108547152e-014*i)*x0^4*x1^2+(1.0+1.42108547152e-014*i)*x0^4*x2^2-2.0*x0^5*x2+(2.0-2.84217094304e-014*i)*x1^4*x2^2-8.0*x0*x1^2*x2^3-1.0*x0*x1^3*x2^2+(-12.0-7.1054273576e-015*i)*x0*x1^4*x2+(3.0-4.97379915032e-014*i)*x0^2*x1*x2^3+(18.0+3.5527136788e-014*i)*x0^2*x1^2*x2^2+(5.0+3.5527136788e-014*i)*x0^2*x1^3*x2-5.0*x0^3*x1*x2^2-x0^6+2*x0^5*x2-x0^4*x2^2-x0^4*x1^2+4*x0^3*x2^3+5*x0^3*x2^2*x1-4*x0^2*x2^4-3*x0^2*x2^3*x1-18*x0^2*x2^2*x1^2-5*x0^2*x2*x1^3-x0*x2^5+8*x0*x2^3*x1^2+x0*x2^2*x1^3+12*x0*x2*x1^4+x0*x1^5-4*x2^6+2*x2^5*x1-2*x2^2*x1^4-2*x1^6")

In [18]: A.regroup()
Out[18]: 0

In [19]: A.ratnormal()
Out[19]: 0

In [20]: A.normal()
Out[20]: 0
``````

frederic han
Messages : 1113
Inscription : dim. mai 20, 2007 7:09 am
Localisation : Paris
Contact :

### Re: Giacpy and some symbolic+numerical computations

NB: if the degree of your polynomial is not too high you can use:

Code : Tout sélectionner

``rootof``
to do symbolic computations with addig the root of a polynomial.

parisse
Messages : 5346
Inscription : mar. déc. 20, 2005 4:02 pm
Contact :

### Re: Giacpy and some symbolic+numerical computations

simplify does not do anything on expressions containing approx numbers (I think I did that because the risk would be too high to produce unexpected expressions for example while converting trig expressions to exponentials). As Frederic wrote, ratnormal does the job, regroup would also work here.

jocaps
Messages : 118
Inscription : lun. avr. 17, 2017 4:32 pm

### Re: Giacpy and some symbolic+numerical computations

Thanks to both of you. I did not know about regroup. I get the impression it is faster than ratnormal. I like it.

frederic han
Messages : 1113
Inscription : dim. mai 20, 2007 7:09 am
Localisation : Paris
Contact :

### Re: Giacpy and some symbolic+numerical computations

Yes but regroup doesn't expand things:

Code : Tout sélectionner

``````0>> regroup((x+1)^2-x^2-x-1-x)
-x^2+(x+1)^2-2*x-1
// Time 0.01
1>> ratnormal((x+1)^2-x^2-x-1-x)
0
``````