{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter2 Vector Spaces" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ex:2.1.1 Pg: 70" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Consider all vectors in R**2 whose components are positive or zero\n", "The subset is first Quadrant of x-y plane,the co-ordinates satisfy x>=0 and y>=0.It is not a subspace.\n", "If the Vector= [1, 1]\n", "Taking a scalar,c=-1\n", "c*v= [-1, -1]\n", "It lies in third Quadrant instead of first,Hence violating the rule(ii).\n" ] } ], "source": [ "print 'Consider all vectors in R**2 whose components are positive or zero'\n", "print 'The subset is first Quadrant of x-y plane,the co-ordinates satisfy x>=0 and y>=0.It is not a subspace.'\n", "v=[1,1]\n", "print 'If the Vector=',v\n", "print 'Taking a scalar,c=-1'\n", "c=-1# #scalar\n", "print 'c*v=',[c*vv for vv in v]\n", "print 'It lies in third Quadrant instead of first,Hence violating the rule(ii).'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ex:2.3.2 Pg: 92" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Given matrix:\n", "[[ 1 3 3 2]\n", " [ 2 6 9 5]\n", " [-1 -3 3 0]]\n", "C2->C2-3*C1\n", "[[ 1 0 3 2]\n", " [ 2 0 9 5]\n", " [-1 0 3 0]]\n", "Here,C2=3*C1,Therefore the columns are linearly dependent.\n", "R3->R3-2*R2+5*R1\n", "[[1 0 3 2]\n", " [2 0 9 5]\n", " [0 0 0 0]]\n", "Here R3=R3-2*R2+5*R1,therefore the rows are linearly dependent.\n" ] } ], "source": [ "from numpy import mat\n", "A=mat([[1, 3 ,3 ,2],[2, 6, 9, 5],[-1, -3, 3, 0]])\n", "print 'Given matrix:'\n", "print A\n", "B=A\n", "print 'C2->C2-3*C1'\n", "A[:,1]=A[:,1]-3*A[:,0]\n", "print A\n", "print 'Here,C2=3*C1,Therefore the columns are linearly dependent.'\n", "print 'R3->R3-2*R2+5*R1'\n", "B[2,:]=B[2,:]-2*B[1,:]+5*B[0,:]\n", "print B\n", "print 'Here R3=R3-2*R2+5*R1,therefore the rows are linearly dependent.'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ex:2.3.3 Pg: " ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A=\n", "[[3 4 2]\n", " [0 1 5]\n", " [0 0 2]]\n", "The columns of the triangular matrix are linearly independent,it has no zeros on the diagonal\n" ] } ], "source": [ "from numpy import mat\n", "A=mat([[3, 4 ,2],[0, 1 ,5],[0, 0, 2]])\n", "print 'A=\\n',A\n", "print 'The columns of the triangular matrix are linearly independent,it has no zeros on the diagonal'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ex:2.3.4 Pg: 93" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The columns of the nxn identity matrix are independent.\n", "I=\n", "[[ 1. 0. 0. 0.]\n", " [ 0. 1. 0. 0.]\n", " [ 0. 0. 1. 0.]\n", " [ 0. 0. 0. 1.]]\n" ] } ], "source": [ "from numpy import eye\n", "print 'The columns of the nxn identity matrix are independent.'\n", "n=4 # size for identity matrix\n", "I=eye(n)\n", "print 'I=\\n',I" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ex:2.3.5 Pg: 93" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Three columns in R2 cannot be independent.\n", "Given matrix:\n", "[[1 2 1]\n", " [1 2 3]]\n", "U=\n", "[[ 1. 2. 1.]\n", " [ 0. 0. 2.]]\n", "If c3 is 1 ,then back-substitution Uc=0 gives c2=-1,c1=1,With these three weights,the first column minus the second plus the third equals zero ,therefore linearly dependent.\n" ] } ], "source": [ "from scipy.linalg import lu\n", "from numpy import mat\n", "print 'Three columns in R2 cannot be independent.'\n", "A=mat([[1, 2, 1],[1, 2, 3]])\n", "print 'Given matrix:\\n',A\n", "L=lu(A)[1]\n", "U=lu(A)[2]\n", "print 'U=\\n',U\n", "print 'If c3 is 1 ,then back-substitution Uc=0 gives c2=-1,c1=1,With these three weights,the first column minus the second plus the third equals zero ,therefore linearly dependent.'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ex:2.3.9 Pg: 96" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "These four columns span the column space U,but they are not independent.\n", "U=\n", "[[1 3 3 2]\n", " [0 0 3 1]\n", " [0 0 0 0]]\n", "The columns that contains pivots (here 1st & 3rd) are a basis for the column space. These columns are independent, and it is easy to see that they span the space.In fact,the column space of U is just the x-y plane withinn R3. C(U) is not the same as the column space C(A) before elimination-but the number of independent columns did not change.\n" ] } ], "source": [ "from numpy import mat\n", "print 'These four columns span the column space U,but they are not independent.'\n", "U=mat([[1, 3 ,3, 2],[0, 0 ,3 ,1],[0, 0, 0, 0]])\n", "print 'U=\\n',U\n", "print 'The columns that contains pivots (here 1st & 3rd) are a basis for the column space. These columns are independent, and it is easy to see that they span the space.In fact,the column space of U is just the x-y plane withinn R3. C(U) is not the same as the column space C(A) before elimination-but the number of independent columns did not change.'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ex:2.4.1 Pg: 107" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A=\n", "[[1 2]\n", " [3 6]]\n", "m= 2\n", "n= 2\n", "rank= 1\n", "Column space= [[1]\n", " [3]]\n", "Null space=\n", "[[-0.89442719]\n", " [ 0.4472136 ]]\n", "Row space=\n", "[[1]\n", " [2]]\n", "Left null sapce=\n", "[[-0.9486833 ]\n", " [ 0.31622777]]\n" ] } ], "source": [ "from numpy import mat,shape\n", "from sympy import Matrix\n", "from scipy import linalg, matrix, compress,transpose\n", "A=mat([[1 ,2],[3, 6]])\n", "print 'A=\\n',A\n", "m=shape(A)[0]\n", "n=shape(A)[1]\n", "print 'm=',m\n", "print 'n=',n\n", "v=Matrix(A).rref()[0]\n", "pivot=Matrix(A).rref()[1]\n", "r=len(pivot)\n", "print 'rank=',r\n", "cs=A[:,r-1]\n", "print 'Column space=',cs\n", "\n", "\n", "def kernel(A, eps=1e-15):\n", " u, s, vh = linalg.svd(A)\n", " null_mask = (s <= eps)\n", " null_space = compress(null_mask, vh, axis=0)\n", " return transpose(null_space)\n", "A=mat([[1 ,2],[3, 6]])\n", "\n", "ns=kernel(A)\n", "print 'Null space=\\n',ns\n", "v=mat(v)\n", "rs=transpose(v[range(0,r),:])\n", "print 'Row space=\\n',rs\n", "lns=kernel(transpose(A))\n", "print 'Left null sapce=\\n',lns" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ex:2.4.2 Pg: 108" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A=\n", "[[4 0 0]\n", " [0 5 0]]\n", "m= 2\n", "n= 3\n", "rank= 2\n", "since m=r=2 ,there exists a right inverse .\n", "Best right inverse=\n", "[[ 0.25 0. ]\n", " [ 0. 0.2 ]\n", " [ 0. 0. ]]\n" ] } ], "source": [ "from numpy import mat,shape,rank,transpose\n", "A=mat([[4, 0, 0],[0, 5, 0]])\n", "print 'A=\\n',A\n", "m=shape(A)[0]\n", "n=shape(A)[1]\n", "print 'm=',m\n", "print 'n=',n\n", "r=rank(A)\n", "print 'rank=',r\n", "print 'since m=r=2 ,there exists a right inverse .'\n", "C=transpose(A)*(A*transpose(A))**-1\n", "print 'Best right inverse=\\n',C" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ex:2.5.1 Pg: 121" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Applying current law Ay=f at nodes 1,2,3:\n", "A' = \n", "[[-1 0 -1 0 -1]\n", " [ 1 -1 0 0 0]\n", " [ 0 1 1 -1 0]]\n", "[[ 0. ]\n", " [ 0. ]\n", " [ 0.59010697]\n", " [ 0. ]\n", " [ 0. ]\n", " [ 0. ]\n", " [ 0.55757785]\n", " [ 0. ]]\n", "The other equation is inv(C)y+Ax=b.The block form of the two equations is:\n", "[[ 1.37051236 0. 0. 0. 0. -1.\n", " 1. 0. ]\n", " [ 0. 1.54412147 0. 0. 0. 0.\n", " -1. 1. ]\n", " [ 0. 0. 9.1949881 0. 0. -1.\n", " 0. 1. ]\n", " [ 0. 0. 0. 13.91590014 0. 0.\n", " 0. -1. ]\n", " [ 0. 0. 0. 0. 2.46587381 -1.\n", " 0. 0. ]\n", " [ -1. 0. -1. 0. -1. 0.\n", " 0. 0. ]\n", " [ 1. -1. 0. 0. 0. 0.\n", " 0. 0. ]\n", " [ 0. 1. 1. -1. 0. 0.\n", " 0. 0. ]]\n", "X=\n", "[['y1']\n", " ['y2']\n", " ['y3']\n", " ['y4']\n", " ['y5']\n", " ['x1']\n", " ['x2']\n", " ['x3']]\n", "X= [[ 0.3717052 ]\n", " [-0.18587265]\n", " [ 0.08836592]\n", " [-0.09750673]\n", " [-0.46007112]\n", " [-1.13447733]\n", " [-1.6439039 ]\n", " [-1.35689395]]\n" ] } ], "source": [ "from numpy import mat,transpose,diag,zeros,random,vstack,hstack,linalg\n", "print 'Applying current law A''y=f at nodes 1,2,3:'\n", "A=mat([[-1, 1 ,0],[0, -1, 1],[ -1, 0 ,1],[0, 0 ,-1],[-1, 0, 0]])\n", "print \"A' = \\n\",transpose(A)\n", "C=diag(random.rand(5))# #Taking some values for the resistances.\\\n", "b=zeros([5,1])\n", "b[2,0]=random.rand(1)[0]##Taking some value of the battery.\n", "f=zeros([3,1])\n", "f[1,0]=random.rand(1)[0]##Taking some value of the current source.\n", "B=vstack([b,f])#[b]#f]\n", "print B\n", "print 'The other equation is inv(C)y+Ax=b.The block form of the two equations is:'\n", "#C=[C**-1 A],[np.transpose(A),np.zeros([3,3])]\n", "C1=hstack([linalg.inv(C),A])\n", "C2=hstack([transpose(A),zeros([3,3])])\n", "C=vstack([C1,C2])\n", "print C\n", "X=mat([['y1'],['y2'],['y3'],['y4'],['y5'],['x1'],['x2'],['x3']])\n", "print \"X=\\n\",X\n", "X=linalg.solve(C,B)\n", "print 'X=',X" ] } ], "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 }