{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 5 - Vectors & Matrices"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex5.2 Pg 103"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "u+v =  [[ 3 -2  4]]\n",
      "5*u =  [[ 10  15 -20]]\n",
      "-v =  [[-1  5 -8]]\n",
      "2*u-3*v =  [[  1  21 -32]]\n",
      "dot product of the two vectors, k = u.v =  [[-45]]\n",
      "norm or length of the vector u =  5.3852\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from numpy import mat\n",
    "from numpy.linalg import det\n",
    "from numpy import mat, add, dot, multiply, inner\n",
    "u=mat([[2,3,-4]])\n",
    "v=mat([[1,-5,8]])\n",
    "print \"u+v = \",add(u,v)\n",
    "print \"5*u = \",multiply(5,u)\n",
    "print \"-v = \",multiply(-1,v)\n",
    "print \"2*u-3*v = \",add(multiply(2,u),multiply(-3,v))\n",
    "print 'dot product of the two vectors, k = u.v = ',inner(u,v)\n",
    "from numpy.linalg import norm\n",
    "l=norm(u)# \n",
    "print 'norm or length of the vector u = ',round(l,4)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex5.3 Pg 104 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2*u-3*v =\n",
      "[[ 1]\n",
      " [ 9]\n",
      " [-2]]\n",
      "The dot product of the two vectors u and v is: [[20]]\n",
      "norm or length of the vector u =  7.0711\n"
     ]
    }
   ],
   "source": [
    "u=mat([[5],[3],[-4]])\n",
    "v=mat([[3],[-1],[-2]])\n",
    "print \"2*u-3*v =\\n\",add(multiply(2,u),multiply(-3,v))\n",
    "print 'The dot product of the two vectors u and v is:', sum(multiply(u,v))\n",
    "l=norm(u)#\n",
    "print 'norm or length of the vector u = ',round(l,4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex5.5 Pg 105"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The addition of the two matrices A and B is:\n",
      "[[ 5  4 11]\n",
      " [ 1  1 -2]]\n",
      "\n",
      "The multiplication of a vector with a scalar is:\n",
      "[[ 3 -6  9]\n",
      " [ 0 12 15]]\n",
      "\n",
      "2*A-3*B = \n",
      "[[-10 -22 -18]\n",
      " [ -3  17  31]]\n"
     ]
    }
   ],
   "source": [
    "A=mat([[1,-2,3],[0,4,5]])\n",
    "B=mat([[4,6,8],[1,-3,-7]])\n",
    "k=add(A,B)\n",
    "print 'The addition of the two matrices A and B is:\\n',k\n",
    "m=multiply(3,A)\n",
    "print '\\nThe multiplication of a vector with a scalar is:\\n',m\n",
    "p=add(multiply(2,A),multiply(-3,B))\n",
    "print \"\\n2*A-3*B = \\n\",p"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex5.6 Pg 106"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "product of a and b is :  [[8]]\n",
      "product of p and q is: [[32]]\n"
     ]
    }
   ],
   "source": [
    "a=mat([[7,-4,5]])\n",
    "b=mat([[3,2,-1]])\n",
    "k=inner(a,b)\n",
    "print 'product of a and b is : ',k\n",
    "p=mat([[6,-1,8,3]])\n",
    "q=mat([[4,-9,-2,5]])\n",
    "l=inner(p,q)\n",
    "print 'product of p and q is:',l"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex5.7 Pg 107"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A*B = \n",
      "[[ 17  -6  14]\n",
      " [ -1   2 -14]]\n",
      "A*B = \n",
      "[[ 5  2]\n",
      " [15 10]]\n",
      "B*A = \n",
      "[[23 34]\n",
      " [-6 -8]]\n",
      "matrix mulitplication is not commutative since AB may not be equal to BA\n"
     ]
    }
   ],
   "source": [
    "A=mat([[1 ,3],[2, -1]])\n",
    "B=mat([[2, 0, -4],[5, -2, 6]])\n",
    "print \"A*B = \\n\", dot(A,B)\n",
    "A=mat([[1, 2],[3, 4]])\n",
    "B=mat([[5, 6],[0, -2]])\n",
    "print \"A*B = \\n\",dot(A,B)\n",
    "print \"B*A = \\n\", dot(B,A)\n",
    "print 'matrix mulitplication is not commutative since AB may not be equal to BA'\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex5.8 Pg 109"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "for the function f(x)=2x**2-3x+5,f(A) is :\n",
      "[[ 16. -18.]\n",
      " [-27.  61.]]\n",
      "for the function g(x)=x**2+3x-10,g(A) is\n",
      "[[ 0.  0.]\n",
      " [ 0.  0.]]\n"
     ]
    }
   ],
   "source": [
    "from numpy import identity as idt\n",
    "A=mat([[1, 2],[3, -4]])\n",
    "A2=dot(A,A)  #multiplying A by itself\n",
    "A3=dot(A2,A)\n",
    "f=add(add(multiply(2,A2),multiply(-3,A)),multiply(5,idt(2)))\n",
    "print 'for the function f(x)=2x**2-3x+5,f(A) is :\\n',f\n",
    "g=add(add(A2,multiply(3,A)),multiply(-10,idt(2)))\n",
    "print 'for the function g(x)=x**2+3x-10,g(A) is\\n',g"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex5.9 Pg 110"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A*b = \n",
      "[[1 0 0]\n",
      " [0 1 0]\n",
      " [0 0 1]]\n",
      "since A*B is identity matrix,A and B are invertible and inverse of each other\n"
     ]
    }
   ],
   "source": [
    "A=mat([[1 ,0 ,2],[2 ,-1, 3],[4, 1, 8]])\n",
    "B=mat([[-11, 2 ,2],[-4, 0 ,1],[6, -1, -1]])\n",
    "print \"A*b = \\n\",dot(A,B)\n",
    "print 'since A*B is identity matrix,A and B are invertible and inverse of each other'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex5.10 Pg 111"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "determinant of A 7.0\n",
      "determinant of B 16.0\n",
      "determinant of C -81.0\n"
     ]
    }
   ],
   "source": [
    "A=mat([[5 ,4],[2, 3]])\n",
    "print 'determinant of A',det(A)\n",
    "B=mat([[2, 1],[-4, 6]])\n",
    "print 'determinant of B',det(B)\n",
    "C=mat([[2, 1, 3],[4, 6, -1],[5 ,1 ,0]])\n",
    "print 'determinant of C',det(C)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex5.13 Pg 115"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x =  [[ 2.]]\n",
      "y =  [[-1.]]\n",
      "z =  [[ 3.]]\n"
     ]
    }
   ],
   "source": [
    "A=mat([[1, 2, 1],[2, 5, -1],[3, -2, -1]])     #left hand side of the system of equations\n",
    "B=mat([[3] ,[-4] ,[5]])                   #right hand side or the constants in the equations\n",
    "from numpy import divide\n",
    "from numpy.linalg import solve\n",
    "X=divide(A,B) #   #unique solution for the system of equations\n",
    "X = solve(A, B)\n",
    "print \"x = \",X[0]\n",
    "print \"y = \",X[1]\n",
    "print \"z = \",X[2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex5.14 Pg 116"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Inverse of A = \n",
      "[[-11.   2.   2.]\n",
      " [ -4.   0.   1.]\n",
      " [  6.  -1.  -1.]]\n"
     ]
    }
   ],
   "source": [
    "A=mat([[1 ,0 ,2],[2, -1, 3],[4, 1, 8]])\n",
    "A_inv = A**-1\n",
    "print \"Inverse of A = \\n\", A_inv"
   ]
  }
 ],
 "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
}