adjoint.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from sympy.core import Basic
  2. from sympy.functions import adjoint, conjugate
  3. from sympy.matrices.expressions.matexpr import MatrixExpr
  4. class Adjoint(MatrixExpr):
  5. """
  6. The Hermitian adjoint of a matrix expression.
  7. This is a symbolic object that simply stores its argument without
  8. evaluating it. To actually compute the adjoint, use the ``adjoint()``
  9. function.
  10. Examples
  11. ========
  12. >>> from sympy import MatrixSymbol, Adjoint, adjoint
  13. >>> A = MatrixSymbol('A', 3, 5)
  14. >>> B = MatrixSymbol('B', 5, 3)
  15. >>> Adjoint(A*B)
  16. Adjoint(A*B)
  17. >>> adjoint(A*B)
  18. Adjoint(B)*Adjoint(A)
  19. >>> adjoint(A*B) == Adjoint(A*B)
  20. False
  21. >>> adjoint(A*B) == Adjoint(A*B).doit()
  22. True
  23. """
  24. is_Adjoint = True
  25. def doit(self, **hints):
  26. arg = self.arg
  27. if hints.get('deep', True) and isinstance(arg, Basic):
  28. return adjoint(arg.doit(**hints))
  29. else:
  30. return adjoint(self.arg)
  31. @property
  32. def arg(self):
  33. return self.args[0]
  34. @property
  35. def shape(self):
  36. return self.arg.shape[::-1]
  37. def _entry(self, i, j, **kwargs):
  38. return conjugate(self.arg._entry(j, i, **kwargs))
  39. def _eval_adjoint(self):
  40. return self.arg
  41. def _eval_transpose(self):
  42. return self.arg.conjugate()
  43. def _eval_conjugate(self):
  44. return self.arg.transpose()
  45. def _eval_trace(self):
  46. from sympy.matrices.expressions.trace import Trace
  47. return conjugate(Trace(self.arg))