Skip to content

deephaven/SuanShu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SuanShu

SuanShu is a Java math library for numerical analysis, statistics, root finding, linear algebra, optimization, curve fitting, interpolation, regression, differential equation solvers, and more.

This repository is a fork of the most recent Open Source SuanShu release (2012-06-06). It has enhancements that include fixes to test cases, serialization, etc. If you are interested in the commercially licensed version, see https://nm.dev/.

Installation

The latest version of this package can be found on Maven.

pom.xml

<dependency>
    <groupId>io.deephaven</groupId>
    <artifactId>SuanShu</artifactId>
    <version>0.1.0</version>
</dependency>

Gradle

implementation 'io.deephaven:SuanShu:0.1.0'

Usage

The following Java class shows a few examples of how to use the package. Source

package com.SuanShuSample.app;

import com.numericalmethod.suanshu.matrix.doubles.Matrix;
import com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense.DenseMatrix;
import com.numericalmethod.suanshu.matrix.doubles.operation.Inverse;
import com.numericalmethod.suanshu.vector.doubles.Vector;
import com.numericalmethod.suanshu.vector.doubles.dense.DenseVector;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

class App {

    private static final Logger log = LoggerFactory.getLogger(App.class);

    public static void main(String[] args) throws Exception {
        App math = new App();
        org.apache.log4j.BasicConfigurator.configure();

        math.addingVectors();
        math.scaleVector();
        math.innerProductVectors();

        math.addingMatrices();
        math.multiplyMatrices();
        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 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 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);
    }

}

About

Extension of original open-sourced math library, SuanShu.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages