Falling factorial (related to rising factorial) is a double valued
function arising in concrete mathematics, hypergeometric functions
and series expansions. It is defined by
When x is a Poly instance of degree >= 1 with single variable,
ff(x,k) = x(y) * x(y-1) * … * x(y-k+1), where y is the variable of x.
This is as described in Peter Paule, “Greatest Factorial Factorization and
Symbolic Summation”, Journal of Symbolic Computation, vol. 20, pp.
235-268, 1995.
Returns a canonical form of cls applied to arguments args.
The eval() method is called when the class cls is about to be
instantiated and it should return either some simplified instance
(possible of some other class), or if the class cls should be
unmodified, return None.
Rising factorial (also called Pochhammer symbol) is a double valued
function arising in concrete mathematics, hypergeometric functions
and series expansions. It is defined by:
When x is a Poly instance of degree >= 1 with a single variable,
rf(x,k) = x(y) * x(y+1) * … * x(y+k-1), where y is the variable of x.
This is as described in Peter Paule, “Greatest Factorial Factorization and
Symbolic Summation”, Journal of Symbolic Computation, vol. 20, pp.
235-268, 1995.
Returns a canonical form of cls applied to arguments args.
The eval() method is called when the class cls is about to be
instantiated and it should return either some simplified instance
(possible of some other class), or if the class cls should be
unmodified, return None.
Implementation of the binomial coefficient. It can be defined
in two ways depending on its desired interpretation:
C(n,k) = n!/(k!(n-k)!) or C(n, k) = ff(n, k)/k!
First, in a strict combinatorial sense it defines the
number of ways we can choose ‘k’ elements from a set of
‘n’ elements. In this case both arguments are nonnegative
integers and binomial is computed using an efficient
algorithm based on prime factorization.
The other definition is generalization for arbitrary ‘n’,
however ‘k’ must also be nonnegative. This case is very
useful when evaluating summations.
For the sake of convenience for negative ‘k’ this function
will return zero no matter what valued is the other argument.
To expand the binomial when n is a symbol, use either
expand_func() or expand(func=True). The former will keep the
polynomial in factored form while the latter will expand the
polynomial itself. See examples for details.
Returns a canonical form of cls applied to arguments args.
The eval() method is called when the class cls is about to be
instantiated and it should return either some simplified instance
(possible of some other class), or if the class cls should be
unmodified, return None.
Implementation of factorial function over nonnegative integers.
By convention (consistent with the gamma function and the binomial
coefficients), factorial of a negative integer is complex infinity.
The factorial is very important in combinatorics where it gives
the number of ways in which n objects can be permuted. It also
arises in calculus, probability, number theory, etc.
There is strict relation of factorial with gamma function. In
fact n! = gamma(n+1) for nonnegative integers. Rewrite of this
kind is very useful in case of combinatorial simplification.
Computation of the factorial is done using two algorithms. For
small arguments a precomputed look up table is used. However for bigger
input algorithm Prime-Swing is used. It is the fastest algorithm
known and computes n! via prime factorization of special class
of numbers, called here the ‘Swing Numbers’.
Returns a canonical form of cls applied to arguments args.
The eval() method is called when the class cls is about to be
instantiated and it should return either some simplified instance
(possible of some other class), or if the class cls should be
unmodified, return None.
The double factorial n!!, not to be confused with (n!)!
The double factorial is defined for nonnegative integers and for odd
negative integers as:
,
| n*(n - 2)*(n - 4)* ... * 1 for n positive odd
n!! = { n*(n - 2)*(n - 4)* ... * 2 for n positive even
| 1 for n = 0
| (n+2)!! / (n+2) for n negative odd
`
Returns a canonical form of cls applied to arguments args.
The eval() method is called when the class cls is about to be
instantiated and it should return either some simplified instance
(possible of some other class), or if the class cls should be
unmodified, return None.
The subfactorial counts the derangements of n items and is
defined for non-negative integers as:
,
| 1 for n = 0
!n = { 0 for n = 1
| (n - 1)*(!(n - 1) + !(n - 2)) for n > 1
`
It can also be written as int(round(n!/exp(1))) but the recursive
definition with caching is implemented for this function.
An interesting analytic expression is the following [2]_
\[!x = \Gamma(x + 1, -1)/e\]
which is valid for non-negative integers x. The above formula
is not very useful incase of non-integers. \(\Gamma(x + 1, -1)\) is
single-valued only for integral arguments x, elsewhere on the positive real
axis it has an infinite number of branches none of which are real.
Returns a canonical form of cls applied to arguments args.
The eval() method is called when the class cls is about to be
instantiated and it should return either some simplified instance
(possible of some other class), or if the class cls should be
unmodified, return None.
This module implements some special functions that commonly appear in
combinatorial contexts (e.g. in power series); in particular,
sequences of rational numbers such as Bernoulli and Fibonacci numbers.
Factorials, binomial coefficients and related functions are located in
the separate ‘factorials’ module.
Returns a canonical form of cls applied to arguments args.
The eval() method is called when the class cls is about to be
instantiated and it should return either some simplified instance
(possible of some other class), or if the class cls should be
unmodified, return None.
The Bernoulli numbers are a sequence of rational numbers
defined by B_0 = 1 and the recursive relation (n > 0):
n___
\ /n+1 \
0=)||*B./___ \ k/kk=0
They are also commonly defined by their exponential generating
function, which is x/(exp(x) - 1). For odd indices > 1, the
Bernoulli numbers are zero.
The Bernoulli polynomials satisfy the analogous formula:
n___
\ /n \ n-kB(x)=)||*B*x.n/___ \ k/kk=0
Bernoulli numbers and Bernoulli polynomials are related as
B_n(0) = B_n.
We compute Bernoulli numbers using Ramanujan’s formula:
/n+3 \
B=(A(n)-S(n))/||n \ n/
where A(n) = (n+3)/3 when n = 0 or 2 (mod 6), A(n) = -(n+3)/6
when n = 4 (mod 6), and:
[n/6]___
\ /n+3 \
S(n)=)||*B/___ \ n-6*k/n-6*kk=1
This formula is similar to the sum given in the definition, but
cuts 2/3 of the terms. For Bernoulli polynomials, we use the
formula in the definition.
bernoulli(n) gives the nth Bernoulli number, B_n
bernoulli(n, x) gives the nth Bernoulli polynomial in x, B_n(x)
Returns a canonical form of cls applied to arguments args.
The eval() method is called when the class cls is about to be
instantiated and it should return either some simplified instance
(possible of some other class), or if the class cls should be
unmodified, return None.
Returns a canonical form of cls applied to arguments args.
The eval() method is called when the class cls is about to be
instantiated and it should return either some simplified instance
(possible of some other class), or if the class cls should be
unmodified, return None.
Returns a canonical form of cls applied to arguments args.
The eval() method is called when the class cls is about to be
instantiated and it should return either some simplified instance
(possible of some other class), or if the class cls should be
unmodified, return None.
The Fibonacci numbers are the integer sequence defined by the
initial terms F_0 = 0, F_1 = 1 and the two-term recurrence
relation F_n = F_{n-1} + F_{n-2}. This definition
extended to arbitrary real and complex arguments using
the formula
The Fibonacci polynomials are defined by F_1(x) = 1,
F_2(x) = x, and F_n(x) = x*F_{n-1}(x) + F_{n-2}(x) for n > 2.
For all positive integers n, F_n(1) = F_n.
fibonacci(n) gives the nth Fibonacci number, F_n
fibonacci(n, x) gives the nth Fibonacci polynomial in x, F_n(x)
Returns a canonical form of cls applied to arguments args.
The eval() method is called when the class cls is about to be
instantiated and it should return either some simplified instance
(possible of some other class), or if the class cls should be
unmodified, return None.
Returns a canonical form of cls applied to arguments args.
The eval() method is called when the class cls is about to be
instantiated and it should return either some simplified instance
(possible of some other class), or if the class cls should be
unmodified, return None.
Returns a canonical form of cls applied to arguments args.
The eval() method is called when the class cls is about to be
instantiated and it should return either some simplified instance
(possible of some other class), or if the class cls should be
unmodified, return None.
Lucas numbers satisfy a recurrence relation similar to that of
the Fibonacci sequence, in which each term is the sum of the
preceding two. They are generated by choosing the initial
values L_0 = 2 and L_1 = 1.
Returns a canonical form of cls applied to arguments args.
The eval() method is called when the class cls is about to be
instantiated and it should return either some simplified instance
(possible of some other class), or if the class cls should be
unmodified, return None.
Return the number of combinations of n items taken k at a time.
Possible values for n::
integer - set of length n
sequence - converted to a multiset internally
multiset - {element: multiplicity}
If k is None then the total of all combinations of length 0
through the number of items represented in n will be returned.
If replacement is True then a given item can appear more than once
in the k items. (For example, for ‘ab’ sets of 2 would include ‘aa’,
‘ab’, and ‘bb’.) The multiplicity of elements in n is ignored when
replacement is True but the total number of elements is considered
since no element can appear more times than the number of elements in
n.
If there are k items with multiplicities m_1,m_2,...,m_k
then the total of all combinations of length 0 hrough k is the
product, (m_1+1)*(m_2+1)*...*(m_k+1). When the multiplicity
of each item is 1 (i.e., k unique items) then there are 2**k
combinations. For example, if there are 4 unique items, the total number
of combinations is 16:
Return the number of permutations of n items taken k at a time.
Possible values for n::
integer - set of length n
sequence - converted to a multiset internally
multiset - {element: multiplicity}
If k is None then the total of all permutations of length 0
through the number of items represented by n will be returned.
If replacement is True then a given item can appear more than once
in the k items. (For example, for ‘ab’ permutations of 2 would
include ‘aa’, ‘ab’, ‘ba’ and ‘bb’.) The multiplicity of elements in
n is ignored when replacement is True but the total number
of elements is considered since no element can appear more times than
the number of elements in n.
Return the number of k-sized partitions of n items.
Possible values for n::
integer - n identical items
sequence - converted to a multiset internally
multiset - {element: multiplicity}
Note: the convention for nT is different than that of nC and
nP in that
here an integer indicates nidentical items instead of a set of
length n; this is in keeping with the partitions function which
treats its integer-n input like a list of n 1s. One can use
range(n) for n to indicate n distinct items.
If k is None then the total number of ways to partition the elements
represented in n will be returned.
n for Stirling numbers of the first kind
-n for signed Stirling numbers of the first kind
k for Stirling numbers of the second kind
The first kind of Stirling number counts the number of permutations of
n distinct items that have k cycles; the second kind counts the
ways in which n distinct items can be partitioned into k parts.
If d is given, the “reduced Stirling number of the second kind” is
returned: S^{d}(n,k)=S(n-d+1,k-d+1) with n>=k>=d.
(This counts the ways to partition n consecutive integers into
k groups with no pairwise difference less than d. See example
below.)
To obtain the signed Stirling numbers of the first kind, use keyword
signed=True. Using this keyword automatically sets kind to 1.