modelparameters.sympy.concrete package

Submodules

modelparameters.sympy.concrete.delta module

This module implements sums and products containing the Kronecker Delta function.

References

modelparameters.sympy.concrete.delta.deltaproduct(f, limit)[source]

Handle products containing a KroneckerDelta.

See also

deltasummation, sympy.functions.special.tensor_functions.KroneckerDelta, sympy.concrete.products.product

modelparameters.sympy.concrete.delta.deltasummation(f, limit, no_piecewise=False)[source]

Handle summations containing a KroneckerDelta.

The idea for summation is the following:

  • If we are dealing with a KroneckerDelta expression, i.e. KroneckerDelta(g(x), j), we try to simplify it.

    If we could simplify it, then we sum the resulting expression. We already know we can sum a simplified expression, because only simple KroneckerDelta expressions are involved.

    If we couldn’t simplify it, there are two cases:

    1. The expression is a simple expression: we return the summation, taking care if we are dealing with a Derivative or with a proper KroneckerDelta.

    2. The expression is not simple (i.e. KroneckerDelta(cos(x))): we can do nothing at all.

  • If the expr is a multiplication expr having a KroneckerDelta term:

    First we expand it.

    If the expansion did work, then we try to sum the expansion.

    If not, we try to extract a simple KroneckerDelta term, then we have two cases:

    1. We have a simple KroneckerDelta term, so we return the summation.

    2. We didn’t have a simple term, but we do have an expression with simplified KroneckerDelta terms, so we sum this expression.

Examples

>>> from .. import oo, symbols
>>> from ..abc import k
>>> i, j = symbols('i, j', integer=True, finite=True)
>>> from .delta import deltasummation
>>> from .. import KroneckerDelta, Piecewise
>>> deltasummation(KroneckerDelta(i, k), (k, -oo, oo))
1
>>> deltasummation(KroneckerDelta(i, k), (k, 0, oo))
Piecewise((1, 0 <= i), (0, True))
>>> deltasummation(KroneckerDelta(i, k), (k, 1, 3))
Piecewise((1, (1 <= i) & (i <= 3)), (0, True))
>>> deltasummation(k*KroneckerDelta(i, j)*KroneckerDelta(j, k), (k, -oo, oo))
j*KroneckerDelta(i, j)
>>> deltasummation(j*KroneckerDelta(i, j), (j, -oo, oo))
i
>>> deltasummation(i*KroneckerDelta(i, j), (i, -oo, oo))
j

See also

deltaproduct, sympy.functions.special.tensor_functions.KroneckerDelta, sympy.concrete.sums.summation

modelparameters.sympy.concrete.expr_with_intlimits module

class modelparameters.sympy.concrete.expr_with_intlimits.ExprWithIntLimits(function, *symbols, **assumptions)[source]

Bases: ExprWithLimits

change_index(var, trafo, newvar=None)[source]

Change index of a Sum or Product.

Perform a linear transformation x mapsto a x + b on the index variable x. For a the only values allowed are pm 1. A new variable to be used after the change of index can also be specified.

Usage

change_index(expr, var, trafo, newvar=None) where var specifies the index variable x to transform. The transformation trafo must be linear and given in terms of var. If the optional argument newvar is provided then var gets replaced by newvar in the final expression.

Examples

>>> from .. import Sum, Product, simplify
>>> from ..abc import x, y, a, b, c, d, u, v, i, j, k, l
>>> S = Sum(x, (x, a, b))
>>> S.doit()
-a**2/2 + a/2 + b**2/2 + b/2
>>> Sn = S.change_index(x, x + 1, y)
>>> Sn
Sum(y - 1, (y, a + 1, b + 1))
>>> Sn.doit()
-a**2/2 + a/2 + b**2/2 + b/2
>>> Sn = S.change_index(x, -x, y)
>>> Sn
Sum(-y, (y, -b, -a))
>>> Sn.doit()
-a**2/2 + a/2 + b**2/2 + b/2
>>> Sn = S.change_index(x, x+u)
>>> Sn
Sum(-u + x, (x, a + u, b + u))
>>> Sn.doit()
-a**2/2 - a*u + a/2 + b**2/2 + b*u + b/2 - u*(-a + b + 1) + u
>>> simplify(Sn.doit())
-a**2/2 + a/2 + b**2/2 + b/2
>>> Sn = S.change_index(x, -x - u, y)
>>> Sn
Sum(-u - y, (y, -b - u, -a - u))
>>> Sn.doit()
-a**2/2 - a*u + a/2 + b**2/2 + b*u + b/2 - u*(-a + b + 1) + u
>>> simplify(Sn.doit())
-a**2/2 + a/2 + b**2/2 + b/2
>>> P = Product(i*j**2, (i, a, b), (j, c, d))
>>> P
Product(i*j**2, (i, a, b), (j, c, d))
>>> P2 = P.change_index(i, i+3, k)
>>> P2
Product(j**2*(k - 3), (k, a + 3, b + 3), (j, c, d))
>>> P3 = P2.change_index(j, -j, l)
>>> P3
Product(l**2*(k - 3), (k, a + 3, b + 3), (l, -d, -c))

When dealing with symbols only, we can make a general linear transformation:

>>> Sn = S.change_index(x, u*x+v, y)
>>> Sn
Sum((-v + y)/u, (y, b*u + v, a*u + v))
>>> Sn.doit()
-v*(a*u - b*u + 1)/u + (a**2*u**2/2 + a*u*v + a*u/2 - b**2*u**2/2 - b*u*v + b*u/2 + v)/u
>>> simplify(Sn.doit())
a**2*u/2 + a/2 - b**2*u/2 + b/2

However, the last result can be inconsistent with usual summation where the index increment is always 1. This is obvious as we get back the original value only for u equal +1 or -1.

See also

sympy.concrete.simplification.index, sympy.concrete.simplification.reorder_limit, sympy.concrete.simplification.reorder, sympy.concrete.simplification.reverse_order

default_assumptions = {}
index(x)[source]

Return the index of a dummy variable in the list of limits.

Usage

index(expr, x) returns the index of the dummy variable x in the limits of expr. Note that we start counting with 0 at the inner-most limits tuple.

Examples

>>> from ..abc import x, y, a, b, c, d
>>> from .. import Sum, Product
>>> Sum(x*y, (x, a, b), (y, c, d)).index(x)
0
>>> Sum(x*y, (x, a, b), (y, c, d)).index(y)
1
>>> Product(x*y, (x, a, b), (y, c, d)).index(x)
0
>>> Product(x*y, (x, a, b), (y, c, d)).index(y)
1

See also

reorder_limit, reorder, reverse_order

is_commutative
reorder(*arg)[source]

Reorder limits in a expression containing a Sum or a Product.

Usage

expr.reorder(*arg) reorders the limits in the expression expr according to the list of tuples given by arg. These tuples can contain numerical indices or index variable names or involve both.

Examples

>>> from .. import Sum, Product
>>> from ..abc import x, y, z, a, b, c, d, e, f
>>> Sum(x*y, (x, a, b), (y, c, d)).reorder((x, y))
Sum(x*y, (y, c, d), (x, a, b))
>>> Sum(x*y*z, (x, a, b), (y, c, d), (z, e, f)).reorder((x, y), (x, z), (y, z))
Sum(x*y*z, (z, e, f), (y, c, d), (x, a, b))
>>> P = Product(x*y*z, (x, a, b), (y, c, d), (z, e, f))
>>> P.reorder((x, y), (x, z), (y, z))
Product(x*y*z, (z, e, f), (y, c, d), (x, a, b))

We can also select the index variables by counting them, starting with the inner-most one:

>>> Sum(x**2, (x, a, b), (x, c, d)).reorder((0, 1))
Sum(x**2, (x, c, d), (x, a, b))

And of course we can mix both schemes:

>>> Sum(x*y, (x, a, b), (y, c, d)).reorder((y, x))
Sum(x*y, (y, c, d), (x, a, b))
>>> Sum(x*y, (x, a, b), (y, c, d)).reorder((y, 0))
Sum(x*y, (y, c, d), (x, a, b))

See also

reorder_limit, index, reverse_order

reorder_limit(x, y)[source]

Interchange two limit tuples of a Sum or Product expression.

Usage

expr.reorder_limit(x, y) interchanges two limit tuples. The arguments x and y are integers corresponding to the index variables of the two limits which are to be interchanged. The expression expr has to be either a Sum or a Product.

Examples

>>> from ..abc import x, y, z, a, b, c, d, e, f
>>> from .. import Sum, Product
>>> Sum(x*y*z, (x, a, b), (y, c, d), (z, e, f)).reorder_limit(0, 2)
Sum(x*y*z, (z, e, f), (y, c, d), (x, a, b))
>>> Sum(x**2, (x, a, b), (x, c, d)).reorder_limit(1, 0)
Sum(x**2, (x, c, d), (x, a, b))
>>> Product(x*y*z, (x, a, b), (y, c, d), (z, e, f)).reorder_limit(0, 2)
Product(x*y*z, (z, e, f), (y, c, d), (x, a, b))

See also

index, reorder, reverse_order

exception modelparameters.sympy.concrete.expr_with_intlimits.ReorderError(expr, msg)[source]

Bases: NotImplementedError

Exception raised when trying to reorder dependent limits.

modelparameters.sympy.concrete.expr_with_limits module

class modelparameters.sympy.concrete.expr_with_limits.AddWithLimits(function, *symbols, **assumptions)[source]

Bases: ExprWithLimits

Represents unevaluated oriented additions. Parent class for Integral and Sum.

default_assumptions = {}
is_commutative
class modelparameters.sympy.concrete.expr_with_limits.ExprWithLimits(function, *symbols, **assumptions)[source]

Bases: Expr

as_dummy()[source]

Replace instances of the given dummy variables with explicit dummy counterparts to make clear what are dummy variables and what are real-world symbols in an object.

Examples

>>> from .. import Integral
>>> from ..abc import x, y
>>> Integral(x, (x, x, y), (y, x, y)).as_dummy()
Integral(_x, (_x, x, _y), (_y, x, y))

If the object supperts the “integral at” limit (x,) it is not treated as a dummy, but the explicit form, (x, x) of length 2 does treat the variable as a dummy.

>>> Integral(x, x).as_dummy()
Integral(x, x)
>>> Integral(x, (x, x)).as_dummy()
Integral(_x, (_x, x))

If there were no dummies in the original expression, then the the symbols which cannot be changed by subs() are clearly seen as those with an underscore prefix.

See also

variables

Lists the integration variables

transform

Perform mapping on the integration variable

default_assumptions = {}
property free_symbols

This method returns the symbols in the object, excluding those that take on a specific value (i.e. the dummy symbols).

Examples

>>> from .. import Sum
>>> from ..abc import x, y
>>> Sum(x, (x, y, 1)).free_symbols
{y}
property function

Return the function applied across limits.

Examples

>>> from .. import Integral
>>> from ..abc import x
>>> Integral(x**2, (x,)).function
x**2
is_commutative
property is_number

Return True if the Sum has no free symbols, else False.

property limits

Return the limits of expression.

Examples

>>> from .. import Integral
>>> from ..abc import x, i
>>> Integral(x**i, (i, 1, 3)).limits
((i, 1, 3),)
property variables

Return a list of the dummy variables

>>> from .. import Sum
>>> from ..abc import x, i
>>> Sum(x**i, (i, 1, 3)).variables
[i]

See also

function, limits, free_symbols

as_dummy

Rename dummy variables

transform

Perform mapping on the dummy variable

modelparameters.sympy.concrete.gosper module

Gosper’s algorithm for hypergeometric summation.

modelparameters.sympy.concrete.gosper.gosper_normal(f, g, n, polys=True)[source]

Compute the Gosper’s normal form of f and g.

Given relatively prime univariate polynomials f and g, rewrite their quotient to a normal form defined as follows:

\[\frac{f(n)}{g(n)} = Z \cdot \frac{A(n) C(n+1)}{B(n) C(n)}\]

where Z is an arbitrary constant and A, B, C are monic polynomials in n with the following properties:

  1. gcd(A(n), B(n+h)) = 1 forall h in mathbb{N}

  2. gcd(B(n), C(n+1)) = 1

  3. gcd(A(n), C(n)) = 1

This normal form, or rational factorization in other words, is a crucial step in Gosper’s algorithm and in solving of difference equations. It can be also used to decide if two hypergeometric terms are similar or not.

This procedure will return a tuple containing elements of this factorization in the form (Z*A, B, C).

Examples

>>> from .gosper import gosper_normal
>>> from ..abc import n
>>> gosper_normal(4*n+5, 2*(4*n+1)*(2*n+3), n, polys=False)
(1/4, n + 3/2, n + 1/4)
modelparameters.sympy.concrete.gosper.gosper_sum(f, k)[source]

Gosper’s hypergeometric summation algorithm.

Given a hypergeometric term f such that:

\[s_n = \sum_{k=0}^{n-1} f_k\]

and f(n) doesn’t depend on n, returns g_{n} - g(0) where g_{n+1} - g_n = f_n, or None if s_n can not be expressed in closed form as a sum of hypergeometric terms.

Examples

>>> from .gosper import gosper_sum
>>> from ..functions import factorial
>>> from ..abc import i, n, k
>>> f = (4*k + 1)*factorial(k)/factorial(2*k + 1)
>>> gosper_sum(f, (k, 0, n))
(-factorial(n) + 2*factorial(2*n + 1))/factorial(2*n + 1)
>>> _.subs(n, 2) == sum(f.subs(k, i) for i in [0, 1, 2])
True
>>> gosper_sum(f, (k, 3, n))
(-60*factorial(n) + factorial(2*n + 1))/(60*factorial(2*n + 1))
>>> _.subs(n, 5) == sum(f.subs(k, i) for i in [3, 4, 5])
True

References

modelparameters.sympy.concrete.gosper.gosper_term(f, n)[source]

Compute Gosper’s hypergeometric term for f.

Suppose f is a hypergeometric term such that:

\[s_n = \sum_{k=0}^{n-1} f_k\]

and f_k doesn’t depend on n. Returns a hypergeometric term g_n such that g_{n+1} - g_n = f_n.

Examples

>>> from .gosper import gosper_term
>>> from ..functions import factorial
>>> from ..abc import n
>>> gosper_term((4*n + 1)*factorial(n)/factorial(2*n + 1), n)
(-n - 1/2)/(n + 1/4)

modelparameters.sympy.concrete.guess module

Various algorithms for helping identifying numbers and sequences.

modelparameters.sympy.concrete.guess.find_simple_recurrence(v, A=a, N=n)[source]

Detects and returns a recurrence relation from a sequence of several integer (or rational) terms. The name of the function in the returned expression is ‘a’ by default; the main variable is ‘n’ by default. The smallest index in the returned expression is always n (and never n-1, n-2, etc.).

Examples

>>> from .guess import find_simple_recurrence
>>> from .. import fibonacci
>>> find_simple_recurrence([fibonacci(k) for k in range(12)])
-a(n) - a(n + 1) + a(n + 2)
>>> from .. import Function, Symbol
>>> a = [1, 1, 1]
>>> for k in range(15): a.append(5*a[-1]-3*a[-2]+8*a[-3])
>>> find_simple_recurrence(a, A=Function('f'), N=Symbol('i'))
-8*f(i) + 3*f(i + 1) - 5*f(i + 2) + f(i + 3)
modelparameters.sympy.concrete.guess.find_simple_recurrence_vector(l)[source]

This function is used internally by other functions from the sympy.concrete.guess module. While most users may want to rather use the function find_simple_recurrence when looking for recurrence relations among rational numbers, the current function may still be useful when some post-processing has to be done.

The function returns a vector of length n when a recurrence relation of order n is detected in the sequence of rational numbers v.

If the returned vector has a length 1, then the returned value is always the list [0], which means that no relation has been found.

While the functions is intended to be used with rational numbers, it should work for other kinds of real numbers except for some cases involving quadratic numbers; for that reason it should be used with some caution when the argument is not a list of rational numbers.

Examples

>>> from .guess import find_simple_recurrence_vector
>>> from .. import fibonacci
>>> find_simple_recurrence_vector([fibonacci(k) for k in range(12)])
[1, -1, -1]

See also

See, user-friendly.

modelparameters.sympy.concrete.guess.guess(l, all=False, evaluate=True, niter=2, variables=None)[source]

This function is adapted from the Rate.m package for Mathematica written by Christian Krattenthaler. It tries to guess a formula from a given sequence of rational numbers.

In order to speed up the process, the ‘all’ variable is set to False by default, stopping the computation as some results are returned during an iteration; the variable can be set to True if more iterations are needed (other formulas may be found; however they may be equivalent to the first ones).

Another option is the ‘evaluate’ variable (default is True); setting it to False will leave the involved products unevaluated.

By default, the number of iterations is set to 2 but a greater value (up to len(l)-1) can be specified with the optional ‘niter’ variable. More and more convoluted results are found when the order of the iteration gets higher:

  • first iteration returns polynomial or rational functions;

  • second iteration returns products of rising factorials and their inverses;

  • third iteration returns products of products of rising factorials and their inverses;

  • etc.

The returned formulas contain symbols i0, i1, i2, … where the main variables is i0 (and auxiliary variables are i1, i2, …). A list of other symbols can be provided in the ‘variables’ option; the length of the least should be the value of ‘niter’ (more is acceptable but only the first symbols will be used); in this case, the main variable will be the first symbol in the list.

>>> from .guess import guess
>>> guess([1,2,6,24,120], evaluate=False)
[Product(i1 + 1, (i1, 1, i0 - 1))]
>>> from .. import symbols
>>> r = guess([1,2,7,42,429,7436,218348,10850216], niter=4)
>>> i0 = symbols("i0")
>>> [r[0].subs(i0,n).doit() for n in range(1,10)]
[1, 2, 7, 42, 429, 7436, 218348, 10850216, 911835460]
modelparameters.sympy.concrete.guess.guess_generating_function(v, X=x, types=['all'], maxsqrtn=2)[source]

Tries to “guess” a generating function for a sequence of rational numbers v. Only a few patterns are implemented yet.

The function returns a dictionary where keys are the name of a given type of generating function. Six types are currently implemented:

type | formal definition

——-+—————————————————————- ogf | f(x) = Sum( a_k * x^k , k: 0..infinity ) egf | f(x) = Sum( a_k * x^k / k! , k: 0..infinity ) lgf | f(x) = Sum( (-1)^(k+1) a_k * x^k / k , k: 1..infinity )

(with initial index being hold as 1 rather than 0)
hlgf | f(x) = Sum( a_k * x^k / k , k: 1..infinity )
(with initial index being hold as 1 rather than 0)

lgdogf | f(x) = derivate( log(Sum( a_k * x^k, k: 0..infinity )), x) lgdegf | f(x) = derivate( log(Sum( a_k * x^k / k!, k: 0..infinity )), x)

In order to spare time, the user can select only some types of generating functions (default being [‘all’]). While forgetting to use a list in the case of a single type may seem to work most of the time as in: types=’ogf’ this (convenient) syntax may lead to unexpected extra results in some cases.

Discarding a type when calling the function does not mean that the type will not be present in the returned dictionary; it only means that no extra computation will be performed for that type, but the function may still add it in the result when it can be easily converted from another type.

Two generating functions (lgdogf and lgdegf) are not even computed if the initial term of the sequence is 0; it may be useful in that case to try again after having removed the leading zeros.

Examples

>>> from .guess import guess_generating_function as ggf
>>> ggf([k+1 for k in range(12)], types=['ogf', 'lgf', 'hlgf'])
{'hlgf': 1/(-x + 1), 'lgf': 1/(x + 1), 'ogf': 1/(x**2 - 2*x + 1)}
>>> from .. import sympify
>>> l = sympify("[3/2, 11/2, 0, -121/2, -363/2, 121]")
>>> ggf(l)
{'ogf': (x + 3/2)/(11*x**2 - 3*x + 1)}
>>> from .. import fibonacci
>>> ggf([fibonacci(k) for k in range(5, 15)], types=['ogf'])
{'ogf': (3*x + 5)/(-x**2 - x + 1)}
>>> from .. import simplify, factorial
>>> ggf([factorial(k) for k in range(12)], types=['ogf', 'egf', 'lgf'])
{'egf': 1/(-x + 1)}
>>> ggf([k+1 for k in range(12)], types=['egf'])
{'egf': (x + 1)*exp(x), 'lgdegf': (x + 2)/(x + 1)}

N-th root of a rational function can also be detected (below is an example coming from the sequence A108626 from http://oeis.org). The greatest n-th root to be tested is specified as maxsqrtn (default 2).

>>> ggf([1, 2, 5, 14, 41, 124, 383, 1200, 3799, 12122, 38919])['ogf']
sqrt(1/(x**4 + 2*x**2 - 4*x + 1))

References

“Concrete Mathematics”, R.L. Graham, D.E. Knuth, O. Patashnik https://oeis.org/wiki/Generating_functions

modelparameters.sympy.concrete.guess.guess_generating_function_rational(v, X=x)[source]

Tries to “guess” a rational generating function for a sequence of rational numbers v.

Examples

>>> from .guess import guess_generating_function_rational
>>> from .. import fibonacci
>>> l = [fibonacci(k) for k in range(5,15)]
>>> guess_generating_function_rational(l)
(3*x + 5)/(-x**2 - x + 1)

See also

See

modelparameters.sympy.concrete.guess.rationalize(x, maxcoeff=10000)[source]

Helps identifying a rational number from a float (or mpmath.mpf) value by using a continued fraction. The algorithm stops as soon as a large partial quotient is detected (greater than 10000 by default).

Examples

>>> from .guess import rationalize
>>> from mpmath import cos, pi
>>> rationalize(cos(pi/3))
1/2
>>> from mpmath import mpf
>>> rationalize(mpf("0.333333333333333"))
1/3

While the function is rather intended to help ‘identifying’ rational values, it may be used in some cases for approximating real numbers. (Though other functions may be more relevant in that case.)

>>> rationalize(pi, maxcoeff = 250)
355/113

See also

Several, like, The, that, global, rational, the, with, expansion, when, on, other

modelparameters.sympy.concrete.products module

class modelparameters.sympy.concrete.products.Product(function, *symbols, **assumptions)[source]

Bases: ExprWithIntLimits

Represents unevaluated products.

Product represents a finite or infinite product, with the first argument being the general form of terms in the series, and the second argument being (dummy_variable, start, end), with dummy_variable taking all integer values from start through end. In accordance with long-standing mathematical convention, the end term is included in the product.

Finite products

For finite products (and products with symbolic limits assumed to be finite) we follow the analogue of the summation convention described by Karr [1], especially definition 3 of section 1.4. The product:

\[\prod_{m \leq i < n} f(i)\]

has the obvious meaning for m < n, namely:

\[\prod_{m \leq i < n} f(i) = f(m) f(m+1) \cdot \ldots \cdot f(n-2) f(n-1)\]

with the upper limit value f(n) excluded. The product over an empty set is one if and only if m = n:

\[\prod_{m \leq i < n} f(i) = 1 \quad \mathrm{for} \quad m = n\]

Finally, for all other products over empty sets we assume the following definition:

\[\prod_{m \leq i < n} f(i) = \frac{1}{\prod_{n \leq i < m} f(i)} \quad \mathrm{for} \quad m > n\]

It is important to note that above we define all products with the upper limit being exclusive. This is in contrast to the usual mathematical notation, but does not affect the product convention. Indeed we have:

\[\prod_{m \leq i < n} f(i) = \prod_{i = m}^{n - 1} f(i)\]

where the difference in notation is intentional to emphasize the meaning, with limits typeset on the top being inclusive.

Examples

>>> from ..abc import a, b, i, k, m, n, x
>>> from .. import Product, factorial, oo
>>> Product(k, (k, 1, m))
Product(k, (k, 1, m))
>>> Product(k, (k, 1, m)).doit()
factorial(m)
>>> Product(k**2,(k, 1, m))
Product(k**2, (k, 1, m))
>>> Product(k**2,(k, 1, m)).doit()
factorial(m)**2

Wallis’ product for pi:

>>> W = Product(2*i/(2*i-1) * 2*i/(2*i+1), (i, 1, oo))
>>> W
Product(4*i**2/((2*i - 1)*(2*i + 1)), (i, 1, oo))

Direct computation currently fails:

>>> W.doit()
Product(4*i**2/((2*i - 1)*(2*i + 1)), (i, 1, oo))

But we can approach the infinite product by a limit of finite products:

>>> from .. import limit
>>> W2 = Product(2*i/(2*i-1)*2*i/(2*i+1), (i, 1, n))
>>> W2
Product(4*i**2/((2*i - 1)*(2*i + 1)), (i, 1, n))
>>> W2e = W2.doit()
>>> W2e
2**(-2*n)*4**n*factorial(n)**2/(RisingFactorial(1/2, n)*RisingFactorial(3/2, n))
>>> limit(W2e, n, oo)
pi/2

By the same formula we can compute sin(pi/2):

>>> from .. import pi, gamma, simplify
>>> P = pi * x * Product(1 - x**2/k**2, (k, 1, n))
>>> P = P.subs(x, pi/2)
>>> P
pi**2*Product(1 - pi**2/(4*k**2), (k, 1, n))/2
>>> Pe = P.doit()
>>> Pe
pi**2*RisingFactorial(1 + pi/2, n)*RisingFactorial(-pi/2 + 1, n)/(2*factorial(n)**2)
>>> Pe = Pe.rewrite(gamma)
>>> Pe
pi**2*gamma(n + 1 + pi/2)*gamma(n - pi/2 + 1)/(2*gamma(1 + pi/2)*gamma(-pi/2 + 1)*gamma(n + 1)**2)
>>> Pe = simplify(Pe)
>>> Pe
sin(pi**2/2)*gamma(n + 1 + pi/2)*gamma(n - pi/2 + 1)/gamma(n + 1)**2
>>> limit(Pe, n, oo)
sin(pi**2/2)

Products with the lower limit being larger than the upper one:

>>> Product(1/i, (i, 6, 1)).doit()
120
>>> Product(i, (i, 2, 5)).doit()
120

The empty product:

>>> Product(i, (i, n, n-1)).doit()
1

An example showing that the symbolic result of a product is still valid for seemingly nonsensical values of the limits. Then the Karr convention allows us to give a perfectly valid interpretation to those products by interchanging the limits according to the above rules:

>>> P = Product(2, (i, 10, n)).doit()
>>> P
2**(n - 9)
>>> P.subs(n, 5)
1/16
>>> Product(2, (i, 10, 5)).doit()
1/16
>>> 1/Product(2, (i, 6, 9)).doit()
1/16

An explicit example of the Karr summation convention applied to products:

>>> P1 = Product(x, (i, a, b)).doit()
>>> P1
x**(-a + b + 1)
>>> P2 = Product(x, (i, b+1, a-1)).doit()
>>> P2
x**(a - b - 1)
>>> simplify(P1 * P2)
1

And another one:

>>> P1 = Product(i, (i, b, a)).doit()
>>> P1
RisingFactorial(b, a - b + 1)
>>> P2 = Product(i, (i, a+1, b-1)).doit()
>>> P2
RisingFactorial(a + 1, -a + b - 1)
>>> P1 * P2
RisingFactorial(b, a - b + 1)*RisingFactorial(a + 1, -a + b - 1)
>>> simplify(P1 * P2)
1

See also

Sum, summation, product

References

default_assumptions = {}
doit(**hints)[source]

Evaluate objects that are not evaluated by default like limits, integrals, sums and products. All objects of this kind will be evaluated recursively, unless some species were excluded via ‘hints’ or unless the ‘deep’ hint was set to ‘False’.

>>> from .. import Integral
>>> from ..abc import x
>>> 2*Integral(x, x)
2*Integral(x, x)
>>> (2*Integral(x, x)).doit()
x**2
>>> (2*Integral(x, x)).doit(deep=False)
2*Integral(x, x)
property function

Return the function applied across limits.

Examples

>>> from .. import Integral
>>> from ..abc import x
>>> Integral(x**2, (x,)).function
x**2

See also

limits, variables, free_symbols

is_commutative
is_convergent()[source]

See docs of Sum.is_convergent() for explanation of convergence in SymPy.

The infinite product:

\[\prod_{1 \leq i < \infty} f(i)\]

is defined by the sequence of partial products:

\[\prod_{i=1}^{n} f(i) = f(1) f(2) \cdots f(n)\]

as n increases without bound. The product converges to a non-zero value if and only if the sum:

\[\sum_{1 \leq i < \infty} \log{f(n)}\]

converges.

References

Examples

>>> from .. import Interval, S, Product, Symbol, cos, pi, exp, oo
>>> n = Symbol('n', integer=True)
>>> Product(n/(n + 1), (n, 1, oo)).is_convergent()
False
>>> Product(1/n**2, (n, 1, oo)).is_convergent()
False
>>> Product(cos(pi/n), (n, 1, oo)).is_convergent()
True
>>> Product(exp(-n**2), (n, 1, oo)).is_convergent()
False
reverse_order(*indices)[source]

Reverse the order of a limit in a Product.

Usage

reverse_order(expr, *indices) reverses some limits in the expression expr which can be either a Sum or a Product. The selectors in the argument indices specify some indices whose limits get reversed. These selectors are either variable names or numerical indices counted starting from the inner-most limit tuple.

Examples

>>> from .. import Product, simplify, RisingFactorial, gamma, Sum
>>> from ..abc import x, y, a, b, c, d
>>> P = Product(x, (x, a, b))
>>> Pr = P.reverse_order(x)
>>> Pr
Product(1/x, (x, b + 1, a - 1))
>>> Pr = Pr.doit()
>>> Pr
1/RisingFactorial(b + 1, a - b - 1)
>>> simplify(Pr)
gamma(b + 1)/gamma(a)
>>> P = P.doit()
>>> P
RisingFactorial(a, -a + b + 1)
>>> simplify(P)
gamma(b + 1)/gamma(a)

While one should prefer variable names when specifying which limits to reverse, the index counting notation comes in handy in case there are several symbols with the same name.

>>> S = Sum(x*y, (x, a, b), (y, c, d))
>>> S
Sum(x*y, (x, a, b), (y, c, d))
>>> S0 = S.reverse_order(0)
>>> S0
Sum(-x*y, (x, b + 1, a - 1), (y, c, d))
>>> S1 = S0.reverse_order(1)
>>> S1
Sum(x*y, (x, b + 1, a - 1), (y, d + 1, c - 1))

Of course we can mix both notations:

>>> Sum(x*y, (x, a, b), (y, 2, 5)).reverse_order(x, 1)
Sum(x*y, (x, b + 1, a - 1), (y, 6, 1))
>>> Sum(x*y, (x, a, b), (y, 2, 5)).reverse_order(y, x)
Sum(x*y, (x, b + 1, a - 1), (y, 6, 1))

See also

index, reorder_limit, reorder

References

property term
modelparameters.sympy.concrete.products.product(*args, **kwargs)[source]

Compute the product.

The notation for symbols is similar to the notation used in Sum or Integral. product(f, (i, a, b)) computes the product of f with respect to i from a to b, i.e.,

                             b
                           _____
product(f(n), (i, a, b)) = |   | f(n)
                           |   |
                           i = a

If it cannot compute the product, it returns an unevaluated Product object. Repeated products can be computed by introducing additional symbols tuples:

>>> from .. import product, symbols
>>> i, n, m, k = symbols('i n m k', integer=True)
>>> product(i, (i, 1, k))
factorial(k)
>>> product(m, (i, 1, k))
m**k
>>> product(i, (i, 1, k), (k, 1, n))
Product(factorial(k), (k, 1, n))

modelparameters.sympy.concrete.summations module

class modelparameters.sympy.concrete.summations.Sum(function, *symbols, **assumptions)[source]

Bases: AddWithLimits, ExprWithIntLimits

Represents unevaluated summation.

Sum represents a finite or infinite series, with the first argument being the general form of terms in the series, and the second argument being (dummy_variable, start, end), with dummy_variable taking all integer values from start through end. In accordance with long-standing mathematical convention, the end term is included in the summation.

Finite sums

For finite sums (and sums with symbolic limits assumed to be finite) we follow the summation convention described by Karr [1], especially definition 3 of section 1.4. The sum:

\[\sum_{m \leq i < n} f(i)\]

has the obvious meaning for m < n, namely:

\[\sum_{m \leq i < n} f(i) = f(m) + f(m+1) + \ldots + f(n-2) + f(n-1)\]

with the upper limit value f(n) excluded. The sum over an empty set is zero if and only if m = n:

\[\sum_{m \leq i < n} f(i) = 0 \quad \mathrm{for} \quad m = n\]

Finally, for all other sums over empty sets we assume the following definition:

\[\sum_{m \leq i < n} f(i) = - \sum_{n \leq i < m} f(i) \quad \mathrm{for} \quad m > n\]

It is important to note that Karr defines all sums with the upper limit being exclusive. This is in contrast to the usual mathematical notation, but does not affect the summation convention. Indeed we have:

\[\sum_{m \leq i < n} f(i) = \sum_{i = m}^{n - 1} f(i)\]

where the difference in notation is intentional to emphasize the meaning, with limits typeset on the top being inclusive.

Examples

>>> from ..abc import i, k, m, n, x
>>> from .. import Sum, factorial, oo, IndexedBase, Function
>>> Sum(k, (k, 1, m))
Sum(k, (k, 1, m))
>>> Sum(k, (k, 1, m)).doit()
m**2/2 + m/2
>>> Sum(k**2, (k, 1, m))
Sum(k**2, (k, 1, m))
>>> Sum(k**2, (k, 1, m)).doit()
m**3/3 + m**2/2 + m/6
>>> Sum(x**k, (k, 0, oo))
Sum(x**k, (k, 0, oo))
>>> Sum(x**k, (k, 0, oo)).doit()
Piecewise((1/(-x + 1), Abs(x) < 1), (Sum(x**k, (k, 0, oo)), True))
>>> Sum(x**k/factorial(k), (k, 0, oo)).doit()
exp(x)

Here are examples to do summation with symbolic indices. You can use either Function of IndexedBase classes:

>>> f = Function('f')
>>> Sum(f(n), (n, 0, 3)).doit()
f(0) + f(1) + f(2) + f(3)
>>> Sum(f(n), (n, 0, oo)).doit()
Sum(f(n), (n, 0, oo))
>>> f = IndexedBase('f')
>>> Sum(f[n]**2, (n, 0, 3)).doit()
f[0]**2 + f[1]**2 + f[2]**2 + f[3]**2

An example showing that the symbolic result of a summation is still valid for seemingly nonsensical values of the limits. Then the Karr convention allows us to give a perfectly valid interpretation to those sums by interchanging the limits according to the above rules:

>>> S = Sum(i, (i, 1, n)).doit()
>>> S
n**2/2 + n/2
>>> S.subs(n, -4)
6
>>> Sum(i, (i, 1, -4)).doit()
6
>>> Sum(-i, (i, -3, 0)).doit()
6

An explicit example of the Karr summation convention:

>>> S1 = Sum(i**2, (i, m, m+n-1)).doit()
>>> S1
m**2*n + m*n**2 - m*n + n**3/3 - n**2/2 + n/6
>>> S2 = Sum(i**2, (i, m+n, m-1)).doit()
>>> S2
-m**2*n - m*n**2 + m*n - n**3/3 + n**2/2 - n/6
>>> S1 + S2
0
>>> S3 = Sum(i, (i, m, m-1)).doit()
>>> S3
0

See also

summation, Product, product

References

default_assumptions = {}
doit(**hints)[source]

Evaluate objects that are not evaluated by default like limits, integrals, sums and products. All objects of this kind will be evaluated recursively, unless some species were excluded via ‘hints’ or unless the ‘deep’ hint was set to ‘False’.

>>> from .. import Integral
>>> from ..abc import x
>>> 2*Integral(x, x)
2*Integral(x, x)
>>> (2*Integral(x, x)).doit()
x**2
>>> (2*Integral(x, x)).doit(deep=False)
2*Integral(x, x)
euler_maclaurin(m=0, n=0, eps=0, eval_integral=True)[source]

Return an Euler-Maclaurin approximation of self, where m is the number of leading terms to sum directly and n is the number of terms in the tail.

With m = n = 0, this is simply the corresponding integral plus a first-order endpoint correction.

Returns (s, e) where s is the Euler-Maclaurin approximation and e is the estimated error (taken to be the magnitude of the first omitted term in the tail):

>>> from ..abc import k, a, b
>>> from .. import Sum
>>> Sum(1/k, (k, 2, 5)).doit().evalf()
1.28333333333333
>>> s, e = Sum(1/k, (k, 2, 5)).euler_maclaurin()
>>> s
-log(2) + 7/20 + log(5)
>>> from .. import sstr
>>> print(sstr((s.evalf(), e.evalf()), full_prec=True))
(1.26629073187415, 0.0175000000000000)

The endpoints may be symbolic:

>>> s, e = Sum(1/k, (k, a, b)).euler_maclaurin()
>>> s
-log(a) + log(b) + 1/(2*b) + 1/(2*a)
>>> e
Abs(1/(12*b**2) - 1/(12*a**2))

If the function is a polynomial of degree at most 2n+1, the Euler-Maclaurin formula becomes exact (and e = 0 is returned):

>>> Sum(k, (k, 2, b)).euler_maclaurin()
(b**2/2 + b/2 - 1, 0)
>>> Sum(k, (k, 2, b)).doit()
b**2/2 + b/2 - 1

With a nonzero eps specified, the summation is ended as soon as the remainder term is less than the epsilon.

eval_zeta_function(f, limits)[source]

Check whether the function matches with the zeta function. If it matches, then return a Piecewise expression because zeta function does not converge unless s > 1 and q > 0

is_absolutely_convergent()[source]

Checks for the absolute convergence of an infinite series.

Same as checking convergence of absolute value of sequence_term of an infinite series.

References

Examples

>>> from .. import Sum, Symbol, sin, oo
>>> n = Symbol('n', integer=True)
>>> Sum((-1)**n, (n, 1, oo)).is_absolutely_convergent()
False
>>> Sum((-1)**n/n**2, (n, 1, oo)).is_absolutely_convergent()
True
is_commutative
is_convergent()[source]

Checks for the convergence of a Sum.

We divide the study of convergence of infinite sums and products in two parts.

First Part: One part is the question whether all the terms are well defined, i.e., they are finite in a sum and also non-zero in a product. Zero is the analogy of (minus) infinity in products as \(e^{-\infty} = 0\).

Second Part: The second part is the question of convergence after infinities, and zeros in products, have been omitted assuming that their number is finite. This means that we only consider the tail of the sum or product, starting from some point after which all terms are well defined.

For example, in a sum of the form:

\[\sum_{1 \leq i < \infty} \frac{1}{n^2 + an + b}\]

where a and b are numbers. The routine will return true, even if there are infinities in the term sequence (at most two). An analogous product would be:

\[\prod_{1 \leq i < \infty} e^{\frac{1}{n^2 + an + b}}\]

This is how convergence is interpreted. It is concerned with what happens at the limit. Finding the bad terms is another independent matter.

Note: It is responsibility of user to see that the sum or product is well defined.

There are various tests employed to check the convergence like divergence test, root test, integral test, alternating series test, comparison tests, Dirichlet tests. It returns true if Sum is convergent and false if divergent and NotImplementedError if it can not be checked.

References

Examples

>>> from .. import factorial, S, Sum, Symbol, oo
>>> n = Symbol('n', integer=True)
>>> Sum(n/(n - 1), (n, 4, 7)).is_convergent()
True
>>> Sum(n/(2*n + 1), (n, 1, oo)).is_convergent()
False
>>> Sum(factorial(n)/5**n, (n, 1, oo)).is_convergent()
False
>>> Sum(1/n**(S(6)/5), (n, 1, oo)).is_convergent()
True

See also

Sum.is_absolutely_convergent, Product.is_convergent

reverse_order(*indices)[source]

Reverse the order of a limit in a Sum.

Usage

reverse_order(self, *indices) reverses some limits in the expression self which can be either a Sum or a Product. The selectors in the argument indices specify some indices whose limits get reversed. These selectors are either variable names or numerical indices counted starting from the inner-most limit tuple.

Examples

>>> from .. import Sum
>>> from ..abc import x, y, a, b, c, d
>>> Sum(x, (x, 0, 3)).reverse_order(x)
Sum(-x, (x, 4, -1))
>>> Sum(x*y, (x, 1, 5), (y, 0, 6)).reverse_order(x, y)
Sum(x*y, (x, 6, 0), (y, 7, -1))
>>> Sum(x, (x, a, b)).reverse_order(x)
Sum(-x, (x, b + 1, a - 1))
>>> Sum(x, (x, a, b)).reverse_order(0)
Sum(-x, (x, b + 1, a - 1))

While one should prefer variable names when specifying which limits to reverse, the index counting notation comes in handy in case there are several symbols with the same name.

>>> S = Sum(x**2, (x, a, b), (x, c, d))
>>> S
Sum(x**2, (x, a, b), (x, c, d))
>>> S0 = S.reverse_order(0)
>>> S0
Sum(-x**2, (x, b + 1, a - 1), (x, c, d))
>>> S1 = S0.reverse_order(1)
>>> S1
Sum(x**2, (x, b + 1, a - 1), (x, d + 1, c - 1))

Of course we can mix both notations:

>>> Sum(x*y, (x, a, b), (y, 2, 5)).reverse_order(x, 1)
Sum(x*y, (x, b + 1, a - 1), (y, 6, 1))
>>> Sum(x*y, (x, a, b), (y, 2, 5)).reverse_order(y, x)
Sum(x*y, (x, b + 1, a - 1), (y, 6, 1))

See also

index, reorder_limit, reorder

References

modelparameters.sympy.concrete.summations.eval_sum(f, limits)[source]
modelparameters.sympy.concrete.summations.eval_sum_direct(expr, limits)[source]
modelparameters.sympy.concrete.summations.eval_sum_hyper(f, i_a_b)[source]
modelparameters.sympy.concrete.summations.eval_sum_symbolic(f, limits)[source]
modelparameters.sympy.concrete.summations.summation(f, *symbols, **kwargs)[source]

Compute the summation of f with respect to symbols.

The notation for symbols is similar to the notation used in Integral. summation(f, (i, a, b)) computes the sum of f with respect to i from a to b, i.e.,

                            b
                          ____
                          \   `
summation(f, (i, a, b)) =  )    f
                          /___,
                          i = a

If it cannot compute the sum, it returns an unevaluated Sum object. Repeated sums can be computed by introducing additional symbols tuples:

>>> from .. import summation, oo, symbols, log
>>> i, n, m = symbols('i n m', integer=True)
>>> summation(2*i - 1, (i, 1, n))
n**2
>>> summation(1/2**i, (i, 0, oo))
2
>>> summation(1/log(n)**n, (n, 2, oo))
Sum(log(n)**(-n), (n, 2, oo))
>>> summation(i, (i, 0, n), (n, 0, m))
m**3/6 + m**2/2 + m/3
>>> from ..abc import x
>>> from .. import factorial
>>> summation(x**n/factorial(n), (n, 0, oo))
exp(x)

See also

Sum, Product, product

modelparameters.sympy.concrete.summations.telescopic(L, R, limits)[source]

Tries to perform the summation using the telescopic property

return None if not possible

modelparameters.sympy.concrete.summations.telescopic_direct(L, R, n, limits)[source]

Returns the direct summation of the terms of a telescopic sum

L is the term with lower index R is the term with higher index n difference between the indexes of L and R

For example:

>>> from .summations import telescopic_direct
>>> from ..abc import k, a, b
>>> telescopic_direct(1/k, -1/(k+2), 2, (k, a, b))
-1/(b + 2) - 1/(b + 1) + 1/(a + 1) + 1/a

Module contents