Calculus¶
Calculus-related methods.
This module implements a method to find Euler-Lagrange Equations for given Lagrangian.
- sympy.calculus.euler.euler_equations(L, funcs=(), vars=())[source]¶
Find the Euler-Lagrange equations [R26] for a given Lagrangian.
- Parameters
L : Expr
The Lagrangian that should be a function of the functions listed in the second argument and their derivatives.
For example, in the case of two functions \(f(x,y)\), \(g(x,y)\) and two independent variables \(x\), \(y\) the Lagrangian would have the form:
\[L\left(f(x,y),g(x,y),\frac{\partial f(x,y)}{\partial x}, \frac{\partial f(x,y)}{\partial y}, \frac{\partial g(x,y)}{\partial x}, \frac{\partial g(x,y)}{\partial y},x,y\right)\]In many cases it is not necessary to provide anything, except the Lagrangian, it will be auto-detected (and an error raised if this couldn’t be done).
funcs : Function or an iterable of Functions
The functions that the Lagrangian depends on. The Euler equations are differential equations for each of these functions.
vars : Symbol or an iterable of Symbols
The Symbols that are the independent variables of the functions.
- Returns
eqns : list of Eq
The list of differential equations, one for each function.
Examples
>>> from sympy import Symbol, Function >>> from sympy.calculus.euler import euler_equations >>> x = Function('x') >>> t = Symbol('t') >>> L = (x(t).diff(t))**2/2 - x(t)**2/2 >>> euler_equations(L, x(t), t) [Eq(-x(t) - Derivative(x(t), (t, 2)), 0)] >>> u = Function('u') >>> x = Symbol('x') >>> L = (u(t, x).diff(t))**2/2 - (u(t, x).diff(x))**2/2 >>> euler_equations(L, u(t, x), [t, x]) [Eq(-Derivative(u(t, x), (t, 2)) + Derivative(u(t, x), (x, 2)), 0)]
References
Singularities¶
This module implements algorithms for finding singularities for a function and identifying types of functions.
The differential calculus methods in this module include methods to identify
the following function types in the given Interval
:
- Increasing
- Strictly Increasing
- Decreasing
- Strictly Decreasing
- Monotonic
- sympy.calculus.singularities.is_decreasing(expression, interval=Reals, symbol=None)[source]¶
Return whether the function is decreasing in the given interval.
- Parameters
expression : Expr
The target function which is being checked.
interval : Set, optional
The range of values in which we are testing (defaults to set of all real numbers).
symbol : Symbol, optional
The symbol present in expression which gets varied over the given range.
- Returns
Boolean
True if
expression
is decreasing (either strictly decreasing or constant) in the giveninterval
, False otherwise.
Examples
>>> from sympy import is_decreasing >>> from sympy.abc import x, y >>> from sympy import S, Interval, oo >>> is_decreasing(1/(x**2 - 3*x), Interval.open(1.5, 3)) True >>> is_decreasing(1/(x**2 - 3*x), Interval.Lopen(3, oo)) True >>> is_decreasing(1/(x**2 - 3*x), Interval.Ropen(-oo, S(3)/2)) False >>> is_decreasing(-x**2, Interval(-oo, 0)) False >>> is_decreasing(-x**2 + y, Interval(-oo, 0), x) False
- sympy.calculus.singularities.is_increasing(expression, interval=Reals, symbol=None)[source]¶
Return whether the function is increasing in the given interval.
- Parameters
expression : Expr
The target function which is being checked.
interval : Set, optional
The range of values in which we are testing (defaults to set of all real numbers).
symbol : Symbol, optional
The symbol present in expression which gets varied over the given range.
- Returns
Boolean
True if
expression
is increasing (either strictly increasing or constant) in the giveninterval
, False otherwise.
Examples
>>> from sympy import is_increasing >>> from sympy.abc import x, y >>> from sympy import S, Interval, oo >>> is_increasing(x**3 - 3*x**2 + 4*x, S.Reals) True >>> is_increasing(-x**2, Interval(-oo, 0)) True >>> is_increasing(-x**2, Interval(0, oo)) False >>> is_increasing(4*x**3 - 6*x**2 - 72*x + 30, Interval(-2, 3)) False >>> is_increasing(x**2 + y, Interval(1, 2), x) True
- sympy.calculus.singularities.is_monotonic(expression, interval=Reals, symbol=None)[source]¶
Return whether the function is monotonic in the given interval.
- Parameters
expression : Expr
The target function which is being checked.
interval : Set, optional
The range of values in which we are testing (defaults to set of all real numbers).
symbol : Symbol, optional
The symbol present in expression which gets varied over the given range.
- Returns
Boolean
True if
expression
is monotonic in the giveninterval
, False otherwise.- Raises
NotImplementedError
Monotonicity check has not been implemented for the queried function.
Examples
>>> from sympy import is_monotonic >>> from sympy.abc import x, y >>> from sympy import S, Interval, oo >>> is_monotonic(1/(x**2 - 3*x), Interval.open(1.5, 3)) True >>> is_monotonic(1/(x**2 - 3*x), Interval.Lopen(3, oo)) True >>> is_monotonic(x**3 - 3*x**2 + 4*x, S.Reals) True >>> is_monotonic(-x**2, S.Reals) False >>> is_monotonic(x**2 + y + 1, Interval(1, 2), x) True
- sympy.calculus.singularities.is_strictly_decreasing(expression, interval=Reals, symbol=None)[source]¶
Return whether the function is strictly decreasing in the given interval.
- Parameters
expression : Expr
The target function which is being checked.
interval : Set, optional
The range of values in which we are testing (defaults to set of all real numbers).
symbol : Symbol, optional
The symbol present in expression which gets varied over the given range.
- Returns
Boolean
True if
expression
is strictly decreasing in the giveninterval
, False otherwise.
Examples
>>> from sympy import is_strictly_decreasing >>> from sympy.abc import x, y >>> from sympy import S, Interval, oo >>> is_strictly_decreasing(1/(x**2 - 3*x), Interval.Lopen(3, oo)) True >>> is_strictly_decreasing(1/(x**2 - 3*x), Interval.Ropen(-oo, S(3)/2)) False >>> is_strictly_decreasing(-x**2, Interval(-oo, 0)) False >>> is_strictly_decreasing(-x**2 + y, Interval(-oo, 0), x) False
- sympy.calculus.singularities.is_strictly_increasing(expression, interval=Reals, symbol=None)[source]¶
Return whether the function is strictly increasing in the given interval.
- Parameters
expression : Expr
The target function which is being checked.
interval : Set, optional
The range of values in which we are testing (defaults to set of all real numbers).
symbol : Symbol, optional
The symbol present in expression which gets varied over the given range.
- Returns
Boolean
True if
expression
is strictly increasing in the giveninterval
, False otherwise.
Examples
>>> from sympy import is_strictly_increasing >>> from sympy.abc import x, y >>> from sympy import Interval, oo >>> is_strictly_increasing(4*x**3 - 6*x**2 - 72*x + 30, Interval.Ropen(-oo, -2)) True >>> is_strictly_increasing(4*x**3 - 6*x**2 - 72*x + 30, Interval.Lopen(3, oo)) True >>> is_strictly_increasing(4*x**3 - 6*x**2 - 72*x + 30, Interval.open(-2, 3)) False >>> is_strictly_increasing(-x**2, Interval(0, oo)) False >>> is_strictly_increasing(-x**2 + y, Interval(-oo, 0), x) False
- sympy.calculus.singularities.monotonicity_helper(expression, predicate, interval=Reals, symbol=None)[source]¶
Helper function for functions checking function monotonicity.
- Parameters
expression : Expr
The target function which is being checked
predicate : function
The property being tested for. The function takes in an integer and returns a boolean. The integer input is the derivative and the boolean result should be true if the property is being held, and false otherwise.
interval : Set, optional
The range of values in which we are testing, defaults to all reals.
symbol : Symbol, optional
The symbol present in expression which gets varied over the given range.
It returns a boolean indicating whether the interval in which
the function’s derivative satisfies given predicate is a superset
of the given interval.
- Returns
Boolean
True if
predicate
is true for all the derivatives whensymbol
is varied inrange
, False otherwise.
- sympy.calculus.singularities.singularities(expression, symbol, domain=None)[source]¶
Find singularities of a given function.
- Parameters
expression : Expr
The target function in which singularities need to be found.
symbol : Symbol
The symbol over the values of which the singularity in expression in being searched for.
- Returns
Set
A set of values for
symbol
for whichexpression
has a singularity. AnEmptySet
is returned ifexpression
has no singularities for any given value ofSymbol
.- Raises
NotImplementedError
Methods for determining the singularities of this function have not been developed.
Notes
This function does not find non-isolated singularities nor does it find branch points of the expression.
- Currently supported functions are:
univariate continuous (real or complex) functions
Examples
>>> from sympy.calculus.singularities import singularities >>> from sympy import Symbol, log >>> x = Symbol('x', real=True) >>> y = Symbol('y', real=False) >>> singularities(x**2 + x + 1, x) EmptySet >>> singularities(1/(x + 1), x) {-1} >>> singularities(1/(y**2 + 1), y) {-I, I} >>> singularities(1/(y**3 + 1), y) {-1, 1/2 - sqrt(3)*I/2, 1/2 + sqrt(3)*I/2} >>> singularities(log(x), x) {0}
References
Finite difference weights¶
This module implements an algorithm for efficient generation of finite difference weights for ordinary differentials of functions for derivatives from 0 (interpolation) up to arbitrary order.
The core algorithm is provided in the finite difference weight generating
function (finite_diff_weights
), and two convenience functions are provided
for:
- estimating a derivative (or interpolate) directly from a series of points
is also provided (
apply_finite_diff
).
- differentiating by using finite difference approximations
(
differentiate_finite
).
- sympy.calculus.finite_diff.apply_finite_diff(order, x_list, y_list, x0=0)[source]¶
Calculates the finite difference approximation of the derivative of requested order at
x0
from points provided inx_list
andy_list
.- Parameters
order: int
order of derivative to approximate. 0 corresponds to interpolation.
x_list: sequence
Sequence of (unique) values for the independent variable.
y_list: sequence
The function value at corresponding values for the independent variable in x_list.
x0: Number or Symbol
At what value of the independent variable the derivative should be evaluated. Defaults to 0.
- Returns
sympy.core.add.Add or sympy.core.numbers.Number
The finite difference expression approximating the requested derivative order at
x0
.
Examples
>>> from sympy.calculus import apply_finite_diff >>> cube = lambda arg: (1.0*arg)**3 >>> xlist = range(-3,3+1) >>> apply_finite_diff(2, xlist, map(cube, xlist), 2) - 12 -3.55271367880050e-15
we see that the example above only contain rounding errors. apply_finite_diff can also be used on more abstract objects:
>>> from sympy import IndexedBase, Idx >>> from sympy.calculus import apply_finite_diff >>> x, y = map(IndexedBase, 'xy') >>> i = Idx('i') >>> x_list, y_list = zip(*[(x[i+j], y[i+j]) for j in range(-1,2)]) >>> apply_finite_diff(1, x_list, y_list, x[i]) ((x[i + 1] - x[i])/(-x[i - 1] + x[i]) - 1)*y[i]/(x[i + 1] - x[i]) - (x[i + 1] - x[i])*y[i - 1]/((x[i + 1] - x[i - 1])*(-x[i - 1] + x[i])) + (-x[i - 1] + x[i])*y[i + 1]/((x[i + 1] - x[i - 1])*(x[i + 1] - x[i]))
Notes
Order = 0 corresponds to interpolation. Only supply so many points you think makes sense to around x0 when extracting the derivative (the function need to be well behaved within that region). Also beware of Runge’s phenomenon.
References
Fortran 90 implementation with Python interface for numerics: finitediff
- sympy.calculus.finite_diff.as_finite_diff(derivative, points=1, x0=None, wrt=None)[source]¶
Deprecated function. Use Diff.as_finite_difference instead.
- sympy.calculus.finite_diff.differentiate_finite(expr, *symbols, points=1, x0=None, wrt=None, evaluate=False)[source]¶
Differentiate expr and replace Derivatives with finite differences.
- Parameters
expr : expression
*symbols : differentiate with respect to symbols
points: sequence, coefficient or undefined function, optional
see
Derivative.as_finite_difference
x0: number or Symbol, optional
see
Derivative.as_finite_difference
wrt: Symbol, optional
see
Derivative.as_finite_difference
Examples
>>> from sympy import sin, Function, differentiate_finite >>> from sympy.abc import x, y, h >>> f, g = Function('f'), Function('g') >>> differentiate_finite(f(x)*g(x), x, points=[x-h, x+h]) -f(-h + x)*g(-h + x)/(2*h) + f(h + x)*g(h + x)/(2*h)
differentiate_finite
works on any expression, including the expressions with embedded derivatives:>>> differentiate_finite(f(x) + sin(x), x, 2) -2*f(x) + f(x - 1) + f(x + 1) - 2*sin(x) + sin(x - 1) + sin(x + 1) >>> differentiate_finite(f(x, y), x, y) f(x - 1/2, y - 1/2) - f(x - 1/2, y + 1/2) - f(x + 1/2, y - 1/2) + f(x + 1/2, y + 1/2) >>> differentiate_finite(f(x)*g(x).diff(x), x) (-g(x) + g(x + 1))*f(x + 1/2) - (g(x) - g(x - 1))*f(x - 1/2)
To make finite difference with non-constant discretization step use undefined functions:
>>> dx = Function('dx') >>> differentiate_finite(f(x)*g(x).diff(x), points=dx(x)) -(-g(x - dx(x)/2 - dx(x - dx(x)/2)/2)/dx(x - dx(x)/2) + g(x - dx(x)/2 + dx(x - dx(x)/2)/2)/dx(x - dx(x)/2))*f(x - dx(x)/2)/dx(x) + (-g(x + dx(x)/2 - dx(x + dx(x)/2)/2)/dx(x + dx(x)/2) + g(x + dx(x)/2 + dx(x + dx(x)/2)/2)/dx(x + dx(x)/2))*f(x + dx(x)/2)/dx(x)
- sympy.calculus.finite_diff.finite_diff_weights(order, x_list, x0=1)[source]¶
Calculates the finite difference weights for an arbitrarily spaced one-dimensional grid (
x_list
) for derivatives atx0
of order 0, 1, …, up toorder
using a recursive formula. Order of accuracy is at leastlen(x_list) - order
, ifx_list
is defined correctly.- Parameters
order: int
Up to what derivative order weights should be calculated. 0 corresponds to interpolation.
x_list: sequence
Sequence of (unique) values for the independent variable. It is useful (but not necessary) to order
x_list
from nearest to furthest fromx0
; see examples below.x0: Number or Symbol
Root or value of the independent variable for which the finite difference weights should be generated. Default is
S.One
.- Returns
list
A list of sublists, each corresponding to coefficients for increasing derivative order, and each containing lists of coefficients for increasing subsets of x_list.
Examples
>>> from sympy import S >>> from sympy.calculus import finite_diff_weights >>> res = finite_diff_weights(1, [-S(1)/2, S(1)/2, S(3)/2, S(5)/2], 0) >>> res [[[1, 0, 0, 0], [1/2, 1/2, 0, 0], [3/8, 3/4, -1/8, 0], [5/16, 15/16, -5/16, 1/16]], [[0, 0, 0, 0], [-1, 1, 0, 0], [-1, 1, 0, 0], [-23/24, 7/8, 1/8, -1/24]]] >>> res[0][-1] # FD weights for 0th derivative, using full x_list [5/16, 15/16, -5/16, 1/16] >>> res[1][-1] # FD weights for 1st derivative [-23/24, 7/8, 1/8, -1/24] >>> res[1][-2] # FD weights for 1st derivative, using x_list[:-1] [-1, 1, 0, 0] >>> res[1][-1][0] # FD weight for 1st deriv. for x_list[0] -23/24 >>> res[1][-1][1] # FD weight for 1st deriv. for x_list[1], etc. 7/8
Each sublist contains the most accurate formula at the end. Note, that in the above example
res[1][1]
is the same asres[1][2]
. Since res[1][2] has an order of accuracy oflen(x_list[:3]) - order = 3 - 1 = 2
, the same is true forres[1][1]
!>>> from sympy import S >>> from sympy.calculus import finite_diff_weights >>> res = finite_diff_weights(1, [S(0), S(1), -S(1), S(2), -S(2)], 0)[1] >>> res [[0, 0, 0, 0, 0], [-1, 1, 0, 0, 0], [0, 1/2, -1/2, 0, 0], [-1/2, 1, -1/3, -1/6, 0], [0, 2/3, -2/3, -1/12, 1/12]] >>> res[0] # no approximation possible, using x_list[0] only [0, 0, 0, 0, 0] >>> res[1] # classic forward step approximation [-1, 1, 0, 0, 0] >>> res[2] # classic centered approximation [0, 1/2, -1/2, 0, 0] >>> res[3:] # higher order approximations [[-1/2, 1, -1/3, -1/6, 0], [0, 2/3, -2/3, -1/12, 1/12]]
Let us compare this to a differently defined
x_list
. Pay attention tofoo[i][k]
corresponding to the gridpoint defined byx_list[k]
.>>> from sympy import S >>> from sympy.calculus import finite_diff_weights >>> foo = finite_diff_weights(1, [-S(2), -S(1), S(0), S(1), S(2)], 0)[1] >>> foo [[0, 0, 0, 0, 0], [-1, 1, 0, 0, 0], [1/2, -2, 3/2, 0, 0], [1/6, -1, 1/2, 1/3, 0], [1/12, -2/3, 0, 2/3, -1/12]] >>> foo[1] # not the same and of lower accuracy as res[1]! [-1, 1, 0, 0, 0] >>> foo[2] # classic double backward step approximation [1/2, -2, 3/2, 0, 0] >>> foo[4] # the same as res[4] [1/12, -2/3, 0, 2/3, -1/12]
Note that, unless you plan on using approximations based on subsets of
x_list
, the order of gridpoints does not matter.The capability to generate weights at arbitrary points can be used e.g. to minimize Runge’s phenomenon by using Chebyshev nodes:
>>> from sympy import cos, symbols, pi, simplify >>> from sympy.calculus import finite_diff_weights >>> N, (h, x) = 4, symbols('h x') >>> x_list = [x+h*cos(i*pi/(N)) for i in range(N,-1,-1)] # chebyshev nodes >>> print(x_list) [-h + x, -sqrt(2)*h/2 + x, x, sqrt(2)*h/2 + x, h + x] >>> mycoeffs = finite_diff_weights(1, x_list, 0)[1][4] >>> [simplify(c) for c in mycoeffs] [(h**3/2 + h**2*x - 3*h*x**2 - 4*x**3)/h**4, (-sqrt(2)*h**3 - 4*h**2*x + 3*sqrt(2)*h*x**2 + 8*x**3)/h**4, (6*h**2*x - 8*x**3)/h**4, (sqrt(2)*h**3 - 4*h**2*x - 3*sqrt(2)*h*x**2 + 8*x**3)/h**4, (-h**3/2 + h**2*x + 3*h*x**2 - 4*x**3)/h**4]
Notes
If weights for a finite difference approximation of 3rd order derivative is wanted, weights for 0th, 1st and 2nd order are calculated “for free”, so are formulae using subsets of
x_list
. This is something one can take advantage of to save computational cost. Be aware that one should definex_list
from nearest to furthest fromx0
. If not, subsets ofx_list
will yield poorer approximations, which might not grand an order of accuracy oflen(x_list) - order
.References
- R28
Generation of Finite Difference Formulas on Arbitrarily Spaced Grids, Bengt Fornberg; Mathematics of computation; 51; 184; (1988); 699-706; doi:10.1090/S0025-5718-1988-0935077-0
- class sympy.calculus.util.AccumulationBounds(min, max)[source]¶
# Note AccumulationBounds has an alias: AccumBounds
AccumulationBounds represent an interval \([a, b]\), which is always closed at the ends. Here \(a\) and \(b\) can be any value from extended real numbers.
The intended meaning of AccummulationBounds is to give an approximate location of the accumulation points of a real function at a limit point.
Let \(a\) and \(b\) be reals such that a <= b.
\(\left\langle a, b\right\rangle = \{x \in \mathbb{R} \mid a \le x \le b\}\)
\(\left\langle -\infty, b\right\rangle = \{x \in \mathbb{R} \mid x \le b\} \cup \{-\infty, \infty\}\)
\(\left\langle a, \infty \right\rangle = \{x \in \mathbb{R} \mid a \le x\} \cup \{-\infty, \infty\}\)
\(\left\langle -\infty, \infty \right\rangle = \mathbb{R} \cup \{-\infty, \infty\}\)
\(oo\) and \(-oo\) are added to the second and third definition respectively, since if either \(-oo\) or \(oo\) is an argument, then the other one should be included (though not as an end point). This is forced, since we have, for example, \(1/AccumBounds(0, 1) = AccumBounds(1, oo)\), and the limit at \(0\) is not one-sided. As x tends to \(0-\), then \(1/x -> -oo\), so \(-oo\) should be interpreted as belonging to \(AccumBounds(1, oo)\) though it need not appear explicitly.
In many cases it suffices to know that the limit set is bounded. However, in some other cases more exact information could be useful. For example, all accumulation values of cos(x) + 1 are non-negative. (AccumBounds(-1, 1) + 1 = AccumBounds(0, 2))
A AccumulationBounds object is defined to be real AccumulationBounds, if its end points are finite reals.
Let \(X\), \(Y\) be real AccumulationBounds, then their sum, difference, product are defined to be the following sets:
\(X + Y = \{ x+y \mid x \in X \cap y \in Y\}\)
\(X - Y = \{ x-y \mid x \in X \cap y \in Y\}\)
\(X * Y = \{ x*y \mid x \in X \cap y \in Y\}\)
When an AccumBounds is raised to a negative power, if 0 is contained between the bounds then an infinite range is returned, otherwise if an endpoint is 0 then a semi-infinite range with consistent sign will be returned.
AccumBounds in expressions behave a lot like Intervals but the semantics are not necessarily the same. Division (or exponentiation to a negative integer power) could be handled with intervals by returning a union of the results obtained after splitting the bounds between negatives and positives, but that is not done with AccumBounds. In addition, bounds are assumed to be independent of each other; if the same bound is used in more than one place in an expression, the result may not be the supremum or infimum of the expression (see below). Finally, when a boundary is
1
, exponentiation to the power ofoo
yieldsoo
, neither1
nornan
.Examples
>>> from sympy import AccumBounds, sin, exp, log, pi, E, S, oo >>> from sympy.abc import x
>>> AccumBounds(0, 1) + AccumBounds(1, 2) AccumBounds(1, 3)
>>> AccumBounds(0, 1) - AccumBounds(0, 2) AccumBounds(-2, 1)
>>> AccumBounds(-2, 3)*AccumBounds(-1, 1) AccumBounds(-3, 3)
>>> AccumBounds(1, 2)*AccumBounds(3, 5) AccumBounds(3, 10)
The exponentiation of AccumulationBounds is defined as follows:
If 0 does not belong to \(X\) or \(n > 0\) then
\(X^n = \{ x^n \mid x \in X\}\)
>>> AccumBounds(1, 4)**(S(1)/2) AccumBounds(1, 2)
otherwise, an infinite or semi-infinite result is obtained:
>>> 1/AccumBounds(-1, 1) AccumBounds(-oo, oo) >>> 1/AccumBounds(0, 2) AccumBounds(1/2, oo) >>> 1/AccumBounds(-oo, 0) AccumBounds(-oo, 0)
A boundary of 1 will always generate all nonnegatives:
>>> AccumBounds(1, 2)**oo AccumBounds(0, oo) >>> AccumBounds(0, 1)**oo AccumBounds(0, oo)
If the exponent is itself an AccumulationBounds or is not an integer then unevaluated results will be returned unless the base values are positive:
>>> AccumBounds(2, 3)**AccumBounds(-1, 2) AccumBounds(1/3, 9) >>> AccumBounds(-2, 3)**AccumBounds(-1, 2) AccumBounds(-2, 3)**AccumBounds(-1, 2)
>>> AccumBounds(-2, -1)**(S(1)/2) sqrt(AccumBounds(-2, -1))
Note: \(<a, b>^2\) is not same as \(<a, b>*<a, b>\)
>>> AccumBounds(-1, 1)**2 AccumBounds(0, 1)
>>> AccumBounds(1, 3) < 4 True
>>> AccumBounds(1, 3) < -1 False
Some elementary functions can also take AccumulationBounds as input. A function \(f\) evaluated for some real AccumulationBounds \(<a, b>\) is defined as \(f(\left\langle a, b\right\rangle) = \{ f(x) \mid a \le x \le b \}\)
>>> sin(AccumBounds(pi/6, pi/3)) AccumBounds(1/2, sqrt(3)/2)
>>> exp(AccumBounds(0, 1)) AccumBounds(1, E)
>>> log(AccumBounds(1, E)) AccumBounds(0, 1)
Some symbol in an expression can be substituted for a AccumulationBounds object. But it doesn’t necessarily evaluate the AccumulationBounds for that expression.
The same expression can be evaluated to different values depending upon the form it is used for substitution since each instance of an AccumulationBounds is considered independent. For example:
>>> (x**2 + 2*x + 1).subs(x, AccumBounds(-1, 1)) AccumBounds(-1, 4)
>>> ((x + 1)**2).subs(x, AccumBounds(-1, 1)) AccumBounds(0, 4)
Notes
Do not use
AccumulationBounds
for floating point interval arithmetic calculations, usempmath.iv
instead.References
- property delta¶
Returns the difference of maximum possible value attained by AccumulationBounds object and minimum possible value attained by AccumulationBounds object.
Examples
>>> from sympy import AccumBounds >>> AccumBounds(1, 3).delta 2
- intersection(other)[source]¶
Returns the intersection of ‘self’ and ‘other’. Here other can be an instance of FiniteSet or AccumulationBounds.
- Parameters
other: AccumulationBounds
Another AccumulationBounds object with which the intersection has to be computed.
- Returns
AccumulationBounds
Intersection of ‘self’ and ‘other’.
Examples
>>> from sympy import AccumBounds, FiniteSet >>> AccumBounds(1, 3).intersection(AccumBounds(2, 4)) AccumBounds(2, 3)
>>> AccumBounds(1, 3).intersection(AccumBounds(4, 6)) EmptySet
>>> AccumBounds(1, 4).intersection(FiniteSet(1, 2, 5)) {1, 2}
- property max¶
Returns the maximum possible value attained by AccumulationBounds object.
Examples
>>> from sympy import AccumBounds >>> AccumBounds(1, 3).max 3
- property mid¶
Returns the mean of maximum possible value attained by AccumulationBounds object and minimum possible value attained by AccumulationBounds object.
Examples
>>> from sympy import AccumBounds >>> AccumBounds(1, 3).mid 2
- property min¶
Returns the minimum possible value attained by AccumulationBounds object.
Examples
>>> from sympy import AccumBounds >>> AccumBounds(1, 3).min 1
- sympy.calculus.util.continuous_domain(f, symbol, domain)[source]¶
Returns the intervals in the given domain for which the function is continuous. This method is limited by the ability to determine the various singularities and discontinuities of the given function.
- Parameters
f : Expr
The concerned function.
symbol : Symbol
The variable for which the intervals are to be determined.
domain : Interval
The domain over which the continuity of the symbol has to be checked.
- Returns
Interval
Union of all intervals where the function is continuous.
- Raises
NotImplementedError
If the method to determine continuity of such a function has not yet been developed.
Examples
>>> from sympy import Symbol, S, tan, log, pi, sqrt >>> from sympy.sets import Interval >>> from sympy.calculus.util import continuous_domain >>> x = Symbol('x') >>> continuous_domain(1/x, x, S.Reals) Union(Interval.open(-oo, 0), Interval.open(0, oo)) >>> continuous_domain(tan(x), x, Interval(0, pi)) Union(Interval.Ropen(0, pi/2), Interval.Lopen(pi/2, pi)) >>> continuous_domain(sqrt(x - 2), x, Interval(-5, 5)) Interval(2, 5) >>> continuous_domain(log(2*x - 1), x, S.Reals) Interval.open(1/2, oo)
- sympy.calculus.util.function_range(f, symbol, domain)[source]¶
Finds the range of a function in a given domain. This method is limited by the ability to determine the singularities and determine limits.
- Parameters
f : Expr
The concerned function.
symbol : Symbol
The variable for which the range of function is to be determined.
domain : Interval
The domain under which the range of the function has to be found.
- Returns
Interval
Union of all ranges for all intervals under domain where function is continuous.
- Raises
NotImplementedError
If any of the intervals, in the given domain, for which function is continuous are not finite or real, OR if the critical points of the function on the domain can’t be found.
Examples
>>> from sympy import Symbol, S, exp, log, pi, sqrt, sin, tan >>> from sympy.sets import Interval >>> from sympy.calculus.util import function_range >>> x = Symbol('x') >>> function_range(sin(x), x, Interval(0, 2*pi)) Interval(-1, 1) >>> function_range(tan(x), x, Interval(-pi/2, pi/2)) Interval(-oo, oo) >>> function_range(1/x, x, S.Reals) Union(Interval.open(-oo, 0), Interval.open(0, oo)) >>> function_range(exp(x), x, S.Reals) Interval.open(0, oo) >>> function_range(log(x), x, S.Reals) Interval(-oo, oo) >>> function_range(sqrt(x), x , Interval(-5, 9)) Interval(0, 3)
- sympy.calculus.util.is_convex(f, *syms, domain=Reals)[source]¶
Determines the convexity of the function passed in the argument.
- Parameters
f : Expr
The concerned function.
syms : Tuple of symbols
The variables with respect to which the convexity is to be determined.
domain : Interval, optional
The domain over which the convexity of the function has to be checked. If unspecified, S.Reals will be the default domain.
- Returns
Boolean
The method returns \(True\) if the function is convex otherwise it returns \(False\).
- Raises
NotImplementedError
The check for the convexity of multivariate functions is not implemented yet.
Notes
To determine concavity of a function pass \(-f\) as the concerned function. To determine logarithmic convexity of a function pass log(f) as concerned function. To determine logartihmic concavity of a function pass -log(f) as concerned function.
Currently, convexity check of multivariate functions is not handled.
Examples
>>> from sympy import symbols, exp, oo, Interval >>> from sympy.calculus.util import is_convex >>> x = symbols('x') >>> is_convex(exp(x), x) True >>> is_convex(x**3, x, domain = Interval(-1, oo)) False
References
- sympy.calculus.util.lcim(numbers)[source]¶
Returns the least common integral multiple of a list of numbers.
The numbers can be rational or irrational or a mixture of both. \(None\) is returned for incommensurable numbers.
- Parameters
numbers : list
Numbers (rational and/or irrational) for which lcim is to be found.
- Returns
number
lcim if it exists, otherwise \(None\) for incommensurable numbers.
Examples
>>> from sympy import S, pi >>> from sympy.calculus.util import lcim >>> lcim([S(1)/2, S(3)/4, S(5)/6]) 15/2 >>> lcim([2*pi, 3*pi, pi, pi/2]) 6*pi >>> lcim([S(1), 2*pi])
- sympy.calculus.util.maximum(f, symbol, domain=Reals)[source]¶
Returns the maximum value of a function in the given domain.
- Parameters
f : Expr
The concerned function.
symbol : Symbol
The variable for maximum value needs to be determined.
domain : Interval
The domain over which the maximum have to be checked. If unspecified, then Global maximum is returned.
- Returns
number
Maximum value of the function in given domain.
Examples
>>> from sympy import Symbol, S, sin, cos, pi, maximum >>> from sympy.sets import Interval >>> x = Symbol('x')
>>> f = -x**2 + 2*x + 5 >>> maximum(f, x, S.Reals) 6
>>> maximum(sin(x), x, Interval(-pi, pi/4)) sqrt(2)/2
>>> maximum(sin(x)*cos(x), x) 1/2
- sympy.calculus.util.minimum(f, symbol, domain=Reals)[source]¶
Returns the minimum value of a function in the given domain.
- Parameters
f : Expr
The concerned function.
symbol : Symbol
The variable for minimum value needs to be determined.
domain : Interval
The domain over which the minimum have to be checked. If unspecified, then Global minimum is returned.
- Returns
number
Minimum value of the function in the given domain.
Examples
>>> from sympy import Symbol, S, sin, cos, minimum >>> from sympy.sets import Interval >>> x = Symbol('x')
>>> f = x**2 + 2*x + 5 >>> minimum(f, x, S.Reals) 4
>>> minimum(sin(x), x, Interval(2, 3)) sin(3)
>>> minimum(sin(x)*cos(x), x) -1/2
- sympy.calculus.util.not_empty_in(finset_intersection, *syms)[source]¶
Finds the domain of the functions in \(finite_set\) in which the \(finite_set\) is not-empty
- Parameters
finset_intersection : The unevaluated intersection of FiniteSet containing
real-valued functions with Union of Sets
syms : Tuple of symbols
Symbol for which domain is to be found
- Raises
NotImplementedError
The algorithms to find the non-emptiness of the given FiniteSet are not yet implemented.
ValueError
The input is not valid.
RuntimeError
It is a bug, please report it to the github issue tracker (https://github.com/sympy/sympy/issues).
Examples
>>> from sympy import FiniteSet, Interval, not_empty_in, oo >>> from sympy.abc import x >>> not_empty_in(FiniteSet(x/2).intersect(Interval(0, 1)), x) Interval(0, 2) >>> not_empty_in(FiniteSet(x, x**2).intersect(Interval(1, 2)), x) Union(Interval(1, 2), Interval(-sqrt(2), -1)) >>> not_empty_in(FiniteSet(x**2/(x + 2)).intersect(Interval(1, oo)), x) Union(Interval.Lopen(-2, -1), Interval(2, oo))
- sympy.calculus.util.periodicity(f, symbol, check=False)[source]¶
Tests the given function for periodicity in the given symbol.
- Parameters
f : Expr.
The concerned function.
symbol : Symbol
The variable for which the period is to be determined.
check : Boolean, optional
The flag to verify whether the value being returned is a period or not.
- Returns
period
The period of the function is returned. \(None\) is returned when the function is aperiodic or has a complex period. The value of \(0\) is returned as the period of a constant function.
- Raises
NotImplementedError
The value of the period computed cannot be verified.
Notes
Currently, we do not support functions with a complex period. The period of functions having complex periodic values such as \(exp\), \(sinh\) is evaluated to \(None\).
The value returned might not be the “fundamental” period of the given function i.e. it may not be the smallest periodic value of the function.
The verification of the period through the \(check\) flag is not reliable due to internal simplification of the given expression. Hence, it is set to \(False\) by default.
Examples
>>> from sympy import Symbol, sin, cos, tan, exp >>> from sympy.calculus.util import periodicity >>> x = Symbol('x') >>> f = sin(x) + sin(2*x) + sin(3*x) >>> periodicity(f, x) 2*pi >>> periodicity(sin(x)*cos(x), x) pi >>> periodicity(exp(tan(2*x) - 1), x) pi/2 >>> periodicity(sin(4*x)**cos(2*x), x) pi >>> periodicity(exp(x), x)
- sympy.calculus.util.stationary_points(f, symbol, domain=Reals)[source]¶
Returns the stationary points of a function (where derivative of the function is 0) in the given domain.
- Parameters
f : Expr
The concerned function.
symbol : Symbol
The variable for which the stationary points are to be determined.
domain : Interval
The domain over which the stationary points have to be checked. If unspecified, S.Reals will be the default domain.
- Returns
Set
A set of stationary points for the function. If there are no stationary point, an EmptySet is returned.
Examples
>>> from sympy import Symbol, S, sin, pi, pprint, stationary_points >>> from sympy.sets import Interval >>> x = Symbol('x')
>>> stationary_points(1/x, x, S.Reals) EmptySet
>>> pprint(stationary_points(sin(x), x), use_unicode=False) pi 3*pi {2*n*pi + -- | n in Integers} U {2*n*pi + ---- | n in Integers} 2 2
>>> stationary_points(sin(x),x, Interval(0, 4*pi)) {pi/2, 3*pi/2, 5*pi/2, 7*pi/2}