interpolator

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2021 License: MIT Imports: 4 Imported by: 3

README

Interpolator

GoDoc Build Status GolangCI Lint Go Report Card

Description

Package interpolator provides univariate data interpolators:

The input data is specified by means of a nonempty slice of two-dimensional points XYs. If a single data point is provided, the resulting interpolator treats the input as a constant for all abscissae.

Installation

go get -u github.com/edgelaboratories/interpolator

Example

package main

import (
	"fmt"
	"log"

	"github.com/edgelaboratories/interpolator"
)

func main() {
	xys := interpolator.XYs{
		{
			X: 0.0,
			Y: 1.2,
		},
		{
			X: 0.5,
			Y: 1.0,
		},
		{
			X: 1.0,
			Y: 1.4,
		},
	}
	interp, err := interpolator.NewGeometric(xys)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("value at 0.75 is %0.2f\n", interp.Value(0.75))
	fmt.Printf("gradient at 0.75 is %0.2f\n", interp.Gradient(0.75))
}

Documentation

Overview

Package interpolator provides interpolators to perform numerical interpolator for univariate data.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Geometric

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

Geometric is a classic geometric interpolator.

func NewGeometric

func NewGeometric(xys XYs) (*Geometric, error)

NewGeometric builds a geometric interpolator. The input `xys` must be ordered, have unique abscissas and positive ordinates.

func (Geometric) Gradient

func (interp Geometric) Gradient(x float64) float64

Gradient computes the gradient of f(x) based on geometric interpolation.

Example
xys := XYs{
	{
		X: 0.0,
		Y: 1.2,
	},
	{
		X: 0.5,
		Y: 1.0,
	},
	{
		X: 1.0,
		Y: 1.4,
	},
}
interp, err := NewGeometric(xys)
if err != nil {
	return
}
fmt.Printf("%0.4f\n", interp.Gradient(0.75))
Output:
0.7962

func (Geometric) Value

func (interp Geometric) Value(x float64) float64

Value compute the value of f(x) based on geometric interpolation.

Example
xys := XYs{
	{
		X: 0.0,
		Y: 1.2,
	},
	{
		X: 0.5,
		Y: 1.0,
	},
	{
		X: 1.0,
		Y: 1.4,
	},
}
interp, err := NewGeometric(xys)
if err != nil {
	return
}
fmt.Printf("%0.4f\n", interp.Value(0.75))
Output:
1.1832

type GeometricSqrt

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

GeometricSqrt performs a geometric interpolation with respect to the square root of the abscissae.

func NewGeometricSqrt

func NewGeometricSqrt(xys XYs) (*GeometricSqrt, error)

NewGeometricSqrt builds a geometric sqrt interpolator. The input `xys` must be ordered, have unique abscissas and positive ordinates.

func (GeometricSqrt) Gradient

func (interp GeometricSqrt) Gradient(x float64) float64

Gradient computes the gradient of f(x) based on geometric sqrt interpolation.

Example
xys := XYs{
	{
		X: 0.0,
		Y: 1.2,
	},
	{
		X: 0.5,
		Y: 1.0,
	},
	{
		X: 1.0,
		Y: 1.4,
	},
}
interp, err := NewGeometricSqrt(xys)
if err != nil {
	return
}
fmt.Printf("%0.4f\n", interp.Gradient(0.75))
Output:
0.6037

func (GeometricSqrt) Value

func (interp GeometricSqrt) Value(x float64) float64

Value compute the value of f(x) based on geometric sqrt interpolation with flat extrapolation.

Example
xys := XYs{
	{
		X: 0.0,
		Y: 1.2,
	},
	{
		X: 0.5,
		Y: 1.0,
	},
	{
		X: 1.0,
		Y: 1.4,
	},
}
interp, err := NewGeometricSqrt(xys)
if err != nil {
	return
}
fmt.Printf("%0.4f\n", interp.Value(0.75))
Output:
1.2686

type PiecewiseConstant

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

PiecewiseConstant is a classic piecewise constant cadlag interpolator.

func NewPiecewiseConstant

func NewPiecewiseConstant(xys XYs) (*PiecewiseConstant, error)

NewPiecewiseConstant builds a piecewise constant interpolator. The input `xys` must be ordered and have unique abscissas.

func (PiecewiseConstant) Gradient

func (interp PiecewiseConstant) Gradient(x float64) float64

Gradient computes the gradient of f(x) based on piecewise constant interpolation.

Example
xys := XYs{
	{
		X: 0.0,
		Y: 1.2,
	},
	{
		X: 0.5,
		Y: 1.0,
	},
	{
		X: 1.0,
		Y: 1.4,
	},
}
interp, err := NewPiecewiseConstant(xys)
if err != nil {
	return
}
fmt.Println(interp.Gradient(0.75))
Output:
0

func (PiecewiseConstant) Value

func (interp PiecewiseConstant) Value(x float64) float64

Value compute the value of f(x) based on piecewise constant interpolation.

Example
xys := XYs{
	{
		X: 0.0,
		Y: 1.2,
	},
	{
		X: 0.5,
		Y: 1.0,
	},
	{
		X: 1.0,
		Y: 1.4,
	},
}
interp, err := NewPiecewiseConstant(xys)
if err != nil {
	return
}
fmt.Println(interp.Value(0.75))
Output:
1

type PiecewiseLinear

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

PiecewiseLinear is a classic piecewise linear interpolator.

func NewPiecewiseLinear

func NewPiecewiseLinear(xys XYs) (*PiecewiseLinear, error)

NewPiecewiseLinear builds a piecewise linear interpolator.

func (PiecewiseLinear) Gradient

func (interp PiecewiseLinear) Gradient(x float64) float64

Gradient computes the gradient of f(x) based on linear interpolation.

Example
xys := XYs{
	{
		X: 0.0,
		Y: 1.2,
	},
	{
		X: 0.5,
		Y: 1.0,
	},
	{
		X: 1.0,
		Y: 1.4,
	},
}
interp, err := NewPiecewiseLinear(xys)
if err != nil {
	return
}
fmt.Printf("%0.1f\n", interp.Gradient(0.75))
Output:
0.8

func (PiecewiseLinear) Value

func (interp PiecewiseLinear) Value(x float64) float64

Value compute the value of f(x) based on piecewise linear interpolation.

Example
xys := XYs{
	{
		X: 0.0,
		Y: 1.2,
	},
	{
		X: 0.5,
		Y: 1.0,
	},
	{
		X: 1.0,
		Y: 1.4,
	},
}
interp, err := NewPiecewiseLinear(xys)
if err != nil {
	return
}
fmt.Println(interp.Value(0.75))
Output:
1.2

type PiecewiseLinearSqrt

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

PiecewiseLinearSqrt performs a piecewise linear interpolation with respect to the square root of the abscissae.

func NewPiecewiseLinearSqrt

func NewPiecewiseLinearSqrt(xys XYs) (*PiecewiseLinearSqrt, error)

NewPiecewiseLinearSqrt builds a piecewise linear sqrt interpolator with flat extrapolation. The input `xys` must be ordered and have unique abscissas.

func (PiecewiseLinearSqrt) Gradient

func (interp PiecewiseLinearSqrt) Gradient(x float64) float64

Gradient computes the gradient of f(x) based on piecewise linear interpolation with flat extrapolation.

Example
xys := XYs{
	{
		X: 0.0,
		Y: 1.2,
	},
	{
		X: 0.5,
		Y: 1.0,
	},
	{
		X: 1.0,
		Y: 1.4,
	},
}
interp, err := NewPiecewiseLinearSqrt(xys)
if err != nil {
	return
}
fmt.Println(interp.Gradient(1.5))
Output:
0

func (PiecewiseLinearSqrt) Value

func (interp PiecewiseLinearSqrt) Value(x float64) float64

Value compute the value of f(x) based on piecewise linear interpolation with flat extrapolation.

Example
xys := XYs{
	{
		X: 0.0,
		Y: 1.2,
	},
	{
		X: 0.5,
		Y: 1.0,
	},
	{
		X: 1.0,
		Y: 1.4,
	},
}
interp, err := NewPiecewiseLinearSqrt(xys)
if err != nil {
	return
}
fmt.Println(interp.Value(1.5))
Output:
1.4

type PiecewiseLinearThreshold

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

PiecewiseLinearThreshold is a piecewise linear interpolator that extrapolates a threshold value.

func NewPiecewiseLinearThreshold

func NewPiecewiseLinearThreshold(xys XYs) (*PiecewiseLinearThreshold, error)

NewPiecewiseLinearThreshold builds a piecewise linear interpolator with flat extrapolation. The input `xys` must be ordered and have unique abscissas.

func (PiecewiseLinearThreshold) Gradient

func (interp PiecewiseLinearThreshold) Gradient(x float64) float64

Gradient computes the gradient of f(x) based on piecewise linear interpolation with flat extrapolation.

Example
xys := XYs{
	{
		X: 0.0,
		Y: 1.2,
	},
	{
		X: 0.5,
		Y: 1.0,
	},
	{
		X: 1.0,
		Y: 1.4,
	},
}
interp, err := NewPiecewiseLinearThreshold(xys)
if err != nil {
	return
}
fmt.Println(interp.Gradient(1.5))
Output:
0

func (PiecewiseLinearThreshold) Value

func (interp PiecewiseLinearThreshold) Value(x float64) float64

Value compute the value of f(x) based on piecewise linear interpolation with flat extrapolation.

Example
xys := XYs{
	{
		X: 0.0,
		Y: 1.2,
	},
	{
		X: 0.5,
		Y: 1.0,
	},
	{
		X: 1.0,
		Y: 1.4,
	},
}
interp, err := NewPiecewiseLinearThreshold(xys)
if err != nil {
	return
}
fmt.Println(interp.Value(1.5))
Output:
1.4

type XY

type XY struct {
	X float64
	Y float64
}

XY represent a 2-dimensional data point.

type XYs

type XYs []XY

XYs represents a slice of data points.

func (XYs) Interval

func (xys XYs) Interval(x float64) (XY, XY)

Interval returns the bracketing points around x for a given XYs. The `xys` must be ordered and have unique abscissas.

Example
xys := XYs{
	{
		X: 0.0,
		Y: 1.2,
	},
	{
		X: 0.5,
		Y: 1.0,
	},
	{
		X: 1.0,
		Y: 1.4,
	},
}
fmt.Println(xys.Interval(0.75))
Output:
{0.5 1} {1 1.4}

Jump to

Keyboard shortcuts

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