Awesome Open Source
Awesome Open Source

finnmath: Mathematical library for the JVM and Android written in Kotlin

License

finnmath is a free software library for the JVM and Android written in Kotlin which provides or will provide implementations of fractions, complex numbers, matrices and vectors and their arithmetic. This library is designed with Kotlin and Java users in mind but the Kotlin support is the main driver for this project (Sequences over Streams, no special treatment of primitives etc.). The great Apache Commons Math and the new Apache Commons Numbers provide less cluttered Java APIs and support implementations based on primitives as well.

What finnmath will provide:

  • Vectors and matrices
  • Fractions and complex numbers
  • Determinant calculation
  • Custom Kotest matchers (separate module)

What finnmath will not provide:

  • Solver for equations
  • Matrix decomposition, reduction or factorization
  • Polynomials

Previously finnmath was written in Java and used Maven as its build automation tool but currently the port to Kotlin and Gradle is in progress. It should interoperate seamlessly with other JVM technologies. Dependencies are big-math, KotlinDiscreteMathToolkit, Katlib, Guava, API Guardian and JetBrains Java Annotations and for tests Kotest, JUnit and AssertJ.

Building

A JDK in version 8 and Gradle 7 is needed to build this project.

gradle default

Implementation details

  • Kotlin 1.5 and Java 8
  • All types are immutable.
  • Absolutely no null values
  • Numbers extend kotlin.Number and fractions implements kotlin.Comparable
  • Comparators implements kotlin.Comparator
  • Numbers, vectors and matrices implement java.io.Serializable
  • Lambdas, Sequences and Streams
  • Classes of numbers, vectors and matrices are final.
  • Useful hashCode, equals and toString methods
  • Data classes
  • Builders for vectors and matrices (Java-style and Kotlin's type-safe builders)
  • Parameter validation and fast failing
  • Singletons via objects
  • Extension functions for kotlin.Int, kotlin.Long, kotlin.Float, kotlin.Double, java.math.BigInteger and java.math.BigDecimal
  • Operators for numbers, vectors and matrices
  • Primary and secondary constructors
  • Default arguments
  • when expressions
  • Derived, delegated and lazy properties
  • Infix notation
  • Single-expression functions
  • JVM annotations
  • Companion objects
  • Generics
  • Lambdas with receivers
  • Explicit API mode
  • Qualified this and implicit this expressions

Usage

Kotlin

// operators
Fraction(2L, 3L) + Fraction(3L, 4L)

// extensions
12.toGaussian()

// getting an element of a vector or matrix
vector[1]
matrix[2, 3]

// check if an element is contained in a vector or matrix
when (0L) {
    in vector -> true
    in matrix -> true
    else -> false
}

// infix notation
Fraction.ZERO min Fraction.ONE

// type-safe builders for vectors and matrices
longVector {
    size = 5
    computationOfAbsent = { _ -> random.nextLong() }
    entry {
        index = 2
        element = 1L
    }
    entry {
        index = 4
        element = 2L
    }
}

Java

class C {
    void m() {
        // arithmetic methods follow the naming scheme of BigInteger/BigDecimal
        Fraction(2L, 3L).add(Fraction(3L, 4L));

        // getting an element of a vector or matrix
        vector.get(1);
        matrix.get(2, 3);

        // check if an element is contained in a vector or matrix
        if (vector.contains(0L)) {
            doSomethingCool();
        }

        // classic builders for vectors and matrices with a fluent API        
        LongVector.builder(5)
                .computationOfAbsent(i -> random.nextLong())
                .set(2, 1L)
                .set(4, 2L)
                .build();
    }
}

Goals

  • Complete KDoc/Javadoc (generated by Dokka)
  • Clean code
  • High code quality with the help of ktlint, detekt and SonarLint
  • High code coverage with the help of Kotest, JUnit, AssertJ and JaCoCo
  • Idiomatic Kotlin and Java API
  • Stay up-to-date with versions of libraries and plugins
  • Object-oriented design with functional abstraction
  • Use Kotlin features to create a DSL-like API
  • Use Kotest features for tests (FunSpec style, matchers and property-based testing)

Please keep in mind that perfection is not reachable, impressions are very subjective and definitions can vary! ;-)

Links

finnmath

finnmath-kotest

Who is Finn?


Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Java (709,240
Kotlin (59,746
Library (11,331
Open Source (7,534
Math (3,611
Mathematics (2,763
Matrix (1,231
Jvm (1,048
Vector (928
Free Software (303
Matrix Multiplication (172
Matrices (155
Complex Numbers (132
Related Projects