Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sgreben/piecewiselinear
tiny linear interpolation library for go
https://github.com/sgreben/piecewiselinear
golang linear linear-interpolation piecewise tiny
Last synced: 12 days ago
JSON representation
tiny linear interpolation library for go
- Host: GitHub
- URL: https://github.com/sgreben/piecewiselinear
- Owner: sgreben
- License: mit
- Created: 2018-10-21T13:19:44.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-12-10T11:12:54.000Z (11 months ago)
- Last Synced: 2024-10-04T12:52:33.519Z (about 1 month ago)
- Topics: golang, linear, linear-interpolation, piecewise, tiny
- Language: Go
- Homepage: https://pkg.go.dev/github.com/sgreben/piecewiselinear
- Size: 18.6 KB
- Stars: 26
- Watchers: 6
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - piecewiselinear - Tiny linear interpolation library. (Science and Data Analysis / HTTP Clients)
- zero-alloc-awesome-go - piecewiselinear - Tiny linear interpolation library. (Science and Data Analysis / HTTP Clients)
- awesome-go - piecewiselinear - tiny linear interpolation library for go (factored out from ) - ★ 5 (Science and Data Analysis)
- awesome-go-extra - piecewiselinear - 10-21T13:19:44Z|2020-12-01T19:30:38Z| (Science and Data Analysis / HTTP Clients)
README
# piecewiselinear
[![](https://godoc.org/github.com/sgreben/piecewiselinear?status.svg)](http://godoc.org/github.com/sgreben/piecewiselinear) [![](https://goreportcard.com/badge/github.com/sgreben/piecewiselinear)](https://goreportcard.com/report/github.com/sgreben/piecewiselinear)
A tiny library for linear interpolation. `O(log(N))` per evaluation for `N` control points (and [`O(1)` in a special case](#fast-special-case)).
```go
import "github.com/sgreben/piecewiselinear"
```- [Get it](#get-it)
- [Use it](#use-it)
- [Fast special case](#fast-special-case)
- [Benchmarks](#benchmarks)## Get it
```sh
go get -u "github.com/sgreben/piecewiselinear"
```## Use it
```go
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):
```go
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
```