mathutil

package module
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2023 License: BSD-3-Clause Imports: 5 Imported by: 123

README

This is a goinstall-able mirror of modified code already published at:
http://git.nic.cz/redmine/projects/gornd/repository

Packages in this repository:

Install: $ go get modernc.org/mathutil
Godocs:  http://godoc.org/modernc.org/mathutil

Install: $ go get modernc.org/mathutil/mersenne
Godocs:  http://godoc.org/modernc.org/mathutil/mersenne

Documentation

Overview

Package mathutil provides utilities supplementing the standard 'math' and 'math/rand' packages.

Release history and compatibility issues

2020-12-20 v1.2.1 fixes MulOverflowInt64.

2020-12-19 Added {Add,Sub,Mul}OverflowInt{8,16,32,64}

2018-10-21 Added BinaryLog

2018-04-25: New functions for determining Max/Min of nullable values. Ex:

func MaxPtr(a, b *int) *int {
func MinPtr(a, b *int) *int {
func MaxBytePtr(a, b *byte) *byte {
func MinBytePtr(a, b *byte) *byte {
...

2017-10-14: New variadic functions for Max/Min. Ex:

func MaxVal(val int, vals ...int) int {
func MinVal(val int, vals ...int) int {
func MaxByteVal(val byte, vals ...byte) byte {
func MinByteVal(val byte, vals ...byte) byte {
...

2016-10-10: New functions QuadPolyDiscriminant and QuadPolyFactors.

2013-12-13: The following functions have been REMOVED

func Uint64ToBigInt(n uint64) *big.Int
func Uint64FromBigInt(n *big.Int) (uint64, bool)

2013-05-13: The following functions are now DEPRECATED

func Uint64ToBigInt(n uint64) *big.Int
func Uint64FromBigInt(n *big.Int) (uint64, bool)

These functions will be REMOVED with Go release 1.1+1.

2013-01-21: The following functions have been REMOVED

func MaxInt() int
func MinInt() int
func MaxUint() uint
func UintPtrBits() int

They are now replaced by untyped constants

MaxInt
MinInt
MaxUint
UintPtrBits

Additionally one more untyped constant was added

IntBits

This change breaks any existing code depending on the above removed functions. They should have not been published in the first place, that was unfortunate. Instead, defining such architecture and/or implementation specific integer limits and bit widths as untyped constants improves performance and allows for static dead code elimination if it depends on these values. Thanks to minux for pointing it out in the mail list (https://groups.google.com/d/msg/golang-nuts/tlPpLW6aJw8/NT3mpToH-a4J).

2012-12-12: The following functions will be DEPRECATED with Go release 1.0.3+1 and REMOVED with Go release 1.0.3+2, b/c of http://code.google.com/p/go/source/detail?r=954a79ee3ea8

func Uint64ToBigInt(n uint64) *big.Int
func Uint64FromBigInt(n *big.Int) (uint64, bool)

Index

Examples

Constants

View Source
const (
	MaxInt      = 1<<(IntBits-1) - 1
	MinInt      = -MaxInt - 1
	MaxUint     = 1<<IntBits - 1
	IntBits     = 1 << (^uint(0)>>32&1 + ^uint(0)>>16&1 + ^uint(0)>>8&1 + 3)
	UintPtrBits = 1 << (^uintptr(0)>>32&1 + ^uintptr(0)>>16&1 + ^uintptr(0)>>8&1 + 3)
)

Architecture and/or implementation specific integer limits and bit widths.

Variables

View Source
var (
	// MaxInt128 represents the maximun Int128 value as big.Int
	MaxInt128 *big.Int
	// MinInt128 represents the minimun Int128 value as big.Int
	MinInt128 *big.Int
	// MaxUint128 represents the maximun Uint128 value as big.Int
	MaxUint128 *big.Int
)

Functions

func AddOverflowInt16 added in v1.2.0

func AddOverflowInt16(a, b int16) (r int16, ovf bool)

AddOverflowInt16 returns a + b and an indication whether the addition overflowed the int16 range.

func AddOverflowInt32 added in v1.2.0

func AddOverflowInt32(a, b int32) (r int32, ovf bool)

AddOverflowInt32 returns a + b and an indication whether the addition overflowed the int32 range.

func AddOverflowInt64 added in v1.2.0

func AddOverflowInt64(a, b int64) (r int64, ovf bool)

AddOverflowInt64 returns a + b and an indication whether the addition overflowed the int64 range.

func AddOverflowInt8 added in v1.2.0

func AddOverflowInt8(a, b int8) (r int8, ovf bool)

AddOverflowInt8 returns a + b and an indication whether the addition overflowed the int8 range.

func AddUint128_64

func AddUint128_64(a, b uint64) (hi uint64, lo uint64)

AddUint128_64 returns the uint128 sum of uint64 a and b.

func BinaryLog

func BinaryLog(n *big.Int, mantissaBits int) (characteristic int, mantissa *big.Int)

BinaryLog computes the binary logarithm of n. The result consists of a characteristic and a mantissa having precision mantissaBits. The value of the binary logarithm is

characteristic + mantissa*(2^-mantissaBits)

BinaryLog panics for n <= 0 or mantissaBits < 0.

Example
const mantBits = 257
x, _ := big.NewInt(0).SetString("89940344608680314083397671686667731393131665861770496634981932531495305005604", 10)
c, m := BinaryLog(x, mantBits)
f := big.NewFloat(0).SetPrec(mantBits).SetInt(m)
f = f.SetMantExp(f, -mantBits)
f.Add(f, big.NewFloat(float64(c)))
f.Quo(f, big.NewFloat(4))
fmt.Printf("%.75f", f)
Output:

63.908875905794799149011030723455843229394283193466612998787786375106246936971

func BitLen

func BitLen(n int) int

BitLen returns the bit width of the non zero part of n.

func BitLenByte

func BitLenByte(n byte) int

BitLenByte returns the bit width of the non zero part of n.

func BitLenUint

func BitLenUint(n uint) int

BitLenUint returns the bit width of the non zero part of n.

func BitLenUint16

func BitLenUint16(n uint16) int

BitLenUint16 returns the bit width of the non zero part of n.

func BitLenUint32

func BitLenUint32(n uint32) int

BitLenUint32 returns the bit width of the non zero part of n.

func BitLenUint64

func BitLenUint64(n uint64) int

BitLenUint64 returns the bit width of the non zero part of n.

func BitLenUintptr

func BitLenUintptr(n uintptr) int

BitLenUintptr returns the bit width of the non zero part of n.

func CheckAddInt64 added in v1.1.0

func CheckAddInt64(a, b int64) (sum int64, gt bool)

CheckAddInt64 returns the a+b and an indicator that the result is greater than math.MaxInt64.

func CheckSubInt64 added in v1.1.0

func CheckSubInt64(a, b int64) (sum int64, lt bool)

CheckSubInt64 returns a-b and an indicator that the result is less than than math.MinInt64.

func Clamp

func Clamp(v, lo, hi int) int

Clamp returns a value restricted between lo and hi.

func ClampByte

func ClampByte(v, lo, hi byte) byte

ClampByte returns a value restricted between lo and hi.

func ClampInt16

func ClampInt16(v, lo, hi int16) int16

ClampInt16 returns a value restricted between lo and hi.

func ClampInt32

func ClampInt32(v, lo, hi int32) int32

ClampInt32 returns a value restricted between lo and hi.

func ClampInt64

func ClampInt64(v, lo, hi int64) int64

ClampInt64 returns a value restricted between lo and hi.

func ClampInt8

func ClampInt8(v, lo, hi int8) int8

ClampInt8 returns a value restricted between lo and hi.

func ClampUint16

func ClampUint16(v, lo, hi uint16) uint16

ClampUint16 returns a value restricted between lo and hi.

func ClampUint32

func ClampUint32(v, lo, hi uint32) uint32

ClampUint32 returns a value restricted between lo and hi.

func ClampUint64

func ClampUint64(v, lo, hi uint64) uint64

ClampUint64 returns a value restricted between lo and hi.

func Envelope

func Envelope(x float64, points []float64, approximation Approximation) float64

Envelope is an utility for defining simple curves using a small (usually) set of data points. Envelope returns a value defined by x, points and approximation. The value of x must be in [0,1) otherwise the result is undefined or the function may panic. Points are interpreted as dividing the [0,1) interval in len(points)-1 sections, so len(points) must be > 1 or the function may panic. According to the left and right points closing/adjacent to the section the resulting value is interpolated using the chosen approximation method. Unsupported values of approximation are silently interpreted as 'Linear'.

func GCDByte

func GCDByte(a, b byte) byte

GCDByte returns the greatest common divisor of a and b. Based on: http://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations

func GCDUint16

func GCDUint16(a, b uint16) uint16

GCDUint16 returns the greatest common divisor of a and b.

func GCDUint32

func GCDUint32(a, b uint32) uint32

GCDUint32 returns the greatest common divisor of a and b.

func GCDUint64

func GCDUint64(a, b uint64) uint64

GCDUint64 returns the greatest common divisor of a and b.

func ISqrt

func ISqrt(n uint32) (x uint32)

ISqrt returns floor(sqrt(n)). Typical run time is few hundreds of ns.

func IsPrime

func IsPrime(n uint32) bool

IsPrime returns true if n is prime. Typical run time is about 100 ns.

func IsPrimeUint16

func IsPrimeUint16(n uint16) bool

IsPrimeUint16 returns true if n is prime. Typical run time is few ns.

func IsPrimeUint64

func IsPrimeUint64(n uint64) bool

IsPrimeUint64 returns true if n is prime. Typical run time is few tens of µs.

SPRP bases: http://miller-rabin.appspot.com

func Log2Byte

func Log2Byte(n byte) int

Log2Byte returns log base 2 of n. It's the same as index of the highest bit set in n. For n == 0 -1 is returned.

func Log2Uint16

func Log2Uint16(n uint16) int

Log2Uint16 returns log base 2 of n. It's the same as index of the highest bit set in n. For n == 0 -1 is returned.

func Log2Uint32

func Log2Uint32(n uint32) int

Log2Uint32 returns log base 2 of n. It's the same as index of the highest bit set in n. For n == 0 -1 is returned.

func Log2Uint64

func Log2Uint64(n uint64) int

Log2Uint64 returns log base 2 of n. It's the same as index of the highest bit set in n. For n == 0 -1 is returned.

func Max

func Max(a, b int) int

Max returns the larger of a and b.

func MaxByte

func MaxByte(a, b byte) byte

MaxByte returns the larger of a and b.

func MaxBytePtr

func MaxBytePtr(a, b *byte) *byte

MaxBytePtr returns a pointer to the larger of a and b, or nil.

func MaxByteVal

func MaxByteVal(val byte, vals ...byte) byte

MaxByteVal returns the largest argument passed.

func MaxInt16

func MaxInt16(a, b int16) int16

MaxInt16 returns the larger of a and b.

func MaxInt16Ptr

func MaxInt16Ptr(a, b *int16) *int16

MaxInt16Ptr returns a pointer to the larger of a and b, or nil.

func MaxInt16Val

func MaxInt16Val(val int16, vals ...int16) int16

MaxInt16Val returns the largest argument passed.

func MaxInt32

func MaxInt32(a, b int32) int32

MaxInt32 returns the larger of a and b.

func MaxInt32Ptr

func MaxInt32Ptr(a, b *int32) *int32

MaxInt32Ptr returns a pointer to the larger of a and b, or nil.

func MaxInt32Val

func MaxInt32Val(val int32, vals ...int32) int32

MaxInt32Val returns the largest argument passed.

func MaxInt64

func MaxInt64(a, b int64) int64

MaxInt64 returns the larger of a and b.

func MaxInt64Ptr

func MaxInt64Ptr(a, b *int64) *int64

MaxInt64Ptr returns a pointer to the larger of a and b, or nil.

func MaxInt64Val

func MaxInt64Val(val int64, vals ...int64) int64

MaxInt64Val returns the largest argument passed.

func MaxInt8

func MaxInt8(a, b int8) int8

MaxInt8 returns the larger of a and b.

func MaxInt8Ptr

func MaxInt8Ptr(a, b *int8) *int8

MaxInt8Ptr returns a pointer to the larger of a and b, or nil.

func MaxInt8Val

func MaxInt8Val(val int8, vals ...int8) int8

MaxInt8Val returns the largest argument passed.

func MaxPtr

func MaxPtr(a, b *int) *int

MaxPtr returns a pointer to the larger of a and b, or nil.

func MaxUint16

func MaxUint16(a, b uint16) uint16

MaxUint16 returns the larger of a and b.

func MaxUint16Ptr

func MaxUint16Ptr(a, b *uint16) *uint16

MaxUint16Ptr returns a pointer to the larger of a and b, or nil.

func MaxUint16Val

func MaxUint16Val(val uint16, vals ...uint16) uint16

MaxUint16Val returns the largest argument passed.

func MaxUint32

func MaxUint32(a, b uint32) uint32

MaxUint32 returns the larger of a and b.

func MaxUint32Ptr

func MaxUint32Ptr(a, b *uint32) *uint32

MaxUint32Ptr returns a pointer to the larger of a and b, or nil.

func MaxUint32Val

func MaxUint32Val(val uint32, vals ...uint32) uint32

MaxUint32Val returns the largest argument passed.

func MaxUint64

func MaxUint64(a, b uint64) uint64

MaxUint64 returns the larger of a and b.

func MaxUint64Ptr

func MaxUint64Ptr(a, b *uint64) *uint64

MaxUint64Ptr returns a pointer to the larger of a and b, or nil.

func MaxUint64Val

func MaxUint64Val(val uint64, vals ...uint64) uint64

MaxUint64Val returns the largest argument passed.

func MaxVal

func MaxVal(val int, vals ...int) int

MaxVal returns the largest argument passed.

func Min

func Min(a, b int) int

Min returns the smaller of a and b.

func MinByte

func MinByte(a, b byte) byte

MinByte returns the smaller of a and b.

func MinBytePtr

func MinBytePtr(a, b *byte) *byte

MinBytePtr returns a pointer to the smaller of a and b, or nil.

func MinByteVal

func MinByteVal(val byte, vals ...byte) byte

MinByteVal returns the smallest argument passed.

func MinInt16

func MinInt16(a, b int16) int16

MinInt16 returns the smaller of a and b.

func MinInt16Ptr

func MinInt16Ptr(a, b *int16) *int16

MinInt16Ptr returns a pointer to the smaller of a and b, or nil.

func MinInt16Val

func MinInt16Val(val int16, vals ...int16) int16

MinInt16Val returns the smallest argument passed.

func MinInt32

func MinInt32(a, b int32) int32

MinInt32 returns the smaller of a and b.

func MinInt32Ptr

func MinInt32Ptr(a, b *int32) *int32

MinInt32Ptr returns a pointer to the smaller of a and b, or nil.

func MinInt32Val

func MinInt32Val(val int32, vals ...int32) int32

MinInt32Val returns the smallest argument passed.

func MinInt64

func MinInt64(a, b int64) int64

MinInt64 returns the smaller of a and b.

func MinInt64Ptr

func MinInt64Ptr(a, b *int64) *int64

MinInt64Ptr returns a pointer to the smaller of a and b, or nil.

func MinInt64Val

func MinInt64Val(val int64, vals ...int64) int64

MinInt64Val returns the smallest argument passed.

func MinInt8

func MinInt8(a, b int8) int8

MinInt8 returns the smaller of a and b.

func MinInt8Ptr

func MinInt8Ptr(a, b *int8) *int8

MinInt8Ptr returns a pointer to the smaller of a and b, or nil.

func MinInt8Val

func MinInt8Val(val int8, vals ...int8) int8

MinInt8Val returns the smallest argument passed.

func MinPtr

func MinPtr(a, b *int) *int

MinPtr returns a pointer to the smaller of a and b, or nil.

func MinUint16

func MinUint16(a, b uint16) uint16

MinUint16 returns the smaller of a and b.

func MinUint16Ptr

func MinUint16Ptr(a, b *uint16) *uint16

MinUint16Ptr returns a pointer to the smaller of a and b, or nil.

func MinUint16Val

func MinUint16Val(val uint16, vals ...uint16) uint16

MinUint16Val returns the smallest argument passed.

func MinUint32

func MinUint32(a, b uint32) uint32

MinUint32 returns the smaller of a and b.

func MinUint32Ptr

func MinUint32Ptr(a, b *uint32) *uint32

MinUint32Ptr returns a pointer to the smaller of a and b, or nil.

func MinUint32Val

func MinUint32Val(val uint32, vals ...uint32) uint32

MinUint32Val returns the smallest argument passed.

func MinUint64

func MinUint64(a, b uint64) uint64

MinUint64 returns the smaller of a and b.

func MinUint64Ptr

func MinUint64Ptr(a, b *uint64) *uint64

MinUint64Ptr returns a pointer to the smaller of a and b, or nil.

func MinUint64Val

func MinUint64Val(val uint64, vals ...uint64) uint64

MinUint64Val returns the smallest argument passed.

func MinVal

func MinVal(val int, vals ...int) int

MinVal returns the smallest argument passed.

func ModPowBigInt

func ModPowBigInt(b, e, m *big.Int) (r *big.Int)

ModPowBigInt computes (b^e)%m. Returns nil for e < 0. It panics for m == 0 || b == e == 0.

func ModPowByte

func ModPowByte(b, e, m byte) byte

ModPowByte computes (b^e)%m. It panics for m == 0 || b == e == 0.

See also: http://en.wikipedia.org/wiki/Modular_exponentiation#Right-to-left_binary_method

func ModPowUint16

func ModPowUint16(b, e, m uint16) uint16

ModPowUint16 computes (b^e)%m. It panics for m == 0 || b == e == 0.

func ModPowUint32

func ModPowUint32(b, e, m uint32) uint32

ModPowUint32 computes (b^e)%m. It panics for m == 0 || b == e == 0.

func ModPowUint64

func ModPowUint64(b, e, m uint64) (r uint64)

ModPowUint64 computes (b^e)%m. It panics for m == 0 || b == e == 0.

func MulOverflowInt16 added in v1.2.0

func MulOverflowInt16(a, b int16) (r int16, ovf bool)

MulOverflowInt16 returns a * b and an indication whether the product overflowed the int16 range.

func MulOverflowInt32 added in v1.2.0

func MulOverflowInt32(a, b int32) (r int32, ovf bool)

MulOverflowInt32 returns a * b and an indication whether the product overflowed the int32 range.

func MulOverflowInt64 added in v1.2.0

func MulOverflowInt64(a, b int64) (r int64, ovf bool)

MulOverflowInt64 returns a * b and an indication whether the product overflowed the int64 range.

func MulOverflowInt8 added in v1.2.0

func MulOverflowInt8(a, b int8) (r int8, ovf bool)

MulOverflowInt8 returns a * b and an indication whether the product overflowed the int8 range.

func MulUint128_64

func MulUint128_64(a, b uint64) (hi, lo uint64)

MulUint128_64 returns the uint128 bit product of uint64 a and b.

func NextPrime

func NextPrime(n uint32) (p uint32, ok bool)

NextPrime returns first prime > n and true if successful or an undefined value and false if there is no next prime in the uint32 limits. Typical run time is about 2 µs.

func NextPrimeUint16

func NextPrimeUint16(n uint16) (p uint16, ok bool)

NextPrimeUint16 returns first prime > n and true if successful or an undefined value and false if there is no next prime in the uint16 limits. Typical run time is few ns.

func NextPrimeUint64

func NextPrimeUint64(n uint64) (p uint64, ok bool)

NextPrimeUint64 returns first prime > n and true if successful or an undefined value and false if there is no next prime in the uint64 limits. Typical run time is in hundreds of µs.

func PermutationFirst

func PermutationFirst(data sort.Interface)

PermutationFirst generates the first permutation of data.

func PermutationNext

func PermutationNext(data sort.Interface) bool

PermutationNext generates the next permutation of data if possible and return true. Return false if there is no more permutation left. Based on the algorithm described here: http://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order

func PopCount

func PopCount(n int) int

PopCount returns population count of n (number of bits set in n).

func PopCountBigInt

func PopCountBigInt(n *big.Int) (r int)

PopCountBigInt returns population count of |n| (number of bits set in |n|).

func PopCountByte

func PopCountByte(n byte) int

PopCountByte returns population count of n (number of bits set in n).

func PopCountUint

func PopCountUint(n uint) int

PopCountUint returns population count of n (number of bits set in n).

func PopCountUint16

func PopCountUint16(n uint16) int

PopCountUint16 returns population count of n (number of bits set in n).

func PopCountUint32

func PopCountUint32(n uint32) int

PopCountUint32 returns population count of n (number of bits set in n).

func PopCountUint64

func PopCountUint64(n uint64) int

PopCountUint64 returns population count of n (number of bits set in n).

func PopCountUintptr

func PopCountUintptr(n uintptr) int

PopCountUintptr returns population count of n (number of bits set in n).

func PowerizeBigInt

func PowerizeBigInt(b, n *big.Int) (e uint32, p *big.Int)

PowerizeBigInt returns (e, p) such that e is the smallest number for which p == b^e is greater or equal n. For n < 0 or b < 2 (0, nil) is returned.

NOTE: Run time for large values of n (above about 2^1e6 ~= 1e300000) can be significant and/or unacceptabe. For any smaller values of n the function typically performs in sub second time. For "small" values of n (cca bellow 2^1e3 ~= 1e300) the same can be easily below 10 µs.

A special (and trivial) case of b == 2 is handled separately and performs much faster.

func PowerizeUint32BigInt

func PowerizeUint32BigInt(b uint32, n *big.Int) (e uint32, p *big.Int)

PowerizeUint32BigInt returns (e, p) such that e is the smallest number for which p == b^e is greater or equal n. For n < 0 or b < 2 (0, nil) is returned.

More info: see PowerizeBigInt.

func PrimorialProductsUint32

func PrimorialProductsUint32(lo, hi, max uint32) (r []uint32)

PrimorialProductsUint32 returns a slice of numbers in [lo, hi] which are a product of max 'max' primorials. The slice is not sorted.

See also: http://en.wikipedia.org/wiki/Primorial

func ProbablyPrimeBigInt

func ProbablyPrimeBigInt(n, a *big.Int) bool

ProbablyPrimeBigInt returns true if n is prime or n is a pseudoprime to base a. It implements the Miller-Rabin primality test for one specific value of 'a' and k == 1. See also ProbablyPrimeUint32.

func ProbablyPrimeBigInt_32

func ProbablyPrimeBigInt_32(n *big.Int, a uint32) bool

ProbablyPrimeBigInt_32 returns true if n is prime or n is a pseudoprime to base a. It implements the Miller-Rabin primality test for one specific value of 'a' and k == 1. See also ProbablyPrimeUint32.

func ProbablyPrimeUint32

func ProbablyPrimeUint32(n, a uint32) bool

ProbablyPrimeUint32 returns true if n is prime or n is a pseudoprime to base a. It implements the Miller-Rabin primality test for one specific value of 'a' and k == 1.

Wrt pseudocode shown at http://en.wikipedia.org/wiki/Miller-Rabin_primality_test#Algorithm_and_running_time

Input: n > 3, an odd integer to be tested for primality;
Input: k, a parameter that determines the accuracy of the test
Output: composite if n is composite, otherwise probably prime
write n − 1 as 2^s·d with d odd by factoring powers of 2 from n − 1
LOOP: repeat k times:
   pick a random integer a in the range [2, n − 2]
   x ← a^d mod n
   if x = 1 or x = n − 1 then do next LOOP
   for r = 1 .. s − 1
      x ← x^2 mod n
      if x = 1 then return composite
      if x = n − 1 then do next LOOP
   return composite
return probably prime

... this function behaves like passing 1 for 'k' and additionally a fixed/non-random 'a'. Otherwise it's the same algorithm.

See also: http://mathworld.wolfram.com/Rabin-MillerStrongPseudoprimeTest.html

func ProbablyPrimeUint64_32

func ProbablyPrimeUint64_32(n uint64, a uint32) bool

ProbablyPrimeUint64_32 returns true if n is prime or n is a pseudoprime to base a. It implements the Miller-Rabin primality test for one specific value of 'a' and k == 1. See also ProbablyPrimeUint32.

func QCmpUint32

func QCmpUint32(a, b, c, d uint32) int

QCmpUint32 compares a/b and c/d and returns:

-1 if a/b <  c/d
 0 if a/b == c/d
+1 if a/b >  c/d

func QScaleUint32

func QScaleUint32(b, c, d uint32) (a uint64)

QScaleUint32 returns a such that a/b >= c/d.

func QuadPolyDiscriminant

func QuadPolyDiscriminant(a, b, c int) (ds, d int, _ error)

QuadPolyDiscriminant returns the discriminant of a quadratic polynomial in one variable of the form a*x^2+b*x+c with integer coefficients a, b, c, or an error on overflow.

ds is the square of the discriminant. If |ds| is a square number, d is set to sqrt(|ds|), otherwise d is < 0.

func QuadPolyDiscriminantBig

func QuadPolyDiscriminantBig(a, b, c *big.Int) (ds, d *big.Int)

QuadPolyDiscriminantBig returns the discriminant of a quadratic polynomial in one variable of the form a*x^2+b*x+c with integer coefficients a, b, c.

ds is the square of the discriminant. If |ds| is a square number, d is set to sqrt(|ds|), otherwise d is nil.

func SqrtBig

func SqrtBig(n *big.Int) (x *big.Int)

SqrtBig returns floor(sqrt(n)). It panics on n < 0.

func SqrtUint64

func SqrtUint64(n uint64) (x uint64)

SqrtUint64 returns floor(sqrt(n)). Typical run time is about 0.5 µs.

func SubOverflowInt16 added in v1.2.0

func SubOverflowInt16(a, b int16) (r int16, ovf bool)

SubOverflowInt16 returns a - b and an indication whether the subtraction overflowed the int16 range.

func SubOverflowInt32 added in v1.2.0

func SubOverflowInt32(a, b int32) (r int32, ovf bool)

SubOverflowInt32 returns a - b and an indication whether the subtraction overflowed the int32 range.

func SubOverflowInt64 added in v1.2.0

func SubOverflowInt64(a, b int64) (r int64, ovf bool)

SubOverflowInt64 returns a - b and an indication whether the subtraction overflowed the int64 range.

func SubOverflowInt8 added in v1.2.0

func SubOverflowInt8(a, b int8) (r int8, ovf bool)

SubOverflowInt8 returns a - b and an indication whether the subtraction overflowed the int8 range.

func ToBase

func ToBase(n *big.Int, b int) []int

ToBase produces n in base b. For example

ToBase(2047, 22) -> [1, 5, 4]

1 * 22^0           1
5 * 22^1         110
4 * 22^2        1936
                ----
                2047

ToBase panics for bases < 2.

func UClamp

func UClamp(v, lo, hi uint) uint

UClamp returns a value restricted between lo and hi.

func UMax

func UMax(a, b uint) uint

UMax returns the larger of a and b.

func UMaxPtr

func UMaxPtr(a, b *uint) *uint

UMaxPtr returns a pointer to the larger of a and b, or nil.

func UMaxVal

func UMaxVal(val uint, vals ...uint) uint

UMaxVal returns the largest argument passed.

func UMin

func UMin(a, b uint) uint

UMin returns the smaller of a and b.

func UMinPtr

func UMinPtr(a, b *uint) *uint

UMinPtr returns a pointer to the smaller of a and b, or nil.

func UMinVal

func UMinVal(val uint, vals ...uint) uint

UMinVal returns the smallest argument passed.

func UintptrBits

func UintptrBits() int

UintptrBits returns the bit width of an uintptr at the executing machine.

Types

type Approximation

type Approximation int

Approximation type determines approximation methods used by e.g. Envelope.

const (
	Linear     Approximation // As named
	Sinusoidal               // Smooth for all derivations
)

Specific approximation method tags

type FC32

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

FC32 is a full cycle PRNG covering the 32 bit signed integer range. In contrast to full cycle generators shown at e.g. http://en.wikipedia.org/wiki/Full_cycle, this code doesn't produce values at constant delta (mod cycle length). The 32 bit limit is per this implementation, the algorithm used has no intrinsic limit on the cycle size. Properties include:

  • Adjustable limits on creation (hi, lo).
  • Positionable/randomly accessible (Pos, Seek).
  • Repeatable (deterministic).
  • Can run forward or backward (Next, Prev).
  • For a billion numbers cycle the Next/Prev PRN can be produced in cca 100-150ns. That's like 5-10 times slower compared to PRNs generated using the (non FC) rand package.

func NewFC32

func NewFC32(lo, hi int, hq bool) (r *FC32, err error)

NewFC32 returns a newly created FC32 adjusted for the closed interval [lo, hi] or an Error if any. If hq == true then trade some generation time for improved (pseudo)randomness.

func (*FC32) Cycle

func (r *FC32) Cycle() int64

Cycle reports the length of the inner FCPRNG cycle. Cycle is atmost the double (HQ: triple) of the generator period (hi - lo + 1).

func (*FC32) Next

func (r *FC32) Next() int

Next returns the first PRN after Pos.

func (*FC32) Pos

func (r *FC32) Pos() int64

Pos reports the current position within the inner cycle.

func (*FC32) Prev

func (r *FC32) Prev() int

Prev return the first PRN before Pos.

func (*FC32) Seed

func (r *FC32) Seed(seed int64)

Seed uses the provided seed value to initialize the generator to a deterministic state. A zero seed produces a "canonical" generator with worse randomness than for most non zero seeds. Still, the FC property holds for any seed value.

func (*FC32) Seek

func (r *FC32) Seek(pos int64)

Seek sets Pos to |pos| % Cycle.

type FCBig

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

FCBig is a full cycle PRNG covering ranges outside of the int32 limits. For more info see the FC32 docs. Next/Prev PRN on a 1e15 cycle can be produced in about 2 µsec.

func NewFCBig

func NewFCBig(lo, hi *big.Int, hq bool) (r *FCBig, err error)

NewFCBig returns a newly created FCBig adjusted for the closed interval [lo, hi] or an Error if any. If hq == true then trade some generation time for improved (pseudo)randomness.

func (*FCBig) Cycle

func (r *FCBig) Cycle() *big.Int

Cycle reports the length of the inner FCPRNG cycle. Cycle is atmost the double (HQ: triple) of the generator period (hi - lo + 1).

func (*FCBig) Next

func (r *FCBig) Next() *big.Int

Next returns the first PRN after Pos.

func (*FCBig) Pos

func (r *FCBig) Pos() *big.Int

Pos reports the current position within the inner cycle.

func (*FCBig) Prev

func (r *FCBig) Prev() *big.Int

Prev return the first PRN before Pos.

func (*FCBig) Seed

func (r *FCBig) Seed(seed int64)

Seed uses the provided seed value to initialize the generator to a deterministic state. A zero seed produces a "canonical" generator with worse randomness than for most non zero seeds. Still, the FC property holds for any seed value.

func (*FCBig) Seek

func (r *FCBig) Seek(pos *big.Int)

Seek sets Pos to |pos| % Cycle.

type FactorTerm

type FactorTerm struct {
	Prime uint32 // The divisor
	Power uint32 // Term == Prime^Power
}

FactorTerm is one term of an integer factorization.

type FactorTerms

type FactorTerms []FactorTerm

FactorTerms represent a factorization of an integer

func FactorInt

func FactorInt(n uint32) (f FactorTerms)

FactorInt returns prime factorization of n > 1 or nil otherwise. Resulting factors are ordered by Prime. Typical run time is few µs.

type Int128

type Int128 struct {
	Lo int64 // Bits 63..0.
	Hi int64 // Bits 127..64.
}

Int128 is an 128 bit signed integer.

func NewInt128FromFloat32 added in v1.2.2

func NewInt128FromFloat32(n float32) (r Int128)

NewInt128FromFloat32 returns a new Int128 value initialized to n. Result is not specified in n does not represent a number within the range of Int128 values.

func NewInt128FromFloat64 added in v1.2.2

func NewInt128FromFloat64(n float64) (r Int128)

NewInt128FromFloat64 returns a new Int128 value initialized to n. Result is not specified in n does not represent a number within the range of Int128 values.

func NewInt128FromInt64 added in v1.2.2

func NewInt128FromInt64(n int64) (r Int128)

NewInt128FromInt64 return a new Int128 value initialized to n.

func NewInt128FromUint64 added in v1.2.2

func NewInt128FromUint64(n uint64) (r Int128)

NewInt128FromUint64 return a new Int128 value initialized to n.

func (Int128) Add

func (x Int128) Add(y Int128) (r Int128, cy bool)

Add returns the sum of x and y and a carry indication.

func (Int128) BigInt

func (x Int128) BigInt() *big.Int

BigInt returns x in the form of a big.Int.

func (Int128) Cmp

func (x Int128) Cmp(y Int128) int

Cmp compares x and y and returns:

-1 if x <  y
 0 if x == y
+1 if x >  y

func (Int128) Neg

func (x Int128) Neg() (r Int128, ok bool)

Neg returns -x and an indication that x was not equal to MinInt128.

func (*Int128) SetBigInt

func (x *Int128) SetBigInt(y *big.Int) (r Int128, err error)

SetBigInt sets x to y, returns x and an error, if any.

func (*Int128) SetInt64

func (x *Int128) SetInt64(y int64) (r Int128)

SetInt64 sets x to y and returns x.

func (*Int128) SetUint64

func (x *Int128) SetUint64(y uint64) (r Int128)

SetUint64 sets x to y and returns x.

func (Int128) Sign

func (x Int128) Sign() int

Sign returns:

-1 if x <  0
 0 if x == 0
+1 if x >  0

func (Int128) String

func (x Int128) String() string

String implements fmt.Stringer()

type PolyFactor

type PolyFactor struct {
	P, Q int
}

PolyFactor describes an irreducible factor of a polynomial in one variable with integer coefficients P, Q of the form P*x+Q.

func QuadPolyFactors

func QuadPolyFactors(a, b, c int) (content int, primitivePart []PolyFactor, _ error)

QuadPolyFactors returns the content and the irreducible factors of the primitive part of a quadratic polynomial in one variable with integer coefficients a, b, c of the form a*x^2+b*x+c in integers, or an error on overflow.

If the factorization in integers does not exists, the return value is (0, nil, nil).

See also: https://en.wikipedia.org/wiki/Factorization_of_polynomials#Primitive_part.E2.80.93content_factorization

type PolyFactorBig

type PolyFactorBig struct {
	P, Q *big.Int
}

PolyFactorBig describes an irreducible factor of a polynomial in one variable with integer coefficients P, Q of the form P*x+Q.

func QuadPolyFactorsBig

func QuadPolyFactorsBig(a, b, c *big.Int) (content *big.Int, primitivePart []PolyFactorBig)

QuadPolyFactorsBig returns the content and the irreducible factors of the primitive part of a quadratic polynomial in one variable with integer coefficients a, b, c of the form a*x^2+b*x+c in integers.

If the factorization in integers does not exists, the return value is (nil, nil).

See also: https://en.wikipedia.org/wiki/Factorization_of_polynomials#Primitive_part.E2.80.93content_factorization

type Uint128 added in v1.2.2

type Uint128 struct {
	Lo uint64 // Bits 63..0.
	Hi uint64 // Bits 127..64.
}

Uint128 is an 128 bit unsigned integer.

func NewUint128FromFloat32 added in v1.2.2

func NewUint128FromFloat32(n float32) (r Uint128)

NewUint128FromFloat32 returns a new Uint128 value initialized to n. Result is not specified in n does not represent a number within the range of Uint128 values.

func NewUint128FromFloat64 added in v1.2.2

func NewUint128FromFloat64(n float64) (r Uint128)

NewUint128FromFloat64 returns a new Uint128 value initialized to n. Result is not specified in n does not represent a number within the range of Uint128 values.

func NewUint128FromInt64 added in v1.2.2

func NewUint128FromInt64(n int64) (r Uint128)

NewUint128FromInt64 return a new Uint128 value initialized to n.

func NewUint128FromUint64 added in v1.2.2

func NewUint128FromUint64(n uint64) (r Uint128)

NewUint128FromUint64 return a new Uint128 value initialized to n.

func (*Uint128) SetBigInt added in v1.2.2

func (x *Uint128) SetBigInt(y *big.Int) (r Uint128, err error)

SetBigInt sets x to y, returns x and an error, if any.

Directories

Path Synopsis
Package mersenne collects utilities related to Mersenne numbers[1] and/or some of their properties.
Package mersenne collects utilities related to Mersenne numbers[1] and/or some of their properties.

Jump to

Keyboard shortcuts

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