gomath

package module
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2022 License: BSD-3-Clause Imports: 5 Imported by: 1

README

gomath

Package gomath contains nifty math routines written in go

Using

import "github.com/keep94/gomath"

Documentation

Overview

Package gomath contains nifty math routines written in go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Factors added in v1.2.0

func Factors(n int64) []int64

Factors returns all the positive integers that divide n in ascending order. Factors panics if n < 1.

func Inverse

func Inverse(
	f func(float64) float64,
	y float64,
	lower float64,
	upper float64) float64

Inverse returns x such that f(x) = y. Caller must choose lower and upper so that x falls in between them. f must be monotone increasing or decreasing between lower and upper.

func Jagged added in v1.3.0

func Jagged(x float64) float64

Jagged is a function that is continuous everywhere but differentiable nowhere.

func ProductsSlice added in v1.2.0

func ProductsSlice(n, count int) []int64

ProductsSlice returns all the numbers in ascending order that can be written as a product of count positive integers each ranging between 1 and n. ProductsSlice panics if n < 1 or if count is negative.

Types

type BigIntCounter

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

BigIntCounter counts how many values on wrapped BigIntStream are less than or equal to a given value. Wrapped stream must be monotone increasing.

func NewBigIntCounter

func NewBigIntCounter(stream BigIntStream) *BigIntCounter

NewBigIntCounter returns a BigIntCounter that wraps stream.

func (*BigIntCounter) CountLE

func (b *BigIntCounter) CountLE(value *big.Int) int64

CountLE returns how many values on wrapped stream are less than or equal to value. CountLE panics if value is less than value in the previous call to CountLE.

type BigIntStream

type BigIntStream interface {
	// Next stores the next big.Int value at value and returns value.
	Next(value *big.Int) *big.Int
}

BigIntStream represents an infinite stream of big.Int values.

func BigIntSummation

func BigIntSummation(stream BigIntStream) BigIntStream

BigIntSummation generates the summation of the values in stream. For example if stream generates the primes, BigIntSummation would generate 2, 5, 10, 17, ...

func Fibonacci

func Fibonacci(first, second int64) BigIntStream

Fibonacci generates fibonacci numbers. first and second are the first and second terms in the sequence, normally 1 and 1.

func Partitions

func Partitions() BigIntStream

Partitions generates p(1), p(2), p(3), ... where p is the partition function. See Partition.

func Ugly

func Ugly(primeFactors ...int64) BigIntStream

Ugly returns the numbers whose prime factors are a subset of primeFactors in ascending order.

type Cake

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

Cake is used to compute the number of pieces you can get cutting an n dimensional cake k times. Cake instances are not safe to use with multiple goroutines

func NewCake

func NewCake() *Cake

NewCake returns a new Cake instance

func (*Cake) Eval

func (c *Cake) Eval(n, k int, result *big.Int) *big.Int

Eval stores the maximum number of pieces you can get cutting an n dimensional cake k times in result and returns result. Eval panics if n < 0 or k < 0.

type IntCounter

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

IntCounter counts how many values on wrapped IntStream are less than or equal to a given value. Wrapped stream must be monotone increasing.

func NewIntCounter

func NewIntCounter(stream IntStream) *IntCounter

NewIntCounter returns an IntCounter that wraps stream.

func (*IntCounter) CountLE

func (i *IntCounter) CountLE(value int64) int64

CountLE returns how many values on wrapped stream are less than or equal to value. CountLE panics if value is less than value in the previous call to CountLE.

type IntStream

type IntStream interface {
	// Next returns the next int64 value. If there are no more values, Next
	// returns 0, false.
	Next() (int64, bool)
}

IntStream represents a finite stream of int64 values.

func DecadePrimes

func DecadePrimes(start int64) IntStream

DecadePrimes generates all x >= start in ascending order such that 10x + 1, 10x + 3, 10x + 7, and 10x + 9 are all prime. DecadePrimes stops generating when 10*x > math.MaxInt64. If start*10 > math.MaxInt64, DecadePrimes generates nothing.

func Happys

func Happys(start int64) IntStream

Happys generates all the happy numbers in order that are greater than or equal to start.

func Harshads

func Harshads(start int64) IntStream

Harshads generates the harshad numbers in order that are greater than or equal to start.

func IntSummation

func IntSummation(stream IntStream) IntStream

IntSummation generates the summation of the values in stream. For example if stream generates the primes, IntSummation would generate 2, 5, 10, 17, ...

func Primes

func Primes(start int64) IntStream

Primes generates the prime numbers in order that are greater than or equal to start.

func Products deprecated added in v1.1.0

func Products(n, count int) IntStream

Deprecated: Use ProductsSlice.

type NthBigInt

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

NthBigInt wraps a BigIntStream and provides the Nth method to return the Nth big.Int off the stream.

func NewNthBigInt

func NewNthBigInt(stream BigIntStream) *NthBigInt

NewNthBigInt creates a new instance that wraps stream.

func (*NthBigInt) Nth

func (b *NthBigInt) Nth(n int64, value *big.Int) *big.Int

Nth returns the nth big.Int taken from the wrapped stream. n is 1-based so 1 returns the first big.Int, 2 returns the second etc. The nth big.Int is stored at value, and Nth returns this value. Nth panics if n is not greater than the number of values already taken from the stream.

type NthInt

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

NthInt wraps an IntStream and provides the Nth method to return the Nth int64 off the stream.

func NewNthInt

func NewNthInt(stream IntStream) *NthInt

NewNthInt creates a new instance that wraps stream.

func (*NthInt) Nth

func (i *NthInt) Nth(n int64) int64

Nth returns the nth int64 taken from the wrapped stream. n is 1-based so 1 returns the first int64, 2 returns the second etc. Nth panics if n is not greater than the number of values already taken from the stream or if there are fewer than N total values on the stream.

func (*NthInt) SafeNth

func (i *NthInt) SafeNth(n int64) (result int64, ok bool)

SafeNth works like Nth except that instead of panicing if n is greather than the total number of values in the stream, it returns ok=false.

type Partition

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

Partition computes the partition function, p, which calculates how many ways n can be partitioned when order doesn't matter.

For example: p(4) = 5 because there are 5 ways to express 4 as a sum when order doesn't matter. 4 = 1+1+1+1, 4 = 1+1+2 (covers 2+1+1 and 1+2+1), 4 = 1+3 (covers 3+1), 4 = 2+2, 4 = 4

Partition instances are not safe to use with multiple goroutines.

func NewPartition

func NewPartition() *Partition

NewPartition creates a new Partition instance

func (*Partition) Chart

func (p *Partition) Chart(n int64, result *big.Int) *big.Int

Chart is used to make a chart of the partition function using the github.com/keep94/gochart package. p.Chart(n, result) is the same as p.Eval(int(n), result)

func (*Partition) Eval

func (p *Partition) Eval(n int, result *big.Int) *big.Int

Eval evaluates p(n). Eval stores the result in result and returns result. Eval panics if n < 0.

type Point

type Point struct {
	X float64
	Y float64
}

Point represents a single (x, y) point

type PrimePower

type PrimePower struct {
	Prime int64
	Power int
}

PrimePower represents a single term of a prime power decomposition

func Factor

func Factor(n int64) []PrimePower

Factor returns the prime power decomposition of n. Factor panics if n < 1.

type Spline

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

Spline represents a cubic spline.

func NewSpline

func NewSpline(points []Point) *Spline

NewSpline returns a new cubic spline going through each point in points. The second derivative of the spline at the first and last point is 0. The x values in points must be strictly increasing.

func NewSplineWithSlopes

func NewSplineWithSlopes(
	points []Point, beginSlope, endSlope float64) *Spline

NewSplineWithSlopes returns a new cubic spline going through each point in points. The x values in points must be strictly increasing. beginSlope and endSlope specify the slope of the spline at the first point and last point respectively.

func (*Spline) Eval

func (s *Spline) Eval(x float64) float64

Eval evaluates this cubic spline at x. Eval panics if x doesn't fall between what MinX and MaxX return. Eval also panics if called on the zero value Spline.

func (*Spline) MaxX

func (s *Spline) MaxX() float64

MaxX returns the maximum value of x for this cubic spline. MaxX panics if called on the zero value Spline.

func (*Spline) MinX

func (s *Spline) MinX() float64

MinX returns the minimum value of x for this cubic spline. MinX panics if called on the zero value Spline.

Jump to

Keyboard shortcuts

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