Skip to content

Instantly share code, notes, and snippets.

@ialexpovad
Last active June 30, 2021 15:10
Show Gist options
  • Save ialexpovad/f7bd9b7b947db5ed7ba3efe2d2cfe995 to your computer and use it in GitHub Desktop.
Save ialexpovad/f7bd9b7b947db5ed7ba3efe2d2cfe995 to your computer and use it in GitHub Desktop.
Matrix inversion without NumPy/SciPy

Matrix algebra and matrix calculus. Inverse matrices.

Intro

The need for an inverse matrix. Consider a typical algebraic equation:

$$ Ax = b, $$

$$ \begin {bmatrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33} \end{bmatrix} \begin{bmatrix} x_{11} \ x_{21} \ x_{31} \end{bmatrix} = \begin{bmatrix} b_{11} \ b_{21} \ b_{31} \end{bmatrix}. $$

We want to solve for $x$, so obtain the inverse of $A$ and do the following:

$$ x = A^{-1} b, $$

$$ \begin{bmatrix} x_{11} \ x_{21} \ x_{31} \end{bmatrix} = \begin{bmatrix} ai_{11} & ai_{12} & ai_{13} \\ ai_{21} & ai_{22} & ai_{23} \\ ai_{31} & ai_{32} & ai_{33} \end{bmatrix} \begin{bmatrix} b_{11} \ b_{21} \ b_{31} \end{bmatrix}.$$

So, have a motive to find $A^{-1}$.

Definition

A square matrix $A$ is called nonsingular (or non-degenerate ) if it has a (necessarily unique) multiplicatively inverse or simply inverse matrix $A^{-1}$ defined by the conditions $AA^{-1}=A^{-1}A=I$. Otherwise $A$ singular (degenerate).

A square matrix $A\equiv [a_{ij}]$ of order $n$ nonsingular if and only if $\det(A)\equiv \det[a_{ij}] \ne 0$; in this case $A^{-1}$, there is the square matrix of the same order $n$:

$$A^{-1}\equiv [a_{ij}]^{-1} \equiv \left[ \frac{A_{ji}}{\det[a_{ij}]}\right ],$$

where $A_{ji}$ – algebraic addition of an element $[a_{ij}]$ in the determinant $\det[a_{ij}]$. A square matrix is not-degenerate if and only if its rows (columns) are linearly independent.

In common

Given a system of equations, write the coefficient matrix $A$, the variable matrix $x$, and the constant matrix $b$. Then:

$$ Ax = b. $$

Multiply both sides by the inverse of $A$ to obtain the solution:

$$ (A^{-1}) A x = (A^{-1})b, $$

$$ \left[(A^{-1}) A \right] x = (A^{-1})b, $$

$$ Ix = (A^{-1})b, $$

$$ x = (A^{-1})b .$$

The steps $S$ that must be performed to do this for a matrix of any size. It is necessary to think of the inverse matrix method as a set of steps for each column from left to right and for each element in the current column, and in each column there is one of the diagonal elements, which are represented as diagonal elements $S_{k1},$ where $k=1 \to n.$ And each $S$ represents an element that is used for scaling. When we are at a certain step $S_{ij},$ where $i$ and $j=1$ to $n$ regardiess of where we are in the matrix, we perform this step over the entire row amd use the row with the diagonal $S_{k1}$ in it as part of this operation.

$$ S = \begin{bmatrix} S_{11} & ... & ... & S_{k2} & ... & ... & S_{n2} \\ S_{12} & ... & ... & S_{k3} & ... & ... & S_{n3} \\ . & & & . & & & .\\ S_{1k} & ... & ... & S_{k1} & ... & ... & S_{nk} \\ . & & & . & & & .\\ S_{1n-1} & ... & ... & S_{kn-1} & ... & ... & S_{nn-1} \\ S_{1} & ... & ... & S_{kn} & ... & ... & S_{n1} \end{bmatrix}$$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment