From 9c2f44173a93545aa545d4d3559f30b001d33819 Mon Sep 17 00:00:00 2001 From: Rolf Heilemann Myhre Date: Thu, 19 Oct 2023 16:01:59 +0200 Subject: [PATCH] Ensure Loki versions of Sum and Product --- loki/transform/transform_scalar_syntax.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/loki/transform/transform_scalar_syntax.py b/loki/transform/transform_scalar_syntax.py index 64b1f7522..9a1931f8e 100644 --- a/loki/transform/transform_scalar_syntax.py +++ b/loki/transform/transform_scalar_syntax.py @@ -62,6 +62,7 @@ def product_value(expr): Note: Negative numbers and subtractions in Sums are represented as Product of the integer -1 and the symbol. This complicates matters. + Note: Ensure that a Loki Product is returned, not a pymbolic Product Parameters ---------- @@ -84,11 +85,11 @@ def product_value(expr): return m if m > 1: - m = IntLiteral(m) + new_children = [IntLiteral(m)] + new_children elif m < -1: - m = Product((-1, IntLiteral(abs(m)))) + new_children = [-1, IntLiteral(abs(m))] + new_children - return m*Product(as_tuple(new_children)) + return Product(as_tuple(new_children)) return expr @@ -100,6 +101,8 @@ def simplify_sum(expr): If the sum can be reduced to a number, it returns an IntLiteral If the Sum reduces to one expression, it returns that expression + Note: Ensure that a Loki Sum is returned, not a pymbolic Sum + Parameters ---------- expr: any pymbolic expression @@ -188,7 +191,7 @@ def construct_length(xrange, caller, call): new_start = process_symbol(xrange.start, caller, call) new_stop = process_symbol(xrange.stop, caller, call) - return simplify_sum(single_sum(new_stop) - new_start + IntLiteral(1)) + return single_sum(new_stop) - new_start + IntLiteral(1) def fix_scalar_syntax(routine): @@ -208,6 +211,10 @@ def fix_scalar_syntax(routine): call myroutine(a(i:i+5,j) + Note: Using the __add__ and __mul__ functions of Sum and Product, respectively, + returns the pymbolic.primitives version of the objuect, not the loki.expressions version. + simplify_sum and product_value returns loki versions, so this is currently not an issue, + but this can cause unexpected behaviour Parameters ----------