Extensions to SymPy#

Symbol Factory#

Functions#

math.prod

Takes a sequence and returns the product of all elements

math.scalar_product

Scalar product between two sequences.

math.kronecker_delta

Kronecker delta for variable number of arguments, 1 if all args are equal, otherwise 0

math.tanh_step_function_approximation

Approximation of step function by a tanh function

math.multidimensional_sum

Multidimensional summation

Expression Analysis#

math.is_constant

Simple version of checking if a sympy expression is constant.

math.summands

math.common_denominator

Finds least common multiple of all denominators occurring in an expression

math.get_symmetric_part

Returns the symmetric part of a sympy expressions.

math.count_operations

Counts the number of additions, multiplications and division.

math.count_operations_in_ast

Counts number of operations in an abstract syntax tree, see also count_operations()

Expression Rewriting and Simplifications#

math.remove_small_floats

Removes all sp.Float objects whose absolute value is smaller than threshold

math.is_integer_sequence

Checks if all elements of the passed sequence can be cast to integers

math.normalize_product

Expects a sympy expression that can be interpreted as a product and returns a list of all factors.

math.symmetric_product

Similar to itertools.product but yields only values where the index is ascending i.e. values below/up to diagonal.

math.fast_subs

Similar to sympy subs function.

math.subs_additive

Transformation for replacing a given subexpression inside a sum.

math.replace_second_order_products

Replaces second order mixed terms like 4*x*y by 2*( (x+y)**2 - x**2 - y**2 ).

math.remove_higher_order_terms

Removes all terms that contain more than 'order' factors of given 'symbols'

math.complete_the_square

Transforms second order polynomial into only squared part.

math.complete_the_squares_in_exp

Completes squares in arguments of exponential which makes them simpler to integrate.

math.extract_most_common_factor

Processes a sum of fractions: determines the most common factor and splits term in common factor and rest

math.recursive_collect

Applies sympy.collect recursively for a list of symbols, collecting symbol 2 in the coefficients of symbol 1, and so on.

math.simplify_by_equality

Uses the equality a = b + c, where a and b must be symbols, to simplify expr by attempting to express additive combinations of two quantities by the third.

Typed Expressions#

class pystencils.TypedSymbol(*args, **kwds)#
class pystencils.DynamicType(value)#

Dynamic data type that will be resolved during kernel creation

NUMERIC_TYPE = 1#

Use the default numeric type set for the kernel

INDEX_TYPE = 2#

Use the default index type set for the kernel.

This is guaranteed to be an interger type.

class pystencils.sympyextensions.typed_sympy.TypeCast(expr, tatom)#

Explicitly cast an expression to a data type.

Parameters:
  • expr (sp.Basic)

  • tatom (TypeAtom)

Return type:

TypeCast | None

classmethod eval(expr, tatom)#

Returns a canonical form of cls applied to arguments args.

Return type:

TypeCast | None

Parameters:
  • expr (Basic)

  • tatom (TypeAtom)

Explanation#

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.

Examples of eval() for the function “sign”

@classmethod
def eval(cls, arg):
    if arg is S.NaN:
        return S.NaN
    if arg.is_zero: return S.Zero
    if arg.is_positive: return S.One
    if arg.is_negative: return S.NegativeOne
    if isinstance(arg, Mul):
        coeff, terms = arg.as_coeff_Mul(rational=True)
        if coeff is not S.One:
            return cls(coeff) * cls(terms)
pystencils.sympyextensions.tcast#

alias of TypeCast

Integer Operations#

integer_functions.bitwise_xor

integer_functions.bit_shift_right

integer_functions.bit_shift_left

integer_functions.bitwise_and

integer_functions.bitwise_or

integer_functions.int_div

C-style round-to-zero integer division

integer_functions.int_rem

C-style round-to-zero integer remainder

integer_functions.round_to_multiple_towards_zero

Returns the next smaller/equal in magnitude integer divisible by given divisor.

integer_functions.ceil_to_multiple

For positive input, returns the next greater/equal integer divisible by given divisor.

integer_functions.div_ceil

For positive input, integer division that is always rounded up, i.e. div_ceil(a, b) = ceil(div(a, b)).