Functional notation support for common calculus methods¶
EXAMPLES: We illustrate each of the calculus functional functions.
sage: simplify(x - x)
0
sage: a = var('a')
sage: derivative(x^a + sin(x), x)
a*x^(a - 1) + cos(x)
sage: diff(x^a + sin(x), x)
a*x^(a - 1) + cos(x)
sage: derivative(x^a + sin(x), x)
a*x^(a - 1) + cos(x)
sage: integral(a*x*sin(x), x)
-(x*cos(x) - sin(x))*a
sage: integrate(a*x*sin(x), x)
-(x*cos(x) - sin(x))*a
sage: limit(a*sin(x)/x, x=0)
a
sage: taylor(a*sin(x)/x, x, 0, 4)
1/120*a*x^4 - 1/6*a*x^2 + a
sage: expand((x - a)^3)
-a^3 + 3*a^2*x - 3*a*x^2 + x^3
>>> from sage.all import *
>>> simplify(x - x)
0
>>> a = var('a')
>>> derivative(x**a + sin(x), x)
a*x^(a - 1) + cos(x)
>>> diff(x**a + sin(x), x)
a*x^(a - 1) + cos(x)
>>> derivative(x**a + sin(x), x)
a*x^(a - 1) + cos(x)
>>> integral(a*x*sin(x), x)
-(x*cos(x) - sin(x))*a
>>> integrate(a*x*sin(x), x)
-(x*cos(x) - sin(x))*a
>>> limit(a*sin(x)/x, x=Integer(0))
a
>>> taylor(a*sin(x)/x, x, Integer(0), Integer(4))
1/120*a*x^4 - 1/6*a*x^2 + a
>>> expand((x - a)**Integer(3))
-a^3 + 3*a^2*x - 3*a*x^2 + x^3
- sage.calculus.functional.derivative(f, *args, **kwds)[source]¶
The derivative of \(f\).
Repeated differentiation is supported by the syntax given in the examples below.
ALIAS: diff
EXAMPLES: We differentiate a callable symbolic function:
sage: f(x,y) = x*y + sin(x^2) + e^(-x) sage: f (x, y) |--> x*y + e^(-x) + sin(x^2) sage: derivative(f, x) (x, y) |--> 2*x*cos(x^2) + y - e^(-x) sage: derivative(f, y) (x, y) |--> x
>>> from sage.all import * >>> __tmp__=var("x,y"); f = symbolic_expression(x*y + sin(x**Integer(2)) + e**(-x)).function(x,y) >>> f (x, y) |--> x*y + e^(-x) + sin(x^2) >>> derivative(f, x) (x, y) |--> 2*x*cos(x^2) + y - e^(-x) >>> derivative(f, y) (x, y) |--> x
We differentiate a polynomial:
sage: t = polygen(QQ, 't') sage: f = (1-t)^5; f -t^5 + 5*t^4 - 10*t^3 + 10*t^2 - 5*t + 1 sage: derivative(f) -5*t^4 + 20*t^3 - 30*t^2 + 20*t - 5 sage: derivative(f, t) -5*t^4 + 20*t^3 - 30*t^2 + 20*t - 5 sage: derivative(f, t, t) -20*t^3 + 60*t^2 - 60*t + 20 sage: derivative(f, t, 2) -20*t^3 + 60*t^2 - 60*t + 20 sage: derivative(f, 2) -20*t^3 + 60*t^2 - 60*t + 20
>>> from sage.all import * >>> t = polygen(QQ, 't') >>> f = (Integer(1)-t)**Integer(5); f -t^5 + 5*t^4 - 10*t^3 + 10*t^2 - 5*t + 1 >>> derivative(f) -5*t^4 + 20*t^3 - 30*t^2 + 20*t - 5 >>> derivative(f, t) -5*t^4 + 20*t^3 - 30*t^2 + 20*t - 5 >>> derivative(f, t, t) -20*t^3 + 60*t^2 - 60*t + 20 >>> derivative(f, t, Integer(2)) -20*t^3 + 60*t^2 - 60*t + 20 >>> derivative(f, Integer(2)) -20*t^3 + 60*t^2 - 60*t + 20
We differentiate a symbolic expression:
sage: var('a x') (a, x) sage: f = exp(sin(a - x^2))/x sage: derivative(f, x) -2*cos(-x^2 + a)*e^(sin(-x^2 + a)) - e^(sin(-x^2 + a))/x^2 sage: derivative(f, a) cos(-x^2 + a)*e^(sin(-x^2 + a))/x
>>> from sage.all import * >>> var('a x') (a, x) >>> f = exp(sin(a - x**Integer(2)))/x >>> derivative(f, x) -2*cos(-x^2 + a)*e^(sin(-x^2 + a)) - e^(sin(-x^2 + a))/x^2 >>> derivative(f, a) cos(-x^2 + a)*e^(sin(-x^2 + a))/x
Syntax for repeated differentiation:
sage: R.<u, v> = PolynomialRing(QQ) sage: f = u^4*v^5 sage: derivative(f, u) 4*u^3*v^5 sage: f.derivative(u) # can always use method notation too 4*u^3*v^5
>>> from sage.all import * >>> R = PolynomialRing(QQ, names=('u', 'v',)); (u, v,) = R._first_ngens(2) >>> f = u**Integer(4)*v**Integer(5) >>> derivative(f, u) 4*u^3*v^5 >>> f.derivative(u) # can always use method notation too 4*u^3*v^5
sage: derivative(f, u, u) 12*u^2*v^5 sage: derivative(f, u, u, u) 24*u*v^5 sage: derivative(f, u, 3) 24*u*v^5
>>> from sage.all import * >>> derivative(f, u, u) 12*u^2*v^5 >>> derivative(f, u, u, u) 24*u*v^5 >>> derivative(f, u, Integer(3)) 24*u*v^5
sage: derivative(f, u, v) 20*u^3*v^4 sage: derivative(f, u, 2, v) 60*u^2*v^4 sage: derivative(f, u, v, 2) 80*u^3*v^3 sage: derivative(f, [u, v, v]) 80*u^3*v^3
>>> from sage.all import * >>> derivative(f, u, v) 20*u^3*v^4 >>> derivative(f, u, Integer(2), v) 60*u^2*v^4 >>> derivative(f, u, v, Integer(2)) 80*u^3*v^3 >>> derivative(f, [u, v, v]) 80*u^3*v^3
We differentiate a scalar field on a manifold:
sage: M = Manifold(2, 'M') sage: X.<x,y> = M.chart() sage: f = M.scalar_field(x^2*y, name='f') sage: derivative(f) 1-form df on the 2-dimensional differentiable manifold M sage: derivative(f).display() df = 2*x*y dx + x^2 dy
>>> from sage.all import * >>> M = Manifold(Integer(2), 'M') >>> X = M.chart(names=('x', 'y',)); (x, y,) = X._first_ngens(2) >>> f = M.scalar_field(x**Integer(2)*y, name='f') >>> derivative(f) 1-form df on the 2-dimensional differentiable manifold M >>> derivative(f).display() df = 2*x*y dx + x^2 dy
We differentiate a differentiable form, getting its exterior derivative:
sage: a = M.one_form(-y, x, name='a'); a.display() a = -y dx + x dy sage: derivative(a) 2-form da on the 2-dimensional differentiable manifold M sage: derivative(a).display() da = 2 dx∧dy
>>> from sage.all import * >>> a = M.one_form(-y, x, name='a'); a.display() a = -y dx + x dy >>> derivative(a) 2-form da on the 2-dimensional differentiable manifold M >>> derivative(a).display() da = 2 dx∧dy
- sage.calculus.functional.diff(f, *args, **kwds)[source]¶
The derivative of \(f\).
Repeated differentiation is supported by the syntax given in the examples below.
ALIAS: diff
EXAMPLES: We differentiate a callable symbolic function:
sage: f(x,y) = x*y + sin(x^2) + e^(-x) sage: f (x, y) |--> x*y + e^(-x) + sin(x^2) sage: derivative(f, x) (x, y) |--> 2*x*cos(x^2) + y - e^(-x) sage: derivative(f, y) (x, y) |--> x
>>> from sage.all import * >>> __tmp__=var("x,y"); f = symbolic_expression(x*y + sin(x**Integer(2)) + e**(-x)).function(x,y) >>> f (x, y) |--> x*y + e^(-x) + sin(x^2) >>> derivative(f, x) (x, y) |--> 2*x*cos(x^2) + y - e^(-x) >>> derivative(f, y) (x, y) |--> x
We differentiate a polynomial:
sage: t = polygen(QQ, 't') sage: f = (1-t)^5; f -t^5 + 5*t^4 - 10*t^3 + 10*t^2 - 5*t + 1 sage: derivative(f) -5*t^4 + 20*t^3 - 30*t^2 + 20*t - 5 sage: derivative(f, t) -5*t^4 + 20*t^3 - 30*t^2 + 20*t - 5 sage: derivative(f, t, t) -20*t^3 + 60*t^2 - 60*t + 20 sage: derivative(f, t, 2) -20*t^3 + 60*t^2 - 60*t + 20 sage: derivative(f, 2) -20*t^3 + 60*t^2 - 60*t + 20
>>> from sage.all import * >>> t = polygen(QQ, 't') >>> f = (Integer(1)-t)**Integer(5); f -t^5 + 5*t^4 - 10*t^3 + 10*t^2 - 5*t + 1 >>> derivative(f) -5*t^4 + 20*t^3 - 30*t^2 + 20*t - 5 >>> derivative(f, t) -5*t^4 + 20*t^3 - 30*t^2 + 20*t - 5 >>> derivative(f, t, t) -20*t^3 + 60*t^2 - 60*t + 20 >>> derivative(f, t, Integer(2)) -20*t^3 + 60*t^2 - 60*t + 20 >>> derivative(f, Integer(2)) -20*t^3 + 60*t^2 - 60*t + 20
We differentiate a symbolic expression:
sage: var('a x') (a, x) sage: f = exp(sin(a - x^2))/x sage: derivative(f, x) -2*cos(-x^2 + a)*e^(sin(-x^2 + a)) - e^(sin(-x^2 + a))/x^2 sage: derivative(f, a) cos(-x^2 + a)*e^(sin(-x^2 + a))/x
>>> from sage.all import * >>> var('a x') (a, x) >>> f = exp(sin(a - x**Integer(2)))/x >>> derivative(f, x) -2*cos(-x^2 + a)*e^(sin(-x^2 + a)) - e^(sin(-x^2 + a))/x^2 >>> derivative(f, a) cos(-x^2 + a)*e^(sin(-x^2 + a))/x
Syntax for repeated differentiation:
sage: R.<u, v> = PolynomialRing(QQ) sage: f = u^4*v^5 sage: derivative(f, u) 4*u^3*v^5 sage: f.derivative(u) # can always use method notation too 4*u^3*v^5
>>> from sage.all import * >>> R = PolynomialRing(QQ, names=('u', 'v',)); (u, v,) = R._first_ngens(2) >>> f = u**Integer(4)*v**Integer(5) >>> derivative(f, u) 4*u^3*v^5 >>> f.derivative(u) # can always use method notation too 4*u^3*v^5
sage: derivative(f, u, u) 12*u^2*v^5 sage: derivative(f, u, u, u) 24*u*v^5 sage: derivative(f, u, 3) 24*u*v^5
>>> from sage.all import * >>> derivative(f, u, u) 12*u^2*v^5 >>> derivative(f, u, u, u) 24*u*v^5 >>> derivative(f, u, Integer(3)) 24*u*v^5
sage: derivative(f, u, v) 20*u^3*v^4 sage: derivative(f, u, 2, v) 60*u^2*v^4 sage: derivative(f, u, v, 2) 80*u^3*v^3 sage: derivative(f, [u, v, v]) 80*u^3*v^3
>>> from sage.all import * >>> derivative(f, u, v) 20*u^3*v^4 >>> derivative(f, u, Integer(2), v) 60*u^2*v^4 >>> derivative(f, u, v, Integer(2)) 80*u^3*v^3 >>> derivative(f, [u, v, v]) 80*u^3*v^3
We differentiate a scalar field on a manifold:
sage: M = Manifold(2, 'M') sage: X.<x,y> = M.chart() sage: f = M.scalar_field(x^2*y, name='f') sage: derivative(f) 1-form df on the 2-dimensional differentiable manifold M sage: derivative(f).display() df = 2*x*y dx + x^2 dy
>>> from sage.all import * >>> M = Manifold(Integer(2), 'M') >>> X = M.chart(names=('x', 'y',)); (x, y,) = X._first_ngens(2) >>> f = M.scalar_field(x**Integer(2)*y, name='f') >>> derivative(f) 1-form df on the 2-dimensional differentiable manifold M >>> derivative(f).display() df = 2*x*y dx + x^2 dy
We differentiate a differentiable form, getting its exterior derivative:
sage: a = M.one_form(-y, x, name='a'); a.display() a = -y dx + x dy sage: derivative(a) 2-form da on the 2-dimensional differentiable manifold M sage: derivative(a).display() da = 2 dx∧dy
>>> from sage.all import * >>> a = M.one_form(-y, x, name='a'); a.display() a = -y dx + x dy >>> derivative(a) 2-form da on the 2-dimensional differentiable manifold M >>> derivative(a).display() da = 2 dx∧dy
- sage.calculus.functional.expand(x, *args, **kwds)[source]¶
EXAMPLES:
sage: a = (x-1)*(x^2 - 1); a (x^2 - 1)*(x - 1) sage: expand(a) x^3 - x^2 - x + 1
>>> from sage.all import * >>> a = (x-Integer(1))*(x**Integer(2) - Integer(1)); a (x^2 - 1)*(x - 1) >>> expand(a) x^3 - x^2 - x + 1
You can also use expand on polynomial, integer, and other factorizations:
sage: x = polygen(ZZ) sage: F = factor(x^12 - 1); F (x - 1) * (x + 1) * (x^2 - x + 1) * (x^2 + 1) * (x^2 + x + 1) * (x^4 - x^2 + 1) sage: expand(F) x^12 - 1 sage: F.expand() x^12 - 1 sage: F = factor(2007); F 3^2 * 223 sage: expand(F) 2007
>>> from sage.all import * >>> x = polygen(ZZ) >>> F = factor(x**Integer(12) - Integer(1)); F (x - 1) * (x + 1) * (x^2 - x + 1) * (x^2 + 1) * (x^2 + x + 1) * (x^4 - x^2 + 1) >>> expand(F) x^12 - 1 >>> F.expand() x^12 - 1 >>> F = factor(Integer(2007)); F 3^2 * 223 >>> expand(F) 2007
Note: If you want to compute the expanded form of a polynomial arithmetic operation quickly and the coefficients of the polynomial all lie in some ring, e.g., the integers, it is vastly faster to create a polynomial ring and do the arithmetic there.
sage: x = polygen(ZZ) # polynomial over a given base ring. sage: f = sum(x^n for n in range(5)) sage: f*f # much faster, even if the degree is huge x^8 + 2*x^7 + 3*x^6 + 4*x^5 + 5*x^4 + 4*x^3 + 3*x^2 + 2*x + 1
>>> from sage.all import * >>> x = polygen(ZZ) # polynomial over a given base ring. >>> f = sum(x**n for n in range(Integer(5))) >>> f*f # much faster, even if the degree is huge x^8 + 2*x^7 + 3*x^6 + 4*x^5 + 5*x^4 + 4*x^3 + 3*x^2 + 2*x + 1
- sage.calculus.functional.integral(f, *args, **kwds)[source]¶
The integral of \(f\).
EXAMPLES:
sage: integral(sin(x), x) -cos(x) sage: integral(sin(x)^2, x, pi, 123*pi/2) 121/4*pi sage: integral( sin(x), x, 0, pi) 2
>>> from sage.all import * >>> integral(sin(x), x) -cos(x) >>> integral(sin(x)**Integer(2), x, pi, Integer(123)*pi/Integer(2)) 121/4*pi >>> integral( sin(x), x, Integer(0), pi) 2
We integrate a symbolic function:
sage: f(x,y,z) = x*y/z + sin(z) sage: integral(f, z) (x, y, z) |--> x*y*log(z) - cos(z)
>>> from sage.all import * >>> __tmp__=var("x,y,z"); f = symbolic_expression(x*y/z + sin(z)).function(x,y,z) >>> integral(f, z) (x, y, z) |--> x*y*log(z) - cos(z)
sage: var('a,b') (a, b) sage: assume(b-a>0) sage: integral( sin(x), x, a, b) cos(a) - cos(b) sage: forget()
>>> from sage.all import * >>> var('a,b') (a, b) >>> assume(b-a>Integer(0)) >>> integral( sin(x), x, a, b) cos(a) - cos(b) >>> forget()
sage: integral(x/(x^3-1), x) 1/3*sqrt(3)*arctan(1/3*sqrt(3)*(2*x + 1)) - 1/6*log(x^2 + x + 1) + 1/3*log(x - 1)
>>> from sage.all import * >>> integral(x/(x**Integer(3)-Integer(1)), x) 1/3*sqrt(3)*arctan(1/3*sqrt(3)*(2*x + 1)) - 1/6*log(x^2 + x + 1) + 1/3*log(x - 1)
sage: integral( exp(-x^2), x ) 1/2*sqrt(pi)*erf(x)
>>> from sage.all import * >>> integral( exp(-x**Integer(2)), x ) 1/2*sqrt(pi)*erf(x)
We define the Gaussian, plot and integrate it numerically and symbolically:
sage: f(x) = 1/(sqrt(2*pi)) * e^(-x^2/2) sage: P = plot(f, -4, 4, hue=0.8, thickness=2) # needs sage.plot sage: P.show(ymin=0, ymax=0.4) # needs sage.plot sage: numerical_integral(f, -4, 4) # random output (0.99993665751633376, 1.1101527003413533e-14) sage: integrate(f, x) x |--> 1/2*erf(1/2*sqrt(2)*x)
>>> from sage.all import * >>> __tmp__=var("x"); f = symbolic_expression(Integer(1)/(sqrt(Integer(2)*pi)) * e**(-x**Integer(2)/Integer(2))).function(x) >>> P = plot(f, -Integer(4), Integer(4), hue=RealNumber('0.8'), thickness=Integer(2)) # needs sage.plot >>> P.show(ymin=Integer(0), ymax=RealNumber('0.4')) # needs sage.plot >>> numerical_integral(f, -Integer(4), Integer(4)) # random output (0.99993665751633376, 1.1101527003413533e-14) >>> integrate(f, x) x |--> 1/2*erf(1/2*sqrt(2)*x)
You can have Sage calculate multiple integrals. For example, consider the function \(exp(y^2)\) on the region between the lines \(x=y\), \(x=1\), and \(y=0\). We find the value of the integral on this region using the command:
sage: area = integral(integral(exp(y^2),x,0,y),y,0,1); area 1/2*e - 1/2 sage: float(area) 0.859140914229522...
>>> from sage.all import * >>> area = integral(integral(exp(y**Integer(2)),x,Integer(0),y),y,Integer(0),Integer(1)); area 1/2*e - 1/2 >>> float(area) 0.859140914229522...
We compute the line integral of \(\sin(x)\) along the arc of the curve \(x=y^4\) from \((1,-1)\) to \((1,1)\):
sage: t = var('t') sage: (x,y) = (t^4,t) sage: (dx,dy) = (diff(x,t), diff(y,t)) sage: integral(sin(x)*dx, t,-1, 1) 0 sage: restore('x,y') # restore the symbolic variables x and y
>>> from sage.all import * >>> t = var('t') >>> (x,y) = (t**Integer(4),t) >>> (dx,dy) = (diff(x,t), diff(y,t)) >>> integral(sin(x)*dx, t,-Integer(1), Integer(1)) 0 >>> restore('x,y') # restore the symbolic variables x and y
Sage is now (Issue #27958) able to compute the following integral:
sage: integral(exp(-x^2)*log(x), x) # long time 1/2*sqrt(pi)*erf(x)*log(x) - x*hypergeometric((1/2, 1/2), (3/2, 3/2), -x^2)
>>> from sage.all import * >>> integral(exp(-x**Integer(2))*log(x), x) # long time 1/2*sqrt(pi)*erf(x)*log(x) - x*hypergeometric((1/2, 1/2), (3/2, 3/2), -x^2)
and its value:
sage: integral( exp(-x^2)*ln(x), x, 0, oo) -1/4*sqrt(pi)*(euler_gamma + 2*log(2))
>>> from sage.all import * >>> integral( exp(-x**Integer(2))*ln(x), x, Integer(0), oo) -1/4*sqrt(pi)*(euler_gamma + 2*log(2))
This definite integral is easy:
sage: integral( ln(x)/x, x, 1, 2) 1/2*log(2)^2
>>> from sage.all import * >>> integral( ln(x)/x, x, Integer(1), Integer(2)) 1/2*log(2)^2
Sage cannot do this elliptic integral (yet):
sage: integral(1/sqrt(2*t^4 - 3*t^2 - 2), t, 2, 3) integrate(1/(sqrt(2*t^2 + 1)*sqrt(t^2 - 2)), t, 2, 3)
>>> from sage.all import * >>> integral(Integer(1)/sqrt(Integer(2)*t**Integer(4) - Integer(3)*t**Integer(2) - Integer(2)), t, Integer(2), Integer(3)) integrate(1/(sqrt(2*t^2 + 1)*sqrt(t^2 - 2)), t, 2, 3)
A double integral:
sage: y = var('y') sage: integral(integral(x*y^2, x, 0, y), y, -2, 2) 32/5
>>> from sage.all import * >>> y = var('y') >>> integral(integral(x*y**Integer(2), x, Integer(0), y), y, -Integer(2), Integer(2)) 32/5
This illustrates using assumptions:
sage: integral(abs(x), x, 0, 5) 25/2 sage: a = var("a") sage: integral(abs(x), x, 0, a) 1/2*a*abs(a) sage: integral(abs(x)*x, x, 0, a) 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, negative or zero? sage: assume(a>0) sage: integral(abs(x)*x, x, 0, a) 1/3*a^3 sage: forget() # forget the assumptions.
>>> from sage.all import * >>> integral(abs(x), x, Integer(0), Integer(5)) 25/2 >>> a = var("a") >>> integral(abs(x), x, Integer(0), a) 1/2*a*abs(a) >>> integral(abs(x)*x, x, Integer(0), a) 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, negative or zero? >>> assume(a>Integer(0)) >>> integral(abs(x)*x, x, Integer(0), a) 1/3*a^3 >>> forget() # forget the assumptions.
We integrate and differentiate a huge mess:
sage: f = (x^2-1+3*(1+x^2)^(1/3))/(1+x^2)^(2/3)*x/(x^2+2)^2 sage: g = integral(f, x) sage: h = f - diff(g, x)
>>> from sage.all import * >>> f = (x**Integer(2)-Integer(1)+Integer(3)*(Integer(1)+x**Integer(2))**(Integer(1)/Integer(3)))/(Integer(1)+x**Integer(2))**(Integer(2)/Integer(3))*x/(x**Integer(2)+Integer(2))**Integer(2) >>> g = integral(f, x) >>> h = f - diff(g, x)
sage: [float(h(x=i)) for i in range(5)] #random [0.0, -1.1102230246251565e-16, -5.5511151231257827e-17, -5.5511151231257827e-17, -6.9388939039072284e-17] sage: h.factor() 0 sage: bool(h == 0) True
>>> from sage.all import * >>> [float(h(x=i)) for i in range(Integer(5))] #random [0.0, -1.1102230246251565e-16, -5.5511151231257827e-17, -5.5511151231257827e-17, -6.9388939039072284e-17] >>> h.factor() 0 >>> bool(h == Integer(0)) True
- sage.calculus.functional.integrate(f, *args, **kwds)[source]¶
The integral of \(f\).
EXAMPLES:
sage: integral(sin(x), x) -cos(x) sage: integral(sin(x)^2, x, pi, 123*pi/2) 121/4*pi sage: integral( sin(x), x, 0, pi) 2
>>> from sage.all import * >>> integral(sin(x), x) -cos(x) >>> integral(sin(x)**Integer(2), x, pi, Integer(123)*pi/Integer(2)) 121/4*pi >>> integral( sin(x), x, Integer(0), pi) 2
We integrate a symbolic function:
sage: f(x,y,z) = x*y/z + sin(z) sage: integral(f, z) (x, y, z) |--> x*y*log(z) - cos(z)
>>> from sage.all import * >>> __tmp__=var("x,y,z"); f = symbolic_expression(x*y/z + sin(z)).function(x,y,z) >>> integral(f, z) (x, y, z) |--> x*y*log(z) - cos(z)
sage: var('a,b') (a, b) sage: assume(b-a>0) sage: integral( sin(x), x, a, b) cos(a) - cos(b) sage: forget()
>>> from sage.all import * >>> var('a,b') (a, b) >>> assume(b-a>Integer(0)) >>> integral( sin(x), x, a, b) cos(a) - cos(b) >>> forget()
sage: integral(x/(x^3-1), x) 1/3*sqrt(3)*arctan(1/3*sqrt(3)*(2*x + 1)) - 1/6*log(x^2 + x + 1) + 1/3*log(x - 1)
>>> from sage.all import * >>> integral(x/(x**Integer(3)-Integer(1)), x) 1/3*sqrt(3)*arctan(1/3*sqrt(3)*(2*x + 1)) - 1/6*log(x^2 + x + 1) + 1/3*log(x - 1)
sage: integral( exp(-x^2), x ) 1/2*sqrt(pi)*erf(x)
>>> from sage.all import * >>> integral( exp(-x**Integer(2)), x ) 1/2*sqrt(pi)*erf(x)
We define the Gaussian, plot and integrate it numerically and symbolically:
sage: f(x) = 1/(sqrt(2*pi)) * e^(-x^2/2) sage: P = plot(f, -4, 4, hue=0.8, thickness=2) # needs sage.plot sage: P.show(ymin=0, ymax=0.4) # needs sage.plot sage: numerical_integral(f, -4, 4) # random output (0.99993665751633376, 1.1101527003413533e-14) sage: integrate(f, x) x |--> 1/2*erf(1/2*sqrt(2)*x)
>>> from sage.all import * >>> __tmp__=var("x"); f = symbolic_expression(Integer(1)/(sqrt(Integer(2)*pi)) * e**(-x**Integer(2)/Integer(2))).function(x) >>> P = plot(f, -Integer(4), Integer(4), hue=RealNumber('0.8'), thickness=Integer(2)) # needs sage.plot >>> P.show(ymin=Integer(0), ymax=RealNumber('0.4')) # needs sage.plot >>> numerical_integral(f, -Integer(4), Integer(4)) # random output (0.99993665751633376, 1.1101527003413533e-14) >>> integrate(f, x) x |--> 1/2*erf(1/2*sqrt(2)*x)
You can have Sage calculate multiple integrals. For example, consider the function \(exp(y^2)\) on the region between the lines \(x=y\), \(x=1\), and \(y=0\). We find the value of the integral on this region using the command:
sage: area = integral(integral(exp(y^2),x,0,y),y,0,1); area 1/2*e - 1/2 sage: float(area) 0.859140914229522...
>>> from sage.all import * >>> area = integral(integral(exp(y**Integer(2)),x,Integer(0),y),y,Integer(0),Integer(1)); area 1/2*e - 1/2 >>> float(area) 0.859140914229522...
We compute the line integral of \(\sin(x)\) along the arc of the curve \(x=y^4\) from \((1,-1)\) to \((1,1)\):
sage: t = var('t') sage: (x,y) = (t^4,t) sage: (dx,dy) = (diff(x,t), diff(y,t)) sage: integral(sin(x)*dx, t,-1, 1) 0 sage: restore('x,y') # restore the symbolic variables x and y
>>> from sage.all import * >>> t = var('t') >>> (x,y) = (t**Integer(4),t) >>> (dx,dy) = (diff(x,t), diff(y,t)) >>> integral(sin(x)*dx, t,-Integer(1), Integer(1)) 0 >>> restore('x,y') # restore the symbolic variables x and y
Sage is now (Issue #27958) able to compute the following integral:
sage: integral(exp(-x^2)*log(x), x) # long time 1/2*sqrt(pi)*erf(x)*log(x) - x*hypergeometric((1/2, 1/2), (3/2, 3/2), -x^2)
>>> from sage.all import * >>> integral(exp(-x**Integer(2))*log(x), x) # long time 1/2*sqrt(pi)*erf(x)*log(x) - x*hypergeometric((1/2, 1/2), (3/2, 3/2), -x^2)
and its value:
sage: integral( exp(-x^2)*ln(x), x, 0, oo) -1/4*sqrt(pi)*(euler_gamma + 2*log(2))
>>> from sage.all import * >>> integral( exp(-x**Integer(2))*ln(x), x, Integer(0), oo) -1/4*sqrt(pi)*(euler_gamma + 2*log(2))
This definite integral is easy:
sage: integral( ln(x)/x, x, 1, 2) 1/2*log(2)^2
>>> from sage.all import * >>> integral( ln(x)/x, x, Integer(1), Integer(2)) 1/2*log(2)^2
Sage cannot do this elliptic integral (yet):
sage: integral(1/sqrt(2*t^4 - 3*t^2 - 2), t, 2, 3) integrate(1/(sqrt(2*t^2 + 1)*sqrt(t^2 - 2)), t, 2, 3)
>>> from sage.all import * >>> integral(Integer(1)/sqrt(Integer(2)*t**Integer(4) - Integer(3)*t**Integer(2) - Integer(2)), t, Integer(2), Integer(3)) integrate(1/(sqrt(2*t^2 + 1)*sqrt(t^2 - 2)), t, 2, 3)
A double integral:
sage: y = var('y') sage: integral(integral(x*y^2, x, 0, y), y, -2, 2) 32/5
>>> from sage.all import * >>> y = var('y') >>> integral(integral(x*y**Integer(2), x, Integer(0), y), y, -Integer(2), Integer(2)) 32/5
This illustrates using assumptions:
sage: integral(abs(x), x, 0, 5) 25/2 sage: a = var("a") sage: integral(abs(x), x, 0, a) 1/2*a*abs(a) sage: integral(abs(x)*x, x, 0, a) 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, negative or zero? sage: assume(a>0) sage: integral(abs(x)*x, x, 0, a) 1/3*a^3 sage: forget() # forget the assumptions.
>>> from sage.all import * >>> integral(abs(x), x, Integer(0), Integer(5)) 25/2 >>> a = var("a") >>> integral(abs(x), x, Integer(0), a) 1/2*a*abs(a) >>> integral(abs(x)*x, x, Integer(0), a) 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, negative or zero? >>> assume(a>Integer(0)) >>> integral(abs(x)*x, x, Integer(0), a) 1/3*a^3 >>> forget() # forget the assumptions.
We integrate and differentiate a huge mess:
sage: f = (x^2-1+3*(1+x^2)^(1/3))/(1+x^2)^(2/3)*x/(x^2+2)^2 sage: g = integral(f, x) sage: h = f - diff(g, x)
>>> from sage.all import * >>> f = (x**Integer(2)-Integer(1)+Integer(3)*(Integer(1)+x**Integer(2))**(Integer(1)/Integer(3)))/(Integer(1)+x**Integer(2))**(Integer(2)/Integer(3))*x/(x**Integer(2)+Integer(2))**Integer(2) >>> g = integral(f, x) >>> h = f - diff(g, x)
sage: [float(h(x=i)) for i in range(5)] #random [0.0, -1.1102230246251565e-16, -5.5511151231257827e-17, -5.5511151231257827e-17, -6.9388939039072284e-17] sage: h.factor() 0 sage: bool(h == 0) True
>>> from sage.all import * >>> [float(h(x=i)) for i in range(Integer(5))] #random [0.0, -1.1102230246251565e-16, -5.5511151231257827e-17, -5.5511151231257827e-17, -6.9388939039072284e-17] >>> h.factor() 0 >>> bool(h == Integer(0)) True
- sage.calculus.functional.lim(f, dir=None, taylor=False, **argv)[source]¶
Return the limit as the variable \(v\) approaches \(a\) from the given direction.
limit(expr, x = a) limit(expr, x = a, dir='above')
INPUT:
dir
– (default:None
) dir may have the value'plus'
(or'above'
) for a limit from above,'minus'
(or'below'
) for a limit from below, or may be omitted (implying a two-sided limit is to be computed).taylor
– (default:False
) ifTrue
, use Taylor series, which allows more limits to be computed (but may also crash in some obscure cases due to bugs in Maxima).\*\*argv
– 1 named parameter
ALIAS: You can also use lim instead of limit.
EXAMPLES:
sage: limit(sin(x)/x, x=0) 1 sage: limit(exp(x), x=oo) +Infinity sage: lim(exp(x), x=-oo) 0 sage: lim(1/x, x=0) Infinity sage: limit(sqrt(x^2+x+1)+x, taylor=True, x=-oo) -1/2 sage: limit((tan(sin(x)) - sin(tan(x)))/x^7, taylor=True, x=0) 1/30
>>> from sage.all import * >>> limit(sin(x)/x, x=Integer(0)) 1 >>> limit(exp(x), x=oo) +Infinity >>> lim(exp(x), x=-oo) 0 >>> lim(Integer(1)/x, x=Integer(0)) Infinity >>> limit(sqrt(x**Integer(2)+x+Integer(1))+x, taylor=True, x=-oo) -1/2 >>> limit((tan(sin(x)) - sin(tan(x)))/x**Integer(7), taylor=True, x=Integer(0)) 1/30
Sage does not know how to do this limit (which is 0), so it returns it unevaluated:
sage: lim(exp(x^2)*(1-erf(x)), x=infinity) -limit((erf(x) - 1)*e^(x^2), x, +Infinity)
>>> from sage.all import * >>> lim(exp(x**Integer(2))*(Integer(1)-erf(x)), x=infinity) -limit((erf(x) - 1)*e^(x^2), x, +Infinity)
- sage.calculus.functional.limit(f, dir=None, taylor=False, **argv)[source]¶
Return the limit as the variable \(v\) approaches \(a\) from the given direction.
limit(expr, x = a) limit(expr, x = a, dir='above')
INPUT:
dir
– (default:None
) dir may have the value'plus'
(or'above'
) for a limit from above,'minus'
(or'below'
) for a limit from below, or may be omitted (implying a two-sided limit is to be computed).taylor
– (default:False
) ifTrue
, use Taylor series, which allows more limits to be computed (but may also crash in some obscure cases due to bugs in Maxima).\*\*argv
– 1 named parameter
ALIAS: You can also use lim instead of limit.
EXAMPLES:
sage: limit(sin(x)/x, x=0) 1 sage: limit(exp(x), x=oo) +Infinity sage: lim(exp(x), x=-oo) 0 sage: lim(1/x, x=0) Infinity sage: limit(sqrt(x^2+x+1)+x, taylor=True, x=-oo) -1/2 sage: limit((tan(sin(x)) - sin(tan(x)))/x^7, taylor=True, x=0) 1/30
>>> from sage.all import * >>> limit(sin(x)/x, x=Integer(0)) 1 >>> limit(exp(x), x=oo) +Infinity >>> lim(exp(x), x=-oo) 0 >>> lim(Integer(1)/x, x=Integer(0)) Infinity >>> limit(sqrt(x**Integer(2)+x+Integer(1))+x, taylor=True, x=-oo) -1/2 >>> limit((tan(sin(x)) - sin(tan(x)))/x**Integer(7), taylor=True, x=Integer(0)) 1/30
Sage does not know how to do this limit (which is 0), so it returns it unevaluated:
sage: lim(exp(x^2)*(1-erf(x)), x=infinity) -limit((erf(x) - 1)*e^(x^2), x, +Infinity)
>>> from sage.all import * >>> lim(exp(x**Integer(2))*(Integer(1)-erf(x)), x=infinity) -limit((erf(x) - 1)*e^(x^2), x, +Infinity)
- sage.calculus.functional.simplify(f, algorithm='maxima', **kwds)[source]¶
Simplify the expression \(f\).
See the documentation of the
simplify()
method of symbolic expressions for details on options.EXAMPLES:
We simplify the expression \(i + x - x\):
sage: f = I + x - x; simplify(f) I
>>> from sage.all import * >>> f = I + x - x; simplify(f) I
In fact, printing \(f\) yields the same thing - i.e., the simplified form.
Some simplifications are algorithm-specific:
sage: x, t = var("x, t") sage: ex = 1/2*I*x + 1/2*I*sqrt(x^2 - 1) + 1/2/(I*x + I*sqrt(x^2 - 1)) sage: simplify(ex) 1/2*I*x + 1/2*I*sqrt(x^2 - 1) + 1/(2*I*x + 2*I*sqrt(x^2 - 1)) sage: simplify(ex, algorithm='giac') # needs giac I*sqrt(x^2 - 1)
>>> from sage.all import * >>> x, t = var("x, t") >>> ex = Integer(1)/Integer(2)*I*x + Integer(1)/Integer(2)*I*sqrt(x**Integer(2) - Integer(1)) + Integer(1)/Integer(2)/(I*x + I*sqrt(x**Integer(2) - Integer(1))) >>> simplify(ex) 1/2*I*x + 1/2*I*sqrt(x^2 - 1) + 1/(2*I*x + 2*I*sqrt(x^2 - 1)) >>> simplify(ex, algorithm='giac') # needs giac I*sqrt(x^2 - 1)
- sage.calculus.functional.taylor(f, *args)[source]¶
Expand
self
in a truncated Taylor or Laurent series in the variable \(v\) around the point \(a\), containing terms through \((x - a)^n\). Functions in more variables are also supported.INPUT:
*args
– the following notation is supportedx
,a
,n
– variable, point, degree(x, a)
,(y, b)
, …,n
– variables with points, degree of polynomial
EXAMPLES:
sage: var('x,k,n') (x, k, n) sage: taylor (sqrt (1 - k^2*sin(x)^2), x, 0, 6) -1/720*(45*k^6 - 60*k^4 + 16*k^2)*x^6 - 1/24*(3*k^4 - 4*k^2)*x^4 - 1/2*k^2*x^2 + 1
>>> from sage.all import * >>> var('x,k,n') (x, k, n) >>> taylor (sqrt (Integer(1) - k**Integer(2)*sin(x)**Integer(2)), x, Integer(0), Integer(6)) -1/720*(45*k^6 - 60*k^4 + 16*k^2)*x^6 - 1/24*(3*k^4 - 4*k^2)*x^4 - 1/2*k^2*x^2 + 1
sage: taylor ((x + 1)^n, x, 0, 4) 1/24*(n^4 - 6*n^3 + 11*n^2 - 6*n)*x^4 + 1/6*(n^3 - 3*n^2 + 2*n)*x^3 + 1/2*(n^2 - n)*x^2 + n*x + 1
>>> from sage.all import * >>> taylor ((x + Integer(1))**n, x, Integer(0), Integer(4)) 1/24*(n^4 - 6*n^3 + 11*n^2 - 6*n)*x^4 + 1/6*(n^3 - 3*n^2 + 2*n)*x^3 + 1/2*(n^2 - n)*x^2 + n*x + 1
sage: taylor ((x + 1)^n, x, 0, 4) 1/24*(n^4 - 6*n^3 + 11*n^2 - 6*n)*x^4 + 1/6*(n^3 - 3*n^2 + 2*n)*x^3 + 1/2*(n^2 - n)*x^2 + n*x + 1
>>> from sage.all import * >>> taylor ((x + Integer(1))**n, x, Integer(0), Integer(4)) 1/24*(n^4 - 6*n^3 + 11*n^2 - 6*n)*x^4 + 1/6*(n^3 - 3*n^2 + 2*n)*x^3 + 1/2*(n^2 - n)*x^2 + n*x + 1
Taylor polynomial in two variables:
sage: x,y=var('x y'); taylor(x*y^3,(x,1),(y,-1),4) (x - 1)*(y + 1)^3 - 3*(x - 1)*(y + 1)^2 + (y + 1)^3 + 3*(x - 1)*(y + 1) - 3*(y + 1)^2 - x + 3*y + 3
>>> from sage.all import * >>> x,y=var('x y'); taylor(x*y**Integer(3),(x,Integer(1)),(y,-Integer(1)),Integer(4)) (x - 1)*(y + 1)^3 - 3*(x - 1)*(y + 1)^2 + (y + 1)^3 + 3*(x - 1)*(y + 1) - 3*(y + 1)^2 - x + 3*y + 3