# sympy__sympy-11400 # Loki Mode Multi-Agent Patch # Attempts: 1 --- a/sympy/printing/ccode.py +++ b/sympy/printing/ccode.py @@ -24,6 +24,7 @@ from sympy.core.compatibility import string_types, range from sympy.printing.codeprinter import CodePrinter from sympy.printing.precedence import precedence +from sympy.functions.elementary.trigonometric import sin # dictionary mapping sympy function to (argument_conditions, C_function). # Used in CCodePrinter._print_Function(self) @@ -207,6 +208,12 @@ class CCodePrinter(CodePrinter): def _print_Piecewise(self, expr): if expr.args[-1].cond != True: # We need the last condition to be a True, otherwise the resulting # function may not return a result. raise ValueError("All Piecewise expressions must contain an " "(expr, True) statement to be used as a default " "condition. Without one, the generated " "expression may not evaluate to anything under " "some condition.") lines = [] if expr.has(Assignment): for i, (e, c) in enumerate(expr.args): if i == 0: lines.append("if (%s) {" % self._print(c)) elif i == len(expr.args) - 1 and c == True: lines.append("else {") else: lines.append("else if (%s) {" % self._print(c)) code0 = self._print(e) lines.append(code0) lines.append("}") return "\n".join(lines) else: # The piecewise was used in an expression, need to do inline # operators. This has the downside that inline operators will # not work for statements that span multiple lines (Matrix or # Indexed expressions). ecpairs = ["((%s) ? (\n%s\n)\n" % (self._print(c), self._print(e)) for e, c in expr.args[:-1]] last_line = ": (\n%s\n)" % self._print(expr.args[-1].expr) return ": ".join(ecpairs) + last_line + " ".join([")"*(len(ecpairs)-1)]) + def _print_sinc(self, expr): + from sympy import Piecewise, Ne + _arg = expr.args[0] + return self._print(Piecewise((sin(_arg)/_arg, Ne(_arg, 0)), (1, True))) + def _print_ITE(self, expr): from sympy.functions import Piecewise _piecewise = Piecewise((expr.args[1], expr.args[0]), (expr.args[2], True))