Skip to content

Commit

Permalink
Ensure Loki versions of Sum and Product
Browse files Browse the repository at this point in the history
  • Loading branch information
rolfhm committed Oct 19, 2023
1 parent c2ec915 commit 9c2f441
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions loki/transform/transform_scalar_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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
----------
Expand Down

0 comments on commit 9c2f441

Please sign in to comment.