cutcutcodec.core.compilation.sympy_to_torch.printer

Static C compilation of atomic sympy expression.

This is enable to write a C code, compile it with gcc, and import the file as a python function.

It is slowler to initialise than the dynamic version but it is faster to evaluate. This static evaluation do not support broadcasting.

Implemented functions:

  • sympy.Abs

  • sympy.Add +

  • sympy.atan

  • sympy.cbrt

  • sympy.cos

  • sympy.Eq

  • sympy.exp

  • sympy.GreaterThan

  • sympy.LessThan

  • sympy.log

  • sympy.Max

  • sympy.Min

  • sympy.Mul *

  • sympy.Or

  • sympy.Piecewise

  • sympy.Pow / and **

  • sympy.sin

  • sympy.sqrt

  • sympy.StrictGreaterThan

  • sympy.StrictLessThan

Not implemented functions:

  • sympy.acos

  • sympy.acosh

  • sympy.Add + and -

  • sympy.And

  • sympy.arg

  • sympy.asin

  • sympy.asinh

  • sympy.atan2

  • sympy.atanh

  • sympy.ceiling

  • sympy.cosh

  • sympy.Determinant

  • sympy.erf

  • sympy.floor

  • sympy.HadamardProduct

  • sympy.im

  • sympy.ITE

  • sympy.loggamma

  • sympy.MatAdd

  • sympy.Mod %

  • sympy.Ne

  • sympy.Not

  • sympy.re

  • sympy.sign

  • sympy.sinh

  • sympy.tan

  • sympy.tanh

  • sympy.Trace

  • sympy.Tuple

Functions

c_piecewise(out, indexing, c_type, *parts)

C .

Details

cutcutcodec.core.compilation.sympy_to_torch.printer.c_piecewise(out: Symbol, indexing: defaultdict[Symbol, str], c_type: str, *parts: Atom) tuple[set[str], set[Symbol], list[str]][source]

C … if … else … operation.

Examples

>>> from sympy.abc import x
>>> from sympy.functions.elementary.piecewise import Piecewise
>>> import numpy as np
>>> from cutcutcodec.core.compilation.sympy_to_torch.lambdify import _lambdify_c
>>> from cutcutcodec.core.compilation.sympy_to_torch.printer import _print_atomic, _printer
>>> func = _lambdify_c(_printer([(x, Piecewise((0, x < 0), (x, True)))], {}, {x}))
>>> func(np.array([np.nan, -np.inf, -1.0, 0.0, 1.0, np.inf]))
array([nan,  0.,  0.,  0.,  1., inf])
>>>