poly

package
v0.0.0-...-9c88ebf Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2022 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package poly performs operations on polynomials.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cubic

func Cubic(d, c, b, a float64, buf []float64) []float64

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.

func Quad

func Quad(c, b, a float64, buf []float64) []float64

Quad finds the real roots of a quadratic 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.

func Quartic

func Quartic(e, d, c, b, a float64, buf []float64) []float64

Quartic finds the real roots of a Quartic 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

type Coefficients interface {
	Coefficient(idx int) float64
	Len() int
}

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

func (d D0) Coefficient(idx int) float64

Coefficient returns underlying float64 if the idx is 0, otherwise it returns 0.

func (D0) Len

func (D0) Len() int

Len is always 1

type D1

type D1 float64

D1 is a degree 1 polynomial with the first coefficient equal to 1.

func (D1) Coefficient

func (d D1) Coefficient(idx int) float64

Coefficient returns the underlying float64 if idx is 0 and returns 1 if the idx is 1.

func (D1) Len

func (D1) Len() int

Len is always equal to 2

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 Empty

type Empty struct{}

Empty constructs an empty polynomial.

func (Empty) Coefficient

func (Empty) Coefficient(idx int) float64

Coefficient always returns 0

func (Empty) Len

func (Empty) Len() int

Len always returns 0

type Integral

type Integral struct {
	Coefficients
	C float64
}

Integral of the underlying Coefficients.

func (Integral) Coefficient

func (i Integral) Coefficient(idx int) float64

Coefficient at idx is Coefficient(idx-1)/idx. Except at 0 where it is C.

func (Integral) Len

func (i Integral) Len() int

Len is always one more than the underlying Coefficients.

type Poly

type Poly struct {
	Coefficients
}

Poly is a 1D polynomial. The index corresponds power of X.

func New

func New(cs ...float64) Poly

New 1D polynomial with the given coefficients.

func (Poly) Add

func (p Poly) Add(p2 Poly) Poly

Add p and p2 using the Sum coefficients.

func (Poly) AssertEqual

func (p Poly) AssertEqual(to interface{}, t cmpr.Tolerance) error

AssertEqual allows Polynomials to be compared. This fulfills geomtest.AssertEqualizer.

func (Poly) Buf

func (p Poly) Buf() []float64

Buf tries to get the Coefficients as a []float64. This is intended for recycling buffers.

func (Poly) Copy

func (p Poly) Copy(buf []float64) Poly

Copy a Polynomial into a buffer.

func (Poly) D

func (p Poly) D() Poly

D returns the derivative of p.

func (Poly) Df

func (p Poly) Df(x float64) float64

Df computes the value of p'(x).

func (Poly) Divide

func (p Poly) Divide(n float64, buf []float64) (Poly, float64)

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

func (p Poly) Exp(n int, buf []float64) Poly

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) F

func (p Poly) F(x float64) float64

F computes the value of p(x).

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) Integral

func (p Poly) Integral(c float64) Poly

Integral of the given polynomial with the constant set to c.

func (Poly) IntegralAt

func (p Poly) IntegralAt(x, y float64) Poly

Integral of the given polynomial with the constant set so that the value of Pt1(x) == y.

func (*Poly) MultSwap

func (p *Poly) MultSwap(p2 Poly, buf []float64) []float64

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

func (p Poly) Multiply(p2 Poly) Poly

Multiply two polynomails. Note that it is not safe to reuse either input as the buffer.

func (Poly) Newton

func (p Poly) Newton(x float64, min cmpr.Tolerance, steps int, d Coefficients) (float64, float64)

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

func (p Poly) Roots(buf []float64) []float64

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.

func (Poly) Scale

func (p Poly) Scale(s float64) Poly

Scale will return an instace of the Scale Coefficient wrapper.

type Product

type Product [2]Coefficients

Product of two Coefficients

func (Product) Coefficient

func (p Product) Coefficient(idx int) float64

Coefficient at idx is the sum of all p[i]*p2[j] where i+j == idx

func (Product) Len

func (p Product) Len() int

Len is one less than the sum of the lengths.

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

func (s Scale) Coefficient(idx int) float64

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

func Buf(c int, buf []float64) Slice

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

func (s Slice) Coefficient(idx int) float64

Coefficient at idx. If the idx is greater than the length of the polynomial, then a 0 is returned.

func (Slice) Len

func (s Slice) Len() int

Len of the polynomial is equal to the length of the slice.

type Sum

type Sum [2]Coefficients

Sum of 2 Coefficients

func (Sum) Coefficient

func (s Sum) Coefficient(idx int) float64

Coefficient at idx is the sum of the underlying Coefficients at idx.

func (Sum) Len

func (s Sum) Len() int

Len is the greater len of the 2 Coefficients.

Jump to

Keyboard shortcuts

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