diff --git a/sympy/matrices/dense.py b/sympy/matrices/dense.py index 3b7e7417c5f4..5595e7d105cf 100644 --- a/sympy/matrices/dense.py +++ b/sympy/matrices/dense.py @@ -244,6 +244,9 @@ def _eval_conjugate(self): lambda i, j: self[i, j].conjugate()) return out + def _eval_adjoint(self): + return self.T.C + def _eval_inverse(self, **kwargs): """Return the matrix inverse using the method indicated (default is Gauss elimination). diff --git a/sympy/matrices/expressions/adjoint.py b/sympy/matrices/expressions/adjoint.py index d77959c8ddf8..171b630f609b 100644 --- a/sympy/matrices/expressions/adjoint.py +++ b/sympy/matrices/expressions/adjoint.py @@ -1,33 +1,35 @@ -from matexpr import MatrixExpr -from sympy import Basic -from sympy.functions.elementary.complexes import conjugate +from sympy.matrices.expressions.matexpr import MatrixExpr +from sympy.core import Basic +from sympy.functions import conjugate, adjoint class Adjoint(MatrixExpr): - """Matrix Adjoint - - Represents the Adjoint of a matrix expression. + """ + The Hermitian adjoint of a matrix expression. Examples ======== - >>> from sympy import MatrixSymbol, Adjoint + >>> from sympy.matrices import MatrixSymbol, Adjoint + >>> from sympy.functions import adjoint >>> A = MatrixSymbol('A', 3, 5) >>> B = MatrixSymbol('B', 5, 3) >>> Adjoint(A*B) + Adjoint(A*B) + >>> adjoint(A*B) Adjoint(B)*Adjoint(A) + >>> adjoint(A*B) == Adjoint(A*B) + False + >>> adjoint(A*B) == Adjoint(A*B).doit() + True """ is_Adjoint = True - def __new__(cls, mat): - try: - return mat._eval_adjoint() - except (AttributeError, NotImplementedError): - pass - try: - return mat.adjoint() - except (AttributeError, NotImplementedError): - pass - return Basic.__new__(cls, mat) + def doit(self, **hints): + arg = self.arg + if hints.get('deep', True) and isinstance(arg, Basic): + return adjoint(arg.doit(**hints)) + else: + return adjoint(self.arg) @property def arg(self): @@ -44,5 +46,5 @@ def _eval_adjoint(self): return self.arg def _eval_trace(self): - from trace import Trace + from sympy.matrices.expressions.trace import Trace return conjugate(Trace(self.arg)) diff --git a/sympy/matrices/expressions/blockmatrix.py b/sympy/matrices/expressions/blockmatrix.py index a1a41fdb3174..049b326344dc 100644 --- a/sympy/matrices/expressions/blockmatrix.py +++ b/sympy/matrices/expressions/blockmatrix.py @@ -1,13 +1,15 @@ -from matexpr import MatrixExpr, ZeroMatrix, Identity -from matmul import MatMul -from matadd import MatAdd -from matpow import MatPow -from transpose import Transpose -from trace import Trace -from inverse import Inverse -from sympy.matrices import Matrix, eye -from sympy import Tuple, Basic, Add +from sympy.core import Tuple, Basic, Add from sympy.rules import typed, canon, debug, do_one, unpack +from sympy.functions import transpose + +from sympy.matrices.expressions.matexpr import MatrixExpr, ZeroMatrix, Identity +from sympy.matrices.expressions.matmul import MatMul +from sympy.matrices.expressions.matadd import MatAdd +from sympy.matrices.expressions.matpow import MatPow +from sympy.matrices.expressions.transpose import Transpose +from sympy.matrices.expressions.trace import Trace +from sympy.matrices.expressions.inverse import Inverse +from sympy.matrices import Matrix, eye class BlockMatrix(MatrixExpr): @@ -96,7 +98,7 @@ def _blockadd(self, other): def _eval_transpose(self): # Flip all the individual matrices - matrices = [Transpose(matrix) for matrix in self.blocks] + matrices = [transpose(matrix) for matrix in self.blocks] # Make a copy M = Matrix(self.blockshape[0], self.blockshape[1], matrices) # Transpose the block structure diff --git a/sympy/matrices/expressions/matadd.py b/sympy/matrices/expressions/matadd.py index 527d31085134..30d5afa1633b 100644 --- a/sympy/matrices/expressions/matadd.py +++ b/sympy/matrices/expressions/matadd.py @@ -1,7 +1,8 @@ -from matexpr import MatrixExpr, ShapeError, ZeroMatrix -from sympy import Add, Basic, sympify +from sympy.core import Add, Basic, sympify +from sympy.functions import transpose, adjoint from sympy.rules import (rm_id, unpack, flatten, sort, condition, debug, exhaust, do_one, glom) +from sympy.matrices.expressions.matexpr import MatrixExpr, ShapeError, ZeroMatrix class MatAdd(MatrixExpr): """A Sum of Matrix Expressions @@ -42,12 +43,10 @@ def _entry(self, i, j): return Add(*[arg._entry(i, j) for arg in self.args]) def _eval_transpose(self): - from transpose import Transpose - return MatAdd(*[Transpose(arg) for arg in self.args]) + return MatAdd(*[transpose(arg) for arg in self.args]) def _eval_adjoint(self): - from adjoint import Adjoint - return MatAdd(*[Adjoint(arg) for arg in self.args]) + return MatAdd(*[adjoint(arg) for arg in self.args]) def _eval_trace(self): from trace import Trace diff --git a/sympy/matrices/expressions/matexpr.py b/sympy/matrices/expressions/matexpr.py index ee94b294309d..74d401100357 100644 --- a/sympy/matrices/expressions/matexpr.py +++ b/sympy/matrices/expressions/matexpr.py @@ -3,6 +3,7 @@ from sympy.core import S, Symbol, sympify, Tuple, Integer, Basic from sympy.core.decorators import call_highest_priority from sympy.core.sympify import SympifyError +from sympy.functions import transpose, conjugate, adjoint from sympy.matrices import ShapeError from sympy.simplify import simplify @@ -127,7 +128,8 @@ def is_square(self): return self.rows == self.cols def _eval_transpose(self): - raise NotImplementedError() + from sympy.matrices.expressions.transpose import Transpose + return Transpose(self) def _eval_inverse(self): raise NotImplementedError() @@ -142,25 +144,21 @@ def _eval_simplify(self, **kwargs): return self.__class__(*[simplify(x, **kwargs) for x in self.args]) def _eval_adjoint(self): - return self.T.conjugate() + from sympy.matrices.expressions.adjoint import Adjoint + return Adjoint(self) def _entry(self, i, j): raise NotImplementedError( "Indexing not implemented for %s" % self.__class__.__name__) def adjoint(self): - raise NotImplementedError( - "adjoint not implemented for %s" % self.__class__.__name__) + return adjoint(self) def conjugate(self): - raise NotImplementedError( - "conjugate not implemented for %s" % self.__class__.__name__) + return conjugate(self) def transpose(self): - try: - return self._eval_transpose() - except (AttributeError, NotImplementedError): - return Basic.__new__(Transpose, self) + return transpose(self) T = property(transpose, None, None, 'Matrix transposition.') @@ -319,6 +317,13 @@ def _entry(self, i, j): def free_symbols(self): return set((self,)) + def doit(self, **hints): + if hints.get('deep', True): + return type(self)(self.name, self.args[1].doit(**hints), + self.args[2].doit(**hints)) + else: + return self + def _eval_simplify(self, **kwargs): return self diff --git a/sympy/matrices/expressions/matmul.py b/sympy/matrices/expressions/matmul.py index 577504776bca..e928045aa1b4 100644 --- a/sympy/matrices/expressions/matmul.py +++ b/sympy/matrices/expressions/matmul.py @@ -1,8 +1,8 @@ -from matexpr import MatrixExpr, ShapeError, Identity, ZeroMatrix -from sympy.core import Mul, Add, Basic -from sympy import sympify +from sympy.core import Mul, Add, Basic, sympify +from sympy.functions import transpose, adjoint from sympy.rules import (rm_id, unpack, condition, debug, flatten, exhaust, do_one, new) +from sympy.matrices.expressions.matexpr import MatrixExpr, ShapeError, Identity, ZeroMatrix class MatMul(MatrixExpr): """A Product of Matrix Expressions @@ -69,12 +69,10 @@ def as_coeff_mmul(self): return coeff, Basic.__new__(MatMul, *matrices) def _eval_transpose(self): - from transpose import Transpose - return MatMul(*[Transpose(arg) for arg in self.args[::-1]]) + return MatMul(*[transpose(arg) for arg in self.args[::-1]]) def _eval_adjoint(self): - from adjoint import Adjoint - return MatMul(*[Adjoint(arg) for arg in self.args[::-1]]) + return MatMul(*[adjoint(arg) for arg in self.args[::-1]]) def _eval_trace(self): factor, mmul = self.as_coeff_mmul() diff --git a/sympy/matrices/expressions/tests/test_adjoint.py b/sympy/matrices/expressions/tests/test_adjoint.py index 36c89c434a9c..b2ab00bb2772 100644 --- a/sympy/matrices/expressions/tests/test_adjoint.py +++ b/sympy/matrices/expressions/tests/test_adjoint.py @@ -1,6 +1,6 @@ from sympy.matrices.expressions import MatrixSymbol, Adjoint, Trace from sympy.matrices import eye, Matrix -from sympy import symbols, S, conjugate +from sympy import symbols, S, conjugate, adjoint n,m,l,k,p = symbols('n m l k p', integer=True) A = MatrixSymbol('A', n, m) @@ -12,15 +12,18 @@ def test_adjoint(): assert Adjoint(A).shape == (m, n) assert Adjoint(A*B).shape == (l, n) - assert Adjoint(Adjoint(A)) == A + assert adjoint(Adjoint(A)) == A + assert isinstance(Adjoint(Adjoint(A)), Adjoint) - assert Adjoint(eye(3)) == eye(3) + assert Adjoint(eye(3)).doit() == eye(3) - assert Adjoint(S(5)) == S(5) + assert Adjoint(S(5)).doit() == S(5) - assert Adjoint(Matrix([[1, 2], [3, 4]])) == Matrix([[1, 3], [2, 4]]) + assert Adjoint(Matrix([[1, 2], [3, 4]])).doit() == Matrix([[1, 3], [2, 4]]) - assert Adjoint(Trace(Sq)) == conjugate(Trace(Sq)) - assert Trace(Adjoint(Sq)) == conjugate(Trace(Sq)) + assert adjoint(Trace(Sq)) == conjugate(Trace(Sq)) + assert Trace(adjoint(Sq)) == conjugate(Trace(Sq)) assert Adjoint(Sq)[0, 1] == conjugate(Sq[1, 0]) + + assert Adjoint(A*B).doit() == Adjoint(B) * Adjoint(A) diff --git a/sympy/matrices/expressions/tests/test_matrix_exprs.py b/sympy/matrices/expressions/tests/test_matrix_exprs.py index 35b390394159..d6e7dee53fb3 100644 --- a/sympy/matrices/expressions/tests/test_matrix_exprs.py +++ b/sympy/matrices/expressions/tests/test_matrix_exprs.py @@ -1,4 +1,6 @@ -from sympy import cos, Lambda, S, simplify, sin, sqrt, symbols, Tuple +from sympy.core import Lambda, S, symbols, Tuple +from sympy.functions import transpose, sin, cos, sqrt +from sympy.simplify import simplify from sympy.matrices import ( BlockDiagMatrix, BlockMatrix, block_collapse, eye, FunctionMatrix, Identity, ImmutableMatrix, Inverse, MatAdd, MatMul, MatPow, Matrix, @@ -20,15 +22,15 @@ def test_transpose(): assert Transpose(A).shape == (m, n) assert Transpose(A*B).shape == (l, n) - assert Transpose(Transpose(A)) == A + assert transpose(Transpose(A)) == A - assert Transpose(eye(3)) == eye(3) + assert transpose(eye(3)) == eye(3) - assert Transpose(S(5)) == S(5) + assert transpose(S(5)) == S(5) - assert Transpose(Matrix([[1, 2], [3, 4]])) == Matrix([[1, 3], [2, 4]]) + assert transpose(Matrix([[1, 2], [3, 4]])) == Matrix([[1, 3], [2, 4]]) - assert Transpose(Trace(Sq)) == Trace(Sq) + assert transpose(Trace(Sq)) == Trace(Sq) def test_inverse(): @@ -119,9 +121,9 @@ def test_BlockMatrix(): assert block_collapse(E.T*A*F) == E.T*A*F assert X.shape == (l + n, k + m) - assert (block_collapse(Transpose(X)) == + assert (block_collapse(transpose(X)) == BlockMatrix(Matrix([[A.T, C.T], [B.T, D.T]]))) - assert Transpose(X).shape == X.shape[::-1] + assert transpose(X).shape == X.shape[::-1] assert X.blockshape == (2, 2) # Test that BlockMatrices and MatrixSymbols can still mix @@ -140,14 +142,14 @@ def test_BlockMatrix(): assert (X*Y).shape == (l + n, 1) assert block_collapse(X*Y).blocks[0, 0] == A*E + B*F assert block_collapse(X*Y).blocks[1, 0] == C*E + D*F - assert (block_collapse(Transpose(block_collapse(Transpose(X*Y)))) == + assert (block_collapse(transpose(block_collapse(transpose(X*Y)))) == block_collapse(X*Y)) # block_collapse passes down into container objects, transposes, and inverse assert block_collapse(Tuple(X*Y, 2*X)) == ( block_collapse(X*Y), block_collapse(2*X)) - assert (block_collapse(Transpose(X*Y)) == - block_collapse(Transpose(block_collapse(X*Y)))) + assert (block_collapse(transpose(X*Y)) == + block_collapse(transpose(block_collapse(X*Y)))) Ab = BlockMatrix([[A]]) Z = MatrixSymbol('Z', *A.shape) @@ -236,7 +238,7 @@ def test_ZeroMatrix(): assert Z*A.T == ZeroMatrix(n, n) assert A - A == ZeroMatrix(*A.shape) - assert Transpose(Z) == ZeroMatrix(m, n) + assert transpose(Z) == ZeroMatrix(m, n) assert Z.conjugate() == Z @@ -248,7 +250,7 @@ def test_Identity(): assert A*Im == A assert In*A == A - assert Transpose(In) == In + assert transpose(In) == In assert Inverse(In) == In assert In.conjugate() == In @@ -314,6 +316,7 @@ def test_MatrixSymbol(): X = MatrixSymbol('X', n, m) assert X.shape == (n, m) raises(TypeError, lambda: MatrixSymbol('X', n, m)(t)) # issue 2756 + assert X.doit() == X def test_dense_conversion(): diff --git a/sympy/matrices/expressions/transpose.py b/sympy/matrices/expressions/transpose.py index 8f5ab4307e3f..c406a0dc695b 100644 --- a/sympy/matrices/expressions/transpose.py +++ b/sympy/matrices/expressions/transpose.py @@ -1,31 +1,41 @@ -from matexpr import MatrixExpr -from sympy import Basic +from sympy.core import Basic +from sympy.functions import transpose +from sympy.matrices.expressions.matexpr import MatrixExpr class Transpose(MatrixExpr): - """Matrix Transpose + """ + The transpose of a matrix expression. - Represents the transpose of a matrix expression. + This is a symbolic object that simply stores its argument without + evaluating it. To actually compute the transpose, use the ``transpose()`` + function, or the ``.T`` attribute of matrices. - Use .T as shorthand + Examples + ======== - >>> from sympy import MatrixSymbol, Transpose + >>> from sympy.matrices import MatrixSymbol, Transpose + >>> from sympy.functions import transpose >>> A = MatrixSymbol('A', 3, 5) >>> B = MatrixSymbol('B', 5, 3) >>> Transpose(A) A' - >>> A.T - A' + >>> A.T == transpose(A) == Transpose(A) + True >>> Transpose(A*B) + (A*B)' + >>> transpose(A*B) B'*A' + """ is_Transpose = True - def __new__(cls, mat): - try: - return mat.transpose() - except (AttributeError, NotImplementedError): - return Basic.__new__(cls, mat) + def doit(self, **hints): + arg = self.arg + if hints.get('deep', True) and isinstance(arg, Basic): + return transpose(arg.doit(**hints)) + else: + return transpose(self.arg) @property def arg(self): diff --git a/sympy/matrices/immutable.py b/sympy/matrices/immutable.py index 9fd86761f502..9273e3cdb8ab 100644 --- a/sympy/matrices/immutable.py +++ b/sympy/matrices/immutable.py @@ -71,6 +71,7 @@ def __setitem__(self, *args): _eval_trace = DenseMatrix._eval_trace _eval_transpose = DenseMatrix._eval_transpose _eval_conjugate = DenseMatrix._eval_conjugate + _eval_adjoint = DenseMatrix._eval_adjoint _eval_inverse = DenseMatrix._eval_inverse _eval_simplify = DenseMatrix._eval_simplify diff --git a/sympy/printing/latex.py b/sympy/printing/latex.py index 720aa3924d79..ae054c2b22f8 100644 --- a/sympy/printing/latex.py +++ b/sympy/printing/latex.py @@ -137,9 +137,9 @@ def _print_Add(self, expr, order=None): for term in terms[1:]: if not _coeff_isneg(term): - tex += " +" - - tex += " " + self._print(term) + tex += " + " + self._print(term) + else: + tex += " - " + self._print(-term) return tex @@ -1015,29 +1015,23 @@ def _print_BlockMatrix(self, expr): def _print_Transpose(self, expr): mat = expr.arg - if mat.is_MatAdd or mat.is_MatMul: + from sympy.matrices import MatrixSymbol + if not isinstance(mat, MatrixSymbol): return r"\left(%s\right)^T" % self._print(mat) else: return "%s^T" % self._print(mat) def _print_Adjoint(self, expr): mat = expr.arg - if mat.is_MatAdd or mat.is_MatMul: + from sympy.matrices import MatrixSymbol + if not isinstance(mat, MatrixSymbol): return r"\left(%s\right)^\dag" % self._print(mat) else: return "%s^\dag" % self._print(mat) def _print_MatAdd(self, expr): - # Stolen from print_Add terms = list(expr.args) - tex = self._print(terms[0]) - - for term in terms[1:]: - if not _coeff_isneg(term): - tex += " +" - - tex += " " + self._print(term) - + tex = " + ".join(map(self._print, terms)) return tex def _print_MatMul(self, expr): @@ -1060,7 +1054,8 @@ def parens(x): def _print_MatPow(self, expr): base, exp = expr.base, expr.exp - if base.is_Add or base.is_Mul: + from sympy.matrices import MatrixSymbol + if not isinstance(base, MatrixSymbol): return r"\left(%s\right)^{%s}" % (self._print(base), self._print(exp)) else: return "%s^{%s}" % (self._print(base), self._print(exp)) diff --git a/sympy/printing/pretty/pretty.py b/sympy/printing/pretty/pretty.py index 9ae356f1601d..55e09ba3d2a8 100644 --- a/sympy/printing/pretty/pretty.py +++ b/sympy/printing/pretty/pretty.py @@ -608,29 +608,24 @@ def _print_MatrixBase(self, e): _print_ImmutableMatrix = _print_MatrixBase _print_Matrix = _print_MatrixBase - def _print_Transpose(self, T): - pform = self._print(T.arg) - if (T.arg.is_MatAdd or T.arg.is_MatMul): + def _print_Transpose(self, expr): + pform = self._print(expr.arg) + from sympy.matrices import MatrixSymbol + if not isinstance(expr.arg, MatrixSymbol): pform = prettyForm(*pform.parens()) - pform = prettyForm(*pform.right("'")) + pform = pform**(prettyForm('T')) return pform - def _print_Adjoint(self, A): - pform = self._print(A.arg) + def _print_Adjoint(self, expr): + pform = self._print(expr.arg) if self._use_unicode: dag = prettyForm(u'\u2020') else: dag = prettyForm('+') - if (A.arg.is_MatAdd or A.arg.is_MatMul): + from sympy.matrices import MatrixSymbol + if not isinstance(expr.arg, MatrixSymbol): pform = prettyForm(*pform.parens()) - pform = pform**prettyForm(u'\u2020') - return pform - - def _print_Inverse(self, I): - pform = self._print(I.arg) - if (I.arg.is_Add or I.arg.is_Mul or I.arg.is_Pow): - pform = prettyForm(*pform.parens()) - pform = prettyForm(*pform.right("^-1")) + pform = pform**dag return pform def _print_BlockMatrix(self, B): @@ -638,6 +633,9 @@ def _print_BlockMatrix(self, B): return self._print(B.blocks[0, 0]) return self._print(B.blocks) + def _print_MatAdd(self, expr): + return self._print_seq(expr.args, None, None, ' + ') + def _print_MatMul(self, expr): args = list(expr.args) from sympy import Add, MatAdd, HadamardProduct @@ -650,8 +648,13 @@ def _print_MatMul(self, expr): return prettyForm.__mul__(*args) - def _print_MatAdd(self, expr): - return self._print_seq(expr.args, None, None, ' + ') + def _print_MatPow(self, expr): + pform = self._print(expr.base) + from sympy.matrices import MatrixSymbol + if not isinstance(expr.base, MatrixSymbol): + pform = prettyForm(*pform.parens()) + pform = pform**(self._print(expr.exp)) + return pform def _print_HadamardProduct(self, expr): from sympy import MatAdd, MatMul diff --git a/sympy/printing/pretty/tests/test_pretty.py b/sympy/printing/pretty/tests/test_pretty.py index 13b4369f34a2..ac16d38fa051 100644 --- a/sympy/printing/pretty/tests/test_pretty.py +++ b/sympy/printing/pretty/tests/test_pretty.py @@ -2164,6 +2164,40 @@ def test_pretty_matrix(): assert upretty(expr) == ucode_str +def test_Adjoint(): + from sympy.matrices import Adjoint, Inverse, MatrixSymbol, Transpose + X = MatrixSymbol('X', 2, 2) + Y = MatrixSymbol('Y', 2, 2) + assert pretty(Adjoint(X)) == " +\nX " + assert pretty(Adjoint(X + Y)) == " +\n(X + Y) " + assert pretty(Adjoint(X) + Adjoint(Y)) == " + +\nX + Y " + assert pretty(Adjoint(X*Y)) == " +\n(X*Y) " + assert pretty(Adjoint(Y)*Adjoint(X)) == " + +\nY *X " + assert pretty(Adjoint(X**2)) == " +\n/ 2\\ \n\\X / " + assert pretty(Adjoint(X)**2) == " 2\n/ +\\ \n\\X / " + assert pretty(Adjoint(Inverse(X))) == " +\n/ -1\\ \n\\X / " + assert pretty(Inverse(Adjoint(X))) == " -1\n/ +\\ \n\\X / " + assert pretty(Adjoint(Transpose(X))) == " +\n/ T\\ \n\\X / " + assert pretty(Transpose(Adjoint(X))) == " T\n/ +\\ \n\\X / " + assert upretty(Adjoint(X)) == u" \u2020\nX " + assert upretty(Adjoint(X + Y)) == u" \u2020\n(X + Y) " + assert upretty(Adjoint(X) + Adjoint(Y)) == u" \u2020 \u2020\nX + Y " + assert upretty(Adjoint(X*Y)) == u" \u2020\n(X\u22c5Y) " + assert upretty(Adjoint(Y)*Adjoint(X)) == u" \u2020 \u2020\nY \u22c5X " + assert upretty(Adjoint(X**2)) == \ + u" \u2020\n\u239b 2\u239e \n\u239dX \u23a0 " + assert upretty(Adjoint(X)**2) == \ + u" 2\n\u239b \u2020\u239e \n\u239dX \u23a0 " + assert upretty(Adjoint(Inverse(X))) == \ + u" \u2020\n\u239b -1\u239e \n\u239dX \u23a0 " + assert upretty(Inverse(Adjoint(X))) == \ + u" -1\n\u239b \u2020\u239e \n\u239dX \u23a0 " + assert upretty(Adjoint(Transpose(X))) == \ + u" \u2020\n\u239b T\u239e \n\u239dX \u23a0 " + assert upretty(Transpose(Adjoint(X))) == \ + u" T\n\u239b \u2020\u239e \n\u239dX \u23a0 " + + def test_pretty_piecewise(): expr = Piecewise((x, x < 1), (x**2, True)) ascii_str = \ diff --git a/sympy/printing/tests/test_latex.py b/sympy/printing/tests/test_latex.py index 825097b13dc0..1e05bdf892ef 100644 --- a/sympy/printing/tests/test_latex.py +++ b/sympy/printing/tests/test_latex.py @@ -487,15 +487,15 @@ def test_latex_Piecewise(): def test_latex_Matrix(): M = Matrix([[1 + x, y], [y, x - 1]]) - assert latex(M) == '\\left[\\begin{smallmatrix}x + 1 & y\\\\y & x -' \ - '1\\end{smallmatrix}\\right]' + assert latex(M) == '\\left[\\begin{smallmatrix}x + 1 & y\\\\y & x - 1' \ + '\\end{smallmatrix}\\right]' settings = {'mat_str': 'bmatrix'} assert latex(M, **settings) == '\\left[\\begin{bmatrix}x + 1 & y\\\\y &' \ - ' x -1\\end{bmatrix}\\right]' + ' x - 1\\end{bmatrix}\\right]' settings['mat_delim'] = None - assert latex(M, **settings) == '\\begin{bmatrix}x + 1 & y\\\\y & x -1' \ + assert latex(M, **settings) == '\\begin{bmatrix}x + 1 & y\\\\y & x - 1' \ '\\end{bmatrix}' - assert latex(M) == '\\left[\\begin{smallmatrix}x + 1 & y\\\\y & x -1' \ + assert latex(M) == '\\left[\\begin{smallmatrix}x + 1 & y\\\\y & x - 1' \ '\\end{smallmatrix}\\right]' @@ -797,11 +797,20 @@ def test_Tr(): def test_Adjoint(): - from sympy.matrices import MatrixSymbol, Adjoint + from sympy.matrices import MatrixSymbol, Adjoint, Inverse, Transpose X = MatrixSymbol('X', 2, 2) Y = MatrixSymbol('Y', 2, 2) - # Either of these would be fine - assert latex(Adjoint(X + Y)) == r'X^\dag + Y^\dag' + assert latex(Adjoint(X)) == r'X^\dag' + assert latex(Adjoint(X + Y)) == r'\left(X + Y\right)^\dag' + assert latex(Adjoint(X) + Adjoint(Y)) == r'X^\dag + Y^\dag' + assert latex(Adjoint(X*Y)) == r'\left(X Y\right)^\dag' + assert latex(Adjoint(Y)*Adjoint(X)) == r'Y^\dag X^\dag' + assert latex(Adjoint(X**2)) == r'\left(X^{2}\right)^\dag' + assert latex(Adjoint(X)**2) == r'\left(X^\dag\right)^{2}' + assert latex(Adjoint(Inverse(X))) == r'\left(X^{-1}\right)^\dag' + assert latex(Inverse(Adjoint(X))) == r'\left(X^\dag\right)^{-1}' + assert latex(Adjoint(Transpose(X))) == r'\left(X^T\right)^\dag' + assert latex(Transpose(Adjoint(X))) == r'\left(X^\dag\right)^T' def test_Hadamard():