pyranha.math

Math module.

pyranha.math.binomial(x, y)[source]

Binomial coefficient.

This function is a wrapper around a lower level function. It will calculate the generalised binomial coefficient \({x \choose y}\), supporting various combinations of integral, rational, floating-point and arbitrary-precision floating-point types as input.

Parameters:
  • x (int, float, Fraction, mpf) – top argument for the binomial coefficient
  • y (int, float, Fraction, mpf) – bottom argument for the binomial coefficient
Returns:

x choose y

Raises:

TypeError if the types of x and/or y are not supported

Raises:

any exception raised by the invoked low-level function

>>> binomial(3,2)
3
>>> binomial(-6,2)
21
>>> from fractions import Fraction as F
>>> binomial(F(-4,5),2)
Fraction(18, 25)
>>> binomial(10,2.4) 
70.3995282...
>>> binomial('hello world',2.4) 
Traceback (most recent call last):
   ...
TypeError: invalid argument type(s)
pyranha.math.cos(arg)[source]

Cosine.

The supported types are int, float, Fraction, mpf and any symbolic type that supports the operation.

Parameters:arg (int, float, Fraction, mpf, or a supported symbolic type.) – cosine argument
Returns:cosine of arg
Raises:TypeError if the type of arg is not supported, or any other exception raised by the invoked low-level function
>>> cos(0)
1
>>> cos(2) 
Traceback (most recent call last):
   ...
ValueError: cannot compute the cosine of a non-zero integer
>>> cos(2.) 
-0.4161468...
>>> from pyranha.types import poisson_series, polynomial, rational, int16, monomial
>>> t = poisson_series[polynomial[rational,monomial[int16]]]()
>>> cos(2 * t('x'))
cos(2*x)
>>> cos('hello') 
Traceback (most recent call last):
   ...
TypeError: invalid argument type(s)
pyranha.math.degree(arg, names=None)[source]

Degree.

This function will return the degree of the input argument. Various symbolic types are supported as input (e.g., polynomials, Poisson series, etc.). If names is None, then the total degree is returned. Otherwise, names must be a list of strings enumerating the variables to be considered for the computation of the partial degree.

Parameters:
  • arg (a supported symbolic type.) – argument whose degree will be returned
  • names (None or a list of strings) – list of the names of the variables to be considered in the computation of the degree
Returns:

the degree of arg

Raises:

TypeError if the type of arg is not supported, or any other exception raised by the invoked low-level function

>>> from pyranha.types import polynomial, rational, k_monomial
>>> t = polynomial[rational,k_monomial]()
>>> x,y,z = t('x'),t('y'),t('z')
>>> degree(x**3+y*z) == 3
True
>>> degree(x**3+y*z,['z']) == 1
True
>>> from fractions import Fraction as F
>>> degree(F(1,2)) 
Traceback (most recent call last):
   ...
TypeError: invalid argument type(s)
pyranha.math.evaluate(arg, eval_dict)[source]

Evaluation.

This function will evaluate arg according to the evaluation dictionary eval_dict. Evaluation is the replacement of all symbolic quantities with numerical values.

eval_dict must be a dictionary of (name,value) pairs, where name is a string referring to the symbol to be replaced and value is the value with which name will be replaced. All values must be of the same type, and this type needs to support the operations needed to compute the evaluation.

Parameters:
  • arg (a symbolic type) – argument for the evaluation
  • eval_dict (a dictionary mapping strings to values, with all values of the same type) – evaluation dictionary
Returns:

the evaluation of arg according to eval_dict

Raises:

TypeError if eval_dict does not satisfy the requirements outlined above

Raises:

ValueError if eval_dict is empty

Raises:

any exception raised by the invoked low-level function

>>> from pyranha.types import polynomial, rational, k_monomial
>>> pt = polynomial[rational,k_monomial]()
>>> x,y,z = pt('x'), pt('y'), pt('z')
>>> evaluate(x*y+4*(y/4)**2*z,{'x':3,'y':-3,'z':5})
Fraction(9, 4)
>>> evaluate(x*y+4*(y/4)**2*z,{'x':3.01,'y':-3.32,'z':5.34}) 
4.7217...
>>> evaluate(x*y+4*(y/4)**2*z,{'x':3.1,'y':-3.2,'z':5}) 
Traceback (most recent call last):
   ...
TypeError: all values in the evaluation dictionary must be of the same type
>>> evaluate(x*y+4*(y/4)**2*z,{}) 
Traceback (most recent call last):
   ...
ValueError: evaluation dictionary cannot be empty
>>> evaluate(x*y+4*(y/4)**2*z,{'x':3,'y':-3,5:5}) 
Traceback (most recent call last):
   ...
TypeError: all keys in the evaluation dictionary must be string objects
>>> evaluate(x*y+4*(y/4)**2*z,[1,2,3]) 
Traceback (most recent call last):
   ...
TypeError: evaluation dictionary must be a dict object
>>> evaluate(x*y+4*(y/4)**2*z,{'x':3,'y':-3}) 
Traceback (most recent call last):
   ...
ValueError: invalid positions map for evaluation
>>> evaluate(x*y+4*(y/4)**2*z,{'x':'a','y':'b', 'z':'c'}) 
Traceback (most recent call last):
   ...
TypeError: invalid argument type(s)
pyranha.math.factorial(n)[source]

Factorial.

Will compute the factorial of n, which must be a non-negative instance of int.

Parameters:n (int) – argument for the factorial
Returns:factorial of n
Raises:TypeError if n is not an int
Raises:ValueError if n is negative or too large
>>> factorial(0)
1
>>> factorial(6)
720
>>> factorial(-1)
Traceback (most recent call last):
   ...
ValueError: invalid argument value
>>> factorial(1.5)
Traceback (most recent call last):
   ...
TypeError: factorial argument must be an integer
pyranha.math.gcd(x, y)[source]

Greatest common divisor.

This function is a wrapper around a lower level function. It will calculate the GCD of x and y, supporting int as argument type.

Parameters:
  • x (int) – first argument
  • y (int) – second argument
Returns:

the GCD of x and y

Raises:

TypeError if the types of x and/or y are not supported

Raises:

any exception raised by the invoked low-level function

>>> gcd(12,9)
3
>>> from pyranha.types import polynomial, integer, k_monomial
>>> pt = polynomial[integer,k_monomial]()
>>> x,y = pt('x'), pt('y')
>>> gcd(x**-1,y) 
Traceback (most recent call last):
   ...
TypeError: invalid argument type(s)
pyranha.math.integrate(arg, name)[source]

Integration.

Compute the antiderivative of arg with respect to the variable name. arg must be a symbolic type and name a string. The success of the operation is not guaranteed, and depends both on the type and value of arg.

Parameters:
  • arg (a symbolic type) – argument for the integration
  • name (string) – name of the variable with respect to which the integration will be calculated
Returns:

antiderivative of arg with respect to name

Raises:

TypeError if the types of arg and/or name are not supported, or any other exception raised by the invoked low-level function

>>> from pyranha.types import polynomial, rational, k_monomial
>>> pt = polynomial[rational,k_monomial]()
>>> x,y = pt('x'), pt('y')
>>> integrate(x + 2*x*y,'x') == x**2/2 + x**2*y
True
>>> integrate(x**-1,'x') 
Traceback (most recent call last):
   ...
ValueError: negative unitary exponent
>>> integrate(x**-1,1) 
Traceback (most recent call last):
   ...
TypeError: invalid argument type(s)
pyranha.math.invert(arg)[source]

Inverse.

Compute the multiplicative inverse of the argument. The supported types are int, float, Fraction, mpf and any symbolic type that supports the operation.

Parameters:arg (int, float, Fraction, mpf, or a supported symbolic type.) – inversion argument
Returns:inverse of arg
Raises:TypeError if the type of arg is not supported, or any other exception raised by the invoked low-level function
>>> from fractions import Fraction as F
>>> invert(F(1,2))
Fraction(2, 1)
>>> invert(1.23) 
0.8130081...
>>> invert(0) 
Traceback (most recent call last):
   ...
ZeroDivisionError: division by zero
>>> from pyranha.types import polynomial, rational, int16, monomial, divisor, divisor_series
>>> t = polynomial[rational,monomial[int16]]()
>>> invert(t('x'))
x**-1
>>> t = divisor_series[polynomial[rational,monomial[int16]],divisor[int16]]()
>>> invert(-2*t('x')+8*t('y'))
-1/2*1/[(x-4*y)]
>>> invert(t('x')+1) 
Traceback (most recent call last):
   ...
ValueError: invalid argument for series exponentiation: negative integral value
>>> invert('hello') 
Traceback (most recent call last):
   ...
TypeError: invalid argument type(s)
pyranha.math.ipow_subs(arg, name, n, x)[source]

Integral power substitution.

This function will replace, in arg, the n-th power of the symbol called name with the generic object x. Internally, the functionality is implemented via a call to a lower-level C++ routine that supports various combinations for the types of arg and x.

Parameters:
  • arg (a symbolic type) – argument for the substitution
  • name (a string) – name of the symbol whose power will be substituted
  • n (an integer) – power of name that will be substituted
  • x (any type supported by the low-level C++ routine) – the quantity that will be substituted for the power of name
Returns:

arg after the substitution of n-th power of the symbol called name with x

Raises:

TypeError in case the input types are not supported or invalid

Raises:

any exception raised by the invoked low-level function

>>> from pyranha.types import rational, polynomial, int16, monomial
>>> pt = polynomial[rational,monomial[int16]]()
>>> x,y,z = pt('x'), pt('y'), pt('z')
>>> ipow_subs(x**5*y,'x',2,z)
x*y*z**2
>>> ipow_subs(x**6*y,'x',2,z**-1)
y*z**-3
>>> ipow_subs(x**6*y,0,2,z**-1)  
Traceback (most recent call last):
   ...
TypeError: invalid argument type(s)
>>> ipow_subs(x**6*y,'x',2.1,z**-1)  
Traceback (most recent call last):
   ...
TypeError: invalid argument type(s)
pyranha.math.lambdify(t, x, names, extra_map={})[source]

Turn a symbolic object into a function.

This function is a wrapper around evaluate() which returns a callable object that can be used to evaluate the input argument x. The call operator of the returned object takes as input a collection of length len(names) of objects of type t which, for the purpose of evaluation, are associated to the list of symbols names at the corresponding positions.

The optional argument extra_map is a dictionary that maps symbol names to callables, and it used to specify what values to assign to specific symbols based on the values of the symbols in names. For instance, suppose that \(z\) depends on \(x\) and \(y\) via \(z\left(x,y\right)=\sqrt{3x+y}\). We can then evaluate the expression \(x+y+z\) as follows:

>>> from pyranha.types import polynomial, rational, k_monomial
>>> pt = polynomial[rational,k_monomial]()
>>> x,y,z = x,y,z = pt('x'),pt('y'),pt('z')
>>> l = lambdify(float,x+y+z,['x','y'],{'z': lambda a: (3.*a[0] + a[1])**.5})
>>> l([1.,2.]) 
5.236067977...

The output value is \(1+2+\sqrt{5}\).

Parameters:
  • t (a supported evaluation type) – the type that will be used for the evaluation of x
  • x (a supported symbolic type) – symbolic object that will be evaluated
  • names (a list of strings) – the symbols that will be used for evaluation
  • extra_map (a dictionary in which the keys are strings and the values are callables) – a dictionary mapping symbol names to custom evaluation functions
Returns:

a callable object that can be used to evaluate x with objects of type t

Return type:

the type returned by the invoked low-level function

Raises:

TypeError if t is not a type, or if names is not a list of strings, or if extra_map is not a dictionary of the specified type

Raises:

ValueError if names contains duplicates or if extra_map contains strings already present in names

Raises:

unspecified any exception raised by the invoked low-level function or by the invoked callables in extra_map

>>> from pyranha.types import polynomial, rational, k_monomial
>>> pt = polynomial[rational,k_monomial]()
>>> x,y,z = x,y,z = pt('x'),pt('y'),pt('z')
>>> l = lambdify(int,2*x-y+3*z,['z','y','x'])
>>> l([1,2,3])
Fraction(7, 1)
>>> l([1,2,-3])
Fraction(-5, 1)
>>> l = lambdify(float,x+y+z,['x','y'],{'z': lambda a: (3.*a[0] + a[1])**.5})
>>> l([1.,2.]) 
5.236067977...
>>> l([1]) 
Traceback (most recent call last):
   ...
ValueError: invalid size of the evaluation vector
>>> l = lambdify(3,2*x-y+3*z,['z','y','x']) 
Traceback (most recent call last):
   ...
TypeError: the 't' argument must be a type
>>> l = lambdify(int,2*x-y+3*z,[1,2,3]) 
Traceback (most recent call last):
   ...
TypeError: the 'names' argument must be a list of strings
>>> l = lambdify(int,2*x-y+3*z,['z','y','x','z']) 
Traceback (most recent call last):
   ...
ValueError: the 'names' argument contains duplicates
>>> l = lambdify(float,x+y+z,['x','y'],{'z': 123})
Traceback (most recent call last):
   ...
TypeError: all the values in the 'extra_map' argument must be callables
pyranha.math.ldegree(arg, names=None)[source]

Low degree.

This function will return the low degree of the input argument. Various symbolic types are supported as input (e.g., polynomials, Poisson series, etc.). If names is None, then the total low degree is returned. Otherwise, names must be a list of strings enumerating the variables to be considered for the computation of the partial low degree.

Parameters:
  • arg (a supported symbolic type.) – argument whose low degree will be returned
  • names (None or a list of strings) – list of the names of the variables to be considered in the computation of the low degree
Returns:

the low degree of arg

Raises:

TypeError if the type of arg is not supported, or any other exception raised by the invoked low-level function

>>> from pyranha.types import polynomial, rational, k_monomial
>>> t = polynomial[rational,k_monomial]()
>>> x,y,z = t('x'),t('y'),t('z')
>>> ldegree(x**3+y*z) == 2
True
>>> ldegree(x**3+y*x,['x']) == 1
True
>>> from fractions import Fraction as F
>>> ldegree(F(1,2)) 
Traceback (most recent call last):
   ...
TypeError: invalid argument type(s)
pyranha.math.partial(arg, name)[source]

Partial derivative.

Compute the partial derivative of arg with respect to the variable name. arg must be a symbolic type and name a string.

Parameters:
  • arg (a symbolic type) – argument for the partial derivative
  • name (string) – name of the variable with respect to which the derivative will be calculated
Returns:

partial derivative of arg with respect to name

Raises:

TypeError if the types of arg and/or name are not supported, or any other exception raised by the invoked low-level function

>>> from pyranha.types import polynomial, integer, int16, monomial
>>> pt = polynomial[integer,monomial[int16]]()
>>> x,y = pt('x'), pt('y')
>>> partial(x + 2*x*y,'y')
2*x
>>> partial(x + 2*x*y,1) 
Traceback (most recent call last):
   ...
TypeError: invalid argument type(s)
pyranha.math.pbracket(f, g, p_list, q_list)[source]

Poisson bracket.

Compute the Poisson bracket of f and g with respect to the momenta with names in p_list and coordinates with names in q_list. f and g must be symbolic objects of the same type, and p_list and q_list lists of strings with the same size and no duplicate entries.

Parameters:
  • f (a symbolic type) – first argument
  • g (a symbolic type) – second argument
  • p_list (list of strings) – list of momenta names
  • q_list (list of strings) – list of coordinates names
Returns:

Poisson bracket of f and g with respect to momenta p_list and coordinates q_list

Raises:

ValueError if p_list and q_list have different sizes or duplicate entries

Raises:

TypeError if the types of the arguments are invalid

Raises:

any exception raised by the invoked low-level function

>>> from pyranha.types import polynomial, rational, int16, monomial
>>> pt = polynomial[rational,monomial[int16]]()
>>> x,v = pt('x'), pt('v')
>>> pbracket(x+v,x+v,['v'],['x']) == 0
True
>>> pbracket(x+v,x+v,[],['x']) 
Traceback (most recent call last):
   ...
ValueError: the number of coordinates is different from the number of momenta
>>> pbracket(x+v,x+v,['v','v'],['x']) 
Traceback (most recent call last):
   ...
ValueError: the list of momenta contains duplicate entries
>>> pbracket(v,x,['v'],[1]) 
Traceback (most recent call last):
   ...
TypeError: invalid argument type(s)
pyranha.math.sin(arg)[source]

Sine.

The supported types are int, float, Fraction, mpf and any symbolic type that supports the operation.

Parameters:arg (int, float, Fraction, mpf, or a supported symbolic type.) – sine argument
Returns:sine of arg
Raises:TypeError if the type of arg is not supported, or any other exception raised by the invoked low-level function
>>> sin(0)
0
>>> sin(2) 
Traceback (most recent call last):
   ...
ValueError: cannot compute the cosine of a non-zero integer
>>> sin(2.) 
0.9092974...
>>> from pyranha.types import poisson_series, polynomial, rational, int16, monomial
>>> t = poisson_series[polynomial[rational,monomial[int16]]]()
>>> sin(2 * t('x'))
sin(2*x)
>>> sin('hello') 
Traceback (most recent call last):
   ...
TypeError: invalid argument type(s)
pyranha.math.subs(arg, subs_dict)[source]

Substitution.

This function will replace, in arg, the symbols listed in the dictionary subs_dict with their mapped values. Internally, the functionality is implemented via a call to a lower-level C++ routine that supports various combinations of input argument types.

subs_dict must be a dictionary of (name,value) pairs, where name is a string referring to the symbol to be substituted and value is the value with which name will be replaced. All values must be of the same type, and this type needs to support the operations needed to compute the substitution.

Parameters:
  • arg (a symbolic type) – the argument for the substitution
  • subs_dict (a dictionary mapping strings to values, with all values of the same type) – the substitution dictionary
Returns:

arg after the substitution of the symbols in subs_dict with the mapped values

Raises:

TypeError if subs_dict does not satisfy the requirements outlined above

Raises:

ValueError if subs_dict is empty

Raises:

any exception raised by the invoked low-level function

>>> from pyranha.types import polynomial, rational, k_monomial
>>> pt = polynomial[rational,k_monomial]()
>>> x,y,z = pt('x'), pt('y'), pt('z')
>>> subs(x*y+4*(y/4)**2*z,{'x':3,'y':-3,'z':2})
-9/2
>>> subs(x*y+4*(y/4)**2*z,{'x':3.01,'y':-3.32,'z':5.34}) 
4.7217...
>>> subs(x*y+4*(y/4)**2*z,{'x':3.1,'y':-3.2,'z':5}) 
Traceback (most recent call last):
   ...
TypeError: all values in an evaluation/substitution dictionary must be of the same type
>>> subs(x*y+4*(y/4)**2*z,{}) 
Traceback (most recent call last):
   ...
ValueError: an evaluation/substitution dictionary cannot be empty
>>> subs(x*y+4*(y/4)**2*z,{'x':3,'y':-3,5:5}) 
Traceback (most recent call last):
   ...
TypeError: all keys in an evaluation/substitution dictionary must be string objects
>>> subs(x*y+4*(y/4)**2*z,[1,2,3]) 
Traceback (most recent call last):
   ...
TypeError: an evaluation/substitution dictionary must be a dict object
>>> subs(x*y+4*(y/4)**2*z,{'x':'a','y':'b', 'z':'c'}) 
Traceback (most recent call last):
   ...
TypeError: invalid argument type(s)
pyranha.math.t_degree(arg, names=None)[source]

Trigonometric degree.

This function will return the trigonometric degree of the input argument. Various symbolic types are supported as input. If names is None, then the total degree is returned. Otherwise, names must be a list of strings enumerating the variables to be considered for the computation of the partial degree.

Parameters:
  • arg (a supported symbolic type.) – argument whose trigonometric degree will be returned
  • names (None or a list of strings) – list of the names of the variables to be considered in the computation of the degree
Returns:

the trigonometric degree of arg

Raises:

TypeError if the type of arg is not supported, or any other exception raised by the invoked low-level function

>>> from pyranha.types import poisson_series, polynomial, rational, k_monomial
>>> from pyranha.math import cos
>>> t = poisson_series[polynomial[rational,k_monomial]]()
>>> x,y,z = t('x'),t('y'),t('z')
>>> t_degree(cos(3*x+y-z)+cos(2*x)) == 3
True
>>> t_degree(cos(3*x+y+z)+cos(x),['z']) == 1
True
>>> from fractions import Fraction as F
>>> t_degree(F(1,2)) 
Traceback (most recent call last):
   ...
TypeError: invalid argument type(s)
pyranha.math.t_ldegree(arg, names=None)[source]

Trigonometric low degree.

This function will return the trigonometric low degree of the input argument. Various symbolic types are supported as input. If names is None, then the total low degree is returned. Otherwise, names must be a list of strings enumerating the variables to be considered for the computation of the partial low degree.

Parameters:
  • arg (a supported symbolic type.) – argument whose trigonometric low degree will be returned
  • names (None or a list of strings) – list of the names of the variables to be considered in the computation of the low degree
Returns:

the trigonometric low degree of arg

Raises:

TypeError if the type of arg is not supported, or any other exception raised by the invoked low-level function

>>> from pyranha.types import poisson_series, polynomial, rational, k_monomial
>>> from pyranha.math import cos
>>> t = poisson_series[polynomial[rational,k_monomial]]()
>>> x,y,z = t('x'),t('y'),t('z')
>>> t_ldegree(cos(3*x+y-z)+cos(2*x)) == 2
True
>>> t_ldegree(cos(3*x+y-z)+cos(2*y+z),['y']) == 1
True
>>> from fractions import Fraction as F
>>> t_ldegree(F(1,2)) 
Traceback (most recent call last):
   ...
TypeError: invalid argument type(s)
pyranha.math.t_lorder(arg, names=None)[source]

Trigonometric low order.

This function will return the trigonometric low order of the input argument. Various symbolic types are supported as input. If names is None, then the total low order is returned. Otherwise, names must be a list of strings enumerating the variables to be considered for the computation of the partial low order.

Parameters:
  • arg (a supported symbolic type.) – argument whose trigonometric low order will be returned
  • names (None or a list of strings) – list of the names of the variables to be considered in the computation of the low order
Returns:

the trigonometric low order of arg

Raises:

TypeError if the type of arg is not supported, or any other exception raised by the invoked low-level function

>>> from pyranha.types import poisson_series, polynomial, rational, k_monomial
>>> from pyranha.math import cos
>>> t = poisson_series[polynomial[rational,k_monomial]]()
>>> x,y,z = t('x'),t('y'),t('z')
>>> t_lorder(cos(4*x+y-z)+cos(2*x)) == 2
True
>>> t_lorder(cos(3*x+y-z)+cos(2*y+z),['y']) == 1
True
>>> from fractions import Fraction as F
>>> t_lorder(F(1,2)) 
Traceback (most recent call last):
   ...
TypeError: invalid argument type(s)
pyranha.math.t_order(arg, names=None)[source]

Trigonometric order.

This function will return the trigonometric order of the input argument. Various symbolic types are supported as input. If names is None, then the total order is returned. Otherwise, names must be a list of strings enumerating the variables to be considered for the computation of the partial order.

Parameters:
  • arg (a supported symbolic type.) – argument whose trigonometric order will be returned
  • names (None or a list of strings) – list of the names of the variables to be considered in the computation of the order
Returns:

the order of arg

Raises:

TypeError if the type of arg is not supported, or any other exception raised by the invoked low-level function

>>> from pyranha.types import poisson_series, polynomial, rational, k_monomial
>>> from pyranha.math import cos
>>> t = poisson_series[polynomial[rational,k_monomial]]()
>>> x,y,z = t('x'),t('y'),t('z')
>>> t_order(cos(3*x+y-z)+cos(x)) == 5
True
>>> t_order(cos(3*x+y-z)-sin(y),['z']) == 1
True
>>> from fractions import Fraction as F
>>> t_order(F(1,2)) 
Traceback (most recent call last):
   ...
TypeError: invalid argument type(s)
pyranha.math.t_subs(arg, name, x, y)[source]

Trigonometric substitution.

This function will replace, in arg, the cosine and sine of the symbol called name with the generic objects x and y. Internally, the functionality is implemented via a call to a lower-level C++ routine that supports various combinations for the types of arg, x and y.

Parameters:
  • arg (a symbolic type) – argument for the substitution
  • name (a string) – name of the symbol whose cosine and sine will be substituted
  • x (any type supported by the low-level C++ routine) – the quantity that will be substituted for the cosine of name
  • y (any type supported by the low-level C++ routine) – the quantity that will be substituted for the sine of name
Returns:

arg after the substitution of the cosine and sine of the symbol called name with x and y

Raises:

TypeError in case the input types are not supported or invalid

Raises:

any exception raised by the invoked low-level function

>>> from pyranha.types import poisson_series, rational, polynomial, int16, monomial
>>> pt = poisson_series[polynomial[rational,monomial[int16]]]()
>>> x,y = pt('x'), pt('y')
>>> t_subs(cos(x+y),'x',1,0)
cos(y)
>>> t_subs(sin(2*x+y),'x',0,1)
-sin(y)
>>> t_subs(sin(2*x+y),0,0,1)  
Traceback (most recent call last):
   ...
TypeError: invalid argument type(s)
pyranha.math.transformation_is_canonical(new_p, new_q, p_list, q_list)[source]

Test if transformation is canonical.

This function will check if a transformation of Hamiltonian momenta and coordinates is canonical using the Poisson bracket test. The transformation is expressed as two separate list of objects, new_p and new_q, representing the new momenta and coordinates as functions of the old momenta p_list and q_list.

The function requires new_p and new_q to be lists of symbolic objects of the same type, and p_list and q_list lists of strings with the same size and no duplicate entries.

Parameters:
  • new_p (list of symbolic instances) – list of objects representing the new momenta
  • new_q (list of symbolic instances) – list of objects representing the new coordinates
  • p_list (list of strings) – list of momenta names
  • q_list (list of strings) – list of coordinates names
Returns:

True if the transformation defined by new_p and new_q is canonical, False otherwise

Raises:

ValueError if the size of all input lists is not the same

Raises:

TypeError if the types of the arguments are invalid

Raises:

any exception raised by the invoked low-level function

>>> from pyranha.types import polynomial, rational, k_monomial
>>> pt = polynomial[rational,k_monomial]()
>>> L,G,H,l,g,h = [pt(_) for _ in 'LGHlgh']
>>> transformation_is_canonical([-l],[L],['L'],['l'])
True
>>> transformation_is_canonical([l],[L],['L'],['l'])
False
>>> transformation_is_canonical([2*L+3*G+2*H,4*L+2*G+3*H,9*L+6*G+7*H],
... [-4*l-g+6*h,-9*l-4*g+15*h,5*l+2*g-8*h],['L','G','H'],['l','g','h'])
True
>>> transformation_is_canonical([2*L+3*G+2*H,4*L+2*G+3*H,9*L+6*G+7*H],
... [-4*l-g+6*h,-9*l-4*g+15*h,5*l+2*g-7*h],['L','G','H'],['l','g','h'])
False
>>> transformation_is_canonical(L,l,'L','l') 
Traceback (most recent call last):
   ...
TypeError: non-list input type
>>> transformation_is_canonical([L,G],[l],['L'],['l']) 
Traceback (most recent call last):
   ...
ValueError: the number of coordinates is different from the number of momenta
>>> transformation_is_canonical([],[],[],[]) 
Traceback (most recent call last):
   ...
ValueError: empty input list(s)
>>> transformation_is_canonical([L,1],[l,g],['L','G'],['l','g']) 
Traceback (most recent call last):
   ...
TypeError: types in input lists are not homogeneous
>>> transformation_is_canonical([L,G],[l,g],['L','G'],['l',1]) 
Traceback (most recent call last):
   ...
TypeError: p_list and q_list must be lists of strings
>>> transformation_is_canonical(['a'],['b'],['c'],['d']) 
Traceback (most recent call last):
   ...
TypeError: invalid argument type(s)
pyranha.math.truncate_degree(arg, max_degree, names=None)[source]

Degree-based truncation.

This function will eliminate from arg parts whose degree is greater than max_degree. The truncation operates on symbolic types both by eliminating entire terms and by truncating recursively the coefficients, if possible.

If names is None, then the total degree is considered for the truncation. Otherwise, names must be a list of strings enumerating the variables to be considered for the computation of the degree.

Parameters:
  • arg (a symbolic type) – argument for the truncation
  • max_degree (a type comparable to the type representing the degree of arg) – maximum degree that will be kept in arg
  • names (None or a list of strings) – list of the names of the variables to be considered in the computation of the degree
Returns:

the truncated counterpart of arg

Raises:

TypeError if names is neither None nor a list of strings

Raises:

any exception raised by the invoked low-level function

>>> from pyranha.types import polynomial, rational, k_monomial, poisson_series
>>> pt = polynomial[rational,k_monomial]()
>>> x,y,z = pt('x'), pt('y'), pt('z')
>>> truncate_degree(x**2*y+x*y+z,2) 
x*y+z
>>> truncate_degree(x**2*y+x*y+z,0,['x'])
z
>>> pt = poisson_series[polynomial[rational,k_monomial]]()
>>> x,y,z,a,b = pt('x'), pt('y'), pt('z'), pt('a'), pt('b')
>>> truncate_degree((x+y*x+x**2*z)*cos(a+b)+(y-y*z+x**4)*sin(2*a+b),3) 
(x+x*y+x**2*z)*cos(a+b)+(-y*z+y)*sin(2*a+b)
>>> truncate_degree((x+y*x+x**2*z)*cos(a+b)+(y-y*z+x**4)*sin(2*a+b),1,['x','y']) 
x*cos(a+b)+(-y*z+y)*sin(2*a+b)
>>> truncate_degree((x+y*x+x**2*z)*cos(a+b)+(y-y*z+x**4)*sin(2*a+b),1,'x') 
Traceback (most recent call last):
   ...
TypeError: the optional 'names' argument must be a list of strings
>>> truncate_degree((x+y*x+x**2*z)*cos(a+b)+(y-y*z+x**4)*sin(2*a+b),1,['x',1]) 
Traceback (most recent call last):
   ...
TypeError: the optional 'names' argument must be a list of strings