[BAEL-2996] Moved previous tests and added own implementation
This commit is contained in:
parent
5296f37727
commit
bc498d61b3
@ -1,4 +1,4 @@
|
||||
package com.baeldung.colt;
|
||||
package com.baeldung.matrices.colt;
|
||||
|
||||
import cern.colt.matrix.DoubleFactory2D;
|
||||
import cern.colt.matrix.DoubleMatrix2D;
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.ejml;
|
||||
package com.baeldung.matrices.ejml;
|
||||
|
||||
import org.ejml.simple.SimpleMatrix;
|
||||
import org.junit.jupiter.api.Test;
|
@ -0,0 +1,52 @@
|
||||
package com.baeldung.matrices.homemade;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class MatrixUnitTest {
|
||||
|
||||
@Test
|
||||
void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
|
||||
double[][] firstMatrix = {
|
||||
new double[]{1d, 5d},
|
||||
new double[]{2d, 3d},
|
||||
new double[]{1d, 7d}
|
||||
};
|
||||
|
||||
double[][] secondMatrix = {
|
||||
new double[]{1d, 2d, 3d, 7d},
|
||||
new double[]{5d, 2d, 8d, 1d}
|
||||
};
|
||||
|
||||
double[][] expected = {
|
||||
new double[]{26d, 12d, 43d, 12d},
|
||||
new double[]{17d, 10d, 30d, 17d},
|
||||
new double[]{36d, 16d, 59d, 14d}
|
||||
};
|
||||
|
||||
double[][] actual = multiplyMatrices(firstMatrix, secondMatrix);
|
||||
|
||||
assertThat(actual).isEqualTo(expected);
|
||||
}
|
||||
|
||||
private double[][] multiplyMatrices(double[][] firstMatrix, double[][] secondMatrix) {
|
||||
double[][] result = new double[firstMatrix.length][secondMatrix[0].length];
|
||||
|
||||
for (int row = 0; row < result.length; row++) {
|
||||
for (int col = 0; col < result[row].length; col++) {
|
||||
result[row][col] = multiplyMatricesCell(firstMatrix, secondMatrix, row, col);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private double multiplyMatricesCell(double[][] firstMatrix, double[][] secondMatrix, int row, int col) {
|
||||
double cell = 0;
|
||||
for (int i = 0; i < secondMatrix.length; i++) {
|
||||
cell += firstMatrix[row][i] * secondMatrix[i][col];
|
||||
}
|
||||
return cell;
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.la4j;
|
||||
package com.baeldung.matrices.la4j;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.la4j.Matrix;
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.nd4j;
|
||||
package com.baeldung.matrices.nd4j;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.nd4j.linalg.api.ndarray.INDArray;
|
Loading…
x
Reference in New Issue
Block a user