-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinearAlgebraTutorial.html
402 lines (283 loc) · 14.8 KB
/
linearAlgebraTutorial.html
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
<html> <head>
<script src="Histo.js"></script>
<script src="Func.js"></script>
<script src="Vector.js"></script>
<script src="Matrix.js"></script>
<script src="distributions.js"></script>
</head> <body>
<script type="text/JavaScript">
/*-----Intro to Clairvoyant Vector and Matrix classes---------------
Clairvoyant provides Vector and Matrix classes for all your linear
algebra applications.
-----------------------------------------------------------------*/
//-----Vector Constructor---------------------------------------------
//Vector(<name>,<elements>)
// <name> a string name for the Vector
// <elements> (optional)an Array() containing the initial
// elements of the vector (default 1 element set to 0)
//Declare a vector with elements (1,2):
var elements = new Array();
elements[0]=1;
elements[1]=2;
var arrow = new Vector(elements);
//Vector objects also carry around an internal element .dim, which is
//the vector's dimension. It's initialized to the length of the Array
//the object is initialized with, or 1 if no Array is given.
//-----dot Method---------------------------------------------
//dot(<vector>,<metric>)
// <vector> the vector to take the dot product of this vector with.
// <metric> (optional) a Matrix object containing the metric of the
// space the vectors live in(default rectangular Cartesian).
//Return type: float on successful exit.
document.write('/////////////////// .dot examples //////////////////////////////////</br>');
//The dot method takes the dot product between this vector and a vector passed to it, by default in
//a rectangular Cartsian space. A different metric may be provided as a Matrix in the optional second
//argument.
var elements2 = new Array();
elements2[0] = 3;
elements2[1] = -1;
var arrow2 = new Vector(elements2);
document.write('</br>Dot product of (1,2) and (3,-1) = '+arrow.dot(arrow2));
//-----getLength Method---------------------------------------------
//getLength(<metric>)
// <metric> (optional) a Matrix object containing the metric of the
// space the vector lives in(default rectangular Cartesian).
//Return type: float on successful exit.
document.write('</br></br>/////////////////// .getLength examples //////////////////////////////////</br>');
//getLength computes the length of this vector, in rectangular cartesian space by default,
//or under another metric passed as a Matrix object as an optional argument.
document.write('</br>Length of vector (1,2) = '+arrow.getLength());
document.write('</br>Length of vector (3,-1) = '+arrow2.getLength());
//-----dump Method---------------------------------------------
//dump() -no arguments
//Return type: 0 on successful exit.
document.write('</br></br>/////////////////// .dump examples (Vector) //////////////////////////////////</br>');
//writes out this Vector to the window.
document.write('</br>Vector arrow = ');
arrow.dump();
//-----scale Method---------------------------------------------
//scale(<sc>)
// <sc> (optional) number to scale this Vector by, default 1 / this.getLength().
//Return type: Vector on successful exit.
document.write('</br></br>/////////////////// .scale examples //////////////////////////////////</br>');
//scale scales this Vector by the function's argument.
document.write('</br>Vector arrow multiplied by 3 = ');
arrow.scale(3).dump();
//-----project Method---------------------------------------------
//project(<vec>)
// <vec> Vector along which to project this Vector.
//Return type: Vector
document.write('</br></br>/////////////////// .project examples //////////////////////////////////</br>');
//Returns the component of this Vector along the argument Vector <vec>.
var component = arrow.project(arrow2);
arrow.dump();
document.write(' projected along ');
arrow2.dump();
document.write(' = ');
component.dump();
//-----add Method---------------------------------------------
//add(<vec>)
// <vec> Vector to add to this Vector.
//Return type: Vector
document.write('</br></br>/////////////////// .sum examples //////////////////////////////////</br>');
//add returns the vector sum of this vector with <vec>.
document.write('Sum of arrow and arrow2 = ')
var sumArrow = arrow.add(arrow2);
sumArrow.dump();
//-----Matrix Constructor---------------------------------------------
//Matrix(<name>,<rows>,<columns>,<predef>)
// <name> a string naming this Matrix.
// <rows> number of rows in this Matrix.
// <columns> number of columns in this Matrix.
// <predef> (optional) option to initialize Matrix as one of a few
// different things.
//Initialize a 3 by 4 matrix as per:
var mtx = new Matrix(3,4);
//All elements are initialized to 0.
//individual elements can be initialized as follows:
var exampleMtx = new Matrix(3,3);
/*
exampleMtx.elements[0][0] = 1
exampleMtx.elements[0][1] = 3
exampleMtx.elements[0][2] = 1
exampleMtx.elements[1][0] = 1
exampleMtx.elements[1][1] = 1
exampleMtx.elements[1][2] = 2
exampleMtx.elements[2][0] = 2
exampleMtx.elements[2][1] = 3
exampleMtx.elements[2][2] = 4
*/
exampleMtx.elements[0][0] = 1
exampleMtx.elements[0][1] = 3
exampleMtx.elements[0][2] = -3
exampleMtx.elements[1][0] = -3
exampleMtx.elements[1][1] = 7
exampleMtx.elements[1][2] = -3
exampleMtx.elements[2][0] = -6
exampleMtx.elements[2][1] = 6
exampleMtx.elements[2][2] = 2
//Matrices can be optionally initialized as one of a few different types:
//get an identity matrix by using 'identity' predef:
var id = new Matrix(4,4,'identity');
//identity may be any dimension, but must be square.
//'Minkowski' predef:
var Mink = new Matrix(4,4,'Minkowski');
//Minkowski preddef must be declared as 4 by 4.
//-----dump Method---------------------------------------------
//dump() - no arguments
//Return type: 0 on successful exit
document.write('</br></br>/////////////////// .dump examples (Matrix) //////////////////////////////////</br>');
//The dump method just writes out this Matrix to the window:
document.write('</br>An identity matrix:');
id.dump();
document.write('</br>the Minkowski metric:');
Mink.dump();
//-----mtxAdd Method---------------------------------------------
//mtxAdd(<matrix>)
// <matrix> Matrix object to be added to this Matrix.
//Return type: Matrix on successful exit.
document.write('</br></br>/////////////////// .mtxAdd examples //////////////////////////////////</br>');
//mtxAdd adds a <Matrix> passed as an argument to this Matrix, and
//returns the result. Both Matrices must have the same dimensions
//for this to make sense!
var sumMtx = id.mtxAdd(Mink);
document.write('4D identity matrix + Minkowski metric:');
sumMtx.dump();
//-----mtxMulti Method---------------------------------------------
//mtxMulti(<object>,<side>)
// <object> Either a Matrix or a Vector object to be multiplied by this Matrix.
// <side> The side of this Matrix to place <object> on before multiplying.
//Return type: Matrix or Vector on successful exit.
document.write('</br></br>/////////////////// .mtxMulti examples //////////////////////////////////</br>');
//mtxMulti handles the multiplication of Matrix and Vector objects, as follows:
var anotherMtx = new Matrix(3,3,'identity');
anotherMtx.elements[0][2] = 4;
anotherMtx.elements[2][0] = -1;
document.write('anotherMtx:');
anotherMtx.dump();
document.write('</br>exampleMtx');
exampleMtx.dump();
var productMtx = anotherMtx.mtxMulti(exampleMtx,'left');
document.write('</br>exampleMtx*anotherMtx:');
productMtx.dump();
document.write('</br>Matrix multiplication is generally noncommutative:</br>anotherMtx*exampleMtx:');
productMtx = anotherMtx.mtxMulti(exampleMtx,'right');
productMtx.dump();
//We can also multiply Vector objects by Matrices:
var coords = new Array();
coords[0] = 5;
coords[1] = 3;
coords[2] = -2
var spaceVec = new Vector(coords);
var transVec = exampleMtx.mtxMulti(spaceVec,'right');
document.write('</br>exampleMtx*(5,3,-2) = ('+transVec.elements[0]+', '+transVec.elements[1]+', '+transVec.elements[2]+')');
//-----getDeterminant Method---------------------------------------------
//getDeterminant() no arguments
//Return type: float on successful exit.
document.write('</br></br>/////////////////// .getDeterminant examples //////////////////////////////////</br>');
//getDeterminant employs the Doolittle algorithm (http://en.wikipedia.org/wiki/LU_decomposition)
//to try and quickly compute the determinant of a square Matrix. Doolittle can have problems if any
//of the diagonal elements become 0 at any step in the algo, so alternatives will be implemented
//for these cases.
var smallMtx = new Matrix(2,2);
smallMtx.elements[0][0] = 3;
smallMtx.elements[0][1] = 6;
smallMtx.elements[1][0] = -2;
smallMtx.elements[1][1] = 1;
document.write('</br>smallMtx:');
smallMtx.dump();
document.write('Determinant of smallMtx = '+smallMtx.getDeterminant());
document.write('</br>Determinant of exampleMtx = '+exampleMtx.getDeterminant());
//-----getMinor Method---------------------------------------------
//getMinor() no arguments
//Return type: Matrix
document.write('</br></br>/////////////////// .getMinor examples //////////////////////////////////</br>');
//the getMinor method returns the minor of this Matrix. Recall that element [n][m] of a matrix's minor is the
//determinant of the matrix generated by deleting row n and column m from the original matrix.
var minorSmall = smallMtx.getMinor();
document.write('</br>Minor of smallMtx:');
minorSmall.dump();
var minorExample = exampleMtx.getMinor();
document.write('</br>Minor of exampleMtx:');
minorExample.dump();
//-----getCofactor Method---------------------------------------------
//getCofactor() no arguments
//Return type: Matrix
document.write('</br></br>/////////////////// .getCofactor examples //////////////////////////////////</br>');
//getCofactor returns the cofactor Matrix of this Matrix. Recall that the cofactor matrix is the same as the
//minor matrix, with element ij multiplied by (-1)^(i+j).
var cofactorExample = exampleMtx.getCofactor();
document.write('</br>Cofactor Matrix of exampleMtx:');
cofactorExample.dump();
//-----getTranspose Method---------------------------------------------
//getTranspose() no arguments
//Return type: Matrix
document.write('</br></br>/////////////////// .getTranspose examples //////////////////////////////////</br>');
//getTranspose returns the transpose of this Matrix.
var exampleTranspose = exampleMtx.getTranspose();
document.write('</br>Transpose of exampleMtx:');
exampleTranspose.dump();
//-----mtxScale Method---------------------------------------------
//mtxScale(<scale>)
// <scale> float by which to multiply every element of this Matrix.
//Return type: Matrix
document.write('</br></br>/////////////////// .mtxScale examples //////////////////////////////////</br>');
//mtxScale multiplies every element of this Matrix by <scale>:
var scaledMtx = exampleMtx.mtxScale(3);
document.write('</br>exampleMtx times 3:');
scaledMtx.dump();
//-----getInverse Method---------------------------------------------
//getInverse()
//Return type: Matrix
document.write('</br></br>/////////////////// .getInverse examples //////////////////////////////////</br>');
//getInverse attempts to return the inverse of this Matrix. Relies on the Doolittle
//determinant calculation.
document.write('</br>Inverse of exampleMtx:');
var exampleInv = exampleMtx.getInverse();
exampleInv.dump();
document.write('exampleMtx*exampleInv:');
(exampleMtx.mtxMulti(exampleInv, 'right')).dump();
//-----orthonormalGS Method---------------------------------------------
//orthonormalGS() -no arguments
//Return type: Array of Vectors
document.write('</br></br>/////////////////// .orthonormalGS examples //////////////////////////////////</br>');
//orthonormalGS uses the Gram-Schmidt algorithm to construct an orthonormal set of
//vectors which span the same space as the columns of this Matrix. Matrix columns need
//to be linearly independent; checking this is still TODO!
document.write('Orthonormal span of columns of exampleMtx:</br>');
var ortho = [];
ortho = exampleMtx.orthonormalGS();
for(i=0; i<3; i++){
(ortho[i]).dump();
document.write(' Length = '+(ortho[i].getLength()));
document.write('</br>');
};
document.write('First dot second = ' + ortho[0].dot(ortho[1]) + '</br>');
document.write('First dot third = ' + ortho[0].dot(ortho[2]) + '</br>');
document.write('Second dot third = ' + ortho[1].dot(ortho[2]) + '</br>');
//-----qr Method---------------------------------------------
//qr(tol, iter)
// tol tolerance when converging Matrix elements to 0 (default 0.000001)
// iter number of iterations to allow before giving up (default 1000)
//Return type: Matrix - Schur form of this Matrix.
document.write('</br></br>/////////////////// .qr examples //////////////////////////////////</br>');
//qr() returns the Schur form of this Matrix, if it can be converged upon within <iter> (default 1000) iterations,
//where convergence requires all elements below the diagonal to be within <tol> (default 0.000001)
//of 0. Recall that the Schur form of a matrix lists that matrix's eigenvalues on its diagonal.
document.write('Schur form of exampleMtx:');
(exampleMtx.qr()).dump();
//-----getEigenvalues Method---------------------------------------------
//getEigenvalues(tol, iter)
// tol tolerance passed to qr() when converging Matrix elements to 0 (default 0.000001)
// iter number of qr() iterations to allow before giving up (default 1000)
//Return type: Array of eigenvalues (diagonal elements of Schur form of this Matrix as found by qr())
document.write('</br></br>/////////////////// .getEigenvalues examples //////////////////////////////////</br>');
//getEigenvalues() evaluates qr() for this Matrix and returns an Array containing
//the diagonal elements of the Schur form of this Matrix. Note that as it stands,
//the eigenvalues returned in this way are very approximate; improvements forthcoming.
var exampleEigenvalues = exampleMtx.getEigenvalues();
document.write('Eigenvalues of exampleMtx: ');
document.write(exampleEigenvalues[0]+', '+exampleEigenvalues[1]+', '+exampleEigenvalues[2]);
document.write('</br>Note that getEigenvalues() is providing a rough estimate; convergence to better accuracy and / or error estimates coming soon');
</script>
</body> </html>