BAEL 767 - Moving module apache-commons-math to module libraries (#1701)

* BAEL 767 - Introduction to apache commons math

* BAEL 767 - Moving examples to test classes

* BAEL 767 - Renaming tests with BDD convention

* BAEL 767 - Introduction to apache commons math

* BAEL 767 - Moving examples to test classes

* BAEL 767 - Renaming tests with BDD convention

* BAEL 767 - Moving module apache-commons-math to module libraries

* BAEL 767 - Delete directory apache-commons-math
This commit is contained in:
Alexandre Lombard
2017-04-21 10:38:01 +02:00
committed by maibin
parent bac494c0be
commit 2a3030dd98
10 changed files with 5 additions and 45 deletions
@@ -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);
}
}
@@ -0,0 +1,19 @@
package com.baeldung.commons.math;
import org.apache.commons.math3.fraction.Fraction;
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());
}
}
@@ -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);
}
}
@@ -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);
}
}
@@ -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);
}
}
@@ -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());
}
}
@@ -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);
}
}
@@ -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);
}
}