cutcutcodec.core.compilation.sympy_to_torch.printer_atom

C implementation of all atomic operations, helper for printer.

Functions

atom2str(elem, indexing, c_type)

Help for _print_atomic.

c_abs(out, indexing, c_type, arg)

C abs operation.

c_add(out, indexing, c_type, *parts)

C + operation.

c_atan(out, indexing, c_type, arg)

C atan operation.

c_cos(out, indexing, c_type, arg)

C cos operation.

c_equality(out, indexing, c_type, *parts)

C == operation.

c_exp(out, indexing, c_type, arg)

C exp operation.

c_greaterthan(out, indexing, c_type, *parts)

C >= operation, with module comparaison for complex numbers.

c_lessthan(out, indexing, c_type, *parts)

C <= operation, with module comparaison for complex numbers.

c_log(out, indexing, c_type, arg)

C log operation.

c_max(out, indexing, c_type, *parts)

C maximum operation.

c_min(out, indexing, c_type, *parts)

C minimum operation.

c_mul(out, indexing, c_type, *parts)

C * operation.

c_pow(out, indexing, c_type, base, exp)

C ** operation.

c_pow_pos_integer(out, indexing, c_type, ...)

Manage the special case of power where the exponant is a positive integer.

c_sin(out, indexing, c_type, arg)

C sin operation.

c_strictgreaterthan(out, indexing, c_type, ...)

C > operation, with module comparaison for complex numbers.

c_strictlessthan(out, indexing, c_type, *parts)

C < operation, with module comparaison for complex numbers.

Details

cutcutcodec.core.compilation.sympy_to_torch.printer_atom.atom2str(elem: Atom, indexing: defaultdict[Symbol, str], c_type: str) str[source]

Help for _print_atomic.

cutcutcodec.core.compilation.sympy_to_torch.printer_atom.c_abs(out: Symbol, indexing: defaultdict[Symbol, str], c_type: str, arg: Atom) tuple[set[str], set[Symbol], list[str]][source]

C abs operation.

Examples

>>> from sympy.abc import x
>>> from sympy.functions.elementary.complexes import Abs
>>> 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, Abs(x))], {}, {x}))
>>> func(np.array([np.nan, -np.inf, -2., 2., np.inf]))
array([nan, inf,  2.,  2., inf])
>>> func(np.array([3+4j, -3+4j, 3-4j, -3-4j]))
array([5.+0.j, 5.+0.j, 5.+0.j, 5.+0.j])
>>>
cutcutcodec.core.compilation.sympy_to_torch.printer_atom.c_add(out: Symbol, indexing: defaultdict[Symbol, str], c_type: str, *parts: Atom) tuple[set[str], set[Symbol], list[str]][source]

C + operation.

Examples

>>> from collections import defaultdict
>>> from sympy.abc import x, y, z
>>> from cutcutcodec.core.compilation.sympy_to_torch.printer import _print_atomic
>>> _print_atomic(1 + x, x, defaultdict(lambda: ""), "float")
(set(), set(), ['x += 1.0f;'])
>>> _print_atomic(1 + x, y, defaultdict(lambda: ""), "float")
(set(), set(), ['y = 1.0f + x;'])
>>> _print_atomic(1 + x + y, z, defaultdict(lambda: ""), "float")
(set(), set(), ['z = 1.0f + x;', 'z += y;'])
>>>
cutcutcodec.core.compilation.sympy_to_torch.printer_atom.c_atan(out: Symbol, indexing: defaultdict[Symbol, str], c_type: str, arg: Atom) tuple[set[str], set[Symbol], list[str]][source]

C atan operation.

Examples

>>> from sympy.abc import x
>>> from sympy.functions.elementary.trigonometric import atan
>>> 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, atan(x))], {}, {x}))
>>> func(np.array([np.nan]))
array([nan])
>>> func(np.array([-np.inf, -1.0, 0.0, 1.0, np.inf])) / np.pi
array([-0.5 , -0.25,  0.  ,  0.25,  0.5 ])
>>>
cutcutcodec.core.compilation.sympy_to_torch.printer_atom.c_cos(out: Symbol, indexing: defaultdict[Symbol, str], c_type: str, arg: Atom) tuple[set[str], set[Symbol], list[str]][source]

C cos operation.

Examples

>>> from sympy.abc import x
>>> from sympy.functions.elementary.trigonometric import cos
>>> 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, cos(x))], {}, {x}))
>>> func(np.array([np.nan]))
array([nan])
>>> func(np.array([0., 0.78539816, 2.35619449, 3.14159265, 3.92699082, 5.49778714, 6.28318531]))
array([ 1.        ,  0.70710678, -0.70710678, -1.        , -0.70710678,
        0.70710678,  1.        ])
>>> func(np.array([1+1j]))
array([0.83373003-0.98889771j])
>>>
cutcutcodec.core.compilation.sympy_to_torch.printer_atom.c_equality(out: Symbol, indexing: defaultdict[Symbol, str], c_type: str, *parts: Atom) tuple[set[str], set[Symbol], list[str]][source]

C == operation.

Examples

>>> from sympy.abc import x
>>> from sympy.core.relational import Equality
>>> 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, Equality(x, 0))], {}, {x}))
>>> func(np.array([-np.inf, -1.0, 0.0, 1.0, np.inf]))
array([0., 0., 1., 0., 0.])
>>>
cutcutcodec.core.compilation.sympy_to_torch.printer_atom.c_exp(out: Symbol, indexing: defaultdict[Symbol, str], c_type: str, arg: Atom) tuple[set[str], set[Symbol], list[str]][source]

C exp operation.

Examples

>>> from sympy.abc import x
>>> from sympy.functions.elementary.trigonometric import exp
>>> 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, exp(x))], {}, {x}))
>>> func(np.array([np.nan]))
array([nan])
>>> func(np.array([-np.inf, -1.0, 0.0, 1.0, np.inf]))
array([0.        , 0.36787944, 1.        , 2.71828183,        inf])
>>> func(np.array([1+1j]))
array([1.46869394+2.28735529j])
>>>
cutcutcodec.core.compilation.sympy_to_torch.printer_atom.c_greaterthan(out: Symbol, indexing: defaultdict[Symbol, str], c_type: str, *parts: Atom) tuple[set[str], set[Symbol], list[str]][source]

C >= operation, with module comparaison for complex numbers.

Examples

>>> from sympy.abc import x
>>> 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, x >= 0)], {}, {x}))
>>> func(np.array([-np.inf, -1.0, 0.0, 1.0, np.inf]))
array([0., 0., 1., 1., 1.])
>>>
cutcutcodec.core.compilation.sympy_to_torch.printer_atom.c_lessthan(out: Symbol, indexing: defaultdict[Symbol, str], c_type: str, *parts: Atom) tuple[set[str], set[Symbol], list[str]][source]

C <= operation, with module comparaison for complex numbers.

Examples

>>> from sympy.abc import x
>>> 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, x <= 0)], {}, {x}))
>>> func(np.array([-np.inf, -1.0, 0.0, 1.0, np.inf]))
array([1., 1., 1., 0., 0.])
>>>
cutcutcodec.core.compilation.sympy_to_torch.printer_atom.c_log(out: Symbol, indexing: defaultdict[Symbol, str], c_type: str, arg: Atom) tuple[set[str], set[Symbol], list[str]][source]

C log operation.

Examples

>>> from sympy.abc import x
>>> from sympy.functions.elementary.exponential import log
>>> 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, log(x))], {}, {x}))
>>> func(np.array([np.nan]))
array([nan])
>>> func(np.array([0.0, 1.0, np.e, np.inf]))
array([-inf,   0.,   1.,  inf])
>>>
cutcutcodec.core.compilation.sympy_to_torch.printer_atom.c_max(out: Symbol, indexing: defaultdict[Symbol, str], c_type: str, *parts: Atom) tuple[set[str], set[Symbol], list[str]][source]

C maximum operation.

Examples

>>> from collections import defaultdict
>>> from sympy.abc import x, y
>>> from sympy.functions.elementary.miscellaneous import Max
>>> from cutcutcodec.core.compilation.sympy_to_torch.printer import _print_atomic
>>> _print_atomic(Max(2, x), x, defaultdict(lambda: ""), "float")
(set(), set(), ['x = 2.0f > x ? 2.0f : x;'])
>>> _print_atomic(Max(2, x), y, defaultdict(lambda: ""), "float complex")
(set(), set(), ['y = (crealf((2.0f + 0.0f * _Complex_I))... ? (2.0f + 0.0f * _Complex_I) : x;'])
>>> _print_atomic(Max(2, x, y), x, defaultdict(lambda: ""), "float")
(set(), set(), ['x = 2.0f > x ? 2.0f : x;', 'x = x > y ? x : y;'])
>>>
cutcutcodec.core.compilation.sympy_to_torch.printer_atom.c_min(out: Symbol, indexing: defaultdict[Symbol, str], c_type: str, *parts: Atom) tuple[set[str], set[Symbol], list[str]][source]

C minimum operation.

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

C * operation.

Examples

>>> from collections import defaultdict
>>> from sympy.abc import x, y, z
>>> from cutcutcodec.core.compilation.sympy_to_torch.printer import _print_atomic
>>> _print_atomic(2 * x, x, defaultdict(lambda: ""), "float")
(set(), set(), ['x *= 2.0f;'])
>>> _print_atomic(2 * x, y, defaultdict(lambda: ""), "float")
(set(), set(), ['y = 2.0f * x;'])
>>> _print_atomic(2 * x * y, z, defaultdict(lambda: ""), "float")
(set(), set(), ['z = 2.0f * x;', 'z *= y;'])
>>>
cutcutcodec.core.compilation.sympy_to_torch.printer_atom.c_pow(out: Symbol, indexing: defaultdict[Symbol, str], c_type: str, base: Atom, exp: Atom) tuple[set[str], set[Symbol], list[str]][source]

C ** operation.

Examples

>>> from collections import defaultdict
>>> from sympy.abc import x, y, z
>>> 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
>>>
>>> _print_atomic(x**y, z, defaultdict(lambda: ""), "float")
(set(), set(), ['z = powf(x, y);'])
>>> _print_atomic(1/x, z, defaultdict(lambda: ""), "float")
(set(), set(), ['z = 1.0f / x;'])
>>> _print_atomic(x**2, y, defaultdict(lambda: ""), "float")
(set(), set(), ['y = x * x;'])
>>> _print_atomic(x**.5, y, defaultdict(lambda: ""), "float")
(set(), set(), ['y = sqrtf(x);'])
>>> _print_atomic(x**(1/3), y, defaultdict(lambda: ""), "float")
(set(), set(), ['y = cbrtf(x);'])
>>> _print_atomic(x**(-7/2), y, defaultdict(lambda: ""), "double")
(set(), {_buf}, ['y = x * x;', '_buf = x * y;', 'y *= y;', 'y *= _buf;', 'y = 1.0 / sqrt(y);'])
>>>
>>> func = _lambdify_c(_printer([(x, x**y)], {}, {x, y}))
>>> func(
...     np.array([2.0   , np.nan, 0.0, 0.0,  0.0, -0.0,    2.0,     2.0]),
...     np.array([np.nan,    2.0, 0.0, 1.0, -1.0, -1.0, np.inf, -np.inf])
... )
array([ nan,  nan,   1.,   0.,  inf, -inf,  inf,   0.])
>>> func(np.array([1+1j]), np.array([1+1j]))
array([0.27395725+0.58370076j])
>>> func = _lambdify_c(_printer([(x, x**2)], {}, {x}))
>>> func(np.array([np.nan, -np.inf, -2.0, -1.0, 1.0, 2.0, np.inf]))
array([nan, inf,  4.,  1.,  1.,  4., inf])
>>> func = _lambdify_c(_printer([(x, 1/x)], {}, {x}))
>>> func(np.array([np.nan, -np.inf, -2.0, -1.0, -0.0, 0.0, 1.0, 2.0, np.inf]))
array([ nan, -0. , -0.5, -1. , -inf,  inf,  1. ,  0.5,  0. ])
>>> func = _lambdify_c(_printer([(x, x**.5)], {}, {x}))
>>> func(np.array([np.nan, -np.inf, -1.0, 0.0, 1.0, 4.0, np.inf]))
array([nan, nan, nan,  0.,  1.,  2., inf])
>>> func = _lambdify_c(_printer([(x, x**(1/3))], {}, {x}))
>>> func(np.array([np.nan, -np.inf, -8.0, -1.0, 0.0, 1.0, 8.0, np.inf]))
array([ nan, -inf,  -2.,  -1.,   0.,   1.,   2.,  inf])
>>> func = _lambdify_c(_printer([(x, x**(-7/2))], {}, {x}))
>>> func(np.array([0.820335356007638]))
array([2.])
>>>
cutcutcodec.core.compilation.sympy_to_torch.printer_atom.c_pow_pos_integer(out: Symbol, indexing: defaultdict[Symbol, str], c_type: str, base: Atom, exp: Atom) tuple[set[str], set[Symbol], list[str]][source]

Manage the special case of power where the exponant is a positive integer.

Examples

>>> from collections import defaultdict
>>> from sympy.abc import x, y
>>> from cutcutcodec.core.compilation.sympy_to_torch.printer import c_pow_pos_integer
>>>
>>> # case allocation in new var
>>> c_pow_pos_integer(y, defaultdict(lambda: ""), "float", x, 0)
(set(), set(), ['y = 1.0f;'])
>>> c_pow_pos_integer(y, defaultdict(lambda: ""), "float", x, 1)
(set(), set(), ['y = x;'])
>>> c_pow_pos_integer(y, defaultdict(lambda: ""), "float", x, 2)
(set(), set(), ['y = x * x;'])
>>> c_pow_pos_integer(y, defaultdict(lambda: ""), "float", x, 3)
(set(), set(), ['y = x * x;', 'y *= x;'])
>>> c_pow_pos_integer(y, defaultdict(lambda: ""), "float", x, 4)
(set(), set(), ['y = x * x;', 'y *= y;'])
>>> c_pow_pos_integer(y, defaultdict(lambda: ""), "float", x, 5)
(set(), set(), ['y = x * x;', 'y *= y;', 'y *= x;'])
>>> c_pow_pos_integer(y, defaultdict(lambda: ""), "float", x, 6)
(set(), {_buf}, ['y = x * x;', '_buf = y;', 'y *= y;', 'y *= _buf;'])
>>> c_pow_pos_integer(y, defaultdict(lambda: ""), "float", x, 7)
(set(), {_buf}, ['y = x * x;', '_buf = x * y;', 'y *= y;', 'y *= _buf;'])
>>> c_pow_pos_integer(y, defaultdict(lambda: ""), "float", x, 8)
(set(), set(), ['y = x * x;', 'y *= y;', 'y *= y;'])
>>> c_pow_pos_integer(y, defaultdict(lambda: ""), "float", x, 9)
(set(), set(), ['y = x * x;', 'y *= y;', 'y *= y;', 'y *= x;'])
>>> c_pow_pos_integer(y, defaultdict(lambda: ""), "float", x, 10)
(set(), {_buf}, ['y = x * x;', '_buf = y;', 'y *= y;', 'y *= y;', 'y *= _buf;'])
>>>
>>> # case allocation inplace
>>> c_pow_pos_integer(x, defaultdict(lambda: ""), "float", x, 0)
(set(), set(), ['x = 1.0f;'])
>>> c_pow_pos_integer(x, defaultdict(lambda: ""), "float", x, 1)
(set(), set(), ['x = x;'])
>>> c_pow_pos_integer(x, defaultdict(lambda: ""), "float", x, 2)
(set(), set(), ['x *= x;'])
>>> c_pow_pos_integer(x, defaultdict(lambda: ""), "float", x, 3)
(set(), {_buf}, ['_buf = x;', 'x *= x;', 'x *= _buf;'])
>>> c_pow_pos_integer(x, defaultdict(lambda: ""), "float", x, 4)
(set(), set(), ['x *= x;', 'x *= x;'])
>>> c_pow_pos_integer(x, defaultdict(lambda: ""), "float", x, 5)
(set(), {_buf}, ['_buf = x;', 'x *= x;', 'x *= x;', 'x *= _buf;'])
>>> c_pow_pos_integer(x, defaultdict(lambda: ""), "float", x, 6)
(set(), {_buf}, ['x *= x;', '_buf = x;', 'x *= x;', 'x *= _buf;'])
>>> c_pow_pos_integer(x, defaultdict(lambda: ""), "float", x, 7)
(set(), {_buf}, ['_buf = x;', 'x *= x;', '_buf *= x;', 'x *= x;', 'x *= _buf;'])
>>> c_pow_pos_integer(x, defaultdict(lambda: ""), "float", x, 8)
(set(), set(), ['x *= x;', 'x *= x;', 'x *= x;'])
>>> c_pow_pos_integer(x, defaultdict(lambda: ""), "float", x, 9)
(set(), {_buf}, ['_buf = x;', 'x *= x;', 'x *= x;', 'x *= x;', 'x *= _buf;'])
>>> c_pow_pos_integer(x, defaultdict(lambda: ""), "float", x, 10)
(set(), {_buf}, ['x *= x;', '_buf = x;', 'x *= x;', 'x *= x;', 'x *= _buf;'])
>>>
cutcutcodec.core.compilation.sympy_to_torch.printer_atom.c_sin(out: Symbol, indexing: defaultdict[Symbol, str], c_type: str, arg: Atom) tuple[set[str], set[Symbol], list[str]][source]

C sin operation.

Examples

>>> from sympy.abc import x
>>> from sympy.functions.elementary.trigonometric import sin
>>> 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, sin(x))], {}, {x}))
>>> func(np.array([np.nan]))
array([nan])
>>> func(np.array([0.78539816, 1.57079633, 2.35619449, 3.92699082, 4.71238898, 5.49778714]))
array([ 0.70710678,  1.        ,  0.70710678, -0.70710678, -1.        ,
       -0.70710678])
>>> func(np.array([1+1j]))
array([1.29845758+0.63496391j])
>>>
cutcutcodec.core.compilation.sympy_to_torch.printer_atom.c_strictgreaterthan(out: Symbol, indexing: defaultdict[Symbol, str], c_type: str, *parts: Atom) tuple[set[str], set[Symbol], list[str]][source]

C > operation, with module comparaison for complex numbers.

Examples

>>> from sympy.abc import x
>>> 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, x > 0)], {}, {x}))
>>> func(np.array([-np.inf, -1.0, 0.0, 1.0, np.inf]))
array([0., 0., 0., 1., 1.])
>>>
cutcutcodec.core.compilation.sympy_to_torch.printer_atom.c_strictlessthan(out: Symbol, indexing: defaultdict[Symbol, str], c_type: str, *parts: Atom) tuple[set[str], set[Symbol], list[str]][source]

C < operation, with module comparaison for complex numbers.

Examples

>>> from sympy.abc import x
>>> 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, x < 0)], {}, {x}))
>>> func(np.array([-np.inf, -1.0, 0.0, 1.0, np.inf]))
array([1., 1., 0., 0., 0.])
>>>