poly

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnpairedConjugate = errors.New("complex roots must come in conjugate pairs")

ErrUnpairedConjugate is returned when a complex root has no matching conjugate, so a polynomial built from the roots can't have real coefficients. Callers in dsp wrap it with their own sentinel, as poly is internal.

Functions

func AppendRepeated

func AppendRepeated(roots []complex128, root complex128, n int) (padded []complex128)

AppendRepeated returns roots followed by n copies of root, leaving roots unmodified. A negative n panics, which flags a filter with more zeros than poles.

func CollapseConjugates

func CollapseConjugates(roots []complex128) (collapsed []complex128, err error)

CollapseConjugates replaces each conjugate pair with one root, the member with a positive imaginary part, averaged with the other's conjugate to cancel rounding, same as SciPy's _cplxreal. Pairs come first, sorted by real part then imaginary part, followed by the real roots, sorted by value.

Returns: ErrUnpairedConjugate for a complex root with no conjugate.

func FromRoots

func FromRoots(roots []complex128) (coeffs []complex128)

FromRoots returns the coefficients of Π(x - r) for the given roots, highest power first, [x^n, x^(n-1), ..., x^0], same as numpy.poly.

func MapRoots

func MapRoots(roots []complex128, fn func(complex128) complex128) (mapped []complex128)

MapRoots applies fn to every root, returning a new slice.

func Mul

func Mul(p, q []float64) (product []float64)

Mul multiplies two polynomials, given highest power first.

func Prod

func Prod(values []complex128) (p complex128)

Prod returns the product of all values, or 1 for an empty slice.

func RealCoeffs

func RealCoeffs(complexCoeffs []complex128) (coeffs []float64, err error)

RealCoeffs drops the imaginary part of polynomial coefficients that should be real, as their roots came in conjugate pairs.

Returns: ErrUnpairedConjugate for an imaginary part larger than rounding.

func ReverseBesselRoots added in v0.10.0

func ReverseBesselRoots(order int, scale ReverseBesselScale) (roots []complex128)

ReverseBesselRoots returns the roots of the reverse Bessel polynomial θ of order N, scaled by scale, each rounded once from its exact value:

θ(s) = Σ (2N - k)!/(2^(N - k)·k!·(N - k)!)·s^k, for k = 0, 1, ..., N

They're very sensitive to rounding, by about 1.8 bits per order, so 1e13 at order 25, and any float64 evaluation of θ, by its coefficients or a recurrence, misses them by that factor times machine epsilon. So they're found in big.Float, at 2 bits per order beyond float64's 53 and a margin, by Aberth's method, with θ and θ′ from the recurrence:

θ_n(s) = (2n - 1)·θ_(n-1)(s) + s²·θ_(n-2)(s),  θ_0 = 1, θ_1 = s + 1

The roots come in conjugate pairs, plus one real root for an odd order, sorted by imaginary part. An order below 1 panics. They're the poles of scipy.signal.besselap(N, norm) for norm 'delay', 'phase' and 'mag', which evaluates θ with scipy.special.kve.

Types

type DelayGrid added in v1.0.0

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

DelayGrid is the frequency grid ω = π·i/n, at which a polynomial's group delay is found. It's the grid freqz uses, n points from 0 to just below π.

func NewDelayGrid added in v1.0.0

func NewDelayGrid(n int) (g DelayGrid)

NewDelayGrid is the grid of n points.

func (DelayGrid) Delay added in v1.0.0

func (g DelayGrid) Delay(c []float64, i int) (delay float64)

Delay is the group delay in samples of Σ c_k e^(-jkω), the numerator or denominator of a filter, at ω = π·i/n. Leading zeros delay it by a whole sample each, and trailing zeros don't change it. With m_k = k - N/2 for degree N,

B(e^(-jω)) = e^(-jNω/2)·F(ω),  F(ω) = Σ c_k e^(-j·m_k·ω) = R + jI

so its delay is N/2 - Im(F′/F). F's real and imaginary parts pair each coefficient with its mirror, c_k ± c_(N-k). So a symmetric or antisymmetric polynomial, such as a linear-phase FIR or a Butterworth numerator, has I or R exactly 0, and a delay of exactly N/2. That holds even at a zero on the unit circle, where it's the limit from either side. Where F is ill-conditioned, near a zero on the unit circle or at one, the delay is found in big.Float.

type Fit added in v1.0.0

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

Fit is the polynomials q_0 to q_degree orthogonal over a window of n positions, the samples' offsets from its centre divided by its spacing, so from -1 to 1. The least-squares fit of a degree is then a projection onto each, with no system to solve:

Σ q_j·⟨q_j, x⟩/‖q_j‖²

They're built by Arnoldi iteration, same as in Vandermonde with Arnoldi, by Brubeck, Nakatsukasa and Trefethen, 2021. Each u·q_j is orthogonalised against every q_i before it, twice, which stays orthogonal at any degree. The Gram polynomials' three-term recurrence fails beyond a degree of about 2√n. q_0 is 1, with ‖q_0‖² = n, so a moving average's weight is 1/n rounded once, rather than (1/√n)² rounded three times. Every later q_j is normalised, ‖q_j‖² = 1.

func NewFit added in v1.0.0

func NewFit(n, degree int) (f Fit)

NewFit is the orthogonal polynomials up to degree, over n positions. degree must be less than n.

func (Fit) Spacing added in v1.0.0

func (f Fit) Spacing() (spacing int)

Spacing is the samples per unit of position: half the window, or 1 for a window of 1. A derivative in position is spacing^deriv times one in samples.

func (Fit) Weights added in v1.0.0

func (f Fit) Weights(at, deriv int) (w []float64)

Weights maps a window's samples to the fitted polynomial's deriv-th derivative, in position u, at sample at:

Σ_j q_j(u_k)·q_j^(deriv)(u_at)/‖q_j‖²

A derivative above the degree is 0, so every weight is 0.

type Pairing added in v1.0.0

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

Pairing holds the zeros and poles not yet placed in a second-order section. Each conjugate pair is stored once, see CollapseConjugates. It follows the loop in SciPy's zpk2sos, with its default pairing='nearest'.

func NewPairing added in v1.0.0

func NewPairing(zeros, poles []complex128) (pr Pairing, sections int, err error)

NewPairing balances zeros and poles, collapses their conjugate pairs, and returns the pool and how many sections it makes.

Returns: ErrUnpairedConjugate, wrapped, if complex zeros or poles aren't in conjugate pairs.

func (*Pairing) Next added in v1.0.0

func (pr *Pairing) Next() (zeros, poles []complex128)

Next removes and returns the zeros and poles for the next section, starting from the pole closest to the unit circle.

type ReverseBesselScale added in v0.10.0

type ReverseBesselScale int

ReverseBesselScale is how ReverseBesselRoots scales the roots.

const (
	// UnitDelay leaves the roots as they are,
	// so θ(0)/θ(s) has a group delay of 1 at DC.
	UnitDelay ReverseBesselScale = iota
	// UnitProduct divides them by θ(0)^(1/N),
	// so their magnitudes multiply to 1.
	UnitProduct
	// UnitHalfPower divides them by the frequency where |θ(0)/θ(jω)|² = 1/2,
	// moving it to 1.
	UnitHalfPower
)

Jump to

Keyboard shortcuts

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