82
85
last_line = self._print(expr.args[-1].expr) + ")"*num_par
83
86
return result+last_line
88
def _print_Pow(self, expr, rational=False):
89
PREC = _precedence(expr)
90
if expr.exp.is_integer and int(expr.exp) == 1:
91
return self.parenthesize(expr.base, PREC)
92
if expr.exp is sp.S.NegativeOne:
93
return "1.0/{0}".format(self.parenthesize(expr.base, PREC))
94
if expr.exp.is_integer and int(expr.exp) in [2, 3]:
95
return "({0})".format(\
96
"*".join(self.parenthesize(expr.base, PREC) \
97
for i in xrange(int(expr.exp))), PREC)
98
if expr.exp.is_integer and int(expr.exp) in [-2, -3]:
99
return "1.0/({0})".format(\
100
"*".join(self.parenthesize(expr.base, PREC) \
101
for i in xrange(int(expr.exp))), PREC)
102
if expr.exp is sp.S.Half and not rational:
103
return "{0}sqrt({1})".format(self._namespace,
104
self._print(expr.base))
106
return "1/{0}sqrt({1})".format(self._namespace,
107
self._print(expr.base))
108
if self._namespace == "ufl.":
109
return "{0}elem_pow({1}, {2})".format(self._namespace,
110
self._print(expr.base),
111
self._print(expr.exp))
112
return "{0}pow({1}, {2})".format(self._namespace,
113
self._print(expr.base),
114
self._print(expr.exp))
116
def _print_Mul(self, expr):
117
from sympytools import ModelSymbol as _ModelSymbol
119
prec = _precedence(expr)
121
if self.order not in ('old', 'none'):
122
args = expr.as_ordered_factors()
124
# use make_args in case expr was something like -x -> x
125
args = sp.Mul.make_args(expr)
127
if _coeff_isneg(expr):
128
# If negative and -1 is the first arg: remove it
129
if args[0].is_integer and int(args[0]) == 1:
132
args = (-args[0],) + args[1:]
137
# If first argument is Mul we do not want to add a parentesize
138
if isinstance(args[0], sp.Mul):
141
a = [] # items in the numerator
142
b = [] # items that are in the denominator (if any)
144
# Gather args for numerator/denominator
146
if item.is_commutative and item.is_Pow and item.exp.is_Rational and item.exp.is_negative:
148
b.append(sp.Pow(item.base, -item.exp, evaluate=False))
150
b.append(sp.Pow(item.base, -item.exp))
151
elif item.is_Rational and item is not sp.S.Infinity:
153
a.append(sp.Rational(item.p))
155
b.append(sp.Rational(item.q))
161
a_str = map(lambda x:self.parenthesize(x, prec), a)
162
b_str = map(lambda x:self.parenthesize(x, prec), b)
165
return sign + '*'.join(a_str)
167
if len(a) == 1 and not (a[0].is_Atom or a[0].is_Add):
168
return sign + "%s/"%a_str[0] + '*'.join(b_str)
170
return sign + '*'.join(a_str) + "/%s"%b_str[0]
172
return sign + '*'.join(a_str) + "/(%s)"%'*'.join(b_str)
85
174
class _CustomPythonCodePrinter(_CustomPythonPrinter):
88
176
def _print_sign(self, expr):
89
177
if self._namespace == "ufl.":
90
178
return "{0}sign({0})".format(self._namespace, \
119
207
expr.func.__name__.lower() + \
120
208
"({0})".format(self.stringify(expr.args, ", ")))
122
def _print_Pow(self, expr, rational=False):
123
PREC = _precedence(expr)
124
if expr.exp is sp.S.NegativeOne:
125
return "1.0/{0}".format(self.parenthesize(expr.base, PREC))
126
if expr.exp.is_integer and int(expr.exp) in [2, 3]:
127
return "({0})".format(\
128
"*".join(self.parenthesize(expr.base, PREC) \
129
for i in xrange(int(expr.exp))), PREC)
130
if expr.exp.is_integer and int(expr.exp) in [-2, -3]:
131
return "1.0/({0})".format(\
132
"*".join(self.parenthesize(expr.base, PREC) \
133
for i in xrange(int(expr.exp))), PREC)
134
if expr.exp is sp.S.Half and not rational:
135
return "{0}sqrt({1})".format(self._namespace,
136
self._print(expr.base))
138
return "1/{0}sqrt({1})".format(self._namespace,
139
self._print(expr.base))
140
if self._namespace == "ufl.":
141
return "{0}elem_pow({1}, {2})".format(self._namespace,
142
self._print(expr.base),
143
self._print(expr.exp))
144
return "{0}pow({1}, {2})".format(self._namespace,
145
self._print(expr.base),
146
self._print(expr.exp))
148
210
def _print_Piecewise(self, expr):
282
348
return '{0}^{1}'.format(self.parenthesize(expr.base, PREC),
283
349
self.parenthesize(expr.exp, PREC))
352
class _CustomLatexPrinter(_LatexPrinter):
353
def _print_Add(self, expr):
354
terms = list(expr.args)
355
tex = self._print(terms[0])
357
for term in terms[1:]:
358
out = self._print(term)
359
if out and out[0] != "-":
285
366
# Different math namespace python printer
286
_python_code_printer = {"":_CustomPythonCodePrinter(""),
367
_python_code_printer = {"":_CustomPythonCodePrinter("", ),
287
368
"np":_CustomPythonCodePrinter("np"),
288
369
"numpy":_CustomPythonCodePrinter("numpy"),
289
370
"math":_CustomPythonCodePrinter("math"),
290
371
"ufl":_CustomPythonCodePrinter("ufl"),}
292
_ccode_printer = _CustomCCodePrinter()
293
_cppcode_printer = _CustomCCodePrinter(cpp=True)
373
_ccode_printer = _CustomCCodePrinter(order="none")
374
_cppcode_printer = _CustomCCodePrinter(cpp=True, order="none")
294
375
_sympy_printer = _CustomPythonPrinter()
295
_matlab_printer = _CustomMatlabCodePrinter()
376
_matlab_printer = _CustomMatlabCodePrinter(order="none")
297
378
def ccode(expr, assign_to=None):
335
416
return "{0} = {1}".format(assign_to, ret)
418
def ccode(expr, assign_to=None):
420
Return a C-code representation of a sympy expression
422
ret = _ccode_printer.doprint(expr)
423
if assign_to is None:
425
return "{0} = {1}".format(assign_to, ret)
427
def latex(expr, **settings):
428
settings["order"] = "none"
429
return _CustomLatexPrinter(settings).doprint(expr)
431
latex.__doc__ = _sympy_latex.__doc__
337
433
octavecode = matlabcode
339
435
__all__ = [_name for _name in globals().keys() if _name[0] != "_"]