package interp
import "gonum.org/v1/gonum/interp"
Package interp implements 1-dimensional algorithms for interpolating values. Outside of the interpolation interval determined by the interpolated data, the returned value is undefined (but we do our best to return something reasonable).
Index ¶
- type AkimaSpline
- func (as *AkimaSpline) Fit(xs, ys []float64) error
- func (as *AkimaSpline) Predict(x float64) float64
- func (as *AkimaSpline) PredictDerivative(x float64) float64
- type ClampedCubic
- func (cc *ClampedCubic) Fit(xs, ys []float64) error
- func (cc *ClampedCubic) Predict(x float64) float64
- func (cc *ClampedCubic) PredictDerivative(x float64) float64
- type Constant
- type DerivativePredictor
- type FittablePredictor
- type Fitter
- type FritschButland
- func (fb *FritschButland) Fit(xs, ys []float64) error
- func (fb *FritschButland) Predict(x float64) float64
- func (fb *FritschButland) PredictDerivative(x float64) float64
- type Function
- type NaturalCubic
- func (nc *NaturalCubic) Fit(xs, ys []float64) error
- func (nc *NaturalCubic) Predict(x float64) float64
- func (nc *NaturalCubic) PredictDerivative(x float64) float64
- type NotAKnotCubic
- func (nak *NotAKnotCubic) Fit(xs, ys []float64) error
- func (nak *NotAKnotCubic) Predict(x float64) float64
- func (nak *NotAKnotCubic) PredictDerivative(x float64) float64
- type PiecewiseConstant
- func (pc *PiecewiseConstant) Fit(xs, ys []float64) error
- func (pc PiecewiseConstant) Predict(x float64) float64
- type PiecewiseCubic
- func (pc *PiecewiseCubic) FitWithDerivatives(xs, ys, dydxs []float64)
- func (pc *PiecewiseCubic) Predict(x float64) float64
- func (pc *PiecewiseCubic) PredictDerivative(x float64) float64
- type PiecewiseLinear
- func (pl *PiecewiseLinear) Fit(xs, ys []float64) error
- func (pl PiecewiseLinear) Predict(x float64) float64
- type Predictor
Examples ¶
Types ¶
type AkimaSpline ¶
type AkimaSpline struct {
// contains filtered or unexported fields
}
AkimaSpline is a piecewise cubic 1-dimensional interpolator with continuous value and first derivative, which can be fitted to (X, Y) value pairs without providing derivatives. See https://www.iue.tuwien.ac.at/phd/rottinger/node60.html for more details.
func (*AkimaSpline) Fit ¶
func (as *AkimaSpline) Fit(xs, ys []float64) error
Fit fits a predictor to (X, Y) value pairs provided as two slices. It panics if len(xs) < 2, elements of xs are not strictly increasing or len(xs) != len(ys). Always returns nil.
func (*AkimaSpline) Predict ¶
func (as *AkimaSpline) Predict(x float64) float64
Predict returns the interpolation value at x.
func (*AkimaSpline) PredictDerivative ¶
func (as *AkimaSpline) PredictDerivative(x float64) float64
PredictDerivative returns the predicted derivative at x.
type ClampedCubic ¶
type ClampedCubic struct {
// contains filtered or unexported fields
}
ClampedCubic is a piecewise cubic 1-dimensional interpolator with continuous value, first and second derivatives, which can be fitted to (X, Y) value pairs without providing derivatives. It uses the boundary conditions Y′(left end ) = Y′(right end) = 0.
func (*ClampedCubic) Fit ¶
func (cc *ClampedCubic) Fit(xs, ys []float64) error
Fit fits a predictor to (X, Y) value pairs provided as two slices. It panics if len(xs) < 2, elements of xs are not strictly increasing or len(xs) != len(ys). It returns an error if solving the required system of linear equations fails.
func (*ClampedCubic) Predict ¶
func (cc *ClampedCubic) Predict(x float64) float64
Predict returns the interpolation value at x.
func (*ClampedCubic) PredictDerivative ¶
func (cc *ClampedCubic) PredictDerivative(x float64) float64
PredictDerivative returns the predicted derivative at x.
type Constant ¶
type Constant float64
Constant predicts a constant value.
func (Constant) Predict ¶
Predict returns the predicted value at x.
type DerivativePredictor ¶
type DerivativePredictor interface { Predictor // PredictDerivative returns the predicted derivative at x. PredictDerivative(x float64) float64 }
DerivativePredictor predicts both the value and the derivative of a function. It handles both interpolation and extrapolation.
type FittablePredictor ¶
FittablePredictor is a Predictor which can fit itself to data.
type Fitter ¶
type Fitter interface { // Fit fits a predictor to (X, Y) value pairs provided as two slices. // It panics if len(xs) < 2, elements of xs are not strictly increasing // or len(xs) != len(ys). Returns an error if fitting fails. Fit(xs, ys []float64) error }
Fitter fits a predictor to data.
type FritschButland ¶
type FritschButland struct {
// contains filtered or unexported fields
}
FritschButland is a piecewise cubic 1-dimensional interpolator with continuous value and first derivative, which can be fitted to (X, Y) value pairs without providing derivatives. It is monotone, local and produces a C^1 curve. Its downside is that exhibits high tension, flattening out unnaturally the interpolated curve between the nodes. See Fritsch, F. N. and Butland, J., "A method for constructing local monotone piecewise cubic interpolants" (1984), SIAM J. Sci. Statist. Comput., 5(2), pp. 300-304.
func (*FritschButland) Fit ¶
func (fb *FritschButland) Fit(xs, ys []float64) error
Fit fits a predictor to (X, Y) value pairs provided as two slices. It panics if len(xs) < 2, elements of xs are not strictly increasing or len(xs) != len(ys). Always returns nil.
func (*FritschButland) Predict ¶
func (fb *FritschButland) Predict(x float64) float64
Predict returns the interpolation value at x.
func (*FritschButland) PredictDerivative ¶
func (fb *FritschButland) PredictDerivative(x float64) float64
PredictDerivative returns the predicted derivative at x.
type Function ¶
Function predicts by evaluating itself.
func (Function) Predict ¶
Predict returns the predicted value at x by evaluating fn(x).
type NaturalCubic ¶
type NaturalCubic struct {
// contains filtered or unexported fields
}
NaturalCubic is a piecewise cubic 1-dimensional interpolator with continuous value, first and second derivatives, which can be fitted to (X, Y) value pairs without providing derivatives. It uses the boundary conditions Y′′(left end ) = Y′′(right end) = 0. See e.g. https://www.math.drexel.edu/~tolya/cubicspline.pdf for details.
func (*NaturalCubic) Fit ¶
func (nc *NaturalCubic) Fit(xs, ys []float64) error
Fit fits a predictor to (X, Y) value pairs provided as two slices. It panics if len(xs) < 2, elements of xs are not strictly increasing or len(xs) != len(ys). It returns an error if solving the required system of linear equations fails.
func (*NaturalCubic) Predict ¶
func (nc *NaturalCubic) Predict(x float64) float64
Predict returns the interpolation value at x.
func (*NaturalCubic) PredictDerivative ¶
func (nc *NaturalCubic) PredictDerivative(x float64) float64
PredictDerivative returns the predicted derivative at x.
type NotAKnotCubic ¶
type NotAKnotCubic struct {
// contains filtered or unexported fields
}
NotAKnotCubic is a piecewise cubic 1-dimensional interpolator with continuous value, first and second derivatives, which can be fitted to (X, Y) value pairs without providing derivatives. It imposes the condition that the third derivative of the interpolant is continuous in the first and last interior node. See http://www.cs.tau.ac.il/~turkel/notes/numeng/spline_note.pdf for details.
func (*NotAKnotCubic) Fit ¶
func (nak *NotAKnotCubic) Fit(xs, ys []float64) error
Fit fits a predictor to (X, Y) value pairs provided as two slices. It panics if len(xs) < 3 (because at least one interior node is required), elements of xs are not strictly increasing or len(xs) != len(ys). It returns an error if solving the required system of linear equations fails.
func (*NotAKnotCubic) Predict ¶
func (nak *NotAKnotCubic) Predict(x float64) float64
Predict returns the interpolation value at x.
func (*NotAKnotCubic) PredictDerivative ¶
func (nak *NotAKnotCubic) PredictDerivative(x float64) float64
PredictDerivative returns the predicted derivative at x.
type PiecewiseConstant ¶
type PiecewiseConstant struct {
// contains filtered or unexported fields
}
PiecewiseConstant is a left-continuous, piecewise constant 1-dimensional interpolator.
func (*PiecewiseConstant) Fit ¶
func (pc *PiecewiseConstant) Fit(xs, ys []float64) error
Fit fits a predictor to (X, Y) value pairs provided as two slices. It panics if len(xs) < 2, elements of xs are not strictly increasing or len(xs) != len(ys). Always returns nil.
func (PiecewiseConstant) Predict ¶
func (pc PiecewiseConstant) Predict(x float64) float64
Predict returns the interpolation value at x.
type PiecewiseCubic ¶
type PiecewiseCubic struct {
// contains filtered or unexported fields
}
PiecewiseCubic is a piecewise cubic 1-dimensional interpolator with continuous value and first derivative.
func (*PiecewiseCubic) FitWithDerivatives ¶
func (pc *PiecewiseCubic) FitWithDerivatives(xs, ys, dydxs []float64)
FitWithDerivatives fits a piecewise cubic predictor to (X, Y, dY/dX) value triples provided as three slices. It panics if len(xs) < 2, elements of xs are not strictly increasing, len(xs) != len(ys) or len(xs) != len(dydxs).
func (*PiecewiseCubic) Predict ¶
func (pc *PiecewiseCubic) Predict(x float64) float64
Predict returns the interpolation value at x.
func (*PiecewiseCubic) PredictDerivative ¶
func (pc *PiecewiseCubic) PredictDerivative(x float64) float64
PredictDerivative returns the predicted derivative at x.
type PiecewiseLinear ¶
type PiecewiseLinear struct {
// contains filtered or unexported fields
}
PiecewiseLinear is a piecewise linear 1-dimensional interpolator.
func (*PiecewiseLinear) Fit ¶
func (pl *PiecewiseLinear) Fit(xs, ys []float64) error
Fit fits a predictor to (X, Y) value pairs provided as two slices. It panics if len(xs) < 2, elements of xs are not strictly increasing or len(xs) != len(ys). Always returns nil.
func (PiecewiseLinear) Predict ¶
func (pl PiecewiseLinear) Predict(x float64) float64
Predict returns the interpolation value at x.
type Predictor ¶
type Predictor interface { // Predict returns the predicted value at x. Predict(x float64) float64 }
Predictor predicts the value of a function. It handles both
interpolation and extrapolation.
Code:play
Output:Example¶
package main
import (
"fmt"
"math"
"os"
"text/tabwriter"
"gonum.org/v1/gonum/interp"
)
func main() {
// An example of fitting different interpolation
// algorithms to (X, Y) data with widely varying slope.
//
// Cubic interpolators have to balance the smoothness
// of the generated curve with suppressing ugly wiggles
// (compare the output of AkimaSpline with that of
// FritschButland).
xs := []float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
ys := []float64{0, 0.001, 0.002, 0.1, 1, 2, 2.5, -10, -10.01, 2.49, 2.53, 2.55}
var pc interp.PiecewiseConstant
var pl interp.PiecewiseLinear
var as interp.AkimaSpline
var fb interp.FritschButland
predictors := []interp.FittablePredictor{&pc, &pl, &as, &fb}
for i, p := range predictors {
err := p.Fit(xs, ys)
if err != nil {
panic(fmt.Sprintf("Error fitting %d-th predictor: %v", i, err))
}
}
n := len(xs)
dx := 0.25
nPts := int(math.Round(float64(n-1)/dx)) + 1
w := tabwriter.NewWriter(os.Stdout, 8, 0, 1, ' ', tabwriter.AlignRight)
fmt.Fprintln(w, "x\tPC\tPL\tAS\tFB\t")
for i := 0; i < nPts; i++ {
x := xs[0] + float64(i)*dx
fmt.Fprintf(w, "%.2f", x)
for _, predictor := range predictors {
y := predictor.Predict(x)
fmt.Fprintf(w, "\t%.2f", y)
}
fmt.Fprintln(w, "\t")
}
fmt.Fprintln(w)
w.Flush()
}
x PC PL AS FB
0.00 0.00 0.00 0.00 0.00
0.25 0.00 0.00 0.00 0.00
0.50 0.00 0.00 0.00 0.00
0.75 0.00 0.00 0.00 0.00
1.00 0.00 0.00 0.00 0.00
1.25 0.00 0.00 0.00 0.00
1.50 0.00 0.00 0.00 0.00
1.75 0.00 0.00 0.00 0.00
2.00 0.00 0.00 0.00 0.00
2.25 0.10 0.03 -0.01 0.01
2.50 0.10 0.05 -0.01 0.03
2.75 0.10 0.08 0.02 0.06
3.00 0.10 0.10 0.10 0.10
3.25 1.00 0.33 0.26 0.22
3.50 1.00 0.55 0.49 0.45
3.75 1.00 0.78 0.75 0.73
4.00 1.00 1.00 1.00 1.00
4.25 2.00 1.25 1.24 1.26
4.50 2.00 1.50 1.50 1.54
4.75 2.00 1.75 1.75 1.79
5.00 2.00 2.00 2.00 2.00
5.25 2.50 2.12 2.22 2.17
5.50 2.50 2.25 2.37 2.33
5.75 2.50 2.38 2.47 2.45
6.00 2.50 2.50 2.50 2.50
6.25 -10.00 -0.62 0.83 0.55
6.50 -10.00 -3.75 -2.98 -3.75
6.75 -10.00 -6.88 -7.18 -8.04
7.00 -10.00 -10.00 -10.00 -10.00
7.25 -10.01 -10.00 -11.16 -10.00
7.50 -10.01 -10.00 -11.55 -10.01
7.75 -10.01 -10.01 -11.18 -10.01
8.00 -10.01 -10.01 -10.01 -10.01
8.25 2.49 -6.88 -7.18 -8.06
8.50 2.49 -3.76 -2.99 -3.77
8.75 2.49 -0.63 0.82 0.53
9.00 2.49 2.49 2.49 2.49
9.25 2.53 2.50 2.50 2.51
9.50 2.53 2.51 2.51 2.52
9.75 2.53 2.52 2.52 2.52
10.00 2.53 2.53 2.53 2.53
10.25 2.55 2.53 2.54 2.54
10.50 2.55 2.54 2.54 2.54
10.75 2.55 2.54 2.55 2.55
11.00 2.55 2.55 2.55 2.55
Source Files ¶
cubic.go doc.go interp.go
- Version
- v0.15.1 (latest)
- Published
- Aug 16, 2024
- Platform
- linux/amd64
- Imports
- 3 packages
- Last checked
- 11 hours ago –
Tools for package owners.