BAEL 767 - Introduction to Apache Commons Math (update) (#1670)
* BAEL 767 - Introduction to apache commons math * BAEL 767 - Moving examples to test classes * BAEL 767 - Renaming tests with BDD convention
This commit is contained in:
parent
7d6bf29092
commit
1aff8ca17d
44
apache-commons-math/pom.xml
Normal file
44
apache-commons-math/pom.xml
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>apache-commons-math</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>${maven-compiler-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-math3</artifactId>
|
||||||
|
<version>3.6.1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -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,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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user