diff options
author | Trupti Kini | 2016-09-03 23:30:23 +0600 |
---|---|---|
committer | Trupti Kini | 2016-09-03 23:30:23 +0600 |
commit | 5f15ee61029068ad350502ad04768d7cd84e4736 (patch) | |
tree | b43a8aa92365b74d47758af2ebd4222961586c20 /Linear_Algebra_And_Its_Applications_by_G._Strang/CHAPTER1.ipynb | |
parent | b417454ff6b5f8c2eb90f054723965a8f52e0268 (diff) | |
download | Python-Textbook-Companions-5f15ee61029068ad350502ad04768d7cd84e4736.tar.gz Python-Textbook-Companions-5f15ee61029068ad350502ad04768d7cd84e4736.tar.bz2 Python-Textbook-Companions-5f15ee61029068ad350502ad04768d7cd84e4736.zip |
Added(A)/Deleted(D) following books
A Linear_Algebra_And_Its_Applications_by_G._Strang/CHAPTER1.ipynb
A Linear_Algebra_And_Its_Applications_by_G._Strang/CHAPTER2.ipynb
A Linear_Algebra_And_Its_Applications_by_G._Strang/CHAPTER3.ipynb
A Linear_Algebra_And_Its_Applications_by_G._Strang/CHAPTER4.ipynb
A Linear_Algebra_And_Its_Applications_by_G._Strang/CHAPTER5.ipynb
A Linear_Algebra_And_Its_Applications_by_G._Strang/CHAPTER6.ipynb
A Linear_Algebra_And_Its_Applications_by_G._Strang/CHAPTER7.ipynb
A Linear_Algebra_And_Its_Applications_by_G._Strang/CHAPTER8.ipynb
A Linear_Algebra_And_Its_Applications_by_G._Strang/screenshots/Ch5Eigenmatrix.png
A Linear_Algebra_And_Its_Applications_by_G._Strang/screenshots/Ch5eigenVectors.png
A Linear_Algebra_And_Its_Applications_by_G._Strang/screenshots/ch5eigenvaluematrix.png
Diffstat (limited to 'Linear_Algebra_And_Its_Applications_by_G._Strang/CHAPTER1.ipynb')
-rw-r--r-- | Linear_Algebra_And_Its_Applications_by_G._Strang/CHAPTER1.ipynb | 927 |
1 files changed, 927 insertions, 0 deletions
diff --git a/Linear_Algebra_And_Its_Applications_by_G._Strang/CHAPTER1.ipynb b/Linear_Algebra_And_Its_Applications_by_G._Strang/CHAPTER1.ipynb new file mode 100644 index 00000000..65e5ae8c --- /dev/null +++ b/Linear_Algebra_And_Its_Applications_by_G._Strang/CHAPTER1.ipynb @@ -0,0 +1,927 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 1 - Matrix notation & matrix multiplication" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ex:1.3.1 Pg:20" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x=\n", + "[[u]\n", + " [v]\n", + " [w]]\n", + "R2=R2-R1,R3=R3-4*R1\n", + "[[1 1 1]\n", + " [0 0 3]\n", + " [0 2 4]]\n", + "R2<->R3\n", + "[[1 1 1]\n", + " [0 2 4]\n", + " [0 0 3]]\n", + "The system is now triangular and the equations can be solved by Back substitution\n" + ] + } + ], + "source": [ + "from sympy.abc import u,v,w\n", + "from numpy import array\n", + "a =array([[1 ,1 ,1],[2, 2, 5],[4, 6, 8]])\n", + "x=[[u],[v],[w]]\n", + "x=array(x)\n", + "print 'x=\\n',x\n", + "print 'R2=R2-R1,R3=R3-4*R1'\n", + "a[1,:]=a[1,:]-2*a[0,:]\n", + "a[2,:]=a[2,:]-4*a[0,:]\n", + "print a\n", + "print 'R2<->R3'\n", + "def swap_rows(arr, frm, to):\n", + " arr[[frm, to],:] = arr[[to, frm],:]\n", + "swap_rows(a,1,2)\n", + "print a\n", + "print 'The system is now triangular and the equations can be solved by Back substitution'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ex:1.3.2 Pg:21" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a=\n", + "[[1 1 1]\n", + " [2 2 5]\n", + " [4 4 8]]\n", + "x=\n", + "[[u]\n", + " [v]\n", + " [w]]\n", + "R2=R2-2*R1,R3=R3-4*R1\n", + "[[1 1 1]\n", + " [0 0 3]\n", + " [0 0 4]]\n", + "No exchange of equations can avoid zero in the second pivot positon ,therefore the equations are unsolvable\n" + ] + } + ], + "source": [ + "from sympy.abc import u,v,w\n", + "from numpy import array\n", + "a =array([[1, 1, 1],[2, 2, 5],[4, 4, 8]])\n", + "print 'a=\\n',a\n", + "x=[[u],[v],[w]]\n", + "x=array(x)\n", + "print 'x=\\n',x\n", + "print 'R2=R2-2*R1,R3=R3-4*R1'\n", + "a[1,:]=a[1,:]-2*a[0,:]\n", + "a[2,:]=a[2,:]-4*a[0,:]\n", + "print a\n", + "print 'No exchange of equations can avoid zero in the second pivot positon ,therefore the equations are unsolvable'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Ex:1.4.1 Pg:21" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "A = \n", + "[[2 3]\n", + " [4 0]]\n", + "B = \n", + "[[ 1 2 0]\n", + " [ 5 -1 0]]\n", + "AB=\n", + "[[17 1 0]\n", + " [ 4 8 0]]\n" + ] + } + ], + "source": [ + "from numpy import array\n", + "A=array([[2, 3],[4, 0]])\n", + "print 'A = \\n',A\n", + "B=array([[1, 2, 0],[5, -1, 0]])\n", + "print 'B = \\n',B\n", + "print 'AB=\\n',A.dot(B)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ex:1.4.2 Pg:22" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "A=\n", + "[[2 3]\n", + " [7 8]]\n", + "P(Row exchange matrix)=\n", + "[[0 1]\n", + " [1 0]]\n", + "PA=\n", + "[[7 8]\n", + " [2 3]]\n" + ] + } + ], + "source": [ + "from numpy import array\n", + "A=array([[2, 3],[7, 8]])\n", + "print 'A=\\n',A\n", + "P=array([[0, 1],[1, 0]])\n", + "print 'P(Row exchange matrix)=\\n',P\n", + "print 'PA=\\n',P.dot(A)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ex:1.4.3 Pg:24" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "A=\n", + "[[1 2]\n", + " [3 4]]\n", + "I=\n", + "[[ 1. 0.]\n", + " [ 0. 1.]]\n", + "IA=\n", + "[[ 1. 2.]\n", + " [ 3. 4.]]\n" + ] + } + ], + "source": [ + "from numpy import array,identity\n", + "A=array([[1, 2],[3, 4]])\n", + "print 'A=\\n',A\n", + "I=identity(2)\n", + "print 'I=\\n',I\n", + "print 'IA=\\n',I.dot(A)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ex:1.4.4 Pg:25" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "E=\n", + "[[ 1. 0. 0.]\n", + " [-2. 1. 0.]\n", + " [ 0. 0. 1.]]\n", + "F=\n", + "[[ 1. 0. 0.]\n", + " [ 0. 1. 0.]\n", + " [ 1. 0. 1.]]\n", + "EF=\n", + "[[ 1. 0. 0.]\n", + " [-2. 1. 0.]\n", + " [ 1. 0. 1.]]\n", + "FE=\n", + "[[ 1. 0. 0.]\n", + " [-2. 1. 0.]\n", + " [ 1. 0. 1.]]\n", + "Here,EF=FE,so this shows that these two matrices commute\n" + ] + } + ], + "source": [ + "from numpy import array,identity\n", + "E=identity(3)\n", + "E[1,:]=E[1,:]-2*E[0,:]\n", + "print 'E=\\n',E\n", + "F=identity(3)\n", + "F[2,:]=F[2,:]+F[0,:]\n", + "print 'F=\\n',F\n", + "print 'EF=\\n',E.dot(F)\n", + "print 'FE=\\n',F.dot(E)\n", + "print 'Here,EF=FE,so this shows that these two matrices commute'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ex:1.4.5 Pg:25" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "E=\n", + "[[ 1. 0. 0.]\n", + " [-2. 1. 0.]\n", + " [ 0. 0. 1.]]\n", + "F=\n", + "[[ 1. 0. 0.]\n", + " [ 0. 1. 0.]\n", + " [ 1. 0. 1.]]\n", + "G=\n", + "[[ 1. 0. 0.]\n", + " [ 0. 1. 0.]\n", + " [ 0. 1. 1.]]\n", + "GE= [[ 1. 0. 0.]\n", + " [-2. 1. 0.]\n", + " [-2. 1. 1.]]\n", + "EG=\n", + "[[ 1. 0. 0.]\n", + " [-2. 1. 0.]\n", + " [ 0. 1. 1.]]\n", + "Here EG is not equal to GE,Therefore these two matrices do not commute and shows that most matrices do not commute.\n", + "GFE=\n", + "[[ 1. 0. 0.]\n", + " [-2. 1. 0.]\n", + " [-1. 1. 1.]]\n", + "EFG=\n", + "[[ 1. 0. 0.]\n", + " [-2. 1. 0.]\n", + " [ 1. 1. 1.]]\n", + "The product GFE is the true order of elimation.It is the matrix that takes the original A to the upper triangular U.\n" + ] + } + ], + "source": [ + "from numpy import array,identity\n", + "E=identity(3)\n", + "E[1,:]=E[1,:]-2*E[0,:]\n", + "print 'E=\\n',E\n", + "F=identity(3)\n", + "F[2,:]=F[2,:]+F[0,:]\n", + "print 'F=\\n',F\n", + "G=identity(3)\n", + "G[2,:]=G[2,:]+G[1,:]\n", + "print 'G=\\n',G\n", + "print 'GE=',G.dot(E)\n", + "print 'EG=\\n',E.dot(G)\n", + "print 'Here EG is not equal to GE,Therefore these two matrices do not commute and shows that most matrices do not commute.'\n", + "print 'GFE=\\n',G.dot(F.dot(E))\n", + "print 'EFG=\\n',E.dot(F.dot(G))\n", + "print 'The product GFE is the true order of elimation.It is the matrix that takes the original A to the upper triangular U.'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ex:1.5.1 Pg: 34" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "A= [[1 2]\n", + " [3 8]]\n", + "L= [[ 1. 0. ]\n", + " [ 0.33333333 1. ]]\n", + "U= [[ 3. 8. ]\n", + " [ 0. -0.66666667]]\n", + "LU=\n", + "[[ 3. 8.]\n", + " [ 1. 2.]]\n" + ] + } + ], + "source": [ + "from numpy import mat,dot\n", + "from scipy.linalg import lu\n", + "A=mat([[1, 2],[3, 8]])\n", + "print 'A=',A\n", + "L=lu(A)[1]\n", + "U=lu(A)[2]\n", + "print 'L=',L\n", + "print 'U=',U\n", + "L=mat(L);U=mat(U)\n", + "print 'LU=\\n',dot(L,U)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ex:1.5.2 Pg: 34" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "A= [[0 2]\n", + " [3 4]]\n", + "Here this cannot be factored into A=LU,(Needs a row exchange)\n" + ] + } + ], + "source": [ + "from numpy import mat\n", + "A=[[0, 2],[3, 4]]\n", + "print 'A=',mat(A)\n", + "print 'Here this cannot be factored into A=LU,(Needs a row exchange)'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ex:1.5.3 Pg: 34" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Given Matrix:\n", + "A= [[1, 1, 1], [1, 2, 2], [1, 2, 3]]\n", + "\n", + "L= [[ 1. 0. 0.]\n", + " [ 1. 1. 0.]\n", + " [ 1. 1. 1.]]\n", + "\n", + "U= [[ 1. 1. 1.]\n", + " [ 0. 1. 1.]\n", + " [ 0. 0. 1.]]\n", + "\n", + "LU=\n", + "[[ 1. 1. 1.]\n", + " [ 1. 2. 2.]\n", + " [ 1. 2. 3.]]\n", + "Here LU=A,from A to U there are subtraction of rows.Frow U to A there are additions of rows\n" + ] + } + ], + "source": [ + "from numpy import mat\n", + "from scipy.linalg import lu\n", + "print 'Given Matrix:'\n", + "A=[[1, 1 ,1],[1, 2, 2],[1, 2, 3]]\n", + "print 'A=',A\n", + "L=lu(A)[1]\n", + "U=lu(A)[2]\n", + "print '\\nL=',L\n", + "print '\\nU=',U\n", + "print '\\nLU=\\n',mat(L)*mat(U)\n", + "print 'Here LU=A,from A to U there are subtraction of rows.Frow U to A there are additions of rows'\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ex:1.5.4 Pg: 34" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "L=\n", + "[[ 1. 0. 0. ]\n", + " [ 0.74342501 1. 0. ]\n", + " [ 0.97556591 0.5785538 1. ]]\n", + "U=\n", + "[[ 1. 0. 0.]\n", + " [ 0. 1. 0.]\n", + " [ 0. 0. 1.]]\n", + "E=\n", + "[[ 1. 0. 0. ]\n", + " [-0.74342501 1. 0. ]\n", + " [ 0. 0. 1. ]]\n", + "F=\n", + "[[ 1. 0. 0. ]\n", + " [ 0. 1. 0. ]\n", + " [-0.97556591 0. 1. ]]\n", + "G=\n", + "[[ 1. 0. 0. ]\n", + " [ 0. 1. 0. ]\n", + " [ 0. -0.5785538 1. ]]\n", + "A=inv(E)*inv(F)*inv(G)*U\n", + "A=\n", + "[[ 1. 0. 0. ]\n", + " [ 0.74342501 1. 0. ]\n", + " [ 0.97556591 0.5785538 1. ]]\n", + "When U is identity matrix then L is same as A\n" + ] + } + ], + "source": [ + "from numpy.random import random\n", + "from numpy import mat,eye\n", + "a=random()\n", + "b=random()\n", + "c=random()\n", + "L=mat([[1, 0, 0],[a, 1, 0],[b, c, 1]])\n", + "print 'L=\\n',L\n", + "U=eye(3)\n", + "print 'U=\\n',U\n", + "E=mat([[1, 0, 0],[-a, 1, 0],[0, 0, 1]])\n", + "print 'E=\\n',E\n", + "F=mat([[1, 0, 0],[0, 1, 0],[-b, 0, 1]])\n", + "print 'F=\\n',F\n", + "G=mat([[1, 0, 0],[0, 1, 0],[0, -c, 1]])\n", + "print 'G=\\n',G\n", + "print 'A=inv(E)*inv(F)*inv(G)*U'\n", + "A=(E**-1)*(F**-1)*(G**-1)*U#\n", + "print 'A=\\n',A\n", + "print 'When U is identity matrix then L is same as A'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ex:1.5.5 Pg: 39" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "A=\n", + "[[ 1 -1 0 0]\n", + " [-1 2 -1 0]\n", + " [ 0 -1 2 -1]\n", + " [ 0 0 -1 2]]\n", + "U=\n", + "[[ 1. -1. 0. 0.]\n", + " [ 0. 1. -1. 0.]\n", + " [ 0. 0. 1. -1.]\n", + " [ 0. 0. 0. 1.]]\n", + "L=\n", + "[[ 1. 0. 0. 0.]\n", + " [-1. 1. 0. 0.]\n", + " [ 0. -1. 1. 0.]\n", + " [ 0. 0. -1. 1.]]\n", + "This shows how a matrix A with 3 diagnols has factors L and U with two diagnols.\n" + ] + } + ], + "source": [ + "from numpy import mat\n", + "from scipy.linalg import lu\n", + "\n", + "A=mat([[1, -1, 0, 0],[-1, 2 ,-1, 0],[0, -1, 2 ,-1],[0, 0 ,-1 ,2]])\n", + "print 'A=\\n',A\n", + "L=lu(A)[1]\n", + "U=lu(A)[2]\n", + "print 'U=\\n',U\n", + "print 'L=\\n',L\n", + "print 'This shows how a matrix A with 3 diagnols has factors L and U with two diagnols.'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ex:1.5.6 Pg: 36" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a=\n", + "[[ 1 -1 0 0]\n", + " [-1 2 -1 0]\n", + " [ 0 -1 2 -1]\n", + " [ 0 0 -1 2]]\n", + "b=\n", + "[[1]\n", + " [1]\n", + " [1]\n", + " [1]]\n", + "Given Equation ,ax=b\n", + "U=\n", + "[[ 1. -1. 0. 0.]\n", + " [ 0. 1. -1. 0.]\n", + " [ 0. 0. 1. -1.]\n", + " [ 0. 0. 0. 1.]]\n", + "L=\n", + "[[ 1. 0. 0. 0.]\n", + " [-1. 1. 0. 0.]\n", + " [ 0. -1. 1. 0.]\n", + " [ 0. 0. -1. 1.]]\n", + "Augmented Matrix of L and b=\n", + "[[ 1. 0. 0. 0. 1. -1. 0. 0.]\n", + " [-1. 1. 0. 0. 0. 1. -1. 0.]\n", + " [ 0. -1. 1. 0. 0. 0. 1. -1.]\n", + " [ 0. 0. -1. 1. 0. 0. 0. 1.]]\n", + "c=\n", + "[[ 1.]\n", + " [ 2.]\n", + " [ 2.]\n", + " [ 2.]]\n", + "Augmented matrix of U and c=\n", + "[[ 1. -1. 0. 0. 1.]\n", + " [ 0. 1. -1. 0. 2.]\n", + " [ 0. 0. 1. -1. 2.]\n", + " [ 0. 0. 0. 1. 2.]]\n", + "x=\n", + "[[ 0.]\n", + " [ 6.]\n", + " [ 4.]\n", + " [ 2.]]\n" + ] + } + ], + "source": [ + "from numpy import mat,hstack,zeros,arange\n", + "from scipy.linalg import lu\n", + "\n", + "a=mat([[1, -1, 0 ,0],[-1, 2, -1, 0],[0, -1, 2, -1],[0, 0, -1, 2]])\n", + "print 'a=\\n',a\n", + "b=mat([[1],[1],[1],[1]])\n", + "print 'b=\\n',b\n", + "print 'Given Equation ,ax=b'\n", + "\n", + "L=lu(a)[1]\n", + "U=lu(a)[2]\n", + "print 'U=\\n',U\n", + "print 'L=\\n',L\n", + "print 'Augmented Matrix of L and b='\n", + "A=hstack([L,U])\n", + "print A\n", + " \n", + "\n", + "c=zeros([4,1])#\n", + "n=4\n", + "#Algorithm Finding the value of c\n", + "c[0]=A[0,n]/A[0,0]\n", + "for i in range(1,n):\n", + " sumk=0#\n", + " for k in range(0,n-1):\n", + " sumk=sumk+A[i,k]*c[k]\n", + " \n", + " c[i]=(A[i,n+1]-sumk)/A[i,i]\n", + "\n", + "print 'c=\\n',c\n", + "x=zeros([4,1])\n", + "print 'Augmented matrix of U and c='\n", + "B=hstack([U,c])\n", + "print B\n", + "#Algorithm for finding value of x\n", + "x[n-1]=B[n-1,n]/B[n-1,n-1]\n", + "for i in arange(n-1-1,-1+1,-1):\n", + " sumk=0#\n", + " for k in range(i+1-1,n):\n", + " sumk=sumk+B[i,k]*x[k]\n", + " \n", + " x[i]=(B[i,n+1-1]-sumk)/B[i,i]\n", + "\n", + "print 'x=\\n',x\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ex:1.5.7 Pg: 39" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "A=\n", + "[[1 1 1]\n", + " [1 1 3]\n", + " [2 5 8]]\n", + "L=\n", + "[[ 1. 0. 0. ]\n", + " [ 0.5 1. 0. ]\n", + " [ 0.5 1. 1. ]]\n", + "U=\n", + "[[ 2. 5. 8. ]\n", + " [ 0. -1.5 -1. ]\n", + " [ 0. 0. -2. ]]\n", + "P=\n", + "[[ 0. 0. 1.]\n", + " [ 0. 1. 0.]\n", + " [ 1. 0. 0.]]\n", + "PA=\n", + "[[ 2. 5. 8.]\n", + " [ 1. 1. 3.]\n", + " [ 1. 1. 1.]]\n", + "LU=\n", + "[[ 2. 0. 0. ]\n", + " [ 0. -1.5 -0. ]\n", + " [ 0. 0. -2. ]]\n" + ] + } + ], + "source": [ + "from numpy import mat\n", + "from scipy.linalg import lu\n", + "\n", + "A=mat([[1, 1, 1],[1, 1, 3],[2, 5, 8]])\n", + "print 'A=\\n',A\n", + "P=lu(A)[0]\n", + "L=lu(A)[1]\n", + "U=lu(A)[2]\n", + "print 'L=\\n',L\n", + "print 'U=\\n',U\n", + "print 'P=\\n',P\n", + "print 'PA=\\n',(P*A)\n", + "print 'LU=\\n',(L*U)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ex:1.6.1 Pg: 47" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Given matrix:\n", + "[[ 2 1 1]\n", + " [ 4 -6 0]\n", + " [-2 7 2]]\n", + "Augmented matrix :\n", + "[[ 2. 1. 1. 1. 0. 0.]\n", + " [ 4. -6. 0. 0. 1. 0.]\n", + " [-2. 7. 2. 0. 0. 1.]]\n", + "R2=R2-2*R1,R3=R3-(-2)*R1\n", + "[[ 2. 1. 1. 1. 0. 0.]\n", + " [ 8. -4. 2. 2. 1. 0.]\n", + " [ 0. 8. 3. 1. 0. 1.]]\n", + "R3=R3-(-1)*R2\n", + "a=\n", + "[[ 2. 1. 1. 1. 0. 0.]\n", + " [ 8. -4. 2. 2. 1. 0.]\n", + " [ 8. 4. 5. 3. 1. 1.]]\n", + "[U inv(L)] :\n", + "[[ 2. 1. 1. 1. 0. 0.]\n", + " [ 8. -4. 2. 2. 1. 0.]\n", + " [ 8. 4. 5. 3. 1. 1.]]\n", + "R2=R2-(-2)*R3,R1=R1-R3\n", + "[[ 10. 5. 6. 4. 1. 1.]\n", + " [ 24. 4. 12. 8. 3. 2.]\n", + " [ 8. 4. 5. 3. 1. 1.]]\n", + "R1=R1-(-1/8)*R2)\n", + "[[ 13. 5.5 7.5 5. 1.375 1.25 ]\n", + " [ 24. 4. 12. 8. 3. 2. ]\n", + " [ 8. 4. 5. 3. 1. 1. ]]\n", + "[I inv(A)]:\n", + "[[ 1. 0.42307692 0.57692308 0.38461538 0.10576923 0.09615385]\n", + " [ 6. 1. 3. 2. 0.75 0.5 ]\n", + " [ 1.6 0.8 1. 0.6 0.2 0.2 ]]\n", + "inv(A):\n", + "[[ 0.38461538 0.10576923 0.09615385]\n", + " [ 2. 0.75 0.5 ]\n", + " [ 0.6 0.2 0.2 ]]\n" + ] + } + ], + "source": [ + "from numpy import mat,shape,nditer,hstack,eye,nditer\n", + "\n", + "print 'Given matrix:'\n", + "A=mat([[2, 1, 1],[4, -6, 0],[-2, 7, 2]])\n", + "print A\n", + "m=int(shape(A)[0])\n", + "n=int(shape(A)[1])\n", + "print 'Augmented matrix :'\n", + "a=hstack([A,eye(n)])\n", + "print a\n", + "print 'R2=R2-2*R1,R3=R3-(-2)*R1'\n", + "def fun1(a,x,y,n):\n", + " import numpy as np\n", + " z=[]\n", + " for xx,yy in nditer([a[x-1],a[y-1]]):\n", + " z.append(xx-n*yy)\n", + " a[x-1]=z\n", + " return a \n", + "\n", + " \n", + "a=fun1(a,2,1,-2)\n", + "a=fun1(a,3,1,-1)\n", + "print a\n", + "print 'R3=R3-(-1)*R2'\n", + "a=fun1(a,3,2,-1) # a(3,:)-(-1)*a(2,:)#\n", + "print 'a=\\n',a\n", + "print '[U inv(L)] :\\n',a\n", + "print 'R2=R2-(-2)*R3,R1=R1-R3'\n", + "a=fun1(a,2,3,-2)\n", + "a=fun1(a,1,3,-1)\n", + "print a\n", + "print 'R1=R1-(-1/8)*R2)'\n", + "a=fun1(a,1,2,-1./8)\n", + "print a\n", + "\n", + "a[0]=a[0]/a[0,0]\n", + "a[1]=a[1]/a[1,1]\n", + "print '[I inv(A)]:'\n", + "a[2]=a[2]/a[2,2]\n", + "print a\n", + "print 'inv(A):'\n", + "print a[:,range(3,6)]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ex:1.6.2 Pg:51" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "R= [[1 2]]\n", + "Transpose of the given matrix is : [[1]\n", + " [2]]\n", + "The product of R & transpose(R)is : [[5]]\n", + "The product of transpose(R)& R is : [[1 2]\n", + " [2 4]]\n", + "Rt*R and R*Rt are not likely to be equal even if m==n.\n" + ] + } + ], + "source": [ + "from numpy import mat,transpose\n", + "R=mat([1, 2])\n", + "print 'R=',R\n", + "Rt=transpose(R)\n", + "print 'Transpose of the given matrix is :',Rt\n", + "print 'The product of R & transpose(R)is :',(R*Rt)\n", + "print 'The product of transpose(R)& R is :',(Rt*R)\n", + "print 'Rt*R and R*Rt are not likely to be equal even if m==n.'" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.9" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} |