-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
844 lines (552 loc) · 24.7 KB
/
Matrix.java
File metadata and controls
844 lines (552 loc) · 24.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
package MatrixLibrary;
import java.util.Arrays;
// This shall be the Java matrix library
// *YIPPEE*
/*
*
* I think it might be worth mentioning the layout of the data
*
* data = {
* {blah, blah, blah},
* {blah, blah, blah},
* {blah, blah, blah}
* }
*
* This is kind of weird because some places might visualize it differently(I would add an example but I don't want to make it
* any more confusing) so just know that to reference things from the data in the data array from the Matrix class you need to use
*
* matrixObject.data[row][column]
*
* Have a great day!
*
*/
public class Matrix {
public static void main(String[] args) {
double[][] data1 = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
double[] data2 = {
1, 2, 3, 4, 5
};
Matrix mat = new Matrix(data2);
Matrix mat1 = new Matrix(mat.toVector());
mat1.print();
// activationFunction sigmoid = x -> 1 / (1 + Math.exp(-x));
// mat.map(sigmoid).print();
}
// PIVs ------------------------------------------
// The dimensions of the matrix
private int rows;
private int cols;
// The data in the matrix
private double[][] data;
// The random constructor
public Matrix(int numRows, int numCols) {
// Taking down the dimensions
this.rows = numRows;
this.cols = numCols;
// We just want a random matrix with this constructor, so here is where we can randomize it
this.data = new double[this.rows][this.cols];
// Looping for every value in the matrix
for (int rowNum = 0; rowNum < this.rows; rowNum++) {
for (int colNum = 0; colNum < this.cols; colNum++) {
this.data[rowNum][colNum] = Math.random();
}
}
}
// The non random constructor
public Matrix(double[][] inputData) {
// Setting the dimensions
this.rows = inputData.length;
this.cols = inputData[0].length;
// Declaring the data variable
this.data = new double[this.rows][this.cols];
// Running a deep copy so that we can make sure there aren't any reference errors
for (int i = 0; i < this.rows; i++) {
this.data[i] = Arrays.copyOf(inputData[i], inputData[i].length);
}
}
// Another non random constructor for vectors
public Matrix(double[] inputData) {
// Setting the dimensions
this.rows = inputData.length;
this.cols = 1;
// Declaring the data variable
this.data = new double[this.rows][this.cols];
// Running a deep copy so that we can make sure there aren't any reference errors
for (int i = 0; i < this.rows; i++) {
this.data[i][0] = inputData[i];
}
}
// The copy constructor
public Matrix(Matrix toCopy) {
// All we really need to do is just the same thing as in another constructor
// Having two just makes it easier for the end user
// Setting the dimensions
this.rows = toCopy.data.length;
this.cols = toCopy.data[0].length;
// Declaring the data variable
this.data = new double[this.rows][this.cols];
// Running a deep copy so that we can make sure there aren't any reference errors
for (int i = 0; i < this.rows; i++) {
this.data[i] = Arrays.copyOf(toCopy.data[i], toCopy.data[i].length);
}
}
// The empty constructor
public Matrix() {
this.cols = 0;
this.rows = 0;
this.data = null;
}
// Now for the methods ---------------------------------------------------------
// A simple print function (not too fancy unfortunately)
public Matrix print() {
// The maximum number of digits
final int MAX_DIGITS = 5;
// Looping for every value in the matrix
for (int rowNum = 0; rowNum < this.rows; rowNum++) {
for (int colNum = 0; colNum < this.cols; colNum++) {
// We want to limit the string length so that we can make sure that all of the squares are nice and pretty
String str = "" + this.data[rowNum][colNum];
// If it is already bigger than the max length, we can truncate it and be done
if (str.length() > MAX_DIGITS) {
str = str.substring(0, 5);
} else {
// It is smaller so we need to append spaces to the end to make it the same size
while (str.length() < MAX_DIGITS) {
str = " " + str;
}
}
// Adding on the style :D
if (colNum == 0) {
str = "| " + str;
if (colNum == this.cols - 1) {
str += " |";
} else {
str += ", ";
}
} else if (colNum == this.cols - 1) {
str += " |";
} else {
str += ", ";
}
// Printing out this element
System.out.print(str);
}
// Adding on the carriage return
System.out.println();
}
// Adding on another carriage return so that we can print a bunch in a row
System.out.println();
// Return self
return this;
}
//--------------------------------------------------Multiplication---------------------------------------------------------------
// The scalar element multiplication method
public Matrix elementMult(double scalar) {
// This specific element multiplication method is going to be the scalar one, but there will also be
// one for Matrices that are the same size
// Looping for every value in the matrix to multiply by the scalar
for (int rowNum = 0; rowNum < this.rows; rowNum++) {
for (int colNum = 0; colNum < this.cols; colNum++) {
this.data[rowNum][colNum] *= scalar;
}
}
// This is not a static method, so it changes itself and returns self
return this;
}
// The static scalar element multiplication method
public static Matrix elementMult(Matrix mat, double scalar) {
// The new matrix that we will be returning
Matrix newMat = new Matrix();
newMat.rows = mat.rows;
newMat.cols = mat.cols;
newMat.data = new double[mat.rows][mat.cols];
// Looping for every value in the matrix to multiply by the scalar
for (int rowNum = 0; rowNum < mat.rows; rowNum++) {
for (int colNum = 0; colNum < mat.cols; colNum++) {
newMat.data[rowNum][colNum] = mat.data[rowNum][colNum] * scalar;
}
}
// This is a static method so no changing self
return newMat;
}
// The element wise multiplication method
public Matrix elementMult(Matrix other) {
// Making sure that they have the same dimensions
if (this.rows != other.rows || this.cols != other.cols) {
// Telling them a bit about the error that just occurred
System.out.println("In element-wise multiplication, both matrices have to have the same dimensions.");
// Printing out the matrices so that they know a bit more about what went wrong
System.out.println("Matrix A:");
this.print();
System.out.println("\nMatrix B:");
other.print();
// Throwing an error to stop the program
throw new Error("ElementMultiplicationSizeError");
}
// Looping for every value in the matrix to multiply by the scalar
for (int rowNum = 0; rowNum < this.rows; rowNum++) {
for (int colNum = 0; colNum < this.cols; colNum++) {
this.data[rowNum][colNum] *= other.data[rowNum][colNum];
}
}
// This is not a static method, so it changes itself and returns self
return this;
}
// The static element wise multiplication method
public static Matrix elementMult(Matrix one, Matrix two) {
// Making sure that they have the same dimensions
if (one.rows != two.rows || one.cols != two.cols) {
// Telling them a bit about the error that just occurred
System.out.println("In element-wise multiplication, both matrices have to have the same dimensions.");
// Printing out the matrices so that they know a bit more about what went wrong
System.out.println("Matrix A:");
one.print();
System.out.println("\nMatrix B:");
two.print();
// Throwing an error to stop the program
throw new Error("StaticElementMultiplicationSizeError");
}
// This is a static method so we need a new Matrix to return
Matrix newMat = new Matrix(one);
// Looping for every value in the matrix to multiply by the scalar
for (int rowNum = 0; rowNum < one.rows; rowNum++) {
for (int colNum = 0; colNum < one.cols; colNum++) {
newMat.data[rowNum][colNum] *= two.data[rowNum][colNum];
}
}
// This is not a static method, so it changes itself and returns self
return newMat;
}
// The matrix multiplication method
public Matrix matrixMult(Matrix other) {
// First thing we have to do is check the sizes
if (other.rows != this.cols) {
System.out.println("The number of columns in the first matrix must match the number of rows in the second matrix in matrix multiplication.");
// Giving them a bit of data
System.out.println("Matrix A:");
this.print();
System.out.println("\nMatrix B:");
other.print();
// Throwing an error to stop their program
throw new Error("MatrixMultiplicationSizeError");
}
// We need to actually make a new matrix to return because it will be a different size
// We are doing this a strange way to avoid an unneeded loop in the constructor that would randomize it
Matrix newMat = new Matrix();
newMat.rows = this.rows;
newMat.cols = other.cols;
newMat.data = new double[this.rows][other.cols];
// Now for the math
// Looping for every value in the matrix
for (int rowNum = 0; rowNum < newMat.rows; rowNum++) {
for (int colNum = 0; colNum < newMat.cols; colNum++) {
// Now that we will be at every spot in the Matrix, we need to find the multiplicative sum
// So we can loop through the shared dimension
double sum = 0.0;
for (int i = 0; i < this.cols; i++) {
sum += this.data[rowNum][i] * other.data[i][colNum];
}
// Now we just need to assign this value to the new matrix
newMat.data[rowNum][colNum] = sum;
}
}
// Reassigning self
this.data = newMat.data;
this.rows = newMat.rows;
this.cols = newMat.cols;
// Returning what we got
return this;
}
// The static matrix multiplication method
public static Matrix matrixMult(Matrix one, Matrix two) {
// First thing we have to do is check the sizes
if (two.rows != one.cols) {
System.out.println("The number of columns in the first matrix must match the number of rows in the second matrix in matrix multiplication.");
// Giving them a bit of data
System.out.println("Matrix A:");
one.print();
System.out.println("\nMatrix B:");
two.print();
// Throwing an error to stop their program
throw new Error("StaticMatrixMultiplicationSizeError");
}
// We need to actually make a new matrix to return because it will be a different size
// We are doing this a strange way to avoid an unneeded loop in the constructor that would randomize it
Matrix newMat = new Matrix();
newMat.rows = one.rows;
newMat.cols = two.cols;
newMat.data = new double[one.rows][two.cols];
// Now for the math
// Looping for every value in the matrix
for (int rowNum = 0; rowNum < newMat.rows; rowNum++) {
for (int colNum = 0; colNum < newMat.cols; colNum++) {
// Now that we will be at every spot in the Matrix, we need to find the multiplicative sum
// So we can loop through the shared dimension
double sum = 0.0;
for (int i = 0; i < one.cols; i++) {
sum += one.data[rowNum][i] * two.data[i][colNum];
}
// Now we just need to assign this value to the new matrix
newMat.data[rowNum][colNum] = sum;
}
}
// Returning what we got
return newMat;
}
//--------------------------------------------------Addition---------------------------------------------------------------
// The scalar addition method
public Matrix add(double scalar) {
// This specific element addition method is going to be the scalar one, but there will also be
// one for Matrices that are the same size
// Looping for every value in the matrix to add by the scalar
for (int rowNum = 0; rowNum < this.rows; rowNum++) {
for (int colNum = 0; colNum < this.cols; colNum++) {
this.data[rowNum][colNum] += scalar;
}
}
// This is not a static method, so it changes itself and returns self
return this;
}
// The static scalar addition method
public static Matrix add(Matrix mat, double scalar) {
// This specific element addition method is going to be the scalar one, but there will also be
// one for Matrices that are the same size
// The Matrix that we are going to return (again the empty matrix in an attempt to skip some calculations)
Matrix newMat = new Matrix();
// Setting the dimensions of the new matrix
newMat.rows = mat.rows;
newMat.cols = mat.cols;
// Looping for every value in the matrix to add by the scalar
for (int rowNum = 0; rowNum < mat.rows; rowNum++) {
for (int colNum = 0; colNum < mat.cols; colNum++) {
newMat.data[rowNum][colNum] = mat.data[rowNum][colNum] + scalar;
}
}
// This is a static method so no return self
return newMat;
}
// The matrix addition method
public Matrix add(Matrix other) {
// Checking to make sure that they are the same size
if (this.rows != other.rows || this.cols != other.cols) {
System.out.println("The matrices in matrix addition must have the same dimensions.");
// Giving them a bit of data
System.out.println("Matrix A:");
this.print();
System.out.println("\nMatrix B:");
other.print();
// Throwing an error to stop their program
throw new Error("MatrixAdditionSizeError");
}
// Looping for every value in the matrix to add by the corresponding value
for (int rowNum = 0; rowNum < this.rows; rowNum++) {
for (int colNum = 0; colNum < this.cols; colNum++) {
this.data[rowNum][colNum] += other.data[rowNum][colNum];
}
}
// Returning self
return this;
}
// The static matrix addition method
public static Matrix add(Matrix one, Matrix two) {
// Checking to make sure that they are the same size
if (one.rows != two.rows || one.cols != two.cols) {
System.out.println("The matrices in matrix addition must have the same dimensions.");
// Giving them a bit of data
System.out.println("Matrix A:");
one.print();
System.out.println("\nMatrix B:");
two.print();
// Throwing an error to stop their program
throw new Error("StaticMatrixAdditionSizeError");
}
// Making a new matrix to return
Matrix newMat = new Matrix();
newMat.rows = one.rows;
newMat.cols = one.cols;
newMat.data = new double[one.rows][one.cols];
// Looping for every value in the matrix to add by the corresponding value
for (int rowNum = 0; rowNum < one.rows; rowNum++) {
for (int colNum = 0; colNum < one.cols; colNum++) {
newMat.data[rowNum][colNum] = one.data[rowNum][colNum] + two.data[rowNum][colNum];
}
}
// Returning the new matrix
return newMat;
}
// ----------------------------------------------Subtraction--------------------------------------------------------------------
// TODO Update subtraction to make them faster
// Element subtraction
public Matrix sub(double scalar) {
// The way that I will handle subtraction is to just multiply by negative 1 and then
return this.add(-scalar);
}
public Matrix sub(Matrix other) {
return this.elementMult(-1).add(other).elementMult(-1);
}
// ----------------------------------------------Misc--------------------------------------------------------------------
// The transposition method
public Matrix transpose() {
// The new matrix that we are returning
Matrix newMat = new Matrix();
newMat.cols = this.rows;
newMat.rows = this.cols;
newMat.data = new double[this.cols][this.rows];
// Looping for every value in the matrix to move it to the new location
for (int rowNum = 0; rowNum < this.rows; rowNum++) {
for (int colNum = 0; colNum < this.cols; colNum++) {
newMat.data[colNum][rowNum] = this.data[rowNum][colNum];
}
}
// Setting this to have the new data
this.data = newMat.data;
this.cols = newMat.cols;
this.rows = newMat.rows;
// Returning self;
return this;
}
// The static transposition method
public static Matrix transpose(Matrix mat) {
// The new matrix that we are returning
Matrix newMat = new Matrix();
newMat.cols = mat.rows;
newMat.rows = mat.cols;
newMat.data = new double[mat.cols][mat.rows];
// Looping for every value in the matrix to move it to the new location
for (int rowNum = 0; rowNum < mat.rows; rowNum++) {
for (int colNum = 0; colNum < mat.cols; colNum++) {
newMat.data[colNum][rowNum] = mat.data[rowNum][colNum];
}
}
// Returning self;
return newMat;
}
// The toArray method that returns a copy of the data
public double[][] toArray() {
// The array that we are going to return
double[][] arr = new double[this.rows][this.cols];
// Running a deep copy
for (int i = 0; i < this.rows; i++) {
arr[i] = Arrays.copyOf(this.data[i], this.data[i].length);
}
// Returning
return arr;
}
// The static toArray method
public static double[][] toArray(Matrix mat) {
// The array that we are going to return
double[][] arr = new double[mat.rows][mat.cols];
// Running a deep copy
for (int i = 0; i < mat.rows; i++) {
arr[i] = Arrays.copyOf(mat.data[i], mat.data[i].length);
}
// Returning
return arr;
}
// A toArray method, just it returns a vector(1D array)
public double[] toVector() {
// We can only do this if one of the dimensions is zero, so here we check
if (this.rows == 1) {
// The array that we are going to return
double[] arr = new double[this.cols];
// Copying over the data
for (int i = 0; i < this.cols; i++) {
arr[i] = this.data[0][i];
}
// Returning
return arr;
} else if (this.cols == 1) {
// The array that we are going to return
double[] arr = new double[this.rows];
// Copying over the data
for (int i = 0; i < this.rows; i++) {
arr[i] = this.data[i][0];
}
// Returning
return arr;
} else {
// Logging the error of their ways
System.out.println("The toVector method can only run for a 1D Matrix (AKA a vector).\nThis Matrix is a " + this.rows + " by " + this.cols + " Matrix, so it is not a vector.");
throw new Error("toVectorSizeError");
}
}
// A soft max function that normalizes the Matrix to percentage values less than one
public Matrix softMax() {
// First we need to find the sum of the every element when e is raised to their power (weird, I know)
double sum = 0;
for (int row = 0; row < this.rows; row++) {
for (int col = 0; col < this.cols; col++) {
// Math :)
sum += Math.exp(this.data[row][col]);
}
}
// Now we can replace every element with e raised to the element over the sum. Simple
for (int row = 0; row < this.rows; row++) {
for (int col = 0; col < this.cols; col++) {
// Math :)
this.data[row][col] = Math.exp(this.data[row][col]) / sum;
}
}
// Returning self
return this;
}
// The static soft max function
public static Matrix softMax(Matrix mat) {
// First we need to find the sum of the every element when e is raised to their power (weird, I know)
double sum = 0;
for (int row = 0; row < mat.rows; row++) {
for (int col = 0; col < mat.cols; col++) {
// Math :)
sum += Math.exp(mat.data[row][col]);
}
}
// The new matrix that we will be editing and returning
Matrix newMat = new Matrix();
newMat.rows = mat.rows;
newMat.cols = mat.cols;
// Now we can replace every element with e raised to the element over the sum. Simple
for (int row = 0; row < mat.rows; row++) {
for (int col = 0; col < mat.cols; col++) {
// Math :)
newMat.data[row][col] = Math.exp(mat.data[row][col]) / sum;
}
}
// Returning the edited one
return newMat;
}
// The mapping function
public Matrix map(activationFunction func) {
// We want to loop through the Matrix now and run each element through the function
for (int row = 0; row < this.rows; row++) {
for (int col = 0; col < this.cols; col++) {
this.data[row][col] = func.run(this.data[row][col]);
}
}
// Returning self
return this;
}
// The static mapping function
public static Matrix map(Matrix mat, activationFunction func) {
// Making a new matrix that we will edit and return
Matrix newMat = new Matrix();
newMat.rows = mat.rows;
newMat.cols = mat.cols;
newMat.data = new double[mat.rows][mat.cols];
// We want to loop through the Matrix now and run each element through the function
for (int row = 0; row < newMat.rows; row++) {
for (int col = 0; col < newMat.cols; col++) {
newMat.data[row][col] = func.run(mat.data[row][col]);
}
}
// Returning the new matrix
return newMat;
}
// This is just a blank interface so that you can create a lambda expression to pass in to the mapping function
public interface activationFunction {
double run(double x);
}
}