def _print_ImmutableDenseMatrix(self, expr): sub_exprs, simplified = cse(expr) lines = [] for var, sub_expr in sub_exprs: lines.append('double ' + self._print(Assignment(var, sub_expr))) M = MatrixSymbol('T_others', *expr.shape) return '\n'.join(lines) + '\n' + self._print(Assignment(M, simplified[0]))
def py_write_code(lst, idx, name, filename): import os import sympy as sym from sympy.printing.ccode import C99CodePrinter as printer from sympy.printing.codeprinter import Assignment mat = sym.Matrix([lst]) # row vector of terms sub_exprs, simplified_rhs = sym.cse(mat) # optimise code with open(os.getcwd() + '/' + filename, 'w') as out: for lhs, rhs in sub_exprs: out.write(printer().doprint(Assignment(lhs, rhs)) + '\n') # this block for scalars if len(idx) == 0: for index, rhs in enumerate(simplified_rhs[0]): lhs = sym.Symbol(name) out.write(printer().doprint(Assignment(lhs, rhs)) + '\n') # this block for matrices else: for index, rhs in enumerate(simplified_rhs[0]): lhs = sym.Symbol(name + ' (' + idx[index][1:-1] + ')') # lhs = sym.Symbol(name+' '+(idx[index]).replace(', ','][')) out.write(printer().doprint(Assignment(lhs, rhs)) + '\n')
def _print_list(self, list_of_exprs): if all(isinstance(x, sp.Eq) for x in list_of_exprs): list_of_lhs = [x.lhs for x in list_of_exprs] list_of_rhs = [x.rhs for x in list_of_exprs] common_variables, list_of_rhs = sp.cse( list_of_rhs, symbols=sp.numbered_symbols(prefix='com')) lines = [] for variable, expression in common_variables: ass = Assignment(variable, expression) lines.append('const double ' + self._print(ass)) for i in range(len(list_of_rhs)): ass = Assignment(list_of_lhs[i], list_of_rhs[i]) lines.append(self._print(ass)) return '\n'.join(lines)
def cdb_write_code(ex, name, filename, num): import os import sympy as sym from sympy.printing.ccode import C99CodePrinter as printer from sympy.printing.codeprinter import Assignment # this block for scalars if num == 0: mat = sym.Matrix([ex._sympy_()]) # row vector with one term sub_exprs, simplified_rhs = sym.cse(mat) # optimise code with open(os.getcwd() + '/' + filename, 'w') as out: for lhs, rhs in sub_exprs: out.write(printer().doprint(Assignment(lhs, rhs)) + '\n') for index, rhs in enumerate(simplified_rhs[0]): lhs = sym.Symbol(name) out.write(printer().doprint(Assignment(lhs, rhs)) + '\n') # this block for tensors else: idx = [] # indices in the form [{x, x}, {x, y} ...] lst = [] # corresponding terms [termxx, termxy, ...] for i in range(len(ex[num])): # num = number of free indices idx.append(str(ex[num][i][0]._sympy_())) # indices for this term lst.append(str(ex[num][i][1]._sympy_())) # the matching term mat = sym.Matrix([lst]) # row vector of terms sub_exprs, simplified_rhs = sym.cse(mat) # optimise code with open(os.getcwd() + '/' + filename, 'w') as out: for lhs, rhs in sub_exprs: out.write(printer().doprint(Assignment(lhs, rhs)) + '\n') for index, rhs in enumerate(simplified_rhs[0]): lhs = sym.Symbol(name + ' (' + idx[index][1:-1] + ')') # lhs = sym.Symbol(name+' '+(idx[index]).replace(', ','][')) out.write(printer().doprint(Assignment(lhs, rhs)) + '\n')
def _print_Assignment(self, expr): from sympy.functions.elementary.piecewise import Piecewise from sympy.tensor.indexed import IndexedBase # Copied from codeprinter, but remove special MatrixSymbol treatment lhs = expr.lhs rhs = expr.rhs # We special case assignments that take multiple lines if not self._settings["inline"] and isinstance(expr.rhs, Piecewise): # Here we modify Piecewise so each expression is now # an Assignment, and then continue on the print. expressions = [] conditions = [] for (e, c) in rhs.args: expressions.append(Assignment(lhs, e)) conditions.append(c) temp = Piecewise(*zip(expressions, conditions)) return self._print(temp) if self._settings["contract"] and (lhs.has(IndexedBase) or rhs.has(IndexedBase)): # Here we check if there is looping to be done, and if so # print the required loops. return self._doprint_loops(rhs, lhs) else: lhs_code = self._print(lhs) rhs_code = self._print(rhs) return self._get_statement("%s = %s" % (lhs_code, rhs_code))
def _print_Assignment(self, expr): from sympy.functions.elementary.piecewise import Piecewise from sympy.matrices.expressions.matexpr import MatrixSymbol from sympy.tensor.indexed import IndexedBase lhs = expr.lhs rhs = expr.rhs # We special case assignments that take multiple lines if isinstance(expr.rhs, Piecewise): # Here we modify Piecewise so each expression is now # an Assignment, and then continue on the print. expressions = [] conditions = [] for (e, c) in rhs.args: expressions.append(Assignment(lhs, e)) conditions.append(c) temp = Piecewise(*zip(expressions, conditions)) return self._print(temp) elif isinstance(lhs, MatrixSymbol): # Here we form an Assignment for each element in the array, # printing each one. lines = [] for (i, j) in self._traverse_matrix_indices(lhs): temp = Assignment(lhs[i, j], rhs[i, j]) if not (rhs[i, j] == 0): code0 = self._print(temp) lines.append(code0) return "\n".join(lines) elif self._settings["contract"] and (lhs.has(IndexedBase) or rhs.has(IndexedBase)): # Here we check if there is looping to be done, and if so # print the required loops. return self._doprint_loops(rhs, lhs) else: lhs_code = self._print(lhs) rhs_code = self._print(rhs) return self._get_statement("%s = %s" % (lhs_code, rhs_code))
def test_Assignment(): x, y = symbols("x, y") A = MatrixSymbol('A', 3, 1) mat = Matrix([1, 2, 3]) B = IndexedBase('B') n = symbols("n", integer=True) i = Idx("i", n) # Here we just do things to show they don't error Assignment(x, y) Assignment(x, 0) Assignment(A, mat) Assignment(A[1, 0], 0) Assignment(A[1, 0], x) Assignment(B[i], x) Assignment(B[i], 0) # Here we test things to show that they error # Matrix to scalar raises(ValueError, lambda: Assignment(B[i], A)) raises(ValueError, lambda: Assignment(B[i], mat)) raises(ValueError, lambda: Assignment(x, mat)) raises(ValueError, lambda: Assignment(x, A)) raises(ValueError, lambda: Assignment(A[1, 0], mat)) # Scalar to matrix raises(ValueError, lambda: Assignment(A, x)) raises(ValueError, lambda: Assignment(A, 0)) # Non-atomic lhs raises(TypeError, lambda: Assignment(mat, A)) raises(TypeError, lambda: Assignment(0, x)) raises(TypeError, lambda: Assignment(x * x, 1)) raises(TypeError, lambda: Assignment(A + A, mat)) raises(TypeError, lambda: Assignment(B, 0))