diff --git a/libraries/pom.xml b/libraries/pom.xml index 6bbe8e6f1c..8ffd33272d 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -711,6 +711,12 @@ ${snakeyaml.version} + + com.numericalmethod + suanshu + ${suanshu.version} + + @@ -731,6 +737,12 @@ Apache Staging https://repository.apache.org/content/groups/staging + + nm-repo + Numerical Method's Maven Repository + http://repo.numericalmethod.com/maven/ + default + @@ -835,6 +847,7 @@ + 4.0.0 1.21 1.23.0 0.1.0 diff --git a/libraries/src/main/java/com/baeldung/suanshu/SuanShuMath.java b/libraries/src/main/java/com/baeldung/suanshu/SuanShuMath.java new file mode 100644 index 0000000000..46af24692d --- /dev/null +++ b/libraries/src/main/java/com/baeldung/suanshu/SuanShuMath.java @@ -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 roots = solver.solve(p); + log.info("Finding polynomial roots: {}", roots); + } + +}