spire.algebra

Type members

Classlikes

trait Action[@specialized(Int) P, G] extends LeftAction[P, G] with RightAction[P, G]

A semigroup/monoid/group action of G on P is the combination of compatible left and right actions, providing:

A semigroup/monoid/group action of G on P is the combination of compatible left and right actions, providing:

 - the implementation of a method `actl(g, p)`, or `g |+|> p`, such that:

1. `(g |+| h) |+|> p === g |+|> (h |+|> p)` for all `g`, `h` in `G` and `p` in `P`.

2. `id |+|> p === p` for all `p` in `P` (if `id` is defined)

 - the implementation of a method `actr(p, g)`, or `p <|+| g`, such that:

3. `p <|+| (g |+| h) === (p <|+| g) <|+| h` for all `g`, `h` in `G` and `p` in `P`.

4. `p <|+| id === p` for all `p` in `P` (if `id` is defined)

In addition, if `G` is a group, left and right actions are compatible:

5. `g |+|> p === p <|+| g.inverse`.
Companion
object
object Action
Companion
class
object Additive
trait AdditiveAction[@specialized(Int) P, G]
trait AdditiveTorsor[V, @specialized(Int, Long, Float, Double) R] extends AdditiveAction[V, R]
Companion
object
Companion
class
trait CModule[V, @specialized(Int, Long, Float, Double) R] extends LeftModule[V, R] with RightModule[V, R]

A module over a commutative ring has by definition equivalent left and right modules.

A module over a commutative ring has by definition equivalent left and right modules.

In addition to the laws above 1-5 left and 1-5 right, we have:

6. (r *: x) :* s = r *: (x :* s)
Type Params
R

Scalar type

V

Abelian group type

Companion
object
object CModule
Companion
class
trait CoordinateSpace[V, @specialized(Float, Double) F] extends InnerProductSpace[V, F]
Companion
object
Companion
class
trait DivisionRing[@specialized(Byte, Short, Int, Long, Float, Double) A] extends Ring[A] with MultiplicativeGroup[A]
Companion
object
Companion
class
trait EuclideanRing[@specialized(Int, Long, Float, Double) A] extends GCDRing[A]

EuclideanRing implements a Euclidean domain.

EuclideanRing implements a Euclidean domain.

The formal definition says that every euclidean domain A has (at least one) euclidean function f: A -> N (the natural numbers) where:

(for every x and non-zero y) x = yq + r, and r = 0 or f(r) < f(y).

This generalizes the Euclidean division of integers, where f represents a measure of length (or absolute value), and the previous equation represents finding the quotient and remainder of x and y. So:

 quot(x, y) = q
 mod(x, y) = r
Companion
object
trait Field[@specialized(Int, Long, Float, Double) A] extends Field[A] with EuclideanRing[A]

Field type class. While algebra already provides one, we provide one in Spire that integrates with the commutative ring hierarchy, in particular GCDRing and EuclideanRing.

Field type class. While algebra already provides one, we provide one in Spire that integrates with the commutative ring hierarchy, in particular GCDRing and EuclideanRing.

On a field, all nonzero elements are invertible, so the remainder of the division is always 0. The Euclidean function can take an arbitrary value on nonzero elements (it is undefined for zero); for compatibility with the degree of polynomials, we use the constant 0.

The GCD and LCM are defined up to a unit; on a field, it means that either the GCD or LCM can be fixed arbitrarily. Some conventions with consistent defaults are provided in the spire.algebra.Field companion object.

Companion
object
object Field extends FieldFunctions[Field] with EuclideanRingFunctions[Field]
Companion
class
trait FieldAssociativeAlgebra[V, @specialized(Float, Double) F] extends RingAssociativeAlgebra[V, F] with VectorSpace[V, F]

A FieldAlgebra is a vector space that is also a Ring. An example is the complex numbers.

A FieldAlgebra is a vector space that is also a Ring. An example is the complex numbers.

Companion
object
trait GCDRing[@specialized(Int, Long, Float, Double) A] extends CommutativeRing[A]

GCDRing implements a GCD ring.

GCDRing implements a GCD ring.

For two elements x and y in a GCD ring, we can choose two elements d and m such that:

d = gcd(x, y)
m = lcm(x, y)
d * m = x * y

Additionally, we require:

gcd(0, 0) = 0
lcm(x, 0) = lcm(0, x) = 0

and commutativity:

gcd(x, y) = gcd(y, x)
lcm(x, y) = lcm(y, x)
Companion
object
Companion
class
trait GCDRingFunctions[R <: (GCDRing)] extends RingFunctions[R]
trait InnerProductSpace[V, @specialized(Int, Long, Float, Double) F] extends VectorSpace[V, F]
Companion
object
Companion
class
trait Involution[@specialized(Int, Long, Float, Double) A]

Describes an involution, which is an operator that satisfies f(f(x)) = x (rule i).

Describes an involution, which is an operator that satisfies f(f(x)) = x (rule i).

If a multiplicative semigroup is available, it describes an antiautomorphism (rule ii); on a multiplicative monoid, it preserves the identity (rule iii).

If a ring is available, it should be compatible with addition (rule iv), and then defines a *-ring (see https://en.wikipedia.org/wiki/%2A-algebra ).

   i. adjoint(adjoint(x)) = x
   ii. adjoint(x*y) = adjoint(y)*adjoint(x) (with an underlying multiplicative semigroup)
 iii. adjoint(1) = 1 (with an underlying multiplicative monoid)
   iv. adjoint(x+y) = adjoint(x)+adjoint(y) (with an underlying ring)

A *-algebra is an associative algebra A over a commutative *-ring R, where A has an involution as well. It satisfies, for x: A, y: A and r: R

   v. adjoint(r * x + y) = adjoint(r)*adjoint(x) + adjoint(y)
Companion
object
object Involution
Companion
class
trait IsAlgebraic[@specialized A] extends IsReal[A]
Companion
object
Companion
class
trait IsIntegral[A] extends IsRational[A]
Companion
object
object IsIntegral
Companion
class
trait IsRational[A] extends IsAlgebraic[A]
Companion
object
object IsRational
Companion
class
trait IsReal[@specialized A] extends Signed[A]

A simple type class for numeric types that are a subset of the reals.

A simple type class for numeric types that are a subset of the reals.

Companion
object
object IsReal
Companion
class
trait LeftAction[@specialized(Int) P, G]

A (left) semigroup/monoid/group action of G on P is simply the implementation of a method actl(g, p), or g |+|> p, such that:

A (left) semigroup/monoid/group action of G on P is simply the implementation of a method actl(g, p), or g |+|> p, such that:

1. `(g |+| h) |+|> p === g |+|> (h |+|> p)` for all `g`, `h` in `G` and `p` in `P`.

2. `id |+|> p === p` for all `p` in `P` (if `id` is defined)
Companion
object
object LeftAction
Companion
class
trait LeftModule[V, @specialized(Int, Long, Float, Double) R] extends AdditiveCommutativeGroup[V]

A left module is a generalization of a vector space over a field, where the scalars are the elements of a ring (not necessarily commutative).

A left module is a generalization of a vector space over a field, where the scalars are the elements of a ring (not necessarily commutative).

A left module has left multiplication by scalars. Let V be an abelian group (with additive notation) and R the scalar ring, we have the following laws for x, y in V and r, s in R:

 1. r *: (x + y) = r *: x + r *: y
 2. (r + s) *: x = r *: x + s *: x
 3. (r * s) *: x = r *: (s *: x)
 4. R.one * x = x

(see https://en.wikipedia.org/wiki/Module_(mathematics) )

Type Params
R

Scalar type

V

Abelian group type

Companion
object
object LeftModule
Companion
class
trait MetricSpace[V, @specialized(Int, Long, Float, Double) R]

This type class models a metric space V. The distance between 2 points in V is measured in R, which should be real (ie. IsReal[R] exists).

This type class models a metric space V. The distance between 2 points in V is measured in R, which should be real (ie. IsReal[R] exists).

Companion
object
object MetricSpace extends MetricSpace0
Companion
class
trait MultiplicativeAction[@specialized(Int) P, G]
trait MultiplicativeTorsor[V, @specialized(Int, Long, Float, Double) R] extends MultiplicativeAction[V, R]
Companion
object
Companion
class
trait NRoot[@specialized(Double, Float, Int, Long) A]

This is a type class for types with n-roots. The value returned by nroot and sqrt are only guaranteed to be approximate answers (except in the case of Real).

This is a type class for types with n-roots. The value returned by nroot and sqrt are only guaranteed to be approximate answers (except in the case of Real).

Also, generally nroots where n is even are not defined for negative numbers. The behaviour is undefined if this is attempted. It would be nice to ensure an exception is raised, but some types may defer computation and testing if a value is negative may not be ideal. So, do not count on ArithmeticExceptions to save you from bad arithmetic!

Companion
object
object NRoot
Companion
class
trait NormedVectorSpace[V, @specialized(Int, Long, Float, Double) F] extends VectorSpace[V, F] with MetricSpace[V, F]

A normed vector space is a vector space equipped with a function norm: V => F. The main constraint is that the norm of a vector must be scaled linearly when the vector is scaled; that is norm(k *: v) == k.abs * norm(v). Additionally, a normed vector space is also a MetricSpace, where distance(v, w) = norm(v - w), and so must obey the triangle inequality.

A normed vector space is a vector space equipped with a function norm: V => F. The main constraint is that the norm of a vector must be scaled linearly when the vector is scaled; that is norm(k *: v) == k.abs * norm(v). Additionally, a normed vector space is also a MetricSpace, where distance(v, w) = norm(v - w), and so must obey the triangle inequality.

An example of a normed vector space is R^n equipped with the euclidean vector length as the norm.

Companion
object
object NormedVectorSpace extends NormedVectorSpace0 with NormedVectorSpaceFunctions
Companion
class
trait RightAction[@specialized(Int) P, G]

A (right) semigroup/monoid/group action of G on P is simply the implementation of a method actr(p, g), or p <|+| g, such that:

A (right) semigroup/monoid/group action of G on P is simply the implementation of a method actr(p, g), or p <|+| g, such that:

1. `p <|+| (g |+| h) === (p <|+| g) <|+| h` for all `g`, `h` in `G` and `p` in `P`.

2. `p <|+| id === p` for all `p` in `P` (if `id` is defined)
Companion
object
Companion
class
trait RightModule[V, @specialized(Int, Long, Float, Double) R] extends AdditiveCommutativeGroup[V]

A right module is a generalization of a vector space over a field, where the scalars are the elements of a ring (not necessarily commutative).

A right module is a generalization of a vector space over a field, where the scalars are the elements of a ring (not necessarily commutative).

A right module has right multiplication by scalars. Let V be an abelian group (with additive notation) and R the scalar ring, we have the following laws for x, y in V and r, s in R:

 1. (x + y) :* r = x :* r + y :* r
 2. x :* (r + s) = x :* r + x :* s
 3. x :* (r * s) = (x :* r) :* s
 4. x :* R.one = x
Type Params
R

Scalar type

V

Abelian group type

Companion
object
Companion
class
trait RingAssociativeAlgebra[V, @specialized R] extends CModule[V, R] with Ring[V]

A RingAssociativeAlgebra is a R-module that is also a Ring. An example is the Gaussian numbers, the quaternions, etc...

A RingAssociativeAlgebra is a R-module that is also a Ring. An example is the Gaussian numbers, the quaternions, etc...

The scalar multiplication satisfies, for r in R, and x, y in V:

 1. r *: (x * y) = (r *: x) * y = x * (r *: y)

TODO: verify the definition, in particular the requirements for Ring[V] (and not Rng[V])

Companion
object
sealed abstract
class Sign(val toInt: Int)

A simple ADT representing the Sign of an object.

A simple ADT representing the Sign of an object.

Companion
object
object Sign
Companion
class
trait Signed[@specialized(Byte, Short, Int, Long, Float, Double) A] extends Order[A]

A trait for linearly ordered additive commutative monoid. The following laws holds:

A trait for linearly ordered additive commutative monoid. The following laws holds:

(1) if `a <= b` then `a + c <= b + c` (linear order),
(2) `signum(x) = -1` if `x < 0`, `signum(x) = 1` if `x > 0`, `signum(x) = 0` otherwise,

Negative elements only appear when scalar is a additive abelian group, and then

(3) `abs(x) = -x` if `x < 0`, or `x` otherwise,

Laws (1) and (2) lead to the triange inequality:

(4) `abs(a + b) <= abs(a) + abs(b)`

Signed should never be extended in implementations, rather the AdditiveCMonoid and AdditiveAbGroup subtraits. We cannot use self-types to express the constraint self: AdditiveCMonoid => (interaction with specialization?).

Companion
object
object Signed
Companion
class
trait SignedAdditiveAbGroup[@specialized(Byte, Short, Int, Long, Float, Double) A] extends SignedAdditiveCMonoid[A] with AdditiveCommutativeGroup[A]
trait SignedAdditiveCMonoid[@specialized(Byte, Short, Int, Long, Float, Double) A] extends Signed[A] with AdditiveCommutativeMonoid[A]
trait Torsor[V, @specialized(Int, Long, Float, Double) R] extends Action[V, R]

A Torsor[V, R] requires an AbGroup[R] and provides Action[V, R], plus a diff operator, <-> in additive notation, such that:

A Torsor[V, R] requires an AbGroup[R] and provides Action[V, R], plus a diff operator, <-> in additive notation, such that:

1. `(g <-> g) === scalar.id` for all `g` in `G`.

2. `(g <-> h) +> h === g` for all `g`, `h` in `G`.

3. `(g <-> h) +> i === (i <-> h) +> g` for all `g`, `h` in `G`.

4. `(g <-> h) === -(h <-> g)` for all `g`, `h` in `G`.
Companion
object
object Torsor
Companion
class
trait Trig[@specialized(Float, Double) A]
Companion
object
object Trig
Companion
class
trait TruncatedDivision[@specialized(Byte, Short, Int, Long, Float, Double) A] extends Signed[A]

Division and modulus for computer scientists taken from https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf

Division and modulus for computer scientists taken from https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf

For two numbers x (dividend) and y (divisor) on an ordered ring with y != 0, there exists a pair of numbers q (quotient) and r (remainder) such that these laws are satisfied:

(1) q is an integer
(2) x = y * q + r (division rule)
(3) |r| < |y|
(4t) r = 0 or sign(r) = sign(x)
(4f) r = 0 or sign(r) = sign(y).

where sign is the sign function, and the absolute value function |x| is defined as |x| = x if x >=0, and |x| = -x otherwise.

We define functions tmod and tquot such that:

q = tquot(x, y) and r = tmod(x, y) obey rule (4t),
(whichtruncates effectively towards zero) and functions fmod and fquot such that:
q = fquot(x, y) and r = fmod(x, y) obey rule (4f)
(which floors the quotient and effectively rounds towards negative infinity).

Law (4t) corresponds to ISO C99 and Haskell's quot/rem.

Law (4f) is described by Knuth and used by Haskell, and fmod corresponds to the REM function of the IEEE floating-point standard.

Companion
object
Companion
class
trait TruncatedDivisionCRing[@specialized(Byte, Short, Int, Long, Float, Double) A] extends TruncatedDivision[A] with CommutativeRing[A]
trait UniqueFactorizationDomain[@specialized(Byte, Short, Int, Long) A]

A unique factorization domain is a commutative ring in which each element can be written as a product of prime elements and a unit.

A unique factorization domain is a commutative ring in which each element can be written as a product of prime elements and a unit.

Unique factorization domains are GCD rings (or domains), but not necessarily Euclidean domains.

This trait is outside the commutative ring hierarchy, because the factorization algorithms are costly. Another reason: in some cases, a deliberate choice should be made by the user, for example to use probabilistic algorithms with a specified probability of failure.

Companion
object
trait VectorSpace[V, @specialized(Int, Long, Float, Double) F] extends CModule[V, F]

A vector space is a group V that can be multiplied by scalars in F that lie in a field. Scalar multiplication must distribute over vector addition

A vector space is a group V that can be multiplied by scalars in F that lie in a field. Scalar multiplication must distribute over vector addition

(`x *: (v + w) === x *: v + x *: w`) and scalar addition
(`(x + y) *: v === x *: v + y *: v`). Scalar multiplication by 1 in `F` is an identity function
(`1 *: v === v`). Scalar multiplication is "associative" (`x *: y *: v === (x * y) *: v`).
Companion
object
Companion
class
trait ZAlgebra[V] extends RingAssociativeAlgebra[V, Int] with Ring[V]

Given any Ring[A] we can construct a RingAlgebra[A, Int]. This is possible since we can define fromInt on Ring generally.

Given any Ring[A] we can construct a RingAlgebra[A, Int]. This is possible since we can define fromInt on Ring generally.

Companion
object
object ZAlgebra
Companion
class

Types

type AbGroup[A] = CommutativeGroup[A]
type AdditiveAbGroup[A] = AdditiveCommutativeGroup[A]
type AdditiveCMonoid[A] = AdditiveCommutativeMonoid[A]
type AdditiveCSemigroup[A] = AdditiveCommutativeSemigroup[A]
type AdditiveGroup[A] = AdditiveGroup[A]
type AdditiveMonoid[A] = AdditiveMonoid[A]
type AdditiveSemigroup[A] = AdditiveSemigroup[A]
type AlgebraField[A] = Field[A]
type Bool[A] = Bool[A]
type CMonoid[A] = CommutativeMonoid[A]
type CRig[A] = CommutativeRig[A]
type CRing[A] = CommutativeRing[A]
type CRng[A] = CommutativeRng[A]
type CSemigroup[A] = CommutativeSemigroup[A]
type CSemiring[A] = CommutativeSemiring[A]
type Eq[A] = Eq[A]
type Group[A] = Group[A]
type Monoid[A] = Monoid[A]
type MultiplicativeAbGroup[A] = MultiplicativeCommutativeGroup[A]
type MultiplicativeCMonoid[A] = MultiplicativeCommutativeMonoid[A]
type MultiplicativeCSemigroup[A] = MultiplicativeCommutativeSemigroup[A]
type MultiplicativeGroup[A] = MultiplicativeGroup[A]
type MultiplicativeMonoid[A] = MultiplicativeMonoid[A]
type MultiplicativeSemigroup[A] = MultiplicativeSemigroup[A]
type Order[A] = Order[A]
type PartialOrder[A] = PartialOrder[A]
type Rig[A] = Rig[A]
type Ring[A] = Ring[A]
type Rng[A] = Rng[A]
type Semigroup[A] = Semigroup[A]
type Semiring[A] = Semiring[A]

Value members

Concrete fields

val AbGroup: CommutativeGroup
val AdditiveAbGroup: AdditiveCommutativeGroup
val AdditiveCMonoid: AdditiveCommutativeMonoid
val AdditiveCSemigroup: AdditiveCommutativeSemigroup
val AdditiveGroup: AdditiveGroup
val AdditiveMonoid: AdditiveMonoid
val AdditiveSemigroup: AdditiveSemigroup
val AlgebraField: Field
val Bool: Bool
val CMonoid: CommutativeMonoid
val CRig: CommutativeRig
val CRing: CommutativeRing
val CRng: CommutativeRng
val CSemigroup: CommutativeSemigroup
val CSemiring: CommutativeSemiring
val Eq: Eq
val Group: Group
val Monoid: Monoid
val MultiplicativeAbGroup: MultiplicativeCommutativeGroup
val MultiplicativeCMonoid: MultiplicativeCommutativeMonoid
val MultiplicativeCSemigroup: MultiplicativeCommutativeSemigroup
val MultiplicativeGroup: MultiplicativeGroup
val MultiplicativeMonoid: MultiplicativeMonoid
val MultiplicativeSemigroup: MultiplicativeSemigroup
val Order: Order
val PartialOrder: PartialOrder
val Rig: Rig
val Ring: Ring
val Rng: Rng
val Semigroup: Semigroup
val Semiring: Semiring