{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 5 - Determinants"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page 143 Example 5.3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A = \n",
      "[[ 3.  9.]\n",
      " [ 3.  1.]]\n",
      "D1(A) =  3.0\n",
      "D2(A) =  -27.0\n",
      "D(A) = D1(A) + D2(A) =  -24.0\n",
      "That is, D is a 2-linear function.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "A = np.random.rand(2,2)\n",
    "for x in range(0,2):\n",
    "    for y in range(0,2):\n",
    "        A[x,y]=round(A[x,y]*10)\n",
    "print 'A = \\n',A\n",
    "\n",
    "D1 = A[0,0]*A[1,1]\n",
    "D2 = - A[0,1]*A[1,0]\n",
    "print 'D1(A) = ',D1\n",
    "print 'D2(A) = ',D2\n",
    "print 'D(A) = D1(A) + D2(A) = ',D1 + D2\n",
    "print 'That is, D is a 2-linear function.'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page 145 Example 5.4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A = \n",
      "Matrix([[x, 0, -x**2], [0, 1, 0], [1, 0, x**3]])\n",
      "e1,e2,e3 are the rows of 3*3 identity matrix, then\n",
      "e1 =  [ 1.  0.  0.]\n",
      "e2 =  [ 0.  1.  0.]\n",
      "e3 =  [ 0.  0.  1.]\n",
      "D(A) = D(x*e1 - x**2*e3, e2, e1 + x**3*e3)\n",
      "Since, D is linear as a function of each row,\n",
      "D(A) = x*D(e1,e2,e1 + x**3*e3) - x**2*D(e3,e2,e1 + x**3*e3)\n",
      "D(A) = x*D(e1,e2,e1) + x**4*D(e1,e2,e3) - x**2*D(e3,e2,e1) - x**5*D(e3,e2,e3)\n",
      "As D is alternating, So\n",
      "D(A) = (x**4 + x**2)*D(e1,e2,e3)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "\n",
    "x = sp.Symbol(\"x\")\n",
    "A = sp.Matrix(([x, 0, -x**2],[0, 1, 0],[1, 0, x**3]))\n",
    "print 'A = \\n',A\n",
    "print 'e1,e2,e3 are the rows of 3*3 identity matrix, then'\n",
    "T = np.identity(3)\n",
    "e1 = T[0,:]\n",
    "e2 = T[1,:]\n",
    "e3 = T[2,:]\n",
    "print 'e1 = ',e1\n",
    "print 'e2 = ',e2\n",
    "print 'e3 = ',e3\n",
    "print 'D(A) = D(x*e1 - x**2*e3, e2, e1 + x**3*e3)'\n",
    "print 'Since, D is linear as a function of each row,'\n",
    "print 'D(A) = x*D(e1,e2,e1 + x**3*e3) - x**2*D(e3,e2,e1 + x**3*e3)'\n",
    "print 'D(A) = x*D(e1,e2,e1) + x**4*D(e1,e2,e3) - x**2*D(e3,e2,e1) - x**5*D(e3,e2,e3)'\n",
    "print 'As D is alternating, So'\n",
    "print 'D(A) = (x**4 + x**2)*D(e1,e2,e3)'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page 147 Example 5.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A = \n",
      "Matrix([[x - 1, x**2, x**3], [0, x - 2, 1], [0, 0, x - 3]])\n",
      "\n",
      "E(A) =  x**3 - 6*x**2 + 11*x - 6\n",
      "--------------------------------------\n",
      "A = \n",
      "Matrix([[0, 1, 0], [0, 0, 1], [1, 0, 0]])\n",
      "\n",
      "E(A) =  1\n"
     ]
    }
   ],
   "source": [
    "import sympy as sp\n",
    "\n",
    "#part a\n",
    "x = sp.Symbol('x')\n",
    "A = sp.Matrix(([x-1, x**2, x**3],[0, x-2, 1],[0, 0, x-3]))\n",
    "print 'A = \\n',A\n",
    "print '\\nE(A) = ',A.det()\n",
    "print '--------------------------------------'\n",
    "#part b\n",
    "A = sp.Matrix(([0 ,1, 0],[0, 0, 1],[1 ,0, 0]))\n",
    "print 'A = \\n',A\n",
    "print '\\nE(A) = ',A.det()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page 158 Example 5.6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Given Matrix:\n",
      "A = \n",
      "[[ 1 -1  2  3]\n",
      " [ 2  2  0  2]\n",
      " [ 4  1 -1 -1]\n",
      " [ 1  2  3  0]]\n",
      "After, Subtracting muliples of row 1 from rows 2 3 4\n",
      "R2 = R2 - 2*R1\n",
      "R3 = R3 - 4*R1\n",
      "R4 = R4 - R1\n",
      "A = \n",
      "[[  1  -1   2   3]\n",
      " [  0   4  -4  -4]\n",
      " [  0   5  -9 -13]\n",
      " [  0   3   1  -3]]\n",
      "We obtain the same determinant as before.\n",
      "Now, applying some more row transformations as:\n",
      "R3 = R3 - 5/4 * R2\n",
      "R4 = R4 - 3/4 * R2\n",
      "We get B as:\n",
      "B = \n",
      "[[ 1 -1  2  3]\n",
      " [ 0  4 -4 -4]\n",
      " [ 0  0 -4 -8]\n",
      " [ 0  0  4  0]]\n",
      "Now,determinant of A and B will be same\n",
      "det A = det B =  128.0\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "print 'Given Matrix:'\n",
    "A = np.array([[1, -1, 2, 3],[ 2, 2, 0, 2],[ 4, 1 ,-1, -1],[1, 2, 3, 0]])\n",
    "print 'A = \\n',A\n",
    "print 'After, Subtracting muliples of row 1 from rows 2 3 4'\n",
    "print 'R2 = R2 - 2*R1'\n",
    "A[1,:] = A[1,:] - 2 * A[0,:]\n",
    "print 'R3 = R3 - 4*R1'\n",
    "A[2,:] = A[2,:] - 4 * A[0,:]\n",
    "print 'R4 = R4 - R1'\n",
    "A[3,:] = A[3,:] - A[0,:]\n",
    "print 'A = \\n',A\n",
    "T = A#                  #Temporary matrix to store A\n",
    "print 'We obtain the same determinant as before.'\n",
    "print 'Now, applying some more row transformations as:'\n",
    "print 'R3 = R3 - 5/4 * R2'\n",
    "T[2,:] = T[2,:] - 5./4 * T[1,:]\n",
    "print 'R4 = R4 - 3/4 * R2'\n",
    "T[3,:] = T[3,:] - 3./4 * T[1,:]\n",
    "B = T#\n",
    "print 'We get B as:'\n",
    "print 'B = \\n',B\n",
    "print 'Now,determinant of A and B will be same'\n",
    "print 'det A = det B = ',np.linalg.det(B)\n",
    "     "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page 160 Example 5.7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A = \n",
      "Matrix([[x**2 + x, x + 1], [x - 1, 1]])\n",
      "B = \n",
      "Matrix([[x**2 - 1, x + 2], [x**2 - 2*x + 3, x]])\n",
      "det A =  x + 1\n",
      "det B =  -6\n",
      "Thus, A is not invertible over K whereas B is invertible\n",
      "adj A =  Matrix([[(x + 1)*((x - 1)/(x*(x + 1)) + 1/(x*(x + 1))), -x - 1], [-x + 1, x*(x + 1)]])\n",
      "adj B =  Matrix([[-6*(x + 2)*(-x**2/6 + 1/6)*(x**2 - 2*x + 3)/(x**2 - 1)**2 - 6/(x**2 - 1), 6*(x + 2)*(-x**2/6 + 1/6)/(x**2 - 1)], [6*(-x**2/6 + 1/6)*(x**2 - 2*x + 3)/(x**2 - 1), x**2 - 1]])\n",
      "(adj A)A = (x+1)I\n",
      "(adj B)B =  -6I\n",
      "B inverse =  Matrix([[(x + 2)*(-x**2/6 + 1/6)*(x**2 - 2*x + 3)/(x**2 - 1)**2 + 1/(x**2 - 1), -(x + 2)*(-x**2/6 + 1/6)/(x**2 - 1)], [-(-x**2/6 + 1/6)*(x**2 - 2*x + 3)/(x**2 - 1), -x**2/6 + 1/6]])\n"
     ]
    }
   ],
   "source": [
    "import sympy as sp\n",
    "import numpy as np\n",
    "\n",
    "x = sp.Symbol(\"x\")\n",
    "A = sp.Matrix(([x**2+x, x+1],[x-1, 1]))\n",
    "B = sp.Matrix(([x**2-1, x+2],[x**2-2*x+3, x]))\n",
    "print 'A = \\n',A\n",
    "print 'B = \\n',B\n",
    "print 'det A = ',A.det()\n",
    "print 'det B = ',B.det()\n",
    "print 'Thus, A is not invertible over K whereas B is invertible'\n",
    "\n",
    "print 'adj A = ',(A**-1)*A.det()\n",
    "print 'adj B = ',(B**-1)*B.det()\n",
    "print '(adj A)A = (x+1)I'\n",
    "print '(adj B)B =  -6I'\n",
    "print 'B inverse = ',B**-1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page 161 Example 5.8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A = \n",
      "[[1 2]\n",
      " [3 4]]\n",
      "det A =  Determinant of A is: -2.0\n",
      "Adjoint of A is: [[-2.  -0. ]\n",
      " [-0.  -0.5]]\n",
      "Thus, A is not invertible as a matrix over the ring of integers.\n",
      "But, A can be regarded as a matrix over field of rational numbers.\n",
      "Then, A is invertible and Inverse of A is: [[-2.   1. ]\n",
      " [ 1.5 -0.5]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "A = np.array([[1, 2],[3, 4]])\n",
    "print 'A = \\n',A\n",
    "d = np.linalg.det(A)#\n",
    "print 'det A = ','Determinant of A is:',d\n",
    "\n",
    "\n",
    "ad = (d* np.identity(2)) / A\n",
    "print 'Adjoint of A is:',ad\n",
    "\n",
    "\n",
    "print 'Thus, A is not invertible as a matrix over the ring of integers.'\n",
    "print 'But, A can be regarded as a matrix over field of rational numbers.'\n",
    "In = np.linalg.inv(A)#\n",
    "#The A inverse matrix given in book has a wrong entry of 1/2. It should be -1/2.\n",
    "print 'Then, A is invertible and Inverse of A is:',In\n"
   ]
  }
 ],
 "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
}