cutcutcodec.core.compilation.sympy_to_torch.printer_atom.c_pow¶
- 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.]) >>>