{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 5 Eigenvalues and Eigenvectors"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:5.1.1 Pg: 238"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Eigen values: [ 3.  2.]\n",
      "Eigen vectors:\n",
      "[[ 1.  0.]] \n",
      " and\n",
      "[[ 0.  1.]]\n"
     ]
    }
   ],
   "source": [
    "from numpy import mat\n",
    "from numpy.linalg import eig\n",
    "A=mat([[3, 0],[0, 2]])\n",
    "Eig,V=eig(A)\n",
    "print 'Eigen values:',Eig\n",
    "print 'Eigen vectors:\\n',V[0],'\\n and\\n',V[1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:5.1.2 Pg: 238"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The eigen values of a projection matrix are 1 or 0.\n",
      "Eigen values: ['1', '0']\n",
      "Eigen vectors:\n",
      "[[ 0.70710678 -0.70710678]] \n",
      " and\n",
      "[[ 0.70710678  0.70710678]]\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from numpy import mat\n",
    "from numpy.linalg import eig\n",
    "print 'The eigen values of a projection matrix are 1 or 0.'\n",
    "P=mat([[1/2, 1/2],[1/2, 1/2]])\n",
    "Eig,V=eig(P)\n",
    "Eig=[\"%.f\"%xx for xx in Eig]\n",
    "print 'Eigen values:',Eig\n",
    "print 'Eigen vectors:\\n',V[0],'\\n and\\n',V[1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:5.2.1 Pg: 238"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Eigenvalue matrix: [ 3.  2.]\n",
      "S=\n",
      "[[ 1.  0.]\n",
      " [ 0.  1.]]\n",
      "AS=S*eigenvaluematrix\n",
      "[[ 3.  0.]\n",
      " [ 0.  2.]]\n",
      "Therefore inv(S)*A*S=eigenvalue matrix\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from numpy import mat\n",
    "from numpy.linalg import eig\n",
    "P=mat([[1/2, 1/2],[1/2, 1/2]])\n",
    "Val,V=eig(A)\n",
    "print 'Eigenvalue matrix:',Val\n",
    "print 'S=\\n',V\n",
    "print 'AS=S*eigenvaluematrix\\n',(A*V)\n",
    "print 'Therefore inv(S)*A*S=eigenvalue matrix'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:5.2.2 Pg: 238"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The eigenvalues themselves are not so clear for a rotation.\n",
      "90 degree rotation\n",
      "K= [[ 0 -1]\n",
      " [ 1  0]]\n",
      "Eigen values: [ 0.+1.j  0.-1.j]\n",
      "Eigen vectors:\n",
      "[[ 0.70710678+0.j  0.70710678-0.j]] \n",
      " and\n",
      "[[ 0.-0.70710678j  0.+0.70710678j]]\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from numpy import mat\n",
    "from numpy.linalg import eig\n",
    "print 'The eigenvalues themselves are not so clear for a rotation.'\n",
    "print '90 degree rotation'\n",
    "K=mat([[0, -1],[1, 0]])\n",
    "print 'K=',K\n",
    "Val,V=eig(K)\n",
    "print 'Eigen values:',Val\n",
    "print 'Eigen vectors:\\n',V[0],'\\n and\\n',V[1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:5.2.3 Pg: 249"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "K is rotation through 90 degree,then K**2 is rotation through 180 degree and inv(k is rotation through -90 degree)\n",
      "K=\n",
      "[[ 0 -1]\n",
      " [ 1  0]]\n",
      "K**2=\n",
      "[[-1  0]\n",
      " [ 0 -1]]\n",
      "K**3=\n",
      "[[ 0  1]\n",
      " [-1  0]]\n",
      "K**4=\n",
      "[[1 0]\n",
      " [0 1]]\n",
      "K**4 is a complete rotation through 360 degree.\n",
      "Eigen value matrix,D of K:\n",
      "[ 0.+1.j  0.-1.j]\n",
      "and also D**4=\n",
      "[ 1.+0.j  1.+0.j]\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from numpy import mat\n",
    "from numpy.linalg import eig\n",
    "print 'K is rotation through 90 degree,then K**2 is rotation through 180 degree and inv(k is rotation through -90 degree)'\n",
    "K=mat([[0, -1],[1, 0]])\n",
    "print 'K=\\n',K\n",
    "print 'K**2=\\n',(K*K)\n",
    "print 'K**3=\\n',(K*K*K)\n",
    "print 'K**4=\\n',(K**4)\n",
    "D,V=eig(K)\n",
    "print 'K**4 is a complete rotation through 360 degree.'\n",
    "print 'Eigen value matrix,D of K:\\n',D\n",
    "print 'and also D**4=\\n',(D**4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:5.3.1 Pg: 249"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A=\n",
      "[[ 0.   4. ]\n",
      " [ 0.   0.5]]\n",
      "Eigen values: [ 0.   0.5]\n",
      "[[ 1.]\n",
      " [ 0.]]\n",
      "k= 0\n",
      "U(k+1)(K from 0 to 5)\n",
      "[[ 0.]\n",
      " [ 0.]]\n",
      "k= 1\n",
      "U(k+1)(K from 0 to 5)\n",
      "[[ 0.]\n",
      " [ 0.]]\n",
      "k= 2\n",
      "U(k+1)(K from 0 to 5)\n",
      "[[ 0.]\n",
      " [ 0.]]\n",
      "k= 3\n",
      "U(k+1)(K from 0 to 5)\n",
      "[[ 0.]\n",
      " [ 0.]]\n",
      "k= 4\n",
      "U(k+1)(K from 0 to 5)\n",
      "[[ 0.]\n",
      " [ 0.]]\n",
      "k= 5\n",
      "U(k+1)(K from 0 to 5)\n",
      "[[ 0.]\n",
      " [ 0.]]\n",
      "k= 5\n",
      "U(k+1)=\n",
      "[[ 0.49613894]\n",
      " [ 0.06201737]]\n",
      "k= 5\n",
      "U(k+1)=\n",
      "[[ 0.24806947]\n",
      " [ 0.03100868]]\n",
      "k= 5\n",
      "U(k+1)=\n",
      "[[ 0.12403473]\n",
      " [ 0.01550434]]\n",
      "k= 5\n",
      "U(k+1)=\n",
      "[[ 0.06201737]\n",
      " [ 0.00775217]]\n",
      "k= 5\n",
      "U(k+1)=\n",
      "[[ 0.03100868]\n",
      " [ 0.00387609]]\n",
      "k= 5\n",
      "U(k+1)=\n",
      "[[ 0.01550434]\n",
      " [ 0.00193804]]\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from numpy import mat\n",
    "from numpy.linalg import eig\n",
    "A=mat([[0, 4],[0, 1/2]])\n",
    "print 'A=\\n',A\n",
    "Eig,zz=eig(A)\n",
    "print 'Eigen values:',Eig\n",
    "D,v=eig(A)\n",
    "u0=v[:,0] #Taking u0 as the 1st eigen Vector.\n",
    "print u0\n",
    "for k in range(0,6):\n",
    "    print 'k=',k\n",
    "    u=A*u0\n",
    "    print 'U(k+1)(K from 0 to 5)\\n',u\n",
    "    u0=u\n",
    "\n",
    "u0=v[:,1] ##Taking u0 as the 2nd eigen vector.\n",
    "for ki in range(0,6):\n",
    "    print 'k=',k\n",
    "    u=A*u0\n",
    "    print 'U(k+1)=\\n',u\n",
    "    u0=u"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:5.5.1 Pg:282"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x= (3+4j)\n",
      "xx_= (25+0j)\n",
      "r= (5+0j)\n"
     ]
    }
   ],
   "source": [
    "from numpy import sqrt\n",
    "x=3+4*1J\n",
    "print 'x=',x\n",
    "x_=x.conjugate()\n",
    "print 'xx_=',x*x_\n",
    "r=sqrt(x*x_)\n",
    "print 'r=',r"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:5.5.2 Pg:282"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Length of x squared: 2.0\n",
      "Length of y squared: 25.0\n"
     ]
    }
   ],
   "source": [
    "from numpy import mat\n",
    "i=1J\n",
    "x=mat([[1, i]]).H\n",
    "y=mat([[2+1*i, 2-4*i]]).H\n",
    "print 'Length of x squared:',abs(x.H*x)[0,0]\n",
    "print 'Length of y squared:',abs( y.H*y)[0,0]"
   ]
  }
 ],
 "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
}