{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 1 - Matrix notation & matrix multiplication"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:1 Pg:20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x=\n",
      "[[u]\n",
      " [v]\n",
      " [w]]\n",
      "R2=R2-R1,R3=R3-4*R1\n",
      "[[1 1 1]\n",
      " [0 0 3]\n",
      " [0 2 4]]\n",
      "R2<->R3\n",
      "[[1 1 1]\n",
      " [0 2 4]\n",
      " [0 0 3]]\n",
      "The system is now triangular and the equations can be solved by Back substitution\n"
     ]
    }
   ],
   "source": [
    "from sympy.abc import u,v,w\n",
    "import numpy as np\n",
    "a =np.array([[1 ,1 ,1],[2, 2, 5],[4, 6, 8]])\n",
    "x=[[u],[v],[w]]\n",
    "x=np.array(x)\n",
    "print 'x=\\n',x\n",
    "print 'R2=R2-R1,R3=R3-4*R1'\n",
    "a[1,:]=a[1,:]-2*a[0,:]\n",
    "a[2,:]=a[2,:]-4*a[0,:]\n",
    "print a\n",
    "print 'R2<->R3'\n",
    "import numpy as np\n",
    "def swap_rows(arr, frm, to):\n",
    "    arr[[frm, to],:] = arr[[to, frm],:]\n",
    "swap_rows(a,1,2)\n",
    "print a\n",
    "print 'The system is now triangular and the equations can be solved by Back substitution'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:2 Pg:21"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a=\n",
      "[[1 1 1]\n",
      " [2 2 5]\n",
      " [4 4 8]]\n",
      "x=\n",
      "[[u]\n",
      " [v]\n",
      " [w]]\n",
      "R2=R2-2*R1,R3=R3-4*R1\n",
      "[[1 1 1]\n",
      " [0 0 3]\n",
      " [0 0 4]]\n",
      "No exchange of equations can avoid zero in the second pivot positon ,therefore the equations are unsolvable\n"
     ]
    }
   ],
   "source": [
    "from sympy.abc import u,v,w\n",
    "import numpy as np\n",
    "a =np.array([[1, 1, 1],[2, 2, 5],[4, 4, 8]])\n",
    "print 'a=\\n',a\n",
    "x=[[u],[v],[w]]\n",
    "x=np.array(x)\n",
    "print 'x=\\n',x\n",
    "print 'R2=R2-2*R1,R3=R3-4*R1'\n",
    "a[1,:]=a[1,:]-2*a[0,:]\n",
    "a[2,:]=a[2,:]-4*a[0,:]\n",
    "print a\n",
    "print 'No exchange of equations can avoid zero in the second pivot positon ,therefore the equations are unsolvable'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Ex:3 Pg:21"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A = \n",
      "[[2 3]\n",
      " [4 0]]\n",
      "B = \n",
      "[[ 1  2  0]\n",
      " [ 5 -1  0]]\n",
      "AB=\n",
      "[[17  1  0]\n",
      " [ 4  8  0]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "A=np.array([[2, 3],[4, 0]])\n",
    "print 'A = \\n',A\n",
    "B=np.array([[1, 2, 0],[5, -1, 0]])\n",
    "print 'B = \\n',B\n",
    "print 'AB=\\n',A.dot(B)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:4 Pg:22"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A=\n",
      "[[2 3]\n",
      " [7 8]]\n",
      "P(Row exchange matrix)=\n",
      "[[0 1]\n",
      " [1 0]]\n",
      "PA=\n",
      "[[7 8]\n",
      " [2 3]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "A=np.array([[2, 3],[7, 8]])\n",
    "print 'A=\\n',A\n",
    "P=np.array([[0, 1],[1, 0]])\n",
    "print 'P(Row exchange matrix)=\\n',P\n",
    "print 'PA=\\n',P.dot(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:5 Pg:24"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A=\n",
      "[[1 2]\n",
      " [3 4]]\n",
      "I=\n",
      "[[ 1.  0.]\n",
      " [ 0.  1.]]\n",
      "IA=\n",
      "[[ 1.  2.]\n",
      " [ 3.  4.]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "A=np.array([[1, 2],[3, 4]])\n",
    "print 'A=\\n',A\n",
    "I=np.identity(2)\n",
    "print 'I=\\n',I\n",
    "print 'IA=\\n',I.dot(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:6 Pg:25"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "E=\n",
      "[[ 1.  0.  0.]\n",
      " [-2.  1.  0.]\n",
      " [ 0.  0.  1.]]\n",
      "F=\n",
      "[[ 1.  0.  0.]\n",
      " [ 0.  1.  0.]\n",
      " [ 1.  0.  1.]]\n",
      "EF=\n",
      "[[ 1.  0.  0.]\n",
      " [-2.  1.  0.]\n",
      " [ 1.  0.  1.]]\n",
      "FE=\n",
      "[[ 1.  0.  0.]\n",
      " [-2.  1.  0.]\n",
      " [ 1.  0.  1.]]\n",
      "Here,EF=FE,so this shows that these two matrices commute\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "E=np.identity(3)\n",
    "E[1,:]=E[1,:]-2*E[0,:]\n",
    "print 'E=\\n',E\n",
    "F=np.identity(3)\n",
    "F[2,:]=F[2,:]+F[0,:]\n",
    "print 'F=\\n',F\n",
    "print 'EF=\\n',E.dot(F)\n",
    "print 'FE=\\n',F.dot(E)\n",
    "print 'Here,EF=FE,so this shows that these two matrices commute'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:7 Pg:25"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "E=\n",
      "[[ 1.  0.  0.]\n",
      " [-2.  1.  0.]\n",
      " [ 0.  0.  1.]]\n",
      "F=\n",
      "[[ 1.  0.  0.]\n",
      " [ 0.  1.  0.]\n",
      " [ 1.  0.  1.]]\n",
      "G=\n",
      "[[ 1.  0.  0.]\n",
      " [ 0.  1.  0.]\n",
      " [ 0.  1.  1.]]\n",
      "GE= [[ 1.  0.  0.]\n",
      " [-2.  1.  0.]\n",
      " [-2.  1.  1.]]\n",
      "EG=\n",
      "[[ 1.  0.  0.]\n",
      " [-2.  1.  0.]\n",
      " [ 0.  1.  1.]]\n",
      "Here EG is not equal to GE,Therefore these two matrices do not commute and shows that most matrices do not commute.\n",
      "GFE=\n",
      "[[ 1.  0.  0.]\n",
      " [-2.  1.  0.]\n",
      " [-1.  1.  1.]]\n",
      "EFG=\n",
      "[[ 1.  0.  0.]\n",
      " [-2.  1.  0.]\n",
      " [ 1.  1.  1.]]\n",
      "The product GFE is the true order of elimation.It is the matrix that takes the original A to the upper triangular U.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "E=np.identity(3)\n",
    "E[1,:]=E[1,:]-2*E[0,:]\n",
    "print 'E=\\n',E\n",
    "F=np.identity(3)\n",
    "F[2,:]=F[2,:]+F[0,:]\n",
    "print 'F=\\n',F\n",
    "G=np.identity(3)\n",
    "G[2,:]=G[2,:]+G[1,:]\n",
    "print 'G=\\n',G\n",
    "print 'GE=',G.dot(E)\n",
    "print 'EG=\\n',E.dot(G)\n",
    "print 'Here EG is not equal to GE,Therefore these two matrices do not commute and shows that most matrices do not commute.'\n",
    "print 'GFE=\\n',G.dot(F.dot(E))\n",
    "print 'EFG=\\n',E.dot(F.dot(G))\n",
    "print 'The product GFE is the true order of elimation.It is the matrix that takes the original A to the upper triangular U.'"
   ]
  }
 ],
 "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
}