rational

package
v0.0.2-0...-db6250e Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2020 License: CC0-1.0, CC0-1.0 Imports: 16 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AreEqual

func AreEqual(x *Element, y *Element) bool

AreEqual returns true iff x equals y.

func Cmp

func Cmp(x *Element, y *Element) int

Cmp returns -1 if x < y, 0 if x == y, and +1 if x > y.

func SetBigRat

func SetBigRat(q *big.Rat, r *Element) *big.Rat

SetBigRat sets the given *big.Rat q to the value of the given rational r. Returns the *big.Rat q.

Types

type Element

type Element struct {
	// contains filtered or unexported fields
}

Element is an element of the field of rational numbers.

func Add

func Add(x *Element, y *Element) *Element

Add returns x + y.

func AddInt64

func AddInt64(x int64, y *Element) *Element

AddInt64 returns x + y.

func AddInteger

func AddInteger(x *integer.Element, y *Element) *Element

AddInteger returns x + y.

func CopySlice

func CopySlice(S []*Element) []*Element

CopySlice returns a copy of the slice S. The capacity will be preserved.

func Divide

func Divide(x *Element, y *Element) (*Element, error)

Divide returns the quotient x / y. It will return an error if y is zero.

func DotProduct

func DotProduct(S1 []*Element, S2 []*Element) *Element

DotProduct returns the dot-product of the slices S1 and S2. That is, it returns S1[0] * S2[0] + ... + S1[k] * S2[k], where k+1 is the minimum of len(S1) and len(S2). The dot-product of two empty slices is the zero element.

func FromBigInt

func FromBigInt(n *big.Int) *Element

FromBigInt returns n as a rational.

func FromBigIntPair

func FromBigIntPair(a *big.Int, b *big.Int) (*Element, error)

FromBigIntPair returns a/b as a rational. It will return an error if b is zero.

func FromBigRat

func FromBigRat(q *big.Rat) *Element

FromBigRat returns a *Element with value q.

func FromInt

func FromInt(n int) *Element

FromInt returns n as a rational.

func FromInt32

func FromInt32(n int32) *Element

FromInt32 returns n as a rational.

func FromInt32Pair

func FromInt32Pair(a int32, b int32) (*Element, error)

FromInt32Pair returns a/b as a rational. It will return an error if b is zero.

func FromInt64

func FromInt64(n int64) *Element

FromInt64 returns a *Element with value n.

func FromInt64Pair

func FromInt64Pair(a int64, b int64) (*Element, error)

FromInt64Pair returns a/b as a rational. It will return an error if b is zero.

func FromIntPair

func FromIntPair(a int, b int) (*Element, error)

FromIntPair returns a/b as a rational. It will return an error if b is zero.

func FromInteger

func FromInteger(n *integer.Element) *Element

FromInteger returns n as a rational.

func FromIntegerPair

func FromIntegerPair(a *integer.Element, b *integer.Element) (*Element, error)

FromIntegerPair returns a/b as a rational. It will return an error if b is zero.

func FromString

func FromString(s string) (*Element, error)

FromString returns the rational represented by the string s.

func FromUint64

func FromUint64(n uint64) *Element

FromUint64 returns a *Element with value n.

func FromUint64Pair

func FromUint64Pair(a uint64, b uint64) (*Element, error)

FromUint64Pair returns a/b as a rational. It will return an error if b is zero.

func GobDecode

func GobDecode(dec *gob.Decoder) (*Element, error)

GobDecode reads the next value from the given gob.Decoder and decodes it as a rational.

func GobDecodeSlice

func GobDecodeSlice(dec *gob.Decoder) ([]*Element, error)

GobDecodeSlice reads the next value from the given gob.Decoder and decodes it as a slice of rationals.

func Inverse

func Inverse(q *Element) (*Element, error)

Inverse returns 1/q. It will return an error if q is zero.

func Max

func Max(S ...*Element) (*Element, error)

Max returns the maximum element in the slice S. If S is empty then an error is returned.

func MaxAndIndex

func MaxAndIndex(S []*Element) (*Element, int, error)

MaxAndIndex returns the maximum element in the slice S, along with the index of the first occurrence of that element in S. If S is empty then an error is returned.

func MaxOfPair

func MaxOfPair(x *Element, y *Element) *Element

MaxOfPair returns the maximum element of x and y.

func Min

func Min(S ...*Element) (*Element, error)

Min returns the minimum element in the slice S. If S is empty then an error is returned.

func MinAndIndex

func MinAndIndex(S []*Element) (*Element, int, error)

MinAndIndex returns the minimum element in the slice S, along with the index of the first occurrence of that element in S. If S is empty then an error is returned.

func MinOfPair

func MinOfPair(x *Element, y *Element) *Element

MinOfPair returns the minimum element of x and y.

func Multiply

func Multiply(x *Element, y *Element) *Element

Multiply returns the product x * y.

func MultiplyByInt64ThenAdd

func MultiplyByInt64ThenAdd(a int64, b *Element, c *Element) *Element

MultiplyByInt64ThenAdd returns the value a * b + c.

func MultiplyByIntegerThenAdd

func MultiplyByIntegerThenAdd(a *integer.Element, b *Element, c *Element) *Element

MultiplyByIntegerThenAdd returns the value a * b + c.

func MultiplyMultiplyThenAdd

func MultiplyMultiplyThenAdd(a *Element, b *Element, c *Element, d *Element) *Element

MultiplyMultiplyThenAdd returns the value a * b + c * d.

func MultiplyMultiplyThenSubtract

func MultiplyMultiplyThenSubtract(a *Element, b *Element, c *Element, d *Element) *Element

MultiplyMultiplyThenSubtract returns the value a * b - c * d.

func MultiplyThenAdd

func MultiplyThenAdd(a *Element, b *Element, c *Element) *Element

MultiplyThenAdd returns the value a * b + c.

func MultiplyThenSubtract

func MultiplyThenSubtract(a *Element, b *Element, c *Element) *Element

MultiplyThenSubtract returns the value a * b - c.

func Negate

func Negate(q *Element) *Element

Negate returns -q.

func One

func One() *Element

One returns 1.

func Power

func Power(q *Element, k *integer.Element) (*Element, error)

Power returns q^k.

func PowerInt64

func PowerInt64(q *Element, k int64) (*Element, error)

PowerInt64 returns q^k.

func Product

func Product(S ...*Element) *Element

Product returns the product of the elements in the slice S. The product of the empty slice is one.

func SliceFromIntPairs

func SliceFromIntPairs(S []int) ([]*Element, error)

SliceFromIntPairs returns a slice T generated by S, where 2 * len(T) = len(S) and T[i] = S[2 * i] / S[2 * i + 1].

func SliceFromIntSlice

func SliceFromIntSlice(S []int) []*Element

SliceFromIntSlice returns a slice of rationals corresponding to the integers in S.

func Sort

func Sort(S []*Element) []*Element

Sort non-destructively sorts the given slice of integers. The sorted slice is returned.

func Subtract

func Subtract(x *Element, y *Element) *Element

Subtract returns x - y.

func Sum

func Sum(S ...*Element) *Element

Sum returns the sum of the elements in the slice S. The sum of the empty slice is the zero element.

func ToElement

func ToElement(x object.Element) (*Element, error)

ToElement attempts to convert the given object.Element to a rational number. If necessary this will be done by first attempting to convert x to an integer, and then from an integer to a rational.

func Zero

func Zero() *Element

Zero returns 0.

func (*Element) Abs

func (q *Element) Abs() *Element

Abs returns the absolute value of q.

func (*Element) BigInt

func (q *Element) BigInt() (*big.Int, error)

BigInt returns the rational as a *big.Int, or an error if this is not possible.

func (*Element) BigRat

func (q *Element) BigRat() *big.Rat

BigRat returns the rational as a *big.Rat.

func (*Element) Ceiling

func (q *Element) Ceiling() *integer.Element

Ceiling returns the smallest integer greater than or equal to q

func (*Element) Decrement

func (q *Element) Decrement() *Element

Decrement returns q - 1.

func (*Element) Denominator

func (q *Element) Denominator() *integer.Element

Denominator returns the denominator of q as an integer.

func (*Element) Floor

func (q *Element) Floor() *integer.Element

Floor returns the biggest integer less than or equal to q

func (*Element) GobDecode

func (q *Element) GobDecode(buf []byte) error

GobDecode implements the gob.GobDecoder interface. Important: Take great care that you are decoding into a new *Element; the safe way to do this is to use the GobDecode(dec *gob.Decode) function.

func (*Element) GobEncode

func (q *Element) GobEncode() ([]byte, error)

GobEncode implements the gob.GobEncoder interface.

func (*Element) Hash

func (q *Element) Hash() hash.Value

Hash returns a hash value for the given rational number.

func (*Element) Increment

func (q *Element) Increment() *Element

Increment returns q + 1.

func (*Element) Int64

func (q *Element) Int64() (int64, error)

Int64 returns the rational as an int64, or an error if this is not possible.

func (*Element) Inverse

func (q *Element) Inverse() (*Element, error)

Inverse returns 1/q. It will return an error if q is zero.

func (*Element) IsEqualTo

func (q *Element) IsEqualTo(r *Element) bool

IsEqualTo returns true iff q = r.

func (*Element) IsEqualToInt64

func (q *Element) IsEqualToInt64(r int64) bool

IsEqualToInt64 returns true iff q = r.

func (*Element) IsEqualToInteger

func (q *Element) IsEqualToInteger(r *integer.Element) bool

IsEqualToInteger returns true iff q = r.

func (*Element) IsEqualToUint64

func (q *Element) IsEqualToUint64(r uint64) bool

IsEqualToUint64 returns true iff q = r.

func (*Element) IsGreaterThan

func (q *Element) IsGreaterThan(r *Element) bool

IsGreaterThan returns true iff q > r.

func (*Element) IsGreaterThanInt64

func (q *Element) IsGreaterThanInt64(r int64) bool

IsGreaterThanInt64 returns true iff q > r.

func (*Element) IsGreaterThanInteger

func (q *Element) IsGreaterThanInteger(r *integer.Element) bool

IsGreaterThanInteger returns true iff q > r.

func (*Element) IsGreaterThanOrEqualTo

func (q *Element) IsGreaterThanOrEqualTo(r *Element) bool

IsGreaterThanOrEqualTo returns true iff q >= r.

func (*Element) IsGreaterThanOrEqualToInt64

func (q *Element) IsGreaterThanOrEqualToInt64(r int64) bool

IsGreaterThanOrEqualToInt64 returns true iff q >= r.

func (*Element) IsGreaterThanOrEqualToInteger

func (q *Element) IsGreaterThanOrEqualToInteger(r *integer.Element) bool

IsGreaterThanOrEqualToInteger returns true iff q >= r.

func (*Element) IsInt64

func (q *Element) IsInt64() bool

IsInt64 returns true iff the rational can be expressed as an int64.

func (*Element) IsIntegral

func (q *Element) IsIntegral() bool

IsIntegral returns true iff q can be represented by an integer.

func (*Element) IsLessThan

func (q *Element) IsLessThan(r *Element) bool

IsLessThan returns true iff q < r.

func (*Element) IsLessThanInt64

func (q *Element) IsLessThanInt64(r int64) bool

IsLessThanInt64 returns true iff q < r.

func (*Element) IsLessThanInteger

func (q *Element) IsLessThanInteger(r *integer.Element) bool

IsLessThanInteger returns true iff q < r.

func (*Element) IsLessThanOrEqualTo

func (q *Element) IsLessThanOrEqualTo(r *Element) bool

IsLessThanOrEqualTo returns true iff q <= r.

func (*Element) IsLessThanOrEqualToInt64

func (q *Element) IsLessThanOrEqualToInt64(r int64) bool

IsLessThanOrEqualToInt64 returns true iff q <= r.

func (*Element) IsLessThanOrEqualToInteger

func (q *Element) IsLessThanOrEqualToInteger(r *integer.Element) bool

IsLessThanOrEqualToInteger returns true iff q <= r.

func (*Element) IsNegative

func (q *Element) IsNegative() bool

IsNegative returns true iff q < 0.

func (*Element) IsOne

func (q *Element) IsOne() bool

IsOne returns true iff q is one.

func (*Element) IsPositive

func (q *Element) IsPositive() bool

IsPositive returns true iff q > 0.

func (*Element) IsUint64

func (q *Element) IsUint64() bool

IsUint64 returns true iff the rational can be expressed as a uint64.

func (*Element) IsZero

func (q *Element) IsZero() bool

IsZero returns true iff q is zero.

func (*Element) Negate

func (q *Element) Negate() *Element

Negate return -q.

func (*Element) Numerator

func (q *Element) Numerator() *integer.Element

Numerator returns the numerator of q as an integer.

func (*Element) Parent

func (q *Element) Parent() object.Parent

Parent returns the field of rational numbers as a object.Parent. This can safely be coerced to type Parent.

func (*Element) Power

func (q *Element) Power(k *integer.Element) (*Element, error)

Power returns q^k.

func (*Element) PowerInt64

func (q *Element) PowerInt64(k int64) (*Element, error)

PowerInt64 returns q^k.

func (*Element) ScalarMultiplyByInt64

func (q *Element) ScalarMultiplyByInt64(a int64) *Element

ScalarMultiplyByInt64 returns the product a * q.

func (*Element) ScalarMultiplyByInteger

func (q *Element) ScalarMultiplyByInteger(a *integer.Element) *Element

ScalarMultiplyByInteger returns the product a * q.

func (*Element) ScalarMultiplyByRational

func (q *Element) ScalarMultiplyByRational(a *Element) *Element

ScalarMultiplyByRational returns the product a * q.

func (*Element) ScalarMultiplyByUint64

func (q *Element) ScalarMultiplyByUint64(a uint64) *Element

ScalarMultiplyByUint64 returns the product a * q.

func (*Element) Sign

func (q *Element) Sign() int

Sign returns the sign of q, i.e. -1 if q < 0; 0 if n == 0; +1 if q > 0.

func (*Element) String

func (q *Element) String() string

String returns a string representation of the given rational.

func (*Element) ToInteger

func (q *Element) ToInteger() (*integer.Element, error)

ToInteger return the rational as an integer, or an error if this is not possible.

func (*Element) Uint64

func (q *Element) Uint64() (uint64, error)

Uint64 returns the rational as a uint64, or an error if this is not possible.

type Parent

type Parent struct{}

Parent represents the (unique) field of rational numbers.

func Field

func Field() Parent

Field returns the (unique) field of rational numbers

func (Parent) Add

Add returns x + y.

func (Parent) AreEqual

func (R Parent) AreEqual(x object.Element, y object.Element) (bool, error)

AreEqual returns true iff x equals y.

func (Parent) Cmp

func (R Parent) Cmp(x object.Element, y object.Element) (int, error)

Cmp returns -1 if x < y, 0 if x == y, and +1 if x > y.

func (Parent) Contains

func (R Parent) Contains(x object.Element) bool

Contains returns true iff x is a rational number, or can naturally be regarded as a rational number.

func (Parent) Divide

func (R Parent) Divide(x object.Element, y object.Element) (object.Element, error)

Divide returns the quotient x / y. Returns a errors.DivisionByZero error if y is zero.

func (Parent) DotProduct

func (R Parent) DotProduct(S1 []object.Element, S2 []object.Element) (object.Element, error)

DotProduct returns the dot-product of the slices S1 and S2. That is, it returns S1[0] * S2[0] + ... + S1[k] * S2[k], where k+1 is the minimum of len(S1) and len(S2). The dot-product of two empty slices is the zero element.

func (Parent) FromInteger

func (R Parent) FromInteger(n *integer.Element) object.Element

FromInteger returns n as rational number.

func (Parent) FromRational

func (R Parent) FromRational(q *Element) (object.Element, error)

FromRational returns q.

func (Parent) Inverse

func (R Parent) Inverse(q object.Element) (object.Element, error)

Inverse returns 1/q. Returns an errors.DivisionByZero error if q is zero.

func (Parent) IsOne

func (R Parent) IsOne(x object.Element) (bool, error)

IsOne returns true iff x is the rational number 1.

func (Parent) IsUnit

func (R Parent) IsUnit(q object.Element) (bool, object.Element, error)

IsUnit returns true iff q is a multiplicative unit (i.e. q is non-zero). If true, also returns the inverse element 1/q.

func (Parent) IsZero

func (R Parent) IsZero(x object.Element) (bool, error)

IsZero returns true iff x is the rational number 0.

func (Parent) Max

func (R Parent) Max(S ...object.Element) (object.Element, error)

Max returns the maximum element in the slice S. If S is empty then an error is returned.

func (Parent) MaxAndIndex

func (R Parent) MaxAndIndex(S []object.Element) (object.Element, int, error)

MaxAndIndex returns the maximum element in the slice S, along with the index of the first occurrence of that element in S. If S is empty then an error is returned.

func (Parent) Min

func (R Parent) Min(S ...object.Element) (object.Element, error)

Min returns the maximum element in the slice S. If S is empty then an error is returned.

func (Parent) MinAndIndex

func (R Parent) MinAndIndex(S []object.Element) (object.Element, int, error)

MinAndIndex returns the minimum element in the slice S, along with the index of the first occurrence of that element in S. If S is empty then an error is returned.

func (Parent) Multiply

func (R Parent) Multiply(x object.Element, y object.Element) (object.Element, error)

Multiply returns the product x * y.

func (Parent) Negate

func (R Parent) Negate(q object.Element) (object.Element, error)

Negate returns -q.

func (Parent) One

func (R Parent) One() object.Element

One returns 1.

func (Parent) Power

func (R Parent) Power(q object.Element, k *integer.Element) (object.Element, error)

Power returns q^k.

func (Parent) Product

func (R Parent) Product(S ...object.Element) (object.Element, error)

Product returns the product of the elements in the slice S. The product of the empty slice is one.

func (Parent) ScalarMultiplyByInteger

func (R Parent) ScalarMultiplyByInteger(n *integer.Element, x object.Element) (object.Element, error)

ScalarMultiplyByInteger returns nx, where this is defined to be x + ... + x (n times) if n is positive, and -x - ... - x (|n| times) if n is negative.

func (Parent) Sort

func (R Parent) Sort(S []object.Element) ([]object.Element, error)

Sort non-destructively sorts the given slice of integers. The sorted slice is returned.

func (Parent) String

func (R Parent) String() string

String returns a string representation of the field of rational numbers.

func (Parent) Subtract

func (R Parent) Subtract(x object.Element, y object.Element) (object.Element, error)

Subtract returns x - y.

func (Parent) Sum

func (R Parent) Sum(S ...object.Element) (object.Element, error)

Sum returns the sum of the elements in the slice S. The sum of the empty slice is the zero element.

func (Parent) ToElement

func (R Parent) ToElement(x object.Element) (object.Element, error)

ToElement returns x as an element of this parent, or an error if x cannot naturally be regarded as an element of this parent.

func (Parent) Zero

func (R Parent) Zero() object.Element

Zero returns 0.

type Slice

type Slice []*Element

Slice wraps an []*Element, implementing slice.Interface.

func (Slice) Entry

func (S Slice) Entry(i int) object.Element

Entry returns the i-th element in the slice. This will panic if i is out of range.

func (Slice) Hash

func (S Slice) Hash() hash.Value

Hash returns a hash value for this slice.

func (Slice) Len

func (S Slice) Len() int

Len returns the length of the slice.

func (Slice) Slice

func (S Slice) Slice(k int, m int) slice.Interface

Slice returns a subslice starting at index k and of length m - k. The returned subslice will be of the same underlying type. This will panic if the arguments are out of range.

type SortableSlice

type SortableSlice []*Element

SortableSlice implements the sort.Interface interface.

func (SortableSlice) Len

func (S SortableSlice) Len() int

Len is the number of elements in the slice.

func (SortableSlice) Less

func (S SortableSlice) Less(i int, j int) bool

Less reports whether the element with index i should sort before the element with index j.

func (SortableSlice) Sort

func (S SortableSlice) Sort()

Sort sorts the slice in place.

func (SortableSlice) Swap

func (S SortableSlice) Swap(i int, j int)

Swap swaps the elements with indexes i and j.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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