Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RandomProblem002.java matrix multiplication
- Loading branch information
1 parent
6dc4bc7
commit aa721a7
Showing
4 changed files
with
72 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...rc/main/java/com/shekhargulati/ninetynine_problems/java8/_00_random/RandomProblem003.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.shekhargulati.ninetynine_problems.java8._00_random; | ||
|
||
/** | ||
* Perform matrix multiplication of two arrays. | ||
* Matrix multiplication of array A of size nxm and array B of size mxp is an array of size nxp | ||
*/ | ||
public class RandomProblem003 { | ||
|
||
public static int[][] matrixMultiplication(int[][] a, int[][] b) { | ||
/* | ||
1. Iterate over all the rows of A | ||
2. For each row of A | ||
2.1 iterate over all the columns of A and multiply them with each column of B | ||
*/ | ||
int rowsA = a.length; | ||
int colsA = a[0].length; | ||
int rowsB = b.length; | ||
int colsB = b[0].length; | ||
|
||
|
||
if (colsA != rowsB) { | ||
throw new IllegalArgumentException("Can't perform matrix calculation on matrix with given sizes"); | ||
} | ||
int[][] result = new int[rowsA][colsB]; | ||
|
||
for (int i = 0; i < rowsA; i++) { | ||
for (int j = 0; j < colsB; j++) { | ||
int fValue = 0; | ||
for (int k = 0; k < colsA; k++) { | ||
fValue += a[i][k] * b[k][j]; | ||
} | ||
result[i][j] = fValue; | ||
} | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...est/java/com/shekhargulati/ninetynine_problems/java8/_00_random/RandomProblem003Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.shekhargulati.ninetynine_problems.java8._00_random; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class RandomProblem003Test { | ||
|
||
@Test | ||
public void performMatrixMultiplicationOfTwoArrays() throws Exception { | ||
int[][] a = { | ||
{1, 2, 3}, | ||
{4, 5, 6} | ||
}; | ||
|
||
int[][] b = { | ||
{7, 8}, | ||
{9, 10}, | ||
{11, 12} | ||
}; | ||
|
||
int[][] result = { | ||
{58, 64}, | ||
{139, 154} | ||
}; | ||
|
||
int[][] c = RandomProblem003.matrixMultiplication(a, b); | ||
assertThat(c, equalTo(result)); | ||
} | ||
} |