-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.js
More file actions
731 lines (505 loc) · 22.1 KB
/
Matrix.js
File metadata and controls
731 lines (505 loc) · 22.1 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
// This will be my matrix library to use with the neural network library I'm making
// My matrix class
class Matrix {
// The constructor
constructor(rows, cols) {
// Remembering how many rows and columns there are
this.rows = rows || 1;
this.cols = cols || 1;
// Making a 2D array that will hold the values
this.data = Array.from({ length: this.rows }, () => Array(this.cols).fill(0));
}
// This will be my multiplication/division method
mult(other) {
// Checking to see if it is matrix multiplication or just scalar multiplication
if (typeof other === 'number') {
// Making a new matrix
let newMat = new Matrix(this.rows, this.cols);
// Multiplying every value by the scalar
for (let i = 0; i < this.rows; i++) {
// Checking multiple columns
for (let j = 0; j < this.cols; j++) {
newMat.data[i][j] = this.data[i][j] * other;
}
}
// Setting this matrix to the new one
Object.assign(this, newMat);
// Returning the new one
return this;
}
// Checking to see if it is matrix multiplication
if (other instanceof Matrix) {
// It is in fact matrix multiplication, so here is where the fun begins
// Checking the sizes to make sure they work
if (this.cols != other.rows) {
console.log("The columns in the first inputted Matrix must be the same as the rows in the second inputted Matrix.");
console.log("Matrix a:");
this.print();
console.log("Matrix b:");
other.print();
throw new Error("MatrixMultiplicationSizeError");
}
// The matrix we will be returning
let newMat = new Matrix(this.rows, other.cols);
// Actually doing the multiplication
for (let row = 0; row < newMat.rows; row++) {
for (let col = 0; col < newMat.cols; col++) {
// We need to multiply every element in the row of the first matrix by every element in the column of the second matrix
// The shared dimension would be the number in the row/column we are looking at
let sum = 0;
for (let i = 0; i < this.cols; i++) {
sum += this.data[row][i] * other.data[i][col];
}
// Now we just need to set the new matrix to include the data
newMat.data[row][col] = sum;
}
}
// Setting this matrix and returning self
Object.assign(this, newMat);
return this;
}
// They didn't put in a useful input
console.log("The inputted argument of the Matrix multiplication method must be another Matrix object or a number.");
throw new Error("MatrixMultiplicationTypeError");
}
// The element wise multiplication method
elementMult(other) {
// Making sure that the input is another Matrix object
if (!(other instanceof Matrix) && !(typeof other === 'number')) {
console.log("The argument in the element-wise multiplication must also be a Matrix object or a number.");
throw new Error("ElementWiseMatrixMultiplicationTypeError");
}
// If it is a number, I can just call the normal mult one
if (typeof other === 'number') {
return this.mult(other);
}
// Checking to make sure they are the same size
if (this.cols != other.cols || this.rows != other.rows) {
console.log("For element-wise Matrix multiplication, both matrices must have the same dimensions.")
console.log("Matrix a:");
this.print();
console.log("Matrix b:");
other.print();
throw new Error("ElementWiseMatrixMultiplicationSizeError");
}
// Looping through this matrix to multiply each by each in the other
for (let row = 0; row < this.rows; row++) {
for (let col = 0; col < this.cols; col++) {
// Actually doing the multiplication
this.data[row][col] *= other.data[row][col];
}
}
return this;
}
// This will be the add/subtract method
add(input) {
let newMat = new Matrix(this.rows, this.cols);
// Checking to see if it is a scalar multiplication
if (typeof input === 'number') {
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.cols; j++) {
newMat.data[i][j] = this.data[i][j] + input;
}
}
// Setting this and returning this
Object.assign(this, newMat);
return this;
}
if (input instanceof Matrix) {
if (this.rows !== input.rows || this.cols !== input.cols) {
console.log("Two added matrices must have the same dimensions.");
console.log("Matrix a:");
this.print();
console.log("Matrix b:");
input.print();
throw new Error("MatrixAdditionSizeError");
}
// Actually adding
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.cols; j++) {
newMat.data[i][j] = this.data[i][j] + input.data[i][j];
}
}
// Resetting and returning
Object.assign(this, newMat);
return this;
}
console.log("The input to the Matrix addition method must be a number or another Matrix object.");
throw new Error("MatrixAdditionTypeError");
}
// Just a method to make subtraction easier
subtract(input) {
if (input instanceof Matrix) {
return this.add(input.mult(-1));
} else if (typeof input === 'number') {
return this.add(input * -1);
}
// There must be an invalid input here
console.log("The input to the subtract method must be another Matrix object or a number.");
throw new Error("MatrixSubtractionTypeError");
}
// This will be to set the matrix to a new matrix
set(newMat) {
// To avoid any weird pointers and reference variables, I'm going to hardcode a deep copy
// Clearing the matrix
this.data = [];
// Check if newMat is a 1D array (vector)
if (newMat[0] === undefined || typeof newMat[0] !== 'object') {
// Setting it up for 1D array
for (let i = 0; i < newMat.length; i++) {
this.data[i] = [Number(newMat[i])];
}
this.rows = newMat.length;
this.cols = 1;
} else {
// Setting it up for 2D array
for (let i = 0; i < newMat.length; i++) {
this.data[i] = [];
for (let j = 0; j < newMat[i].length; j++) {
this.data[i][j] = Number(newMat[i][j]);
}
}
this.rows = newMat.length;
this.cols = newMat[0].length;
}
// Returning self because that can be useful
return this;
}
// A method that returns a copy of the matrix
copy() {
// First to make a new one to return
let newMat = new Matrix(this.rows, this.cols);
// Looping to recreate the data
newMat.data = [];
for (let i = 0; i < this.data.length; i++) {
newMat.data.push(this.data[i].slice());
}
// Returning the new matrix
return newMat;
// There might be an error here; I was watching the Olympics at the same time ¯\_(ツ)_/¯
}
// This will be the dot product one
dot(other) {
// Checking to make sure it is a matrix
if (!(other instanceof Matrix)) {
console.error("Dot product requires another Matrix instance.");
throw new Error("DotProductTypeError");
}
// Checking to make sure that the sizes match
if (this.cols !== 1 || other.cols !== 1 || this.rows !== other.rows) {
console.error("Both matrices must be vectors of the same length.");
throw new Error("DotProductSizeError");
}
// Actually calculating the sum
let sum = 0;
for (let i = 0; i < this.rows; i++) {
sum += this.data[i][0] * other.data[i][0];
}
return sum;
}
// A function to randomize the matrix
randomize() {
// Looping for each element to randomize it
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.cols; j++) {
this.data[i][j] = Math.random();
}
}
return this;
}
// A function to floor each of the elements in case I only want integers
floor() {
// Looping for each of the elements of
for (let i = 0; i < this.data.length; i++) {
for (let j = 0; j < this.data[0].length; j++) {
this.data[i][j] = Math.floor(this.data[i][j]);
}
}
return this;
}
// A function to transpose the elements of the matrix
transpose() {
// The matrix we want to return
let newMat = new Matrix(this.cols, this.rows);
// Transpose the elements
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.cols; j++) {
newMat.data[j][i] = this.data[i][j];
}
}
// Resetting and returning
Object.assign(this, newMat);
return this;
}
// A nice and easy print function
print() {
console.table(this.data);
// Returning itself so that it can be used to track assignment operations
return this;
}
// A function that will simply apply a function to every single element
map(fn) {
// The Matrix to return
let newMat = new Matrix(this.rows, this.cols);
for (let i = 0; i < this.rows; i++) {
// Checking multiple columns
if (this.cols > 1) {
for (let j = 0; j < this.cols; j++) {
newMat.data[i][j] = fn(this.data[i][j], i, j);
}
} else {
newMat.data[i] = [fn(this.data[i][0], i, 0)];
}
}
// Resetting and returning
Object.assign(this, newMat);
return this;
}
// A method that will return the data as an array
toArray() {
// Deep copy of the data to avoid direct reference issues
return this.data.map(row => row.slice());
}
// -------------------STATIC METHODS-----------------------
// These will return a new matrix object for each
// The multiplication method
static mult(one, two) {
// Checking to make sure that the first one is a matrix object
if (!(one instanceof Matrix)) {
console.log("The first input into the static matrix multiplication must be a matrix.");
throw new Error("StaticMultiplicationTypeError");
}
// Checking to see if it is scalar multiplication
if (typeof two === 'number') {
// Making a new matrix
let newMat = new Matrix(one.rows, one.cols);
// Multiplying every value by the scalar
for (let i = 0; i < one.rows; i++) {
// Checking multiple columns
for (let j = 0; j < one.cols; j++) {
newMat.data[i][j] = one.data[i][j] * two;
}
}
// Returning the new one
return newMat;
}
// Checking to see if it is matrix multiplication
if (two instanceof Matrix) {
// It is in fact matrix multiplication, so here is where the fun begins
// Checking the sizes to make sure they work
if (one.cols != two.rows) {
console.log("Incorrect sizes in static matrix multiplication.");
console.log("Matrix a:");
one.print();
console.log("Matrix b:");
two.print();
throw new Error("StaticMultiplicationSizeError");
}
// The matrix we will be returning
let newMat = new Matrix(one.rows, two.cols);
// Actually doing the multiplication
for (let row = 0; row < newMat.rows; row++) {
for (let col = 0; col < newMat.cols; col++) {
// We need to multiply every element in the row of the first matrix by every element in the column of the second matrix
// The shared dimension would be the number in the row/column we are looking at
let sum = 0;
for (let i = 0; i < one.cols; i++) {
sum += one.data[row][i] * two.data[i][col];
}
// Now we just need to set the new matrix to include the data
newMat.data[row][col] = sum;
}
}
return newMat;
}
// They didn't put in a useful input
console.log("The second argument in static matrix multiplication must be a number or a Matrix object.");
throw new Error("InvalidInputMultiplicationError");
}
// The static element wise multiplication
static elementMult(one, two) {
// Making sure that the inputs are Matrix object.
if (!(two instanceof Matrix) && !(typeof two === 'number')) {
console.log("The second argument in the static element-wise multiplication must also be a Matrix object or a number.");
throw new Error("StaticElementWiseMatrixMultiplicationTypeError");
}
if (!(one instanceof Matrix) && !(typeof one === 'number')) {
console.log("The first argument in the static element-wise multiplication must also be a Matrix object or a number.");
throw new Error("StaticElementWiseMatrixMultiplicationTypeError");
}
// If it is a number, I can just call the normal mult one
if (typeof two === 'number') {
return one.mult(two);
}
// Checking to make sure they are the same size
if (one.cols != two.cols || one.rows != two.rows) {
console.log("For element-wise Matrix multiplication, both matrices must have the same dimensions.");
console.log("Matrix a:");
one.print();
console.log("Matrix b:");
two.print();
throw new Error("ElementWiseMatrixMultiplicationSizeError");
}
// The thing to return
let newMat = new Matrix(one.rows, one.cols);
// Looping through this matrix to multiply each by each in the other
for (let row = 0; row < one.rows; row++) {
for (let col = 0; col < one.cols; col++) {
// Actually doing the multiplication
newMat.data[row][col] = one.data[one][two] * two.data[row][col];
}
}
return newMat;
}
// The addition method
static add(one, two) {
let newMat = new Matrix(one.rows, one.cols);
// Checking to see if it is a simple scalar
if (typeof two === 'number') {
for (let i = 0; i < one.rows; i++) {
for (let j = 0; j < one.cols; j++) {
newMat.data[i][j] = one.data[i][j] + two;
}
}
return newMat;
}
// Checking to see if it's matrix addition
if (two instanceof Matrix) {
// Checking the sizes
if (one.rows !== two.rows || one.cols !== two.cols) {
// Throwing size errors
console.log("The the added matrices must have the same dimensions.");
console.log("Matrix a:");
one.print();
console.log("Matrix b:");
two.print();
throw new Error("StaticMatrixAdditionSizeError");
}
// Doing the addition
for (let i = 0; i < one.rows; i++) {
for (let j = 0; j < one.cols; j++) {
newMat.data[i][j] = one.data[i][j] + two.data[i][j];
}
}
return newMat;
}
console.log("Invalid addition input.");
throw new Error("StaticInvalidAdditionInputError");
}
// The static subtraction method
static subtract(one, two) {
// Making sure the first input is actually a Matrix object
if (!(one instanceof Matrix)) {
console.log("The first argument in the static matrix subtraction must be a Matrix object.");
throw new Error("StaticMatrixSubtractionTypeError");
}
// Checking to see if it is scalar subtraction
if (typeof two === 'number') {
return Matrix.add(one, two * -1);
}
// Checking to see if it is matrix subtraction
if (two instanceof Matrix) {
return Matrix.add(one, Matrix.mult(two, -1));
}
// If it got here, then there is a type error with the second argument
console.log("The second argument in the static matrix subtraction call must be either a number or a Matrix object.");
throw new Error("StaticMatrixSubtractionTypeError");
}
// The static set method, but it will be a from array method instead
static fromArray(arr) {
// Returning a new matrix
return new Matrix(arr.length, arr[0].length).set(arr);
}
// The static dot product method
static dot(one, two) {
// Checking to make sure it is a matrix
if (!(one instanceof Matrix) || !(two instanceof Matrix)) {
console.log("Both inputs to the static dot product method must be Matrix objects.");
throw new Error("StaticDotProductTypeError");
}
// Checking to make sure that the sizes match
if (one.cols !== 1 || two.cols !== 1 || one.rows !== two.rows) {
console.error("Both matrices in the dot product must be vectors of the same length.");
throw new Error("StaticDotProductSizeError");
}
// Actually calculating the sum
let sum = 0;
for (let i = 0; i < one.rows; i++) {
sum += one.data[i][0] * two.data[i][0];
}
return sum;
}
// The static transposition method
static transpose(one) {
// Checking to make sure the input is a Matrix object
if (!(one instanceof Matrix)) {
console.log("The static transpose method requires an input of a Matrix.");
throw new Error("StaticTransposeTypeError");
}
// The matrix we want to return
let newMat = new Matrix(one.cols, one.rows);
// Transpose the elements
for (let i = 0; i < one.rows; i++) {
for (let j = 0; j < one.cols; j++) {
newMat.data[j][i] = one.data[i][j];
}
}
return newMat;
}
// The static map function
static map(mat, fn) {
// Checking to make sure that the matrix input is actually a matrix
if (!(mat instanceof Matrix)) {
console.log("The first argument in the static map function must be a Matrix object.");
throw new Error("StaticMapTypeError");
}
// The Matrix to return
let newMat = new Matrix(mat.rows, mat.cols);
// Looping through and applying the function
for (let i = 0; i < mat.rows; i++) {
for (let j = 0; j < mat.cols; j++) {
newMat.data[i][j] = fn(mat.data[i][j], i, j);
}
}
return newMat;
}
// This one is going to be a simple soft max function
// What it does is normalize all of the data inside the Matrix to be percentages
softMax() {
// First, we need to find the sum of e raised to all of the elements
let sum = 0;
for (let row = 0; row < this.rows; row++) {
for (let col = 0; col < this.cols; col++) {
sum += Math.exp(this.data[row][col]);
}
}
// Now we need to replace each element with e raised to the element divided by the sum
for (let row = 0; row < this.rows; row++) {
for (let col = 0; col < this.cols; col++) {
this.data[row][col] = Math.exp(this.data[row][col]) / sum;
}
}
// Returning self
return this;
}
// The static version of the soft max
static softMax(mat) {
// First, we need to find the sum of e raised to all of the elements
let sum = 0;
for (let row = 0; row < mat.rows; row++) {
for (let col = 0; col < mat.cols; col++) {
sum += Math.exp(mat.data[row][col]);
}
}
// The new matrix that we want to return
let newMat = new Matrix(mat.rows, mat.cols);
// Now we need to replace each element with e raised to the element divided by the sum
for (let row = 0; row < mat.rows; row++) {
for (let col = 0; col < mat.cols; col++) {
newMat.data[row][col] = Math.exp(mat.data[row][col]) / sum;
}
}
// Returning the new matrix
return newMat;
}
}