Returns the determinant of a square matrix 'M' calculated using recursive minor expansion (Laplace expansion). For large matrices with symbolic entries, this is faster than the built-in sympy.Matrix.det() method. :param M: Sympy matrix :type M: sympy.Matrix :param meth
(M, method="ME")
| 19 | from copy import deepcopy |
| 20 | |
| 21 | def det(M, method="ME"): |
| 22 | """ |
| 23 | Returns the determinant of a square matrix 'M' calculated using recursive |
| 24 | minor expansion (Laplace expansion). |
| 25 | For large matrices with symbolic entries, this is faster than the built-in |
| 26 | sympy.Matrix.det() method. |
| 27 | |
| 28 | :param M: Sympy matrix |
| 29 | :type M: sympy.Matrix |
| 30 | |
| 31 | :param method: Method used: |
| 32 | |
| 33 | - ME: SLiCAP Minor expansion |
| 34 | - BS: SLiCAP Bareis fraction-free expansion |
| 35 | - FC: SLiCAP Frequency Constants method |
| 36 | - LU: Sympy built-in LU method |
| 37 | - bareis: Sympy built-in Bareis method |
| 38 | |
| 39 | :return: Determinant of 'M' |
| 40 | :rtype: sympy.Expr |
| 41 | """ |
| 42 | M = float2rational( |
| 43 | sp.Matrix(M)) # Have a Mutable Matrix with rational numbers |
| 44 | factor = 1 |
| 45 | if M.shape[0] != M.shape[1]: |
| 46 | print("ERROR: Cannot determine determinant of non-square matrix.") |
| 47 | D = None |
| 48 | if (method == "ME" or method == "BS") and ini.reduce_matrix and len(M.atoms(sp.Symbol)) > 0: |
| 49 | M, factor = _eliminateVars(M, method) |
| 50 | dim = M.shape[0] |
| 51 | if M.is_zero_matrix: |
| 52 | D = 0 |
| 53 | elif dim == 1: |
| 54 | D = M[0, 0] * factor |
| 55 | elif dim == 2: |
| 56 | D = sp.expand(M[0, 0]*M[1, 1]-M[1, 0]*M[0, 1]) * factor |
| 57 | elif method == "ME": |
| 58 | D = _detME(M) * factor |
| 59 | elif method == "BS": |
| 60 | D = _detBS(M) * factor |
| 61 | elif method == "LU": |
| 62 | D = M.det(method="LU") |
| 63 | elif method == "bareiss": |
| 64 | D = M.det(method="bareiss") |
| 65 | elif method == "laplace": |
| 66 | D = M.det(method="laplace") |
| 67 | else: |
| 68 | print("ERROR: Unknown method for det(M).") |
| 69 | D = None |
| 70 | return D |
| 71 | |
| 72 | def _eliminateVars(M, method): |
| 73 | """ |
no test coverage detected