def vpprint(expr, **settings): r"""Function for pretty printing of expressions generated in the sympy.physics vector package. Mainly used for expressions not inside a vector; the output of running scripts and generating equations of motion. Takes the same options as SymPy's pretty_print(); see that function for more information. Parameters ========== expr : valid sympy object SymPy expression to pretty print settings : args Same as pretty print Examples ======== Use in the same way as pprint """ mp = VectorPrettyPrinter(settings) print(mp.doprint(expr))
def test_vector_pretty_print(): # TODO : The unit vectors should print with subscripts but they just # print as `n_x` instead of making `x` a subscritp with unicode. pp = VectorPrettyPrinter() expected = (u' 2\na *\x1b[94m\x1b[1mn_x\x1b[0;0m\x1b[0;0m + ' u'b*\x1b[94m\x1b[1mn_y\x1b[0;0m\x1b[0;0m + ' u'c\u22c5sin(\u03b1)*\x1b[94m\x1b[1mn_z\x1b[0;0m\x1b[0;0m') assert expected == pp.doprint(v) assert expected == v._pretty().render() expected = (u'\u03b1*\x1b[94m\x1b[1mn_x\x1b[0;0m\x1b[0;0m + ' u'sin(\u03c9)*\x1b[94m\x1b[1mn_y\x1b[0;0m\x1b[0;0m + ' u'\u03b1\u22c5\u03b2*\x1b[94m\x1b[1mn_z\x1b[0;0m\x1b[0;0m') assert expected == pp.doprint(w) assert expected == w._pretty().render()
def render(self, *args, **kwargs): self = e ar = self.args # just to shorten things mpp = VectorPrettyPrinter() if len(ar) == 0: return unicode(0) ol = [] # output list, to be concatenated to a string for i, v in enumerate(ar): # if the coef of the dyadic is 1, we skip the 1 if ar[i][0] == 1: ol.append(u(" + ") + mpp.doprint(ar[i][1]) + u("\u2a02 ") + mpp.doprint(ar[i][2])) # if the coef of the dyadic is -1, we skip the 1 elif ar[i][0] == -1: ol.append(u(" - ") + mpp.doprint(ar[i][1]) + u("\u2a02 ") + mpp.doprint(ar[i][2])) # If the coefficient of the dyadic is not 1 or -1, # we might wrap it in parentheses, for readability. elif ar[i][0] != 0: arg_str = mpp.doprint(ar[i][0]) if isinstance(ar[i][0], Add): arg_str = u("(%s)") % arg_str if arg_str.startswith(u("-")): arg_str = arg_str[1:] str_start = u(" - ") else: str_start = u(" + ") ol.append(str_start + arg_str + u(" ") + mpp.doprint(ar[i][1]) + u("\u2a02 ") + mpp.doprint(ar[i][2])) outstr = u("").join(ol) if outstr.startswith(u(" + ")): outstr = outstr[3:] elif outstr.startswith(" "): outstr = outstr[1:] return outstr