summaryrefslogtreecommitdiff
path: root/Linear_Algebra_by_K._Hoffman_and_R._Kunze/Chapter5.ipynb
blob: 9d29340ccc3dc48769c326e6da8825664e84c805 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
{
 "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
}