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
29ea632b
Commit
29ea632b
authored
Mar 12, 2019
by
Alejandro Molina Villegas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
review tarea gaussj
parent
4f069cc7
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
118 additions
and
4 deletions
+118
-4
05-NumPy&Pandas.ipynb
05-NumPy&Pandas.ipynb
+118
-4
No files found.
05-NumPy&Pandas.ipynb
View file @
29ea632b
...
...
@@ -817,10 +817,124 @@
},
{
"cell_type": "code",
"execution_count":
null
,
"execution_count":
2
,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"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",
...
...
@@ -1994,7 +2108,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.
5.3
"
"version": "3.
7.1
"
}
},
"nbformat": 4,
...
...
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