-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix2D.js
402 lines (346 loc) · 13.4 KB
/
Matrix2D.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
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
import {WebSystemObject} from "./system/WebSystemObject.js";
// 2D Matrix class based on the creation of: ¿Código Da vinci?, twitter: @fadi_davenchy
export class Matrix2D extends WebSystemObject {
constructor(r = 3, c = 3, i = 0) {
super();
// create matrix
if (Array.isArray(r)) this.matrix = r.map(c => c.slice(0)); else if (r instanceof Matrix2D) this.copyOf(r); else this.matrix = this.generateArray(r, c, i);
// check the matrix
const rows = this.matrix.length;
const columns = this.matrix[0].length;
const columnsLength = this.matrix.filter(c => c.length === columns).length;
if (columnsLength !== rows) throw new Error(`each column must have the same number of values: ${rows}`);
}
get isSquare() {
return this.rows === this.columns;
}
get isInversable() {
return this.determinate !== 0;
}
get determinate() {
const m = this.clone();
if (!m.isSquare) throw new Error('matrix must be a square matrix');
const M = m.getValue.bind(m);
// handle (1x1) Matrix
if (m.rows === 1) return M(1, 1);
// handle (2x2) Matrix
if (m.rows === 2) return M(1, 1) * M(2, 2) - M(1, 2) * M(2, 1);
// handle (3x3) or more Matrices
if (m.rows >= 3) {
let sum = 0;
for (let i = 1; i <= m.rows; i++) {
const det = m.clone().reduce(1, i).determinate;
sum += Math.pow(-1, i - 1) * M(1, i) * det;
}
return sum;
}
}
get rows() {
return this.matrix.length;
}
get columns() {
return this.matrix[0].length;
}
get type() {
return `(${this.rows} x ${this.columns})`
}
reset(i = 0) {
this.matrix = this.generateArray(this.rows, this.columns, i);
return this;
}
copyOf(m) {
this.matrix = m.matrix.map(c => c.slice(0));
return this;
}
clone() {
return new Matrix2D(this.matrix);
}
toString() {
return this.matrix.map(c => c.join('\t')).join('\n');
}
isEqualTo(m2) {
if (!m2 instanceof Matrix2D) throw new Error('need matrix to compare to');
const m1 = this;
if (m1.rows !== m2.rows || m1.columns !== m2.columns) return false;
m1.loop(({ i, j, value }) => {
if (m2.getValue(i, j) !== value) return false;
});
return true;
}
setColumn(n, c = []) {
if (c.length !== this.rows) throw new Error(`column must has ${this.rows} value(s)`);
if (n > this.columns || n <= 0) throw new Error(`column number must be in range [1, ${this.columns}]`);
this.matrix.forEach((col, i) => {
col[n - 1] = c[i];
});
return this;
}
addColumn(c = []) {
if (c.length !== this.rows) throw new Error(`column must has ${this.rows} value(s)`);
this.matrix.forEach((col, i) => {
col.push(c[i]);
});
return this;
}
getColumn(c = 1) {
if (c > this.columns || c <= 0) throw new Error(`column number must be in range [1, ${this.columns}]`);
return this.matrix.map(r => r[c - 1]);
}
removeColumn(c) {
if (c > this.columns || c <= 0) throw new Error(`column number must be in range [1, ${this.columns}]`);
this.matrix.forEach(r => r.splice(c - 1, 1));
return this;
}
setRow(n, r = []) {
if (r.length !== this.columns) throw new Error(`row must has ${this.columns} value(s)`);
if (n > this.rows || n <= 0) throw new Error(`row number must be in range [1, ${this.rows}]`);
this.matrix[n - 1] = r;
return this;
}
addRow(r = []) {
if (r.length !== this.columns) throw new Error(`row must has ${this.columns} value(s)`);
this.matrix.push(r);
return this;
}
getRow(r = 1) {
if (r > this.rows || r <= 0) throw new Error(`row number must be in range [1, ${this.rows}]`);
return this.matrix[r - 1].slice(0);
}
removeRow(r) {
if (r > this.rows || r <= 0) throw new Error(`row number must be in range [1, ${this.rows}]`);
this.matrix.splice(r - 1, 1);
return this;
}
setValue(r = 1, c = 1, v) {
this.matrix[r - 1][c - 1] = v;
return this;
}
getValue(r = 1, c = 1) {
if (r <= 0 || r > this.rows || c <= 0 || c > this.columns) return;
return this.matrix[r - 1][c - 1];
}
loop(cb) {
const self = this;
const { rows, columns } = this;
let counter = 0;
for (let i = 1; i <= rows; i++) for (let j = 1; j <= columns; j++) {
const tools = {
self,
i,
j,
clone: self.clone(),
sign: Math.pow(-1, counter),
counter: counter++,
value: self.getValue(i, j),
setValue: v => self.setValue(i, j, v),
column: self.getColumn(j),
row: self.getRow(i),
}
try {
tools.determinate = self.determinate;
} catch (_) {
tools.determinate = null;
}
tools.reduce = () => tools.clone.reduce(i, j);
tools.inverse = () => tools.clone.inverse();
tools.transpose = () => tools.clone.transpose();
tools.adjacency = () => tools.clone.adjacency();
tools.identity = () => tools.clone.identity();
cb(tools);
}
return this;
}
multiply(...matrices) {
const m1 = this;
matrices.forEach(m2 => {
if (m1.columns !== m2.rows) throw new Error('can not multiply the 2 matrices');
const temp = new Matrix2D(m1.rows, m2.columns);
temp.loop(({ i, j, setValue }) => {
const product = this.vectorDotProduct(m1.getRow(i), m2.getColumn(j));
setValue(product);
});
m1.copyOf(temp);
});
return this;
}
addNumber(n) {
const self = this;
const { rows, columns } = this;
this.loop(t => t.setValue(t.value + n));
return this;
}
subtractNumber(n) {
const self = this;
const { rows, columns } = this;
this.loop(t => t.setValue(t.value - n));
return this;
}
multiplyNumber(n) {
const self = this;
const { rows, columns } = this;
this.loop(t => t.setValue(t.value * n));
return this;
}
divideNumber(n) {
if (n === 0) throw new Error('can not divide by zero');
const self = this;
const { rows, columns } = this;
this.loop(t => t.setValue(t.value / n));
return this;
}
transpose() {
const mat = Array(this.columns).fill(0);
this.matrix = mat.map((r, i) => this.getColumn(i + 1));
return this;
}
inverse() {
const delta = Math.pow(this.determinate, -1);
this.adjacency().multiplyNumber(delta);
return this;
}
adjacency() {
if (!this.isInversable) throw new Error('matrix determinate value equal to zero');
if (!this.isSquare) throw new Error('must be a square matrix');
// 1x1 Matrix
if (this.rows === 1) this.setValue(1, 1, Math.pow(this.getValue(1, 1), -1));
// 2x2 Matrix
else if (this.rows === 2) {
const temp = this.getValue(1, 1);
this.setValue(1, 2, this.getValue(1, 2) * -1);
this.setValue(2, 1, this.getValue(2, 1) * -1);
this.setValue(1, 1, this.getValue(2, 2));
this.setValue(2, 2, temp);
}
// 3x3 or more Matrices
else if (this.rows >= 3) {
const temp = this.clone().reset();
this.loop(({ i, j, reduce, sign }) => {
temp.setValue(i, j, reduce().determinate * sign)
}).copyOf(temp.transpose());
}
return this;
}
identity() {
const m = this;
if (!m.isSquare) throw new Error('must be a square matrix');
const n = m.rows;
const i = new Matrix2D(n, n);
for (let a = 1; a <= n; a++) {
i.setValue(a, a, 1);
}
return i;
}
reduce(r, c) {
this.removeRow(r).removeColumn(c);
return this;
}
generateArray(r = 3, c = 3, i = 0) {
return Array(r).map(r => Array(c).map(c => i));
};
vectorDotProduct(a, b) {
if (a.length !== b.length) throw new Error(`length of 'a' not equal length of 'b'`);
return a.map((x, i) => x * b[i]).reduce((a, b) => a + b);
}
//
// Matrices de transformación de coordenadas.
// Pueden multiplicarse por un vector para transformar o entre sí para obtener composiciones.
//
// Sección 2D (XY)
traslacion2D(punto) {
// solve
return new Matrix2D(2, 2, 1);
}
rotacion2DOrigen(phi) {
let result = new Matrix2D(2, 2, 1);
result.setRow([Math.cos(phi), -Math.sin(phi)]);
result.setRow([Math.cos(phi), -Math.sin(phi)]);
return result;
}
rotacion2DPunto(phi, centro) {
return new Matrix2D(2, 2, 1); // solve
}
//
// Sección 3D (XYZ)
rotacion3DEjeX(phi) { // to rev
let result = new Matrix2D(2, 2, 1);
result.setRow([1, 0, 0]);
result.setRow([0, Math.cos(phi), -Math.sin(phi)]);
result.setRow([0, Math.sin(phi), Math.cos(phi)]);
return result;
}
rotacion3DEjeY(phi) { // to rev
let result = new Matrix2D(2, 2, 1);
result.setRow([Math.cos(phi), 0, Math.sin(phi)]);
result.setRow([0, 1, 0]);
result.setRow([-Math.sin(phi), 0, Math.cos(phi)]);
return result;
}
rotacion3DEjeZ(phi) { // to rev
let result = new Matrix2D(2, 2, 1);
result.setRow([Math.cos(phi), -Math.sin(phi), 0]);
result.setRow([Math.sin(phi), Math.cos(phi), 0]);
result.setRow([0, 0, 1]);
return result;
}
rotacion3DEjeArbirtario(vec, phi) { // to rev
let result = new Matrix2D(2, 2, 1);
let u = vec.unitary;
result.setRow([Math.cos(phi) + Math.pow(u.x, 2) * (1 - Math.cos(phi)), u.x * u.y * (1 - Math.cos(phi)) - u.z * Math.sin(phi), u.x * u.z * (1 - Math.cos(phi)) - u.y * Math.sin(phi)]);
result.setRow([u.y * u.x * (1 - Math.cos(phi)) - u.z * Math.sin(phi), Math.cos(phi) + Math.pow(u.y, 2) * (1 - Math.cos(phi)), u.y * u.z * (1 - Math.cos(phi)) - u.x * Math.sin(phi)]);
result.setRow([u.z * u.x * (1 - Math.cos(phi)) - u.y * Math.sin(phi), u.z * u.y * (1 - Math.cos(phi)) - u.x * Math.sin(phi), Math.cos(phi) + Math.pow(u.z, 2) * (1 - Math.cos(phi))]);
return result;
}
//
// Sección 4D (XYZ) Cuaterniones (Revisar contra los documentos del profesor Hektor).
// Se introduce una nueva columna para comprender a la traslación como una nueva transformación.
traslacion4D(punto) {
let result = new Matrix2D(2, 2, 1); // rev
result.setRow([1, 0, 0, punto.x]);
result.setRow([0, 1, 0, punto.y]);
result.setRow([0, 0, 1, punto.z]);
result.setRow([0, 0, 0, 1]);
return result;
}
// También se le llama escalado
homotecia4D(prop) {
let result = new Matrix2D(2, 2, 1);
let u = prop.unitary;
result.setRow([u.x, 0, 0, 0]);
result.setRow([0, u.y, 0, 0]);
result.setRow([0, 0, u.z, 0]);
result.setRow([0, 0, 0, 1]);
return result;
}
rotacion4DEjeX(phi) {
let result = new Matrix2D(2, 2, 1);
result.setRow([1, 0, 0, 0]);
result.setRow([0, Math.cos(phi), -Math.sin(phi), 0]);
result.setRow([0, Math.sin(phi), Math.cos(phi), 0]);
result.setRow([0, 0, 0, 1]);
return result;
}
rotacion4DEjeY(phi) {
let result = new Matrix2D(2, 2, 1);
result.setRow([Math.cos(phi), 0, Math.sin(phi), 0]);
result.setRow([0, 1, 0, 0]);
result.setRow([-Math.sin(phi), 0, Math.cos(phi), 0]);
result.setRow([0, 0, 0, 1]);
return result;
}
rotacion4DEjeZ(phi) {
let result = new Matrix2D(2, 2, 1);
result.setRow([Math.cos(phi), -Math.sin(phi), 0, 0]);
result.setRow([Math.sin(phi), Math.cos(phi), 0, 0]);
result.setRow([0, 0, 1, 0]);
result.setRow([0, 0, 0, 1]);
return result;
}
rotacion4DEjeArbirtario(vec, phi) { // to rev
let result = new Matrix2D(2, 2, 1);
let u = vec.unitary;
result.setRow([Math.cos(phi) + Math.pow(u.x, 2) * (1 - Math.cos(phi)), u.x * u.y * (1 - Math.cos(phi)) - u.z * Math.sin(phi), u.x * u.z * (1 - Math.cos(phi)) - u.y * Math.sin(phi)]);
result.setRow([u.y * u.x * (1 - Math.cos(phi)) - u.z * Math.sin(phi), Math.cos(phi) + Math.pow(u.y, 2) * (1 - Math.cos(phi)), u.y * u.z * (1 - Math.cos(phi)) - u.x * Math.sin(phi)]);
result.setRow([u.z * u.x * (1 - Math.cos(phi)) - u.y * Math.sin(phi), u.z * u.y * (1 - Math.cos(phi)) - u.x * Math.sin(phi), Math.cos(phi) + Math.pow(u.z, 2) * (1 - Math.cos(phi))]);
return result;
}
}