poly

package
v1.2.23 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Eval

func Eval[S util.Comparable[S], T Term[S, T], P Polynomial[S, T, P]](poly P, env func(S) big.Int) *big.Int

Eval evaluates a given polynomial with a given environment (i.e. mapping of variables to values)

func Lisp

func Lisp[S util.Comparable[S], T Term[S, T], P Polynomial[S, T, P]](poly P, env func(S) string) sexp.SExp

Lisp constructs a suitable lisp representation for a given polynomial assuming an environment which maps identifiers to strings.

func String

func String[S util.Comparable[S], T Term[S, T], P Polynomial[S, T, P]](poly P, env func(S) string) string

String constructs a suitable string representation for a given polynomial assuming an environment which maps identifiers to strings.

Types

type ArrayPoly

type ArrayPoly[S util.Comparable[S]] struct {
	// contains filtered or unexported fields
}

ArrayPoly is the simpliest (and least efficient) polynomial implementation. It provides a reference against which other (more efficient) implementations can be compared. Observe that an unitialised ArrayPoly variable corresponds with zero.

func Parse

func Parse(input string) (*ArrayPoly[Var], []source.SyntaxError)

Parse a given input string representing a polynomial, or produce one or more errors.

func (*ArrayPoly[S]) Add

func (p *ArrayPoly[S]) Add(other *ArrayPoly[S]) *ArrayPoly[S]

Add another polynomial onto this polynomial.

func (*ArrayPoly[S]) AddScalar

func (p *ArrayPoly[S]) AddScalar(scalar *big.Int) *ArrayPoly[S]

AddScalar adds a signed scalar value to this polynomial

func (*ArrayPoly[S]) AddTerm

func (p *ArrayPoly[S]) AddTerm(other Monomial[S])

AddTerm adds a single term into this polynomial.

func (*ArrayPoly[S]) Clone

func (p *ArrayPoly[S]) Clone() *ArrayPoly[S]

Clone performs a deep copy of this polynomial

func (*ArrayPoly[S]) Cmp

func (p *ArrayPoly[S]) Cmp(other *ArrayPoly[S]) int

Cmp implementation for Comparable interface

func (*ArrayPoly[S]) Const64

func (p *ArrayPoly[S]) Const64(val uint64) *ArrayPoly[S]

Const64 Initialise this polynomial from a 64bit constant.

func (*ArrayPoly[S]) Equal

func (p *ArrayPoly[S]) Equal(other *ArrayPoly[S]) bool

Equal performs structural equality between two polynomials. That is, they are consider the same provide they have identical structure.

func (*ArrayPoly[S]) IsZero

func (p *ArrayPoly[S]) IsZero() (res bool, ok bool)

IsZero returns an indication as to whether this polynomial is equivalent to zero (or not). This is a three valued logic system which can return either "yes", "no" or "maybe" where: (i) "yes" means the polynomial always evaluates to zero; (ii) "no" means the polynomial never evaluates to zero; (iii) "maybe" indicates the polynomial may sometimes evaluate to zero. When the return ok holds then res indicates either yes or not. Otherwise, the result is maybe.

func (*ArrayPoly[S]) Len

func (p *ArrayPoly[S]) Len() uint

Len returns the number of terms in this polynomial.

func (*ArrayPoly[S]) Mul

func (p *ArrayPoly[S]) Mul(other *ArrayPoly[S]) *ArrayPoly[S]

Mul this polynomial by another polynomial.

func (*ArrayPoly[S]) MulScalar

func (p *ArrayPoly[S]) MulScalar(scalar *big.Int) *ArrayPoly[S]

MulScalar multiplies this polynomial by scalar.

func (*ArrayPoly[S]) Negate

func (p *ArrayPoly[S]) Negate() *ArrayPoly[S]

Negate polynomial

func (*ArrayPoly[S]) Set

func (p *ArrayPoly[S]) Set(terms ...Monomial[S]) *ArrayPoly[S]

Set initialises this polynomial from zero or more terms.

func (*ArrayPoly[S]) Shr

func (p *ArrayPoly[S]) Shr(n uint) (*ArrayPoly[S], *ArrayPoly[S])

Shr performs a "shift right" operation on this polynomial.

func (*ArrayPoly[S]) Signed

func (p *ArrayPoly[S]) Signed() bool

Signed determines whether or not this polynomial can evaluate to both positive and negative values. Currently, this is defined simply as whether or not a contained monomial has a negative coefficient.

func (*ArrayPoly[S]) Sub

func (p *ArrayPoly[S]) Sub(other *ArrayPoly[S]) *ArrayPoly[S]

Sub another polynomial from this polynomil

func (*ArrayPoly[S]) SubTerm

func (p *ArrayPoly[S]) SubTerm(other Monomial[S])

SubTerm subtracts a single term from this polynomial.

func (*ArrayPoly[S]) Term

func (p *ArrayPoly[S]) Term(ith uint) Monomial[S]

Term returns the ith term in this polynomial.

type Monomial

type Monomial[S util.Comparable[S]] struct {
	// contains filtered or unexported fields
}

Monomial represents a monomial within an array polynomial.

func NewMonomial

func NewMonomial[S util.Comparable[S]](coefficient big.Int, vars ...S) Monomial[S]

NewMonomial constructs a new array term with a given coefficient and zero or more variables.

func (Monomial[S]) Clone

func (p Monomial[S]) Clone() Monomial[S]

Clone an array term

func (Monomial[S]) Cmp

func (p Monomial[S]) Cmp(other Monomial[S]) int

Cmp implementation for the Comparable interface

func (Monomial[S]) Coefficient

func (p Monomial[S]) Coefficient() big.Int

Coefficient returns the coefficient of this term.

func (Monomial[S]) Contains

func (p Monomial[S]) Contains(v S) bool

Contains checks whether this monomial contains the given variable, or not.

func (Monomial[S]) Equal

func (p Monomial[S]) Equal(other Monomial[S]) bool

Equal performs structural equality between two mononomials. That is, they are consider the same provide they have identical structure.

func (Monomial[S]) FactorOut

func (p Monomial[S]) FactorOut(v S) Monomial[S]

FactorOut produces a fresh monomial containing one less occurrence of the given variable (if it is contained within). Otherwise, it returns an identical monomial.

func (Monomial[S]) IsNegative

func (p Monomial[S]) IsNegative() bool

IsNegative checks whether or not the coefficient for this monomial is negative.

func (Monomial[S]) IsZero

func (p Monomial[S]) IsZero() bool

IsZero checks whether or not this monomial is zero. Or, put another way, whether or not the coefficient of this monomial is zero.

func (Monomial[S]) Len

func (p Monomial[S]) Len() uint

Len returns the number of variables in this polynomial term.

func (Monomial[S]) Matches

func (p Monomial[S]) Matches(other Monomial[S]) bool

Matches determines whether or not the variables of this term match those of the other.

func (Monomial[S]) Mul

func (p Monomial[S]) Mul(other Monomial[S]) Monomial[S]

Mul returns a fresh monomial representing the multiplication of this monomial and another.

func (Monomial[S]) MulScalar

func (p Monomial[S]) MulScalar(scalar *big.Int) Monomial[S]

MulScalar multiplies this monomial by scalar.

func (Monomial[S]) Neg

func (p Monomial[S]) Neg() Monomial[S]

Neg returns a negated copy of this monomial

func (Monomial[S]) Negate

func (p Monomial[S]) Negate() Monomial[S]

Negate the coefficient of this monomial

func (Monomial[S]) Nth

func (p Monomial[S]) Nth(index uint) S

Nth returns the nth variable in this polynomial term.

func (Monomial[S]) Shr

func (p Monomial[S]) Shr(n uint) (quot Monomial[S], rem Monomial[S])

Shr performs a "shift right" on this monomial.

func (Monomial[S]) String

func (p Monomial[S]) String(env func(S) string) string

String constructs a suitable string representation for a given polynomial assuming an environment which maps identifiers to strings.

func (Monomial[S]) Vars

func (p Monomial[S]) Vars() []S

Vars retursnt the variables of this monomial as an array.

type Polynomial

type Polynomial[S util.Comparable[S], T Term[S, T], P any] interface {
	// Len returns the number of terms in this polynomial.
	Len() uint

	// Term returns the ith term in this polynomial.
	Term(uint) T

	// Initialise this polynomial from zero or more terms, returnining this.
	Set(...T) P

	// Initialise this polynomial from a 64bit constant.
	Const64(uint64) P

	// Cmp provides an ordering over polynomials.
	Cmp(o P) int

	// IsZero returns an indication as to whether this polynomial is equivalent
	// to zero (or not).  This is a three valued logic system which can return
	// either "yes", "no" or "maybe" where: (i) "yes" means the polynomial
	// always evaluates to zero; (ii) "no" means the polynomial never evaluates
	// to zero; (iii) "maybe" indicates the polynomial may sometimes evaluate to
	// zero.  When the return ok holds then res indicates either yes or not.
	// Otherwise, the result is maybe.
	IsZero() (res bool, ok bool)

	// Add another polynomial onto this polynomial, such that this polynomial is
	// updated in place.
	Add(P) P

	// Subtract another polynomial from this polynomial, such that this
	// polynomial is updated in place.
	Sub(P) P

	// Multiply this polynomial by another polynomial, such that this polynomial
	// is updated in place.
	Mul(P) P

	// Negate each mononial in this polynomial.
	Negate() P

	// For a given bitwidth n, divide a polynomial by 2^n produces a quotient and
	// remainder.  For example, dividing 256*x1+x0 by 2^8 gives x1 remainder x0.
	// This algorithm is somehow akin to "shifting" a polynomial downwards.  For
	// example, consider our example again:
	//
	//	 15             8 7               0
	//	+----------------+-----------------+
	//	|     2^8*x1     |        x0       |
	//	+----------------+-----------------+
	//
	// Then, shifting this down by 8bits gives:
	//
	//	                  7               0
	//	                 +-----------------+
	//	>>>>>>>>>>>>>>>> |        x1       |
	//	                 +-----------------+
	//
	// And we are left with a remainder as well.
	Shr(n uint) (quot P, rem P)
}

Polynomial represents a sum of terms of a type T of variables.

type Term

type Term[S any, T any] interface {
	// Coefficient returns the coefficient of this term.
	Coefficient() big.Int
	// Len returns the number of variables in this polynomial term.
	Len() uint
	// Negate this term
	Negate() T
	// Nth returns the nth variable in this polynomial term.
	Nth(uint) S
	// Matches determines whether or not the variables of this term match those
	// of the other.
	Matches(other T) bool
	// Check whether this term has a negative coefficient (or not)
	IsNegative() bool
}

Term represents a product (or monomial) within a polynomial.

type Var

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

Var is a wrapper around a string

func (Var) Cmp

func (p Var) Cmp(o Var) int

Cmp implementation for Comparable interface

func (Var) String

func (p Var) String(func(string) string) string

Jump to

Keyboard shortcuts

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