{"id":13413757,"url":"https://github.com/sgreben/piecewiselinear","last_synced_at":"2025-08-21T12:17:30.897Z","repository":{"id":57485025,"uuid":"154010475","full_name":"sgreben/piecewiselinear","owner":"sgreben","description":"tiny linear interpolation library for go","archived":false,"fork":false,"pushed_at":"2023-12-10T11:12:54.000Z","size":19,"stargazers_count":28,"open_issues_count":0,"forks_count":4,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-05-01T09:46:01.661Z","etag":null,"topics":["golang","linear","linear-interpolation","piecewise","tiny"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/sgreben/piecewiselinear","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sgreben.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-21T13:19:44.000Z","updated_at":"2025-02-16T12:30:36.000Z","dependencies_parsed_at":"2024-06-18T22:53:36.673Z","dependency_job_id":"6053975e-ffc1-41f5-8b0c-8f708cbeab40","html_url":"https://github.com/sgreben/piecewiselinear","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/sgreben/piecewiselinear","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sgreben%2Fpiecewiselinear","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sgreben%2Fpiecewiselinear/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sgreben%2Fpiecewiselinear/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sgreben%2Fpiecewiselinear/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sgreben","download_url":"https://codeload.github.com/sgreben/piecewiselinear/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sgreben%2Fpiecewiselinear/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271478047,"owners_count":24766432,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-21T02:00:08.990Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["golang","linear","linear-interpolation","piecewise","tiny"],"created_at":"2024-07-30T20:01:48.341Z","updated_at":"2025-08-21T12:17:30.813Z","avatar_url":"https://github.com/sgreben.png","language":"Go","funding_links":[],"categories":["Science and Data Analysis","数据分析与数据科学","Relational Databases","科学与数据分析"],"sub_categories":["HTTP Clients","查询语","HTTP客户端"],"readme":"# piecewiselinear\n\n[![](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)\n\nA tiny library for linear interpolation. `O(log(N))` per evaluation for `N` control points (and [`O(1)` in a special case](#fast-special-case)).\n\n```go\nimport \"github.com/sgreben/piecewiselinear\"\n```\n\n- [Get it](#get-it)\n- [Use it](#use-it)\n  - [Fast special case](#fast-special-case)\n- [Benchmarks](#benchmarks)\n\n\n## Get it\n\n```sh\ngo get -u \"github.com/sgreben/piecewiselinear\"\n```\n\n## Use it\n\n```go\nimport \"github.com/sgreben/piecewiselinear\"\n\nfunc main() {\n    f := piecewiselinear.Function{Y:[]float64{0,1,0}} // range: \"hat\" function\n    f.X = []float64{0, 0.5, 1}                        // domain: equidistant points along X axis\n    fmt.Println(\n\t\tf.At(0),      // f.At(x) evaluates f at x\n\t\tf.At(0.25),\n\t\tf.At(0.5),\n\t\tf.At(0.75),\n\t\tf.At(1.0),\n\t\tf.At(123.0),  // outside its domain X the function is constant 0\n\t\tf.At(-123.0), //\n\n\t\tf.Area(),\n\t\tf.AreaUpTo(0.5),\n\t)\n\t// Output:\n\t// 0 0.5 1 0.5 0 0 0 0.5 0.25\n}\n```\n\n### Fast special case\n\nIf the control points are uniformly spaced, `piecewiselinear.FunctionUniform` is much faster (no search required):\n\n```go\nimport \"github.com/sgreben/piecewiselinear\"\n\nfunc main() {\n\tf := piecewiselinear.FunctionUniform{Y: []float64{0, 1, 0}} // range: \"hat\" function\n\tf.Xmin, f.Xmax = 0, 1                                       // domain: equidistant points along X axis\n\tfmt.Println(\n\t\tf.At(0), // f.At(x) evaluates f at x\n\t\tf.At(0.25),\n\t\tf.At(0.5),\n\t\tf.At(0.75),\n\t\tf.At(1.0),\n\t\tf.At(123.0),  // outside its domain X the function is constant 0\n\t\tf.At(-123.0), //\n\n\t\tf.Area(),\n\t\tf.AreaUpTo(0.5),\n\t)\n\t// Output:\n\t// 0 0.5 1 0.5 0 0 0 0.5 0.25\n}\n```\n\n## Benchmarks\n\nOn an Apple M1 Pro:\n\n- **6ns** per evaluation (`.At(x)`) for 10 control points\n- **320ns** per evaluation for 10 million control points.\n\nand, for `FunctionUniform`, **2ns** per evaluation regardless of the number of control points.\n\n```\ngoos: darwin\ngoarch: arm64\npkg: github.com/sgreben/piecewiselinear\nBenchmarkAt4-10                 230890022                5.499 ns/op           0 B/op          0 allocs/op\nBenchmarkAt8-10                 199668106                6.084 ns/op           0 B/op          0 allocs/op\nBenchmarkAt10-10                192352903                6.206 ns/op           0 B/op          0 allocs/op\nBenchmarkAt100-10               138742411                8.613 ns/op           0 B/op          0 allocs/op\nBenchmarkAt1k-10                46360660                25.50 ns/op            0 B/op          0 allocs/op\nBenchmarkAt10k-10               16649996                70.02 ns/op            0 B/op          0 allocs/op\nBenchmarkAt100k-10              11696936               100.4 ns/op             0 B/op          0 allocs/op\nBenchmarkAt1M-10                 8512652               140.6 ns/op             0 B/op          0 allocs/op\nBenchmarkAt10M-10                3769648               320.4 ns/op             0 B/op          0 allocs/op\nBenchmarkUniformAt10M-10        571224222                2.185 ns/op           0 B/op          0 allocs/op\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsgreben%2Fpiecewiselinear","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsgreben%2Fpiecewiselinear","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsgreben%2Fpiecewiselinear/lists"}