Symbolic Integration#

class sage.symbolic.integration.integral.DefiniteIntegral[source]#

Bases: BuiltinFunction

The symbolic function representing a definite integral.

EXAMPLES:

sage: from sage.symbolic.integration.integral import definite_integral
sage: definite_integral(sin(x),x,0,pi)
2
>>> from sage.all import *
>>> from sage.symbolic.integration.integral import definite_integral
>>> definite_integral(sin(x),x,Integer(0),pi)
2
class sage.symbolic.integration.integral.IndefiniteIntegral[source]#

Bases: BuiltinFunction

Class to represent an indefinite integral.

EXAMPLES:

sage: from sage.symbolic.integration.integral import indefinite_integral
sage: indefinite_integral(log(x), x) #indirect doctest
x*log(x) - x
sage: indefinite_integral(x^2, x)
1/3*x^3
sage: indefinite_integral(4*x*log(x), x)
2*x^2*log(x) - x^2
sage: indefinite_integral(exp(x), 2*x)
2*e^x
>>> from sage.all import *
>>> from sage.symbolic.integration.integral import indefinite_integral
>>> indefinite_integral(log(x), x) #indirect doctest
x*log(x) - x
>>> indefinite_integral(x**Integer(2), x)
1/3*x^3
>>> indefinite_integral(Integer(4)*x*log(x), x)
2*x^2*log(x) - x^2
>>> indefinite_integral(exp(x), Integer(2)*x)
2*e^x
sage.symbolic.integration.integral.integral(expression, v=None, a=None, b=None, algorithm=None, hold=False)[source]#

Return the indefinite integral with respect to the variable \(v\), ignoring the constant of integration. Or, if endpoints \(a\) and \(b\) are specified, returns the definite integral over the interval \([a, b]\).

If self has only one variable, then it returns the integral with respect to that variable.

If definite integration fails, it could be still possible to evaluate the definite integral using indefinite integration with the Newton - Leibniz theorem (however, the user has to ensure that the indefinite integral is continuous on the compact interval \([a,b]\) and this theorem can be applied).

INPUT:

  • v – a variable or variable name. This can also be a tuple of the variable (optional) and endpoints (i.e., (x,0,1) or (0,1)).

  • a – (optional) lower endpoint of definite integral

  • b – (optional) upper endpoint of definite integral

  • algorithm – (default: 'maxima', 'libgiac' and 'sympy') one of

    • 'maxima' – use maxima

    • 'sympy' – use sympy (also in Sage)

    • 'mathematica_free' – use http://integrals.wolfram.com/

    • 'fricas' – use FriCAS (the optional fricas spkg has to be installed)

    • 'giac' – use Giac

    • 'libgiac' – use libgiac

To prevent automatic evaluation, use the hold argument.

See also

To integrate a polynomial over a polytope, use the optional latte_int package sage.geometry.polyhedron.base.Polyhedron_base.integrate().

EXAMPLES:

sage: x = var('x')
sage: h = sin(x)/(cos(x))^2
sage: h.integral(x)
1/cos(x)
>>> from sage.all import *
>>> x = var('x')
>>> h = sin(x)/(cos(x))**Integer(2)
>>> h.integral(x)
1/cos(x)
sage: f = x^2/(x+1)^3
sage: f.integral(x)
1/2*(4*x + 3)/(x^2 + 2*x + 1) + log(x + 1)
>>> from sage.all import *
>>> f = x**Integer(2)/(x+Integer(1))**Integer(3)
>>> f.integral(x)
1/2*(4*x + 3)/(x^2 + 2*x + 1) + log(x + 1)
sage: f = x*cos(x^2)
sage: f.integral(x, 0, sqrt(pi))
0
sage: f.integral(x, a=-pi, b=pi)
0
>>> from sage.all import *
>>> f = x*cos(x**Integer(2))
>>> f.integral(x, Integer(0), sqrt(pi))
0
>>> f.integral(x, a=-pi, b=pi)
0
sage: f(x) = sin(x)
sage: f.integral(x, 0, pi/2)
1
>>> from sage.all import *
>>> __tmp__=var("x"); f = symbolic_expression(sin(x)).function(x)
>>> f.integral(x, Integer(0), pi/Integer(2))
1

The variable is required, but the endpoints are optional:

sage: y = var('y')
sage: integral(sin(x), x)
-cos(x)
sage: integral(sin(x), y)
y*sin(x)
sage: integral(sin(x), x, pi, 2*pi)
-2
sage: integral(sin(x), y, pi, 2*pi)
pi*sin(x)
sage: integral(sin(x), (x, pi, 2*pi))
-2
sage: integral(sin(x), (y, pi, 2*pi))
pi*sin(x)
>>> from sage.all import *
>>> y = var('y')
>>> integral(sin(x), x)
-cos(x)
>>> integral(sin(x), y)
y*sin(x)
>>> integral(sin(x), x, pi, Integer(2)*pi)
-2
>>> integral(sin(x), y, pi, Integer(2)*pi)
pi*sin(x)
>>> integral(sin(x), (x, pi, Integer(2)*pi))
-2
>>> integral(sin(x), (y, pi, Integer(2)*pi))
pi*sin(x)

Using the hold parameter it is possible to prevent automatic evaluation, which can then be evaluated via simplify():

sage: integral(x^2, x, 0, 3)
9
sage: a = integral(x^2, x, 0, 3, hold=True) ; a
integrate(x^2, x, 0, 3)
sage: a.simplify()
9
>>> from sage.all import *
>>> integral(x**Integer(2), x, Integer(0), Integer(3))
9
>>> a = integral(x**Integer(2), x, Integer(0), Integer(3), hold=True) ; a
integrate(x^2, x, 0, 3)
>>> a.simplify()
9

Constraints are sometimes needed:

sage: var('x, n')
(x, n)
sage: integral(x^n,x)
Traceback (most recent call last):
...
ValueError: Computation failed since Maxima requested additional
constraints; using the 'assume' command before evaluation
*may* help (example of legal syntax is 'assume(n>0)', see `assume?`
for more details)
Is n equal to -1?
sage: assume(n > 0)
sage: integral(x^n,x)
x^(n + 1)/(n + 1)
sage: forget()
>>> from sage.all import *
>>> var('x, n')
(x, n)
>>> integral(x**n,x)
Traceback (most recent call last):
...
ValueError: Computation failed since Maxima requested additional
constraints; using the 'assume' command before evaluation
*may* help (example of legal syntax is 'assume(n>0)', see `assume?`
for more details)
Is n equal to -1?
>>> assume(n > Integer(0))
>>> integral(x**n,x)
x^(n + 1)/(n + 1)
>>> forget()

Usually the constraints are of sign, but others are possible:

sage: assume(n==-1)
sage: integral(x^n,x)
log(x)
>>> from sage.all import *
>>> assume(n==-Integer(1))
>>> integral(x**n,x)
log(x)

Note that an exception is raised when a definite integral is divergent:

sage: forget() # always remember to forget assumptions you no longer need
sage: integrate(1/x^3,(x,0,1))
Traceback (most recent call last):
...
ValueError: Integral is divergent.
sage: integrate(1/x^3,x,-1,3)
Traceback (most recent call last):
...
ValueError: Integral is divergent.
>>> from sage.all import *
>>> forget() # always remember to forget assumptions you no longer need
>>> integrate(Integer(1)/x**Integer(3),(x,Integer(0),Integer(1)))
Traceback (most recent call last):
...
ValueError: Integral is divergent.
>>> integrate(Integer(1)/x**Integer(3),x,-Integer(1),Integer(3))
Traceback (most recent call last):
...
ValueError: Integral is divergent.

But Sage can calculate the convergent improper integral of this function:

sage: integrate(1/x^3,x,1,infinity)
1/2
>>> from sage.all import *
>>> integrate(Integer(1)/x**Integer(3),x,Integer(1),infinity)
1/2

The examples in the Maxima documentation:

sage: var('x, y, z, b')
(x, y, z, b)
sage: integral(sin(x)^3, x)
1/3*cos(x)^3 - cos(x)
sage: integral(x/sqrt(b^2-x^2), b)
x*log(2*b + 2*sqrt(b^2 - x^2))
sage: integral(x/sqrt(b^2-x^2), x)
-sqrt(b^2 - x^2)
sage: integral(cos(x)^2 * exp(x), x, 0, pi)
3/5*e^pi - 3/5
sage: integral(x^2 * exp(-x^2), x, -oo, oo)
1/2*sqrt(pi)
>>> from sage.all import *
>>> var('x, y, z, b')
(x, y, z, b)
>>> integral(sin(x)**Integer(3), x)
1/3*cos(x)^3 - cos(x)
>>> integral(x/sqrt(b**Integer(2)-x**Integer(2)), b)
x*log(2*b + 2*sqrt(b^2 - x^2))
>>> integral(x/sqrt(b**Integer(2)-x**Integer(2)), x)
-sqrt(b^2 - x^2)
>>> integral(cos(x)**Integer(2) * exp(x), x, Integer(0), pi)
3/5*e^pi - 3/5
>>> integral(x**Integer(2) * exp(-x**Integer(2)), x, -oo, oo)
1/2*sqrt(pi)

We integrate the same function in both Mathematica and Sage (via Maxima):

sage: _ = var('x, y, z')
sage: f = sin(x^2) + y^z
sage: g = mathematica(f)                            # optional - mathematica
sage: print(g)                                      # optional - mathematica
          z        2
         y  + Sin[x ]
sage: print(g.Integrate(x))                         # optional - mathematica
            z        Pi                2
         x y  + Sqrt[--] FresnelS[Sqrt[--] x]
                     2                 Pi
sage: print(f.integral(x))
x*y^z + 1/16*sqrt(pi)*((I + 1)*sqrt(2)*erf((1/2*I + 1/2)*sqrt(2)*x)
                        + (I - 1)*sqrt(2)*erf((1/2*I - 1/2)*sqrt(2)*x)
                        - (I - 1)*sqrt(2)*erf(sqrt(-I)*x)
                        + (I + 1)*sqrt(2)*erf((-1)^(1/4)*x))
>>> from sage.all import *
>>> _ = var('x, y, z')
>>> f = sin(x**Integer(2)) + y**z
>>> g = mathematica(f)                            # optional - mathematica
>>> print(g)                                      # optional - mathematica
          z        2
         y  + Sin[x ]
>>> print(g.Integrate(x))                         # optional - mathematica
            z        Pi                2
         x y  + Sqrt[--] FresnelS[Sqrt[--] x]
                     2                 Pi
>>> print(f.integral(x))
x*y^z + 1/16*sqrt(pi)*((I + 1)*sqrt(2)*erf((1/2*I + 1/2)*sqrt(2)*x)
                        + (I - 1)*sqrt(2)*erf((1/2*I - 1/2)*sqrt(2)*x)
                        - (I - 1)*sqrt(2)*erf(sqrt(-I)*x)
                        + (I + 1)*sqrt(2)*erf((-1)^(1/4)*x))

Alternatively, just use algorithm=’mathematica_free’ to integrate via Mathematica over the internet (does NOT require a Mathematica license!):

sage: _ = var('x, y, z')  # optional - internet
sage: f = sin(x^2) + y^z   # optional - internet
sage: f.integrate(x, algorithm="mathematica_free")   # optional - internet
x*y^z + sqrt(1/2)*sqrt(pi)*fresnel_sin(sqrt(2)*x/sqrt(pi))
>>> from sage.all import *
>>> _ = var('x, y, z')  # optional - internet
>>> f = sin(x**Integer(2)) + y**z   # optional - internet
>>> f.integrate(x, algorithm="mathematica_free")   # optional - internet
x*y^z + sqrt(1/2)*sqrt(pi)*fresnel_sin(sqrt(2)*x/sqrt(pi))

We can also use Sympy:

sage: integrate(x*sin(log(x)), x)
-1/5*x^2*(cos(log(x)) - 2*sin(log(x)))
sage: integrate(x*sin(log(x)), x, algorithm='sympy')                            # needs sympy
-1/5*x^2*cos(log(x)) + 2/5*x^2*sin(log(x))
sage: _ = var('y, z')
sage: (x^y - z).integrate(y)
-y*z + x^y/log(x)
sage: (x^y - z).integrate(y, algorithm="sympy")                                 # needs sympy
-y*z + cases(((log(x) != 0, x^y/log(x)), (1, y)))
>>> from sage.all import *
>>> integrate(x*sin(log(x)), x)
-1/5*x^2*(cos(log(x)) - 2*sin(log(x)))
>>> integrate(x*sin(log(x)), x, algorithm='sympy')                            # needs sympy
-1/5*x^2*cos(log(x)) + 2/5*x^2*sin(log(x))
>>> _ = var('y, z')
>>> (x**y - z).integrate(y)
-y*z + x^y/log(x)
>>> (x**y - z).integrate(y, algorithm="sympy")                                 # needs sympy
-y*z + cases(((log(x) != 0, x^y/log(x)), (1, y)))

We integrate the above function in Maple now:

sage: g = maple(f); g.sort()         # optional - maple
y^z+sin(x^2)
sage: g.integrate(x).sort()          # optional - maple
x*y^z+1/2*2^(1/2)*Pi^(1/2)*FresnelS(2^(1/2)/Pi^(1/2)*x)
>>> from sage.all import *
>>> g = maple(f); g.sort()         # optional - maple
y^z+sin(x^2)
>>> g.integrate(x).sort()          # optional - maple
x*y^z+1/2*2^(1/2)*Pi^(1/2)*FresnelS(2^(1/2)/Pi^(1/2)*x)

We next integrate a function with no closed form integral. Notice that the answer comes back as an expression that contains an integral itself.

sage: A = integral(1/ ((x-4) * (x^4+x+1)), x); A
integrate(1/((x^4 + x + 1)*(x - 4)), x)
>>> from sage.all import *
>>> A = integral(Integer(1)/ ((x-Integer(4)) * (x**Integer(4)+x+Integer(1))), x); A
integrate(1/((x^4 + x + 1)*(x - 4)), x)

Sometimes, in this situation, using the algorithm “maxima” gives instead a partially integrated answer:

sage: integral(1/(x**7-1),x,algorithm='maxima')
-1/7*integrate((x^5 + 2*x^4 + 3*x^3 + 4*x^2 + 5*x + 6)/(x^6 + x^5 + x^4 + x^3 + x^2 + x + 1), x) + 1/7*log(x - 1)
>>> from sage.all import *
>>> integral(Integer(1)/(x**Integer(7)-Integer(1)),x,algorithm='maxima')
-1/7*integrate((x^5 + 2*x^4 + 3*x^3 + 4*x^2 + 5*x + 6)/(x^6 + x^5 + x^4 + x^3 + x^2 + x + 1), x) + 1/7*log(x - 1)

We now show that floats are not converted to rationals automatically since we by default have keepfloat: true in maxima.

sage: integral(e^(-x^2),(x, 0, 0.1))
0.05623145800914245*sqrt(pi)
>>> from sage.all import *
>>> integral(e**(-x**Integer(2)),(x, Integer(0), RealNumber('0.1')))
0.05623145800914245*sqrt(pi)

An example of an integral that fricas can integrate:

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)) - 2*arctan(sqrt(x + sqrt(x^2 + 1)))
 - log(sqrt(x + sqrt(x^2 + 1)) + 1) + log(sqrt(x + sqrt(x^2 + 1)) - 1)
>>> from sage.all import *
>>> __tmp__=var("x"); f = symbolic_expression(sqrt(x+sqrt(Integer(1)+x**Integer(2)))/x).function(x)
>>> integrate(f(x), x, algorithm="fricas")      # optional - fricas
2*sqrt(x + sqrt(x^2 + 1)) - 2*arctan(sqrt(x + sqrt(x^2 + 1)))
 - log(sqrt(x + sqrt(x^2 + 1)) + 1) + log(sqrt(x + sqrt(x^2 + 1)) - 1)

where the default integrator obtains another answer:

sage: integrate(f(x), x)  # long time
1/8*sqrt(x)*gamma(1/4)*gamma(-1/4)^2*hypergeometric((-1/4, -1/4, 1/4),
                                                    (1/2, 3/4),
                                                    -1/x^2)/(pi*gamma(3/4))
>>> from sage.all import *
>>> integrate(f(x), x)  # long time
1/8*sqrt(x)*gamma(1/4)*gamma(-1/4)^2*hypergeometric((-1/4, -1/4, 1/4),
                                                    (1/2, 3/4),
                                                    -1/x^2)/(pi*gamma(3/4))

The following definite integral is not found by maxima:

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, algorithm='maxima')
integrate((x^4 - 3*x^2 + 6)/(x^6 - 5*x^4 + 5*x^2 + 4), x, 1, 2)
>>> from sage.all import *
>>> __tmp__=var("x"); f = symbolic_expression((x**Integer(4) - Integer(3)*x**Integer(2) + Integer(6)) / (x**Integer(6) - Integer(5)*x**Integer(4) + Integer(5)*x**Integer(2) + Integer(4))).function(x)
>>> integrate(f(x), x, Integer(1), Integer(2), algorithm='maxima')
integrate((x^4 - 3*x^2 + 6)/(x^6 - 5*x^4 + 5*x^2 + 4), x, 1, 2)

but is nevertheless computed:

sage: integrate(f(x), x, 1, 2)
-1/2*pi + arctan(8) + arctan(5) + arctan(2) + arctan(1/2)
>>> from sage.all import *
>>> integrate(f(x), x, Integer(1), Integer(2))
-1/2*pi + arctan(8) + arctan(5) + arctan(2) + arctan(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(8) + arctan(5) + arctan(2) + arctan(1/2)
sage: integrate(f(x), x, 1, 2, algorithm="sympy")                               # needs sympy
-1/2*pi + arctan(8) + arctan(5) + arctan(2) + arctan(1/2)
>>> from sage.all import *
>>> integrate(f(x), x, Integer(1), Integer(2), algorithm="fricas")  # optional - fricas
-1/2*pi + arctan(8) + arctan(5) + arctan(2) + arctan(1/2)
>>> integrate(f(x), x, Integer(1), Integer(2), algorithm="sympy")                               # needs sympy
-1/2*pi + arctan(8) + arctan(5) + arctan(2) + arctan(1/2)

Using Giac to integrate the absolute value of a trigonometric expression:

sage: integrate(abs(cos(x)), x, 0, 2*pi, algorithm='giac')
4
sage: result = integrate(abs(cos(x)), x, 0, 2*pi)
...
sage: result
4
>>> from sage.all import *
>>> integrate(abs(cos(x)), x, Integer(0), Integer(2)*pi, algorithm='giac')
4
>>> result = integrate(abs(cos(x)), x, Integer(0), Integer(2)*pi)
...
>>> result
4

ALIASES: integral() and integrate() are the same.

EXAMPLES:

Here is an example where we have to use assume:

sage: a,b = var('a,b')
sage: integrate(1/(x^3 *(a+b*x)^(1/3)), x)
Traceback (most recent call last):
...
ValueError: Computation failed since Maxima requested additional
constraints; using the 'assume' command before evaluation
*may* help (example of legal syntax is 'assume(a>0)', see `assume?`
for more details)
Is a positive or negative?
>>> from sage.all import *
>>> a,b = var('a,b')
>>> integrate(Integer(1)/(x**Integer(3) *(a+b*x)**(Integer(1)/Integer(3))), x)
Traceback (most recent call last):
...
ValueError: Computation failed since Maxima requested additional
constraints; using the 'assume' command before evaluation
*may* help (example of legal syntax is 'assume(a>0)', see `assume?`
for more details)
Is a positive or negative?

So we just assume that \(a>0\) and the integral works:

sage: assume(a>0)
sage: integrate(1/(x^3 *(a+b*x)^(1/3)), x)
2/9*sqrt(3)*b^2*arctan(1/3*sqrt(3)*(2*(b*x + a)^(1/3) + a^(1/3))/a^(1/3))/a^(7/3)
 - 1/9*b^2*log((b*x + a)^(2/3) + (b*x + a)^(1/3)*a^(1/3) + a^(2/3))/a^(7/3)
 + 2/9*b^2*log((b*x + a)^(1/3) - a^(1/3))/a^(7/3) + 1/6*(4*(b*x + a)^(5/3)*b^2
 - 7*(b*x + a)^(2/3)*a*b^2)/((b*x + a)^2*a^2 - 2*(b*x + a)*a^3 + a^4)
>>> from sage.all import *
>>> assume(a>Integer(0))
>>> integrate(Integer(1)/(x**Integer(3) *(a+b*x)**(Integer(1)/Integer(3))), x)
2/9*sqrt(3)*b^2*arctan(1/3*sqrt(3)*(2*(b*x + a)^(1/3) + a^(1/3))/a^(1/3))/a^(7/3)
 - 1/9*b^2*log((b*x + a)^(2/3) + (b*x + a)^(1/3)*a^(1/3) + a^(2/3))/a^(7/3)
 + 2/9*b^2*log((b*x + a)^(1/3) - a^(1/3))/a^(7/3) + 1/6*(4*(b*x + a)^(5/3)*b^2
 - 7*(b*x + a)^(2/3)*a*b^2)/((b*x + a)^2*a^2 - 2*(b*x + a)*a^3 + a^4)
sage.symbolic.integration.integral.integrate(expression, v=None, a=None, b=None, algorithm=None, hold=False)[source]#

Return the indefinite integral with respect to the variable \(v\), ignoring the constant of integration. Or, if endpoints \(a\) and \(b\) are specified, returns the definite integral over the interval \([a, b]\).

If self has only one variable, then it returns the integral with respect to that variable.

If definite integration fails, it could be still possible to evaluate the definite integral using indefinite integration with the Newton - Leibniz theorem (however, the user has to ensure that the indefinite integral is continuous on the compact interval \([a,b]\) and this theorem can be applied).

INPUT:

  • v – a variable or variable name. This can also be a tuple of the variable (optional) and endpoints (i.e., (x,0,1) or (0,1)).

  • a – (optional) lower endpoint of definite integral

  • b – (optional) upper endpoint of definite integral

  • algorithm – (default: 'maxima', 'libgiac' and 'sympy') one of

    • 'maxima' – use maxima

    • 'sympy' – use sympy (also in Sage)

    • 'mathematica_free' – use http://integrals.wolfram.com/

    • 'fricas' – use FriCAS (the optional fricas spkg has to be installed)

    • 'giac' – use Giac

    • 'libgiac' – use libgiac

To prevent automatic evaluation, use the hold argument.

See also

To integrate a polynomial over a polytope, use the optional latte_int package sage.geometry.polyhedron.base.Polyhedron_base.integrate().

EXAMPLES:

sage: x = var('x')
sage: h = sin(x)/(cos(x))^2
sage: h.integral(x)
1/cos(x)
>>> from sage.all import *
>>> x = var('x')
>>> h = sin(x)/(cos(x))**Integer(2)
>>> h.integral(x)
1/cos(x)
sage: f = x^2/(x+1)^3
sage: f.integral(x)
1/2*(4*x + 3)/(x^2 + 2*x + 1) + log(x + 1)
>>> from sage.all import *
>>> f = x**Integer(2)/(x+Integer(1))**Integer(3)
>>> f.integral(x)
1/2*(4*x + 3)/(x^2 + 2*x + 1) + log(x + 1)
sage: f = x*cos(x^2)
sage: f.integral(x, 0, sqrt(pi))
0
sage: f.integral(x, a=-pi, b=pi)
0
>>> from sage.all import *
>>> f = x*cos(x**Integer(2))
>>> f.integral(x, Integer(0), sqrt(pi))
0
>>> f.integral(x, a=-pi, b=pi)
0
sage: f(x) = sin(x)
sage: f.integral(x, 0, pi/2)
1
>>> from sage.all import *
>>> __tmp__=var("x"); f = symbolic_expression(sin(x)).function(x)
>>> f.integral(x, Integer(0), pi/Integer(2))
1

The variable is required, but the endpoints are optional:

sage: y = var('y')
sage: integral(sin(x), x)
-cos(x)
sage: integral(sin(x), y)
y*sin(x)
sage: integral(sin(x), x, pi, 2*pi)
-2
sage: integral(sin(x), y, pi, 2*pi)
pi*sin(x)
sage: integral(sin(x), (x, pi, 2*pi))
-2
sage: integral(sin(x), (y, pi, 2*pi))
pi*sin(x)
>>> from sage.all import *
>>> y = var('y')
>>> integral(sin(x), x)
-cos(x)
>>> integral(sin(x), y)
y*sin(x)
>>> integral(sin(x), x, pi, Integer(2)*pi)
-2
>>> integral(sin(x), y, pi, Integer(2)*pi)
pi*sin(x)
>>> integral(sin(x), (x, pi, Integer(2)*pi))
-2
>>> integral(sin(x), (y, pi, Integer(2)*pi))
pi*sin(x)

Using the hold parameter it is possible to prevent automatic evaluation, which can then be evaluated via simplify():

sage: integral(x^2, x, 0, 3)
9
sage: a = integral(x^2, x, 0, 3, hold=True) ; a
integrate(x^2, x, 0, 3)
sage: a.simplify()
9
>>> from sage.all import *
>>> integral(x**Integer(2), x, Integer(0), Integer(3))
9
>>> a = integral(x**Integer(2), x, Integer(0), Integer(3), hold=True) ; a
integrate(x^2, x, 0, 3)
>>> a.simplify()
9

Constraints are sometimes needed:

sage: var('x, n')
(x, n)
sage: integral(x^n,x)
Traceback (most recent call last):
...
ValueError: Computation failed since Maxima requested additional
constraints; using the 'assume' command before evaluation
*may* help (example of legal syntax is 'assume(n>0)', see `assume?`
for more details)
Is n equal to -1?
sage: assume(n > 0)
sage: integral(x^n,x)
x^(n + 1)/(n + 1)
sage: forget()
>>> from sage.all import *
>>> var('x, n')
(x, n)
>>> integral(x**n,x)
Traceback (most recent call last):
...
ValueError: Computation failed since Maxima requested additional
constraints; using the 'assume' command before evaluation
*may* help (example of legal syntax is 'assume(n>0)', see `assume?`
for more details)
Is n equal to -1?
>>> assume(n > Integer(0))
>>> integral(x**n,x)
x^(n + 1)/(n + 1)
>>> forget()

Usually the constraints are of sign, but others are possible:

sage: assume(n==-1)
sage: integral(x^n,x)
log(x)
>>> from sage.all import *
>>> assume(n==-Integer(1))
>>> integral(x**n,x)
log(x)

Note that an exception is raised when a definite integral is divergent:

sage: forget() # always remember to forget assumptions you no longer need
sage: integrate(1/x^3,(x,0,1))
Traceback (most recent call last):
...
ValueError: Integral is divergent.
sage: integrate(1/x^3,x,-1,3)
Traceback (most recent call last):
...
ValueError: Integral is divergent.
>>> from sage.all import *
>>> forget() # always remember to forget assumptions you no longer need
>>> integrate(Integer(1)/x**Integer(3),(x,Integer(0),Integer(1)))
Traceback (most recent call last):
...
ValueError: Integral is divergent.
>>> integrate(Integer(1)/x**Integer(3),x,-Integer(1),Integer(3))
Traceback (most recent call last):
...
ValueError: Integral is divergent.

But Sage can calculate the convergent improper integral of this function:

sage: integrate(1/x^3,x,1,infinity)
1/2
>>> from sage.all import *
>>> integrate(Integer(1)/x**Integer(3),x,Integer(1),infinity)
1/2

The examples in the Maxima documentation:

sage: var('x, y, z, b')
(x, y, z, b)
sage: integral(sin(x)^3, x)
1/3*cos(x)^3 - cos(x)
sage: integral(x/sqrt(b^2-x^2), b)
x*log(2*b + 2*sqrt(b^2 - x^2))
sage: integral(x/sqrt(b^2-x^2), x)
-sqrt(b^2 - x^2)
sage: integral(cos(x)^2 * exp(x), x, 0, pi)
3/5*e^pi - 3/5
sage: integral(x^2 * exp(-x^2), x, -oo, oo)
1/2*sqrt(pi)
>>> from sage.all import *
>>> var('x, y, z, b')
(x, y, z, b)
>>> integral(sin(x)**Integer(3), x)
1/3*cos(x)^3 - cos(x)
>>> integral(x/sqrt(b**Integer(2)-x**Integer(2)), b)
x*log(2*b + 2*sqrt(b^2 - x^2))
>>> integral(x/sqrt(b**Integer(2)-x**Integer(2)), x)
-sqrt(b^2 - x^2)
>>> integral(cos(x)**Integer(2) * exp(x), x, Integer(0), pi)
3/5*e^pi - 3/5
>>> integral(x**Integer(2) * exp(-x**Integer(2)), x, -oo, oo)
1/2*sqrt(pi)

We integrate the same function in both Mathematica and Sage (via Maxima):

sage: _ = var('x, y, z')
sage: f = sin(x^2) + y^z
sage: g = mathematica(f)                            # optional - mathematica
sage: print(g)                                      # optional - mathematica
          z        2
         y  + Sin[x ]
sage: print(g.Integrate(x))                         # optional - mathematica
            z        Pi                2
         x y  + Sqrt[--] FresnelS[Sqrt[--] x]
                     2                 Pi
sage: print(f.integral(x))
x*y^z + 1/16*sqrt(pi)*((I + 1)*sqrt(2)*erf((1/2*I + 1/2)*sqrt(2)*x)
                        + (I - 1)*sqrt(2)*erf((1/2*I - 1/2)*sqrt(2)*x)
                        - (I - 1)*sqrt(2)*erf(sqrt(-I)*x)
                        + (I + 1)*sqrt(2)*erf((-1)^(1/4)*x))
>>> from sage.all import *
>>> _ = var('x, y, z')
>>> f = sin(x**Integer(2)) + y**z
>>> g = mathematica(f)                            # optional - mathematica
>>> print(g)                                      # optional - mathematica
          z        2
         y  + Sin[x ]
>>> print(g.Integrate(x))                         # optional - mathematica
            z        Pi                2
         x y  + Sqrt[--] FresnelS[Sqrt[--] x]
                     2                 Pi
>>> print(f.integral(x))
x*y^z + 1/16*sqrt(pi)*((I + 1)*sqrt(2)*erf((1/2*I + 1/2)*sqrt(2)*x)
                        + (I - 1)*sqrt(2)*erf((1/2*I - 1/2)*sqrt(2)*x)
                        - (I - 1)*sqrt(2)*erf(sqrt(-I)*x)
                        + (I + 1)*sqrt(2)*erf((-1)^(1/4)*x))

Alternatively, just use algorithm=’mathematica_free’ to integrate via Mathematica over the internet (does NOT require a Mathematica license!):

sage: _ = var('x, y, z')  # optional - internet
sage: f = sin(x^2) + y^z   # optional - internet
sage: f.integrate(x, algorithm="mathematica_free")   # optional - internet
x*y^z + sqrt(1/2)*sqrt(pi)*fresnel_sin(sqrt(2)*x/sqrt(pi))
>>> from sage.all import *
>>> _ = var('x, y, z')  # optional - internet
>>> f = sin(x**Integer(2)) + y**z   # optional - internet
>>> f.integrate(x, algorithm="mathematica_free")   # optional - internet
x*y^z + sqrt(1/2)*sqrt(pi)*fresnel_sin(sqrt(2)*x/sqrt(pi))

We can also use Sympy:

sage: integrate(x*sin(log(x)), x)
-1/5*x^2*(cos(log(x)) - 2*sin(log(x)))
sage: integrate(x*sin(log(x)), x, algorithm='sympy')                            # needs sympy
-1/5*x^2*cos(log(x)) + 2/5*x^2*sin(log(x))
sage: _ = var('y, z')
sage: (x^y - z).integrate(y)
-y*z + x^y/log(x)
sage: (x^y - z).integrate(y, algorithm="sympy")                                 # needs sympy
-y*z + cases(((log(x) != 0, x^y/log(x)), (1, y)))
>>> from sage.all import *
>>> integrate(x*sin(log(x)), x)
-1/5*x^2*(cos(log(x)) - 2*sin(log(x)))
>>> integrate(x*sin(log(x)), x, algorithm='sympy')                            # needs sympy
-1/5*x^2*cos(log(x)) + 2/5*x^2*sin(log(x))
>>> _ = var('y, z')
>>> (x**y - z).integrate(y)
-y*z + x^y/log(x)
>>> (x**y - z).integrate(y, algorithm="sympy")                                 # needs sympy
-y*z + cases(((log(x) != 0, x^y/log(x)), (1, y)))

We integrate the above function in Maple now:

sage: g = maple(f); g.sort()         # optional - maple
y^z+sin(x^2)
sage: g.integrate(x).sort()          # optional - maple
x*y^z+1/2*2^(1/2)*Pi^(1/2)*FresnelS(2^(1/2)/Pi^(1/2)*x)
>>> from sage.all import *
>>> g = maple(f); g.sort()         # optional - maple
y^z+sin(x^2)
>>> g.integrate(x).sort()          # optional - maple
x*y^z+1/2*2^(1/2)*Pi^(1/2)*FresnelS(2^(1/2)/Pi^(1/2)*x)

We next integrate a function with no closed form integral. Notice that the answer comes back as an expression that contains an integral itself.

sage: A = integral(1/ ((x-4) * (x^4+x+1)), x); A
integrate(1/((x^4 + x + 1)*(x - 4)), x)
>>> from sage.all import *
>>> A = integral(Integer(1)/ ((x-Integer(4)) * (x**Integer(4)+x+Integer(1))), x); A
integrate(1/((x^4 + x + 1)*(x - 4)), x)

Sometimes, in this situation, using the algorithm “maxima” gives instead a partially integrated answer:

sage: integral(1/(x**7-1),x,algorithm='maxima')
-1/7*integrate((x^5 + 2*x^4 + 3*x^3 + 4*x^2 + 5*x + 6)/(x^6 + x^5 + x^4 + x^3 + x^2 + x + 1), x) + 1/7*log(x - 1)
>>> from sage.all import *
>>> integral(Integer(1)/(x**Integer(7)-Integer(1)),x,algorithm='maxima')
-1/7*integrate((x^5 + 2*x^4 + 3*x^3 + 4*x^2 + 5*x + 6)/(x^6 + x^5 + x^4 + x^3 + x^2 + x + 1), x) + 1/7*log(x - 1)

We now show that floats are not converted to rationals automatically since we by default have keepfloat: true in maxima.

sage: integral(e^(-x^2),(x, 0, 0.1))
0.05623145800914245*sqrt(pi)
>>> from sage.all import *
>>> integral(e**(-x**Integer(2)),(x, Integer(0), RealNumber('0.1')))
0.05623145800914245*sqrt(pi)

An example of an integral that fricas can integrate:

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)) - 2*arctan(sqrt(x + sqrt(x^2 + 1)))
 - log(sqrt(x + sqrt(x^2 + 1)) + 1) + log(sqrt(x + sqrt(x^2 + 1)) - 1)
>>> from sage.all import *
>>> __tmp__=var("x"); f = symbolic_expression(sqrt(x+sqrt(Integer(1)+x**Integer(2)))/x).function(x)
>>> integrate(f(x), x, algorithm="fricas")      # optional - fricas
2*sqrt(x + sqrt(x^2 + 1)) - 2*arctan(sqrt(x + sqrt(x^2 + 1)))
 - log(sqrt(x + sqrt(x^2 + 1)) + 1) + log(sqrt(x + sqrt(x^2 + 1)) - 1)

where the default integrator obtains another answer:

sage: integrate(f(x), x)  # long time
1/8*sqrt(x)*gamma(1/4)*gamma(-1/4)^2*hypergeometric((-1/4, -1/4, 1/4),
                                                    (1/2, 3/4),
                                                    -1/x^2)/(pi*gamma(3/4))
>>> from sage.all import *
>>> integrate(f(x), x)  # long time
1/8*sqrt(x)*gamma(1/4)*gamma(-1/4)^2*hypergeometric((-1/4, -1/4, 1/4),
                                                    (1/2, 3/4),
                                                    -1/x^2)/(pi*gamma(3/4))

The following definite integral is not found by maxima:

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, algorithm='maxima')
integrate((x^4 - 3*x^2 + 6)/(x^6 - 5*x^4 + 5*x^2 + 4), x, 1, 2)
>>> from sage.all import *
>>> __tmp__=var("x"); f = symbolic_expression((x**Integer(4) - Integer(3)*x**Integer(2) + Integer(6)) / (x**Integer(6) - Integer(5)*x**Integer(4) + Integer(5)*x**Integer(2) + Integer(4))).function(x)
>>> integrate(f(x), x, Integer(1), Integer(2), algorithm='maxima')
integrate((x^4 - 3*x^2 + 6)/(x^6 - 5*x^4 + 5*x^2 + 4), x, 1, 2)

but is nevertheless computed:

sage: integrate(f(x), x, 1, 2)
-1/2*pi + arctan(8) + arctan(5) + arctan(2) + arctan(1/2)
>>> from sage.all import *
>>> integrate(f(x), x, Integer(1), Integer(2))
-1/2*pi + arctan(8) + arctan(5) + arctan(2) + arctan(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(8) + arctan(5) + arctan(2) + arctan(1/2)
sage: integrate(f(x), x, 1, 2, algorithm="sympy")                               # needs sympy
-1/2*pi + arctan(8) + arctan(5) + arctan(2) + arctan(1/2)
>>> from sage.all import *
>>> integrate(f(x), x, Integer(1), Integer(2), algorithm="fricas")  # optional - fricas
-1/2*pi + arctan(8) + arctan(5) + arctan(2) + arctan(1/2)
>>> integrate(f(x), x, Integer(1), Integer(2), algorithm="sympy")                               # needs sympy
-1/2*pi + arctan(8) + arctan(5) + arctan(2) + arctan(1/2)

Using Giac to integrate the absolute value of a trigonometric expression:

sage: integrate(abs(cos(x)), x, 0, 2*pi, algorithm='giac')
4
sage: result = integrate(abs(cos(x)), x, 0, 2*pi)
...
sage: result
4
>>> from sage.all import *
>>> integrate(abs(cos(x)), x, Integer(0), Integer(2)*pi, algorithm='giac')
4
>>> result = integrate(abs(cos(x)), x, Integer(0), Integer(2)*pi)
...
>>> result
4

ALIASES: integral() and integrate() are the same.

EXAMPLES:

Here is an example where we have to use assume:

sage: a,b = var('a,b')
sage: integrate(1/(x^3 *(a+b*x)^(1/3)), x)
Traceback (most recent call last):
...
ValueError: Computation failed since Maxima requested additional
constraints; using the 'assume' command before evaluation
*may* help (example of legal syntax is 'assume(a>0)', see `assume?`
for more details)
Is a positive or negative?
>>> from sage.all import *
>>> a,b = var('a,b')
>>> integrate(Integer(1)/(x**Integer(3) *(a+b*x)**(Integer(1)/Integer(3))), x)
Traceback (most recent call last):
...
ValueError: Computation failed since Maxima requested additional
constraints; using the 'assume' command before evaluation
*may* help (example of legal syntax is 'assume(a>0)', see `assume?`
for more details)
Is a positive or negative?

So we just assume that \(a>0\) and the integral works:

sage: assume(a>0)
sage: integrate(1/(x^3 *(a+b*x)^(1/3)), x)
2/9*sqrt(3)*b^2*arctan(1/3*sqrt(3)*(2*(b*x + a)^(1/3) + a^(1/3))/a^(1/3))/a^(7/3)
 - 1/9*b^2*log((b*x + a)^(2/3) + (b*x + a)^(1/3)*a^(1/3) + a^(2/3))/a^(7/3)
 + 2/9*b^2*log((b*x + a)^(1/3) - a^(1/3))/a^(7/3) + 1/6*(4*(b*x + a)^(5/3)*b^2
 - 7*(b*x + a)^(2/3)*a*b^2)/((b*x + a)^2*a^2 - 2*(b*x + a)*a^3 + a^4)
>>> from sage.all import *
>>> assume(a>Integer(0))
>>> integrate(Integer(1)/(x**Integer(3) *(a+b*x)**(Integer(1)/Integer(3))), x)
2/9*sqrt(3)*b^2*arctan(1/3*sqrt(3)*(2*(b*x + a)^(1/3) + a^(1/3))/a^(1/3))/a^(7/3)
 - 1/9*b^2*log((b*x + a)^(2/3) + (b*x + a)^(1/3)*a^(1/3) + a^(2/3))/a^(7/3)
 + 2/9*b^2*log((b*x + a)^(1/3) - a^(1/3))/a^(7/3) + 1/6*(4*(b*x + a)^(5/3)*b^2
 - 7*(b*x + a)^(2/3)*a*b^2)/((b*x + a)^2*a^2 - 2*(b*x + a)*a^3 + a^4)