Ask Your Question
0

Integral with fricas

asked 2022-12-04 18:37:27 +0200

draz gravatar image

updated 2022-12-04 18:46:40 +0200

FrédéricC gravatar image

In sagemath 9.7 the following code is working, however when I try it in sagecell I get an error.

def integrate_fricas(f):
    return fricas(integrate(f,x))
f = 1/(x*(x-1)^2)
pretty_print(integrate_fricas(f))
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2022-12-05 08:17:59 +0200

Emmanuel Charpentier gravatar image

updated 2022-12-06 09:41:15 +0200

Your code doesn't do what you probaby mean to do. Sequentially, your function :

  • computes integrate(f, x) (using Sage's default integrator), then

  • creates a Fricas object representing this integral, and returns it

Your code then tries to pretty_print this fricas object, which fails (the error message is pretty specific...).

A couple suggestions :

  • Integrating via Fricas is more easily done by integrate(f, x, algorithm="fricas"), which returns the Sage object representing the Fricas value returned by Fricas' integrator. (EDIT : typo fixed).

  • You can also do this manually with fricas.integrate(*map(fricas, (f, x))).sage(), which returns -((x - 1)*log(x - 1) - (x - 1)*log(x) + 1)/(x - 1).

Quite intentionally, I leave the necessary explanations of this syntax to your explorations of Python and Sage documentations...

  • The effect of pretty_print may be platform-dependent but also object-dependant...

EDIT : On Sagemath 9.8.beta4 running locally, I get :

sage: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:f = 1/(x*(x-1)^2)
:print("default          : ",f.integrate(x))
:print("maxima           : ",f.integrate(x, algorithm="maxima"))
:print("giac             : ",f.integrate(x, algorithm="giac"))
:print("sympy            : ",f.integrate(x, algorithm="sympy"))
:print("mathematica_free : ",f.integrate(x, algorithm="mathematica_free"))
:print("fricas           : ",f.integrate(x, algorithm="fricas"))
:--
default          :  -1/(x - 1) - log(x - 1) + log(x)
maxima           :  -1/(x - 1) - log(x - 1) + log(x)
giac             :  -1/(x - 1) + log(abs(-1/(x - 1) - 1))
sympy            :  -1/(x - 1) - log(x - 1) + log(x)
mathematica_free :  -1/(x - 1) - log(x - 1) + log(x)
fricas           :  -((x - 1)*log(x - 1) - (x - 1)*log(x) + 1)/(x - 1)

On SageCell on 2022-12-06, reults are similar for default, maxima, giac and sympy integrators ; fricas fails and mathematica_free "never" returns.

HTH,

edit flag offensive delete link more

Comments

The code you suggested:

f = 1/(x*(x-1)^2) 
integrate(x, f,algorithm="fricas")

does not work either on sagecell or my notebook. The code

f = 1/(x*(x-1)^2)
fricas.integrate(*map(fricas, (f, x))).sage()

does not work also in sagecell.

Finally, I 'm not sure about your first remark. If you provide

sage:f = 1/sqrt((x^2+1)*(x+1))
sage:integrate_fricas(f)

                        8   80 3 x + 1
2 weierstrassPInverse(- -,- --,-------)
                        3   27    3

instead from

sage:integrate(f,x)

you get

integrate(1/sqrt((x^2 + 1)*(x + 1)), x)

Your remark seems reasonable, but it seems that sagemath does something else.

draz gravatar imagedraz ( 2022-12-05 09:32:04 +0200 )edit
  • integrate(x, f,algorithm="fricas") is a typo for integrate(f, x,algorithm="fricas"). My abject apologies :-)...

  • fricas.integrate(*map(fricas, (f, x))).sage() WorksForMe ... on my local 9.8.beta4. Indeed, id does not (yet ?) works on Sagecell. I don't know why.

I do not get your third remark. Could you clarify ?

BTW :

sage: (1/sqrt((x^2+1)*(x+1))).integrate(x, algorithm="fricas")
2*weierstrassPInverse(-8/3, -80/27, x + 1/3)

Same result as you, but converted to Sage...

sage: mathematica.Integrate((1/sqrt((x^2+1)*(x+1))), x).sage()
(x + 1)*sqrt(-(I + 1)*x - I + 1)*sqrt((2*I - 2)*x + 2*I + 2)*elliptic_f(arcsin(1/2*sqrt(2)*sqrt((I + 1)*x + I + 1)), -I)/(sqrt(x^3 + x^2 + x + 1)*sqrt((I + 1)*x + I + 1))

Quite a different antiderivative. May or may not be OK.

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2022-12-05 16:52:25 +0200 )edit

I meant that the command

fricas(integrate(f,x))

and

f.integrate(x, algorithm="fricas")

provide the same results, and the command alone (ie. the built in sagemath without calling fricas)

integrate(f,x)

does not do anything (for f=1/sqrt((x^2+1)*(x+1)) ). So your first comment about "Your code doesn't do what you probably mean to do" is not true. Thank you for your answer.

draz gravatar imagedraz ( 2022-12-05 20:31:41 +0200 )edit

does not do anything (for f=1/sqrt((x^2+1)*(x+1)) )

When Sage returns the unevaluated integrate command, this means that the choosen integrator fails to find an antiderivative (or a defined integral).

More generally : Sage 9.8.beta4 (locally installed) gives more answers than Sagecell as of 20222-12-06 : see my second edit to my answer.

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2022-12-06 09:44:55 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2022-12-04 18:37:27 +0200

Seen: 179 times

Last updated: Dec 06 '22