Skip to content
Snippets Groups Projects
Closed Divisions are printed using `pow`
  • View options
  • Divisions are printed using `pow`

  • View options
  • Closed Issue created by Daniel Bauer

    Description

    pystencils prints divisions like 1/x as pow(x, -1.0).

    The floating point pow function is of course undesirable due to performance concerns.

    It looks like pystencils intends to print powers with small integer exponent using multiplications and divisions. However, the types of powers are collated between the base and exponent.

    Note that sympy converts divisions to powers automatically.

    MWE

    import pystencils as ps
    from pystencils import CreateKernelConfig
    from pystencils.astnodes import Block, KernelFunction, SympyAssignment
    from sympy.abc import x, y
    
    equ = SympyAssignment(y, 1/x)
    typed_equ = ps.typing.transformations.add_types(equ, CreateKernelConfig())
    print(typed_equ)
    
    kernel = KernelFunction(
      Block([typed_equ]),
      ps.Target.CPU,
      ps.Backend.C,
      ps.cpu.cpujit.make_python_function,
      None,
    )
    code = ps.get_code_str(kernel)
    print(code)

    Actual output

    y  x**CastFunc(-1, double)
    FUNC_PREFIX void kernel(double x)
    {
       const double y = pow(x, -1.0);
    }

    Expected output

    y  x**CastFunc(-1, int)
    FUNC_PREFIX void kernel(double x)
    {
       const double y = 1.0 / x;
    }

    Linked items ... 0

  • Activity

    • All activity
    • Comments only
    • History only
    • Newest first
    • Oldest first
    Loading Loading Loading Loading Loading Loading Loading Loading Loading Loading