Documentation ¶
Overview ¶
Package poly performs operations on polynomials.
Index ¶
- func Cubic(d, c, b, a float64, buf []float64) []float64
- func Quad(c, b, a float64, buf []float64) []float64
- func Quartic(e, d, c, b, a float64, buf []float64) []float64
- type Coefficients
- type D0
- type D1
- type Derivative
- type Empty
- type Integral
- type Poly
- func (p Poly) Add(p2 Poly) Poly
- func (p Poly) AssertEqual(to interface{}, t cmpr.Tolerance) error
- func (p Poly) Buf() []float64
- func (p Poly) Copy(buf []float64) Poly
- func (p Poly) D() Poly
- func (p Poly) Df(x float64) float64
- func (p Poly) Divide(n float64, buf []float64) (Poly, float64)
- func (p Poly) Exp(n int, buf []float64) Poly
- func (p Poly) F(x float64) float64
- func (p Poly) Halley(x float64, min cmpr.Tolerance, steps int, d, d2 Coefficients) (float64, float64)
- func (p Poly) Integral(c float64) Poly
- func (p Poly) IntegralAt(x, y float64) Poly
- func (p *Poly) MultSwap(p2 Poly, buf []float64) []float64
- func (p Poly) Multiply(p2 Poly) Poly
- func (p Poly) Newton(x float64, min cmpr.Tolerance, steps int, d Coefficients) (float64, float64)
- func (p Poly) Roots(buf []float64) []float64
- func (p Poly) Scale(s float64) Poly
- type Product
- type RemoveLeadingZero
- type Scale
- type Slice
- type Sum
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Cubic ¶
Cubic finds the real roots of a cubic equation. The number of roots to return is set by the length of the buffer. If the length is zero then the max number of roots will be found.
Types ¶
type Coefficients ¶
Coefficients wraps the concept of a list of float64. It can express the order of the polynomial and return any coeffcient.
type D0 ¶
type D0 float64
D0 is a degree 0 polynomial - a constant.
func (D0) Coefficient ¶
Coefficient returns underlying float64 if the idx is 0, otherwise it returns 0.
type D1 ¶
type D1 float64
D1 is a degree 1 polynomial with the first coefficient equal to 1.
func (D1) Coefficient ¶
Coefficient returns the underlying float64 if idx is 0 and returns 1 if the idx is 1.
type Derivative ¶
type Derivative struct {
Coefficients
}
Derivative of the Coefficients
func (Derivative) Coefficient ¶
func (d Derivative) Coefficient(idx int) float64
Coefficient at idx is (idx+1)*Coefficient(idx+1).
func (Derivative) Len ¶
func (d Derivative) Len() int
Len is always one less than the underlying Coefficients.
type Integral ¶
type Integral struct { Coefficients C float64 }
Integral of the underlying Coefficients.
func (Integral) Coefficient ¶
Coefficient at idx is Coefficient(idx-1)/idx. Except at 0 where it is C.
type Poly ¶
type Poly struct {
Coefficients
}
Poly is a 1D polynomial. The index corresponds power of X.
func (Poly) AssertEqual ¶
AssertEqual allows Polynomials to be compared. This fulfills geomtest.AssertEqualizer.
func (Poly) Buf ¶
Buf tries to get the Coefficients as a []float64. This is intended for recycling buffers.
func (Poly) Divide ¶
Divide creates a new polynomial by dividing p by (x-n). The float64 returned is the remainder. If (x-n) is a root of p this value will be 0.
func (Poly) Exp ¶
Exp raises p to the power of n. To effiently allocate the buf it should have capacity of 3*(len(tc.p)*tc.pow - tc.pow + 1).
func (Poly) Halley ¶
func (p Poly) Halley(x float64, min cmpr.Tolerance, steps int, d, d2 Coefficients) (float64, float64)
Halley's method to find one root of the polynomial. The initial guess is passed in as x; min sets how close to 0 is acceptible and it will return if a value closer than that is found; steps limits the maximum number of iterations that will; d is the derivative; d2 is the second derivative. It is not required to provide d or d2, but if there is a cached instance available, it reduces repeated computation.
func (Poly) IntegralAt ¶
Integral of the given polynomial with the constant set so that the value of Pt1(x) == y.
func (*Poly) MultSwap ¶
MultSwap does a multiply and swap. It is used for effiency when doing consecutive multiplications. It is equivalent to:
p = p.Multiply(p2)
but it swaps the slice backing p with the buf after the multiplicaiton. It will generally be used like this:
buf = p.MultSwap(p2, buf)
func (Poly) Multiply ¶
Multiply two polynomails. Note that it is not safe to reuse either input as the buffer.
func (Poly) Newton ¶
Newton's method to find one root of the polynomial. The initial guess is passed in as x; min sets how close to 0 is acceptible and it will return if a value closer than that is found; steps limits the maximum number of iterations that will; d is the derivative. It is not required to provide d, but if there is a cached instance available, it reduces repeated computation.
func (Poly) Roots ¶
Roots finds the real roots of the polynomial. If an algebraic solution exists, that will be used. Otherwise it will use Halley's method to get it down to an order 5 solution. Because Halley's method is an approximation, errors tend to compound and this seems to become unreliable above a degree 10 polynomial. It is not safe to use p as the buffer. If the order of p>5 then the optimal buffer size is 5*p.Len()-6. The number of roots returned is set by the length of the buffer passed in. If the length is 0 then the max number of roots is returned.
type Product ¶
type Product [2]Coefficients
Product of two Coefficients
func (Product) Coefficient ¶
Coefficient at idx is the sum of all p[i]*p2[j] where i+j == idx
type RemoveLeadingZero ¶
type RemoveLeadingZero struct{ Coefficients }
RemoveLeadingZero simplifies a Polynomial where the leading Coefficient is zero. Note that this does no verification, it is only intended as a wrapper.
func (RemoveLeadingZero) Len ¶
func (r RemoveLeadingZero) Len() int
Len is always one less than the underlying Coefficients.
type Scale ¶
type Scale struct { By float64 Coefficients }
Scale Coefficients by a constant value
func (Scale) Coefficient ¶
Coefficient is product of scale factor and the underlying Coefficient at idx.
type Slice ¶
type Slice []float64
Slice fulfills Coefficients with a []float64.
func Buf ¶
Buf creates an instance of Poly with c capacity and a value of 1. This is useful when taking the product of several polynomials.
func (Slice) Coefficient ¶
Coefficient at idx. If the idx is greater than the length of the polynomial, then a 0 is returned.