def test_printing(): for c in (LatexPrinter, LatexPrinter(), MathMLPrinter, PrettyPrinter, prettyForm, stringPict, stringPict("a"), Printer, Printer(), PythonPrinter, PythonPrinter()): #FIXME-py3k: sympy/printing/printer.py", line 220, in order #FIXME-py3k: return self._settings['order'] #FIXME-py3k: KeyError: 'order' check(c)
def __init__(self, **kwargs): """Initialize the printer. All keyword arguments are forwarded to the base class :py:class:`BasePrinter`. """ super().__init__(PythonPrinter(), **kwargs)
def _print_Function(self, expr): if expr.func == re: arg = expr.args[0] if hasattr(arg, 'name'): return arg.name + '_re' return str(arg.func) + '(' + ','.join( [self._print(x) for x in arg.args]) + ')' if expr.func == im: arg = expr.args[0] if hasattr(arg, 'name'): return arg.name + '_im' return str(arg.func) + '(' + ','.join( [self._print(x) for x in arg.args]) + ')' return PythonPrinter._print_Function(self, expr)
def test_printing(): for c in ( LatexPrinter, LatexPrinter(), MathMLContentPrinter, MathMLPresentationPrinter, PrettyPrinter, prettyForm, stringPict, stringPict("a"), Printer, Printer(), PythonPrinter, PythonPrinter(), ): check(c)
def __init__( self, zeros='zeros', dtype=None, einsum='einsum', extr_unary=True, add_globals=None, **kwargs ): """Initialize the printer. """ globals_ = { 'einsum': einsum } if add_globals is not None: globals_.update(add_globals) super().__init__( PythonPrinter(), extr_unary=extr_unary, add_globals=globals_, **kwargs ) self._zeros = zeros self._dtype = dtype self._einsum = einsum
C_UFs = { "Heaviside": "Heaviside", "DiracDelta": "DiracDelta", "Abs": "fabsf", "Min": "fminf", "Max": "fmaxf" } Py_UFs = { "Abs": "abs", "Min": "min", "Max": "max", } pyprint = PythonPrinter(None) def ccode(expr): return sp.ccode(expr, user_functions=C_UFs) def pycode(expr): return pyprint._str(expr.subs(Py_UFs)) def ccode_matrix(matexpr, indent): lines = [] H, W = matexpr.shape indent = ' ' * indent for j in range(H):
def __init__(self, skip_multiplication_by_one=False): self.skip_multiplication_by_one = skip_multiplication_by_one PythonPrinter.__init__(self)
def test_base_printer_ctx(simple_drudge, colourful_tensor): """Test the context formation facility in base printer.""" dr = simple_drudge p = dr.names tensor = colourful_tensor # Process indexed names by mangling the base name. printer = BasePrinter( PythonPrinter(), mangle_base(lambda base, indices: base + str(len(indices)))) ctx = printer.transl(tensor) def check_range(ctx, index): """Check the range information in a context for a index.""" assert ctx.index == index assert ctx.range == p.R assert ctx.lower == '0' assert ctx.upper == 'n' assert ctx.size == 'n' assert ctx.base == 'x2' for i, j in zip(ctx.indices, ['a', 'b']): check_range(i, j) continue assert len(ctx.terms) == 2 for term in ctx.terms: if len(term.sums) == 0: # The transpose term. assert term.phase == '+' assert term.numerator == '2*r' assert term.denominator == '(3*s)' assert len(term.indexed_factors) == 1 factor = term.indexed_factors[0] assert factor.base == 'u2' for i, j in zip(factor.indices, ['b', 'a']): check_range(i, j) continue assert len(term.other_factors) == 0 elif len(term.sums) == 1: check_range(term.sums[0], 'c') assert term.phase == '-' assert term.numerator == '1' assert term.denominator == '2' assert len(term.indexed_factors) == 2 for factor in term.indexed_factors: if factor.base == 'u2': expected = ['a', 'c'] elif factor.base == 'v2': expected = ['c', 'b'] else: assert False for i, j in zip(factor.indices, expected): check_range(i, j) continue continue assert len(term.other_factors) == 1 assert term.other_factors[0] == 'c**2' else: assert False
def test_events_generation(eval_seq_deps): """Test the event generation facility in the base printer.""" eval_seq = eval_seq_deps with patch.object(BasePrinter, '__abstractmethods__', frozenset()): printer = BasePrinter(PythonPrinter()) events = printer.form_events(eval_seq) i1 = IndexedBase('I1') i2 = IndexedBase('I2') i3 = Symbol('I3') r1 = IndexedBase('R1') r2 = IndexedBase('R2') events.reverse() # For easy popping from front. for i in [i1, i2, i3]: event = events.pop() assert isinstance(event, TensorDecl) assert event.comput.target == i continue event = events.pop() assert isinstance(event, BeginBody) event = events.pop() assert isinstance(event, BeforeComp) assert event.comput.target == i1 event = events.pop() assert isinstance(event, CompTerm) assert event.comput.target == i1 assert event.term_idx == 0 # I1 drives I3. event = events.pop() assert isinstance(event, BeforeComp) assert event.comput.target == i3 event = events.pop() assert isinstance(event, CompTerm) assert event.comput.target == i3 assert event.term_idx == 0 # I3, I1, drives the first term of R1. event = events.pop() assert isinstance(event, BeforeComp) assert event.comput.target == r1 event = events.pop() assert isinstance(event, CompTerm) assert event.comput.target == r1 assert event.term_idx == 0 # Now I3 should be out of dependency. event = events.pop() assert isinstance(event, OutOfUse) assert event.comput.target == i3 # Another one driven by I1. event = events.pop() assert isinstance(event, BeforeComp) assert event.comput.target == r2 event = events.pop() assert isinstance(event, CompTerm) assert event.comput.target == r2 assert event.term_idx == 0 # I1 no longer needed any more. event = events.pop() assert isinstance(event, OutOfUse) assert event.comput.target == i1 # Nothing driven. event = events.pop() assert isinstance(event, BeforeComp) assert event.comput.target == i2 event = events.pop() assert isinstance(event, CompTerm) assert event.comput.target == i2 assert event.term_idx == 0 # The last term in R1. event = events.pop() assert isinstance(event, CompTerm) assert event.comput.target == r1 assert event.term_idx == 1 # Finally, free I2. event = events.pop() assert isinstance(event, OutOfUse) assert event.comput.target == i2 event = events.pop() assert isinstance(event, EndBody) assert len(events) == 0