Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
tap1012
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
3
Merge Requests
3
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Mario Chirinos Colunga
tap1012
Commits
e25a9c74
Commit
e25a9c74
authored
Mar 09, 2019
by
Aldo Eduardo Niebla Cruz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ejercicios realizados
parent
59da3761
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
139 additions
and
38 deletions
+139
-38
05-NumPy&Pandas.ipynb
05-NumPy&Pandas.ipynb
+139
-38
No files found.
05-NumPy&Pandas.ipynb
View file @
e25a9c74
...
...
@@ -910,7 +910,7 @@
},
{
"cell_type": "code",
"execution_count":
null
,
"execution_count":
1
,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -943,31 +943,23 @@
" [-0. , -0.23076923, -0.15384615]])\n",
" ''' \n",
" A = np.array(A, dtype=np.float64)\n",
" if A.ndim != 2 and A.shape[0] != A.shape[1]:\n",
" print('La matriz no es cuadrada, por lo tanto NO INVERTIBLE estrictamente')\n",
" else:\n",
" n = len(A) #Tamaño de la matriz\n",
" Inver = np.eye(n, dtype=np.float64) #Inicializo la matriz Inversa\n",
" if A.ndim == 2 and A.shape[0] == A.shape[1]: #Valida si la matriz es cuadrada\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",
" \n",
" #Eliminación de Gauss-Jordan hacia abajo\n",
" piv = 0\n",
" while piv < n-1:\n",
" intercambiaRenglon(A, Inver, piv) #Intercambia renglon usando pivoteo parcial\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",
" for pivote in range(n):\n",
" intercambiaRenglon(A, Inver, pivote) #Intercambia renglon usando pivoteo parcial\n",
" eliminaColumna(A, Inver, pivote) #Elimina la columna por debajo del pivote\n",
" \n",
" #Eliminación de Gauss-Jordan hacia arriba\n",
" if verbosity: print('Ahora hacia arriba: ')\n",
" for piv in reversed(range(1,n)):\n",
" eliminaColumna(A, Inver, piv, reverse=True)\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",
" if verbosity: print('Ahora elimino hacia arriba: ')\n",
" for pivote in reversed(range(n)):\n",
" eliminaColumna(A, Inver, pivote, reverse=True) #Elimina la columna por arriba del pivote\n",
" return Inver\n",
" else:\n",
" print('La matriz no es cuadrada, por lo tanto NO INVERTIBLE estrictamente')\n",
"\n",
"def eliminaColumna(A, Inver, p, reverse=False):\n",
" direction = range(p+1,len(A))\n",
...
...
@@ -976,18 +968,18 @@
" A[p], Inver[p] = A[p] / A[p,p], Inver[p] / A[p,p]\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",
" 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",
" \n",
" \n",
"def intercambiaRenglon(A, Inver, p):\n",
" filas = dict( enumerate(A[p+1:,p]) )\n",
" row = max(filas.items(), key=lambda x: abs(x[1])) # Encuentra la fila con valor abs maximo\n",
" if A[p,p] == 0 and all(value == 0 for value in filas.values()):\n",
" filas = dict( enumerate(A[p:,p]) )\n",
" if all(value == 0 for value in filas.values()):\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",
" 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",
" if verbosity: print('Intercambio renglon',p+1 ,'por renglon', row[0]+p+
2,'
\\n', A, '\\n')\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+
1,':
\\n', A, '\\n')\n",
" return A, Inver, p\n",
"\n",
"#import doctest\n",
...
...
@@ -996,9 +988,128 @@
},
{
"cell_type": "code",
"execution_count":
null
,
"execution_count":
2
,
"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": [
"import numpy as np\n",
"H = np.array([[0, 2, -3, 4, 5],\n",
...
...
@@ -1007,16 +1118,6 @@
" [2, 0, -1, 1, 1],\n",
" [0, 0, 0, 1, 8]])\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('\\nnp.linalg.inv=\\n', np.linalg.inv(H))"
]
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment