algebra

package
v0.0.0-...-9687f63 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package algebra implements various generic algebra types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs(x int) int

func GCD

func GCD(a, b int) int

GCD returns the greatest common divisor of a and b.

Types

type BuiltinField

type BuiltinField[T Fieldable] struct{}

BuiltinField implements a field over T using built-in arithmetic.

func (BuiltinField[T]) Add

func (BuiltinField[T]) Add(x, y T) T

Add returns x+y.

func (BuiltinField[T]) Identity

func (BuiltinField[T]) Identity() T

Identity returns 1.

func (BuiltinField[T]) Inv

func (BuiltinField[T]) Inv(x T) T

Inv returns 1/x. This panics if x is zero.

func (BuiltinField[T]) Mul

func (BuiltinField[T]) Mul(x, y T) T

Mul returns x*y.

func (BuiltinField[T]) Neg

func (BuiltinField[T]) Neg(x T) T

Neg returns -x.

func (BuiltinField[T]) Zero

func (BuiltinField[T]) Zero() T

Zero returns 0.

type BuiltinRing

type BuiltinRing[T Ringable] struct{}

BuiltinField implements a ring over T using built-in arithmetic.

func (BuiltinRing[T]) Add

func (BuiltinRing[T]) Add(x, y T) T

Add returns x+y.

func (BuiltinRing[T]) Identity

func (BuiltinRing[T]) Identity() T

Identity returns 1.

func (BuiltinRing[T]) Mul

func (BuiltinRing[T]) Mul(x, y T) T

Mul returns x*y.

func (BuiltinRing[T]) Neg

func (BuiltinRing[T]) Neg(x T) T

Neg returns -x.

func (BuiltinRing[T]) Zero

func (BuiltinRing[T]) Zero() T

Zero returns 0.

type Cmplx

type Cmplx = BuiltinField[complex128]

Cmplx implements ℂ using complex128 (not with the Complex type).

type Complex

type Complex[T any, R Ring[T]] struct{}

Complex implements complex numbers generically as a 2-dimensional algebra over any ring.

func (Complex[T, R]) Add

func (Complex[T, R]) Add(z, w [2]T) [2]T

Add returns z+w.

func (Complex[T, R]) Conjugate

func (Complex[T, R]) Conjugate(z [2]T) [2]T

Conjugate returns the conjugate of z.

func (Complex[T, R]) Dot

func (Complex[T, R]) Dot(z, w [2]T) T

Dot returns Re(z)*Re(w) + Im(z)*Im(w).

func (Complex[T, R]) Format

func (Complex[T, R]) Format(z [2]T) string

Format formats a complex number.

func (Complex[T, R]) Identity

func (Complex[T, R]) Identity() [2]T

Identity returns 1 + 0𝕚.

func (Complex[T, R]) Inv

func (c Complex[T, R]) Inv(z [2]T) [2]T

Inv returns 1/z, or panics if R is not a division ring or is zero.

func (Complex[T, R]) Mul

func (Complex[T, R]) Mul(z, w [2]T) [2]T

Mul returns z*w.

func (Complex[T, R]) Neg

func (Complex[T, R]) Neg(z [2]T) [2]T

Neg returns -z.

func (Complex[T, R]) ScalarMul

func (Complex[T, R]) ScalarMul(k T, z [2]T) [2]T

ScalarMul returns k*z.

func (Complex[T, R]) Zero

func (Complex[T, R]) Zero() [2]T

Zero returns 0 + 0𝕚.

type DivisionRing

type DivisionRing[T any] interface {
	Ring[T]
	Inv(T) T
}

DivisionRing is a Ring that also has multiplicative inverses (which can be expected to panic on zero). This is the same interface required for Field, but has a separate name to remind us that multiplication is not commutative. TODO: continue bothering with this distinction?

type Field

type Field[T any] DivisionRing[T]

Field is a commutative division ring. The interface is the same.

type Fieldable

type Fieldable interface {
	~float32 | ~float64 | ~complex64 | ~complex128
}

Fieldable matches types with built-in arithmetic that can support a field.

type GaussianInteger

type GaussianInteger = Complex[int, Integer]

GaussianInteger implements the integral domain ℤ[𝕚] using int.

type Integer

type Integer = BuiltinRing[int]

Integer implements ℤ using int.

type Matrix

type Matrix[T any, R Ring[T]] struct{}

Matrix implements matrix algebra on grid.Dense[T] given a ring R.

func (Matrix[T, R]) Add

func (Matrix[T, R]) Add(m, n grid.Dense[T]) grid.Dense[T]

Add adds two matrices.

func (Matrix[T, R]) IdentityMatrix

func (Matrix[T, R]) IdentityMatrix(n int) grid.Dense[T]

IdentityMatrix returns an identity matrix of size n.

func (Matrix[T, R]) Mul

func (M Matrix[T, R]) Mul(m, n grid.Dense[T]) grid.Dense[T]

Mul multiplies two matrices of compatible size (the width of m must equal the height of n)

func (Matrix[T, R]) Neg

func (Matrix[T, R]) Neg(m grid.Dense[T]) grid.Dense[T]

Neg returns the matrix with all entries negated.

func (Matrix[T, R]) ScalarMul

func (Matrix[T, R]) ScalarMul(k T, m grid.Dense[T]) grid.Dense[T]

ScalarMul returns the matrix with entries multiplied by k.

func (Matrix[T, R]) ZeroMatrix

func (Matrix[T, R]) ZeroMatrix(h, w int) grid.Dense[T]

ZeroMatrix returns a zero matrix of size h x w.

type QR2

type QR2 struct{}

QR2 implements numbers in the algebraic number field ℚ(√2) (the rationals adjoined with √2). ℚ(√2) = {(a + b√2)/c : a,b,c ∈ ℤ}.

func (QR2) Add

func (q QR2) Add(x, y [3]int) [3]int

Add returns x+y.

func (QR2) Canon

func (QR2) Canon(x [3]int) [3]int

Canon returns x in a canonical form (reduced fraction with positive denominator).

func (QR2) Float

func (QR2) Float(x [3]int) float64

Float returns a float64 representation of x.

func (QR2) Format

func (QR2) Format(x [3]int) string

func (QR2) Identity

func (QR2) Identity() [3]int

Identity returns the triple representing 1.

func (QR2) Inv

func (q QR2) Inv(x [3]int) [3]int

Inv returns 1/x.

func (QR2) Mul

func (q QR2) Mul(x, y [3]int) [3]int

Mul returns x*y.

func (QR2) Neg

func (QR2) Neg(x [3]int) [3]int

Neg returns -x.

func (QR2) Zero

func (QR2) Zero() [3]int

Zero returns the triple representing 0.

type Quaternion

type Quaternion[T any, R Ring[T]] struct{}

Quaternion implements quaternions generically as an algebra over some other ring. Traditional quaternions (ℍ) use ℝ.

func (Quaternion[T, R]) Add

func (Quaternion[T, R]) Add(x, y [4]T) [4]T

Add returns x+y.

func (Quaternion[T, R]) Conjugate

func (Quaternion[T, R]) Conjugate(x [4]T) [4]T

Conjugate returns the quaternion conjugate. This is equal to the inverse for rotation quaternions (those with norm 1).

func (Quaternion[T, R]) Dot

func (Quaternion[T, R]) Dot(x, y [4]T) T

Dot returns the dot product of q with r (treating them as 4D vectors).

func (Quaternion[T, R]) Format

func (Quaternion[T, R]) Format(x [4]T) string

Format formats x into a string.

func (Quaternion[T, R]) Identity

func (Quaternion[T, R]) Identity() [4]T

Identity returns 1 + 0𝕚 + 0𝕛 + 0𝕜

func (Quaternion[T, R]) Inv

func (q Quaternion[T, R]) Inv(x [4]T) [4]T

Inv returns x⁻¹, or panics if R is not a division ring or x.x has no inverse.

func (Quaternion[T, R]) Mul

func (Quaternion[T, R]) Mul(x, y [4]T) [4]T

Mul returns the quaternion product qr.

func (Quaternion[T, R]) Neg

func (Quaternion[T, R]) Neg(x [4]T) [4]T

Neg returns -x.

func (Quaternion[T, R]) ScalarMul

func (Quaternion[T, R]) ScalarMul(k T, x [4]T) [4]T

ScalarMul returns k*x.

func (Quaternion[T, R]) Zero

func (Quaternion[T, R]) Zero() [4]T

Zero returns 0 + 0𝕚 + 0𝕛 + 0𝕜

type Real

type Real = BuiltinField[float64]

Real implements ℝ using float64.

type Ring

type Ring[T any] interface {
	Add(T, T) T
	Neg(T) T
	Zero() T
	Mul(T, T) T
	Identity() T
}

Ring describes an algebraic structure with addition, negation, and multiplication. Rings also have a zero element and an identity element.

type Ringable

type Ringable interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | Fieldable
}

Ringable matches types with built-in arithmetic that can support a ring.

type Vector

type Vector[T any, R Ring[T]] struct{}

Vector implements a vector space for vectors of type []T and operations from any ring R.

func (Vector[T, R]) Add

func (Vector[T, R]) Add(v, w []T) []T

Add returns the vector sum v+w.

func (Vector[T, R]) Dot

func (Vector[T, R]) Dot(v, w []T) T

Dot returns the dot product (also known as inner product) v.w.

func (Vector[T, R]) Neg

func (Vector[T, R]) Neg(v []T) []T

Neg returns -v (the vector pointing in the opposite direction).

func (Vector[T, R]) ScalarMul

func (Vector[T, R]) ScalarMul(k T, v []T) []T

ScalarMul returns kv.

type VectorSpace

type VectorSpace[V, K any] interface {
	Add(V, V) V
	Neg(V) V
	ScalarMul(K, V) V
	Dot(V, V) K
}

VectorSpace describes a vector space (supports addition, scalar multiplication, and dot product).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL