Coverage for /usr/lib/python3/dist-packages/sympy/matrices/expressions/determinant.py: 44%
54 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
1from sympy.core.basic import Basic
2from sympy.core.expr import Expr
3from sympy.core.singleton import S
4from sympy.core.sympify import sympify
5from sympy.matrices.common import NonSquareMatrixError
8class Determinant(Expr):
9 """Matrix Determinant
11 Represents the determinant of a matrix expression.
13 Examples
14 ========
16 >>> from sympy import MatrixSymbol, Determinant, eye
17 >>> A = MatrixSymbol('A', 3, 3)
18 >>> Determinant(A)
19 Determinant(A)
20 >>> Determinant(eye(3)).doit()
21 1
22 """
23 is_commutative = True
25 def __new__(cls, mat):
26 mat = sympify(mat)
27 if not mat.is_Matrix:
28 raise TypeError("Input to Determinant, %s, not a matrix" % str(mat))
30 if mat.is_square is False:
31 raise NonSquareMatrixError("Det of a non-square matrix")
33 return Basic.__new__(cls, mat)
35 @property
36 def arg(self):
37 return self.args[0]
39 @property
40 def kind(self):
41 return self.arg.kind.element_kind
43 def doit(self, expand=False, **hints):
44 try:
45 return self.arg._eval_determinant()
46 except (AttributeError, NotImplementedError):
47 return self
49def det(matexpr):
50 """ Matrix Determinant
52 Examples
53 ========
55 >>> from sympy import MatrixSymbol, det, eye
56 >>> A = MatrixSymbol('A', 3, 3)
57 >>> det(A)
58 Determinant(A)
59 >>> det(eye(3))
60 1
61 """
63 return Determinant(matexpr).doit()
65class Permanent(Expr):
66 """Matrix Permanent
68 Represents the permanent of a matrix expression.
70 Examples
71 ========
73 >>> from sympy import MatrixSymbol, Permanent, ones
74 >>> A = MatrixSymbol('A', 3, 3)
75 >>> Permanent(A)
76 Permanent(A)
77 >>> Permanent(ones(3, 3)).doit()
78 6
79 """
81 def __new__(cls, mat):
82 mat = sympify(mat)
83 if not mat.is_Matrix:
84 raise TypeError("Input to Permanent, %s, not a matrix" % str(mat))
86 return Basic.__new__(cls, mat)
88 @property
89 def arg(self):
90 return self.args[0]
92 def doit(self, expand=False, **hints):
93 try:
94 return self.arg.per()
95 except (AttributeError, NotImplementedError):
96 return self
98def per(matexpr):
99 """ Matrix Permanent
101 Examples
102 ========
104 >>> from sympy import MatrixSymbol, Matrix, per, ones
105 >>> A = MatrixSymbol('A', 3, 3)
106 >>> per(A)
107 Permanent(A)
108 >>> per(ones(5, 5))
109 120
110 >>> M = Matrix([1, 2, 5])
111 >>> per(M)
112 8
113 """
115 return Permanent(matexpr).doit()
117from sympy.assumptions.ask import ask, Q
118from sympy.assumptions.refine import handlers_dict
121def refine_Determinant(expr, assumptions):
122 """
123 >>> from sympy import MatrixSymbol, Q, assuming, refine, det
124 >>> X = MatrixSymbol('X', 2, 2)
125 >>> det(X)
126 Determinant(X)
127 >>> with assuming(Q.orthogonal(X)):
128 ... print(refine(det(X)))
129 1
130 """
131 if ask(Q.orthogonal(expr.arg), assumptions):
132 return S.One
133 elif ask(Q.singular(expr.arg), assumptions):
134 return S.Zero
135 elif ask(Q.unit_triangular(expr.arg), assumptions):
136 return S.One
138 return expr
141handlers_dict['Determinant'] = refine_Determinant