Code Monkey home page Code Monkey logo

piecewiselinear's Introduction

piecewiselinear

A tiny library for linear interpolation. O(log(N)) per evaluation for N control points (and O(1) in a special case).

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 = []float64{0, 0.5, 1}                        // 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), //

		f.Area(),
		f.AreaUpTo(0.5),
	)
	// Output:
	// 0 0.5 1 0.5 0 0 0 0.5 0.25
}

Fast special case

If the control points are uniformly spaced, piecewiselinear.FunctionUniform is much faster (no search required):

import "github.com/sgreben/piecewiselinear"

func main() {
	f := piecewiselinear.FunctionUniform{Y: []float64{0, 1, 0}} // range: "hat" function
	f.Xmin, f.Xmax = 0, 1                                       // 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), //

		f.Area(),
		f.AreaUpTo(0.5),
	)
	// Output:
	// 0 0.5 1 0.5 0 0 0 0.5 0.25
}

Benchmarks

On an Apple M1 Pro:

  • 6ns per evaluation (.At(x)) for 10 control points
  • 320ns per evaluation for 10 million control points.

and, for FunctionUniform, 2ns per evaluation regardless of the number of control points.

goos: darwin
goarch: arm64
pkg: github.com/sgreben/piecewiselinear
BenchmarkAt4-10                 230890022                5.499 ns/op           0 B/op          0 allocs/op
BenchmarkAt8-10                 199668106                6.084 ns/op           0 B/op          0 allocs/op
BenchmarkAt10-10                192352903                6.206 ns/op           0 B/op          0 allocs/op
BenchmarkAt100-10               138742411                8.613 ns/op           0 B/op          0 allocs/op
BenchmarkAt1k-10                46360660                25.50 ns/op            0 B/op          0 allocs/op
BenchmarkAt10k-10               16649996                70.02 ns/op            0 B/op          0 allocs/op
BenchmarkAt100k-10              11696936               100.4 ns/op             0 B/op          0 allocs/op
BenchmarkAt1M-10                 8512652               140.6 ns/op             0 B/op          0 allocs/op
BenchmarkAt10M-10                3769648               320.4 ns/op             0 B/op          0 allocs/op
BenchmarkUniformAt10M-10        571224222                2.185 ns/op           0 B/op          0 allocs/op

piecewiselinear's People

Contributors

chmorgan avatar sgreben avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

piecewiselinear's Issues

Zero on start X value

Hello!
I've tried to run simple example before using in project:

f := piecewiselinear.Function{Y: []float64{100, 105, 120}}
f.X = piecewiselinear.Span(10, 16, len(f.Y))
fmt.Println(f.X)
fmt.Println(f.Y)
fmt.Println(
f.At(10), // f.At(x) evaluates f at x
f.At(11),
f.At(12),
f.At(13),
f.At(14),
f.At(15),
f.At(16),
)

And I've got the output:
[10 13 16]
[100 105 120]
0 101.66666666666667 103.33333333333334 105 110.00000000000001 115 120

Everything is fine except zero on defined X value.

Thank you

Way to indicate out of range?

Would a pull request to modify At() to return (float64, error) be accepted? In my use case I'd prefer not to rely on a magic value of 0 to indicate not found but would propose instead we returned something like 0, fmt.Errorf("value out of range") so the user could decide how to handle those cases?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.