• Stars
    star
    26
  • Rank 899,552 (Top 19 %)
  • Language
    Go
  • License
    MIT License
  • Created over 5 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

tiny linear interpolation library for go

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

More Repositories

1

jp

dead simple terminal plots from JSON data. single binary, no dependencies. linux, osx, windows.
Go
1,302
star
2

yeetgif

gif effects CLI. single binary, no dependencies. linux, osx, windows. #1 workplace productivity booster. #yeetgif #eggplant #golang
Go
551
star
3

tj

stdin line timestamps. single binary, no dependencies. osx & linux & windows. plays well with jq.
Go
230
star
4

http-file-server

tiny portable HTTP file server. single binary, no dependencies. linux, osx, windows. #golang
Go
199
star
5

ok

OCaml implementation of the K 2.0 array programming language
OCaml
80
star
6

regex-builder

Write regular expressions in pure Java
Java
61
star
7

flagvar

A collection of CLI argument types for the Go `flag` package.
Go
43
star
8

slack-emoji-upload

Slack emoji uploader, CLI. single binary, no dependencies. linux, osx, windows.
Go
31
star
9

testing-with-gomock

Supporting materials for the blog post "Testing with GoMock"
Go
26
star
10

jira-project-export

Export issues and metadata for a single JIRA project as JSON.
Shell
19
star
11

subst

substitutes $variables in text. single binary, no dependencies. linux, osx, windows.
Go
14
star
12

render

A flexible go-template renderer.
Go
13
star
13

ezrgif

Easy GIF effects for Slack/Discord emoji, from the command line
Shell
13
star
14

sshtunnel

library providing a convenient dialer for SSH-tunneled TCP and Unix domain socket connections. #golang
Go
13
star
15

telegram-emoji-reactions-bot

Telegram bot for Slack-like emoji reactions. Single binary, no dependencies. linux, osx, windows. #golang #cli #telegram #slack
Go
11
star
16

image-palette-tools

extract palettes from images / cluster images by their palettes
Go
11
star
17

ocaml-rename

An .mli-aware command-line renaming tool for OCaml projects
OCaml
11
star
18

telegram-sticker-thief-bot

Telegram bot to clone/re-combine sticker packs. Single binary, no dependencies. linux, osx, windows. #golang #cli #telegram
Go
9
star
19

ocaml-redis-protocol

Redis Serialization Protocol (RESP) for OCaml
OCaml
9
star
20

FsTypeVis

Simple visualisation for F# types
F#
8
star
21

with-ssh-docker-socket

access a remote Docker daemon over SSH. single binary, no dependencies. linux, osx, windows.
Go
7
star
22

digitalproductid

decodes the Windows 7/8/10 license key from the registry
Go
7
star
23

tcp-time

measures TCP connection durations to a given target, prints JSON
Go
7
star
24

versions

command-line version operations. single static binary. osx, linux, windows.
Go
5
star
25

docker-awscli

Docker image for the AWS CLI (automatically tracks AWS CLI releases)
Dockerfile
4
star
26

terrafile-ify

Generate Terrafiles and (optionally) re-write Terraform source to use vendored modules
Go
4
star
27

symbolic-plc

Symbolic execution for IEC 61131 PLCs using F# and Z3
F#
4
star
28

url

command-line URL parser. single binary, no dependencies. osx & linux & windows.
Go
4
star
29

0sh

very simple (sub-POSIX) shell for scripting. single binary, no dependencies. osx, linux, windows. #golang
Go
3
star
30

stdin-spinner

terminal spinner indicating whether data is coming in on stdin. single binary, no dependencies. linux, osx, windows.
Go
3
star
31

gopass-jsonapi-encode

reads JSON on stdin and writes gopass JSONAPI messages on stdout. single binary, no dependencies. linux, osx, windows. #golang
Go
3
star
32

http-echo

tiny HTTP server that echoes the requests it receives. single binary, no dependencies. linux, osx, windows.
Go
3
star
33

caretStack

R package for stacking caret models
R
2
star
34

docker-awscli-with-assume-role

Docker image for the AWS CLI with a wrapper that automatically assumes an IAM role (upstream AWS CLI releases are tracked automatically)
Shell
2
star
35

watchfs

docker-aware nodemon-like filesystem event watcher. single binary, no dependencies. linux, osx, windows.
Go
1
star
36

rc

A rule checker library for Java. Checks sets of rules for completeness, overlap, and constraint satisfaction.
Java
1
star
37

docker-awsebcli-with-assume-role

Docker image for the AWS EB CLI with a wrapper that automatically assumes an IAM role (upstream AWS EB CLI releases are tracked automatically)
Shell
1
star
38

http-subst-server

tiny static file server, with dead simple templates (just $VARIABLES). single binary, no dependencies. linux, osx, windows. #golang
Go
1
star
39

docker-awsebcli

Docker image for the AWS Elastic Beanstalk CLI (automatically tracks AWS EB CLI releases)
Dockerfile
1
star
40

slack-wipe

deletes all your messages and/or files in a single Slack channel. single binary, no dependencies. linux, osx, windows. #golang
Go
1
star
41

csv-dl

csv-dl reads CSV on stdin, and downloads linked files. single binary, no dependencies. linux, osx, windows. #golang
Go
1
star