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
|
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Chapter 8 - Inner product spaces"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Page 271 Example 8.1"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"n = 5\n",
"a = [[ 1. 4. 2. 8. 8.]]\n",
"b = [[ 10. 8. 3. 1. 8.]]\n",
"Then, (a|b) = \n",
"\n",
"[[ 10. 40. 20. 80. 80.]\n",
" [ 8. 32. 16. 64. 64.]\n",
" [ 3. 12. 6. 24. 24.]\n",
" [ 1. 4. 2. 8. 8.]\n",
" [ 8. 32. 16. 64. 64.]]\n"
]
}
],
"source": [
"import numpy as np\n",
"n=np.random.randint(2,9)\n",
"a=np.random.rand(1,n)\n",
"b=np.random.rand(1,n)\n",
"for i in range(0,n):\n",
" a[0,i]=round(a[0,i]*10)\n",
" b[0,i]=round(b[0,i]*10)\n",
"print 'n = ',n\n",
"print 'a = ',a\n",
"print 'b = ',b\n",
"print 'Then, (a|b) = \\n\\n',a*np.transpose(b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Page 271 Example 8.2"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a = [[ 4. 3.]]\n",
"b = [[ 7. 5.]]\n",
"Then, a|b = 47.0\n"
]
}
],
"source": [
"import numpy as np\n",
"a=np.random.rand(1,2)\n",
"b=np.random.rand(1,2)\n",
"for i in range(0,2):\n",
" a[0,i]=round(a[0,i]*10)\n",
" b[0,i]=round(b[0,i]*10)\n",
"print 'a = ',a\n",
"print 'b = ',b\n",
"x1 = a[0,0]#\n",
"x2 = a[0,1]#\n",
"y1 = b[0,0]#\n",
"y2 = b[0,1]#\n",
"t = x1*y1 - x2*y1 - x1*y2 + 4*x2*y2#\n",
"print 'Then, a|b = ',t"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Page 307 Example 8.28"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x1 and x2 are two real nos. i.e., x1**2 + x2**2 = 1\n",
"x1 = 0.383547227589\n",
"x2 = 0.923521263539\n",
"B = \n",
"[[ 0.38354723 0.92352126 0. ]\n",
" [ 0. 1. 0. ]\n",
" [ 0. 0. 1. ]]\n",
"Applying Gram-Schmidt process to B:\n",
"a1 = [ 0.38354723 0.92352126 0. ]\n",
"a2 = [-0.35421402 0.14710848 0. ]\n",
"a3 = [0 0 1]\n",
"U = \n",
"[[[ 0.38354723 0.92352126 0. ]]\n",
"\n",
" [[-0.92352126 0.38354723 0. ]]\n",
"\n",
" [[ 0. 0. 1. ]]]\n",
"M = \n",
"[[ 1. 0. 0. ]\n",
" [-2.40784236 2.60724085 0. ]\n",
" [ 0. 0. 1. ]]\n",
"inverse(M) * U = [[[ 3.83547228e-01 -4.25822963e-17 0.00000000e+00]\n",
" [ 3.54214020e-01 3.54214020e-01 0.00000000e+00]\n",
" [ 0.00000000e+00 0.00000000e+00 0.00000000e+00]]\n",
"\n",
" [[ -9.23521264e-01 -1.76848356e-17 0.00000000e+00]\n",
" [ -8.52891524e-01 1.47108476e-01 0.00000000e+00]\n",
" [ -0.00000000e+00 0.00000000e+00 0.00000000e+00]]\n",
"\n",
" [[ 0.00000000e+00 -0.00000000e+00 0.00000000e+00]\n",
" [ 0.00000000e+00 0.00000000e+00 0.00000000e+00]\n",
" [ 0.00000000e+00 0.00000000e+00 1.00000000e+00]]]\n",
"So, B = inverse(M) * U\n"
]
}
],
"source": [
"import numpy as np\n",
"print 'x1 and x2 are two real nos. i.e., x1**2 + x2**2 = 1'\n",
"x1 = np.random.rand()\n",
"x2 = np.sqrt(1 - x1**2)\n",
"print 'x1 = ',x1\n",
"print 'x2 = ',x2\n",
"B = np.array([[x1, x2, 0],[0, 1, 0],[0, 0, 1]])\n",
"print 'B = \\n',B\n",
"print 'Applying Gram-Schmidt process to B:'\n",
"a1 = np.array([x1, x2, 0])\n",
"a2 = np.array([0 ,1 ,0]) - x2 * np.array([x1 ,x2 ,0])\n",
"a3 = np.array([0, 0, 1])\n",
"print 'a1 = ',a1\n",
"print 'a2 = ',a2\n",
"print 'a3 = ',a3\n",
"U = np.array([[a1],[a2/x1],[a3]])\n",
"print 'U = \\n',U\n",
"M = np.array([[1, 0, 0],[-x2/x1, 1/x1, 0],[0, 0, 1]])\n",
"print 'M = \\n',M\n",
"print 'inverse(M) * U = ',np.linalg.inv(M) * U\n",
"print 'So, B = inverse(M) * U'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Page 278 Example 8.9"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(x,y) = [[ 5. 3.]]\n",
"(-y,x) = [-3.0, 5.0]\n",
"Inner product of these vectors is:\n",
"(x,y)|(-y,x) = 0.0\n",
"So, these are orthogonal.\n",
"------------------------------------------\n",
"If inner product is defined as:\n",
"(x1,x2)|(y1,y2) = x1y1- x2y1 - x1y2 + 4x2y2\n",
"Then, (x,y)|(-y,x) = -x*y+y**2-x**2+4*x*y = 0 if,\n",
"y = 1/2(-3 + sqrt(13))*x\n",
"or\n",
"y = 1/2(-3 - sqrt(13))*x\n",
"Hence,\n",
"[[ 5. 3.]]\n",
"is orthogonal to\n",
"[-3.0, 5.0]\n"
]
}
],
"source": [
"import numpy as np\n",
"#a = round(rand(1,2) * 10)#\n",
"a=np.random.rand(1,2)\n",
"for j in [0,1]:\n",
" a[0,j]=round(a[0,j]*10)\n",
"\n",
"x = a[0,0]\n",
"y = a[0,1]\n",
"b = [-y, x]#\n",
"print '(x,y) = ',a\n",
"print '(-y,x) = ',b\n",
"print 'Inner product of these vectors is:'\n",
"t = -x*y + y*x#\n",
"print '(x,y)|(-y,x) = ',t\n",
"\n",
"print 'So, these are orthogonal.'\n",
"print '------------------------------------------'\n",
"print 'If inner product is defined as:'\n",
"print '(x1,x2)|(y1,y2) = x1y1- x2y1 - x1y2 + 4x2y2'\n",
"print 'Then, (x,y)|(-y,x) = -x*y+y**2-x**2+4*x*y = 0 if,'\n",
"print 'y = 1/2(-3 + sqrt(13))*x'\n",
"print 'or'\n",
"print 'y = 1/2(-3 - sqrt(13))*x'\n",
"print 'Hence,'\n",
"if y == (1./2*(-3 + np.sqrt(13))*x) or (1./2*(-3 - np.sqrt(13))*x):\n",
" print a\n",
" print 'is orthogonal to'\n",
" print b\n",
"else:\n",
" print a\n",
" print 'is not orthogonal to'\n",
" print b\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Page 282 Example 8.12"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"b1 = [3 0 4]\n",
"b2 = [-1 0 7]\n",
"b3 = [ 2 9 11]\n",
"Applying the Gram-Schmidt process to b1,b2,b3:\n",
"a1 = [3 0 4]\n",
"a2 = [2 0 3]\n",
"a3 = [2 9 4]\n",
"{a1,a2,a3} are mutually orthogonal and hence forms orthogonal basis for R**3\n",
"Any arbitrary vector {x1,x2,x3} in R**3 can be expressed as:\n",
"y = {x1,x2,x3} = (3*x1 + 4*x3)/25*a1 + (-4*x1 + 3*x3)/25*a2 + x2/9*a3\n",
"x1 = 1\n",
"x2 = 2\n",
"x3 = 3\n",
"y = [0 0 0]\n",
"i.e. y = [x1 x2 x3], according to above equation.\n",
"Hence, we get the orthonormal basis as:\n",
", [ 0.6 0. 0.8]\n",
", [ 0.4 0. 0.6]\n",
"[ 0.22222222 1. 0.44444444]\n"
]
}
],
"source": [
"import numpy as np\n",
"b1 = np.array([3, 0, 4])\n",
"b2 = np.array([-1 ,0 ,7])\n",
"b3 = np.array([2 ,9 ,11])\n",
"print 'b1 = ',b1\n",
"print 'b2 = ',b2\n",
"print 'b3 = ',b3\n",
"print 'Applying the Gram-Schmidt process to b1,b2,b3:'\n",
"a1 = b1\n",
"print 'a1 = ',a1\n",
"a2 = b2-(np.transpose((b2*np.transpose(b1)))/25*b1)\n",
"print 'a2 = ',a2\n",
"a3 = b3-(np.transpose(b3*np.transpose(b1))/25*b1) - (np.transpose(b3*np.transpose(a2))/25*a2)\n",
"print 'a3 = ',a3\n",
"print '{a1,a2,a3} are mutually orthogonal and hence forms orthogonal basis for R**3'\n",
"print 'Any arbitrary vector {x1,x2,x3} in R**3 can be expressed as:'\n",
"print 'y = {x1,x2,x3} = (3*x1 + 4*x3)/25*a1 + (-4*x1 + 3*x3)/25*a2 + x2/9*a3'\n",
"x1 = 1#\n",
"x2 = 2#\n",
"x3 = 3#\n",
"y = (3*x1 + 4*x3)/25*a1 + (-4*x1 + 3*x3)/25*a2 + x2/9*a3#\n",
"print 'x1 = ',x1\n",
"print 'x2 = ',x2\n",
"print 'x3 = ',x3\n",
"print 'y = ',y\n",
"print 'i.e. y = [x1 x2 x3], according to above equation.'\n",
"print 'Hence, we get the orthonormal basis as:'\n",
"\n",
"print ',',1./5*a1\n",
"print ',',1./5*a2\n",
"print 1/9.*a3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Page 283 Example 8.13"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A = [[ 1.25598176 1.81258697]\n",
" [ 0.6193707 0.80341686]]\n",
"b1 = [ 1.25598176 1.81258697]\n",
"b2 = [ 0.6193707 0.80341686]\n",
"Applying the orthogonalization process to b1,b2:\n",
"[1.255981755902444, 1.8125869670307564] a1 = \n",
"[] a2 = \n",
"a2 is not equal to zero if and only if b1 and b2 are linearly independent.\n",
"That is, if determinant of A is non-zero.\n"
]
}
],
"source": [
"import numpy as np\n",
"A = np.random.rand(2,2)\n",
"A[0,:] = A[0,:] + 1# #so b1 is not equal to zero\n",
"a = A[0,0]\n",
"b = A[0,1]\n",
"c = A[1,0]\n",
"d = A[1,1]\n",
"b1 = A[0,:]\n",
"b2 = A[1,:]\n",
"print 'A = ',A\n",
"print 'b1 = ',b1\n",
"print 'b2 = ',b2\n",
"print 'Applying the orthogonalization process to b1,b2:'\n",
"\n",
"a1 = [a, b]\n",
"a2 = (np.linalg.det(A)/(a**2 + b**2))*[-np.transpose(b), np.transpose(a)]\n",
"print a1,'a1 = '\n",
"print a2,'a2 = '\n",
"print 'a2 is not equal to zero if and only if b1 and b2 are linearly independent.'\n",
"print 'That is, if determinant of A is non-zero.'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Page 286 Example 8.14"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"v = [-10 2 8]\n",
"u = [ 3 12 -1]\n",
"Orthogonal projection of v1 on subspace W spanned by v2 is given by:\n",
"[-3 0 1]\n",
"Orthogonal projection of R**3 on W is the linear transformation E given by:\n",
"(x1,x2,x3) -> (3*x1 + 12*x2 - x3)/%d * (3 12 -1) 154\n",
"Rank(E) = 1\n",
"Nullity(E) = 2\n"
]
}
],
"source": [
"import numpy as np\n",
"v = np.array([-10 ,2 ,8])\n",
"u = np.array([3, 12, -1])\n",
"print 'v = ',v\n",
"print 'u = ',u\n",
"print 'Orthogonal projection of v1 on subspace W spanned by v2 is given by:'\n",
"a = (np.transpose(u*np.transpose(v)))/(u[0]**2 + u[1]**2 + u[2]**2) * u\n",
"print a\n",
"print 'Orthogonal projection of R**3 on W is the linear transformation E given by:'\n",
"print '(x1,x2,x3) -> (3*x1 + 12*x2 - x3)/%d * (3 12 -1)',(u[0]**2 + u[1]**2 + u[2]**2)\n",
"print 'Rank(E) = 1'\n",
"print 'Nullity(E) = 2'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Page 288 Example 8.15"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"f = (sqrt(2)*cos(2*pi*t) + sqrt(2)*sin(4*pi*t))**2\n",
"Integration (f dt) in limits 0 to 1 = 2.0\n"
]
}
],
"source": [
"from sympy.mpmath import quad,cos,sin,pi,sqrt\n",
"\n",
"#part c\n",
"print 'f = (sqrt(2)*cos(2*pi*t) + sqrt(2)*sin(4*pi*t))**2'\n",
"print 'Integration (f dt) in limits 0 to 1 = ',\n",
"x0 = 0#\n",
"x1 = 1#\n",
"X = quad(lambda t:(sqrt(2)*cos(2*pi*t) + sqrt(2)*sin(4*pi*t))**2,[x0,x1])\n",
"print X"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Page 294 Example 8.17"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Matrix of projection E in orthonormal basis is:\n",
"A = 1/154 * [[ 9 36 -3]\n",
" [ 36 144 -12]\n",
" [ -3 -12 1]]\n",
"A* = [[ 9 36 -3]\n",
" [ 36 144 -12]\n",
" [ -3 -12 1]]\n",
"Since, E = E* and A = A*, then A is also the matrix of E*\n",
"a1 = [154, 0, 0]\n",
"a2 = [145, -36, 3]\n",
"a3 = [-36, 10, 12]\n",
"{a1,a2,a3} is the basis.\n",
"Ea1 = [9, 36, -3]\n",
"Ea2 = [0, 0, 0]\n",
"Ea3 = [0, 0, 0]\n",
"Matrix B of E in the basis is:\n",
"B = \n",
"[[-1 0 0]\n",
" [-1 0 0]\n",
" [ 0 0 0]]\n",
"B* = \n",
"[[-1 -1 0]\n",
" [ 0 0 0]\n",
" [ 0 0 0]]\n",
"Since, B is not equal to B*, B is not the matrix of E*\n"
]
}
],
"source": [
"import numpy as np\n",
"#Equation given in example 14 is used.\n",
"def transform(x,y,z):\n",
" x1 = 3*x#\n",
" x2 = 12*y#\n",
" x3 = -z#\n",
" m = [x1 ,x2, x3]\n",
" return m\n",
"\n",
"print 'Matrix of projection E in orthonormal basis is:'\n",
"t1 = transform(3,3,3)#\n",
"t2 = transform(12,12,12)#\n",
"t3 = transform(-1,-1,-1)#\n",
"A = np.vstack([t1,t2,t3])#[t1# t2# t3]#\n",
"print 'A = 1/154 * ',A\n",
"\n",
"A1 = np.transpose(np.conj(A))\n",
"print 'A* = ',A1\n",
"print 'Since, E = E* and A = A*, then A is also the matrix of E*'\n",
"a1 = [154, 0, 0]#\n",
"a2 = [145 ,-36, 3]#\n",
"a3 = [-36 ,10 ,12]#\n",
"print 'a1 = ',a1\n",
"print 'a2 = ',a2\n",
"print 'a3 = ',a3\n",
"print '{a1,a2,a3} is the basis.'\n",
"Ea1 = [9 ,36 ,-3]#\n",
"Ea2 = [0 ,0, 0]#\n",
"Ea3 = [0 ,0 ,0]#\n",
"print 'Ea1 = ',Ea1\n",
"print 'Ea2 = ',Ea2\n",
"print 'Ea3 = ',Ea3\n",
"B = np.array([[-1, 0, 0],[-1, 0 ,0],[0, 0, 0]])\n",
"print 'Matrix B of E in the basis is:'\n",
"print 'B = \\n',B\n",
"B1 = np.transpose(np.conj(B))\n",
"print 'B* = \\n',B1\n",
"print 'Since, B is not equal to B*, B is not the matrix of E*'"
]
}
],
"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
}
|