csaps-js 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 mnaoizy
4
+
5
+ This project is a TypeScript port of the Python "csaps" library
6
+ (https://github.com/espdev/csaps), Copyright (c) 2017 Eugene Prilepin,
7
+ which is also distributed under the MIT License.
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ of this software and associated documentation files (the "Software"), to deal
11
+ in the Software without restriction, including without limitation the rights
12
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ copies of the Software, and to permit persons to whom the Software is
14
+ furnished to do so, subject to the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be included in all
17
+ copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,192 @@
1
+ # csaps-js
2
+
3
+ **Cubic spline approximation (smoothing) for Node.js and the browser.**
4
+
5
+ A dependency-free TypeScript port of the Python [`csaps`](https://github.com/espdev/csaps)
6
+ library. It computes a **cubic smoothing spline** that balances closeness to the
7
+ data against smoothness of the curve, for **univariate**, **multivariate** and
8
+ **N-D gridded** data.
9
+
10
+ The smoothing spline `f` minimizes
11
+
12
+ ```
13
+ p · Σ wᵢ (yᵢ − f(xᵢ))² + (1 − p) · ∫ f''(x)² dx
14
+ ```
15
+
16
+ where the smoothing parameter `p ∈ [0, 1]`:
17
+
18
+ - `p = 0` → the weighted least-squares **straight line**,
19
+ - `p = 1` → the natural cubic spline **interpolant** (passes through every point),
20
+ - in between → a smooth approximation. If you omit `p`, a sensible value is
21
+ computed automatically from the data.
22
+
23
+ Results match the reference Python implementation to within floating-point
24
+ tolerance (verified by a test suite generated from `csaps` 1.3.3).
25
+
26
+ ## Features
27
+
28
+ - ✅ Univariate data smoothing (`x: number[]`, `y: number[]`)
29
+ - ✅ Multivariate smoothing (many curves sharing the same `x`)
30
+ - ✅ N-D gridded data smoothing (2-D surfaces, 3-D volumes, …)
31
+ - ✅ Automatic or manual smoothing parameter; optional `normalizedsmooth`
32
+ - ✅ Per-site weights
33
+ - ✅ Derivative evaluation (`nu`) and optional extrapolation
34
+ - ✅ Zero runtime dependencies — works in Node.js, bundlers and the browser
35
+ - ✅ ESM + CommonJS + IIFE builds, full TypeScript types
36
+
37
+ ## Installation
38
+
39
+ ```bash
40
+ npm install csaps-js
41
+ ```
42
+
43
+ ## Quick start
44
+
45
+ ```ts
46
+ import { csaps } from 'csaps-js';
47
+
48
+ const x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
49
+ const y = [2.3, 1.1, 3.8, 3.1, 5.2, 4.9, 7.0, 6.2, 8.1, 9.4];
50
+
51
+ // Smooth and evaluate at new sites in one call:
52
+ const xi = Array.from({ length: 50 }, (_, i) => 1 + (i * 9) / 49);
53
+ const yi = csaps(x, y, xi, { smooth: 0.7 }); // number[]
54
+
55
+ // Let csaps choose the smoothing parameter for you:
56
+ const { values, smooth } = csaps(x, y, xi);
57
+ console.log('auto smoothing parameter:', smooth);
58
+
59
+ // Or build a reusable spline object and evaluate later:
60
+ const spline = csaps(x, y, { smooth: 0.7 });
61
+ const a = spline.evaluate([2.5, 5.5]); // values
62
+ const slope = spline.evaluate([2.5], { nu: 1 }); // first derivative
63
+ ```
64
+
65
+ ## Usage
66
+
67
+ ### `csaps(x, y, xi?, options?)`
68
+
69
+ The convenience function. Its return value depends on the arguments:
70
+
71
+ | Call | Returns |
72
+ | --------------------------------- | ------------------------------------------------------------- |
73
+ | `csaps(x, y, options?)` | a spline object (`.evaluate(...)`, `.smooth`, `.spline`) |
74
+ | `csaps(x, y, xi, { smooth })` | the smoothed values at `xi` |
75
+ | `csaps(x, y, xi)` (auto smooth) | `{ values, smooth }` (an `AutoSmoothingResult`) |
76
+
77
+ **Options**
78
+
79
+ - `smooth` — smoothing parameter in `[0, 1]`. Omit for automatic selection.
80
+ For gridded data this may be a per-axis array (entries may be `null` to
81
+ auto-select that axis).
82
+ - `weights` — per-site weights (a vector for univariate data, one vector per
83
+ axis for gridded data).
84
+ - `axis` — for 2-D `y`, the axis that varies with `x` (default `-1`, the last).
85
+ - `normalizedsmooth` — when `true`, rescales `smooth` so the result is
86
+ invariant to the `x` range and less sensitive to non-uniform spacing/weights.
87
+
88
+ ### Univariate
89
+
90
+ ```ts
91
+ import { csaps, CubicSmoothingSpline } from 'csaps-js';
92
+
93
+ const spline = new CubicSmoothingSpline(x, y, { smooth: 0.85 });
94
+ const yi = spline.evaluate(xi); // values
95
+ const d1 = spline.evaluate(xi, { nu: 1 }); // 1st derivative
96
+ const inside = spline.evaluate([-100, 100], { extrapolate: false }); // [NaN, NaN]
97
+ console.log(spline.smooth); // effective smoothing parameter
98
+ ```
99
+
100
+ ### Multivariate (many curves, shared `x`)
101
+
102
+ `y` is a 2-D array of shape `[numCurves][x.length]`; the result has the same
103
+ shape `[numCurves][xi.length]`.
104
+
105
+ ```ts
106
+ const y2 = [
107
+ [0, 1, 4, 9, 16], // curve 1
108
+ [0, 1, 8, 27, 64], // curve 2
109
+ ];
110
+ const yi = csaps([0, 1, 2, 3, 4], y2, [0, 1, 2, 3, 4], { smooth: 0.9 });
111
+ // yi: number[][] of shape [2][5]
112
+ ```
113
+
114
+ ### N-D gridded data
115
+
116
+ Pass a **list of site vectors** as `x` (one per dimension) and an N-D array as
117
+ `y`. Detection is automatic: if `x` is an array of arrays, gridded smoothing is
118
+ used.
119
+
120
+ ```ts
121
+ import { csaps, NdGridCubicSmoothingSpline } from 'csaps-js';
122
+
123
+ const xg = [
124
+ [0, 1, 2, 3, 4], // axis 0 sites
125
+ [0, 1, 2, 3, 4, 5], // axis 1 sites
126
+ ];
127
+ // z[i][j] = f(xg[0][i], xg[1][j])
128
+ const z = xg[0].map((xi) => xg[1].map((yj) => Math.sin(xi) * Math.cos(yj)));
129
+
130
+ const zi = csaps(xg, z, [
131
+ [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4],
132
+ [0, 1, 2, 3, 4, 5],
133
+ ], { smooth: 0.9 });
134
+ // zi: number[][] (a smoothed surface on the evaluation grid)
135
+
136
+ // Per-axis smoothing parameters are also supported:
137
+ const surface = new NdGridCubicSmoothingSpline(xg, z, { smooth: [0.5, 0.95] });
138
+ console.log(surface.smooth); // [0.5, 0.95]
139
+ ```
140
+
141
+ 3-D (and higher) grids work the same way: provide three site vectors and a
142
+ `number[][][]` for `y`.
143
+
144
+ ## Browser usage
145
+
146
+ ### As a module (bundlers / modern browsers)
147
+
148
+ ```ts
149
+ import { csaps } from 'csaps-js';
150
+ ```
151
+
152
+ ### Via a `<script>` tag (CDN)
153
+
154
+ The IIFE build exposes a global `csapsjs`:
155
+
156
+ ```html
157
+ <script src="https://unpkg.com/csaps-js"></script>
158
+ <script>
159
+ const yi = csapsjs.csaps([1, 2, 3, 4], [1, 3, 2, 5], [1, 2, 3, 4], { smooth: 0.8 });
160
+ console.log(yi);
161
+ </script>
162
+ ```
163
+
164
+ ## API surface
165
+
166
+ - `csaps(x, y, xi?, options?)` — the shortcut function.
167
+ - `CubicSmoothingSpline` — univariate / multivariate spline class.
168
+ - `NdGridCubicSmoothingSpline` — N-D gridded spline class.
169
+ - `PPoly` — the underlying piecewise-polynomial evaluator.
170
+ - Types: `CsapsOptions`, `AutoSmoothingResult`, `CubicSmoothingSplineOptions`,
171
+ `NdGridCubicSmoothingSplineOptions`, `EvaluateOptions`, `NdEvaluateOptions`.
172
+
173
+ Each spline exposes `.smooth` (the effective parameter) and `.spline`
174
+ (breakpoints and raw piecewise-polynomial coefficients).
175
+
176
+ ## Notes & differences from Python `csaps`
177
+
178
+ - Numerics follow de Boor's algorithm exactly; the sparse solve is replaced by
179
+ a banded `LDLᵀ` factorization of the (symmetric, positive-definite,
180
+ pentadiagonal) system.
181
+ - The univariate `axis` option supports 1-D `y`, and 2-D `y` with `axis` 0 or
182
+ -1/1. Arbitrary N-D multivariate `y` along an arbitrary axis is not handled
183
+ (use the gridded API or reshape your data).
184
+ - Evaluation follows `scipy.interpolate.PPoly` semantics: half-open intervals
185
+ `[x[i], x[i+1])` (last closed), local power basis, first/last-piece
186
+ extrapolation unless `extrapolate: false`.
187
+
188
+ ## License
189
+
190
+ MIT. This is a port of the MIT-licensed Python
191
+ [`csaps`](https://github.com/espdev/csaps) by Eugene Prilepin. See
192
+ [LICENSE](./LICENSE).