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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
|
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Chapter 6 - Elementary canonical forms"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Page 184 Example 6.1"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Standard ordered matrix for Linear operator T on R**2 is:\n",
"A = \n",
"Matrix([[0, -1], [1, 0]])\n",
"The characteristic polynomial for T or A is: Matrix([[x, 1], [-1, x]])\n",
"Since this polynomial has no real roots,T has no characteristic values.\n"
]
}
],
"source": [
"from sympy import Symbol,Matrix,eye\n",
"print 'Standard ordered matrix for Linear operator T on R**2 is:'\n",
"A = Matrix(([0, -1],[1 ,0]))\n",
"print 'A = \\n',A\n",
"print 'The characteristic polynomial for T or A is:',\n",
"x = Symbol(\"x\")\n",
"p = (x*eye(2)-A)\n",
"print p\n",
"print 'Since this polynomial has no real roots,T has no characteristic values.'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Page 184 Example 6.2"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A = \n",
"Matrix([[3, 1, -1], [2, 2, -1], [2, 2, 0]])\n",
"Characteristic polynomial for A is: x**3 - 5*x**2 + 8*x - 4\n",
"or\n",
"(x-1)(x-2)**2\n",
"The characteristic values of A are:\n",
"[1, 2]\n",
"Now, A-I = \n",
"Matrix([[2, 1, -1], [2, 1, -1], [2, 2, -1]])\n",
"rank of A - I= 2\n",
"So, nullity of T-I = 1\n",
"The vector that spans the null space of T-I = [1, 0, 2]\n",
"Now,A-2I = \n",
"Matrix([[1, 1, -1], [2, 0, -1], [2, 2, -2]])\n",
"rank of A - 2I= 2\n",
"T*alpha = 2*alpha if alpha is a scalar multiple of a2\n",
"a2 = [1, 1, 2]\n"
]
}
],
"source": [
"from sympy import Symbol,Matrix,eye,solve\n",
"A = Matrix(([3, 1, -1],[ 2, 2, -1],[2, 2, 0]))\n",
"print 'A = \\n',A\n",
"print 'Characteristic polynomial for A is:',\n",
"x=Symbol('x')\n",
"p = A.charpoly(x)#\n",
"print p.as_expr()\n",
"print 'or'\n",
"print '(x-1)(x-2)**2'\n",
"\n",
"r = solve(p.as_expr())#\n",
"[m,n] = A.shape\n",
"print 'The characteristic values of A are:'\n",
"print r #print round(r)\n",
"B = A-eye(m)\n",
"print 'Now, A-I = \\n',B\n",
"\n",
"print 'rank of A - I= ',B.rank()\n",
"print 'So, nullity of T-I = 1'\n",
"a1 = [1 ,0 ,2]#\n",
"print 'The vector that spans the null space of T-I = ',a1\n",
"B = A-2*eye(m)\n",
"print 'Now,A-2I = \\n',B\n",
"print 'rank of A - 2I= ',B.rank()\n",
"print 'T*alpha = 2*alpha if alpha is a scalar multiple of a2'\n",
"a2 = [1 ,1 ,2]\n",
"print 'a2 = ',a2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Page 187 Example 6.3"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Standard ordered matrix for Linear operator T on R**3 is:\n",
"A = \n",
"Matrix([[5, -6, -6], [-1, 4, 2], [3, -6, -4]])\n",
"xI - A = \n",
"Matrix([[x - 5, 6, 6], [1, x - 4, -2], [-3, 6, x + 4]])\n",
"Applying row and column transformations:\n",
"C2 = C2 - C3\n",
"=>\n",
"Matrix([[x - 5, 0, 6], [1, x - 2, -2], [-3, -x + 2, x + 4]])\n",
"Taking (x-2) common from C2\n",
"=>\n",
" * x - 2\n",
"Matrix([[x - 5, 0, 6], [1, 1, -2], [-3, (-x + 2)/(x - 2), x + 4]])\n",
"R3 = R3 + R2\n",
"=>\n",
" * x - 2\n",
"Matrix([[x - 5, 0, 6], [1, 1, -2], [-2, (-x + 2)/(x - 2) + 1, x + 2]])\n",
"=>\n",
" * x - 2\n",
"Matrix([[x - 5, 6], [-2, x + 2]])\n",
"=>\n",
" * x - 2\n",
"x**2 - 3*x + 2\n",
"This is the characteristic polynomial\n",
"Now, A - I = Matrix([[4, -6, -6], [-1, 3, 2], [3, -6, -5]])\n",
"And, A- 2I = Matrix([[3, -6, -6], [-1, 2, 2], [3, -6, -6]])\n",
"rank(A-I) = 2\n",
"rank(A-2I) = 2\n",
"W1,W2 be the spaces of characteristic vectors associated with values 1,2\n",
"So by theorem 2, T is diagonalizable\n",
"Null space of (T- I) i.e basis of W1 is spanned by a1 = [[ 3 -1 3]]\n",
"Null space of (T- 2I) i.e. basis of W2 is spanned by vectors x1,x2,x3 such that x1 = 2x1 + 2x3\n",
"One example :\n",
"a2 = [[2 1 0]]\n",
"a3 = [[2 0 1]]\n",
"The diagonal matrix is:\n",
"D = [[1 0 0]\n",
" [0 2 0]\n",
" [0 0 2]]\n",
"The standard basis matrix is denoted as:\n",
"P = [[ 3 2 2]\n",
" [-1 1 0]\n",
" [ 3 0 1]]\n",
"AP = Matrix([[3, 4, 4], [-1, 2, 0], [3, 0, 2]])\n",
"PD = [[3 0 0]\n",
" [0 2 0]\n",
" [0 0 2]]\n",
"That is, AP = PD\n",
"=> inverse(P)*A*P = D\n"
]
}
],
"source": [
"from numpy import array,transpose,vstack,rank\n",
"from sympy import Symbol,Matrix,eye\n",
"print 'Standard ordered matrix for Linear operator T on R**3 is:'\n",
"A = Matrix(([5, -6, -6],[ -1, 4, 2],[ 3, -6, -4]))\n",
"print 'A = \\n',A\n",
"print 'xI - A = '\n",
"B = eye(3)\n",
"x = Symbol('x')\n",
"P = x*B - A#\n",
"print P\n",
"\n",
"print 'Applying row and column transformations:'\n",
"print 'C2 = C2 - C3'\n",
"P[:,1] = P[:,1] - P[:,2]\n",
"print '=>'\n",
"print P\n",
"print 'Taking (x-2) common from C2'\n",
"c = x-2#\n",
"P[:,1] = P[:,1] / (x-2)\n",
"print '=>'\n",
"print ' * ', c\n",
"print P\n",
"print 'R3 = R3 + R2'\n",
"P[2,:] = P[2,:] + P[1,:]\n",
"print '=>'\n",
"print ' * ', c\n",
"print P\n",
"P = Matrix(([P[0,0], P[0,2]],[P[2,0], P[2,2]]))\n",
"print '=>'\n",
"print ' * ', c\n",
"print P\n",
"print '=>'\n",
"print ' * ',c\n",
"print P.det()\n",
"print 'This is the characteristic polynomial'\n",
"\n",
"print 'Now, A - I = ',A-B\n",
"print 'And, A- 2I = ',A-2*B\n",
"print 'rank(A-I) = ',rank(A-B)\n",
"\n",
"print 'rank(A-2I) = ',rank(A-2*B)\n",
"print 'W1,W2 be the spaces of characteristic vectors associated with values 1,2'\n",
"print 'So by theorem 2, T is diagonalizable'\n",
"a1 = array([[3, -1 ,3]])\n",
"a2 = array([[2, 1, 0]])\n",
"a3 = array([[2, 0, 1]])\n",
"print 'Null space of (T- I) i.e basis of W1 is spanned by a1 = ',a1\n",
"print 'Null space of (T- 2I) i.e. basis of W2 is spanned by vectors x1,x2,x3 such that x1 = 2x1 + 2x3'\n",
"print 'One example :'\n",
"print 'a2 = ',a2\n",
"print 'a3 = ',a3\n",
"print 'The diagonal matrix is:'\n",
"D = array([[1 ,0 ,0 ],[0, 2, 0],[0, 0, 2]])\n",
"print 'D = ',D\n",
"print 'The standard basis matrix is denoted as:'\n",
"P = transpose(vstack([a1,a2,a3]))\n",
"print 'P = ',P\n",
"print 'AP = ',A*P\n",
"print 'PD = ',P*D\n",
"print 'That is, AP = PD'\n",
"print '=> inverse(P)*A*P = D'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Page 193 Example 6.4"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A = \n",
"[[ 5 -6 -6]\n",
" [-1 4 2]\n",
" [ 3 -6 -4]]\n",
"Characteristic polynomial of A is:\n",
"f = (x-1)(x-2)**2\n",
"i.e., f = (x - 2)**2*(x - 1)\n",
"(A-I)(A-2I) = Matrix([[0, 0, 0], [0, 0, 0], [0, 0, 0]])\n",
"Since, (A-I)(A-2I) = 0. So, Minimal polynomial for above is:\n",
"p = (x - 2)*(x - 1)\n",
"---------------------------------------\n",
"A = \n",
"[[ 3 1 -1]\n",
" [ 2 2 -1]\n",
" [ 2 2 0]]\n",
"Characteristic polynomial of A is:\n",
"f = (x-1)(x-2)**2\n",
"i.e., f = (x - 2)**2*(x - 1)\n",
"(A-I)(A-2I) = Matrix([[2, 0, -1], [2, 0, -1], [4, 0, -2]])\n",
"Since, (A-I)(A-2I) is not equal to 0. T is not diagonalizable. So, Minimal polynomial cannot be p.\n",
"---------------------------------------\n",
"A = \n",
"[[ 0 -1]\n",
" [ 1 0]]\n",
"Characteristic polynomial of A is:\n",
"f = x**2 + 1\n",
"A**2 + I = Matrix([[1, 1], [1, 1]])\n",
"Since, A**2 + I = 0, so minimal polynomial is\n",
"p = x**2 + 1\n"
]
}
],
"source": [
"from numpy import array,transpose,vstack,rank\n",
"from sympy import Symbol,Matrix,eye\n",
"\n",
"x = Symbol(\"x\")\n",
"A = array([[5, -6, -6],[ -1, 4 ,2],[ 3, -6, -4]]) #Matrix given in Example 3\n",
"print 'A = \\n',A\n",
"f = (x-1)*(x-2)**2# \n",
"print 'Characteristic polynomial of A is:'\n",
"print 'f = (x-1)(x-2)**2'\n",
"print 'i.e., f = ',f\n",
"p = (x-1)*(x-2)#\n",
"print '(A-I)(A-2I) = ',(A-eye(3))*(A-2 * eye(3))\n",
"print 'Since, (A-I)(A-2I) = 0. So, Minimal polynomial for above is:'\n",
"print 'p = ',p\n",
"print '---------------------------------------'\n",
"\n",
"A = array([[3, 1 ,-1],[ 2, 2 ,-1],[2, 2, 0]]) #Matrix given in Example 2\n",
"print 'A = \\n',A\n",
"f = (x-1)*(x-2)**2# \n",
"print 'Characteristic polynomial of A is:'\n",
"print 'f = (x-1)(x-2)**2'\n",
"print 'i.e., f = ',f\n",
"print '(A-I)(A-2I) = ',(A-eye(3))*(A-2 * eye(3))\n",
"print 'Since, (A-I)(A-2I) is not equal to 0. T is not diagonalizable. So, Minimal polynomial cannot be p.'\n",
"print '---------------------------------------'\n",
"A = array([[0, -1],[1, 0]])\n",
"print 'A = \\n',A\n",
"f = x**2 + 1#\n",
"print 'Characteristic polynomial of A is:'\n",
"print 'f = ',f\n",
"print 'A**2 + I = ',A**2 + eye(2)\n",
"print 'Since, A**2 + I = 0, so minimal polynomial is'\n",
"p = x**2 + 1\n",
"print 'p = ',p"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Page 197 Example 6.5"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" A = \n",
"[[0 1 0 1]\n",
" [1 0 1 0]\n",
" [0 1 0 1]\n",
" [1 0 1 0]]\n",
"Computing powers on A:\n",
"A**2 = \n",
"[[0 1 0 1]\n",
" [1 0 1 0]\n",
" [0 1 0 1]\n",
" [1 0 1 0]]\n",
"A**3 = \n",
"[[0 1 0 1]\n",
" [1 0 1 0]\n",
" [0 1 0 1]\n",
" [1 0 1 0]]\n",
"if p = x**3 - 4x, then\n",
"p(A) = [[ 0 -3 0 -3]\n",
" [-3 0 -3 0]\n",
" [ 0 -3 0 -3]\n",
" [-3 0 -3 0]]\n",
"Minimal polynomial for A is: x**3 - 4*x\n",
"Characteristic values for A are: [-2, 0, 2]\n",
"Rank(A) = 2\n",
"So, from theorem 2, characteristic polynomial for A is: x**4 - 4*x**2\n"
]
}
],
"source": [
"from numpy import array,transpose,vstack,rank\n",
"from sympy import Symbol,Matrix,eye,solve\n",
"A = array([[0, 1, 0, 1],[1, 0 ,1 ,0],[0, 1, 0, 1],[1, 0, 1, 0]])\n",
"print 'A = \\n',A\n",
"print 'Computing powers on A:'\n",
"print 'A**2 = \\n',A*A\n",
"print 'A**3 = \\n',A*A*A\n",
"def p(x):\n",
" pp = x**3 - 4*x\n",
" return pp\n",
"print 'if p = x**3 - 4x, then'\n",
"print 'p(A) = ',p(A)\n",
"x = Symbol(\"x\")\n",
"f = x**3 - 4*x\n",
"print 'Minimal polynomial for A is: ',f\n",
"print 'Characteristic values for A are:',solve(f,x)\n",
"print 'Rank(A) = ',rank(A)\n",
"print 'So, from theorem 2, characteristic polynomial for A is:',Matrix(A).charpoly(x).as_expr()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Page 210 Example 6.12"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A = \n",
"[[ 9. 3. 3.]\n",
" [ 7. 4. 4.]\n",
" [ 1. 1. 2.]]\n",
"A transpose is:\n",
"A' = \n",
"[[ 9. 7. 1.]\n",
" [ 3. 4. 1.]\n",
" [ 3. 4. 2.]]\n",
"Since, A' is not equal to A, A is not a symmetric matrix.\n",
"Since, A' is not equal to -A, A is not a skew-symmetric matrix.\n",
"A can be expressed as sum of A1 and A2\n",
"i.e., A = A1 + A2\n",
"A1 = \n",
"[[ 9. 5. 2. ]\n",
" [ 5. 4. 2.5]\n",
" [ 2. 2.5 2. ]]\n",
"A2 = \n",
"[[ 0. -2. 1. ]\n",
" [ 2. 0. 1.5]\n",
" [-1. -1.5 0. ]]\n",
"A1 + A2 = \n",
"[[ 9. 3. 3.]\n",
" [ 7. 4. 4.]\n",
" [ 1. 1. 2.]]\n"
]
}
],
"source": [
"from numpy import array,transpose,random,equal\n",
"\n",
"A = random.rand(3,3)\n",
"for i in range(0,3):\n",
" for j in range(0,3):\n",
" A[i,j]=round(A[i,j]*10)\n",
" \n",
"print 'A = \\n',A\n",
"print 'A transpose is:\\n',\n",
"Adash=transpose(A)\n",
"print \"A' = \\n\",Adash\n",
"if equal(Adash,A).all():\n",
" print \"Since, A' = A, A is a symmetric matrix.\"\n",
"else:\n",
" print \"Since, A' is not equal to A, A is not a symmetric matrix.\"\n",
"\n",
"if equal(Adash,-A).all():\n",
" print \"Since, A' = -A, A is a skew-symmetric matrix.\"\n",
"else:\n",
" print \"Since, A' is not equal to -A, A is not a skew-symmetric matrix.\"\n",
"\n",
"A1 = 1./2*(A + Adash)\n",
"A2 = 1./2*(A - Adash)\n",
"print 'A can be expressed as sum of A1 and A2'\n",
"print 'i.e., A = A1 + A2'\n",
"print 'A1 = \\n',A1\n",
"print 'A2 = \\n',A2\n",
"print 'A1 + A2 = \\n',A1 + A2"
]
}
],
"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
}
|