def test_latex_printer(): r = Function('r')('t') assert VectorLatexPrinter().doprint(r**2) == "r^{2}" r2 = Function('r^2')('t') assert VectorLatexPrinter().doprint(r2.diff()) == r'\dot{r^{2}}' ra = Function('r__a')('t') assert VectorLatexPrinter().doprint(ra.diff().diff()) == r'\ddot{r^{a}}'
def test_latex_printer(): r = Function("r")("t") assert VectorLatexPrinter().doprint(r ** 2) == "r^{2}" r2 = Function("r^2")("t") assert VectorLatexPrinter().doprint(r2.diff()) == r"\dot{r^{2}}" ra = Function("r__a")("t") assert VectorLatexPrinter().doprint(ra.diff().diff()) == r"\ddot{r^{a}}"
def test_vector_latex(): a, b, c, d, omega = symbols('a, b, c, d, omega') v = (a ** 2 + b / c) * A.x + sqrt(d) * A.y + cos(omega) * A.z assert v._latex() == (r'(a^{2} + \frac{b}{c})\mathbf{\hat{a}_x} + ' r'\sqrt{d}\mathbf{\hat{a}_y} + ' r'\operatorname{cos}\left(\omega\right)' r'\mathbf{\hat{a}_z}') theta, omega, alpha, q = dynamicsymbols('theta, omega, alpha, q') v = theta * A.x + omega * omega * A.y + (q * alpha) * A.z assert v._latex() == (r'\theta\mathbf{\hat{a}_x} + ' r'\omega^{2}\mathbf{\hat{a}_y} + ' r'\alpha q\mathbf{\hat{a}_z}') phi1, phi2, phi3 = dynamicsymbols('phi1, phi2, phi3') theta1, theta2, theta3 = symbols('theta1, theta2, theta3') v = (sin(theta1) * A.x + cos(phi1) * cos(phi2) * A.y + cos(theta1 + phi3) * A.z) assert v._latex() == (r'\operatorname{sin}\left(\theta_{1}\right)' r'\mathbf{\hat{a}_x} + \operatorname{cos}' r'\left(\phi_{1}\right) \operatorname{cos}' r'\left(\phi_{2}\right)\mathbf{\hat{a}_y} + ' r'\operatorname{cos}\left(\theta_{1} + ' r'\phi_{3}\right)\mathbf{\hat{a}_z}') N = ReferenceFrame('N') a, b, c, d, omega = symbols('a, b, c, d, omega') v = (a ** 2 + b / c) * N.x + sqrt(d) * N.y + cos(omega) * N.z expected = (r'(a^{2} + \frac{b}{c})\mathbf{\hat{n}_x} + ' r'\sqrt{d}\mathbf{\hat{n}_y} + ' r'\operatorname{cos}\left(\omega\right)' r'\mathbf{\hat{n}_z}') assert v._latex() == expected lp = VectorLatexPrinter() assert lp.doprint(v) == expected # Try custom unit vectors. N = ReferenceFrame('N', latexs=(r'\hat{i}', r'\hat{j}', r'\hat{k}')) v = (a ** 2 + b / c) * N.x + sqrt(d) * N.y + cos(omega) * N.z expected = (r'(a^{2} + \frac{b}{c})\hat{i} + ' r'\sqrt{d}\hat{j} + ' r'\operatorname{cos}\left(\omega\right)\hat{k}') assert v._latex() == expected
def _latex(self, printer=None): """Latex Printing method. """ from sympy.physics.vector.printing import VectorLatexPrinter ar = self.args # just to shorten things if len(ar) == 0: return str(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(" + " + ar[i][1].latex_vecs[j]) # if the coef of the basis vector is -1, we skip the 1 elif ar[i][0][j] == -1: ol.append(" - " + ar[i][1].latex_vecs[j]) elif ar[i][0][j] != 0: # If the coefficient of the basis vector is not 1 or -1; # also, we might wrap it in parentheses, for readability. arg_str = VectorLatexPrinter().doprint(ar[i][0][j]) if isinstance(ar[i][0][j], Add): arg_str = "(%s)" % arg_str if arg_str[0] == "-": arg_str = arg_str[1:] str_start = " - " else: str_start = " + " ol.append(str_start + arg_str + ar[i][1].latex_vecs[j]) outstr = "".join(ol) if outstr.startswith(" + "): outstr = outstr[3:] elif outstr.startswith(" "): outstr = outstr[1:] return outstr
def vlatex(expr, **settings): r"""Function for printing latex representation of sympy.physics.vector objects. For latex representation of Vectors, Dyadics, and dynamicsymbols. Takes the same options as SymPy's latex(); see that function for more information; Parameters ========== expr : valid sympy object SymPy expression to represent in LaTeX form settings : args Same as latex() Examples ======== >>> from sympy.physics.vector import vlatex, ReferenceFrame, dynamicsymbols >>> N = ReferenceFrame('N') >>> q1, q2 = dynamicsymbols('q1 q2') >>> q1d, q2d = dynamicsymbols('q1 q2', 1) >>> q1dd, q2dd = dynamicsymbols('q1 q2', 2) >>> vlatex(N.x + N.y) '\\mathbf{\\hat{n}_x} + \\mathbf{\\hat{n}_y}' >>> vlatex(q1 + q2) 'q_{1} + q_{2}' >>> vlatex(q1d) '\\dot{q}_{1}' >>> vlatex(q1 * q2d) 'q_{1} \\dot{q}_{2}' >>> vlatex(q1dd * q1 / q1d) '\\frac{q_{1} \\ddot{q}_{1}}{\\dot{q}_{1}}' """ return VectorLatexPrinter(settings).doprint(expr)
def test_latex_printer(): r = Function('r')('t') assert VectorLatexPrinter().doprint(r ** 2) == "r^{2}"
def test_vector_latex(): a, b, c, d, omega = symbols("a, b, c, d, omega") v = (a ** 2 + b / c) * A.x + sqrt(d) * A.y + cos(omega) * A.z assert v._latex() == ( r"(a^{2} + \frac{b}{c})\mathbf{\hat{a}_x} + " r"\sqrt{d}\mathbf{\hat{a}_y} + " r"\operatorname{cos}\left(\omega\right)" r"\mathbf{\hat{a}_z}" ) theta, omega, alpha, q = dynamicsymbols("theta, omega, alpha, q") v = theta * A.x + omega * omega * A.y + (q * alpha) * A.z assert v._latex() == ( r"\theta\mathbf{\hat{a}_x} + " r"\omega^{2}\mathbf{\hat{a}_y} + " r"\alpha q\mathbf{\hat{a}_z}" ) phi1, phi2, phi3 = dynamicsymbols("phi1, phi2, phi3") theta1, theta2, theta3 = symbols("theta1, theta2, theta3") v = sin(theta1) * A.x + cos(phi1) * cos(phi2) * A.y + cos(theta1 + phi3) * A.z assert v._latex() == ( r"\operatorname{sin}\left(\theta_{1}\right)" r"\mathbf{\hat{a}_x} + \operatorname{cos}" r"\left(\phi_{1}\right) \operatorname{cos}" r"\left(\phi_{2}\right)\mathbf{\hat{a}_y} + " r"\operatorname{cos}\left(\theta_{1} + " r"\phi_{3}\right)\mathbf{\hat{a}_z}" ) N = ReferenceFrame("N") a, b, c, d, omega = symbols("a, b, c, d, omega") v = (a ** 2 + b / c) * N.x + sqrt(d) * N.y + cos(omega) * N.z expected = ( r"(a^{2} + \frac{b}{c})\mathbf{\hat{n}_x} + " r"\sqrt{d}\mathbf{\hat{n}_y} + " r"\operatorname{cos}\left(\omega\right)" r"\mathbf{\hat{n}_z}" ) assert v._latex() == expected lp = VectorLatexPrinter() assert lp.doprint(v) == expected # Try custom unit vectors. N = ReferenceFrame("N", latexs=(r"\hat{i}", r"\hat{j}", r"\hat{k}")) v = (a ** 2 + b / c) * N.x + sqrt(d) * N.y + cos(omega) * N.z expected = ( r"(a^{2} + \frac{b}{c})\hat{i} + " r"\sqrt{d}\hat{j} + " r"\operatorname{cos}\left(\omega\right)\hat{k}" ) assert v._latex() == expected expected = ( r"\alpha\mathbf{\hat{n}_x} + \operatorname{asin}\left(\omega" r"\right)\mathbf{\hat{n}_y} - \beta \dot{\alpha}\mathbf{\hat{n}_z}" ) assert ww._latex() == expected assert lp.doprint(ww) == expected expected = ( r"- \mathbf{\hat{n}_x}\otimes \mathbf{\hat{n}_y} - " r"\mathbf{\hat{n}_x}\otimes \mathbf{\hat{n}_z}" ) assert xx._latex() == expected assert lp.doprint(xx) == expected expected = ( r"\mathbf{\hat{n}_x}\otimes \mathbf{\hat{n}_y} + " r"\mathbf{\hat{n}_x}\otimes \mathbf{\hat{n}_z}" ) assert xx2._latex() == expected assert lp.doprint(xx2) == expected