-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear-algebra.js
More file actions
198 lines (173 loc) · 4.93 KB
/
linear-algebra.js
File metadata and controls
198 lines (173 loc) · 4.93 KB
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
/**
* @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;
}
/**
* 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;
}
/**
* 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;
}
}
/**
* Contains static vector operation methods.
* @hideconstructor
*/
class Vector {
/**
* 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);
}
}