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 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 render(self, *args, **kwargs): self = e ar = self.args # just to shorten things if len(ar) == 0: return unicode(0) ol = [] # output list, to be concatenated to a string for i, v in enumerate(ar): for j in 0, 1, 2: # if the coef of the basis vector is 1, we skip the 1 if ar[i][0][j] == 1: ol.append(u(" + ") + ar[i][1].pretty_vecs[j]) # if the coef of the basis vector is -1, we skip the 1 elif ar[i][0][j] == -1: ol.append(u(" - ") + ar[i][1].pretty_vecs[j]) elif ar[i][0][j] != 0: # If the basis vector coeff is not 1 or -1, # we might wrap it in parentheses, for readability. arg_str = (VectorPrettyPrinter().doprint( ar[i][0][j])) if isinstance(ar[i][0][j], Add): arg_str = u("(%s)") % arg_str if arg_str[0] == u("-"): arg_str = arg_str[1:] str_start = u(" - ") else: str_start = u(" + ") ol.append(str_start + arg_str + '*' + ar[i][1].pretty_vecs[j]) outstr = u("").join(ol) if outstr.startswith(u(" + ")): outstr = outstr[3:] elif outstr.startswith(" "): outstr = outstr[1:] return outstr
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. # TODO : The pretty print division does not print correctly here: # w = alpha * N.x + sin(omega) * N.y + alpha / beta * N.z pp = VectorPrettyPrinter() expected = u(' 2\na n_x + b n_y + c\u22c5sin(\u03b1) n_z') assert expected == pp.doprint(v) assert expected == v._pretty().render() expected = u('\u03b1 n_x + sin(\u03c9) n_y + \u03b1\u22c5\u03b2 n_z') assert expected == pp.doprint(w) assert expected == w._pretty().render()
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 subscript with unicode. # TODO : The pretty print division does not print correctly here: # w = alpha * N.x + sin(omega) * N.y + alpha / beta * N.z pp = VectorPrettyPrinter() expected = u("""\ 2 a n_x + b n_y + c⋅sin(α) n_z\ """) assert expected == pp.doprint(v) assert expected == v._pretty().render() expected = u('α n_x + sin(ω) n_y + α⋅β n_z') assert expected == pp.doprint(w) assert expected == w._pretty().render()
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 subscript with unicode. # TODO : The pretty print division does not print correctly here: # w = alpha * N.x + sin(omega) * N.y + alpha / beta * N.z pp = VectorPrettyPrinter() expected = u("""\ 2 a n_x + b n_y + c⋅sin(α) n_z\ """) assert expected == pp.doprint(v) assert expected == v._pretty().render() expected = u('α n_x + sin(ω) n_y + α⋅β n_z') assert expected == pp.doprint(w) assert expected == w._pretty().render()
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. # TODO : The pretty print division does not print correctly here: # w = alpha * N.x + sin(omega) * N.y + alpha / beta * N.z pp = VectorPrettyPrinter() expected = (u' 2\na \x1b[94m\x1b[1mn_x\x1b[0;0m\x1b[0;0m + b \x1b[94m' u'\x1b[1mn_y\x1b[0;0m\x1b[0;0m + c\u22c5sin(\u03b1) \x1b[9' u'4m\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 + sin(\u03c9' u') \x1b[94m\x1b[1mn_y\x1b[0;0m\x1b[0;0m + \u03b1\u22c5' u'\u03b2 \x1b[94m\x1b[1mn_z\x1b[0;0m\x1b[0;0m') assert expected == pp.doprint(w) assert expected == w._pretty().render()
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. # TODO : The pretty print division does not print correctly here: # w = alpha * N.x + sin(omega) * N.y + alpha / beta * N.z pp = VectorPrettyPrinter() expected = u(' 2\na \x1b[94m\x1b[1mn_x\x1b[0;0m\x1b[0;0m + b \x1b[94m' '\x1b[1mn_y\x1b[0;0m\x1b[0;0m + c\u22c5sin(\u03b1) \x1b[9' '4m\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 + sin(\u03c9' ') \x1b[94m\x1b[1mn_y\x1b[0;0m\x1b[0;0m + \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): ar = e.args # just to shorten things if len(ar) == 0: return unicode(0) settings = printer._settings if printer else {} vp = printer if printer else VectorPrettyPrinter(settings) pforms = [] # output list, to be concatenated to a string for i, v in enumerate(ar): for j in 0, 1, 2: # if the coef of the basis vector is 1, we skip the 1 if ar[i][0][j] == 1: pform = vp._print(ar[i][1].pretty_vecs[j]) # if the coef of the basis vector is -1, we skip the 1 elif ar[i][0][j] == -1: pform = vp._print(ar[i][1].pretty_vecs[j]) pform = prettyForm(*pform.left(" - ")) bin = prettyForm.NEG pform = prettyForm(binding=bin, *pform) elif ar[i][0][j] != 0: # If the basis vector coeff is not 1 or -1, # we might wrap it in parentheses, for readability. pform = vp._print(ar[i][0][j]) if isinstance(ar[i][0][j], Add): tmp = pform.parens() pform = prettyForm(tmp[0], tmp[1]) pform = prettyForm( *pform.right(" ", ar[i][1].pretty_vecs[j])) else: continue pforms.append(pform) pform = prettyForm.__add__(*pforms) kwargs["wrap_line"] = kwargs.get("wrap_line") kwargs["num_columns"] = kwargs.get("num_columns") out_str = pform.render(*args, **kwargs) mlines = [line.rstrip() for line in out_str.split("\n")] return "\n".join(mlines)
def render(self, *args, **kwargs): ar = e.args # just to shorten things if len(ar) == 0: return unicode(0) settings = printer._settings if printer else {} vp = printer if printer else VectorPrettyPrinter(settings) ol = [] # output list, to be concatenated to a string for i, v in enumerate(ar): for j in 0, 1, 2: # if the coef of the basis vector is 1, we skip the 1 if ar[i][0][j] == 1: ol.append(u" + " + ar[i][1].pretty_vecs[j]) # if the coef of the basis vector is -1, we skip the 1 elif ar[i][0][j] == -1: ol.append(u" - " + ar[i][1].pretty_vecs[j]) elif ar[i][0][j] != 0: # If the basis vector coeff is not 1 or -1, # we might wrap it in parentheses, for readability. if isinstance(ar[i][0][j], Add): arg_str = vp._print(ar[i][0][j]).parens()[0] else: arg_str = (vp.doprint(ar[i][0][j])) if arg_str[0] == u"-": arg_str = arg_str[1:] str_start = u" - " else: str_start = u" + " ol.append(str_start + arg_str + ' ' + ar[i][1].pretty_vecs[j]) outstr = u"".join(ol) if outstr.startswith(u" + "): outstr = outstr[3:] elif outstr.startswith(" "): outstr = outstr[1:] return outstr