From dd28d03121a2b88f9e6bbe84217e928d8913cb26 Mon Sep 17 00:00:00 2001 From: Stephan Seitz <stephan.seitz@fau.de> Date: Thu, 10 Oct 2019 17:38:05 +0200 Subject: [PATCH] Fix printing of sp.Infinity/sp.NegativeInfinity For sympy, oo s a number. So pystencils prints a type infinity a double INFINITY as INFINITY.0 --- pystencils/backends/cbackend.py | 2 +- pystencils_tests/test_print_infinity.py | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 pystencils_tests/test_print_infinity.py diff --git a/pystencils/backends/cbackend.py b/pystencils/backends/cbackend.py index 7248846b..55606525 100644 --- a/pystencils/backends/cbackend.py +++ b/pystencils/backends/cbackend.py @@ -350,7 +350,7 @@ class CustomSympyPrinter(CCodePrinter): return "&(%s)" % self._print(expr.args[0]) elif isinstance(expr, cast_func): arg, data_type = expr.args - if isinstance(arg, sp.Number): + if isinstance(arg, sp.Number) and arg.is_finite: return self._typed_number(arg, data_type) else: return "((%s)(%s))" % (data_type, self._print(arg)) diff --git a/pystencils_tests/test_print_infinity.py b/pystencils_tests/test_print_infinity.py new file mode 100644 index 00000000..4b1e4888 --- /dev/null +++ b/pystencils_tests/test_print_infinity.py @@ -0,0 +1,22 @@ +import pytest + +import pystencils +from sympy import oo + + +@pytest.mark.parametrize('type', ('float32', 'float64', 'int64')) +@pytest.mark.parametrize('negative', (False, 'Negative')) +@pytest.mark.parametrize('target', ('cpu', 'gpu')) +def test_print_infinity(type, negative, target): + + x = pystencils.fields(f'x: {type}[1d]') + + if negative: + assignment = pystencils.Assignment(x.center, -oo) + else: + assignment = pystencils.Assignment(x.center, oo) + ast = pystencils.create_kernel(assignment, data_type=type, target=target) + + ast.compile() + + print(ast.compile().code) -- GitLab