Skip to content
/ math Public

Some math-related Scala code written just for fun

License

Notifications You must be signed in to change notification settings

skozlov/math

Repository files navigation

Math

Some math-related Scala code written just for fun.

Features

Strict Arithmetic

Rational numbers:

import com.github.skozlov.math.arithmetic.Implicits._
import com.github.skozlov.math.arithmetic.Rational
0.1 + 0.2 // 0.30000000000000004
BigDecimal("0.1") + BigDecimal("0.2") // 0.3
Rational("0.1") + Rational("0.2") // 3/10
BigDecimal(1) / 3 * 3 // 0.9999999999999999999999999999999999
Rational("1/3") * 3 // 1

Real numbers with arbitrary precision:

import com.github.skozlov.math.arithmetic.{Rational, π}
Math.PI // 3.141592653589793
π.round(precision = Rational(10) ^ -16).toPositional() // 3.1415926535897932

Complex numbers:

import com.github.skozlov.math.arithmetic.{Complex, i}
import com.github.skozlov.math.arithmetic.Implicits._
Complex(0, 1) * i // -1

Algebraic Structures

Declare basic properties of operations, and when you combine them into algebraic structures, the Scala compiler will check that the operations satisfy the axioms of those structures.

For example, if you declare that the string concatenation is associative and has an identity element, then you can use it as a monoid:

import com.github.skozlov.math.algebra.Implicits._
import com.github.skozlov.math.algebra.operation_properties.associativity.Associativity
import com.github.skozlov.math.algebra.operation_properties.id.Id
import com.github.skozlov.math.algebra.structures.Monoid
object StringConcatenation extends ((String, String) => String) {
override def apply(a: String, b: String): String = a + b
override val toString: String = "String concatenation"
}
implicit val stringConcatenationAssociativity
: Associativity[String, StringConcatenation.type] =
new Associativity[String, StringConcatenation.type] {}
implicit val stringConcatenationIdentity: Id[String, StringConcatenation.type] =
Id("")
def printMonoid[A, Op <: (A, A) => A](
op: Op
)(implicit monoid: Monoid[A, Op]): Unit = {
println(s"$op is a monoid with identity element = `${monoid.id.id}`")
}
printMonoid[String, StringConcatenation.type](StringConcatenation)

Contributing

Prerequisites:

  • Git.
  • JDK 17 (have not tested with other versions).
  • SBT 1.x.

How to contribute:

  1. Make the necessary changes in your fork of this repository.
  2. Make sure that the code is covered by tests. 100% coverage is required. The code which may be uncovered should be enclosed in special comments:
// $COVERAGE-OFF$
throw new RuntimeException("This code should never be executed")
// $COVERAGE-ON$
  1. Format Scala and SBT files (sbt scalafmtAll scalafmtSbt).
  2. Verify the code (sbt rebuild). This commands performs sbt clean and then builds the project. To skip clean during development, use sbt build.
  3. Make sure that the branch is clean:
    • typically, contains one commit with a clear message,
    • contains no merge commits.
  4. Create a pull request.
  5. Make sure that the CI pipeline is green.

Contributing with IntelliJ IDEA

Prerequisites (besides common ones described above):

  • IntelliJ IDEA (Community or Ultimate Edition).
  • Recommended plugins:
    • Git.
    • Scala.
  • Project SDK: JDK 17.

How to make IDEA use .scalafmt.conf to reformat the code:

  1. File -> Settings -> Editor -> Code Style -> Scala.
  2. Formatter: Scalafmt.
  3. Configuration: ./.scalafmt.conf (default).

About

Some math-related Scala code written just for fun

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages