Commit 609b6346 authored by geobumac's avatar geobumac
parents 834b7496 156dce24
...@@ -817,10 +817,124 @@ ...@@ -817,10 +817,124 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 2,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
"source": [] {
"name": "stdout",
"output_type": "stream",
"text": [
"Inversa(C)=\n",
" [[-2. -2.07692308 -0.38461538]\n",
" [ 1. 1.07692308 0.38461538]\n",
" [-0. -0.23076923 -0.15384615]]\n",
"\n",
"np.linalg.inv(C)=\n",
" [[-2. -2.07692308 -0.38461538]\n",
" [ 1. 1.07692308 0.38461538]\n",
" [-0. -0.23076923 -0.15384615]]\n",
"Trying:\n",
" A = np.array([[2, 4, 3], [0, 1, -1], [3, 5, 7]])\n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" Inversa(A)\n",
"Expecting:\n",
" array([[ 4. , -4.33333333, -2.33333333],\n",
" [-1. , 1.66666667, 0.66666667],\n",
" [-1. , 0.66666667, 0.66666667]])\n",
"ok\n",
"Trying:\n",
" C = np.array([[1, 3, 5], [-2, -4, -5], [3, 6, 1]])\n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" Inversa(C)\n",
"Expecting:\n",
" array([[-2. , -2.07692308, -0.38461538],\n",
" [ 1. , 1.07692308, 0.38461538],\n",
" [-0. , -0.23076923, -0.15384615]])\n",
"ok\n",
"1 items had no tests:\n",
" __main__\n",
"1 items passed all tests:\n",
" 4 tests in __main__.Inversa\n",
"4 tests in 2 items.\n",
"4 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=4)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"import numpy as np\n",
"import sys\n",
"\n",
"def Inversa(A):\n",
" '''Función que calcula la inversa (si existe) de una matriz de n x n mediante el metodo de eliminación\n",
" de Gauss-Jordan.\n",
" \n",
" Args: \n",
" A (np.array): Lista de Arreglos que representa la matriz con valores tipo float.\n",
" Se dice que la matriz inversa A-1 tiene inversa, si A-1*A = I, donde I es la matriz identidad \n",
" \n",
" Returns:\n",
" np.array: La matriz inversa de A\n",
" \n",
" Ejemplos:\n",
" >>> A = np.array([[2, 4, 3], [0, 1, -1], [3, 5, 7]])\n",
" >>> Inversa(A)\n",
" array([[ 4. , -4.33333333, -2.33333333],\n",
" [-1. , 1.66666667, 0.66666667],\n",
" [-1. , 0.66666667, 0.66666667]])\n",
" \n",
" >>> C = np.array([[1, 3, 5], [-2, -4, -5], [3, 6, 1]])\n",
" >>> Inversa(C)\n",
" array([[-2. , -2.07692308, -0.38461538],\n",
" [ 1. , 1.07692308, 0.38461538],\n",
" [-0. , -0.23076923, -0.15384615]])\n",
" '''\n",
" if len(A) != len(A[0]):\n",
" print('La matriz no es cuadrada, por lo tanto NO INVERTIBLE estrictamente')\n",
" else:\n",
" A = np.array(A, dtype=np.float64)\n",
" n = len(A)\n",
" Inver = np.eye(n, dtype=np.float64)\n",
" #Método L por eliminación de Gauss, p es el pivote\n",
" for p in range(n-1):\n",
" if A[p,p] == 0: #Verifica que el pivote no sea cero\n",
" sys.exit('La matriz es singular, por lo tanto NO INVERTIBLE')\n",
" A[p,:], Inver[p,:] = A[p,:] / A[p,p], Inver[p,:] / A[p,p]\n",
" for k in range(p+1,n):\n",
" A[k,:], Inver[k,:] = A[p,:] * A[k,p] - A[k,:], Inver[p,:] * A[k,p] - Inver[k,:]\n",
" #Método U por eliminación de Gauss\n",
" for p in reversed(range(1,n)):\n",
" if A[p,p] == 0: #Verifica que el pivote no sea cero\n",
" sys.exit('La matriz es singular, por lo tanto NO INVERTIBLE')\n",
" A[p,:], Inver[p,:] = A[p,:] / A[p,p], Inver[p,:] / A[p,p]\n",
" for k in reversed(range(p)):\n",
" A[k,:], Inver[k,:] = A[p,:] * A[k,p] - A[k,:], Inver[p,:] * A[k,p] - Inver[k,:]\n",
" return Inver\n",
"\n",
"C = np.array([[1, 3, 5],\n",
" [-2, -4, -5],\n",
" [3, 6, 1]], dtype=float)\n",
"\n",
"print('Inversa(C)=\\n',Inversa(C))\n",
"print('\\nnp.linalg.inv(C)=\\n', np.linalg.inv(C))\n",
"import doctest\n",
"doctest.testmod(verbose=True)\n"
]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -1994,7 +2108,7 @@ ...@@ -1994,7 +2108,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.5.3" "version": "3.7.1"
} }
}, },
"nbformat": 4, "nbformat": 4,
......
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment