{
 "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": [
    "import numpy as np\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=np.array([a1,a2]).dot(np.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 =  5\n",
      "x2 =  2\n",
      "T(5,2) =  [7, 5]\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 =  8\n",
      "So, x1 =  8\n",
      "x2 =  -8\n",
      "Hence, T is onto.\n",
      "inverse(T) =  [8, -8]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "#x = round(rand(1,2) * 10)#\n",
    "x1 = np.random.randint(1,9)\n",
    "x2 = np.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 = np.random.randint(0,9)\n",
    "z2 = np.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": 5,
   "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": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "print 'Differentiation operator D is defined as:'\n",
    "D = np.zeros([4,4])\n",
    "x=sp.Symbol('x')\n",
    "for i in range(1,5):\n",
    "    t= i-1#\n",
    "    f = sp.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": 6,
   "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": [
    "import numpy as np\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 = np.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 = np.array([1, 1])\n",
    "E2 = np.array([2 ,1])\n",
    "print 'E1 = ',E1\n",
    "print 'E2 = ',E2\n",
    "P = np.transpose(([E1,E2]))\n",
    "print 'So, matrix P = \\n',P\n",
    "Pinv=np.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": 8,
   "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": [
    "import sympy as sp\n",
    "t = sp.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 = sp.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',sp.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 = sp.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 = ',sp.Matrix.inv(P)*D*P\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page 98 Example 3.19"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "n =  8\n",
      "A = \n",
      "[[  7.   3.   0.   4.   6.   8.   4.   4.]\n",
      " [  6.   4.   2.   8.   7.   8.   1.   7.]\n",
      " [  7.   0.   9.   3.  10.   9.   3.   8.]\n",
      " [  7.   5.  10.   1.   8.   6.   6.   5.]\n",
      " [  8.   8.   9.   9.   1.   9.  10.   4.]\n",
      " [  6.   3.   5.   2.   2.   4.   8.   4.]\n",
      " [  5.   1.   1.   2.   6.   9.   9.   5.]\n",
      " [  8.   6.   9.   9.   8.   9.   1.   2.]]\n",
      "Trace of A:\n",
      "tr(A) =  37.0\n",
      "--------------------------------\n",
      "c =  3\n",
      "B = \n",
      "[[  4.   6.  10.   5.   8.   4.   1.   9.]\n",
      " [  9.   9.   3.   6.   3.   8.   2.   6.]\n",
      " [  1.   6.   0.   7.   7.   2.   8.   4.]\n",
      " [  5.   5.   9.   7.   9.   3.   9.   9.]\n",
      " [  7.   7.  10.   6.   1.   1.   7.   4.]\n",
      " [  0.   3.  10.   9.   5.   2.   8.   4.]\n",
      " [  1.   8.   2.   4.   5.   4.   4.   8.]\n",
      " [  7.   0.   1.   8.   2.   7.   4.   7.]]\n",
      "Trace of B:\n",
      "tr(B) =  34.0\n",
      "tr(cA + B) =  145.0\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\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=np.random.randint(1,9)\n",
    "print 'n = ',n\n",
    "#A = round(rand(n,n) * 10)#\n",
    "A=np.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=np.random.randint(2,9)\n",
    "print 'c = ',c\n",
    "\n",
    "B=np.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": 10,
   "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": [
    "import numpy as np\n",
    "print 'Matrix represented by given linear functionals on R**4:'\n",
    "A = np.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": 11,
   "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": [
    "import numpy as np\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 = np.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
}