-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmatrix.js
761 lines (596 loc) · 16.8 KB
/
matrix.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
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
/**
* @author Isaac Vega Rodriguez <[email protected]>
*/
class Row {
constructor(size, vals) {
let len = ~~Math.abs(size);
this.data = [];
this.size = len;
for (let i = 0; i < len; i += 1) {
this.data[i] = vals || 0;
}
this.normalize();
}
static EPS() {
return 1e-9;
}
get(i) {
return this.data[i];
}
set(i, val) {
return this.data[i] = val;
}
size() {
return this.size;
}
length() {
return this.size;
}
clone() {
let r = new Row(this.size);
for (let i = 0; i < r.size; i += 1) {
r.set(i, this.get(i));
}
return r;
}
normalize() {
this.clz = 0;
for (let i = 0; i < this.size && Math.abs( this.get(i) ) < Row.EPS() ; i += 1) {
this.clz = i + 1;
}
}
delAt(pos) {
if ( pos >= 0 && pos < this.size ) {
this.data.splice(pos, 1);
this.size -= 1;
}
}
add(n, thisRow) {
let r = ( thisRow ) ? this : this.clone();
let isRow = (n instanceof Row);
if ( isRow && n.size != this.size ) {
throw new TypeError('Lengths does not match');
}
for (let i = 0; i < r.size; i += 1) {
r.set(i, r.get(i) + (( isRow ) ? n.get(i) : n) );
}
return r;
}
sub(n, thisRow) {
return this.add(( n instanceof Row ) ? n.mul(-1) : -n, thisRow);
}
mul(n, thisRow) {
let r = ( thisRow ) ? this : this.clone();
let isRow = (n instanceof Row);
if ( isRow && n.size != this.size ) {
throw new TypeError('Lengths does not match');
}
for (let i = 0; i < r.size; i += 1) {
r.set(i, r.get(i) * (( isRow ) ? n.get(i) : n));
}
return r;
}
div(n, thisRow) {
let r = ( thisRow ) ? this : this.clone();
for (let i = 0; i < r.size; i += 1) {
r.set(i, r.get(i) / n);
}
return r;
}
append(n, thisRow) {
let r = ( thisRow ) ? this : this.clone();
if ( n instanceof Row ) {
for (let i = 0; i < n.size; i += 1) {
r.data.push( n.get(i) );
}
r.size += n.size;
} else {
r.data.push( n );
r.size += 1;
}
return r;
}
prepend(n, thisRow) {
let r = ( thisRow ) ? this : this.clone();
if ( n instanceof Row ) {
for (let i = n.size - 1; i >= 0; i -= 1) {
r.data.unshift( n.get(i) );
}
r.size += n.size;
} else {
r.data.unshift( n );
r.size += 1;
}
return r;
}
}
class Matrix {
constructor(rows, cols, val) {
this.rows = rows;
this.cols = cols;
this.matrix = [];
for (let i = 0; i < rows; i += 1) {
this.matrix[i] = new Row(cols, val);
}
}
static EPS() {
return 1e-9;
}
static Identity(n) {
let res = new Matrix(n, n);
for (let i = 0; i < n; i += 1) {
res.set(i, i, 1);
res.matrix[i].clz = i;
}
return res;
}
static toFlatArray(arr) {
let res = [];
let Q = [];
Q.push(arr);
while( Q.length > 0 ) {
let elem = Q.shift();
if ( Array.isArray( elem ) ) {
elem.forEach(e => Q.push(e));
} else {
res.push(elem);
}
}
return res;
}
static fromArray(arr, shape) {
if ( shape.length < 2 ) {
throw new TypeError('Invalid shape. Must have a length >= 2');
}
let tot = shape[0] * shape[1];
let arrF = Matrix.toFlatArray(arr);
if ( arrF.length < tot ) {
throw new TypeError(`Expected to have an array with length ${tot} but is has only ${arrF.length}`);
}
let res = new Matrix(shape[0], shape[1]);
let idx = 0;
for (let i = 0; i < shape[0]; i += 1) {
for (let j = 0; j < shape[1]; j += 1) {
res.set(i, j, arrF[idx++]);
}
}
return res;
}
dims() {
return [ this.rows, this.cols ];
}
toArray() {
let res = [];
for (let i = 0; i < this.rows; i += 1) {
for (let j = 0; j < this.cols; j += 1) {
res.push( this.get(i, j) );
}
}
return res;
}
get(i, j) {
if ( typeof j === 'undefined' ) {
return this.matrix[i];
}
return this.matrix[i].get(j);
}
set(i, j, value) {
if ( (typeof value === 'undefined') && (j instanceof Row) && (j.size === this.cols) ) {
let val = j;
for (let c = 0; c < val.size; c += 1) {
this.get(i).set(c, val.get(c));
// this.matrix[i].data[c] = value.data[c];
}
return this.get(i);
}
return this.get(i).set(j, Math.abs(value) < Matrix.EPS() ? 0 : value );
}
add(m, thisMatrix) {
let isMatrix = (m instanceof Matrix);
if ( isMatrix && (m.rows != this.rows || m.cols != this.cols) ) {
throw new TypeError(`Dimensions does not match ${this.rows}x${this.cols} ${m.rows}x${m.cols}`);
}
let result = ( thisMatrix ) ? this : this.clone();
for (let i = 0, maxi = this.rows; i < maxi; i += 1) {
result.set(i, result.get(i).add( ( isMatrix ) ? m.get(i) : m ));
}
return result;
}
sub(m, thisMatrix) {
return this.add( ( m instanceof Matrix ) ? m.mul(-1) : -m, thisMatrix );
}
mul(m) {
let isMatrix = (m instanceof Matrix);
if ( isMatrix && this.cols != m.rows ) {
throw new TypeError(
`Can not multiply those matrices ${this.rows}x${this.cols} ${m.rows}x${m.cols}`
);
}
let result;
if ( isMatrix ) {
result = new Matrix(this.rows, m.cols);
let EPS = Matrix.EPS();
for (let i = 0, maxi = this.rows; i < maxi; i += 1) {
for (let j = 0, maxj = m.cols; j < maxj; j += 1) {
let sum = 0;
for (let k = 0, maxk = this.cols; k < maxk; k += 1) {
sum += this.get(i, k) * m.get(k, j);
}
result.set(i, j, sum);
}
}
} else {
result = this.clone();
for (let i = 0, maxi = this.rows; i < maxi; i += 1) {
for (let j = 0, maxj = this.cols; j < maxj; j += 1) {
result.set(i, j, result.get(i, j) * m);
}
}
}
return result;
}
div(m, thisMatrix) {
if ( m instanceof Matrix ) {
throw new TypeError('Matrix division is not defined');
}
let result = ( thisMatrix ) ? this : this.clone();
for (let i = 0, maxi = this.rows; i < maxi; i += 1) {
for (let j = 0, maxj = this.cols; j < maxj; j += 1) {
result.set(i, j, result.get(i, j) / m);
}
}
return result;
}
transpose() {
let result = new Matrix(this.cols, this.rows);
for (let i = 0, maxi = this.rows; i < maxi; i += 1) {
for (let j = 0, maxj = this.cols; j < maxj; j += 1) {
result.set(j, i, this.get(i, j));
}
}
return result;
}
clone() {
let result = new Matrix(this.rows, this.cols);
for (let i = 0, maxi = this.rows; i < maxi; i += 1) {
for (let j = 0, maxj = this.cols; j < maxj; j += 1) {
result.set(i, j, this.get(i, j));
}
}
return result;
}
setCol(col, c) {
if ( c < 0 || c >= this.cols ) {
throw new TypeError(`Column ${c} does not exists`);
}
if ( col.length != this.rows ) {
throw new TypeError('The length of this column does not match');
}
for (let i = 0, maxi = this.rows; i < maxi; i += 1) {
this.set(i, c, col[i]);
}
}
setRow(row, r) {
if ( r < 0 || r >= this.rows ) {
throw new TypeError(`Row ${r} does not exists`);
}
if ( ( Array.isArray(row) && row.length != this.cols) ||
( row instanceof Row && row.size != this.cols ) ) {
throw new TypeError('The length of this row does not match');
}
if ( Array.isArray(row) ) {
for (let i = 0, maxi = this.cols; i < maxi; i += 1) {
this.set(r, i, row[i]);
}
} else if ( row instanceof Row ) {
this.set(r, row);
}
}
delRow(r, thisMatrix) {
if ( r < 0 || r >= this.rows ) {
throw new TypeError(`Row ${r} does not exists`);
}
let result = ( thisMatrix ) ? this : this.clone();
result.matrix.splice(r, 1);
result.rows -= 1;
return result;
}
delCol(c, thisMatrix) {
if ( c < 0 || c >= this.cols ) {
throw new TypeError(`Column ${c} does not exists`);
}
let result = ( thisMatrix ) ? this : this.clone();
for (let i = 0, maxi = this.rows; i < maxi; i += 1) {
result.get(i).delAt(c);
}
result.cols -= 1;
return result;
}
det() {
if ( this.cols !== this.rows ) {
throw new TypeError('Undefined determinant of a non-squared matrix');
}
if ( this.rows === 1 ) {
return this.get(0, 0);
}
if ( this.rows === 2 ) {
return this.get(0, 0) * this.get(1, 1) - this.get(1, 0) * this.get(0, 1);
}
let res = 0;
let m1 = this.clone();
let m2;
// console.log('M1, M2');
m1.delRow(0, true);
m2 = m1.clone();
// m1.print();
// m2.print();
// console.log('/M1, M2');
for(let i = 0, j = 1; i < this.cols; i += 1, j = -j) {
res = res + m2.delCol(i).det() * this.get(0, i) * j;
}
return res;
}
randomize(a, b, isInt) {
let A = a || 0;
let B = b || 1;
let I = isInt || false;
for (let i = 0, maxi = this.rows; i < maxi; i += 1) {
for (let j = 0, maxj = this.cols; j < maxj; j += 1) {
this.set(i, j, A + Math.random() * (B - A));
if ( I ) {
this.set(i, j, ~~this.get(i, j));
}
}
}
}
print(label) {
let table = this.matrix.map(r => r.data);
if ( typeof label === 'string' ) {
console.info(label);
}
console.table(table);
}
appendRight(m, thisMatrix) {
if ( !(m instanceof Matrix) ) {
throw new TypeError('Only can append a matrix');
}
if ( this.rows != m.rows ) {
throw new TypeError('Can not append that matrix. Rows does not match');
}
let res = ( thisMatrix ) ? this : this.clone();
for (let i = 0, maxi = this.rows; i < maxi; i += 1) {
res.get(i).append( m.get(i), true );
}
res.cols += m.cols;
return res;
}
appendLeft(m, thisMatrix) {
if ( !(m instanceof Matrix) ) {
throw new TypeError('Only can append a matrix');
}
if ( this.rows != m.rows ) {
throw new TypeError('Can not append that matrix. Rows does not match');
}
let res = ( thisMatrix ) ? this : this.clone();
for (let i = 0, maxi = this.rows; i < maxi; i += 1) {
res.get(i).prepend( m.get(i), true );
}
res.cols += m.cols;
return res;
}
appendBottom(m, thisMatrix) {
if ( !(m instanceof Matrix) ) {
throw new TypeError('Only can append a matrix');
}
if ( this.cols != m.cols ) {
throw new TypeError('Can not append that matrix. Rows does not match');
}
let res = ( thisMatrix ) ? this : this.clone();
for (let i = 0, maxi = m.rows; i < maxi; i += 1) {
res.matrix.push( m.get(i).clone() );
}
res.rows += m.rows;
return res;
}
appendTop(m, thisMatrix) {
if ( !(m instanceof Matrix) ) {
throw new TypeError('Only can append a matrix');
}
if ( this.cols != m.cols ) {
throw new TypeError('Can not append that matrix. Rows does not match');
}
let res = ( thisMatrix ) ? this : this.clone();
for (let i = m.rows - 1; i >= 0; i -= 1) {
res.matrix.unshift( m.get(i).clone() );
}
res.rows += m.rows;
return res;
}
normalize() {
for (let i = 0, maxi = this.rows; i < maxi; i += 1) {
this.get(i).normalize();
}
}
to_esc(thisMatrix) {
let res = ( thisMatrix ) ? this : this.clone();
res.normalize();
while(true) {
res.matrix.sort((a, b) => a.clz - b.clz);
// sort(m.begin(), m.end(), cmp_clz<T>);
let ok = true;
for(let i = 1; i < res.rows && ok; i += 1) {
if( res.get(i).clz === res.get(i - 1).clz && res.get(i).clz != res.cols) {
ok = false;
let a = 0, b = 0;
a = res.get(i - 1, res.get(i).clz );
b = res.get(i, res.get(i).clz);
res.set(i, res.get(i - 1).mul(b).sub( res.get(i).mul(a) ) );
res.get(i).normalize();
}
}
if(ok) {
break;
}
}
return res;
}
exchangeRows(i, j) {
if ( i < 0 || i >= this.rows || j < 0 || j >= this.rows ) {
throw new ReferenceError('Out of range indexes');
}
for (let c = 0; c < this.cols; c += 1) {
let temp = this.get(i, c);
this.set(i, c, this.get(j, c));
this.set(j, c, temp);
}
}
GaussJordan() {
if ( this.rows > this.cols ) {
throw new TypeError('Can not apply Gauss-Jordan Elimination to this matrix');
}
let m = this.clone();
for (let i = 0; i < m.cols && i < m.rows; i += 1) {
// console.log("I = %d", i);
// m.clone().print();
let maxv = Math.abs( m.get(i, i) );
let maxp = i;
for (let j = i; j < m.rows; j += 1) {
if ( Math.abs( m.get(j, i) ) > maxv ) {
maxv = Math.abs( m.get(j, i) );
maxp = j;
}
}
if ( maxp != i ) {
m.exchangeRows(i, maxp);
}
// console.log('MAX_VALUE ', maxv, ' at ', maxp);
// m.clone().print();
if ( maxv > Matrix.EPS() ) {
let r = m.get(i);
let v = r.get(i);
for (let j = 0; j < m.rows; j += 1) {
if ( Math.abs( m.get(j, i) ) > Matrix.EPS() ) {
if ( j < i ) {
// console.log('ADJUST %d %d', j, i, v, m.get(j, i), m.get(j).sub( r.div(v) ));
// m.clone().print();
m.set(j, m.get(j).sub( r.mul( m.get(j, i) / v ) ) );
// m.clone().print();
} else if ( j > i ) {
// console.log('ADJUST %d %d', j, i, v, m.get(j, i), m.get(j).mul( v ).sub( r.mul( m.get(j, i) )));
// m.clone().print();
let mv = Math.max(v, m.get(j, i));
m.set(j, m.get(j).mul( v / mv ).sub( r.mul( m.get(j, i) / mv )) );
// m.clone().print();
}
}
}
r.div(v, true);
// console.log('FINAL ADJUST FOR COL %d', i);
// m.clone().print();
} else {
return new Matrix(m.rows, m.cols);
}
}
return m;
}
inv() {
if ( this.rows != this.cols ) {
throw new TypeError('Only NxN matrices have an inverse');
}
let mat = this.appendRight( Matrix.Identity( this.rows ) ).GaussJordan();
let v;
for (let i = 0; i < this.rows; i += 1) {
for (let j = 0; j < this.rows; j += 1) {
v = ( i === j ) ? 1 : 0;
if ( Math.abs(mat.get(i, j) - v) > Matrix.EPS() ) {
return new Matrix(this.rows, this.cols);
}
}
}
// console.log('GAUSS JORDAN');
// mat.print();
for(let i = 0; i < mat.rows; i += 1) {
mat.delCol(0, true);
}
return mat;
// return new Matrix(this.rows, this.cols);
}
dot(m, thisMatrix) {
if ( this.rows != m.rows || this.cols != m.cols ) {
throw new TypeError('Dot product does not apply on different sizes matrices');
}
let result = ( thisMatrix ) ? this : this.clone();
for (let i = 0; i < m.rows; i += 1) {
for (let j = 0; j < m.cols; j += 1) {
result.set(i, j, result.get(i, j) * m.get(i, j));
}
}
return result;
}
npdot(m) {
let prod = this.mul(m);
let res = new Matrix(prod.rows, 1);
for (let i = 0; i < prod.rows; i += 1) {
let sum = 0;
for (let j = 0; j < prod.cols; j += 1) {
sum += prod.get(i, j);
}
res.set(i, 0, sum);
}
return res;
}
agregate(fn) {
let func = ( typeof fn === 'function' ) ? fn : x => x;
let res = 0;
for (let i = 0; i < this.rows; i += 1) {
for (let j = 0; j < this.cols; j += 1) {
res += func( this.get(i, j) );
}
}
return res;
}
forEach(fn, thisMatrix) {
let func = ( typeof fn === 'function' ) ? fn : (x => x);
let result = this.clone();
for (let i = 0; i < result.rows; i += 1) {
for (let j = 0; j < result.cols; j += 1) {
result.set(i, j, func(this.get(i, j), i, j));
}
}
if ( thisMatrix ) {
for (let i = 0; i < result.rows; i += 1) {
for (let j = 0; j < result.cols; j += 1) {
this.set(i, j, result.get(i, j));
}
}
return this;
}
return result;
}
subCols(c1, c2) {
let ini = Math.max(0, Math.min( c1 || 0, c2 || this.cols ));
let fin = Math.min(this.cols, Math.max( c1 || 0, c2 || this.cols ));
let res = new Matrix(this.rows, fin - ini);
for (let i = 0; i < this.rows; i += 1) {
for (let j = ini; j < fin; j += 1) {
res.set(i, j - ini, this.get(i, j));
}
}
return res;
}
subRows(r1, r2) {
let ini = Math.max(0, Math.min( r1 || 0, r2 || this.rows ));
let fin = Math.min(this.rows, Math.max( r1 || 0, r2 || this.rows ));
// console.log('SubRows: ', ini, fin);
let res = new Matrix(fin - ini, this.cols);
for (let i = ini; i < fin; i += 1) {
for (let j = 0; j < this.cols; j += 1) {
res.set(i - ini, j, this.get(i, j));
}
}
return res;
}
submat(x1, y1, x2, y2) {
return this.subRows(x1, x2).subCols(y1, y2);
}
}