spire

math

package math

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. math
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Type Members

  1. case class Above[A] extends Interval[A] with Product with Serializable

  2. final class Algebraic extends ScalaNumber with ScalaNumericConversions with RealLike[Algebraic] with BMFSSBound[Algebraic] with BigDecimalApprox[Algebraic] with FPFilter[Algebraic] with ConstantFolder[Algebraic] with BubbleUpDivs[Algebraic] with PrettyToString[Algebraic] with Ordered[Algebraic] with Serializable

    An general Algebraic type.

    An general Algebraic type. Can be used represent real numbers and approximate them on-demand.

    Annotations
    @SerialVersionUID()
  3. class AlgebraicAlgebra extends AlgebraicIsField with AlgebraicIsNRoot with AlgebraicIsReal with Serializable

    Annotations
    @SerialVersionUID()
  4. trait AlgebraicInstances extends AnyRef

  5. case class All[A] extends Interval[A] with Product with Serializable

  6. trait Approximation[A, B, C] extends (A, B) ⇒ C

    A typeclass approach for getting an approximation to an A using error B in type C.

  7. case class ApproximationContext[A](error: A) extends Product with Serializable

  8. case class Below[A] extends Interval[A] with Product with Serializable

  9. trait BitString[A] extends BooleanAlgebra[A]

  10. final case class Complex[T](real: T, imag: T) extends ScalaNumber with ScalaNumericConversions with Serializable with Product

    Annotations
    @SerialVersionUID()
  11. trait ComplexInstances extends ComplexInstances1

  12. trait ComplexInstances0 extends AnyRef

  13. trait ComplexInstances1 extends ComplexInstances0

  14. class ComplexIsNumeric[A] extends ComplexEq[A] with ComplexIsField[A] with Numeric[Complex[A]] with ComplexIsTrig[A] with ComplexIsNRoot[A] with ConvertableFromComplex[A] with ConvertableToComplex[A] with Order[Complex[A]] with ComplexIsSigned[A] with Serializable

    Annotations
    @SerialVersionUID()
  15. trait ConvertableFrom[A] extends AnyRef

  16. trait ConvertableTo[A] extends AnyRef

  17. final class FixedPoint extends AnyVal

    FixedPoint is a value class that provides fixed point arithmetic operations (using an implicit denominator) to unboxed Long values.

    FixedPoint is a value class that provides fixed point arithmetic operations (using an implicit denominator) to unboxed Long values.

    Working with FixedPoint values is similar to other fractional types, except that most operations require an implicit FixedScale instance (which provides the denominator).

    For example:

    // interpret FixedPoint(n) as n/1000 implicit val scale = FixedScale(1000)

    // these three values are equivalent val a = FixedPoint("12.345") // decimal repr val b = FixedPoint(Rational(2469, 200)) // fraction repr val c = new FixedPoint(12345L) // "raw" repr

  18. class FixedPointOverflow extends Exception

  19. case class FixedScale(denom: Int) extends Product with Serializable

  20. final class FloatComplex extends AnyVal

    Value class which encodes two floating point values in a Long.

    Value class which encodes two floating point values in a Long.

    We get (basically) unboxed complex numbers using this hack. The underlying implementation lives in the FastComplex object.

  21. trait Fractional[A] extends Field[A] with NRoot[A] with Integral[A]

  22. trait HighBranchingMedianOf5 extends AnyRef

  23. trait Integral[A] extends EuclideanRing[A] with ConvertableFrom[A] with ConvertableTo[A] with IsReal[A]

  24. class IntegralOps[A] extends AnyRef

  25. sealed abstract class Interval[A] extends AnyRef

    Interval represents a set of values, usually numbers.

    Interval represents a set of values, usually numbers.

    Intervals have upper and lower bounds. Each bound can be one of three kinds:

    * Closed: The boundary value is included in the interval. * Open: The boundary value is excluded from the interval. * Unbound: There is no boundary value.

    When the underlying type of the interval supports it, intervals may be used in arithmetic. There are several possible interpretations of interval arithmetic: the interval can represent uncertainty about a single value (for instance, a quantity +/- tolerance in engineering) or it can represent all values in the interval simultaneously. In this implementation we have chosen to use the probabillistic interpretation.

    One common pitfall with interval arithmetic is that many familiar algebraic relations do not hold. For instance, given two intervals a and b:

    a == b does not imply a * a == a * b

    Consider a = b = [-1, 1]. Since any number times itself is non-negative, a * a = [0, 1]. However, a * b = [-1, 1], since we may actually have a=1 and b=-1.

    These situations will result in loss of precision (in the form of wider intervals). The result is not wrong per se, but less acccurate than it could be.

  26. final case class Jet[T](real: T, infinitesimal: Array[T]) extends ScalaNumber with ScalaNumericConversions with Serializable with Product

    Annotations
    @SerialVersionUID()
  27. case class JetDim(dimension: Int) extends Product with Serializable

    Used to implicitly define the dimensionality of the Jet space.

    Used to implicitly define the dimensionality of the Jet space.

    dimension

    the number of dimensions.

  28. trait JetInstances extends AnyRef

  29. trait MutatingMedianOf5 extends AnyRef

  30. sealed abstract class Natural extends ScalaNumber with ScalaNumericConversions with Serializable

    Annotations
    @SerialVersionUID()
  31. class NaturalAlgebra extends NaturalIsRig with NaturalIsReal with Serializable

    Annotations
    @SerialVersionUID()
  32. trait NaturalInstances extends AnyRef

  33. sealed trait Number extends ScalaNumber with ScalaNumericConversions with Serializable

  34. class NumberAlgebra extends NumberIsField with NumberIsNRoot with NumberIsTrig with NumberIsReal with Serializable

    Annotations
    @SerialVersionUID()
  35. trait NumberInstances extends AnyRef

  36. trait Numeric[A] extends Ring[A] with AdditiveAbGroup[A] with MultiplicativeAbGroup[A] with NRoot[A] with ConvertableFrom[A] with ConvertableTo[A] with IsReal[A]

    TODO 3.

    TODO 3. LiteralOps? Literal conversions? 4. Review operator symbols? 5. Support for more operators? 6. Start to worry about things like e.g. pow(BigInt, BigInt)

  37. trait Polynomial[C] extends AnyRef

  38. trait PolynomialEq[C] extends Eq[Polynomial[C]]

  39. trait PolynomialEuclideanRing[C] extends PolynomialRing[C] with EuclideanRing[Polynomial[C]] with VectorSpace[Polynomial[C], C]

  40. trait PolynomialInstances extends PolynomialInstances3

  41. trait PolynomialInstances0 extends AnyRef

  42. trait PolynomialInstances1 extends PolynomialInstances0

  43. trait PolynomialInstances2 extends PolynomialInstances1

  44. trait PolynomialInstances3 extends PolynomialInstances2

  45. trait PolynomialRig[C] extends PolynomialSemiring[C] with Rig[Polynomial[C]]

  46. trait PolynomialRing[C] extends PolynomialRng[C] with Ring[Polynomial[C]]

  47. trait PolynomialRng[C] extends PolynomialSemiring[C] with RingAlgebra[Polynomial[C], C]

  48. trait PolynomialSemiring[C] extends Semiring[Polynomial[C]]

  49. final case class Quaternion[A](r: A, i: A, j: A, k: A) extends ScalaNumber with ScalaNumericConversions with Serializable with Product

  50. trait QuaternionInstances extends AnyRef

  51. case class Ranged[A] extends Interval[A] with Product with Serializable

  52. sealed abstract class Rational extends ScalaNumber with ScalaNumericConversions with Ordered[Rational]

  53. class RationalAlgebra extends RationalIsField with RationalIsReal with Serializable

    Annotations
    @SerialVersionUID()
  54. trait RationalInstances extends AnyRef

  55. class RationalIsNRoot0 extends RationalIsNRoot with Serializable

    Annotations
    @SerialVersionUID()
  56. sealed trait Real extends ScalaNumber with ScalaNumericConversions

  57. trait RealIsFractional extends Fractional[Real] with Order[Real] with Signed[Real] with Trig[Real]

  58. sealed trait SafeLong extends ScalaNumber with ScalaNumericConversions with Ordered[SafeLong]

    Provides a type to do safe long arithmetic.

    Provides a type to do safe long arithmetic. This type will never overflow, but rather convert the underlying long to a BigInt as need and back down to a Long when possible.

  59. trait SafeLongInstances extends AnyRef

  60. trait Select extends AnyRef

  61. trait SelectLike extends Select

    Given a function for finding approximate medians, this will create an exact median finder.

  62. trait Sort extends AnyRef

    Interface for a sorting strategy object.

  63. final class UByte extends AnyVal with ScalaNumericAnyConversions

  64. trait UByteInstances extends AnyRef

  65. final class UInt extends AnyVal

  66. trait UIntInstances extends AnyRef

  67. final class ULong extends AnyVal

  68. trait ULongInstances extends AnyRef

  69. final class UShort extends AnyVal

  70. trait UShortInstances extends AnyRef

Value Members

  1. object Algebraic extends AlgebraicInstances with Serializable

  2. object Approximation

  3. object ApproximationContext extends Serializable

  4. object BitString

  5. object Complex extends ComplexInstances with Serializable

  6. object ConvertableFrom

  7. object ConvertableTo

  8. object FastComplex

    FastComplex is an ugly, beautiful hack.

    FastComplex is an ugly, beautiful hack.

    The basic idea is to encode two 32-bit Floats into a single 64-bit Long. The lower-32 bits are the "real" Float and the upper-32 are the "imaginary" Float.

    Since we're overloading the meaning of Long, all the operations have to be defined on the FastComplex object, meaning the syntax for using this is a bit ugly. To add to the ugly beauty of the whole thing I could imagine defining implicit operators on Long like +@, -@, *@, /@, etc.

    You might wonder why it's even worth doing this. The answer is that when you need to allocate an array of e.g. 10-20 million complex numbers, the GC overhead of using *any* object is HUGE. Since we can't build our own "pass-by-value" types on the JVM we are stuck doing an encoding like this.

    Here are some profiling numbers for summing an array of complex numbers, timed against a concrete case class implementation using Float (in ms):

    size | encoded | class 1M | 5.1 | 5.8 5M | 28.5 | 91.7 10M | 67.7 | 828.1 20M | 228.0 | 2687.0

    Not bad, eh?

  9. object FixedPoint

  10. object FloatComplex

  11. object Fractional

  12. final def IEEEremainder(x: Double, d: Double): Double

  13. object InsertionSort extends Sort

    Simple implementation of insertion sort.

    Simple implementation of insertion sort.

    Works for small arrays but due to O(n^2) complexity is not generally good.

  14. object Integral

  15. object Interval

  16. object Jet extends JetInstances with Serializable

    A simple implementation of N-dimensional dual numbers, for automatically computing exact derivatives of functions.

    Overview

    A simple implementation of N-dimensional dual numbers, for automatically computing exact derivatives of functions. This code (and documentation) closely follow the one in Google's "Ceres" library of non-linear least-squares solvers (see Sameer Agarwal, Keir Mierle, and others: Ceres Solver.)

    While a complete treatment of the mechanics of automatic differentiation is beyond the scope of this header (see http://en.wikipedia.org/wiki/Automatic_differentiation for details), the basic idea is to extend normal arithmetic with an extra element "h" such that h != 0, but h2 = 0. Dual numbers are extensions of the real numbers analogous to complex numbers: whereas complex numbers augment the reals by introducing an imaginary unit i such that i2 = -1, dual numbers introduce an "infinitesimal" unit h such that h2 = 0. Analogously to a complex number c = x + y*i, a dual number d = x * y*h has two components: the "real" component x, and an "infinitesimal" component y. Surprisingly, this leads to a convenient method for computing exact derivatives without needing to manipulate complicated symbolic expressions.

    For example, consider the function

    f(x) = x * x ,

    evaluated at 10. Using normal arithmetic, f(10) = 100, and df/dx(10) = 20. Next, augment 10 with an infinitesimal h to get:

    f(10 + h) = (10 + h) * (10 + h)
    = 100 + 2 * 10 * h + h * h
    = 100 + 20 * h       +---
            +-----       |
            |            +--- This is zero
            |
            +----------------- This is df/dx

    Note that the derivative of f with respect to x is simply the infinitesimal component of the value of f(x + h). So, in order to take the derivative of any function, it is only necessary to replace the numeric "object" used in the function with one extended with infinitesimals. The class Jet, defined in this header, is one such example of this, where substitution is done with generics.

    To handle derivatives of functions taking multiple arguments, different infinitesimals are used, one for each variable to take the derivative of. For example, consider a scalar function of two scalar parameters x and y:

    f(x, y) = x * x + x * y

    Following the technique above, to compute the derivatives df/dx and df/dy for f(1, 3) involves doing two evaluations of f, the first time replacing x with x + h, the second time replacing y with y + h.

    For df/dx:

    f(1 + h, y) = (1 + h) * (1 + h) + (1 + h) * 3
                = 1 + 2 * h + 3 + 3 * h
                = 4 + 5 * h
    
    Therefore df/dx = 5

    For df/dy:

    f(1, 3 + h) = 1 * 1 + 1 * (3 + h)
                = 1 + 3 + h
                = 4 + h
    
    Therefore df/dy = 1

    To take the gradient of f with the implementation of dual numbers ("jets") in this file, it is necessary to create a single jet type which has components for the derivative in x and y, and pass them to a routine computing function f. It is convenient to use a generic version of f, that can be called also with non-jet numbers for standard evaluation:

    def f[@specialized(Double) T : Field](x: T, y: T): T = x * x + x * y
    
    val xValue = 9.47892774
    val yValue = 0.287740
    
    // The "2" means there should be 2 dual number components.
    implicit val dimension = JetDim(2)
    val x: Jet[Double] = xValue + Jet.h[Double](0);  // Pick the 0th dual number for x.
    val y: Jet[Double] = yValue + Jet.h[Double](1);  // Pick the 1th dual number for y.
    
    val z: Jet[Double] = f(x, y);
    println("df/dx = " + z.infinitesimal(0) + ", df/dy = " + z.infinitesimal(1));

    For the more mathematically inclined, this file implements first-order "jets". A 1st order jet is an element of the ring

    T[N] = T[t_1, ..., t_N] / (t_1, ..., t_N)^2

    which essentially means that each jet consists of a "scalar" value 'a' from T and a 1st order perturbation vector 'v' of length N:

    x = a + \sum_i v[i] t_i

    A shorthand is to write an element as x = a + u, where u is the perturbation. Then, the main point about the arithmetic of jets is that the product of perturbations is zero:

    (a + u) * (b + v) = ab + av + bu + uv
    = ab + (av + bu) + 0

    which is what operator* implements below. Addition is simpler:

    (a + u) + (b + v) = (a + b) + (u + v).

    The only remaining question is how to evaluate the function of a jet, for which we use the chain rule:

    f(a + u) = f(a) + f'(a) u

    where f'(a) is the (scalar) derivative of f at a.

    By pushing these things through generics, we can write routines that at same time evaluate mathematical functions and compute their derivatives through automatic differentiation.

  17. object LinearSelect extends SelectLike with HighBranchingMedianOf5

  18. object MergeSort extends Sort

    In-place merge sort implementation.

    In-place merge sort implementation. This sort is stable but does mutate the given array. It is an in-place sort but it does allocate a temporary array of the same size as the input. It uses InsertionSort for sorting very small arrays.

  19. object Natural extends NaturalInstances with Serializable

  20. object Number extends NumberInstances with Serializable

    Convenient apply and implicits for Numbers

  21. object Numeric

  22. object Polynomial extends PolynomialInstances

    Polynomial A univariate polynomial class and EuclideanRing extension trait for arithmetic operations.

    Polynomial A univariate polynomial class and EuclideanRing extension trait for arithmetic operations. Polynomials can be instantiated using any type C for which a Ring[C] and Signed[C] are in scope, with exponents given by Int values. Some operations require a Field[C] to be in scope.

  23. object Quaternion extends QuaternionInstances with Serializable

  24. object QuickSelect extends SelectLike with HighBranchingMedianOf5

  25. object QuickSort

    In-place quicksort implementation.

    In-place quicksort implementation. It is not stable, but does not allocate extra space (other than stack). Like MergeSort, it uses InsertionSort for sorting very small arrays.

  26. object Rational extends RationalInstances with Serializable

  27. object Real extends Serializable

  28. object SafeLong extends SafeLongInstances with Serializable

  29. object Searching

  30. object Selection

  31. object Sorting

    Object providing in-place sorting capability for arrays.

    Object providing in-place sorting capability for arrays.

    Sorting.sort() uses quickSort() by default (in-place, not stable, generally fastest but might hit bad cases where it's O(n^2)). Also provides mergeSort() (in-place, stable, uses extra memory, still pretty fast) and insertionSort(), which is slow except for small arrays.

  32. object UByte extends UByteInstances

  33. object UInt extends UIntInstances

  34. object ULong extends ULongInstances

  35. object UShort extends UShortInstances

  36. final def abs[A](a: A)(implicit ev: Signed[A]): A

  37. final def abs(n: Double): Double

  38. final def abs(n: Float): Float

  39. final def abs(n: Long): Long

  40. final def abs(n: Int): Int

  41. final def abs(n: Short): Short

  42. final def abs(n: Byte): Byte

    abs

  43. final def acos[A](a: A)(implicit ev: Trig[A]): A

  44. package algebraic

  45. final def asin[A](a: A)(implicit ev: Trig[A]): A

  46. final def atan[A](a: A)(implicit ev: Trig[A]): A

  47. final def atan2[A](y: A, x: A)(implicit ev: Trig[A]): A

  48. final def cbrt(x: Double): Double

  49. final def ceil[A](a: A)(implicit ev: IsReal[A]): A

  50. final def ceil(n: BigDecimal): BigDecimal

  51. final def ceil(n: Double): Double

  52. final def ceil(n: Float): Float

    ceil

  53. def choose(n: Long, k: Long): BigInt

    choose (binomial coefficient)

  54. final def copySign(m: Float, s: Float): Float

  55. final def copySign(m: Double, s: Double): Double

  56. final def cos[A](a: A)(implicit ev: Trig[A]): A

  57. final def cosh(x: Double): Double

  58. final def cosh[A](x: A)(implicit ev: Trig[A]): A

  59. final def e[A](implicit ev: Trig[A]): A

  60. final def e: Double

    e

  61. final def exp[A](a: A)(implicit t: Trig[A]): A

  62. final def exp(k: BigDecimal): BigDecimal

  63. final def exp(k: Int, precision: Int): BigDecimal

  64. final def exp(n: Double): Double

    exp

  65. final def expm1(x: Double): Double

  66. def fact(n: Long): BigInt

    factorial

  67. def fib(n: Long): BigInt

    fibonacci

  68. final def floor[A](a: A)(implicit ev: IsReal[A]): A

  69. final def floor(n: BigDecimal): BigDecimal

  70. final def floor(n: Double): Double

  71. final def floor(n: Float): Float

    floor

  72. package fpf

  73. final def gcd[A](x: A, y: A, z: A, rest: A*)(implicit ev: EuclideanRing[A]): A

  74. final def gcd[A](xs: Seq[A])(implicit ev: EuclideanRing[A]): A

  75. final def gcd[A](x: A, y: A)(implicit ev: EuclideanRing[A]): A

  76. final def gcd(a: BigInt, b: BigInt): BigInt

  77. final def gcd(_x: Long, _y: Long): Long

    gcd

  78. final def getExponent(x: Float): Int

  79. final def getExponent(x: Double): Int

  80. final def hypot(x: Double, y: Double): Double

  81. final def lcm[A](x: A, y: A)(implicit ev: EuclideanRing[A]): A

  82. final def lcm(a: BigInt, b: BigInt): BigInt

  83. final def lcm(x: Long, y: Long): Long

    lcm

  84. final def log[A](a: A)(implicit t: Trig[A]): A

  85. final def log(n: BigDecimal): BigDecimal

  86. final def log(n: Double): Double

    log

  87. final def log10(x: Double): Double

  88. final def log1p(x: Double): Double

  89. final def max[A](x: A, y: A)(implicit ev: Order[A]): A

  90. final def max(x: Double, y: Double): Double

  91. final def max(x: Float, y: Float): Float

  92. final def max(x: Long, y: Long): Long

  93. final def max(x: Int, y: Int): Int

  94. final def max(x: Short, y: Short): Short

  95. final def max(x: Byte, y: Byte): Byte

    max

  96. final def min[A](x: A, y: A)(implicit ev: Order[A]): A

  97. final def min(x: Double, y: Double): Double

  98. final def min(x: Float, y: Float): Float

  99. final def min(x: Long, y: Long): Long

  100. final def min(x: Int, y: Int): Int

  101. final def min(x: Short, y: Short): Short

  102. final def min(x: Byte, y: Byte): Byte

    min

  103. final def nextAfter(x: Float, y: Float): Float

  104. final def nextAfter(x: Double, y: Double): Double

  105. final def nextUp(x: Float): Float

  106. final def nextUp(x: Double): Double

  107. final def pi[A](implicit ev: Trig[A]): A

  108. final def pi: Double

    pi

  109. package poly

  110. final def pow(base: Double, exponent: Double): Double

  111. final def pow(base: Long, exponent: Long): Long

    Exponentiation function, e.

    Exponentiation function, e.g. x^y

    If base^ex doesn't fit in a Long, the result will overflow (unlike Math.pow which will return +/- Infinity).

  112. final def pow(base: BigInt, ex: BigInt): BigInt

  113. final def pow(base: BigDecimal, exponent: BigDecimal): BigDecimal

    pow

  114. package prime

    Basic tools for prime factorization.

    Basic tools for prime factorization.

    This package is intended to provide tools for factoring numbers, checking primality, generating prime numbers, etc. For now, its main contributions are a method for factoring integers (spire.math.prime.factor) and a type for representing prime factors and their exponents (spire.math.prime.Factors).

    The factorization currently happens via an implementation of Pollard-Rho with Brent's optimization. This technique works very well for composites with small prime factors (up to 10 decimal digits or so) and can support semiprimes (products of two similarly-sized primes) of 20-40 digits.

    The implementation does cheat, use BigInteger.isProbablePrime(30) to test basic primality. This has a roughly 1-in-1,000,000,000,000 chance of being wrong.

    Since Pollard-Rho uses random primes, its performance is somewhat non-deterministic. On this machine, factoring 20-digit semiprimes seem to average about 1.5s and factoring 30-digit semiprimes seem to average about 20s. Much larger numbers can be factored provided they are either prime or composites with smallish factors.

  115. final def random(): Double

  116. final def rint(x: Double): Double

  117. final def round[A](a: A)(implicit ev: IsReal[A]): A

  118. final def round(a: BigDecimal): BigDecimal

  119. final def round(a: Double): Double

  120. final def round(a: Float): Float

    round

  121. final def scalb(d: Float, s: Int): Float

  122. final def scalb(d: Double, s: Int): Double

  123. final def signum[A](a: A)(implicit ev: Signed[A]): Int

  124. final def signum(x: Float): Float

  125. final def signum(x: Double): Double

    signum

  126. final def sin[A](a: A)(implicit ev: Trig[A]): A

  127. final def sinh[A](x: A)(implicit ev: Trig[A]): A

  128. final def sqrt[A](a: A)(implicit ev: NRoot[A]): A

  129. final def sqrt(x: Double): Double

    sqrt

  130. final def tan[A](a: A)(implicit ev: Trig[A]): A

  131. final def tanh[A](x: A)(implicit ev: Trig[A]): A

  132. final def toDegrees(a: Double): Double

  133. final def toRadians(a: Double): Double

  134. final def ulp(x: Float): Double

  135. final def ulp(x: Double): Double

Inherited from AnyRef

Inherited from Any

Ungrouped