diff --git a/apache-commons-math/pom.xml b/apache-commons-math/pom.xml new file mode 100644 index 0000000000..98c6953120 --- /dev/null +++ b/apache-commons-math/pom.xml @@ -0,0 +1,44 @@ + + + 4.0.0 + + com.baeldung + apache-commons-math + 1.0-SNAPSHOT + + + 3.6.0 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + + + + + org.apache.commons + commons-math3 + 3.6.1 + + + + junit + junit + 4.12 + test + + + + \ No newline at end of file diff --git a/apache-commons-math/src/test/java/com/baeldung/commons/math/ComplexTests.java b/apache-commons-math/src/test/java/com/baeldung/commons/math/ComplexTests.java new file mode 100644 index 0000000000..2e5074453e --- /dev/null +++ b/apache-commons-math/src/test/java/com/baeldung/commons/math/ComplexTests.java @@ -0,0 +1,20 @@ +package com.baeldung.commons.math; + +import org.apache.commons.math3.complex.Complex; +import org.junit.Assert; +import org.junit.Test; + +public class ComplexTests { + + @Test + public void whenComplexPow_thenCorrect() { + Complex first = new Complex(1.0, 3.0); + Complex second = new Complex(2.0, 5.0); + + Complex power = first.pow(second); + + Assert.assertEquals(-0.007563724861696302, power.getReal(), 1e-7); + Assert.assertEquals(0.01786136835085382, power.getImaginary(), 1e-7); + } + +} diff --git a/apache-commons-math/src/test/java/com/baeldung/commons/math/FractionTests.java b/apache-commons-math/src/test/java/com/baeldung/commons/math/FractionTests.java new file mode 100644 index 0000000000..6efef79b23 --- /dev/null +++ b/apache-commons-math/src/test/java/com/baeldung/commons/math/FractionTests.java @@ -0,0 +1,20 @@ +package com.baeldung.commons.math; + +import org.apache.commons.math3.fraction.Fraction; +import org.apache.commons.math3.fraction.FractionFormat; +import org.junit.Assert; +import org.junit.Test; + +public class FractionTests { + + @Test + public void whenFractionAdd_thenCorrect() { + Fraction lhs = new Fraction(1, 3); + Fraction rhs = new Fraction(2, 5); + Fraction sum = lhs.add(rhs); + + Assert.assertEquals(11, sum.getNumerator()); + Assert.assertEquals(15, sum.getDenominator()); + } + +} diff --git a/apache-commons-math/src/test/java/com/baeldung/commons/math/GeometryTests.java b/apache-commons-math/src/test/java/com/baeldung/commons/math/GeometryTests.java new file mode 100644 index 0000000000..17cbb607d6 --- /dev/null +++ b/apache-commons-math/src/test/java/com/baeldung/commons/math/GeometryTests.java @@ -0,0 +1,21 @@ +package com.baeldung.commons.math; + +import org.apache.commons.math3.geometry.euclidean.twod.Line; +import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; +import org.junit.Assert; +import org.junit.Test; + +public class GeometryTests { + + @Test + public void whenLineIntersection_thenCorrect() { + Line l1 = new Line(new Vector2D(0, 0), new Vector2D(1, 1), 0); + Line l2 = new Line(new Vector2D(0, 1), new Vector2D(1, 1.5), 0); + + Vector2D intersection = l1.intersection(l2); + + Assert.assertEquals(2, intersection.getX(), 1e-7); + Assert.assertEquals(2, intersection.getY(), 1e-7); + } + +} diff --git a/apache-commons-math/src/test/java/com/baeldung/commons/math/IntegrationTests.java b/apache-commons-math/src/test/java/com/baeldung/commons/math/IntegrationTests.java new file mode 100644 index 0000000000..62ae898fa8 --- /dev/null +++ b/apache-commons-math/src/test/java/com/baeldung/commons/math/IntegrationTests.java @@ -0,0 +1,21 @@ +package com.baeldung.commons.math; + +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.integration.SimpsonIntegrator; +import org.apache.commons.math3.analysis.integration.UnivariateIntegrator; +import org.junit.Assert; +import org.junit.Test; + +public class IntegrationTests { + + @Test + public void whenUnivariateIntegratorIntegrate_thenCorrect() { + final UnivariateFunction function = v -> v; + final UnivariateIntegrator integrator = new SimpsonIntegrator(1.0e-12, 1.0e-8, 1, 32); + + final double i = integrator.integrate(100, function, 0, 10); + + Assert.assertEquals(16 + 2d/3d, i, 1e-7); + } + +} diff --git a/apache-commons-math/src/test/java/com/baeldung/commons/math/LinearAlgebraTests.java b/apache-commons-math/src/test/java/com/baeldung/commons/math/LinearAlgebraTests.java new file mode 100644 index 0000000000..981f839280 --- /dev/null +++ b/apache-commons-math/src/test/java/com/baeldung/commons/math/LinearAlgebraTests.java @@ -0,0 +1,25 @@ +package com.baeldung.commons.math; + +import org.apache.commons.math3.linear.*; +import org.junit.Assert; +import org.junit.Test; + +public class LinearAlgebraTests { + + @Test + public void whenDecompositionSolverSolve_thenCorrect() { + RealMatrix a = + new Array2DRowRealMatrix(new double[][] { { 2, 3, -2 }, { -1, 7, 6 }, { 4, -3, -5 } }, + false); + RealVector b = new ArrayRealVector(new double[] { 1, -2, 1 }, false); + + DecompositionSolver solver = new LUDecomposition(a).getSolver(); + + RealVector solution = solver.solve(b); + + Assert.assertEquals(-0.3698630137, solution.getEntry(0), 1e-7); + Assert.assertEquals(0.1780821918, solution.getEntry(1), 1e-7); + Assert.assertEquals(-0.602739726, solution.getEntry(2), 1e-7); + } + +} diff --git a/apache-commons-math/src/test/java/com/baeldung/commons/math/ProbabilitiesTests.java b/apache-commons-math/src/test/java/com/baeldung/commons/math/ProbabilitiesTests.java new file mode 100644 index 0000000000..41ccb16c5a --- /dev/null +++ b/apache-commons-math/src/test/java/com/baeldung/commons/math/ProbabilitiesTests.java @@ -0,0 +1,15 @@ +package com.baeldung.commons.math; + +import org.apache.commons.math3.distribution.NormalDistribution; +import org.junit.Test; + +public class ProbabilitiesTests { + + @Test + public void whenNormalDistributionSample_thenSuccess() { + final NormalDistribution normalDistribution = new NormalDistribution(10, 3); + + System.out.println(normalDistribution.sample()); + } + +} diff --git a/apache-commons-math/src/test/java/com/baeldung/commons/math/RootFindingTests.java b/apache-commons-math/src/test/java/com/baeldung/commons/math/RootFindingTests.java new file mode 100644 index 0000000000..3e6b542c01 --- /dev/null +++ b/apache-commons-math/src/test/java/com/baeldung/commons/math/RootFindingTests.java @@ -0,0 +1,20 @@ +package com.baeldung.commons.math; + +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.solvers.BracketingNthOrderBrentSolver; +import org.apache.commons.math3.analysis.solvers.UnivariateSolver; +import org.junit.Assert; +import org.junit.Test; + +public class RootFindingTests { + + @Test + public void whenUnivariateSolverSolver_thenCorrect() { + final UnivariateFunction function = v -> Math.pow(v, 2) - 2; + UnivariateSolver solver = new BracketingNthOrderBrentSolver(1.0e-12, 1.0e-8, 5); + double c = solver.solve(100, function, -10.0, 10.0, 0); + + Assert.assertEquals(-Math.sqrt(2), c, 1e-7); + } + +} diff --git a/apache-commons-math/src/test/java/com/baeldung/commons/math/StatisticsTests.java b/apache-commons-math/src/test/java/com/baeldung/commons/math/StatisticsTests.java new file mode 100644 index 0000000000..c1b0619b11 --- /dev/null +++ b/apache-commons-math/src/test/java/com/baeldung/commons/math/StatisticsTests.java @@ -0,0 +1,38 @@ +package com.baeldung.commons.math; + +import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class StatisticsTests { + + private double[] values; + private DescriptiveStatistics descriptiveStatistics; + + @Before + public void setUp() { + values = new double[] {65, 51 , 16, 11 , 6519, 191 ,0 , 98, 19854, 1, 32}; + + descriptiveStatistics = new DescriptiveStatistics(); + for(double v : values) { + descriptiveStatistics.addValue(v); + } + } + + @Test + public void whenDescriptiveStatisticsGetMean_thenCorrect() { + Assert.assertEquals(2439.8181818181815, descriptiveStatistics.getMean(), 1e-7); + } + + @Test + public void whenDescriptiveStatisticsGetMedian_thenCorrect() { + Assert.assertEquals(51, descriptiveStatistics.getPercentile(50), 1e-7); + } + + @Test + public void whenDescriptiveStatisticsGetStandardDeviation_thenCorrect() { + Assert.assertEquals(6093.054649651221, descriptiveStatistics.getStandardDeviation(), 1e-7); + } + +}