-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinear-algebra.js
327 lines (282 loc) · 8.52 KB
/
linear-algebra.js
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
/**
* @file Matrix and Vector operations.
*
* @copyright Oscar Litorell 2019
*/
/**
* Holds an n * m matrix.
* @property {number} m - The height of the matrix. (read-only)
* @property {number} n - The width of the matrix. (read-only)
* @property {Matrix} transpose - The transpose of the matrix. (read-only)
*/
class Matrix {
/**
* Is constructed with an array of arrays.
* @param {number[][]} [matrix] - An array of arrays representing the matrix.
* @example
* new Matrix([
* [1, 0, 0],
* [0, 1, 0],
* [0, 0, 1]
* ]);
*
*/
constructor(matrix) {
this.matrix = matrix;
}
/**
* Returns a square identity matrix with the given size.
* @param {number} size
*/
static identity(size) {
let m = Matrix.fromSize(size, size)
for (let i = 0; i < size; i++) {
m.matrix[i][i] = 1
}
return m
}
/**
* Create a matrix with a given width and height, and an optional value to fill the matrix with.
* @param {number} width - The width (n) of the matrix.
* @param {number} height - The height (m) of the matrix.
* @param {number} [value] - The value to fill the matrix with.
* @returns {Matrix}
* @example
* Matrix.fromSize(3, 2, 1);
* // returns new Matrix([
* // [1, 1, 1],
* // [1, 1, 1]
* // ])
*/
static fromSize(width, height, value=0) {
let matrix = []
for (let i = 0; i < height; i++) {
matrix.push([]);
for (let j = 0; j < width; j++) {
matrix[i].push(value)
}
}
return new Matrix(matrix);
}
// Height of the matrix
get m() {
return this.matrix.length;
}
// Width of the matrix
get n() {
return this.matrix[0].length;
}
get transpose() {
let n = this.m;
let m = this.n
let newMatrix = [];
for (let y = 0; y < m; y++) {
newMatrix.push([]);
for (let x = 0; x < n; x++) {
newMatrix[y].push(this.matrix[x][y]);
}
}
return new Matrix(newMatrix);
}
/**
* Applies the matrix's transformation to a vector.
* @param {number[]} vector - The vector to transform
* @returns {number[]} The transformed vector.
*/
transformVector(vector) {
let result = new Array(this.m);
result.fill(0);
for (let i = 0; i < result.length; i++) {
for (let j = 0; j < Math.min(vector.length, this.n); j++) {
result[i] += this.matrix[i][j] * vector[j];
}
}
return result;
}
/**
* Returns a rotation matrix
* @param {number} axis The axis along which to rotate. (0, 1, 2) for (x, y, z)
* @param {number} radians
*/
static rotation3D(axis, radians) {
let matrix = Matrix.fromSize(3, 3, 0)
let m = matrix.matrix
let s = Math.sin(radians)
let c = Math.cos(radians)
m[axis][axis] = 1
m[(axis + 1) % 3][(axis + 1) % 3] = c
m[(axis + 2) % 3][(axis + 2) % 3] = c
m[(axis + 2) % 3][(axis + 1) % 3] = s
m[(axis + 1) % 3][(axis + 2) % 3] = -s
return matrix
}
/**
* Matrix multiplication. Multiply two matrices.
* @param {Matrix} matrix1 - The left matrix.
* @param {Matrix} matrix2 - The right matrix.
* @returns {Matrix}
*
* @example
* let matrix1 = new Matrix([
* [0, 1],
* [0, 0]
* ]);
* let matrix2 = new Matrix([
* [0, 0],
* [1, 0]
* ]);
* Matrix.multiplication(matrix1, matrix2);
* // returns new Matrix([
* // [1, 0],
* // [0, 0]
* // ]);
*/
static multiplication(matrix1, matrix2) {
let m = matrix1.matrix.length;
let n = matrix2.matrix[0].length;
let result = Matrix.fromSize(n, m);
for (let i = 0; i < n; i++) {
let vector = new Array(n);
for (let j = 0; j < m; j++) {
vector[j] = matrix2.matrix[j][i];
}
vector = matrix1.transformVector(vector);
for (let j = 0; j < m; j++) {
result.matrix[j][i] = vector[j];
}
}
return result;
}
/**
* Returns a copy of the matrix.
*/
get copy() {
return new Matrix(this.matrix.map(row => row.slice()))
}
multiplyRow(row, scalar) {
this.matrix[row] = this.matrix[row].map(x => x * scalar)
}
multiplyAndAdd(from, to, scalar) {
this.matrix[to] = this.matrix[from].map((val, i) => val * scalar + this.matrix[to][i])
}
swap(rowIndex1, rowIndex2) {
let row1 = this.matrix[rowIndex1]
let row2 = this.matrix[rowIndex2]
for (let i = 0; i < this.n; i++) {
let temp = row1[i]
row1[i] = row2[i]
row2[i] = temp
}
}
/**
* The inverse of a matrix.
*/
get inverse() {
let cols = this.n
let rows = this.m
if (cols !== rows) return null
let old = this.copy
let newMatrix = Matrix.identity(cols)
// Bottom left corner zeros
for (let col = 0; col < cols; col++) {
if (old.matrix[col][col] === 0) {
for (let i = col; i < cols; i++) {
if (old.matrix[i][i] !== 0) {
old.swap(i, col)
newMatrix.swap(i, col)
break
}
}
}
let value = old.matrix[col][col]
old.multiplyRow(col, 1 / value)
newMatrix.multiplyRow(col, 1 / value)
for (let row = col + 1; row < rows; row++) {
let rowValue = old.matrix[row][col]
old.multiplyAndAdd(col, row, -rowValue)
newMatrix.multiplyAndAdd(col, row, -rowValue)
}
}
// Top right corner zeros
for (let col = cols - 1; col > -1; col--) {
for (let row = col - 1; row > -1; row--) {
let rowValue = old.matrix[row][col]
old.multiplyAndAdd(col, row, -rowValue)
newMatrix.multiplyAndAdd(col, row, -rowValue)
}
}
return newMatrix
}
}
/**
* Contains static vector operation methods.
* @hideconstructor
*/
class Vector extends Array {
/**
* Add two vectors.
* @param {number[]} vector1
* @param {number[]} vector2
* @returns {number[]}
*/
static addition(vector1, vector2) {
let length = Math.max(vector1.length, vector2.length);
let result = [];
for (let i = 0; i < length; i++) {
let term1 = vector1[i];
let term2 = vector2[i];
if (isNaN(term1)) term1 = 0;
if (isNaN(term2)) term2 = 0;
result.push(term1 + term2);
}
return result;
}
/**
* Subtract one vector from another.
* @param {number[]} vector1 - The vector to subtract from.
* @param {number[]} vector2 - The vector to subtract.
* @returns {number[]}
*/
static subtraction(vector1, vector2) {
let length = Math.max(vector1.length, vector2.length);
let result = [];
for (let i = 0; i < length; i++) {
let term1 = vector1[i];
let term2 = vector2[i];
if (isNaN(term1)) term1 = 0;
if (isNaN(term2)) term2 = 0;
result.push(term1 - term2);
}
return result;
}
/**
* Multiply a vector with a constant.
* @param {number[]} vector
* @param {number} scalar
* @returns {number[]}
*/
static scalarMultiplication(vector, scalar) {
return vector.map(x => x * scalar);
}
/**
* Returns a vector with a length of 1, colinear to the input vector
* @param {number[]} vector
*/
static normalize(vector) {
let c = 1 / Math.sqrt(vector.map(x => Math.pow(x, 2)).reduce((prev, curr) => prev + curr))
return vector.map(x => x * c)
}
/**
* Returns the cross product of two 3-dimensional vectors
* @param {number[]} vector1
* @param {number[]} vector2
*/
static crossProduct(vector1, vector2) {
let newVector = []
for (let i = 0; i < 3; i++) {
newVector.push(vector1[(i + 1) % 3] * vector2[(i + 2) % 3])
newVector[i] -= (vector1[(i + 2) % 3] * vector2[(i + 1) % 3])
}
return newVector
}
}