Ejercicios realizados

parent 59da3761
...@@ -910,7 +910,7 @@ ...@@ -910,7 +910,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 1,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
...@@ -943,31 +943,23 @@ ...@@ -943,31 +943,23 @@
" [-0. , -0.23076923, -0.15384615]])\n", " [-0. , -0.23076923, -0.15384615]])\n",
" ''' \n", " ''' \n",
" A = np.array(A, dtype=np.float64)\n", " A = np.array(A, dtype=np.float64)\n",
" if A.ndim != 2 and A.shape[0] != A.shape[1]:\n", " if A.ndim == 2 and A.shape[0] == A.shape[1]: #Valida si la matriz es cuadrada\n",
" print('La matriz no es cuadrada, por lo tanto NO INVERTIBLE estrictamente')\n", " n = len(A) #Tamaño de la matriz\n",
" else:\n", " Inver = np.eye(n, dtype=np.float64) #Inicializo la matriz Inversa\n",
" n = len(A) #Tamaño de la matriz\n",
" Inver = np.eye(n, dtype=np.float64) #Inicializo la matriz Inversa\n",
" if verbosity: print('Matriz = \\n', A, '\\n')\n", " if verbosity: print('Matriz = \\n', A, '\\n')\n",
" \n", " \n",
" #Eliminación de Gauss-Jordan hacia abajo\n", " #Eliminación de Gauss-Jordan hacia abajo\n",
" piv = 0\n", " for pivote in range(n):\n",
" while piv < n-1:\n", " intercambiaRenglon(A, Inver, pivote) #Intercambia renglon usando pivoteo parcial\n",
" intercambiaRenglon(A, Inver, piv) #Intercambia renglon usando pivoteo parcial\n", " eliminaColumna(A, Inver, pivote) #Elimina la columna por debajo del pivote\n",
" eliminaColumna(A, Inver, piv)\n",
" piv+=1\n",
" if A[piv,piv] == 0: #Verifica que el ultimo pivote no sea cero\n",
" sys.exit('La matriz es singular, por lo tanto NO INVERTIBLE')\n",
" A[piv], Inver[piv] = A[piv] / A[piv,piv], Inver[piv] / A[piv,piv]\n",
" \n", " \n",
" #Eliminación de Gauss-Jordan hacia arriba\n", " #Eliminación de Gauss-Jordan hacia arriba\n",
" if verbosity: print('Ahora hacia arriba: ')\n", " if verbosity: print('Ahora elimino hacia arriba: ')\n",
" for piv in reversed(range(1,n)):\n", " for pivote in reversed(range(n)):\n",
" eliminaColumna(A, Inver, piv, reverse=True)\n", " eliminaColumna(A, Inver, pivote, reverse=True) #Elimina la columna por arriba del pivote\n",
" A[piv-1], Inver[piv-1] = A[piv-1] / A[piv-1,piv-1], Inver[piv-1] / A[piv-1,piv-1]\n",
" if verbosity: print('Normalizo ultimo Pivote=',piv, '\\n', A, '\\n', '\\nInversa=\\n', Inver)\n",
" \n",
" return Inver\n", " return Inver\n",
" else:\n",
" print('La matriz no es cuadrada, por lo tanto NO INVERTIBLE estrictamente')\n",
"\n", "\n",
"def eliminaColumna(A, Inver, p, reverse=False):\n", "def eliminaColumna(A, Inver, p, reverse=False):\n",
" direction = range(p+1,len(A))\n", " direction = range(p+1,len(A))\n",
...@@ -976,18 +968,18 @@ ...@@ -976,18 +968,18 @@
" A[p], Inver[p] = A[p] / A[p,p], Inver[p] / A[p,p]\n", " A[p], Inver[p] = A[p] / A[p,p], Inver[p] / A[p,p]\n",
" for k in direction:\n", " for k in direction:\n",
" A[k], Inver[k] = A[p] * A[k,p] - A[k], Inver[p] * A[k,p] - Inver[k]\n", " A[k], Inver[k] = A[p] * A[k,p] - A[k], Inver[p] * A[k,p] - Inver[k]\n",
" if verbosity: print('Elimino Columna=',p+1, '\\n', A, '\\n')\n", " if verbosity: print('Elimino Columna',p+1, ':\\n', A, '\\n')\n",
" return A, Inver, p\n", " return A, Inver, p\n",
" \n", " \n",
" \n", " \n",
"def intercambiaRenglon(A, Inver, p):\n", "def intercambiaRenglon(A, Inver, p):\n",
" filas = dict( enumerate(A[p+1:,p]) )\n", " filas = dict( enumerate(A[p:,p]) )\n",
" row = max(filas.items(), key=lambda x: abs(x[1])) # Encuentra la fila con valor abs maximo\n", " if all(value == 0 for value in filas.values()):\n",
" if A[p,p] == 0 and all(value == 0 for value in filas.values()):\n",
" sys.exit('La matriz es singular, por lo tanto NO INVERTIBLE')\n", " sys.exit('La matriz es singular, por lo tanto NO INVERTIBLE')\n",
" row = max(filas.items(), key=lambda x: abs(x[1])) #Encuentra la fila con mayor valor abs\n",
" if abs(A[p,p]) < abs(row[1]):\n", " if abs(A[p,p]) < abs(row[1]):\n",
" A[[p, row[0]+p+1]], Inver[[p, row[0]+p+1]] = A[[row[0]+p+1, p]], Inver[[row[0]+p+1, p]]\n", " A[[p, row[0]+p]], Inver[[p, row[0]+p]] = A[[row[0]+p, p]], Inver[[row[0]+p, p]]\n",
" if verbosity: print('Intercambio renglon',p+1 ,'por renglon', row[0]+p+2,'\\n', A, '\\n')\n", " if verbosity: print('Intercambio renglon',p+1 ,'por renglon', row[0]+p+1,':\\n', A, '\\n')\n",
" return A, Inver, p\n", " return A, Inver, p\n",
"\n", "\n",
"#import doctest\n", "#import doctest\n",
...@@ -996,9 +988,128 @@ ...@@ -996,9 +988,128 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 2,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Matriz = \n",
" [[ 0. 2. -3. 4. 5.]\n",
" [ 0. 0. 2. -1. 2.]\n",
" [ 1. 1. 0. 2. -1.]\n",
" [ 2. 0. -1. 1. 1.]\n",
" [ 0. 0. 0. 1. 8.]] \n",
"\n",
"Intercambio renglon 1 por renglon 4 :\n",
" [[ 2. 0. -1. 1. 1.]\n",
" [ 0. 0. 2. -1. 2.]\n",
" [ 1. 1. 0. 2. -1.]\n",
" [ 0. 2. -3. 4. 5.]\n",
" [ 0. 0. 0. 1. 8.]] \n",
"\n",
"Elimino Columna 1 :\n",
" [[ 1. 0. -0.5 0.5 0.5]\n",
" [ 0. 0. -2. 1. -2. ]\n",
" [ 0. -1. -0.5 -1.5 1.5]\n",
" [ 0. -2. 3. -4. -5. ]\n",
" [ 0. 0. -0. -1. -8. ]] \n",
"\n",
"Intercambio renglon 2 por renglon 4 :\n",
" [[ 1. 0. -0.5 0.5 0.5]\n",
" [ 0. -2. 3. -4. -5. ]\n",
" [ 0. -1. -0.5 -1.5 1.5]\n",
" [ 0. 0. -2. 1. -2. ]\n",
" [ 0. 0. -0. -1. -8. ]] \n",
"\n",
"Elimino Columna 2 :\n",
" [[ 1. 0. -0.5 0.5 0.5]\n",
" [-0. 1. -1.5 2. 2.5]\n",
" [ 0. 0. 2. -0.5 -4. ]\n",
" [-0. 0. 2. -1. 2. ]\n",
" [-0. 0. 0. 1. 8. ]] \n",
"\n",
"Elimino Columna 3 :\n",
" [[ 1. 0. -0.5 0.5 0.5 ]\n",
" [-0. 1. -1.5 2. 2.5 ]\n",
" [ 0. 0. 1. -0.25 -2. ]\n",
" [ 0. 0. 0. 0.5 -6. ]\n",
" [ 0. 0. 0. -1. -8. ]] \n",
"\n",
"Intercambio renglon 4 por renglon 5 :\n",
" [[ 1. 0. -0.5 0.5 0.5 ]\n",
" [-0. 1. -1.5 2. 2.5 ]\n",
" [ 0. 0. 1. -0.25 -2. ]\n",
" [ 0. 0. 0. -1. -8. ]\n",
" [ 0. 0. 0. 0.5 -6. ]] \n",
"\n",
"Elimino Columna 4 :\n",
" [[ 1. 0. -0.5 0.5 0.5 ]\n",
" [-0. 1. -1.5 2. 2.5 ]\n",
" [ 0. 0. 1. -0.25 -2. ]\n",
" [-0. -0. -0. 1. 8. ]\n",
" [-0. -0. -0. 0. 10. ]] \n",
"\n",
"Elimino Columna 5 :\n",
" [[ 1. 0. -0.5 0.5 0.5 ]\n",
" [-0. 1. -1.5 2. 2.5 ]\n",
" [ 0. 0. 1. -0.25 -2. ]\n",
" [-0. -0. -0. 1. 8. ]\n",
" [-0. -0. -0. 0. 1. ]] \n",
"\n",
"Ahora elimino hacia arriba: \n",
"Elimino Columna 5 :\n",
" [[-1. -0. 0.5 -0.5 0. ]\n",
" [ 0. -1. 1.5 -2. 0. ]\n",
" [ 0. 0. -1. 0.25 0. ]\n",
" [ 0. 0. 0. -1. 0. ]\n",
" [-0. -0. -0. 0. 1. ]] \n",
"\n",
"Elimino Columna 4 :\n",
" [[ 1. 0. -0.5 0. 0. ]\n",
" [ 0. 1. -1.5 0. 0. ]\n",
" [-0. -0. 1. 0. -0. ]\n",
" [-0. -0. -0. 1. -0. ]\n",
" [-0. -0. -0. 0. 1. ]] \n",
"\n",
"Elimino Columna 3 :\n",
" [[-1. 0. 0. -0. 0.]\n",
" [ 0. -1. 0. -0. 0.]\n",
" [-0. -0. 1. 0. -0.]\n",
" [-0. -0. -0. 1. -0.]\n",
" [-0. -0. -0. 0. 1.]] \n",
"\n",
"Elimino Columna 2 :\n",
" [[ 1. 0. -0. 0. -0.]\n",
" [-0. 1. -0. 0. -0.]\n",
" [-0. -0. 1. 0. -0.]\n",
" [-0. -0. -0. 1. -0.]\n",
" [-0. -0. -0. 0. 1.]] \n",
"\n",
"Elimino Columna 1 :\n",
" [[ 1. 0. -0. 0. -0.]\n",
" [-0. 1. -0. 0. -0.]\n",
" [-0. -0. 1. 0. -0.]\n",
" [-0. -0. -0. 1. -0.]\n",
" [-0. -0. -0. 0. 1.]] \n",
"\n",
"Mi Inversa=\n",
" [[ 0.05 0.35 -0.1 0.55 -0.2 ]\n",
" [ 0.8 1.35 -0.6 0.3 -0.95]\n",
" [-0.25 0. 0.5 -0.25 0.25]\n",
" [-0.4 -0.8 0.8 -0.4 0.6 ]\n",
" [ 0.05 0.1 -0.1 0.05 0.05]]\n",
"\n",
"np.linalg.inv=\n",
" [[ 0.05 0.35 -0.1 0.55 -0.2 ]\n",
" [ 0.8 1.35 -0.6 0.3 -0.95]\n",
" [-0.25 0. 0.5 -0.25 0.25]\n",
" [-0.4 -0.8 0.8 -0.4 0.6 ]\n",
" [ 0.05 0.1 -0.1 0.05 0.05]]\n"
]
}
],
"source": [ "source": [
"import numpy as np\n", "import numpy as np\n",
"H = np.array([[0, 2, -3, 4, 5],\n", "H = np.array([[0, 2, -3, 4, 5],\n",
...@@ -1007,16 +1118,6 @@ ...@@ -1007,16 +1118,6 @@
" [2, 0, -1, 1, 1],\n", " [2, 0, -1, 1, 1],\n",
" [0, 0, 0, 1, 8]])\n", " [0, 0, 0, 1, 8]])\n",
"\n", "\n",
"Leontief = np.array([[1.234, 0.014, 0.007, 0.064, 0.006, 0.017],\n",
" [0.017, 1.436, 0.056, 0.014, 0.019, 0.032],\n",
" [0.078, 0.467, 1.878, 0.036, 0.044, 0.031],\n",
" [0.752, 0.133, 0.101, 1.741, 0.065, 0.123],\n",
" [0.061, 0.045, 0.130, 0.083, 1.578, 0.059],\n",
" [0.340, 0.236, 0.307, 0.315, 0.376, 1.349]])\n",
"\n",
"\n",
"e = np.transpose([[99640, 75548, 14444, 33501, 23527, 263985]])\n",
"\n",
"print('Mi Inversa=\\n', Inversa(H))\n", "print('Mi Inversa=\\n', Inversa(H))\n",
"print('\\nnp.linalg.inv=\\n', np.linalg.inv(H))" "print('\\nnp.linalg.inv=\\n', np.linalg.inv(H))"
] ]
......
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