modelparameters.sympy.calculus package

Submodules

modelparameters.sympy.calculus.euler module

This module implements a method to find Euler-Lagrange Equations for given Lagrangian.

modelparameters.sympy.calculus.euler.euler_equations(L, funcs=(), vars=())[source]

Find the Euler-Lagrange equations [1]_ 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 – The list of differential equations, one for each function.

Return type:

list of Eq

Examples

>>> from .. import Symbol, Function
>>> from .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, t), 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, t) + Derivative(u(t, x), x, x), 0)]

References

modelparameters.sympy.calculus.finite_diff module

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).

modelparameters.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 in x_list and y_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 S(0).

Returns:

The finite difference expression approximating the requested derivative order at x0.

Return type:

sympy.core.add.Add or sympy.core.numbers.Number

Examples

>>> from ..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 .. import IndexedBase, Idx
>>> from ..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.

See also

sympy.calculus.finite_diff.finite_diff_weights

References

Fortran 90 implementation with Python interface for numerics: finitediff

modelparameters.sympy.calculus.finite_diff.as_finite_diff(derivative, points=1, x0=None, wrt=None)

Returns an approximation of a derivative of a function in the form of a finite difference formula. The expression is a weighted sum of the function at a number of discrete values of (one of) the independent variable(s).

Parameters:
  • derivative (a Derivative instance) –

  • points (sequence or coefficient, optional) – If sequence: discrete values (length >= order+1) of the independent variable used for generating the finite difference weights. If it is a coefficient, it will be used as the step-size for generating an equidistant sequence of length order+1 centered around x0. default: 1 (step-size 1)

  • x0 (number or Symbol, optional) – the value of the independent variable (wrt) at which the derivative is to be approximated. Default: same as wrt.

  • wrt (Symbol, optional) – “with respect to” the variable for which the (partial) derivative is to be approximated for. If not provided it is required that the Derivative is ordinary. Default: None.

Examples

>>> from .. import symbols, Function, exp, sqrt, Symbol, as_finite_diff
>>> from ..utilities.exceptions import SymPyDeprecationWarning
>>> import warnings
>>> warnings.simplefilter("ignore", SymPyDeprecationWarning)
>>> x, h = symbols('x h')
>>> f = Function('f')
>>> as_finite_diff(f(x).diff(x))
-f(x - 1/2) + f(x + 1/2)

The default step size and number of points are 1 and order + 1 respectively. We can change the step size by passing a symbol as a parameter:

>>> as_finite_diff(f(x).diff(x), h)
-f(-h/2 + x)/h + f(h/2 + x)/h

We can also specify the discretized values to be used in a sequence:

>>> as_finite_diff(f(x).diff(x), [x, x+h, x+2*h])
-3*f(x)/(2*h) + 2*f(h + x)/h - f(2*h + x)/(2*h)

The algorithm is not restricted to use equidistant spacing, nor do we need to make the approximation around x0, but we can get an expression estimating the derivative at an offset:

>>> e, sq2 = exp(1), sqrt(2)
>>> xl = [x-h, x+h, x+e*h]
>>> as_finite_diff(f(x).diff(x, 1), xl, x+h*sq2)
2*h*((h + sqrt(2)*h)/(2*h) - (-sqrt(2)*h + h)/(2*h))*f(E*h + x)/((-h + E*h)*(h + E*h)) + (-(-sqrt(2)*h + h)/(2*h) - (-sqrt(2)*h + E*h)/(2*h))*f(-h + x)/(h + E*h) + (-(h + sqrt(2)*h)/(2*h) + (-sqrt(2)*h + E*h)/(2*h))*f(h + x)/(-h + E*h)

Partial derivatives are also supported:

>>> y = Symbol('y')
>>> d2fdxdy=f(x,y).diff(x,y)
>>> as_finite_diff(d2fdxdy, wrt=x)
-Derivative(f(x - 1/2, y), y) + Derivative(f(x + 1/2, y), y)

See also

sympy.calculus.finite_diff.apply_finite_diff, sympy.calculus.finite_diff.finite_diff_weights

modelparameters.sympy.calculus.finite_diff.differentiate_finite(expr, *symbols, **kwargs)[source]

Differentiate expr and replace Derivatives with finite differences.

Parameters:
  • expr (expression) –

  • *symbols (differentiate with respect to symbols) –

  • points (sequence or coefficient, optional) – see Derivative.as_finite_difference

  • x0 (number or Symbol, optional) – see Derivative.as_finite_difference

  • wrt (Symbol, optional) – see Derivative.as_finite_difference

  • evaluate (bool) – kwarg passed on to diff, whether or not to evaluate the Derivative intermediately (default: False).

Examples

>>> from .. import cos, sin, Function, differentiate_finite
>>> from ..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)

Note that the above form preserves the product rule in discrete form. If we want we can pass evaluate=True to get another form (which is usually not what we want):

>>> differentiate_finite(f(x)*g(x), x, points=[x-h, x+h], evaluate=True).simplify()
-((f(-h + x) - f(h + x))*g(x) + (g(-h + x) - g(h + x))*f(x))/(2*h)

differentiate_finite works on any expression:

>>> 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) + sin(x), x, 2, evaluate=True)
-2*f(x) + f(x - 1) + f(x + 1) - sin(x)
>>> 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)
modelparameters.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 at x0 of order 0, 1, …, up to order using a recursive formula. Order of accuracy is at least len(x_list) - order, if x_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 from x0; 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:

A list of sublists, each corresponding to coefficients for increasing derivative order, and each containing lists of coefficients for increasing subsets of x_list.

Return type:

list

Examples

>>> from .. import S
>>> from ..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 as res[1][2]. Since res[1][2] has an order of accuracy of len(x_list[:3]) - order = 3 - 1 = 2, the same is true for res[1][1]!

>>> from .. import S
>>> from ..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 to foo[i][k] corresponding to the gridpoint defined by x_list[k].

>>> from .. import S
>>> from ..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 .. import cos, symbols, pi, simplify
>>> from ..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*x/h**2 - 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 define x_list from nearest to farest from x0. If not, subsets of x_list will yield poorer approximations, which might not grand an order of accuracy of len(x_list) - order.

See also

sympy.calculus.finite_diff.apply_finite_diff

References

modelparameters.sympy.calculus.singularities module

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

modelparameters.sympy.calculus.singularities.is_decreasing(expression, interval=S.Reals, symbol=None)[source]

Return whether the function is decreasing in the given interval.

Examples

>>> from .. import is_decreasing
>>> from ..abc import x, y
>>> from .. 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
modelparameters.sympy.calculus.singularities.is_increasing(expression, interval=S.Reals, symbol=None)[source]

Return whether the function is increasing in the given interval.

Examples

>>> from .. import is_increasing
>>> from ..abc import x, y
>>> from .. 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
modelparameters.sympy.calculus.singularities.is_monotonic(expression, interval=S.Reals, symbol=None)[source]

Return whether the function is monotonic in the given interval.

Examples

>>> from .. import is_monotonic
>>> from ..abc import x, y
>>> from .. 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
modelparameters.sympy.calculus.singularities.is_strictly_decreasing(expression, interval=S.Reals, symbol=None)[source]

Return whether the function is strictly decreasing in the given interval.

Examples

>>> from .. import is_strictly_decreasing
>>> from ..abc import x, y
>>> from .. 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
modelparameters.sympy.calculus.singularities.is_strictly_increasing(expression, interval=S.Reals, symbol=None)[source]

Return whether the function is strictly increasing in the given interval.

Examples

>>> from .. import is_strictly_increasing
>>> from ..abc import x, y
>>> from .. 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
modelparameters.sympy.calculus.singularities.monotonicity_helper(expression, predicate, interval=S.Reals, symbol=None)[source]

Helper function for functions checking function monotonicity.

It returns a boolean indicating whether the interval in which the function’s derivative satisfies given predicate is a superset of the given interval.

modelparameters.sympy.calculus.singularities.singularities(expression, symbol)[source]

Find singularities of a given function.

Currently supported functions are: - univariate rational (real or complex) functions

Examples

>>> from .singularities import singularities
>>> from .. import Symbol
>>> 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}

Notes

This function does not find nonisolated singularities nor does it find branch points of the expression.

References

modelparameters.sympy.calculus.util module

modelparameters.sympy.calculus.util.AccumBounds

alias of AccumulationBounds

class modelparameters.sympy.calculus.util.AccumulationBounds(min, max)[source]

Bases: AtomicExpr

# 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.

langle a, brangle = {x in mathbb{R} mid a le x le b}

langle -infty, brangle = {x in mathbb{R} mid x le b} cup {-infty, infty}

langle a, infty rangle = {x in mathbb{R} mid a le x} cup {-infty, infty}

langle -infty, infty 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}

There is, however, no consensus on Interval division.

X / Y = { z mid exists x in X, y in Y mid y neq 0, z = x/y}

Note: According to this definition the quotient of two AccumulationBounds may not be a AccumulationBounds object but rather a union of AccumulationBounds.

Note

The main focus in the interval arithmetic is on the simplest way to calculate upper and lower endpoints for the range of values of a function in one or more variables. These barriers are not necessarily the supremum or infimum, since the precise calculation of those values can be difficult or impossible.

Examples

>>> from .. import AccumBounds, sin, exp, log, pi, E, S, oo
>>> from ..abc import x
>>> AccumBounds(0, 1) + AccumBounds(1, 2)
<1, 3>
>>> AccumBounds(0, 1) - AccumBounds(0, 2)
<-2, 1>
>>> AccumBounds(-2, 3)*AccumBounds(-1, 1)
<-3, 3>
>>> AccumBounds(1, 2)*AccumBounds(3, 5)
<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}

otherwise

X^n = { x^n mid x neq 0, x in X} cup {-infty, infty}

Here for fractional n, the part of X resulting in a complex AccumulationBounds object is neglected.

>>> AccumBounds(-1, 4)**(S(1)/2)
<0, 2>
>>> AccumBounds(1, 2)**2
<1, 4>
>>> AccumBounds(-1, oo)**(-1)
<-oo, oo>

Note: <a, b>^2 is not same as <a, b>*<a, b>

>>> AccumBounds(-1, 1)**2
<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(langle a, brangle) = { f(x) mid a le x le b }

>>> sin(AccumBounds(pi/6, pi/3))
<1/2, sqrt(3)/2>
>>> exp(AccumBounds(0, 1))
<1, E>
>>> log(AccumBounds(1, E))
<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.

Same expression can be evaluated to different values depending upon the form it is used for substituion. For example:

>>> (x**2 + 2*x + 1).subs(x, AccumBounds(-1, 1))
<-1, 4>
>>> ((x + 1)**2).subs(x, AccumBounds(-1, 1))
<0, 4>

References

Notes

Do not use AccumulationBounds for floating point interval arithmetic calculations, use mpmath.iv instead.

default_assumptions = {'commutative': True, 'complex': True, 'hermitian': True, 'imaginary': False, 'real': True}
property delta

Returns the difference of maximum possible value attained by AccumulationBounds object and minimum possible value attained by AccumulationBounds object.

Examples

>>> from .. 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.

Examples

>>> from .. import AccumBounds, FiniteSet
>>> AccumBounds(1, 3).intersection(AccumBounds(2, 4))
<2, 3>
>>> AccumBounds(1, 3).intersection(AccumBounds(4, 6))
EmptySet()
>>> AccumBounds(1, 4).intersection(FiniteSet(1, 2, 5))
{1, 2}
is_commutative = True
is_complex = True
is_hermitian = True
is_imaginary = False
is_real = True
property max

Returns the maximum possible value attained by AccumulationBounds object.

Examples

>>> from .. 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 .. import AccumBounds
>>> AccumBounds(1, 3).mid
2
property min

Returns the minimum possible value attained by AccumulationBounds object.

Examples

>>> from .. import AccumBounds
>>> AccumBounds(1, 3).min
1
union(other)[source]
modelparameters.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.

Examples

>>> from .. import Symbol, S, tan, log, pi, sqrt
>>> from ..sets import Interval
>>> from .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)
modelparameters.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.

Examples

>>> from .. import Symbol, S, exp, log, pi, sqrt, sin, tan
>>> from ..sets import Interval
>>> from .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)
Interval(-oo, 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)
modelparameters.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.

Examples

>>> from .. import S, pi
>>> from .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])
modelparameters.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:

Examples

>>> from .. import FiniteSet, Interval, not_empty_in, oo
>>> from ..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(-sqrt(2), -1), Interval(1, 2))
>>> not_empty_in(FiniteSet(x**2/(x + 2)).intersect(Interval(1, oo)), x)
Union(Interval.Lopen(-2, -1), Interval(2, oo))
modelparameters.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) – The flag to verify whether the value being returned is a period or not.

Returns:

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.

Return type:

period

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 .. import Symbol, sin, cos, tan, exp
>>> from .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)

Module contents

Calculus-related methods.

modelparameters.sympy.calculus.AccumBounds

alias of AccumulationBounds

modelparameters.sympy.calculus.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 in x_list and y_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 S(0).

Returns:

The finite difference expression approximating the requested derivative order at x0.

Return type:

sympy.core.add.Add or sympy.core.numbers.Number

Examples

>>> from ..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 .. import IndexedBase, Idx
>>> from ..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.

See also

sympy.calculus.finite_diff.finite_diff_weights

References

Fortran 90 implementation with Python interface for numerics: finitediff

modelparameters.sympy.calculus.as_finite_diff(derivative, points=1, x0=None, wrt=None)

Returns an approximation of a derivative of a function in the form of a finite difference formula. The expression is a weighted sum of the function at a number of discrete values of (one of) the independent variable(s).

Parameters:
  • derivative (a Derivative instance) –

  • points (sequence or coefficient, optional) – If sequence: discrete values (length >= order+1) of the independent variable used for generating the finite difference weights. If it is a coefficient, it will be used as the step-size for generating an equidistant sequence of length order+1 centered around x0. default: 1 (step-size 1)

  • x0 (number or Symbol, optional) – the value of the independent variable (wrt) at which the derivative is to be approximated. Default: same as wrt.

  • wrt (Symbol, optional) – “with respect to” the variable for which the (partial) derivative is to be approximated for. If not provided it is required that the Derivative is ordinary. Default: None.

Examples

>>> from .. import symbols, Function, exp, sqrt, Symbol, as_finite_diff
>>> from ..utilities.exceptions import SymPyDeprecationWarning
>>> import warnings
>>> warnings.simplefilter("ignore", SymPyDeprecationWarning)
>>> x, h = symbols('x h')
>>> f = Function('f')
>>> as_finite_diff(f(x).diff(x))
-f(x - 1/2) + f(x + 1/2)

The default step size and number of points are 1 and order + 1 respectively. We can change the step size by passing a symbol as a parameter:

>>> as_finite_diff(f(x).diff(x), h)
-f(-h/2 + x)/h + f(h/2 + x)/h

We can also specify the discretized values to be used in a sequence:

>>> as_finite_diff(f(x).diff(x), [x, x+h, x+2*h])
-3*f(x)/(2*h) + 2*f(h + x)/h - f(2*h + x)/(2*h)

The algorithm is not restricted to use equidistant spacing, nor do we need to make the approximation around x0, but we can get an expression estimating the derivative at an offset:

>>> e, sq2 = exp(1), sqrt(2)
>>> xl = [x-h, x+h, x+e*h]
>>> as_finite_diff(f(x).diff(x, 1), xl, x+h*sq2)
2*h*((h + sqrt(2)*h)/(2*h) - (-sqrt(2)*h + h)/(2*h))*f(E*h + x)/((-h + E*h)*(h + E*h)) + (-(-sqrt(2)*h + h)/(2*h) - (-sqrt(2)*h + E*h)/(2*h))*f(-h + x)/(h + E*h) + (-(h + sqrt(2)*h)/(2*h) + (-sqrt(2)*h + E*h)/(2*h))*f(h + x)/(-h + E*h)

Partial derivatives are also supported:

>>> y = Symbol('y')
>>> d2fdxdy=f(x,y).diff(x,y)
>>> as_finite_diff(d2fdxdy, wrt=x)
-Derivative(f(x - 1/2, y), y) + Derivative(f(x + 1/2, y), y)

See also

sympy.calculus.finite_diff.apply_finite_diff, sympy.calculus.finite_diff.finite_diff_weights

modelparameters.sympy.calculus.differentiate_finite(expr, *symbols, **kwargs)[source]

Differentiate expr and replace Derivatives with finite differences.

Parameters:
  • expr (expression) –

  • *symbols (differentiate with respect to symbols) –

  • points (sequence or coefficient, optional) – see Derivative.as_finite_difference

  • x0 (number or Symbol, optional) – see Derivative.as_finite_difference

  • wrt (Symbol, optional) – see Derivative.as_finite_difference

  • evaluate (bool) – kwarg passed on to diff, whether or not to evaluate the Derivative intermediately (default: False).

Examples

>>> from .. import cos, sin, Function, differentiate_finite
>>> from ..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)

Note that the above form preserves the product rule in discrete form. If we want we can pass evaluate=True to get another form (which is usually not what we want):

>>> differentiate_finite(f(x)*g(x), x, points=[x-h, x+h], evaluate=True).simplify()
-((f(-h + x) - f(h + x))*g(x) + (g(-h + x) - g(h + x))*f(x))/(2*h)

differentiate_finite works on any expression:

>>> 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) + sin(x), x, 2, evaluate=True)
-2*f(x) + f(x - 1) + f(x + 1) - sin(x)
>>> 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)
modelparameters.sympy.calculus.euler_equations(L, funcs=(), vars=())[source]

Find the Euler-Lagrange equations [1]_ 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 – The list of differential equations, one for each function.

Return type:

list of Eq

Examples

>>> from .. import Symbol, Function
>>> from .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, t), 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, t) + Derivative(u(t, x), x, x), 0)]

References

modelparameters.sympy.calculus.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 at x0 of order 0, 1, …, up to order using a recursive formula. Order of accuracy is at least len(x_list) - order, if x_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 from x0; 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:

A list of sublists, each corresponding to coefficients for increasing derivative order, and each containing lists of coefficients for increasing subsets of x_list.

Return type:

list

Examples

>>> from .. import S
>>> from ..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 as res[1][2]. Since res[1][2] has an order of accuracy of len(x_list[:3]) - order = 3 - 1 = 2, the same is true for res[1][1]!

>>> from .. import S
>>> from ..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 to foo[i][k] corresponding to the gridpoint defined by x_list[k].

>>> from .. import S
>>> from ..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 .. import cos, symbols, pi, simplify
>>> from ..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*x/h**2 - 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 define x_list from nearest to farest from x0. If not, subsets of x_list will yield poorer approximations, which might not grand an order of accuracy of len(x_list) - order.

See also

sympy.calculus.finite_diff.apply_finite_diff

References

modelparameters.sympy.calculus.is_decreasing(expression, interval=S.Reals, symbol=None)[source]

Return whether the function is decreasing in the given interval.

Examples

>>> from .. import is_decreasing
>>> from ..abc import x, y
>>> from .. 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
modelparameters.sympy.calculus.is_increasing(expression, interval=S.Reals, symbol=None)[source]

Return whether the function is increasing in the given interval.

Examples

>>> from .. import is_increasing
>>> from ..abc import x, y
>>> from .. 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
modelparameters.sympy.calculus.is_monotonic(expression, interval=S.Reals, symbol=None)[source]

Return whether the function is monotonic in the given interval.

Examples

>>> from .. import is_monotonic
>>> from ..abc import x, y
>>> from .. 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
modelparameters.sympy.calculus.is_strictly_decreasing(expression, interval=S.Reals, symbol=None)[source]

Return whether the function is strictly decreasing in the given interval.

Examples

>>> from .. import is_strictly_decreasing
>>> from ..abc import x, y
>>> from .. 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
modelparameters.sympy.calculus.is_strictly_increasing(expression, interval=S.Reals, symbol=None)[source]

Return whether the function is strictly increasing in the given interval.

Examples

>>> from .. import is_strictly_increasing
>>> from ..abc import x, y
>>> from .. 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
modelparameters.sympy.calculus.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:

Examples

>>> from .. import FiniteSet, Interval, not_empty_in, oo
>>> from ..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(-sqrt(2), -1), Interval(1, 2))
>>> not_empty_in(FiniteSet(x**2/(x + 2)).intersect(Interval(1, oo)), x)
Union(Interval.Lopen(-2, -1), Interval(2, oo))
modelparameters.sympy.calculus.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) – The flag to verify whether the value being returned is a period or not.

Returns:

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.

Return type:

period

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 .. import Symbol, sin, cos, tan, exp
>>> from .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)
modelparameters.sympy.calculus.singularities(expression, symbol)[source]

Find singularities of a given function.

Currently supported functions are: - univariate rational (real or complex) functions

Examples

>>> from .singularities import singularities
>>> from .. import Symbol
>>> 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}

Notes

This function does not find nonisolated singularities nor does it find branch points of the expression.

References