Skip to content

Instantly share code, notes, and snippets.

@kyoro1
Created November 2, 2015 09:36
Show Gist options
  • Save kyoro1/0cb870173e3b0b973798 to your computer and use it in GitHub Desktop.
Save kyoro1/0cb870173e3b0b973798 to your computer and use it in GitHub Desktop.
Pythonで逆行列を求める&検算 ref: http://qiita.com/kyoro1/items/9ff500db0d519bd2a6f5
A=\left(
\begin{matrix}
1 & 2 \\
3 & 4
\end{matrix}
\right)
> import numpy as np
> A = np.array([[1,2],[3,4]])
> np.dot(A,inv_A)
array([[ 1.00000000e+00, 1.11022302e-16],
[ 0.00000000e+00, 1.00000000e+00]])
> np.dot(inv_A,A)
array([[ 1.00000000e+00, 4.44089210e-16],
[ 0.00000000e+00, 1.00000000e+00]])
> A
array([[1, 2],
[3, 4]])
{\rm det}(A)={\rm det}\left(
\begin{matrix}
1 & 2 \\
3 & 4
\end{matrix}
\right)
=1\times 4-2\times3=-2
> np.linalg.det(A)
-2.0000000000000004
A = \left(
\begin{matrix}
a & b \\
c & d
\end{matrix}\right)
A^{-1} = \frac{1}{{\rm det}A}\left(
\begin{matrix}
d & -b \\
-c & a
\end{matrix}\right)
A^{-1} = \frac{1}{-2}\left(
\begin{matrix}
4 & -2 \\
-3 & 1
\end{matrix}\right)
=\left(
\begin{matrix}
-2 & 1 \\
1.5 & -0.5
\end{matrix}
\right)
> inv_A = np.linalg.inv(A)
> inv_A
array([[-2. , 1. ],
[ 1.5, -0.5]])
AA^{-1}=A^{-1}A=
\left(
\begin{matrix}
1 & 0 \\
0 & 1
\end{matrix}
\right)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment