-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.js
More file actions
1550 lines (1354 loc) · 47.7 KB
/
matrix.js
File metadata and controls
1550 lines (1354 loc) · 47.7 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
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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
*
* The Matrix class represents an "n" by "m" dimensional vector
*
* Dependecies:
* - misc.js
* - vector.js
*
*/
class Matrix {
/**
*
* Creates a Matrix.
* The arguments can be:
* - Nothing: The Matrix will have 0 rows and 0 columns
* - One Matrix: In this case this is a copy constructor
* - One Vector, bool: The matrix will represent the Vector. if the bool argument is ommited or false the vector is treated as a row vector, if the bool === true the vector is treated as a column vector
* - A series of Objectswhiches can define a Vector: Creates Vectors from the Objects, and then creates a Matrix of those Vectors
* - An Array of the above: Works as the above
* - A JSON string of the above works the same
* - arguments[0] == 'init': arguments[1] an array where arguments[1][0] is the number of rows and the arguments[1][1] is the number of columns of this matrix (every dimension's vaule is arguments[2] or 0 if omitted)
* - arguments[0] an integer: arguments[0] as the number of columns then one number parameter for every element. The given numbers will be the element values from left to right then from top to bottom (the rest will be == 0)
*
*/
constructor(){
this.innerRep;
this.innerColNum;
if(arguments.length === 0){
this.constructorForEmpty();
} else if( isNumber(arguments[0]) ){
this.constructorForNumbers.apply(this, arguments);
} else if(typeof arguments[0] === 'object'){
if(Matrix.isMatrix(arguments[0]))
this.constructorForCopy.apply(this, arguments);
else if( Vector.isVector(arguments[0]) && (arguments.length == 1 || (arguments.length == 2 && typeof arguments[1] == 'boolean') ) ){
this.constructorForObjects(arguments[0]);
if(arguments.length == 2 && arguments[1] === true)
this.transpose();
}else if(Array.isArray(arguments[0]) && !isNumber(arguments[0][0])){
this.constructorForObjects.apply(this, arguments[0]);
} else
this.constructorForObjects.apply(this, arguments);
} else if(typeof arguments[0] === 'string'){
if(arguments[0] === 'init')
this.constructorForInit.apply(this, arguments);
else
this.constructorForObjects(JSON.parse(arguments[0]));
} else{
throw "Wrong argument(s)";
}
}
/**
*
* Helper for the constructor (not for outer useage)
* Constructor: Empty
*
*/
constructorForEmpty(){
this.innerRep = [];
this.innerColNum = 0;
}
/**
*
* Helper for the constructor (not for outer useage)
* Constructor: Copy
*
* @param {Matrix} matrix
*/
constructorForCopy(matrix){
this.innerRep = [];
this.innerColNum = matrix.colNum;
for(let row = 0; row < matrix.rowNum; row++){
this.innerRep.push([]);
for(let col = 0; col < matrix.colNum; col++){
this.innerRep[row].push(matrix.getElement(row, col));
}
}
}
/**
*
* Helper for the constructor (not for outer useage)
* Constructor: Object
*
* @param {...Object} objectArr
*/
constructorForObjects(...objectArr){
this.innerRep = [];
for(let row = 0; row < objectArr.length; row++){
this.innerRep.push([]);
let copyVector = Vector.vectorify(objectArr[row]);
if(this.innerColNum == undefined || this.innerColNum < copyVector.dimensions)
this.innerColNum = copyVector.dimensions;
for(let col = 0; col < copyVector.dimensions; col++){
this.innerRep[row].push(copyVector.getDimension(col));
}
}
this.expandCols(this.innerColNum);
}
/**
*
* Helper for the constructor (not for outer useage)
* Constructor: Init
*
* @param {Array} size
* @param {float} defaultValue
*/
constructorForInit(size, defaultValue = 0){
this.innerRep = [];
this.innerColNum = size[1];
for(let row = 0; row < size[0]; row++){
this.innerRep.push([]);
for(let col = 0; col < size[1]; col++){
this.innerRep[row].push(defaultValue);
}
}
}
/**
*
* Helper for the constructor (not for outer useage)
* Constructor: Numbers
*
* @param {int} columns
* @param {...float} elementArr
*/
constructorForNumbers(columns, ...elementArr){
this.innerRep = [];
this.innerColNum = columns;
let row = -1;
let col;
for(let element = 0; element < elementArr.length; element++){
col = element % columns;
if(col == 0){
this.innerRep.push([]);
row++;
}
this.innerRep[row].push(elementArr[element]);
}
while(col < columns){
col++;
this.innerRep[row].push(0);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
*
* Same as "new Matrix(this)"
*
*/
get copy(){
return new Matrix(this);
}
/**
*
* Returns it's size as an object
* It's y property is the number of rows and the x is the number of columns this matrix has
*
*/
get size(){
return {
y: this.rowNum,
x: this.colNum
};
}
/**
*
* Returns it's size as a Vector
* It's y property is the number of rows and the x is the number of columns this matrix has
*
*/
get sizeAsVec(){
return new Vector(this.colNum, this.rowNum);
}
/**
*
* Returns it's size as an Array
* The first number is the number of columns and the second is the number of rows this matrix has
*
*/
get sizeAsArr(){
return [
this.colNum,
this.rowNum
];
}
/**
*
* Extends or shrinks the matrix to possess the given number of rows and columns
*
* @param {Vectorify-able} size
*/
set size(size){
let sizeVec = Vector.vectorify(size);
this.rowNum = sizeVec.y;
this.colNum = sizeVec.x;
}
/**
*
* Extends or shrinks the matrix to possess the given number of rows and columns
*
* Returns itself so the function is chainable
*
* @param {Vectorify-able} size
*/
setSize(size){
this.size = size;
return this;
}
/**
*
* Returns whether or not this matrix is a square matrix
*
*/
get isSquareMatrix(){
return this.rowNum == this.colNum;
}
/**
*
* Returns the number of rows this matrix has
*
*/
get rowNum(){
return this.innerRep.length;
}
/**
*
* Extends or shrinks the matrix to possess the given number of rows
*
* @param {int} rowNum
*/
set rowNum(rowNum){
if(this.rowNum < rowNum)
this.expandRows(rowNum);
else if(rowNum < this.rowNum)
this.shrinkRows(rowNum);
}
/**
*
* Extends or shrinks the matrix to possess the given number of rows
*
* Returns itself so the function is chainable
*
* @param {int} rowNum
*/
setRowNum(rowNum){
this.rowNum = rowNum;
return this;
}
/**
*
* Returns the number of columns this matrix has
*
*/
get colNum(){
return this.innerColNum;
}
/**
*
* Extends or shrinks the matrix to possess the given number of columns
*
* @param {int} colNum
*/
set colNum(colNum){
if(this.colNum < colNum)
this.expandCols(colNum);
else if(colNum < this.colNum)
this.shrinkCols(colNum);
}
/**
*
* Extends or shrinks the matrix to possess the given number of columns
*
* Returns itself so the function is chainable
*
* @param {int} colNum
*/
setColNum(colNum){
this.colNum = colNum;
return this;
}
/**
*
* Returns a copy of the desired row as a Vector
*
* @param {int} row
*/
getRowVector(row){
return new Vector(this.getRowVectorAsArray(row));
}
/**
*
* Returns a copy of the desired row as an Array
*
* @param {int} row
*/
getRowVectorAsArray(row){
if(this.hasRow(row))
return this.innerRep[row].slice();
else
return createArray(this.colNum, Number);
}
/**
*
* Changes the given row to the copy of the given vector
* - If the given vector is too short it will be filled with 0s
* - If the given vector is too long, the matrix will be expanded to that size
*
* @param {int} rowNum
* @param {Vectorify-able} rowVec
*/
setRowVector(rowNum, rowVec){
let newRowVec = new Vector(rowVec);
for(let col = 0; col < newRowVec.dimensions; col++)
this.setElement(rowNum, col, newRowVec.getDimension(col));
for(let col = newRowVec.dimensions; col < this.rowNum; col++)
this.setElement(rowNum, col, 0);
}
/**
*
* Returns a copy of the desired column as a Vector
*
* @param {int} column
*/
getColVector(column){
return new Vector(this.getColVectorAsArray(column));
}
/**
*
* Returns a copy of the desired column as an Array
*
* @param {int} column
*/
getColVectorAsArray(column){
if(this.hasCol(column)){
let res = [];
for(let row = 0; row < this.rowNum; row++){
res.push(this.innerRep[row][column]);
}
return res;
} else
return createArray(this.rowNum, Number);
}
/**
*
* Changes the given column to the copy of the given vector
* - If the given vector is too short it will be filled with 0s
* - If the given vector is too long, the matrix will be expanded to that size
*
* @param {int} colNum
* @param {Vectorify-able} colVec
*/
setColVector(colNum, colVec){
let newColVec = new Vector(colVec);
for(let row = 0; row < newColVec.dimensions; row++)
this.setElement(row, colNum, newColVec.getDimension(row));
for(let row = newColVec.dimensions; row < this.rowNum; row++)
this.setElement(row, colNum, 0);
}
/**
*
* Returns a given element (counts from 0,0)
* If the parameters are greater then this matrix's sizes or less then 0 then this returns 0
*
* @param {int} row
* @param {int} column
*/
getElement(row, column){
if(this.hasElement(row, column))
return this.innerRep[row][column];
else
return 0;
}
/**
*
* Sets the given element to the given value
* Extends this matrix if it has to.
*
* @param {int} row
* @param {int} column
* @param {float} value
*/
setElement(row, column, value){
if(!this.hasRow(row))
this.expandRows(row);
if(!this.hasCol(column))
this.expandCols(column);
this.innerRep[row][column] = parseInt(value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
*
* Adds the minimum number of rows and coulumns to the matrix so there wouldn't be less then the given amount
*
* @param {*} rows
* @param {*} columns
*/
expand(rows, columns){
this.expandRows(rows);
this.expandCols(columns);
}
/**
*
* Removes the minimum number of rows and coulumns from the matrix so there wouldn't be more then the given amount
*
* @param {*} rows
* @param {*} columns
*/
shrink(rows, columns){
this.shrinkRows(rows);
this.shrinkCols(columns);
}
/**
*
* Adds the minimum number of rows to the matrix so there wouldn't be less then the given amount
*
* @param {*} rows
*/
expandRows(rows){
while(this.rowNum < rows){
this.innerRep.push(createArray(this.colNum, Number));
}
}
/**
*
* Removes the minimum number of rows from the matrix so there wouldn't be more then the given amount
*
* @param {*} rows
*/
shrinkRows(rows){
while(rows < this.rowNum){
this.innerRep.pop();
}
}
/**
*
* Adds the minimum number of coulumns to the matrix so there wouldn't be less then the given amount
*
* @param {*} columns
*/
expandCols(columns){
for(let row = 0; row < this.rowNum; row++){
while(this.innerRep[row].length < columns){
this.innerRep[row].push(0);
}
}
this.innerColNum = columns;
}
/**
*
* Removes the minimum number of coulumns from the matrix so there wouldn't be more then the given amount
*
* @param {*} columns
*/
shrinkCols(columns){
for(let row = 0; row < this.rowNum; row++){
while(columns < this.innerRep[row].length){
this.innerRep[row].pop();
}
}
this.innerColNum = columns;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
*
* Inserts a new row and column filled with the given value
*
* Returns itself so the function is chainable
*
* @param {int} row
* @param {int} column
* @param {float} value 0 by default
* @param {int} rowCount 1 by default
* @param {int} columnCount 1 by default
*/
insert(row, column, value = 0, rowCount = 1, columnCount = 1){
for(let i = 0; i < rowCount; i++)
this.insertRow(row, value);
for(let i = 0; i < columnCount; i++)
this.insertCol(column, value);
return this;
}
/**
*
* Removes the given rows and columns
*
* Returns itself so the function is chainable
*
* @param {int} row
* @param {int} column
* @param {int} rowCount 1 by default
* @param {int} columnCount 1 by default
*/
remove(row, column, rowCount = 1, columnCount = 1){
this.removeRow(row, rowCount);
this.removeCol(column, columnCount);
return this;
}
/**
*
* Follows the rules of Array.splice, except it returns the removed elements as a marix
*
* Returns the removed elements as a matrix
*
* @param {int} start
* @param {int} removeCount
* @param {(...Vectory-able|float)} newRows
*/
spliceRows(start, removeCount = this.rowNum - start, ...newRows){
let removed = [];
for(let i = 0; i < removeCount; i++){
removed.push(this.innerRep[start]);
this.removeRow(start);
}
for(let i = 0; i < newRows.length; i++){
this.insertRow(start + i, newRows[i]);
}
return new Matrix(removed);
}
/**
*
* Inserts a new row filled with the given value (0 by default)
*
* Returns itself so the function is chainable
*
* @param {int} row
* @param {Vectorify-able|float} value
*/
insertRow(row, value){
let newRow;
if(isNumber(value))
newRow = new Vector('init', this.colNum, value).toArray();
else{
newRow = Vector.vectorify(value).promote(this.colNum).toArray();
this.expandCols(newRow.length);
}
this.innerRep.splice(row, 0, newRow);
return this;
}
/**
*
* Removes the given rows
*
* Returns itself so the function is chainable
*
* @param {int} row
* @param {int} rowCount 1 by default
*/
removeRow(row, rowCount = 1){
this.innerRep.splice(row, rowCount);
return this;
}
/**
*
* Follows the rules of Array.splice, except it returns the removed elements as a marix
*
* Returns the removed elements as a matrix
*
* @param {int} start
* @param {int} removeCount
* @param {(...Vectory-able|float)} newCols
*/
spliceCols(start, removeCount = this.rowNum - start, ...newCols){
let removed = [];
for(let i = 0; i < removeCount; i++){
removed.push(this.getColVectorAsArray(start));
this.removeCol(start);
}
for(let i = 0; i < newCols.length; i++){
this.insertCol(start + i, newCols[i]);
}
return new Matrix(removed);
}
/**
*
* Inserts a new column filled with the given value (0 by default)
*
* Returns itself so the function is chainable
*
* @param {int} column
* @param {Vectorify-able|float} value
*/
insertCol(column, value){
let newCol;
if(isNumber(value))
newCol = new Vector('init', this.rowNum, value).toArray();
else{
newCol = Vector.vectorify(value).promote(this.rowNum).toArray();
this.expandRows(newCol.length);
}
for(let row = 0; row < this.rowNum; row++)
this.innerRep[row].splice(column, 0, newCol[row]);
return this;
}
/**
*
* Removes the given columns
*
* Returns itself so the function is chainable
*
* @param {int} column
* @param {int} columnCount 1 by default
*/
removeCol(column, columnCount = 1){
let removedArr;
for(let row = 0; row < this.rowNum; row++)
removedArr = this.innerRep[row].splice(mathMod(column, this.colNum), columnCount);
this.innerColNum -= removedArr.length;
return this;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
*
* Changes this to the copy of this matrix's given submatrix (follows the rules of the Array.slice function)
*
* Returns itself so the function is chainable
*
* @param {Vectorify-able} start
* @param {Vectorify-able} end
*/
subMatrix(start = new Vector(0,0), end = this.sizeAsVec){
this.innerRep = this.toArray(start, end);
this.innerColNum = end.x - start.x;
return this;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
*
* Negates its elements' values.
*
*/
negate(){
this.map(element => -element);
return this;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
*
* Adds the given parameter to this.
*
* If the arguments are a(n):
* - Matrix: Adds the given matrix's elements to this matrix's elements one by one. Extends this matrix if it has to.
* - Object: Attemps to convert it to a Matrix then proceeds as it was one.
* - Vectorify-able, int, bool: Adds the given vector's coordinates to this matrix's "num"th row or column one by one (row by default, and column if the 3rd parameter === true). Extends the matrix if it has to.
* - Number: The number will be added to the matrix's every element.
*
* Returns itself so the function is chainable
*
* @param {Matrix|Vector|Object|float} addition
* @param {int} num
* @param {bool} isColVector
*/
add(addition, num, isColVector){
if(isNumber(addition)){
this.map(element => element + addition);
} else if(arguments.length > 1){
this.addVec(Vector.vectorify(addition, num, isColVector));
} else if(typeof addition == 'object'){
let realMat = Matrix.matrixify(addition);
this.expand(realMat.rowNum, realMat.colNum);
this.map((element, rowInd, colInd) => element + matrix.getElement(rowInd, colInd));
} else{
throw "Wrong arguments";
}
return this;
}
/**
*
* Adds the given vector's coordinates to this matrix one by one.
* Extends this matrix if it has to.
*
* Returns itself so the function is chainable
*
* @param {Vector} vector
* @param {int} num Which row / col
* @param {bool} isColVector
*/
addVec(vector, num, isColVector){
if(isColVector === true)
this.addColVec(vector, num);
else
this.addRowVec(vector, num);
return this;
}
/**
*
* Adds the given vector's coordinates to this matrix's given row one by one.
* Extends this matrix if it has to.
*
* Returns itself so the function is chainable
*
* @param {Vector} vector
* @param {int} row
*/
addRowVec(vector, row){
for(let col = 0; col < vector.dimensions; col++)
this.setElement(row, col, this.getElement(row, col) + vector.getDimension(col) );
return this;
}
/**
*
* Adds the given vector's coordinates to this matrix's given column one by one.
* Extends this matrix if it has to.
*
* Returns itself so the function is chainable
*
* @param {Vector} vector
* @param {int} column
*/
addColVec(vector, column){
for(let row = 0; row < vector.dimensions; row++)
this.setElement(row, column, this.getElement(row, column) + vector.getDimension(row) );
return this;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
*
* Subs the given parameter from this.
*
* If the arguments are a(n):
* - Matrix: Subs the given matrix's elements from this matrix's elements one by one. Extends this matrix if it has to.
* - Object: Attemps to convert it to a Matrix then proceeds as it was one.
* - Vectorify-able, int, bool: Subs the given vector's coordinates from this matrix's "num"th row or column one by one (row by default, and column if the 3rd parameter === true). Extends the matrix if it has to.
* - Number: The number will be subbed from the matrix's every element.
*
* Returns itself so the function is chainable
*
* @param {Matrix|Vector|Object|float} substraction
* @param {int} num
* @param {bool} isColVector
*/
sub(substraction, num, isColVector){
if(isNumber(substraction)){
this.subNum(substraction);
} else if(arguments.length > 1){
this.subVec(Vector.vectorify(substraction, num, isColVector));
} else if(typeof substraction == 'object'){
this.subMat(Matrix.matrixify(substraction));
} else{
throw "Wrong arguments";
}
return this;
}
/**
*
* The number will be subbed from the matrix's every element.
*
* Returns itself so the function is chainable
*
* @param {float} number
*/
subNum(number){
this.map(element => element - number);
return this;
}
/**
*
* Subs the given matrix's elements from this matrix's elements one by one.
* Extends this matrix if it has to.
*
* Returns itself so the function is chainable
*
* @param {Matrix} matrix
*/
subMat(matrix){
for(let row = 0; row < matrix.rowNum; row++)
for(let col = 0; col < matrix.colNum; col++)
this.setElement(row, col, this.getElement(row, col) - matrix.getElement(row, col) );
return this;
}
/**
*
* Subs the given vector's coordinates from this matrix's "num"th row or column one by one (row by default, and column if the 3rd parameter === true).
* Extends the matrix if it has to.
*
* Returns itself so the function is chainable
*
* @param {Vector} vector
* @param {int} num
* @param {bool} isColVector
*/
subVec(vector, num, isColVector){
if(isColVector === true)
this.subColVec(vector, num);
else
this.subRowVec(vector, num);
return this;
}
/**
*
* Adds the given vector's coordinates to this matrix's given row one by one.
* Extends this matrix if it has to.
*
* Returns itself so the function is chainable
*
* @param {Vector} vector
* @param {int} row
*/
subRowVec(vector, row){
for(let col = 0; col < vector.dimensions; col++)
this.setElement(row, col, this.getElement(row, col) - vector.getDimension(col) );
return this;
}
/**
*
* Adds the given vector's coordinates to this matrix's given column one by one.
* Extends this matrix if it has to.
*
* Returns itself so the function is chainable
*
* @param {Vector} vector
* @param {int} column
*/
subColVec(vector, column){
for(let row = 0; row < vector.dimensions; row++)
this.setElement(row, column, this.getElement(row, column) - vector.getDimension(row) );
return this;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
*
* Multiplies this with the given parameter.
*
* If the argument is a(n):
* - Matrix: Returns the cross product of this and the k vectors in order. In case of k vector, the result will be k+2 dimensional and the calculation uses k+2 dimensions from all of them. The cross product is NOT commutative, thus the order of the vectors count.
* - k Vector|Object: Attemps to convert the Objects to Vectors then proceeds as they were one.
* - Number: Every dimension will be multiplied with the given number.
*
* Returns itself so the function is chainable
*
* @param {Matrixify-able|float} multiplier
* @param {bool} fromLeft
*/
mul(multiplier, fromLeft = false){
if(isNumber(multiplier))
this.mulNum(multiplier);
else if(typeof multiplier == 'object'){
mulMat(Matrix.matrixify(multiplier), fromLeft);
} else
throw 'Wrong arguments';
return this;
}
mulMat(matrix, fromLeft = false){
if(fromLeft)
mulLeftMat(matrix);
else
mulRightMat(matrix);
return this;
}
mulRightMat(matrix){
this.constructorForCopy( Matrix.mul(this, matrix) );
return this;
}
mulLeftMat(matrix){
this.constructorForCopy( Matrix.mul(matrix, this) );
return this;
}
mulNum(number){
this.map(element => element * number);
return this;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
*
* Div function for every element one by one
*
* Returns itself so the function is chainable