BAEL-1215: Introduction to the Suanshu math library (#5469)

* BAEL-1215: Introduction to the Suanshu math library

* Updated examples based on feedback.
This commit is contained in:
Paul van Gool 2018-10-17 13:00:14 -07:00 committed by maibin
parent 03065a43e9
commit 831cdf2bd6
2 changed files with 155 additions and 0 deletions

View File

@ -711,6 +711,12 @@
<version>${snakeyaml.version}</version>
</dependency>
<dependency>
<groupId>com.numericalmethod</groupId>
<artifactId>suanshu</artifactId>
<version>${suanshu.version}</version>
</dependency>
</dependencies>
<repositories>
@ -731,6 +737,12 @@
<id>Apache Staging</id>
<url>https://repository.apache.org/content/groups/staging</url>
</repository>
<repository>
<id>nm-repo</id>
<name>Numerical Method's Maven Repository</name>
<url>http://repo.numericalmethod.com/maven/</url>
<layout>default</layout>
</repository>
</repositories>
@ -835,6 +847,7 @@
</build>
<properties>
<suanshu.version>4.0.0</suanshu.version>
<snakeyaml.version>1.21</snakeyaml.version>
<googleclient.version>1.23.0</googleclient.version>
<crdt.version>0.1.0</crdt.version>

View File

@ -0,0 +1,142 @@
package com.baeldung.suanshu;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.numericalmethod.suanshu.algebra.linear.matrix.doubles.Matrix;
import com.numericalmethod.suanshu.algebra.linear.vector.doubles.Vector;
import com.numericalmethod.suanshu.algebra.linear.vector.doubles.dense.DenseVector;
import com.numericalmethod.suanshu.algebra.linear.matrix.doubles.matrixtype.dense.DenseMatrix;
import com.numericalmethod.suanshu.algebra.linear.matrix.doubles.operation.Inverse;
import com.numericalmethod.suanshu.analysis.function.polynomial.Polynomial;
import com.numericalmethod.suanshu.analysis.function.polynomial.root.PolyRoot;
import com.numericalmethod.suanshu.analysis.function.polynomial.root.PolyRootSolver;
import com.numericalmethod.suanshu.number.complex.Complex;
class SuanShuMath {
private static final Logger log = LoggerFactory.getLogger(SuanShuMath.class);
public static void main(String[] args) throws Exception {
SuanShuMath math = new SuanShuMath();
math.addingVectors();
math.scaleVector();
math.innerProductVectors();
math.addingIncorrectVectors();
math.addingMatrices();
math.multiplyMatrices();
math.multiplyIncorrectMatrices();
math.inverseMatrix();
Polynomial p = math.createPolynomial();
math.evaluatePolynomial(p);
math.solvePolynomial();
}
public void addingVectors() throws Exception {
Vector v1 = new DenseVector(new double[]{1, 2, 3, 4, 5});
Vector v2 = new DenseVector(new double[]{5, 4, 3, 2, 1});
Vector v3 = v1.add(v2);
log.info("Adding vectors: {}", v3);
}
public void scaleVector() throws Exception {
Vector v1 = new DenseVector(new double[]{1, 2, 3, 4, 5});
Vector v2 = v1.scaled(2.0);
log.info("Scaling a vector: {}", v2);
}
public void innerProductVectors() throws Exception {
Vector v1 = new DenseVector(new double[]{1, 2, 3, 4, 5});
Vector v2 = new DenseVector(new double[]{5, 4, 3, 2, 1});
double inner = v1.innerProduct(v2);
log.info("Vector inner product: {}", inner);
}
public void addingIncorrectVectors() throws Exception {
Vector v1 = new DenseVector(new double[]{1, 2, 3});
Vector v2 = new DenseVector(new double[]{5, 4});
Vector v3 = v1.add(v2);
log.info("Adding vectors: {}", v3);
}
public void addingMatrices() throws Exception {
Matrix m1 = new DenseMatrix(new double[][]{
{1, 2, 3},
{4, 5, 6}
});
Matrix m2 = new DenseMatrix(new double[][]{
{3, 2, 1},
{6, 5, 4}
});
Matrix m3 = m1.add(m2);
log.info("Adding matrices: {}", m3);
}
public void multiplyMatrices() throws Exception {
Matrix m1 = new DenseMatrix(new double[][]{
{1, 2, 3},
{4, 5, 6}
});
Matrix m2 = new DenseMatrix(new double[][]{
{1, 4},
{2, 5},
{3, 6}
});
Matrix m3 = m1.multiply(m2);
log.info("Multiplying matrices: {}", m3);
}
public void multiplyIncorrectMatrices() throws Exception {
Matrix m1 = new DenseMatrix(new double[][]{
{1, 2, 3},
{4, 5, 6}
});
Matrix m2 = new DenseMatrix(new double[][]{
{3, 2, 1},
{6, 5, 4}
});
Matrix m3 = m1.multiply(m2);
log.info("Multiplying matrices: {}", m3);
}
public void inverseMatrix() {
Matrix m1 = new DenseMatrix(new double[][]{
{1, 2},
{3, 4}
});
Inverse m2 = new Inverse(m1);
log.info("Inverting a matrix: {}", m2);
log.info("Verifying a matrix inverse: {}", m1.multiply(m2));
}
public Polynomial createPolynomial() {
return new Polynomial(new double[]{3, -5, 1});
}
public void evaluatePolynomial(Polynomial p) {
// Evaluate using a real number
log.info("Evaluating a polynomial using a real number: {}", p.evaluate(5));
// Evaluate using a complex number
log.info("Evaluating a polynomial using a complex number: {}", p.evaluate(new Complex(1, 2)));
}
public void solvePolynomial() {
Polynomial p = new Polynomial(new double[]{2, 2, -4});
PolyRootSolver solver = new PolyRoot();
List<? extends Number> roots = solver.solve(p);
log.info("Finding polynomial roots: {}", roots);
}
}