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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Chapter3 Orthogonality"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Ex:3.1.1 Pg: 143"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x1=\n",
"[[ 2]\n",
" [ 2]\n",
" [-1]]\n",
"x2=\n",
"[[-1]\n",
" [ 2]\n",
" [ 2]]\n",
"x1'*x2=\n",
"[[0]]\n",
"Therefore,X1 is orthogonal to x2 .Both have length of sqrt(14).\n"
]
}
],
"source": [
"from numpy import mat,transpose\n",
"x1=mat([[2],[2],[-1]])\n",
"print 'x1=\\n',x1\n",
"x2=mat([[-1],[2],[2]])\n",
"print 'x2=\\n',x2\n",
"print \"x1'*x2=\\n\",(transpose(x1)*x2)\n",
"print 'Therefore,X1 is orthogonal to x2 .Both have length of sqrt(14).'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Ex:3.1.3 Pg: 145"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A=\n",
"[[1 3]\n",
" [2 6]\n",
" [3 9]]\n",
"Null space=\n",
"[[-0.9486833 ]\n",
" [ 0.31622777]]\n",
"A(1,:)*ns= [[ -2.22044605e-16]]\n",
"A(2,:)*ns= [[ -4.44089210e-16]]\n",
"A(3,:)*ns= [[ -4.44089210e-16]]\n",
"This shows that the null space of A is orthogonal to the row space.\n"
]
}
],
"source": [
"from numpy import mat,linalg,atleast_2d\n",
"A=mat([[1, 3],[2, 6],[3, 9]])\n",
"print 'A=\\n',A\n",
"def nullspace(A, atol=1e-13, rtol=0):\n",
" \n",
" A = atleast_2d(A)\n",
" u, s, vh = linalg.svd(A)\n",
" tol = max(atol, rtol * s[0])\n",
" nnz = (s >= tol).sum()\n",
" ns = vh[nnz:].conj().T\n",
" return ns\n",
"ns=nullspace(A)\n",
"print 'Null space=\\n',ns\n",
"print 'A(1,:)*ns=',(A[0]*ns)\n",
"print 'A(2,:)*ns=',(A[1]*ns)\n",
"print 'A(3,:)*ns=',(A[2]*ns)\n",
"print 'This shows that the null space of A is orthogonal to the row space.'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## Ex:3.2.1 Pg: 155"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"b=\n",
"[[1]\n",
" [2]\n",
" [3]]\n",
"a=\n",
"[[1]\n",
" [1]\n",
" [1]]\n",
"Projection p of b onto the line through a is x***a=\n",
"[[2]\n",
" [2]\n",
" [2]]\n",
"cos(thetha) = 0.925820099773\n"
]
}
],
"source": [
"from numpy import mat,transpose,sqrt,transpose\n",
"b=mat([[1],[2],[3]])\n",
"print 'b=\\n',b\n",
"a=mat([[1],[1],[1]])\n",
"print 'a=\\n',a\n",
"x=(transpose(a)*b)/(transpose(a)*a)\n",
"x=x[0,0]\n",
"print 'Projection p of b onto the line through a is x***a=\\n',(x*a)\n",
"cos_theta=(transpose(a)*b)[0,0]/(sqrt(transpose(a)*a)[0,0]*sqrt(transpose(b)*b)[0,0])\n",
"print 'cos(thetha) =',cos_theta"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Ex:3.2.2 Pg: 156"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a=\n",
"[[ 1.]\n",
" [ 1.]\n",
" [ 1.]]\n",
"Matrix that projects onto a line through a=(1,1,1) is\n",
"[[ 0.33333333 0.33333333 0.33333333]\n",
" [ 0.33333333 0.33333333 0.33333333]\n",
" [ 0.33333333 0.33333333 0.33333333]]\n"
]
}
],
"source": [
"from numpy import mat,transpose\n",
"a=mat([[1.],[1],[1]])\n",
"print 'a=\\n',a\n",
"P=(a*transpose(a))/(transpose(a)*a)[0,0]\n",
"print 'Matrix that projects onto a line through a=(1,1,1) is\\n',P"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Ex:3.2.3 Pg: 156"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a=\n",
"[[ 0.70710678]\n",
" [ 0.70710678]]\n",
"Projection of line onto the thetha-direction(thetha taken as 45) in the x-y plane passing through a is\n",
"[[ 0.5 0.5]\n",
" [ 0.5 0.5]]\n"
]
}
],
"source": [
"from numpy import mat,transpose,sin,pi,cos\n",
"thetha=45# #Taking some value for thetha\n",
"a=mat([[cos(thetha*pi/180)],[sin(thetha*pi/180)]])\n",
"print 'a=\\n',a\n",
"P=(a*transpose(a))/(transpose(a)*a)\n",
"print 'Projection of line onto the thetha-direction(thetha taken as 45) in the x-y plane passing through a is\\n',P"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Ex:3.3.1 Pg: 165"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A=\n",
"[[ 0.09246118 0.04909557 0.71167391 0.77035248]\n",
" [ 0.4079182 0.15730679 0.91259156 0.21064709]\n",
" [ 0.02898159 0.87211114 0.11852139 0.69009963]\n",
" [ 0.16889615 0.35412933 0.60067694 0.1180308 ]]\n",
"P=A*inv(A*A)*A\n",
"Projection of a invertible 4x4 matrix on to the whole space is:\n",
"[[ 3.38771356e-02 -8.74775251e-02 -1.51348916e-02 1.06873528e+00]\n",
" [ -8.74775251e-02 1.39664206e-03 1.08138671e+00 4.69417392e-03]\n",
" [ -1.51348916e-02 1.08138671e+00 -1.24018336e-04 -6.61277992e-02]\n",
" [ 1.06873528e+00 4.69417392e-03 -6.61277992e-02 -7.30165590e-03]]\n",
"Its identity matrix.\n"
]
}
],
"source": [
"from numpy import mat,transpose,linalg,random\n",
"A=random.rand(4,4)\n",
"print 'A=\\n',A\n",
"P=A*linalg.inv(transpose(A)*A)*transpose(A)\n",
"print 'P=A*inv(A''*A)*A'\n",
"print 'Projection of a invertible 4x4 matrix on to the whole space is:\\n',P\n",
"print 'Its identity matrix.'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Ex:3.3.2 Pg: 166"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"b=C+Dt\n",
"Ax=b\n",
"A=\n",
"[[ 1 -1]\n",
" [ 1 1]\n",
" [ 1 2]]\n",
"b=\n",
"[[1]\n",
" [1]\n",
" [3]]\n",
"If Ax=b could be solved then they would be no errors, they cant be solved because the points are not on a line.Therefore they are solved by least squares.\n",
"so,AAx**=Ab\n",
"C** = 1.28571428571\n",
"D**= 0.571428571429\n",
"The best line is 9/7+4/7t\n"
]
}
],
"source": [
"from numpy import mat,transpose,zeros,linalg\n",
"print 'b=C+Dt'\n",
"print 'Ax=b'\n",
"A=mat([[1, -1],[1, 1],[1, 2]])\n",
"print 'A=\\n',A\n",
"b=mat([[1],[1],[3]])\n",
"print 'b=\\n',b\n",
"print 'If Ax=b could be solved then they would be no errors, they can''t be solved because the points are not on a line.Therefore they are solved by least squares.'\n",
"print 'so,A''Ax**=A''b'\n",
"x=zeros([1,2])\n",
"x=linalg.solve((transpose(A)*A), (transpose(A)*b))\n",
"print 'C** =',x[0,0]\n",
"print 'D**=',x[1,0]\n",
"print 'The best line is 9/7+4/7t'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Ex:3.4.1 Pg: 175"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Q=\n",
"[[ 0.70710678 -0.70710678]\n",
" [ 0.70710678 0.70710678]]\n",
"\n",
"Q'=inv(Q)=\n",
"[[ 0.70710678 0.70710678]\n",
" [-0.70710678 0.70710678]]\n",
"\n",
"Q rotates every vector through an angle thetha, and Q rotates it back through -thetha.The columns are clearly orthogonal and they are orthonormal because sin**2(theta)+cos**2(thetha)=1.\n"
]
}
],
"source": [
"from numpy import mat,transpose,sin,pi,cos\n",
"thetha=45##Taking some value for thetha.\n",
"Q=mat([[cos(pi/180*thetha),-sin(thetha*pi/180)],[sin(pi/180*thetha),cos(pi/180*thetha)]])\n",
"print 'Q=\\n',Q\n",
"print \"\\nQ'=inv(Q)=\\n\",transpose(Q)\n",
"print '\\nQ rotates every vector through an angle thetha, and Q'' rotates it back through -thetha.The columns are clearly orthogonal and they are orthonormal because sin**2(theta)+cos**2(thetha)=1.'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Ex:3.4.2 Pg: 175"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Any permutation matrix is an orthogonal matrix.The columns are certainly unit vectors and certainly orthogonal-because the 1 appears in a differnt place in each column\n",
"P=\n",
"[[0 1 0]\n",
" [0 0 1]\n",
" [1 0 0]]\n",
"inv(P)=P'=\n",
"[[0 0 1]\n",
" [1 0 0]\n",
" [0 1 0]]\n",
"And,P'*P=\n",
"[[1 0 0]\n",
" [0 1 0]\n",
" [0 0 1]]\n"
]
}
],
"source": [
"from numpy import mat,transpose\n",
"print 'Any permutation matrix is an orthogonal matrix.The columns are certainly unit vectors and certainly orthogonal-because the 1 appears in a differnt place in each column'\n",
"P=mat([[0, 1, 0],[0, 0 ,1],[1, 0, 0]])\n",
"print 'P=\\n',P\n",
"print \"inv(P)=P'=\\n\",transpose(P)\n",
"print \"And,P'*P=\\n\",(transpose(P)*P)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Ex:3.4.3 Pg: 175"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"If we project b=(x,y,z) onto the x-y plane then its projection is p=(x,y,0),and is the sum of projection onto x- any y-axes.\n",
"q1=\n",
"[[1]\n",
" [0]\n",
" [0]]\n",
"q2=\n",
"[[0]\n",
" [1]\n",
" [0]]\n",
"Overall projection matrix,P=\n",
"[[1 0 0]\n",
" [0 1 0]\n",
" [0 0 0]]\n",
"and,P[x#y#z]=[x#y#0]\n",
"Projection onto a plane=sum of projections onto orthonormal q1 and q2.\n"
]
}
],
"source": [
"from numpy import mat,transpose,random\n",
"print 'If we project b=(x,y,z) onto the x-y plane then its projection is p=(x,y,0),and is the sum of projection onto x- any y-axes.'\n",
"b=random.rand(3,1)\n",
"q1=mat([[1],[0],[0]])\n",
"print 'q1=\\n',q1\n",
"q2=mat([[0],[1],[0]])\n",
"print 'q2=\\n',q2\n",
"P=q1*transpose(q1)+q2*transpose(q2)\n",
"print 'Overall projection matrix,P=\\n',P\n",
"print 'and,P[x#y#z]=[x#y#0]'\n",
"print 'Projection onto a plane=sum of projections onto orthonormal q1 and q2.'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Ex:3.4.4 Pg: 166"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"y=C+Dt\n",
"Ax=b\n",
"A=\n",
"[[ 1 -3]\n",
" [ 1 0]\n",
" [ 1 3]]\n",
"y=\n",
"[[ 0.72089857]\n",
" [ 0.89298883]\n",
" [ 0.60457288]]\n",
"the columns of A are orthogonal,so\n",
"C** =\n",
"[[ 0.12324779]]\n",
"D** =\n",
"[[-0.12014976 0. 0.12014976]\n",
" [-0.14883147 0. 0.14883147]\n",
" [-0.10076215 0. 0.10076215]]\n",
"C** gives the besy fit ny horizontal line, whereas D**t is the best fit by a straight line through the origin.\n"
]
}
],
"source": [
"from numpy import mat,transpose,random,zeros\n",
"print 'y=C+Dt'\n",
"print 'Ax=b'\n",
"A=mat([[1, -3],[1, 0],[1, 3]])\n",
"print 'A=\\n',A\n",
"y=random.rand(3,1)\n",
"print 'y=\\n',y\n",
"print 'the columns of A are orthogonal,so'\n",
"x=zeros([1,2])\n",
"print \"C** =\\n\",( (mat([1, 1, 1])*y)/(transpose(A[:,1])*A[:,1]) )\n",
"\n",
"print \"D** =\\n\",( mat([-3, 0 ,3]*y)/(transpose(A[:,1])*A[:,1]) )\n",
"print 'C** gives the besy fit ny horizontal line, whereas D**t is the best fit by a straight line through the origin.'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Ex:3.4.5 Pg: 166"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A=\n",
"[[1 0 1]\n",
" [1 0 0]\n",
" [2 1 0]]\n",
"Q=\n",
"[[ 0.40824829 0. 0.91287093]\n",
" [ 0.40824829 0. -0.18257419]\n",
" [ 0.81649658 1. -0.36514837]]\n"
]
}
],
"source": [
"from numpy import mat,transpose,zeros,shape,linalg\n",
"A=mat([[1, 0 ,1],[1, 0, 0],[2, 1, 0]]) #independent vectors stored in columns of A\n",
"print 'A=\\n',A\n",
"m,n=shape(A)\n",
"V=mat(zeros([n,n]))\n",
"R=mat(zeros([n,n]))\n",
"for k in range(0,n):\n",
" V[:,k]=A[:,k]\n",
" for j in range(0,k-1):\n",
" R[j,k]=transpose(V[:,j])*A[:,k]\n",
" V[:,k]=V[:,k]-R[j,k]*V[:,j]\n",
" \n",
" R[k,k]=linalg.norm(V[:,k])\n",
" V[:,k]=V[:,k]/R[k,k]\n",
"\n",
"print 'Q=\\n',V"
]
}
],
"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
}
|