{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 1 - Linear Equations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page 8 Example 1.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a=\n",
      "[[ 2 -1  3  2]\n",
      " [ 1  4  0 -1]\n",
      " [ 2  6 -1  5]]\n",
      "Applying row transformations:\n",
      "R1 = R1-2*R2\n",
      "a = \n",
      "[[ 0 -9  3  4]\n",
      " [ 1  4  0 -1]\n",
      " [ 2  6 -1  5]]\n",
      "R3 = R3-2*R2\n",
      "a = \n",
      "[[ 0 -9  3  4]\n",
      " [ 1  4  0 -1]\n",
      " [ 0 -2 -1  7]]\n",
      "R3 = R3/-2\n",
      "a = \n",
      "[[ 0 -9  3  4]\n",
      " [ 1  4  0 -1]\n",
      " [ 0  1  0 -3]]\n",
      "R2 = R2-4*R3\n",
      "a = \n",
      "[[ 0 -9  3  4]\n",
      " [ 1  0  0 11]\n",
      " [ 0  1  0 -3]]\n",
      "R1 = R1+9*R3\n",
      "a = \n",
      "[[  0   0   3 -23]\n",
      " [  1   0   0  11]\n",
      " [  0   1   0  -3]]\n",
      "R1 = R1*2/15\n",
      "a = \n",
      "[[ 0  0  0 -4]\n",
      " [ 1  0  0 11]\n",
      " [ 0  1  0 -3]]\n",
      "R2 = R2+2*R1\n",
      "a = \n",
      "[[ 0  0  0 -4]\n",
      " [ 1  0  0  3]\n",
      " [ 0  1  0 -3]]\n",
      "R3 = R3-R1/2\n",
      "a = \n",
      "[[ 0  0  0 -4]\n",
      " [ 1  0  0  3]\n",
      " [ 0  1  0 -1]]\n",
      "We get the system of equations as:\n",
      "2*x1 - x2 + 3*x3 + 2*x4 = 0\n",
      "x1 + 4*x2 - x4 = 0\n",
      "2*x1 + 6* x2 - x3 + 5*x4 = 0\n",
      "and\n",
      "x2 - 5/3*x4 = 0 x1 + 17/3*x4 = 0 x3 - 11/3*x4 = 0\n",
      "now by assigning any rational value c to x4 in system second, the solution is evaluated as:\n",
      "(-17/3*c,5/3,11/3*c,c)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([[2, -1, 3, 2],[1, 4, 0 ,-1],[2, 6, -1, 5]])\n",
    "print 'a=\\n',a\n",
    "print 'Applying row transformations:'\n",
    "print 'R1 = R1-2*R2'\n",
    "a[0,:] = a[0,:] - 2*a[1,:]#\n",
    "print 'a = \\n',a\n",
    "print 'R3 = R3-2*R2'\n",
    "a[2,:] = a[2,:] - 2*a[1,:]\n",
    "print 'a = \\n',a\n",
    "print 'R3 = R3/-2'\n",
    "a[2,:] = -1.0/2*a[2,:]#\n",
    "print 'a = \\n',a\n",
    "print 'R2 = R2-4*R3'\n",
    "a[1,:] = a[1,:] - 4*a[2,:]\n",
    "print 'a = \\n',a\n",
    "print 'R1 = R1+9*R3'\n",
    "a[0,:] = a[0,:] + 9*a[2,:]#\n",
    "print 'a = \\n',a\n",
    "print 'R1 = R1*2/15'\n",
    "a[0,:] = a[0,:] * 2/15\n",
    "print 'a = \\n',a\n",
    "print 'R2 = R2+2*R1'\n",
    "a[1,:] = a[1,:] + 2*a[0,:]\n",
    "print 'a = \\n',a\n",
    "print 'R3 = R3-R1/2'\n",
    "a[2,:] = a[2,:] - 1.0/2*a[0,:]\n",
    "print 'a = \\n',a\n",
    "print 'We get the system of equations as:'\n",
    "print '2*x1 - x2 + 3*x3 + 2*x4 = 0'\n",
    "print 'x1 + 4*x2 - x4 = 0'\n",
    "print '2*x1 + 6* x2 - x3 + 5*x4 = 0'\n",
    "print 'and'\n",
    "print 'x2 - 5/3*x4 = 0','x1 + 17/3*x4 = 0','x3 - 11/3*x4 = 0'\n",
    "print 'now by assigning any rational value c to x4 in system second, the solution is evaluated as:'\n",
    "print '(-17/3*c,5/3,11/3*c,c)'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page 9 Example 1.6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a = \n",
      "[[-1.+0.j  0.+1.j]\n",
      " [ 0.-1.j  3.+0.j]\n",
      " [ 1.+0.j  2.+0.j]]\n",
      "Applying row transformations:\n",
      "R1 = R1+R3 and R2 = R2 + i *R3\n",
      "a = \n",
      "[[ 0.+0.j  2.+1.j]\n",
      " [ 0.+0.j  3.+2.j]\n",
      " [ 1.+0.j  2.+0.j]]\n",
      "R1 = R1 * (1/2+i)\n",
      "a = \n",
      "[[ 0.+0.j  1.+0.j]\n",
      " [ 0.+0.j  3.+2.j]\n",
      " [ 1.+0.j  2.+0.j]]\n",
      "R2 = R2-R1*(3+2i) and R3 = R3 - 2 *R1\n",
      "a = \n",
      "[[ 0.+0.j  1.+0.j]\n",
      " [ 0.+0.j  0.+0.j]\n",
      " [ 1.+0.j  0.+0.j]]\n",
      "Thus the system of equations is:\n",
      "x1 + 2*x2 = 0 -i*x1 + 3*x2 = 0 -x1+i*x2 = 0\n",
      "It has only trivial solution x1 = x2 = 0\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a=np.array([[-1, 1J],[-1J, 3],[1 ,2]])\n",
    "print 'a = \\n',a\n",
    "print 'Applying row transformations:'\n",
    "print 'R1 = R1+R3 and R2 = R2 + i *R3'\n",
    "a[0,:] = a[0,:] +a[2,:]\n",
    "a[1,:] = a[1,:] + 1J * a[2,:]\n",
    "print 'a = \\n',a\n",
    "print 'R1 = R1 * (1/2+i)'\n",
    "a[0,:] = 1.0/(2 + 1J) * a[0,:]\n",
    "print 'a = \\n',a\n",
    "print 'R2 = R2-R1*(3+2i) and R3 = R3 - 2 *R1'\n",
    "a[1,:] = (a[1,:] - (3 + 2 * 1J) * a[0,:])\n",
    "a[2,:] = (a[2,:] - 2 * a[0,:])\n",
    "print 'a = \\n',a\n",
    "print 'Thus the system of equations is:'\n",
    "print 'x1 + 2*x2 = 0','-i*x1 + 3*x2 = 0','-x1+i*x2 = 0'\n",
    "print 'It has only trivial solution x1 = x2 = 0'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page 9 Example 1.7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.  0.  0.  0.  0.  0.  0.  0.]\n",
      " [ 0.  1.  0.  0.  0.  0.  0.  0.]\n",
      " [ 0.  0.  1.  0.  0.  0.  0.  0.]\n",
      " [ 0.  0.  0.  1.  0.  0.  0.  0.]\n",
      " [ 0.  0.  0.  0.  1.  0.  0.  0.]\n",
      " [ 0.  0.  0.  0.  0.  1.  0.  0.]\n",
      " [ 0.  0.  0.  0.  0.  0.  1.  0.]\n",
      " [ 0.  0.  0.  0.  0.  0.  0.  1.]]\n",
      "This is an Identity matrix of order 8 * 8\n",
      "And It is a row reduced matrix.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "n = np.random.randint(9)\n",
    "print np.identity(n)\n",
    "print 'This is an Identity matrix of order %d * %d'%(n,n)\n",
    "print 'And It is a row reduced matrix.'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page 12 Example 1.8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.  0.  0.  0.  0.  0.]\n",
      " [ 0.  1.  0.  0.  0.  0.]\n",
      " [ 0.  0.  1.  0.  0.  0.]\n",
      " [ 0.  0.  0.  1.  0.  0.]\n",
      " [ 0.  0.  0.  0.  1.  0.]\n",
      " [ 0.  0.  0.  0.  0.  1.]]\n",
      "This is an Identity matrix of order 6 * 6\n",
      "And It is a row reduced matrix.\n",
      "[[ 0.  0.  0.  0.  0.]]\n",
      "This is an Zero matrix of order 1 * 5\n",
      "And It is also a row reduced matrix.\n",
      "a = \n",
      "[[ 0.   1.  -3.   0.   0.5]\n",
      " [ 0.   0.   0.   1.   2. ]\n",
      " [ 0.   0.   0.   0.   0. ]]\n",
      "This is a non-trivial row reduced matrix.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "n = np.random.randint(9)\n",
    "print np.identity(n)\n",
    "print 'This is an Identity matrix of order %d * %d'%(n,n)\n",
    "print 'And It is a row reduced matrix.'\n",
    "m = np.random.randint(0,9)\n",
    "n = np.random.randint(9)\n",
    "print np.zeros([m,n])\n",
    "print 'This is an Zero matrix of order %d * %d'%(m,n)\n",
    "print 'And It is also a row reduced matrix.'\n",
    "a = np.array([[0, 1, -3, 0, 1.0/2],[0, 0, 0, 1, 2],[0, 0 ,0 ,0 ,0]])\n",
    "print 'a = \\n',a\n",
    "print 'This is a non-trivial row reduced matrix.'\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page 14 Example 1.9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A = \n",
      "[[ 1 -2  1]\n",
      " [ 2  1  1]\n",
      " [ 0  5 -1]]\n",
      "Applying row transformations:\n",
      "R2 = R2 - 2*R1\n",
      "A = \n",
      "[[ 1 -2  1]\n",
      " [ 0  5 -1]\n",
      " [ 0  5 -1]]\n",
      "R3 = R3 - R2\n",
      "A = \n",
      "[[ 1 -2  1]\n",
      " [ 0  5 -1]\n",
      " [ 0  0  0]]\n",
      "R2 = 1/5*R2\n",
      "A = \n",
      "[[ 1 -2  1]\n",
      " [ 0  1  0]\n",
      " [ 0  0  0]]\n",
      "R1 = R1 - 2*R2\n",
      "A = \n",
      "[[1 0 1]\n",
      " [0 1 0]\n",
      " [0 0 0]]\n",
      "The condition that the system have a solution is:\n",
      "2*y1 - y2 + y3 = 0\n",
      "where, y1,y2,y3 are some scalars\n",
      "If the condition is satisfied then solutions are obtained by assigning a value c to x3\n",
      "Solutions are:\n",
      "x2 = 1/5*c + 1/5*(y2 - 2*y1) x1 = -3/5*c + 1/5*(y1 + 2*y2)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "A = np.array([[1, -2, 1],[2, 1, 1],[0, 5, -1]])\n",
    "print 'A = \\n',A\n",
    "print 'Applying row transformations:'\n",
    "print 'R2 = R2 - 2*R1'\n",
    "A[1,:] = A[1,:] - 2*A[0,:]\n",
    "print 'A = \\n',A\n",
    "print 'R3 = R3 - R2'\n",
    "A[2,:] = A[2,:] - A[1,:]\n",
    "print 'A = \\n',A\n",
    "print 'R2 = 1/5*R2'\n",
    "A[1,:] = 1.0/5*A[1,:]\n",
    "print 'A = \\n',A\n",
    "print 'R1 = R1 - 2*R2'\n",
    "A[0,:] = A[0,:] + 2*A[1,:]\n",
    "print 'A = \\n',A\n",
    "print 'The condition that the system have a solution is:'\n",
    "print '2*y1 - y2 + y3 = 0'\n",
    "print 'where, y1,y2,y3 are some scalars'\n",
    "print 'If the condition is satisfied then solutions are obtained by assigning a value c to x3'\n",
    "print 'Solutions are:'\n",
    "print 'x2 = 1/5*c + 1/5*(y2 - 2*y1)','x1 = -3/5*c + 1/5*(y1 + 2*y2)'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page 17 Example 1.10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a=\n",
      "[[ 1  0]\n",
      " [-3  1]]\n",
      "b=\n",
      "[[ 5 -1  2]\n",
      " [15  4  8]]\n",
      "ab = \n",
      "[[ 5 -1  2]\n",
      " [ 0  7  2]]\n",
      "-----------------------------------------------------------------\n",
      "a=\n",
      "[[ 1  0]\n",
      " [-2  3]\n",
      " [ 5  4]\n",
      " [ 0  1]]\n",
      "b=\n",
      "[[ 0  6  1]\n",
      " [ 3  8 -2]]\n",
      "ab = \n",
      "[[ 0  6  1]\n",
      " [ 9 12 -8]\n",
      " [12 62 -3]\n",
      " [ 3  8 -2]]\n",
      "-----------------------------------------------------------------\n",
      "a=\n",
      "[[2 1]\n",
      " [5 4]]\n",
      "b=\n",
      "[[1]\n",
      " [6]]\n",
      "ab = \n",
      "[[ 8]\n",
      " [29]]\n",
      "-----------------------------------------------------------------\n",
      "a=\n",
      "[[-1]\n",
      " [ 3]]\n",
      "b=\n",
      "[[2 4]]\n",
      "ab = \n",
      "[[-2 -4]\n",
      " [ 6 12]]\n",
      "-----------------------------------------------------------------\n",
      "a=\n",
      "[[2 4]]\n",
      "b=\n",
      "[[-1]\n",
      " [ 3]]\n",
      "ab = \n",
      "[[10]]\n",
      "-----------------------------------------------------------------\n",
      "a=\n",
      "[[0 1 0]\n",
      " [0 0 0]\n",
      " [0 0 0]]\n",
      "b=\n",
      "[[ 1 -5  2]\n",
      " [ 2  3  4]\n",
      " [ 9 -1  3]]\n",
      "ab = \n",
      "[[2 3 4]\n",
      " [0 0 0]\n",
      " [0 0 0]]\n",
      "-----------------------------------------------------------------\n",
      "a=\n",
      "[[ 1 -5  2]\n",
      " [ 2  3  4]\n",
      " [ 9 -1  3]]\n",
      "b=\n",
      "[[0 1 0]\n",
      " [0 0 0]\n",
      " [0 0 0]]\n",
      "ab = \n",
      "[[0 1 0]\n",
      " [0 2 0]\n",
      " [0 9 0]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "#Part a\n",
    "a = np.array([[1, 0],[-3, 1]])\n",
    "b = np.array([[5, -1, 2],[15, 4, 8]])\n",
    "print 'a=\\n',a\n",
    "print 'b=\\n',b\n",
    "print 'ab = \\n',a.dot(b)\n",
    "\n",
    "print '-----------------------------------------------------------------'\n",
    "#Part b\n",
    "a = np.array([[1, 0],[-2, 3],[5 ,4],[0, 1]])\n",
    "b = np.array([[0, 6, 1],[3 ,8 ,-2]])\n",
    "print 'a=\\n',a\n",
    "print 'b=\\n',b\n",
    "print 'ab = \\n',a.dot(b)\n",
    "print '-----------------------------------------------------------------'\n",
    "#Part c\n",
    "a = np.array([[2, 1],[5, 4]])\n",
    "b = np.array([[1],[6]])\n",
    "print 'a=\\n',a\n",
    "print 'b=\\n',b\n",
    "print 'ab = \\n',a.dot(b)\n",
    "print '-----------------------------------------------------------------'\n",
    "#Part d\n",
    "a = np.array([[-1],[3]])\n",
    "b = np.array([[2, 4]])\n",
    "print 'a=\\n',a\n",
    "print 'b=\\n',b\n",
    "print 'ab = \\n',a.dot(b)\n",
    "print '-----------------------------------------------------------------'\n",
    "#Part e\n",
    "a = np.array([[2, 4]])\n",
    "b = np.array([[-1],[3]])\n",
    "print 'a=\\n',a\n",
    "print 'b=\\n',b\n",
    "print 'ab = \\n',a.dot(b)\n",
    "print '-----------------------------------------------------------------'\n",
    "#Part f\n",
    "a = np.array([[0, 1 ,0],[0, 0, 0],[0, 0, 0]])\n",
    "b = np.array([[1, -5, 2],[2, 3, 4],[9 ,-1, 3]])\n",
    "print 'a=\\n',a\n",
    "print 'b=\\n',b\n",
    "print 'ab = \\n',a.dot(b)\n",
    "print '-----------------------------------------------------------------'\n",
    "#Part g\n",
    "a = np.array([[1, -5, 2],[2, 3, 4],[9, -1, 3]])\n",
    "b = np.array([[0, 1, 0],[0 ,0 ,0],[0, 0, 0]])\n",
    "print 'a=\\n',a\n",
    "print 'b=\\n',b\n",
    "print 'ab = \\n',a.dot(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page 22 Example 1.14"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a = \n",
      "[[0 1]\n",
      " [1 0]]\n",
      "inverse a = \n",
      "[[ 0.  1.]\n",
      " [ 1.  0.]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([[0, 1],[1, 0]])\n",
    "print 'a = \\n',a\n",
    "print 'inverse a = \\n',np.linalg.inv(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page 25 Example 1.15"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a = \n",
      "[[ 2 -1]\n",
      " [ 1  3]]\n",
      "Applying row tranformations\n",
      "Interchange R1 and R2\n",
      "a = \n",
      "[[ 1  3]\n",
      " [ 2 -1]]\n",
      "R2 = R2 - 2 * R1\n",
      "a =\n",
      "  [[ 1  3]\n",
      " [ 0 -7]]\n",
      "R2 = R2 *1/(-7)\n",
      "a = \n",
      "[[1 3]\n",
      " [0 1]]\n",
      "R1 = R1 - 3 * R2\n",
      "a = \n",
      "[[1 0]\n",
      " [0 1]]\n",
      "Since a  has become an identity matrix. So, a is invertible\n",
      "inverse of a = \n",
      "[[ 0.42857143  0.14285714]\n",
      " [-0.14285714  0.28571429]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([[2, -1],[1 ,3]])\n",
    "b = np.array([[2, -1],[1 ,3]]) #Temporary variable to store a\n",
    "print 'a = \\n',a\n",
    "print 'Applying row tranformations'\n",
    "print 'Interchange R1 and R2'\n",
    "a[0,:] = a[1,:]\n",
    "a[1,:] = b[0,:]\n",
    "print 'a = \\n',a\n",
    "print 'R2 = R2 - 2 * R1'\n",
    "a[1,:] = a[1,:] - 2 * a[0,:]\n",
    "print 'a =\\n ',a\n",
    "print 'R2 = R2 *1/(-7)'\n",
    "a[1,:] = (-1.0/7) * a[1,:]\n",
    "print 'a = \\n',a\n",
    "print 'R1 = R1 - 3 * R2'\n",
    "a[0,:] = a[0,:] - 3 * a[1,:]\n",
    "print 'a = \\n',a\n",
    "print 'Since a  has become an identity matrix. So, a is invertible'\n",
    "print 'inverse of a = '\n",
    "print np.linalg.inv(b)# #a was stored in b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page 25 Example 1.16"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a = \n",
      "[[ 1.          0.5         0.33333333]\n",
      " [ 0.5         0.33333333  0.25      ]\n",
      " [ 0.33333333  0.25        0.2       ]]\n",
      "b = \n",
      "[[ 1.  0.  0.]\n",
      " [ 0.  1.  0.]\n",
      " [ 0.  0.  1.]]\n",
      "Applying row transformations on a and b simultaneously,\n",
      "R2 = R2 - 1/2 * R1 and R3 = R3 - 1/3*R1\n",
      "a = \n",
      "[[ 1.          0.5         0.33333333]\n",
      " [ 0.          0.08333333  0.08333333]\n",
      " [ 0.          0.08333333  0.08888889]]\n",
      "b = \n",
      "[[ 1.          0.          0.        ]\n",
      " [-0.5         1.          0.        ]\n",
      " [-0.33333333  0.          1.        ]]\n",
      "R3 = R3 - R2\n",
      "a = \n",
      "[[  1.00000000e+00   5.00000000e-01   3.33333333e-01]\n",
      " [  0.00000000e+00   8.33333333e-02   8.33333333e-02]\n",
      " [  0.00000000e+00   2.77555756e-17   5.55555556e-03]]\n",
      "b = \n",
      "[[ 1.          0.          0.        ]\n",
      " [-0.5         1.          0.        ]\n",
      " [ 0.16666667 -1.          1.        ]]\n",
      "R2 = R2 * 12 and R3 = R3 * 180\n",
      "a = \n",
      "[[  1.00000000e+00   5.00000000e-01   3.33333333e-01]\n",
      " [  0.00000000e+00   1.00000000e+00   1.00000000e+00]\n",
      " [  0.00000000e+00   4.99600361e-15   1.00000000e+00]]\n",
      "b = \n",
      "[[   1.    0.    0.]\n",
      " [  -6.   12.    0.]\n",
      " [  30. -180.  180.]]\n",
      "R2 = R2 - R3 and R1 = R1 - 1/3*R3\n",
      "a = \n",
      "[[  1.00000000e+00   5.00000000e-01  -4.44089210e-16]\n",
      " [  0.00000000e+00   1.00000000e+00  -1.33226763e-15]\n",
      " [  0.00000000e+00   4.99600361e-15   1.00000000e+00]]\n",
      "b = \n",
      "[[  -9.   60.  -60.]\n",
      " [ -36.  192. -180.]\n",
      " [  30. -180.  180.]]\n",
      "R1 = R1 - 1/2 * R2\n",
      "a = \n",
      "[[ 1.  0.  0.]\n",
      " [ 0.  1. -0.]\n",
      " [ 0.  0.  1.]]\n",
      "b = \n",
      "[[   9.  -36.   30.]\n",
      " [ -36.  192. -180.]\n",
      " [  30. -180.  180.]]\n",
      "Since, a = identity matrix of order 3*3. So, b is inverse of a\n",
      "inverse(a) = \n",
      "[[   9.  -36.   30.]\n",
      " [ -36.  192. -180.]\n",
      " [  30. -180.  180.]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([[1 ,1./2, 1.0/3],[1.0/2 ,1.0/3, 1.0/4],[1.0/3, 1.0/4, 1.0/5]])\n",
    "print 'a = \\n',a\n",
    "b = np.identity(3)\n",
    "print 'b = \\n',b\n",
    "print 'Applying row transformations on a and b simultaneously,'\n",
    "print 'R2 = R2 - 1/2 * R1 and R3 = R3 - 1/3*R1'\n",
    "a[1,:] = a[1,:] - 1.0/2 * a[0,:]\n",
    "a[2,:] = a[2,:] - 1.0/3 * a[0,:]\n",
    "b[1,:] = b[1,:] - 1.0/2 * b[0,:]\n",
    "b[2,:] = b[2,:] - 1.0/3 * b[0,:]\n",
    "print 'a = \\n',a\n",
    "print 'b = \\n',b\n",
    "print 'R3 = R3 - R2'\n",
    "a[2,:] = a[2,:] -  a[1,:]#\n",
    "b[2,:] = b[2,:] -  b[1,:]\n",
    "print 'a = \\n',a\n",
    "print 'b = \\n',b\n",
    "print 'R2 = R2 * 12 and R3 = R3 * 180'\n",
    "a[1,:] = a[1,:] *12#\n",
    "a[2,:] = a[2,:] * 180#\n",
    "b[1,:] = b[1,:] * 12#\n",
    "b[2,:] = b[2,:] * 180#\n",
    "print 'a = \\n',a\n",
    "print 'b = \\n',b\n",
    "print 'R2 = R2 - R3 and R1 = R1 - 1/3*R3'\n",
    "a[1,:] = a[1,:] - a[2,:]#\n",
    "a[0,:] = a[0,:] - 1./3 * a[2,:]#\n",
    "b[1,:] = b[1,:] - b[2,:]#\n",
    "b[0,:] = b[0,:] - 1./3 * b[2,:]#\n",
    "print 'a = \\n',a\n",
    "print 'b = \\n',b\n",
    "print 'R1 = R1 - 1/2 * R2'\n",
    "a[0,:] = a[0,:] -  1./2 * a[1,:]#\n",
    "b[0,:] = b[0,:] -  1./2 * b[1,:]#\n",
    "print 'a = \\n',np.matrix.round(a)\n",
    "print 'b = \\n',b\n",
    "print 'Since, a = identity matrix of order 3*3. So, b is inverse of a'\n",
    "print 'inverse(a) = \\n',b"
   ]
  }
 ],
 "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
}