interpolate

package
v0.0.0-...-beb861e Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2020 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CubicSpline

func CubicSpline(xs, ys []float64) func(x float64) float64

CubicSpline adapted from https://github.com/morganherlocker/cubic-spline

Example
xs := make([]float64, 20)
ys := make([]float64, len(xs))
for i := range xs {
	xs[i] = float64(i) - 9.5
	ys[i] = rand.NormFloat64()
}
f := CubicSpline(xs, ys)
x2 := make([]float64, 220)
y2 := make([]float64, 220)
for i := range x2 {
	x2[i] = float64(i)/10 - 10.5
	y2[i] = f(x2[i])
}

p, err := plot.New()
check(err)
scatter, err := plotter.NewScatter(&xy{xs, ys})
line, err := plotter.NewLine(&xy{x2, y2})
p.Add(scatter, line)

if *visualDebug {
	filename := "/tmp/spline.svg"
	os.Remove(filename)
	if err := p.Save(16*vg.Inch, 9*vg.Inch, filename); err != nil {
		panic(err)
	}
	_, err = os.Stat("/usr/bin/display")
	if err == nil {
		cmd := exec.Command("/usr/bin/display", "/tmp/spline.svg")
		cmd.Start()
	}
}
Output:

func Interp1d

func Interp1d(x, y []float64) func(x float64) float64

Interp1d return linear interpolation from x,y points mimics partly scipy.interpolate.interp1d

Example
x := []float64{1, 2, 3, 4}
y := []float64{-1, -2, 6, 8}
ea := func(expected, actual float64) {
	if expected != actual {
		fmt.Printf("expected:%g actual:%g\n", expected, actual)
	}
}
subtest := func(shuffle bool) {
	if shuffle {
		perm := rand.Perm(len(x))
		for sort.IntsAreSorted(perm) {
			perm = rand.Perm(len(x))
		}
		for i, p := range perm {
			if p > i {
				x[p], x[i] = x[i], x[p]
				y[p], y[i] = y[i], y[p]
			}
		}
	}
	f := Interp1d(x, y)
	ea(1, f(-1))
	ea(0, f(0))
	ea(-1, f(1))
	ea(-1.5, f(1.5))
	ea(-2, f(2))
	ea(2, f(2.5))
	ea(6, f(3))
	ea(7, f(3.5))
	ea(8, f(4))
	ea(10, f(5))
}
subtest(false)
subtest(true)
Output:

func Interp2d

func Interp2d(x, y, z []float64) func(x, y float64) float64

Interp2d computes bilinear interpolation from x,y to z

Example
x := []float64{1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4}
y := []float64{1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4}
z := []float64{0, 0, 0, 0, 0, 4, 6, 0, 0, 8, 10, 0, 0, 0, 0, 0}
f := Interp2d(x, y, z)
ea := func(expected, actual float64) {
	if expected != actual {
		fmt.Printf("expected:%g actual:%g\n", expected, actual)
	}
}
ea(7, f(2.5, 2.5))
ea(5.5, f(2.25, 2.25))
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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