-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixMultiplication.java
118 lines (105 loc) · 3.48 KB
/
MatrixMultiplication.java
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
import java.util.Date;
/**
* Simple class showing single-threaded matrix multiplication. Includes methods that are easily split up
* to allow easy integration with threading.
*
* @author John Doggett
* @date Thu 27-Feb-2020
*
*/
public class MatrixMultiplication {
/**
* accumulates the product of each position of two arrays
*
* @author John Doggett
* @date Thu 27-Feb-2020
*
* @param arrayOne
* @param arrayTwo
* @return value of accumulation
*/
static double multiplyTwoArrays(double[] arrayOne, double[] arrayTwo) {
double output = 0;
for(int a = 0; a < arrayTwo.length; a++) {
output += arrayOne[a] * arrayTwo[a];
}
return output;
}
/**
* converts standard matrix from [column][row] to [row][column]
*
* @author John Doggett
* @date Thu 27-Feb-2020
*
* @param matrix
* @return converted matrix
*/
static double[][] convertSecondMatrixFormat(double[][] matrix){
double[][] output = new double[matrix[0].length][matrix.length];
for(int a = 0; a < matrix.length; a++) {
for(int b = 0; b < matrix[0].length; b++) {
output[b][a] = matrix[a][b];
}
}
return output;
}
/**
* creates an output array based on multiply a column of matrix one by matrix two
*
* @author John Doggett
* @date Thu 27-Feb-2020
*
* @param matrixOneLine
* @param matrixTwo
* @return row of the sum of the matrix with respect to a single matrix one row
*/
static double[] multiplyOneArrayInMatrix(double[] matrixOneLine, double[][] matrixTwo) {
double[] output = new double[matrixTwo.length];
for(int a = 0; a < matrixTwo.length; a++) {
output[a] = multiplyTwoArrays(matrixOneLine, matrixTwo[a]);
}
return output;
}
/**
* generates a new empty matrix based on [matrix one's column length][matrix two's row length]
* if the two were multiplied
*
* @author John Doggett
* @date Thu 27-Feb-2020
*
* @param matrixOne
* @param matrixTwo
* @return output empty matrix
*/
static double[][] generateEmptyOutputMatrix(double[][] matrixOne, double[][] matrixTwo){
return new double[matrixOne.length][matrixTwo[0].length];
}
/**
* multiplies two matrices together by running threw each row of matrix one and multiplying it to matrix two
*
* @author John Doggett
* @date Thu 27-Feb-2020
*
* @param matrixOne
* @param matrixTwo
* @return the output sum matrix
*/
static double[][] multiplyMatrix(double[][] matrixOne, double[][] matrixTwo) {
double[][] output = MatrixMultiplication.generateEmptyOutputMatrix(matrixOne, matrixTwo);
double[][] fixedMatrixTwo = MatrixMultiplication.convertSecondMatrixFormat(matrixTwo);
for(int a = 0; a < matrixOne.length; a++) {
output[a] = MatrixMultiplication.multiplyOneArrayInMatrix(matrixOne[a], fixedMatrixTwo);
}
return output;
}
// MAIN METHOD, may require extra allocated ram to run depending on size of calculation
public static void main(String[] args) {
//Create input matrices. Both matrices must call the same size createAnArray.
double[][] one = Generate2DArray.createAnArray(2000); //create matrix one
double[][] two = Generate2DArray.createAnArray(2000); //create matrix two
long t1 = java.lang.System.nanoTime();
double[][] sum = multiplyMatrix(one, two); //multiply matrix one x matrix two
long t2 = java.lang.System.nanoTime();
System.out.println(t2-t1); //print out time it took to find sum, in nano seconds
}
}