{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 6 - Elementary canonical forms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page 184 Example 6.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Standard ordered matrix for Linear operator T on R**2 is:\n",
      "A = \n",
      "Matrix([[0, -1], [1, 0]])\n",
      "The characteristic polynomial for T or A is: Matrix([[x, 1], [-1, x]])\n",
      "Since this polynomial has no real roots,T has no characteristic values.\n"
     ]
    }
   ],
   "source": [
    "import sympy as sp\n",
    "print 'Standard ordered matrix for Linear operator T on R**2 is:'\n",
    "A = sp.Matrix(([0, -1],[1 ,0]))\n",
    "print 'A = \\n',A\n",
    "print 'The characteristic polynomial for T or A is:',\n",
    "x = sp.Symbol(\"x\")\n",
    "p = (x*sp.eye(2)-A)\n",
    "print p\n",
    "print 'Since this polynomial has no real roots,T has no characteristic values.'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page 184 Example 6.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A = \n",
      "Matrix([[3, 1, -1], [2, 2, -1], [2, 2, 0]])\n",
      "Characteristic polynomial for A is: x**3 - 5*x**2 + 8*x - 4\n",
      "or\n",
      "(x-1)(x-2)**2\n",
      "The characteristic values of A are:\n",
      "[1, 2]\n",
      "Now, A-I = \n",
      "Matrix([[2, 1, -1], [2, 1, -1], [2, 2, -1]])\n",
      "rank of A - I=  2\n",
      "So, nullity of T-I = 1\n",
      "The vector that spans the null space of T-I =  [1, 0, 2]\n",
      "Now,A-2I = \n",
      "Matrix([[1, 1, -1], [2, 0, -1], [2, 2, -2]])\n",
      "rank of A - 2I=  2\n",
      "T*alpha = 2*alpha if alpha is a scalar multiple of a2\n",
      "a2 =  [1, 1, 2]\n"
     ]
    }
   ],
   "source": [
    "import sympy as sp\n",
    "A = sp.Matrix(([3, 1, -1],[ 2, 2, -1],[2, 2, 0]))\n",
    "print 'A = \\n',A\n",
    "print 'Characteristic polynomial for A is:',\n",
    "x=sp.Symbol('x')\n",
    "p = A.charpoly(x)#\n",
    "print p.as_expr()\n",
    "print 'or'\n",
    "print '(x-1)(x-2)**2'\n",
    "\n",
    "r = sp.solve(p.as_expr())#\n",
    "[m,n] = A.shape\n",
    "print 'The characteristic values of A are:'\n",
    "print r  #print round(r)\n",
    "B = A-sp.eye(m)\n",
    "print 'Now, A-I = \\n',B\n",
    "\n",
    "print 'rank of A - I= ',B.rank()\n",
    "print 'So, nullity of T-I = 1'\n",
    "a1 = [1 ,0 ,2]#\n",
    "print 'The vector that spans the null space of T-I = ',a1\n",
    "B = A-2*sp.eye(m)\n",
    "print 'Now,A-2I = \\n',B\n",
    "print 'rank of A - 2I= ',B.rank()\n",
    "print 'T*alpha = 2*alpha if alpha is a scalar multiple of a2'\n",
    "a2 = [1 ,1 ,2]\n",
    "print 'a2 = ',a2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page 187 Example 6.3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Standard ordered matrix for Linear operator T on R**3 is:\n",
      "A = \n",
      "Matrix([[5, -6, -6], [-1, 4, 2], [3, -6, -4]])\n",
      "xI - A = \n",
      "Matrix([[x - 5, 6, 6], [1, x - 4, -2], [-3, 6, x + 4]])\n",
      "Applying row and column transformations:\n",
      "C2 = C2 - C3\n",
      "=>\n",
      "Matrix([[x - 5, 0, 6], [1, x - 2, -2], [-3, -x + 2, x + 4]])\n",
      "Taking (x-2) common from C2\n",
      "=>\n",
      " *  x - 2\n",
      "Matrix([[x - 5, 0, 6], [1, 1, -2], [-3, (-x + 2)/(x - 2), x + 4]])\n",
      "R3 = R3 + R2\n",
      "=>\n",
      " *  x - 2\n",
      "Matrix([[x - 5, 0, 6], [1, 1, -2], [-2, (-x + 2)/(x - 2) + 1, x + 2]])\n",
      "=>\n",
      " *  x - 2\n",
      "Matrix([[x - 5, 6], [-2, x + 2]])\n",
      "=>\n",
      " *  x - 2\n",
      "x**2 - 3*x + 2\n",
      "This is the characteristic polynomial\n",
      "Now, A - I =  Matrix([[4, -6, -6], [-1, 3, 2], [3, -6, -5]])\n",
      "And, A- 2I =  Matrix([[3, -6, -6], [-1, 2, 2], [3, -6, -6]])\n",
      "rank(A-I) =  2\n",
      "rank(A-2I) =  2\n",
      "W1,W2 be the spaces of characteristic vectors associated with values 1,2\n",
      "So by theorem 2, T is diagonalizable\n",
      "Null space of (T- I) i.e basis of W1 is spanned by a1 =  [[ 3 -1  3]]\n",
      "Null space of (T- 2I) i.e. basis of W2 is spanned by vectors x1,x2,x3 such that x1 = 2x1 + 2x3\n",
      "One example :\n",
      "a2 =  [[2 1 0]]\n",
      "a3 =  [[2 0 1]]\n",
      "The diagonal matrix is:\n",
      "D =  [[1 0 0]\n",
      " [0 2 0]\n",
      " [0 0 2]]\n",
      "The standard basis matrix is denoted as:\n",
      "P =  [[ 3  2  2]\n",
      " [-1  1  0]\n",
      " [ 3  0  1]]\n",
      "AP =  Matrix([[3, 4, 4], [-1, 2, 0], [3, 0, 2]])\n",
      "PD =  [[3 0 0]\n",
      " [0 2 0]\n",
      " [0 0 2]]\n",
      "That is, AP = PD\n",
      "=>  inverse(P)*A*P = D\n"
     ]
    }
   ],
   "source": [
    "import sympy as sp\n",
    "import numpy as np\n",
    "print 'Standard ordered matrix for Linear operator T on R**3 is:'\n",
    "A = sp.Matrix(([5, -6, -6],[ -1, 4, 2],[ 3, -6, -4]))\n",
    "print 'A = \\n',A\n",
    "print 'xI - A = '\n",
    "B = sp.eye(3)\n",
    "x = sp.Symbol('x')\n",
    "P = x*B - A#\n",
    "print P\n",
    "\n",
    "print 'Applying row and column transformations:'\n",
    "print 'C2 = C2 - C3'\n",
    "P[:,1] = P[:,1] - P[:,2]\n",
    "print '=>'\n",
    "print P\n",
    "print 'Taking (x-2) common from C2'\n",
    "c = x-2#\n",
    "P[:,1] = P[:,1] / (x-2)\n",
    "print '=>'\n",
    "print ' * ', c\n",
    "print P\n",
    "print 'R3 = R3 + R2'\n",
    "P[2,:] = P[2,:] + P[1,:]\n",
    "print '=>'\n",
    "print ' * ', c\n",
    "print P\n",
    "P = sp.Matrix(([P[0,0], P[0,2]],[P[2,0], P[2,2]]))\n",
    "print '=>'\n",
    "print ' * ', c\n",
    "print P\n",
    "print '=>'\n",
    "print ' * ',c\n",
    "print P.det()\n",
    "print 'This is the characteristic polynomial'\n",
    "\n",
    "print 'Now, A - I = ',A-B\n",
    "print 'And, A- 2I = ',A-2*B\n",
    "print 'rank(A-I) = ',np.rank(A-B)\n",
    "\n",
    "print 'rank(A-2I) = ',np.rank(A-2*B)\n",
    "print 'W1,W2 be the spaces of characteristic vectors associated with values 1,2'\n",
    "print 'So by theorem 2, T is diagonalizable'\n",
    "a1 = np.array([[3, -1 ,3]])\n",
    "a2 = np.array([[2, 1, 0]])\n",
    "a3 = np.array([[2, 0, 1]])\n",
    "print 'Null space of (T- I) i.e basis of W1 is spanned by a1 = ',a1\n",
    "print 'Null space of (T- 2I) i.e. basis of W2 is spanned by vectors x1,x2,x3 such that x1 = 2x1 + 2x3'\n",
    "print 'One example :'\n",
    "print 'a2 = ',a2\n",
    "print 'a3 = ',a3\n",
    "print 'The diagonal matrix is:'\n",
    "D = np.array([[1 ,0 ,0 ],[0, 2, 0],[0, 0, 2]])\n",
    "print 'D = ',D\n",
    "print 'The standard basis matrix is denoted as:'\n",
    "P = np.transpose(np.vstack([a1,a2,a3]))\n",
    "print 'P = ',P\n",
    "print 'AP = ',A*P\n",
    "print 'PD = ',P*D\n",
    "print 'That is, AP = PD'\n",
    "print '=>  inverse(P)*A*P = D'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page 193 Example 6.4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A = \n",
      "[[ 5 -6 -6]\n",
      " [-1  4  2]\n",
      " [ 3 -6 -4]]\n",
      "Characteristic polynomial of A is:\n",
      "f = (x-1)(x-2)**2\n",
      "i.e., f =  (x - 2)**2*(x - 1)\n",
      "(A-I)(A-2I) =  Matrix([[0, 0, 0], [0, 0, 0], [0, 0, 0]])\n",
      "Since, (A-I)(A-2I) = 0. So, Minimal polynomial for above is:\n",
      "p =  (x - 2)*(x - 1)\n",
      "---------------------------------------\n",
      "A = \n",
      "[[ 3  1 -1]\n",
      " [ 2  2 -1]\n",
      " [ 2  2  0]]\n",
      "Characteristic polynomial of A is:\n",
      "f = (x-1)(x-2)**2\n",
      "i.e., f =  (x - 2)**2*(x - 1)\n",
      "(A-I)(A-2I) =  Matrix([[2, 0, -1], [2, 0, -1], [4, 0, -2]])\n",
      "Since, (A-I)(A-2I) is not equal to 0. T is not diagonalizable. So, Minimal polynomial cannot be p.\n",
      "---------------------------------------\n",
      "A = \n",
      "[[ 0 -1]\n",
      " [ 1  0]]\n",
      "Characteristic polynomial of A is:\n",
      "f =  x**2 + 1\n",
      "A**2 + I =  Matrix([[1, 1], [1, 1]])\n",
      "Since, A**2 + I = 0, so minimal polynomial is\n",
      "p =  x**2 + 1\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "\n",
    "x = sp.Symbol(\"x\")\n",
    "A = np.array([[5, -6, -6],[ -1, 4 ,2],[ 3, -6, -4]])   #Matrix given in Example 3\n",
    "print 'A = \\n',A\n",
    "f = (x-1)*(x-2)**2# \n",
    "print 'Characteristic polynomial of A is:'\n",
    "print 'f = (x-1)(x-2)**2'\n",
    "print 'i.e., f = ',f\n",
    "p = (x-1)*(x-2)#\n",
    "print '(A-I)(A-2I) = ',(A-sp.eye(3))*(A-2 * sp.eye(3))\n",
    "print 'Since, (A-I)(A-2I) = 0. So, Minimal polynomial for above is:'\n",
    "print 'p = ',p\n",
    "print '---------------------------------------'\n",
    "\n",
    "A = np.array([[3, 1 ,-1],[ 2, 2 ,-1],[2, 2, 0]])    #Matrix given in Example 2\n",
    "print 'A = \\n',A\n",
    "f = (x-1)*(x-2)**2# \n",
    "print 'Characteristic polynomial of A is:'\n",
    "print 'f = (x-1)(x-2)**2'\n",
    "print 'i.e., f = ',f\n",
    "print '(A-I)(A-2I) = ',(A-sp.eye(3))*(A-2 * sp.eye(3))\n",
    "print 'Since, (A-I)(A-2I) is not equal to 0. T is not diagonalizable. So, Minimal polynomial cannot be p.'\n",
    "print '---------------------------------------'\n",
    "A = np.array([[0, -1],[1, 0]])\n",
    "print 'A = \\n',A\n",
    "f = x**2 + 1#\n",
    "print 'Characteristic polynomial of A is:'\n",
    "print 'f = ',f\n",
    "print 'A**2 + I = ',A**2 + sp.eye(2)\n",
    "print 'Since, A**2 + I = 0, so minimal polynomial is'\n",
    "p = x**2 + 1\n",
    "print 'p = ',p"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page 197 Example 6.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A = \n",
      "[[0 1 0 1]\n",
      " [1 0 1 0]\n",
      " [0 1 0 1]\n",
      " [1 0 1 0]]\n",
      "Computing powers on A:\n",
      "A**2 = \n",
      "[[0 1 0 1]\n",
      " [1 0 1 0]\n",
      " [0 1 0 1]\n",
      " [1 0 1 0]]\n",
      "A**3 = \n",
      "[[0 1 0 1]\n",
      " [1 0 1 0]\n",
      " [0 1 0 1]\n",
      " [1 0 1 0]]\n",
      "if p = x**3 - 4x, then\n",
      "p(A) =  [[ 0 -3  0 -3]\n",
      " [-3  0 -3  0]\n",
      " [ 0 -3  0 -3]\n",
      " [-3  0 -3  0]]\n",
      "Minimal polynomial for A is:  x**3 - 4*x\n",
      "Characteristic values for A are: [-2, 0, 2]\n",
      "Rank(A) =  2\n",
      "So, from theorem 2, characteristic polynomial for A is: x**4 - 4*x**2\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([[0, 1, 0, 1],[1, 0 ,1 ,0],[0, 1, 0, 1],[1, 0, 1, 0]])\n",
    "print 'A = \\n',A\n",
    "print 'Computing powers on A:'\n",
    "print 'A**2 = \\n',A*A\n",
    "print 'A**3 = \\n',A*A*A\n",
    "def p(x):\n",
    "    pp = x**3 - 4*x\n",
    "    return pp\n",
    "print 'if p = x**3 - 4x, then'\n",
    "print 'p(A) = ',p(A)\n",
    "x = sp.Symbol(\"x\")\n",
    "f = x**3 - 4*x\n",
    "print 'Minimal polynomial for A is: ',f\n",
    "print 'Characteristic values for A are:',sp.solve(f,x)\n",
    "print 'Rank(A) = ',np.rank(A)\n",
    "print 'So, from theorem 2, characteristic polynomial for A is:',sp.Matrix(A).charpoly(x).as_expr()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page 210 Example 6.12"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A = \n",
      "[[  9.   1.   3.]\n",
      " [ 10.   1.   3.]\n",
      " [ 10.   5.   1.]]\n",
      "A transpose is:\n",
      "A' = \n",
      "[[  9.  10.  10.]\n",
      " [  1.   1.   5.]\n",
      " [  3.   3.   1.]]\n",
      "Since, A' is not equal to A, A is not a symmetric matrix.\n",
      "Since, A' is not equal to -A, A is not a skew-symmetric matrix.\n",
      "A can be expressed as sum of A1 and A2\n",
      "i.e., A = A1 + A2\n",
      "A1 = \n",
      "[[ 9.   5.5  6.5]\n",
      " [ 5.5  1.   4. ]\n",
      " [ 6.5  4.   1. ]]\n",
      "A2 = \n",
      "[[ 0.  -4.5 -3.5]\n",
      " [ 4.5  0.  -1. ]\n",
      " [ 3.5  1.   0. ]]\n",
      "A1 + A2 = \n",
      "[[  9.   1.   3.]\n",
      " [ 10.   1.   3.]\n",
      " [ 10.   5.   1.]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "A = np.random.rand(3,3)\n",
    "for i in range(0,3):\n",
    "    for j in range(0,3):\n",
    "        A[i,j]=round(A[i,j]*10)\n",
    "    \n",
    "print 'A = \\n',A\n",
    "print 'A transpose is:\\n',\n",
    "Adash=np.transpose(A)\n",
    "print \"A' = \\n\",Adash\n",
    "if np.equal(Adash,A).all():\n",
    "    print \"Since, A' = A, A is a symmetric matrix.\"\n",
    "else:\n",
    "    print \"Since, A' is not equal to A, A is not a symmetric matrix.\"\n",
    "\n",
    "if np.equal(Adash,-A).all():\n",
    "    print \"Since, A' = -A, A is a skew-symmetric matrix.\"\n",
    "else:\n",
    "    print \"Since, A' is not equal to -A, A is not a skew-symmetric matrix.\"\n",
    "\n",
    "A1 = 1./2*(A + Adash)\n",
    "A2 = 1./2*(A - Adash)\n",
    "print 'A can be expressed as sum of A1 and A2'\n",
    "print 'i.e., A = A1 + A2'\n",
    "print 'A1 = \\n',A1\n",
    "print 'A2 = \\n',A2\n",
    "print 'A1 + A2 = \\n',A1 + A2"
   ]
  }
 ],
 "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
}