Exemple #1
0
def test_vector_latex():

    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 = ('(a^{2} + \\frac{b}{c})\\mathbf{\\hat{n}_x} + '
                '\\sqrt{d}\\mathbf{\\hat{n}_y} + '
                '\\operatorname{cos}\\left(\\omega\\right)\\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 = ('(a^{2} + \\frac{b}{c})\\hat{i} + '
                '\\sqrt{d}\\hat{j} + '
                '\\operatorname{cos}\\left(\\omega\\right)\\hat{k}')
    assert v._latex() == expected
Exemple #2
0
 def _latex(self, printer=None):
     ar = self.args  # just to shorten things
     if len(ar) == 0:
         return str(0)
     ol = []  # output list, to be concatenated to a string
     mlp = VectorLatexPrinter()
     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(' + ' + mlp.doprint(ar[i][1]) + r"\otimes " +
                     mlp.doprint(ar[i][2]))
         # if the coef of the dyadic is -1, we skip the 1
         elif ar[i][0] == -1:
             ol.append(' - ' +
                       mlp.doprint(ar[i][1]) +
                       r"\otimes " +
                       mlp.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 = mlp.doprint(ar[i][0])
             if isinstance(ar[i][0], Add):
                 arg_str = '(%s)' % arg_str
             if arg_str.startswith('-'):
                 arg_str = arg_str[1:]
                 str_start = ' - '
             else:
                 str_start = ' + '
             ol.append(str_start + arg_str + mlp.doprint(ar[i][1]) +
                       r"\otimes " + mlp.doprint(ar[i][2]))
     outstr = ''.join(ol)
     if outstr.startswith(' + '):
         outstr = outstr[3:]
     elif outstr.startswith(' '):
         outstr = outstr[1:]
     return outstr