piecewiselinear

package module
Version: v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2020 License: MIT Imports: 1 Imported by: 5

README

piecewiselinear

gocover.io Build

A tiny library for linear interpolation. O(log(N)) per evaluation for N control points.

import "github.com/sgreben/piecewiselinear"

Get it

go get -u "github.com/sgreben/piecewiselinear"

Use it

import "github.com/sgreben/piecewiselinear"

func main() {
    f := piecewiselinear.Function{Y:[]float64{0,1,0}} // range: "hat" function
    f.X = piecewiselinear.Span(0, 1, len(f.Y)) // domain: equidistant points along X axis
    fmt.Println(
		f.At(0), // f.At(x) evaluates f at x
		f.At(0.25),
		f.At(0.5),
		f.At(0.75),
		f.At(1.0),
		f.At(123.0),  // outside its domain X the function is constant 0
		f.At(-123.0), //
	)
    // Output:
    // 0 0.5 1 0.5 0 0 0
}

Documentation

Overview

Package piecewiselinear is a tiny library for linear interpolation.

Example
f := Function{Y: []float64{0, 1, 0}} // range: "hat" function
f.X = Span(0, 1, len(f.Y))           // domain: equidistant points along X axis
fmt.Println(
	f.At(0), // f.At(x) evaluates f at x
	f.At(0.25),
	f.At(0.5),
	f.At(0.75),
	f.At(1.0),
	f.At(123.0),  // outside its domain X the function is constant 0
	f.At(-123.0), //
)
Output:

0 0.5 1 0.5 0 0 0

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Span

func Span(min, max float64, nPoints int) []float64

Span generates `nPoints` equidistant points spanning [min,max]

Types

type Function

type Function struct {
	X []float64
	Y []float64
}

Function is a piecewise-linear 1-dimensional function

func (Function) Area

func (f Function) Area() (area float64)

Area returns the definite integral of the function on its domain X.

Time complexity: O(N), where N is the number of points. Space complexity: O(1)

func (Function) AreaUpTo

func (f Function) AreaUpTo(x float64) (area float64)

AreaUpTo returns the definite integral of the function on its domain X intersected with [-Inf, x].

Time complexity: O(N), where N is the number of points. Space complexity: O(1)

func (Function) At

func (f Function) At(x float64) float64

At returns the function's value at the given point. Outside its domain X, the function is constant at 0.

The function's X and Y slices are expected to be the same legnth. The length property is _not_ verified. The function's X slice is expected to be sorted in ascending order. The sortedness property is _not_ verified.

Time complexity: O(log(N)), where N is the number of points. Space complexity: O(1)

func (Function) IsInterpolatedAt

func (f Function) IsInterpolatedAt(x float64) bool

IsInterpolatedAt returns true if x is within the given range of points, false if outside of that range

Jump to

Keyboard shortcuts

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