Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Implement algorithm="fricas" for integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilfried Huss authored and Frédéric Chapoton committed Dec 7, 2014
1 parent d22956c commit 2b719db
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/sage/symbolic/integration/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,47 @@ def mma_free_integrator(expression, v, a=None, b=None):
return ans
except TypeError:
raise ValueError("Unable to parse: %s" % mexpr)


def fricas_integrator(expression, v, a=None, b=None):
"""
Integration using FriCAS
EXAMPLES::
sage: from sage.symbolic.integration.external import fricas_integrator # optional - fricas
sage: fricas_integrator(sin(x), x) # optional - fricas
-cos(x)
sage: fricas_integrator(cos(x), x) # optional - fricas
sin(x)
sage: fricas_integrator(1/(x^2-2), x, 0, 1) # optional - fricas
1/4*(log(3*sqrt(2) - 4) - log(sqrt(2)))*sqrt(2)
sage: fricas_integrator(1/(x^2+6), x, -oo, oo) # optional - fricas
1/6*pi*sqrt(6)
"""
if not isinstance(expression, Expression):
expression = SR(expression)
if a is None:
result = expression._fricas_().integrate(v)
else:
import sage.rings.infinity
if a == sage.rings.infinity.PlusInfinity():
a = "%plusInfinity"
elif a == sage.rings.infinity.MinusInfinity():
a = "%minusInfinity"
if b == sage.rings.infinity.PlusInfinity():
b = "%plusInfinity"
elif b == sage.rings.infinity.MinusInfinity():
b = "%minusInfinity"

result = expression._fricas_().integrate("{}={}..{}".format(v, a, b))
locals = {str(v): v for v in expression.variables()}
if str(result) == "potentialPole":
raise ValueError("The integrand has a potential pole"
" in the integration interval")
parsed_result = result.unparsed_input_form()
import sage.misc.sage_eval
try:
return sage.misc.sage_eval.sage_eval(parsed_result, locals=locals)
except:
raise ValueError("Unable to parse: {}".format(parsed_result))
25 changes: 25 additions & 0 deletions src/sage/symbolic/integration/integral.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
available_integrators['maxima'] = external.maxima_integrator
available_integrators['sympy'] = external.sympy_integrator
available_integrators['mathematica_free'] = external.mma_free_integrator
available_integrators['fricas'] = external.fricas_integrator

######################################################
#
Expand Down Expand Up @@ -344,6 +345,8 @@ def integrate(expression, v=None, a=None, b=None, algorithm=None, hold=False):
- 'mathematica_free' - use http://integrals.wolfram.com/
- 'fricas' - use FriCAS (the optional fricas spkg has to be installed)
To prevent automatic evaluation use the ``hold`` argument.
EXAMPLES::
Expand Down Expand Up @@ -518,6 +521,28 @@ def integrate(expression, v=None, a=None, b=None, algorithm=None, hold=False):
sage: integral(e^(-x^2),(x, 0, 0.1))
0.05623145800914245*sqrt(pi)
An example of an integral that fricas can integrate, but the
default integrator cannot::
sage: f(x) = sqrt(x+sqrt(1+x^2))/x
sage: integrate(f(x), x, algorithm="fricas") # optional - fricas
2*sqrt(x + sqrt(x^2 + 1)) + log(sqrt(x + sqrt(x^2 + 1)) - 1)
- log(sqrt(x + sqrt(x^2 + 1)) + 1) - 2*arctan(sqrt(x + sqrt(x^2 + 1)))
The following definite integral is not found with the
default integrator::
sage: f(x) = (x^4 - 3*x^2 + 6) / (x^6 - 5*x^4 + 5*x^2 + 4)
sage: integrate(f(x), x, 1, 2)
integrate((x^4 - 3*x^2 + 6)/(x^6 - 5*x^4 + 5*x^2 + 4), x, 1, 2)
Both fricas and sympy give the correct result::
sage: integrate(f(x), x, 1, 2, algorithm="fricas") # optional - fricas
-1/2*pi + arctan(1/2) + arctan(2) + arctan(5) + arctan(8)
sage: integrate(f(x), x, 1, 2, algorithm="sympy")
-1/2*pi + arctan(1/2) + arctan(2) + arctan(5) + arctan(8)
ALIASES: integral() and integrate() are the same.
EXAMPLES:
Expand Down

0 comments on commit 2b719db

Please sign in to comment.