{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 3 - Linear transformation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Page 70 Example 3.6" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a1 = [1, 2]\n", "a2 = [3, 4]\n", "a1 and a2 are linearly independent and hence form a basis for R**2\n", "According to theorem 1, there is a linear transformation from R**2 to R**3 with the transformation functions as:\n", "Ta1 = [3, 2, 1]\n", "Ta2 = [6, 5, 4]\n", "Now, we find scalars c1 and c2 for that we know T(c1a1 + c2a2) = c1(Ta1) + c2(Ta2))\n", "if(1,0) = c1(1,2) + c2(3,4), then \n", "c1 = 1\n", "c2 = 3\n", "The transformation function T(1,0) will be:\n", "T(1,0) = [3, 2, 1, 6, 5, 4, 6, 5, 4, 6, 5, 4]\n" ] } ], "source": [ "from numpy import array\n", "a1 = [1, 2]#\n", "a2 = [3 ,4]#\n", "print 'a1 = ',a1\n", "print 'a2 = ',a2\n", "print 'a1 and a2 are linearly independent and hence form a basis for R**2'\n", "print 'According to theorem 1, there is a linear transformation from R**2 to R**3 with the transformation functions as:'\n", "Ta1 = [3 ,2 ,1]#\n", "Ta2 = [6, 5, 4]#\n", "print 'Ta1 = ',Ta1\n", "print 'Ta2 = ',Ta2\n", "print 'Now, we find scalars c1 and c2 for that we know T(c1a1 + c2a2) = c1(Ta1) + c2(Ta2))'\n", "print 'if(1,0) = c1(1,2) + c2(3,4), then '\n", "#c = inv([a1#a2]') * [1#0]#\n", "c=array([a1,a2]).dot(array([[1],[0]]))\n", "c1 = c[0,0]\n", "c2 = c[1,0]\n", "print 'c1 = ',c1\n", "print 'c2 = ',c2\n", "print 'The transformation function T(1,0) will be:'\n", "T = c1*Ta1 + c2*Ta2#\n", "print 'T(1,0) = ',T\n", "#end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Page 81 Example 3.12" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x1 = 3\n", "x2 = 8\n", "T(3,8) = [11, 3]\n", "If, T(x1,x2) = 0, then\n", "x1 = x2 = 0\n", "So, T is non-singular\n", "z1,z2 are two scalars in F\n", "z1 = 0\n", "z2 = 4\n", "So, x1 = 4\n", "x2 = -4\n", "Hence, T is onto.\n", "inverse(T) = [4, -4]\n" ] } ], "source": [ "from numpy import array,random\n", "#x = round(rand(1,2) * 10)#\n", "x1 = random.randint(1,9)\n", "x2 = random.randint(1,9)\n", "T = [x1+x2 ,x1]\n", "print 'x1 = ',x1\n", "print 'x2 = ',x2\n", "print 'T(%d,%d) = '%(x1,x2),\n", "print T\n", "print 'If, T(x1,x2) = 0, then'\n", "print 'x1 = x2 = 0'\n", "print 'So, T is non-singular'\n", "print 'z1,z2 are two scalars in F'\n", "\n", "z1 = random.randint(0,9)\n", "z2 = random.randint(0,9)\n", "print 'z1 = ',z1\n", "print 'z2 = ',z2\n", "x1 = z2#\n", "x2 = z1 - z2#\n", "print 'So, x1 = ',x1\n", "print 'x2 = ',x2\n", "print 'Hence, T is onto.'\n", "Tinv = [z2, z1-z2]# \n", "print 'inverse(T) = ',Tinv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Page 89 Example 3.14" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "T is a linear operator on F**2 defined as:\n", "T(x1,x2) = (x1,0)\n", "B = {e1,e2} is a standard ordered basis for F**2,then\n", "So, Te1 = T(1,0) = [1, 0]\n", "So, Te2 = T(0,1) = [0, 0]\n", "so,matrix T in ordered basis B is: \n", "T = \n", "[[1, 0], [0, 0]]\n" ] } ], "source": [ "print 'T is a linear operator on F**2 defined as:'\n", "print 'T(x1,x2) = (x1,0)'\n", "print 'B = {e1,e2} is a standard ordered basis for F**2,then'\n", "x1 = 1#\n", "x2 = 0#\n", "Te1 = [x1, 0]#\n", "x1 = 0#\n", "x2 = 1#\n", "Te2 = [x1 ,0]#\n", "print 'So, Te1 = T(1,0) = ',Te1\n", "print 'So, Te2 = T(0,1) = ',Te2\n", "print 'so,matrix T in ordered basis B is: '\n", "T = [Te1,Te2]\n", "print 'T = \\n',T" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Page 89 Example 3.15" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Differentiation operator D is defined as:\n", "(Df1)(x) = 0\n", "(Df2)(x) = 1\n", "(Df3)(x) = 2*x\n", "(Df4)(x) = 3*x**2\n", "Matrix of D in ordered basis is:\n", "[D] = \n", "[[ 0. 1. 0. 0.]\n", " [ 0. 0. 2. 0.]\n", " [ 0. 0. 0. 3.]\n", " [ 0. 0. 0. 0.]]\n" ] } ], "source": [ "from numpy import array,zeros\n", "from sympy import Symbol,diff\n", "print 'Differentiation operator D is defined as:'\n", "D = zeros([4,4])\n", "x=Symbol('x')\n", "for i in range(1,5):\n", " t= i-1#\n", " f = diff(x**t,'x')\n", " print '(Df%d)(x) = '%(i),\n", " print f\n", " if not(i == 1):\n", " D[i-2,i-1] = i-1#\n", " \n", "\n", "print 'Matrix of D in ordered basis is:'\n", "print '[D] = \\n',D" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Page 92 Example 3.16" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "T is a linear operator on R**2 defined as T(x1,x2) = (x1,0)\n", "So, the matrix T in standard ordered basis B = {e1,e2} is \n", "[T]B = [[1 0]\n", " [0 0]]\n", "Let B is the ordered basis for R**2 consisting of vectors:\n", "E1 = [1 1]\n", "E2 = [2 1]\n", "So, matrix P = \n", "[[1 2]\n", " [1 1]]\n", "P inverse = \n", "[[-1. 2.]\n", " [ 1. -1.]]\n", "So, matrix T in ordered basis B is [T]B = \n", "[[-1. 0.]\n", " [ 0. -0.]]\n" ] } ], "source": [ "from numpy import array,transpose,linalg\n", "print 'T is a linear operator on R**2 defined as T(x1,x2) = (x1,0)'\n", "print 'So, the matrix T in standard ordered basis B = {e1,e2} is '\n", "T = array([[1, 0],[0, 0]])\n", "print '[T]B = ',T\n", "print 'Let B'' is the ordered basis for R**2 consisting of vectors:'\n", "E1 = array([1, 1])\n", "E2 = array([2 ,1])\n", "print 'E1 = ',E1\n", "print 'E2 = ',E2\n", "P = transpose(([E1,E2]))\n", "print 'So, matrix P = \\n',P\n", "Pinv=linalg.inv(P)\n", "print 'P inverse = \\n',Pinv\n", "T1 = Pinv*T*P#\n", "print 'So, matrix T in ordered basis B'' is [T]B'' = \\n',T1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Page 93 Example 3.17" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "g1 = f1\n", "g2 = t*f1 + f2\n", "g3 = t**2*f1 + 2*t*f2 + f3\n", "g4 = t**3*f1 + 3*t**2*f2 + 3*t*f3 + f4\n", "P = \n", "Matrix([[1, t, t**2, t**3], [0, 1, 2*t, 3*t**2], [0, 0, 1, 3*t], [0, 0, 0, 1]])\n", "inverse P = \n", "Matrix([[1, -t, t**2, -t**3], [0, 1, -2*t, 3*t**2], [0, 0, 1, -3*t], [0, 0, 0, 1]])\n", "Matrix of differentiation operator D in ordered basis B is:\n", "D = \n", "Matrix([[0, 1, 0, 0], [0, 0, 2, 0], [0, 0, 0, 3], [0, 0, 0, 0]])\n", "Matrix of D in ordered basis B is:\n", "inverse(P) * D * P = Matrix([[0, 1, 0, 0], [0, 0, 2, 0], [0, 0, 0, 3], [0, 0, 0, 0]])\n" ] } ], "source": [ "from sympy import Symbol, Matrix\n", "t = Symbol(\"t\")\n", "print 'g1 = f1'\n", "print 'g2 = t*f1 + f2'\n", "print 'g3 = t**2*f1 + 2*t*f2 + f3'\n", "print 'g4 = t**3*f1 + 3*t**2*f2 + 3*t*f3 + f4'\n", "P = Matrix(([1, t, t**2, t**3],[0 ,1 ,2*t, 3*t**2],[0, 0, 1, 3*t],[0, 0, 0, 1]))\n", "print 'P = \\n',P\n", "\n", "print 'inverse P = \\n',Matrix.inv(P)\n", "\n", "\n", "\n", "print 'Matrix of differentiation operator D in ordered basis B is:'# #As found in example 15\n", "D = Matrix(([0, 1, 0, 0],[0, 0, 2, 0],[0, 0, 0, 3],[0, 0, 0, 0]))\n", "print 'D = \\n',D\n", "print 'Matrix of D in ordered basis B'' is:'\n", "print 'inverse(P) * D * P = ',Matrix.inv(P)*D*P\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Page 98 Example 3.19" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "n = 5\n", "A = \n", "[[ 5. 9. 7. 8. 6.]\n", " [ 3. 3. 2. 7. 6.]\n", " [ 5. 1. 3. 2. 6.]\n", " [ 1. 2. 10. 4. 1.]\n", " [ 9. 0. 7. 5. 7.]]\n", "Trace of A:\n", "tr(A) = 22.0\n", "--------------------------------\n", "c = 2\n", "B = \n", "[[ 6. 8. 8. 2. 4.]\n", " [ 7. 6. 4. 3. 7.]\n", " [ 6. 9. 8. 4. 8.]\n", " [ 1. 4. 8. 4. 6.]\n", " [ 10. 8. 2. 1. 6.]]\n", "Trace of B:\n", "tr(B) = 30.0\n", "tr(cA + B) = 74.0\n" ] } ], "source": [ "from numpy import array,random\n", "def trace_matrix(M,n):\n", " tr=0\n", " for i in range(0,n):\n", " tr = tr + M[i,i]#\n", " return tr\n", "#n = round(rand() * 10 + 2)#\n", "n=random.randint(1,9)\n", "print 'n = ',n\n", "#A = round(rand(n,n) * 10)#\n", "A=random.rand(n,n)\n", "for x in range(0,n):\n", " for y in range(0,n):\n", " A[x,y]=round(A[x,y]*10)\n", "print 'A = \\n',A\n", "\n", "\n", "tr = 0#\n", "print 'Trace of A:'\n", "tr1 = trace_matrix(A,n)#\n", "print 'tr(A) = ',tr1\n", "print '--------------------------------'\n", "#c = round(rand() * 10 + 2)#\n", "c=random.randint(2,9)\n", "print 'c = ',c\n", "\n", "B=random.rand(n,n)\n", "for x in range(0,n):\n", " for y in range(0,n):\n", " B[x,y]=round(B[x,y]*10)\n", "print 'B = \\n',B\n", "\n", "print 'Trace of B:'\n", "tr2 = trace_matrix(B,n)#\n", "print 'tr(B) = ',tr2\n", "print 'tr(cA + B) = ',(c*tr1+tr2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Page 103 Example 3.23" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix represented by given linear functionals on R**4:\n", "A = \n", "[[ 1 2 2 1]\n", " [ 0 2 0 1]\n", " [-2 0 -4 3]]\n", "To find Row reduced echelon matrix of A given by R:\n", "Applying row transformations on A,we get\n", "R1 = R1-R2\n", "A = \n", "[[ 1 0 2 0]\n", " [ 0 2 0 1]\n", " [-2 0 -4 3]]\n", "R3 = R3 + 2*R1\n", "A = \n", "[[1 0 2 0]\n", " [0 2 0 1]\n", " [0 0 0 3]]\n", "R3 = R3/3\n", "A = \n", "[[1 0 2 0]\n", " [0 2 0 1]\n", " [0 0 0 1]]\n", "R2 = R2/2\n", "A = \n", "[[1 0 2 0]\n", " [0 1 0 0]\n", " [0 0 0 1]]\n", "R2 = R2 - 1/2*R3\n", "A = \n", "[[1 0 2 0]\n", " [0 1 0 0]\n", " [0 0 0 1]]\n", "Row reduced echelon matrix of A is:\n", "R = \n", "[[1 0 2 0]\n", " [0 1 0 0]\n", " [0 0 0 1]]\n", "Therefore,linear functionals g1,g2,g3 span the same subspace of (R**4)* as f1,f2,f3 are given by:\n", "g1(x1,x2,x3,x4) = x1 + 2*x3\n", "g1(x1,x2,x3,x4) = x2\n", "g1(x1,x2,x3,x4) = x4\n", "The subspace consists of the vectors with\n", "x1 = -2*x3\n", "x2 = x4 = 0\n" ] } ], "source": [ "from numpy import array\n", "print 'Matrix represented by given linear functionals on R**4:'\n", "A = array([[1, 2 ,2 ,1],[0, 2, 0, 1],[-2 ,0 ,-4, 3]])\n", "print 'A = \\n',A\n", "T = A #Temporary matrix to store A\n", "print 'To find Row reduced echelon matrix of A given by R:'\n", "print 'Applying row transformations on A,we get'\n", "print 'R1 = R1-R2'\n", "A[0,:] = A[0,:] - A[1,:]\n", "print 'A = \\n',A\n", "print 'R3 = R3 + 2*R1'\n", "A[2,:] = A[2,:] + 2*A[0,:]\n", "print 'A = \\n',A\n", "print 'R3 = R3/3'\n", "A[2,:] = 1./3*A[2,:]#\n", "print 'A = \\n',A\n", "print 'R2 = R2/2'\n", "A[1,:] = 1./2*A[1,:]\n", "print 'A = \\n',A\n", "print 'R2 = R2 - 1/2*R3'\n", "A[1,:] = A[1,:] - 1./2*A[2,:]\n", "print 'A = \\n',A\n", "R = A#\n", "A = T#\n", "print 'Row reduced echelon matrix of A is:'\n", "print 'R = \\n',R\n", "print 'Therefore,linear functionals g1,g2,g3 span the same subspace of (R**4)* as f1,f2,f3 are given by:'\n", "print 'g1(x1,x2,x3,x4) = x1 + 2*x3'\n", "print 'g1(x1,x2,x3,x4) = x2'\n", "print 'g1(x1,x2,x3,x4) = x4'\n", "print 'The subspace consists of the vectors with'\n", "print 'x1 = -2*x3'\n", "print 'x2 = x4 = 0'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Page 104 Example 3.24" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "W be the subspace of R**5 spanned by vectors:\n", "a1 = [2, -2, 3, 4, -1]\n", "a2 = [-1, 1, 2, 5, 2]\n", "a3 = [0, 0, -1, -2, 3]\n", "a4 = [1, -1, 2, 3, 0]\n", "Matrix A by the row vectors a1,a2,a3,a4 will be:\n", "A = \n", "[[ 2 -2 3 4 -1]\n", " [-1 1 2 5 2]\n", " [ 0 0 -1 -2 3]\n", " [ 1 -1 2 3 0]]\n", "After Applying row transformations, we get the row reduced echelon matrix R of A\n", "R = \n", "[[ 1 -1 0 -1 0]\n", " [ 0 0 1 2 0]\n", " [ 0 0 0 0 1]\n", " [ 0 0 0 0 0]]\n", "Then we obtain all the linear functionals f by assigning arbitrary values to c2 and c4\n", "Let c2 = a, c4 = b then c1 = a+b, c3 = -2b, c5 = 0.\n", "So, W0 consists all linear functionals f of the form\n", "f(x1,x2,x3,x4,x5) = (a+b)x1 + ax2 -2bx3 + bx4\n", "Dimension of W0 = 2 and basis {f1,f2} can be found by first taking a = 1, b = 0. Then a = 0,b = 1\n" ] } ], "source": [ "from numpy import array\n", "print 'W be the subspace of R**5 spanned by vectors:'\n", "a1 = [2, -2, 3 ,4 ,-1]#\n", "a2 = [-1, 1, 2, 5, 2]#\n", "a3 = [0 ,0 ,-1, -2, 3]#\n", "a4 = [1 ,-1, 2, 3 ,0]#\n", "print 'a1 = ',a1\n", "print 'a2 = ',a2\n", "print 'a3 = ',a3\n", "print 'a4 = ',a4\n", "print 'Matrix A by the row vectors a1,a2,a3,a4 will be:'\n", "A = array([a1,a2,a3,a4])\n", "print 'A = \\n',A\n", "print 'After Applying row transformations, we get the row reduced echelon matrix R of A'\n", "\n", "T = A# #Temporary matrix to store A\n", "#R1 = R1 - R4 and R2 = R2 + R4\n", "A[0,:] = A[0,:] - A[3,:]#\n", "A[1,:] = A[1,:] + A[3,:]#\n", "#R2 = R2/2\n", "A[1,:] = 1./2 * A[1,:]#\n", "#R3 = R3 + R2 and R4 = R4 - R1\n", "A[2,:] = A[2,:] + A[1,:]#\n", "A[3,:] = A[3,:] - A[0,:]#\n", "#R3 = R3 - R4\n", "A[2,:] = A[2,:] - A[3,:]#\n", "#R3 = R3/3\n", "A[2,:] = 1./3 * A[2,:]#\n", "#R2 = R2 - R3\n", "A[1,:] = A[1,:] - A[2,:]#\n", "#R2 = R2/2 and R4 = R4 - R2 - R3\n", "A[1,:] = 1./2 * A[1,:]#\n", "A[3,:] = A[3,:] - A[1,:] - A[2,:]#\n", "#R1 = R1 - R2 + R3\n", "A[0,:] = A[0,:] - A[1,:] + A[2,:]#\n", "R = A#\n", "A = T#\n", "print 'R = \\n',R\n", "print 'Then we obtain all the linear functionals f by assigning arbitrary values to c2 and c4'\n", "print 'Let c2 = a, c4 = b then c1 = a+b, c3 = -2b, c5 = 0.'\n", "print 'So, W0 consists all linear functionals f of the form'\n", "print 'f(x1,x2,x3,x4,x5) = (a+b)x1 + ax2 -2bx3 + bx4'\n", "print 'Dimension of W0 = 2 and basis {f1,f2} can be found by first taking a = 1, b = 0. Then a = 0,b = 1'" ] } ], "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 }