Added multiply by element function
This commit is contained in:
parent
acc3ccfe1f
commit
3df20257b0
2 changed files with 24 additions and 0 deletions
|
|
@ -110,6 +110,24 @@ public class MatrixMath {
|
|||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Element multiplication, each element in matrix1 will be
|
||||
* multiplied with the corresponding element in matrix2.
|
||||
*
|
||||
* @return a new matrix with the result
|
||||
*/
|
||||
public static double[][] elemMultiply(double[][] matrix1, double[][] matrix2){
|
||||
elementalPreCheck(matrix1, matrix2);
|
||||
double[][] result = new double[matrix1.length][matrix1[0].length];
|
||||
|
||||
for (int y=0; y < matrix1.length; ++y) {
|
||||
for (int x=0; x < matrix1[y].length; ++x){
|
||||
result[y][x] = matrix1[y][x] * matrix2[y][x];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void elementalPreCheck(double[][] matrix1, double[][] matrix2) {
|
||||
if (matrix1.length != matrix2.length || matrix1[0].length != matrix2[0].length)
|
||||
throw new IllegalArgumentException("Matrices need to be of same dimension: " +
|
||||
|
|
|
|||
|
|
@ -47,6 +47,12 @@ public class MatrixMathTest {
|
|||
MatrixMath.subtract(new double[][]{{2,3},{-4,9}}, new double[][]{{1,2},{3,4}}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void elementalMultiply(){
|
||||
assertArrayEquals(new double[][]{{2,6},{-12,36}},
|
||||
MatrixMath.elemMultiply(new double[][]{{2,3},{-4,9}}, new double[][]{{1,2},{3,4}}));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue