diff --git a/java-math/src/main/java/com/baeldung/matrices/HomemadeMatrix.java b/java-math/src/main/java/com/baeldung/matrices/HomemadeMatrix.java new file mode 100644 index 0000000000..0676250959 --- /dev/null +++ b/java-math/src/main/java/com/baeldung/matrices/HomemadeMatrix.java @@ -0,0 +1,23 @@ +package com.baeldung.matrices; + +public class HomemadeMatrix { + public static 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 static 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; + } +} \ No newline at end of file diff --git a/java-math/src/test/java/com/baeldung/algorithms/logarithm/LogarithmUnitTest.java b/java-math/src/test/java/com/baeldung/algorithms/logarithm/LogarithmUnitTest.java index a9a04c8d6f..facad1edc4 100644 --- a/java-math/src/test/java/com/baeldung/algorithms/logarithm/LogarithmUnitTest.java +++ b/java-math/src/test/java/com/baeldung/algorithms/logarithm/LogarithmUnitTest.java @@ -4,7 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.Test; -public class CombinationUnitTest { +public class LogarithmUnitTest { @Test public void givenLog10_shouldReturnValidResults() { @@ -13,9 +13,9 @@ public class CombinationUnitTest { } @Test - public void givenLog10_shouldReturnValidResults() { + public void givenLogE_shouldReturnValidResults() { assertEquals(Math.log(Math.E), 1); - assertEquals(Math.log(10), 2.30258); + assertEquals(Math.log(10), 2.30258, 0.00001); } @Test diff --git a/java-math/src/test/java/com/baeldung/matrices/MatrixMultiplicationBenchmarking.java b/java-math/src/test/java/com/baeldung/matrices/MatrixMultiplicationBenchmarking.java index 1e3b183aa7..171a1d28a4 100644 --- a/java-math/src/test/java/com/baeldung/matrices/MatrixMultiplicationBenchmarking.java +++ b/java-math/src/test/java/com/baeldung/matrices/MatrixMultiplicationBenchmarking.java @@ -1,9 +1,84 @@ package com.baeldung.matrices; +import cern.colt.matrix.DoubleFactory2D; +import cern.colt.matrix.DoubleMatrix2D; +import cern.colt.matrix.linalg.Algebra; +import org.apache.commons.math3.linear.Array2DRowRealMatrix; +import org.apache.commons.math3.linear.RealMatrix; +import org.ejml.simple.SimpleMatrix; +import org.la4j.Matrix; +import org.la4j.matrix.dense.Basic2DMatrix; +import org.nd4j.linalg.api.ndarray.INDArray; +import org.nd4j.linalg.factory.Nd4j; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import java.util.concurrent.TimeUnit; + public class MatrixMultiplicationBenchmarking { public static void main(String[] args) throws Exception { - org.openjdk.jmh.Main.main(args); + Options opt = new OptionsBuilder() + .include(MatrixMultiplicationBenchmarking.class.getSimpleName()) + .mode(Mode.AverageTime) + .forks(2) + .warmupIterations(5) + .measurementIterations(10) + .timeUnit(TimeUnit.MICROSECONDS) + .build(); + + new Runner(opt).run(); } -} + @Benchmark + public Object homemadeMatrixMultiplication(MatrixProvider matrixProvider) { + return HomemadeMatrix.multiplyMatrices(matrixProvider.getFirstMatrix(), matrixProvider.getSecondMatrix()); + } + + @Benchmark + public Object ejmlMatrixMultiplication(MatrixProvider matrixProvider) { + SimpleMatrix firstMatrix = new SimpleMatrix(matrixProvider.getFirstMatrix()); + SimpleMatrix secondMatrix = new SimpleMatrix(matrixProvider.getSecondMatrix()); + + return firstMatrix.mult(secondMatrix); + } + + @Benchmark + public Object apacheCommonsMatrixMultiplication(MatrixProvider matrixProvider) { + RealMatrix firstMatrix = new Array2DRowRealMatrix(matrixProvider.getFirstMatrix()); + RealMatrix secondMatrix = new Array2DRowRealMatrix(matrixProvider.getSecondMatrix()); + + return firstMatrix.multiply(secondMatrix); + } + + @Benchmark + public Object la4jMatrixMultiplication(MatrixProvider matrixProvider) { + Matrix firstMatrix = new Basic2DMatrix(matrixProvider.getFirstMatrix()); + Matrix secondMatrix = new Basic2DMatrix(matrixProvider.getSecondMatrix()); + + return firstMatrix.multiply(secondMatrix); + } + + @Benchmark + public Object nd4jMatrixMultiplication(MatrixProvider matrixProvider) { + INDArray firstMatrix = Nd4j.create(matrixProvider.getFirstMatrix()); + INDArray secondMatrix = Nd4j.create(matrixProvider.getSecondMatrix()); + + return firstMatrix.mmul(secondMatrix); + } + + @Benchmark + public Object coltMatrixMultiplication(MatrixProvider matrixProvider) { + DoubleFactory2D doubleFactory2D = DoubleFactory2D.dense; + + DoubleMatrix2D firstMatrix = doubleFactory2D.make(matrixProvider.getFirstMatrix()); + DoubleMatrix2D secondMatrix = doubleFactory2D.make(matrixProvider.getSecondMatrix()); + + Algebra algebra = new Algebra(); + return algebra.mult(firstMatrix, secondMatrix); + } + +} \ No newline at end of file diff --git a/java-math/src/test/java/com/baeldung/matrices/MatrixProvider.java b/java-math/src/test/java/com/baeldung/matrices/MatrixProvider.java new file mode 100644 index 0000000000..33bd074b6e --- /dev/null +++ b/java-math/src/test/java/com/baeldung/matrices/MatrixProvider.java @@ -0,0 +1,33 @@ +package com.baeldung.matrices; + +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; + +@State(Scope.Benchmark) +public class MatrixProvider { + private double[][] firstMatrix; + private double[][] secondMatrix; + + public MatrixProvider() { + firstMatrix = + new double[][] { + new double[] {1d, 5d}, + new double[] {2d, 3d}, + new double[] {1d ,7d} + }; + + secondMatrix = + new double[][] { + new double[] {1d, 2d, 3d, 7d}, + new double[] {5d, 2d, 8d, 1d} + }; + } + + public double[][] getFirstMatrix() { + return firstMatrix; + } + + public double[][] getSecondMatrix() { + return secondMatrix; + } +} \ No newline at end of file diff --git a/java-math/src/test/java/com/baeldung/matrices/apache/RealMatrixUnitTest.java b/java-math/src/test/java/com/baeldung/matrices/apache/RealMatrixUnitTest.java index 05944e7b3a..e7d99fbe3e 100644 --- a/java-math/src/test/java/com/baeldung/matrices/apache/RealMatrixUnitTest.java +++ b/java-math/src/test/java/com/baeldung/matrices/apache/RealMatrixUnitTest.java @@ -7,15 +7,10 @@ import org.openjdk.jmh.annotations.*; import static org.assertj.core.api.Assertions.assertThat; -@BenchmarkMode(Mode.AverageTime) -@Fork(value = 2) -@Warmup(iterations = 5) -@Measurement(iterations = 10) -public class RealMatrixUnitTest { +class RealMatrixUnitTest { @Test - @Benchmark - public void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() { + void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() { RealMatrix firstMatrix = new Array2DRowRealMatrix( new double[][] { new double[] {1d, 5d}, @@ -43,5 +38,4 @@ public class RealMatrixUnitTest { assertThat(actual).isEqualTo(expected); } - } diff --git a/java-math/src/test/java/com/baeldung/matrices/colt/DoubleMatrix2DUnitTest.java b/java-math/src/test/java/com/baeldung/matrices/colt/DoubleMatrix2DUnitTest.java index fb4a419eb0..da66cd7d61 100644 --- a/java-math/src/test/java/com/baeldung/matrices/colt/DoubleMatrix2DUnitTest.java +++ b/java-math/src/test/java/com/baeldung/matrices/colt/DoubleMatrix2DUnitTest.java @@ -8,15 +8,10 @@ import org.openjdk.jmh.annotations.*; import static org.assertj.core.api.Assertions.assertThat; -@BenchmarkMode(Mode.AverageTime) -@Fork(value = 2) -@Warmup(iterations = 5) -@Measurement(iterations = 10) -public class DoubleMatrix2DUnitTest { +class DoubleMatrix2DUnitTest { @Test - @Benchmark - public void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() { + void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() { DoubleFactory2D doubleFactory2D = DoubleFactory2D.dense; DoubleMatrix2D firstMatrix = doubleFactory2D.make( @@ -48,4 +43,4 @@ public class DoubleMatrix2DUnitTest { assertThat(actual).isEqualTo(expected); } -} +} \ No newline at end of file diff --git a/java-math/src/test/java/com/baeldung/matrices/ejml/SimpleMatrixUnitTest.java b/java-math/src/test/java/com/baeldung/matrices/ejml/SimpleMatrixUnitTest.java index b025266a1d..60381ece63 100644 --- a/java-math/src/test/java/com/baeldung/matrices/ejml/SimpleMatrixUnitTest.java +++ b/java-math/src/test/java/com/baeldung/matrices/ejml/SimpleMatrixUnitTest.java @@ -6,15 +6,10 @@ import org.openjdk.jmh.annotations.*; import static org.assertj.core.api.Assertions.assertThat; -@BenchmarkMode(Mode.AverageTime) -@Fork(value = 2) -@Warmup(iterations = 5) -@Measurement(iterations = 10) -public class SimpleMatrixUnitTest { +class SimpleMatrixUnitTest { @Test - @Benchmark - public void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() { + void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() { SimpleMatrix firstMatrix = new SimpleMatrix( new double[][] { new double[] {1d, 5d}, @@ -43,4 +38,4 @@ public class SimpleMatrixUnitTest { assertThat(actual).matches(m -> m.isIdentical(expected, 0d)); } -} +} \ No newline at end of file diff --git a/java-math/src/test/java/com/baeldung/matrices/homemade/HomemadeMatrixUnitTest.java b/java-math/src/test/java/com/baeldung/matrices/homemade/HomemadeMatrixUnitTest.java index be9e483d5b..d1a062ee79 100644 --- a/java-math/src/test/java/com/baeldung/matrices/homemade/HomemadeMatrixUnitTest.java +++ b/java-math/src/test/java/com/baeldung/matrices/homemade/HomemadeMatrixUnitTest.java @@ -5,15 +5,10 @@ import org.openjdk.jmh.annotations.*; import static org.assertj.core.api.Assertions.assertThat; -@BenchmarkMode(Mode.AverageTime) -@Fork(value = 2) -@Warmup(iterations = 5) -@Measurement(iterations = 10) -public class HomemadeMatrixUnitTest { +class HomemadeMatrixUnitTest { @Test - @Benchmark - public void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() { + void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() { double[][] firstMatrix = { new double[]{1d, 5d}, new double[]{2d, 3d}, @@ -55,4 +50,5 @@ public class HomemadeMatrixUnitTest { } return cell; } -} + +} \ No newline at end of file diff --git a/java-math/src/test/java/com/baeldung/matrices/la4j/Basic2DMatrixUnitTest.java b/java-math/src/test/java/com/baeldung/matrices/la4j/Basic2DMatrixUnitTest.java index afb84ff3db..01731a3dd5 100644 --- a/java-math/src/test/java/com/baeldung/matrices/la4j/Basic2DMatrixUnitTest.java +++ b/java-math/src/test/java/com/baeldung/matrices/la4j/Basic2DMatrixUnitTest.java @@ -7,15 +7,10 @@ import org.openjdk.jmh.annotations.*; import static org.assertj.core.api.Assertions.assertThat; -@BenchmarkMode(Mode.AverageTime) -@Fork(value = 2) -@Warmup(iterations = 5) -@Measurement(iterations = 10) -public class Basic2DMatrixUnitTest { +class Basic2DMatrixUnitTest { @Test - @Benchmark - public void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() { + void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() { Matrix firstMatrix = new Basic2DMatrix( new double[][]{ new double[]{1d, 5d}, @@ -44,4 +39,4 @@ public class Basic2DMatrixUnitTest { assertThat(actual).isEqualTo(expected); } -} +} \ No newline at end of file diff --git a/java-math/src/test/java/com/baeldung/matrices/nd4j/INDArrayUnitTest.java b/java-math/src/test/java/com/baeldung/matrices/nd4j/INDArrayUnitTest.java index fb3030bccf..72ef60a571 100644 --- a/java-math/src/test/java/com/baeldung/matrices/nd4j/INDArrayUnitTest.java +++ b/java-math/src/test/java/com/baeldung/matrices/nd4j/INDArrayUnitTest.java @@ -7,14 +7,10 @@ import org.openjdk.jmh.annotations.*; import static org.assertj.core.api.Assertions.assertThat; -@BenchmarkMode(Mode.AverageTime) -@Fork(value = 2) -@Warmup(iterations = 5) -@Measurement(iterations = 10) -public class INDArrayUnitTest { +class INDArrayUnitTest { @Test - public void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() { + void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() { INDArray firstMatrix = Nd4j.create( new double[][]{ new double[]{1d, 5d}, @@ -43,4 +39,4 @@ public class INDArrayUnitTest { assertThat(actual).isEqualTo(expected); } -} +} \ No newline at end of file