{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 2: Determinants and Matrices"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.1, page no. 55"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Determinant of A is:  a*b*c - a*f**2 - b*g**2 - c*h**2 + 2*f*g*h\n"
     ]
    }
   ],
   "source": [
    "import sympy\n",
    "\n",
    "a = sympy.Symbol('a')\n",
    "h = sympy.Symbol('h')\n",
    "g = sympy.Symbol('g')\n",
    "b = sympy.Symbol('b')\n",
    "f = sympy.Symbol('f')\n",
    "c = sympy.Symbol('c')\n",
    "A = sympy.Matrix([[a,h,g],[h,b,f],[g,f,c]])\n",
    "\n",
    "print \"Determinant of A is: \", A.det()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.2, page no. 55"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Determinanat of a is: 88.0\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "A = numpy.array([[0,1,2,3],[1,0,3,0],[2,3,0,1],[3,0,1,2]])\n",
    "print \"Determinant of a is:\",numpy.linalg.det(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.3, page no. 56"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-a**3*b**2*c + a**3*b*c**2 + a**2*b**3*c - a**2*b*c**3 + a**2*b - a**2*c - a*b**3*c**2 + a*b**2*c**3 - a*b**2 + a*c**2 + b**2*c - b*c**2\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "import sympy\n",
    "\n",
    "a = sympy.Symbol('a');\n",
    "b = sympy.Symbol('b');\n",
    "c = sympy.Symbol('c');\n",
    "A = sympy.Matrix([[a,a**2,a**3-1],[b,b**2,b**3-1],[c,c**2,c**3-1]])\n",
    "\n",
    "print A.det()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.4, page no. 57"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Determinanat of a is: -24.0\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "A = numpy.array([[21,17,7,10],[24,22,6,10],[6,8,2,3],[6,7,1,2]])\n",
    "print \"Determinanat of a is:\",numpy.linalg.det(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "imp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:  x**y*log(x)\n",
      "b:  x**y*y*log(x)/x + x**y/x\n",
      "c:  x**y*y**2*log(x)/x**2 - x**y*y*log(x)/x**2 + 2*x**y*y/x**2 - x**y/x**2\n",
      "d:  x**y*y/x\n",
      "e:  x**y*y*log(x)/x + x**y/x\n",
      "f:  x**y*y**2*log(x)/x**2 - x**y*y*log(x)/x**2 + 2*x**y*y/x**2 - x**y/x**2\n",
      "Clearly c = f\n"
     ]
    }
   ],
   "source": [
    "import sympy\n",
    "\n",
    "x = sympy.Symbol('x');\n",
    "y = sympy.Symbol('y')\n",
    "u = x**y\n",
    "a = sympy.diff(u, y)\n",
    "b = sympy.diff(a, x)\n",
    "c = sympy.diff(b, x)\n",
    "d = sympy.diff(u, x)\n",
    "e = sympy.diff(d, y)\n",
    "f = sympy.diff(e, x)\n",
    "print \"a: \", a\n",
    "print \"b: \", b\n",
    "print \"c: \", c\n",
    "print \"d: \", d\n",
    "print \"e: \", e\n",
    "print \"f: \", f\n",
    "print \"Clearly c = f\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.17, page no. 65"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A*B=\n",
      "[[ 5  9 13]\n",
      " [-1  2  4]\n",
      " [-2  2  4]]\n",
      "\n",
      "B*A=\n",
      "[[-1 12 11]\n",
      " [-1  7  8]\n",
      " [-2 -1  5]]\n",
      "Clearly AB is not equal to BA\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "A = numpy.array([[1,3,0],[-1,2,1],[0,0,2]])\n",
    "B = numpy.array([[2,3,4],[1,2,3],[-1,1,2]])\n",
    "ma = numpy.matrix(A)\n",
    "mb = numpy.matrix(B)\n",
    "print \"A*B=\"\n",
    "print ma*mb\n",
    "print \"\"\n",
    "print \"B*A=\"\n",
    "print mb*ma\n",
    "print \"Clearly AB is not equal to BA\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.18, page no. 65"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AB=C−−>B=inv(A)∗C\n",
      "\n",
      "[[  1.00000000e+00  -1.77635684e-15  -8.88178420e-16]\n",
      " [ -2.22044605e-16   2.00000000e+00  -1.11022302e-16]\n",
      " [  1.77635684e-15   0.00000000e+00   1.00000000e+00]]\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "A = numpy.matrix([[3,2,2],[1,3,1],[5,3,4]])\n",
    "C = numpy.matrix([[3,4,2],[1,6,1],[5,6,4]])\n",
    "print \"AB=C−−>B=inv(A)∗C\"\n",
    "print \"\"\n",
    "B = numpy.linalg.inv(A)*C \n",
    "print B"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.19, page no. 66"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Aˆ3−4∗Aˆ2−3A+11*I=\n",
      "\n",
      "[[ 0.  0.  0.]\n",
      " [ 0.  0.  0.]\n",
      " [ 0.  0.  0.]]\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "A = numpy.array([[1,3,2],[2,0,-1],[1,2,3]])\n",
    "I = numpy.eye(3)\n",
    "A = numpy.matrix(A)\n",
    "print \"Aˆ3−4∗Aˆ2−3A+11*I=\"\n",
    "print \"\"\n",
    "print A**3-4*A**2-3*A+11*I"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.20, page no. 67"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter the value of n: 3\n",
      "Calculating A ^ n: \n",
      "[[ 31 -75]\n",
      " [ 12 -29]]\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "0\n",
    "A = numpy.matrix([[11,-25],[4, -9]])\n",
    "n = int(raw_input(\"Enter the value of n: \"))\n",
    "print \"Calculating A ^ n: \"\n",
    "print  A**n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.23, page no. 70"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Inverse of A is: \n",
      "[[ 3.    1.    1.5 ]\n",
      " [-1.25 -0.25 -0.75]\n",
      " [-0.25 -0.25 -0.25]]\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "A = numpy.matrix([[1,1,3],[1,3,-3],[-2,-4,-4]])\n",
    "print \"Inverse of A is: \"\n",
    "print numpy.linalg.inv(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.24.1, page no. 71"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rank of A is: 2\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "A = numpy.matrix([[1,2,3],[1,4,2],[2,6,5]])\n",
    "print \"Rank of A is:\",numpy.linalg.matrix_rank(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.24.2, page no. 71"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rank of A is: 2\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "A = numpy.matrix([[0,1,-3,-1],[1,0,1,1],[3,1,0,2],[1,1,-2,0]])\n",
    "print \"Rank of A is:\",numpy.linalg.matrix_rank(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.25, page no. 72"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Inverse of A is: \n",
      "[[ 3.    1.    1.5 ]\n",
      " [-1.25 -0.25 -0.75]\n",
      " [-0.25 -0.25 -0.25]]\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "A = numpy.matrix([[1,1,3],[1,3,-3],[-2,-4,-4]])\n",
    "print \"Inverse of A is: \"\n",
    "print numpy.linalg.inv(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.26, page no. 73"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rank of A is: 3\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "A = numpy.matrix([[2,3,-1,-1],[1,-1,-2,-4],[3,1,3,-2],[6,3,0,-7]])\n",
    "r,p = numpy.linalg.eigh ( A )\n",
    "print \"Rank of A is:\",numpy.linalg.matrix_rank(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.28, page no. 75"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Inverse of A is: \n",
      "[[ 1.4  0.2 -0.4]\n",
      " [-1.5  0.   0.5]\n",
      " [ 1.1 -0.2 -0.1]]\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "A = numpy.matrix([[1,1,1],[4,3,-1],[3,5,3]])\n",
    "print \"Inverse of A is: \"\n",
    "print numpy.linalg.inv(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.31, page no. 78"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " The equations can be rewritten as AX=B where X=[ x1 ; x2 ; x3 ; x4 ] and \n",
      "Determinant of A=\n",
      "8.0\n",
      "Inverse of A =\n",
      "[[ 0.5  0.5  0.5 -0.5]\n",
      " [-0.5  0.   0.   0.5]\n",
      " [ 0.  -0.5  0.   0.5]\n",
      " [ 0.   0.  -0.5  0.5]]\n",
      "X= [[ 1.]\n",
      " [-1.]\n",
      " [ 2.]\n",
      " [-2.]]\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "print  \"The equations can be rewritten as AX=B where X=[ x1 ; x2 ; x3 ; x4 ] and \"\n",
    "A = numpy.matrix([[1,-1,1,1],[1,1,-1,1],[1,1,1,-1],[1,1,1,1]])\n",
    "B = numpy.matrix([[2],[-4],[4],[0]])\n",
    "print \"Determinant of A=\"\n",
    "print numpy.linalg.det(A)\n",
    "print \"Inverse of A =\"\n",
    "print numpy.linalg.inv(A)\n",
    "print \"X=\",numpy.linalg.inv(A)*B"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.32, page no. 78"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The equations can be rewritten as AX=B where X=[x;y;z] and\n",
      "Determinant of A=\n",
      "-8.79296635503e-14\n",
      "Since det(A)=0 , hence, this system of equation will have infinite solutions.. hence, the system is consistent\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "print \"The equations can be rewritten as AX=B where X=[x;y;z] and\"\n",
    "A = numpy.matrix([[5,3,7],[3,26,2],[7,2,10]])\n",
    "B = numpy.matrix([[4],[9],[5]])\n",
    "print \"Determinant of A=\"\n",
    "print numpy.linalg.det(A)\n",
    "print \"Since det(A)=0 , hence, this system of equation will have infinite solutions.. hence, the system is consistent\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.34.1, page no. 80"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rank of A is 3\n",
      "Equations have only a trivial solution : x=y=z=0\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "A = numpy.matrix([[1,2,3],[3,4,4],[7,10,12]])\n",
    "p = numpy.linalg.matrix_rank(A)\n",
    "print \"Rank of A is\",p\n",
    "if p==3:\n",
    "    print \"Equations have only a trivial solution : x=y=z=0\"\n",
    "else:\n",
    "    print \"Equations have infinite no . of solutions.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Example 2.34.2, page no. 80"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rank of A is 2\n",
      "Equations have infinite no. of solutions.\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "A = numpy.matrix([[4,2,1,3],[6,3,4,7],[2,1,0,1]])\n",
    "p = numpy.linalg.matrix_rank(A)\n",
    "print \"Rank of A is\",p\n",
    "if p ==4:\n",
    "    print \"Equations have only a trivial solution : x=y=z=0\"\n",
    "else:\n",
    "    print \"Equations have infinite no. of solutions.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Example 2.38, page no. 83"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The given equations can be written as Y=AX where\n",
      "Determinant of A is -1.0\n",
      "Since, its non−singular, hence transformation is regular\n",
      "Inverse of A is\n",
      "[[ 2. -2. -1.]\n",
      " [-4.  5.  3.]\n",
      " [ 1. -1. -1.]]\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "print \"The given equations can be written as Y=AX where\"\n",
    "A = numpy.matrix([[2,1,1],[1,1,2],[1,0,-2]])\n",
    "print \"Determinant of A is\",numpy.linalg.det ( A )\n",
    "print \"Since, its non−singular, hence transformation is regular\"\n",
    "print\"Inverse of A is\"\n",
    "print numpy.linalg.inv ( A )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.39, page no. 84"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.66666667  0.33333333  0.66666667]\n",
      " [ 0.66666667  0.66666667  0.33333333]\n",
      " [ 0.33333333 -0.66666667  0.66666667]]\n",
      "A transpose is equal to\n",
      "[[-0.66666667  0.66666667  0.33333333]\n",
      " [ 0.33333333  0.66666667 -0.66666667]\n",
      " [ 0.66666667  0.33333333  0.66666667]]\n",
      "A∗(transpose of A)=\n",
      "[[ 1.  0.  0.]\n",
      " [ 0.  1.  0.]\n",
      " [ 0.  0.  1.]]\n",
      "Hence, A is orthogonal\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "A = numpy.matrix([[-2./3,1./3,2./3],[2./3,2./3,1./3],[1./3,-2./3,2./3]])\n",
    "print A\n",
    "print \"A transpose is equal to\"\n",
    "print A.transpose()\n",
    "print \"A∗(transpose of A)=\"\n",
    "print A*A.transpose()\n",
    "print \"Hence, A is orthogonal\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Example 2.42, page no. 87"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Let R represents the matrix of transformation and P represents a diagonal matrix whose values are the eigenvalues of A. then\n",
      "R is normalised. let U represents unnormalised version of r\n",
      "Two eigen vectors are the two columns of U\n",
      "[[ 4.  1.]\n",
      " [ 0.  0.]]\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "import math\n",
    "\n",
    "A = numpy.matrix([[5,4],[1,2]])\n",
    "print \"Let R represents the matrix of transformation and P represents a diagonal matrix whose values are the eigenvalues of A. then\"\n",
    "P,R= numpy.linalg.eig(A)\n",
    "U = numpy.zeros([2, 2])\n",
    "print \"R is normalised. let U represents unnormalised version of r\"\n",
    "U[0,0]= R[0,0]*math.sqrt(17)\n",
    "U[0,1]= R[0,1]*math.sqrt(17)\n",
    "U[0,1]= R[1,1]*math.sqrt(2)\n",
    "print \"Two eigen vectors are the two columns of U\"\n",
    "print U"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Examle 2.43, page no. 88"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Let Rrepresents the matrix of transformation and Prepresents a diagonalmatrix whose values are the eigenvalues of A. then\n",
      "R is normalised. let U represents unnormalised version of r\n",
      "[-2.  3.  6.]\n",
      "Three eigen vectors are the three columns of U\n",
      "[[ -2.82842712   5.19615242  14.69693846]\n",
      " [  0.           0.           0.        ]\n",
      " [  0.           0.           0.        ]]\n"
     ]
    }
   ],
   "source": [
    "import numpy,math\n",
    "\n",
    "A = numpy.matrix([[1,1,3],[1,5,1],[3,1,1]])\n",
    "U = numpy.zeros([3,3])\n",
    "print \"Let Rrepresents the matrix of transformation and Prepresents a diagonalmatrix whose values are the eigenvalues of A. then\"\n",
    "R,P = numpy.linalg.eig(A)\n",
    "print \"R is normalised. let U represents unnormalised version of r\"\n",
    "print R\n",
    "U[0,0] = R[0]*math.sqrt(2) \n",
    "U[0,1] = R[1]*math.sqrt(3)\n",
    "U[0,2] = R[2]*math.sqrt(6)\n",
    "print \"Three eigen vectors are the three columns of U\"\n",
    "print U"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.44, page no. 89"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Let Rrepresents the matrix of transformation and Prepresents a diagonalmatrix whose values are the eigenvalues of A. then\n",
      "R is normalised. let U represents unnormalised version of r\n",
      "[ 3.  2.  5.]\n",
      "Three eigen vectors are the three columns of U\n",
      "[[  3.           2.          18.70828693]\n",
      " [  0.           0.           0.        ]\n",
      " [  0.           0.           0.        ]]\n"
     ]
    }
   ],
   "source": [
    "import numpy,math\n",
    "\n",
    "A = numpy.matrix([[3,1,4],[0,2,6],[0,0,5]])\n",
    "U = numpy.zeros([3,3])\n",
    "print \"Let Rrepresents the matrix of transformation and Prepresents a diagonalmatrix whose values are the eigenvalues of A. then\"\n",
    "R,P = numpy.linalg.eig(A)\n",
    "print \"R is normalised. let U represents unnormalised version of r\"\n",
    "print R\n",
    "U[0,0] = R[0]*math.sqrt(1) \n",
    "U[0,1] = R[1]*math.sqrt(1)\n",
    "U[0,2] = R[2]*math.sqrt(14)\n",
    "print \"Three eigen vectors are the three columns of U\"\n",
    "print U"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.45, page no. 90"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Eigen values of A are\n",
      "(array([-1.,  5.]), matrix([[-0.89442719, -0.70710678],\n",
      "        [ 0.4472136 , -0.70710678]]))\n",
      "Let\n",
      "Hence, the characteristic equation is ( x−a ) ( x−b)\n",
      "[-8 -5]\n",
      "Aˆ2−4∗A−5∗ I=\n",
      "[[ 0.  0.]\n",
      " [ 0.  0.]]\n",
      "Inverse of A=\n",
      "[[-0.6  0.8]\n",
      " [ 0.4 -0.2]]\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "x = numpy.poly([0])\n",
    "A = numpy.matrix([[1,4],[2,3]])\n",
    "I = numpy.eye(2)\n",
    "print \"Eigen values of A are\"\n",
    "print numpy.linalg.eig(A)\n",
    "print \"Let\"\n",
    "a = -1;\n",
    "b = 5;\n",
    "print \"Hence, the characteristic equation is ( x−a ) ( x−b)\"\n",
    "print ( x - a ) *( x - b )\n",
    "\n",
    "print \"Aˆ2−4∗A−5∗ I=\"\n",
    "print A**2-4*A-5* I\n",
    "print \"Inverse of A=\"\n",
    "print numpy.linalg.inv ( A )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.46, page no. 91"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Egenvalues of A are\n",
      "(array([ 4.25683813,  0.40327935, -4.66011748]), matrix([[ 0.10296232, -0.91299477, -0.48509974],\n",
      "        [-0.90473047,  0.40531299,  0.37306899],\n",
      "        [ 0.41335402,  0.04649661,  0.79088417]]))\n",
      "Let\n",
      "Hence, the characteristic equation is ( x−a ) ( x−b) ( x−c )\n",
      "[-10.99999905   8.00000095]\n",
      "Inverse of A=\n",
      "[[ 3.    1.    1.5 ]\n",
      " [-1.25 -0.25 -0.75]\n",
      " [-0.25 -0.25 -0.25]]\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "x = numpy.poly([0])\n",
    "A = numpy.matrix([[1,1,3],[1,3,-3],[-2,-4,-4]])\n",
    "print \"Egenvalues of A are\"\n",
    "print numpy.linalg.eig(A)\n",
    "print \"Let\"\n",
    "a =4.2568381\n",
    "b =0.4032794\n",
    "c = -4.6601175\n",
    "print \"Hence, the characteristic equation is ( x−a ) ( x−b) ( x−c )\"\n",
    "p = (x-a)*(x-b)*(x-c)\n",
    "print p\n",
    "print \"Inverse of A=\"\n",
    "print numpy.linalg.inv(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.47, page no. 91"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Eigenvalues of A are\n",
      "(array([ 3.,  1.,  1.]), matrix([[ 0.70710678, -0.70710678, -0.40824829],\n",
      "        [ 0.        ,  0.        ,  0.81649658],\n",
      "        [ 0.70710678,  0.70710678, -0.40824829]]))\n",
      "Let\n",
      "Hence, the characteristic  equation is (x−a)(x−b)(x−c)=\n",
      "[ 0 -3]\n",
      "Aˆ8−5∗Aˆ7+7∗Aˆ6−3∗Aˆ5+Aˆ4−5∗Aˆ3+8∗Aˆ2−2∗A+I =\n",
      "[[ 8.  5.  5.]\n",
      " [ 0.  3.  0.]\n",
      " [ 5.  5.  8.]]\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "x = numpy.poly([0])\n",
    "A = numpy.matrix([[2,1,1],[0,1,0],[1,1,2]])\n",
    "I = numpy.eye(3)\n",
    "print \"Eigenvalues of A are\"\n",
    "print numpy.linalg.eig(A)\n",
    "print \"Let\"\n",
    "a =1\n",
    "b =1\n",
    "c =3\n",
    "print \"Hence, the characteristic  equation is (x−a)(x−b)(x−c)=\"\n",
    "p = (x-a)*(x-b)*(x-c)\n",
    "print p\n",
    "print \"Aˆ8−5∗Aˆ7+7∗Aˆ6−3∗Aˆ5+Aˆ4−5∗Aˆ3+8∗Aˆ2−2∗A+I =\"\n",
    "print A**8-5*A**7+7*A**6-3*A**5+A**4-5*A**3+8*A**2-2*A+I"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Example 2.48, page no. 93"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "R is matrix of transformation and D is a diagonal matrix\n",
      "[-1.65544238 -0.21075588  2.86619826]\n",
      "[[-0.87936655 -0.34661859 -0.32645063]\n",
      " [ 0.11410244  0.51222983 -0.85123513]\n",
      " [-0.46227167  0.78579651  0.41088775]]\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "A = numpy.matrix([[-1,2,-2],[1,2,1],[-1,-1,0]])\n",
    "print \"R is matrix of transformation and D is a diagonal matrix\"\n",
    "[R,D]= numpy.linalg.eigh(A)\n",
    "print R\n",
    "print D"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.49, page no. 93"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "R is matrix of transformation and D is a diagonal matrix\n",
      "R is normalised, let P denotes unnormalised version of R . Then \n",
      "[ 3.  2.  5.]\n",
      "[[  4.24264069   3.46410162  12.24744871]\n",
      " [  0.           0.           0.        ]\n",
      " [  0.           0.           0.        ]]\n",
      "A^4=  [[  81   65 1502]\n",
      " [   0   16 1218]\n",
      " [   0    0  625]]\n"
     ]
    }
   ],
   "source": [
    "import numpy,math\n",
    "\n",
    "A = numpy.matrix([[3,1,4],[0,2,6],[0,0,5]])\n",
    "P = numpy.zeros([3,3])\n",
    "print \"R is matrix of transformation and D is a diagonal matrix\"\n",
    "R,D = numpy.linalg.eig(A)\n",
    "print \"R is normalised, let P denotes unnormalised version of R . Then \"\n",
    "print R\n",
    "P[0,0] = R[0]*math.sqrt(2) \n",
    "P[0,1] = R[1]*math.sqrt(3)\n",
    "P[0,2] = R[2]*math.sqrt(6)\n",
    "print P\n",
    "print \"A^4= \",A**4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.50, page no. 94"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3∗xˆ2+5∗yˆ2+3∗zˆ2−2∗y∗z+2∗z∗x−2∗x∗y\n",
      "The matrix of the given quadratic form is\n",
      "Let R represents the matrix of transformation and Prepresents a diagonal matrix whose values are the eigenvalues of A. then\n",
      "So, canonical form is 2∗xˆ2+3∗yˆ2+6∗zˆ2\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "print  \"3∗xˆ2+5∗yˆ2+3∗zˆ2−2∗y∗z+2∗z∗x−2∗x∗y\"\n",
    "print \"The matrix of the given quadratic form is\"\n",
    "A = numpy.matrix([[3,-1,1],[-1,5,-1],[1,-1,3]])\n",
    "print \"Let R represents the matrix of transformation and Prepresents a diagonal matrix whose values are the eigenvalues of A. then\"\n",
    "[R,P] = numpy.linalg.eig(A)\n",
    "print \"So, canonical form is 2∗xˆ2+3∗yˆ2+6∗zˆ2\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.51, page no. 95"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2∗x1∗x2+2∗x1∗x3−2∗x2∗x3\n",
      "The matrix of the given quadratic form is\n",
      "Let R represents the matrix of transformation and P represents a diagonal matrix whose values are the eigenvalues of A. then\n",
      "so, canonical form is −2∗xˆ2+yˆ2+ zˆ2\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "print \"2∗x1∗x2+2∗x1∗x3−2∗x2∗x3\"\n",
    "print \"The matrix of the given quadratic form is\"\n",
    "A = numpy.matrix([[0,1,1],[1,0,-1],[1,-1,0]])\n",
    "print \"Let R represents the matrix of transformation and P represents a diagonal matrix whose values are the eigenvalues of A. then\"\n",
    "[R,P] = numpy.linalg.eig(A)\n",
    "print \"so, canonical form is −2∗xˆ2+yˆ2+ zˆ2\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.52, page no. 96"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A∗= [[ 2.-1.j -5.-0.j]\n",
      " [ 3.-0.j  0.-1.j]\n",
      " [-1.-3.j  4.+2.j]]\n",
      "AA∗= [[ 24.+0.j -20.+2.j]\n",
      " [-20.-2.j  46.+0.j]]\n",
      "Clearly, AA∗ is hermitian matrix\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "'''\n",
    "A=[2+%i 3 -1+3*%i;-5 %i 4-2*%i]\n",
    "'''\n",
    "\n",
    "A = numpy.matrix([[2+1j,3,-1+3*1j],[-5,1j,4-2*1j]])\n",
    "#A = A.getH()\n",
    "print \"A∗=\", A.getH()\n",
    "print \"AA∗=\", A*(A.getH())\n",
    "print \"Clearly, AA∗ is hermitian matrix\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.53, page no. 97"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " A∗= [[ 0.-0.j  0.-0.j]\n",
      " [-0.-0.j  0.-0.j]]\n",
      "AA∗= [[ 0.+0.j  0.+0.j]\n",
      " [ 0.+0.j  0.+0.j]]\n",
      "A∗A= [[ 0.+0.j  0.+0.j]\n",
      " [ 0.+0.j  0.+0.j]]\n"
     ]
    }
   ],
   "source": [
    "import numpy \n",
    "\n",
    "A = numpy.matrix([[(1/2)*(1+1j),(1/2)*(-1+1j)],[(1/2)*(1+1j),(1/2)*(1-1j)]])\n",
    "print \"A∗=\", A.getH()\n",
    "print \"AA∗=\", A*(A.getH())\n",
    "print \"A∗A=\", (A.getH())*A"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.54, page no. 97"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I−A=\n",
      "inverse of (I+A)=\n",
      "[[ 0.16666667+0.j         -0.16666667-0.33333333j]\n",
      " [ 0.16666667-0.33333333j  0.16666667+0.j        ]]\n",
      "((I−A)(inverse(I+A)))∗((I−A)(inverse(I+A)))=\n",
      "[[  1.11111111e-01-0.44444444j  -2.77555756e-17+0.88888889j]\n",
      " [ -2.77555756e-17+0.88888889j   1.11111111e-01+0.44444444j]]\n",
      "((I−A)(inverse(I+A)))((I−A)(inverse(I+A)))∗=\n",
      "[[  1.11111111e-01+0.44444444j   1.11022302e-16+0.88888889j]\n",
      " [  1.11022302e-16+0.88888889j   1.11111111e-01-0.44444444j]]\n",
      "Clearly, the product is an identity matrix.hence, it is a unitary matrix\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "A = numpy.matrix([[0,1+2*1j],[-1+2*1j,0]])\n",
    "I = numpy.eye(2)\n",
    "print \"I−A=\"\n",
    "I-A\n",
    "print \"inverse of (I+A)=\"\n",
    "print numpy.linalg.inv(I+A)\n",
    "print \"((I−A)(inverse(I+A)))∗((I−A)(inverse(I+A)))=\"\n",
    "print (((I-A)*(numpy.linalg.inv(I+A))).T)*((I-A)*(numpy.linalg.inv(I+A)))\n",
    "print \"((I−A)(inverse(I+A)))((I−A)(inverse(I+A)))∗=\"\n",
    "print ((I-A)*(numpy.linalg.inv(I+A)))*(((I-A)*(numpy.linalg.inv(I+A))).T)\n",
    "print \"Clearly, the product is an identity matrix.hence, it is a unitary matrix\""
   ]
  }
 ],
 "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.11+"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}