Documentation ¶
Overview ¶
Package auxmath contains a number of auxiliary mathematical functions.
These functions are mainly intended for internal use in the algobra package, but because of their generality, they have been exported.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Factorize ¶
Factorize computes the prime factorization of n.
The output slice factors contains each distinct prime factor, and exponents contains the corresponding exponents. When n is one, both will be empty, representing the empty product.
As an exception, the function will return 0 as the only factor and 1 as the exponent when given zero as input.
Example ¶
package main import ( "fmt" "github.com/ReneBoedker/algobra/auxmath" ) func main() { var n uint = 2 * 2 * 2 * 3 * 3 * 13 * 13 * 13 fact, exps := auxmath.Factorize(n) fmt.Println(fact, exps) }
Output: [2 3 13] [3 2 3]
func FactorizePrimePower ¶
FactorizePrimePower computes p and n such that q=p^n.
If q is not a prime power, the function returns an InputValue-error.
Example ¶
package main import ( "fmt" "github.com/ReneBoedker/algobra/auxmath" ) func main() { q, err := auxmath.Pow(7, 12) if err != nil { fmt.Println(err) return } p, n, _ := auxmath.FactorizePrimePower(q) fmt.Printf("%d = %d^%d\n", q, p, n) _, _, err = auxmath.FactorizePrimePower(2 * 5) fmt.Println(err) }
Output: 13841287201 = 7^12 Factorizing prime power: 10 does not seem to be a prime power.
func Pow ¶
Pow returns a to the power of n.
It returns an Overflow-error if the input is likely to overflow the uint type.
Example (Overflow) ¶
package main import ( "fmt" "github.com/ReneBoedker/algobra/auxmath" ) func main() { _, err := auxmath.Pow(5, 28) fmt.Println(err) }
Output: Computing power of unsigned integer: 5^28 is likely to overflow uint
Types ¶
type CombinIter ¶
type CombinIter struct {
// contains filtered or unexported fields
}
CombinIter is an iterator for combinations.
It will iterate over all possible ways to choose a given number of elements from n elements. The combinations are represented by sorted slices of indices, and the iterator will produce them in lexicographically increasing order. For instance, it will generate the sequence [0,1,2], [0,1,3],..., [3,4,5] if defined with n=6 and k=3.
Example ¶
package main import ( "fmt" "github.com/ReneBoedker/algobra/auxmath" ) func main() { for ci := auxmath.NewCombinIter(6, 3); ci.Active(); ci.Next() { fmt.Println(ci.Current()) } }
Output: [0 1 2] [0 1 3] [0 1 4] [0 1 5] [0 2 3] [0 2 4] [0 2 5] [0 3 4] [0 3 5] [0 4 5] [1 2 3] [1 2 4] [1 2 5] [1 3 4] [1 3 5] [1 4 5] [2 3 4] [2 3 5] [2 4 5] [3 4 5]
func NewCombinIter ¶
func NewCombinIter(n, k int) *CombinIter
NewCombinIter returns a new iterator for k-combinations of n elements.
func (*CombinIter) Active ¶
func (ci *CombinIter) Active() bool
Active returns a boolean describing whether there are more combinations that have not been considered.
func (*CombinIter) Current ¶
func (ci *CombinIter) Current() []int
Current returns the slice of indices representing the current combination. The return value is a pointer to the underlying slice, meaning that changing the slice values may cause unexpected results.
func (*CombinIter) Next ¶
func (ci *CombinIter) Next()
Next increments the iterator to the next combination with respect to the lexicographical ordering. If the last combination has already been produced, the function returns immediately.
Example ¶
package main import ( "fmt" "github.com/ReneBoedker/algobra/auxmath" ) func main() { ci := auxmath.NewCombinIter(3, 2) fmt.Println(ci.Current()) // Iterate to the last combination for ci.Active() { ci.Next() } fmt.Println(ci.Current()) // Attempt incrementing, but already at end ci.Next() fmt.Println(ci.Current()) }
Output: [0 1] [1 2] [1 2]