Matrix of Cofactors¶
- matrix_of_cofactors(matrix)¶
Create the matrix of cofactors corresponding to a given matrix
- Parameters
matrix (list of lists of int or float) – List of lists of numbers representing a matrix
- Raises
TypeError – Argument must be a 2-dimensional list
TypeError – Elements nested within argument must be integers or floats
- Returns
matrix – List of lists in which each inner element alternates being positive or negative versions of the corresponding element from the original matrix
- Return type
list of lists of int or float
Notes
Original matrix: \(\mathbf{A} = \begin{bmatrix} a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\ a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\ \cdots & \cdots & \cdots & \cdots \\ a_{m,1} & a_{m,2} & \cdots & a_{m,n} \end{bmatrix}\)
Matrix of cofactors (if \(\mathbf{A}\) contains an odd number of rows and columns): \(\mathbf{A}^C = \begin{bmatrix} a_{1,1} & -1\cdot{a_{1,2}} & \cdots & a_{1,n} \\ -1\cdot{a_{2,1}} & a_{2,2} & \cdots & -1\cdot{a_{2,n}} \\ \cdots & \cdots & \cdots & \cdots \\ a_{m,1} & -1\cdot{a_{m,2}} & \cdots & a_{m,n} \end{bmatrix}\)
Matrix of cofactors (if \(\mathbf{A}\) contains an even number of rows and columns): \(\mathbf{A}^C = \begin{bmatrix} a_{1,1} & -1\cdot{a_{1,2}} & \cdots & -1\cdot{a_{1,n}} \\ -1\cdot{a_{2,1}} & a_{2,2} & \cdots & a_{2,n} \\ \cdots & \cdots & \cdots & \cdots \\ -1\cdot{a_{m,1}} & a_{m,2} & \cdots & a_{m,n} \end{bmatrix}\)
Examples
- Import matrix_of_cofactors function from regressions library
>>> from regressions.matrices.cofactors import matrix_of_cofactors
- Create the matrix of cofactors for [[1, 2, 3], [4, 5, 6]]
>>> matrix_3x2 = matrix_of_cofactors([[1, 2, 3], [4, 5, 6]]) >>> print(matrix_3x2) [[1, -2, 3], [-4, 5, -6]]
- Create the matrix of cofactors for [[2, 3], [5, 7]]
>>> matrix_2x2 = matrix_of_cofactors([[2, 3], [5, 7]]) >>> print(matrix_2x2) [[2, -3], [-5, 7]]