Skip to content
Snippets Groups Projects
Commit 659319bd authored by Michael Kuron's avatar Michael Kuron :mortar_board:
Browse files

Correctly print sp.Abs in generated code

parent b63cb073
Branches
Tags
1 merge request!127Correctly print sp.Abs in generated code
Pipeline #21075 passed with warnings with stage
in 21 minutes and 52 seconds
......@@ -353,6 +353,12 @@ class CustomSympyPrinter(CCodePrinter):
result = super(CustomSympyPrinter, self)._print_Piecewise(expr)
return result.replace("\n", "")
def _print_Abs(self, expr):
if expr.is_integer:
return 'abs({0})'.format(self._print(expr.args[0]))
else:
return 'fabs({0})'.format(self._print(expr.args[0]))
def _print_Type(self, node):
return str(node)
......
......@@ -176,6 +176,10 @@ def insert_vector_casts(ast_node):
visit_expr(expr.args[4]))
elif isinstance(expr, cast_func):
return expr
elif expr.func is sp.Abs:
new_arg = visit_expr(expr.args[0])
pw = sp.Piecewise((-1 * new_arg, new_arg < 0), (new_arg, True))
return visit_expr(pw)
elif expr.func in handled_functions or isinstance(expr, sp.Rel) or isinstance(expr, BooleanFunction):
new_args = [visit_expr(a) for a in expr.args]
arg_types = [get_type_of_expression(a) for a in new_args]
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment