bsplx 0.1.0__py3-none-any.whl
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.
- bsplx/__init__.py +167 -0
- bsplx-0.1.0.dist-info/METADATA +14 -0
- bsplx-0.1.0.dist-info/RECORD +5 -0
- bsplx-0.1.0.dist-info/WHEEL +4 -0
- bsplx-0.1.0.dist-info/licenses/LICENSE +21 -0
bsplx/__init__.py
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import jax, jax.numpy as jnp
|
|
2
|
+
from functools import partial
|
|
3
|
+
|
|
4
|
+
from jaxtyping import jaxtyped, Array, Float
|
|
5
|
+
from beartype import beartype
|
|
6
|
+
|
|
7
|
+
@jax.jit(static_argnames=('i', 'd'))
|
|
8
|
+
@jaxtyped(typechecker=beartype)
|
|
9
|
+
def cxdb(x: Float[Array, ''], knots: Float[Array, 'k'], i: int, d: int) -> Float[Array, '']:
|
|
10
|
+
'''Cox-De-Boor (JIT Compatible)
|
|
11
|
+
Args:
|
|
12
|
+
x: input `[]`
|
|
13
|
+
knots: knots `[k]`
|
|
14
|
+
i: basis index `int`
|
|
15
|
+
d: degree `int`
|
|
16
|
+
Returns:
|
|
17
|
+
`[]` evaluation of the basis function in `x`
|
|
18
|
+
'''
|
|
19
|
+
|
|
20
|
+
if d == 0:
|
|
21
|
+
is_last_interval = (i == len(knots) - 2)
|
|
22
|
+
in_interval = (knots[i] <= x) & (x < knots[i+1])
|
|
23
|
+
at_right_end = (x == knots[-1])
|
|
24
|
+
return jnp.where(in_interval | (is_last_interval & at_right_end), 1.0, 0.0)
|
|
25
|
+
|
|
26
|
+
a, b = knots[i+d] - knots[i], knots[i+d+1] - knots[i+1]
|
|
27
|
+
|
|
28
|
+
left = jnp.where(a != 0, (x - knots[i]) / jnp.where(a != 0, a, 1.0) * cxdb(x, knots, i, d-1), 0.0)
|
|
29
|
+
right = jnp.where(b != 0, (knots[i+d+1] - x) / jnp.where(b != 0, b, 1.0) * cxdb(x, knots, i+1, d-1), 0.0)
|
|
30
|
+
|
|
31
|
+
return left + right
|
|
32
|
+
|
|
33
|
+
@jax.jit(static_argnames=('i', 'd'))
|
|
34
|
+
@jaxtyped(typechecker=beartype)
|
|
35
|
+
def dcxdb(x: Float[Array, ''], knots: Float[Array, 'k'], i: int, d: int) -> Float[Array, '']:
|
|
36
|
+
'''Derivative of Cox-De-Boor
|
|
37
|
+
Args:
|
|
38
|
+
x: input `[]`
|
|
39
|
+
knots: knots `[k]`
|
|
40
|
+
i: basis index `int`
|
|
41
|
+
d: degree `int`
|
|
42
|
+
Returns:
|
|
43
|
+
`[]` evaluation of the derivative basis function in `x`
|
|
44
|
+
'''
|
|
45
|
+
a, b = knots[i+d] - knots[i], knots[i+d+1] - knots[i+1]
|
|
46
|
+
left = jnp.where(a != 0, d / jnp.where(a != 0, a, 1.0) * cxdb(x, knots, i, d-1), 0.0)
|
|
47
|
+
right = jnp.where(b != 0, d / jnp.where(b != 0, b, 1.0) * cxdb(x, knots, i+1, d-1), 0.0)
|
|
48
|
+
return left - right
|
|
49
|
+
|
|
50
|
+
@jax.jit(static_argnames='d')
|
|
51
|
+
@jaxtyped(typechecker=beartype)
|
|
52
|
+
def repeat_knots(knots: Float[Array, 'k'], d: int) -> Float[Array, 'K']:
|
|
53
|
+
'''Repeat boundary knots d times on each end.
|
|
54
|
+
Args:
|
|
55
|
+
knots: knots `[k]`
|
|
56
|
+
d: degree `int`
|
|
57
|
+
Returns:
|
|
58
|
+
`[k+d+d]` knots with repeated start and end
|
|
59
|
+
'''
|
|
60
|
+
left = jnp.repeat(knots[0], d)
|
|
61
|
+
right = jnp.repeat(knots[-1], d)
|
|
62
|
+
return jnp.concat([left, knots, right])
|
|
63
|
+
|
|
64
|
+
@jax.jit(static_argnames='d')
|
|
65
|
+
@jaxtyped(typechecker=beartype)
|
|
66
|
+
def design_matrix_row(x: Float[Array, ''], knots: Float[Array, 'k'], d: int) -> Float[Array, 'n']:
|
|
67
|
+
'''Evaluate the basis functions for a single input.
|
|
68
|
+
Args:
|
|
69
|
+
x: point `[]`
|
|
70
|
+
knots: knots `[k]`
|
|
71
|
+
d: degree `int`
|
|
72
|
+
Returns:
|
|
73
|
+
`[n]` evaluation of `x` in all basis functions
|
|
74
|
+
'''
|
|
75
|
+
x = jnp.where(x >= knots[-1], knots[-1] - 1e-6, x)
|
|
76
|
+
n_basis = len(knots) - d - 1
|
|
77
|
+
return jnp.stack([cxdb(x, knots, i, d) for i in range(n_basis)])
|
|
78
|
+
|
|
79
|
+
@jax.jit(static_argnames='d')
|
|
80
|
+
@jaxtyped(typechecker=beartype)
|
|
81
|
+
def design_dmatrix_row(x: Float[Array, ''], knots: Float[Array, 'k'], d: int) -> Float[Array, 'n']:
|
|
82
|
+
'''Evaluate the basis function derivatives for a single input.
|
|
83
|
+
Args:
|
|
84
|
+
x: point `[]`
|
|
85
|
+
knots: knots `[k]`
|
|
86
|
+
d: degree `int`
|
|
87
|
+
Returns:
|
|
88
|
+
`[n]` evaluation of `x` in all derivative basis functions
|
|
89
|
+
'''
|
|
90
|
+
x = jnp.where(x >= knots[-1], knots[-1] - 1e-6, x)
|
|
91
|
+
x = jnp.where(x >= knots[-1], knots[-1] - 1e-6, x)
|
|
92
|
+
n_basis = len(knots) - d - 1
|
|
93
|
+
return jnp.stack([dcxdb(x, knots, i, d) for i in range(n_basis)])
|
|
94
|
+
|
|
95
|
+
@jax.jit(static_argnames='d')
|
|
96
|
+
@jaxtyped(typechecker=beartype)
|
|
97
|
+
def design_matrix(x: Float[Array, 'N'], knots: Float[Array, 'k'], d: int) -> Float[Array, 'N n']:
|
|
98
|
+
'''Evaluate the basis functions for N inputs.
|
|
99
|
+
Args:
|
|
100
|
+
x: points `[N]`
|
|
101
|
+
knots: knots `[k]`
|
|
102
|
+
d: degree `int`
|
|
103
|
+
Returns:
|
|
104
|
+
`[N n]` design matrix for inputs `x`
|
|
105
|
+
'''
|
|
106
|
+
return jax.vmap(partial(design_matrix_row, knots=knots, d=d))(x)
|
|
107
|
+
|
|
108
|
+
@jax.jit(static_argnames='d')
|
|
109
|
+
@jaxtyped(typechecker=beartype)
|
|
110
|
+
def design_dmatrix(x: Float[Array, 'N'], knots: Float[Array, 'k'], d: int) -> Float[Array, 'N n']:
|
|
111
|
+
'''Evaluate the basis function derivatives for N inputs.
|
|
112
|
+
Args:
|
|
113
|
+
x: points `[N]`
|
|
114
|
+
knots: knots `[k]`
|
|
115
|
+
d: degree `int`
|
|
116
|
+
Returns:
|
|
117
|
+
`[N n]` derivative design matrix for inputs `x`
|
|
118
|
+
'''
|
|
119
|
+
return jax.vmap(partial(design_dmatrix_row, knots=knots, d=d))(x)
|
|
120
|
+
|
|
121
|
+
@jax.jit(static_argnames='d')
|
|
122
|
+
@jaxtyped(typechecker=beartype)
|
|
123
|
+
def fit_bspline_coefs(x: Float[Array, 'N'], y: Float[Array, 'N'], knots: Float[Array, 'k'], d: int) -> Float[Array, 'n']:
|
|
124
|
+
'''Solve the least-squares problem for the B-splines coefficients.
|
|
125
|
+
Args:
|
|
126
|
+
x: points `[N]`
|
|
127
|
+
y: targets `[N]`
|
|
128
|
+
knots: knots `[k]`
|
|
129
|
+
d: degree `int`
|
|
130
|
+
Returns:
|
|
131
|
+
`[n]` fitted coefficients
|
|
132
|
+
'''
|
|
133
|
+
B = design_matrix(x, knots, d)
|
|
134
|
+
return jnp.linalg.lstsq(B, y)[0]
|
|
135
|
+
|
|
136
|
+
@jax.jit(static_argnames='d')
|
|
137
|
+
@jaxtyped(typechecker=beartype)
|
|
138
|
+
def fit_bspline_dcoefs(x: Float[Array, 'N'], y: Float[Array, 'N'], knots: Float[Array, 'k'], d: int) -> Float[Array, 'n']:
|
|
139
|
+
'''Solve the least-squares problem for the B-splines derivatives coefficients.
|
|
140
|
+
Args:
|
|
141
|
+
x: points `[N]`
|
|
142
|
+
y: targets `[N]`
|
|
143
|
+
knots: knots `[k]`
|
|
144
|
+
d: degree `int`
|
|
145
|
+
Returns:
|
|
146
|
+
`[n]` fitted derivative coefficients
|
|
147
|
+
'''
|
|
148
|
+
B = design_dmatrix(x, knots, d)
|
|
149
|
+
return jnp.linalg.lstsq(B, y)[0]
|
|
150
|
+
|
|
151
|
+
@jax.jit(static_argnames='d')
|
|
152
|
+
def bspline_inference(
|
|
153
|
+
x: Float[Array, 'N'],
|
|
154
|
+
c: Float[Array, 'n'],
|
|
155
|
+
knots: Float[Array, 'k'],
|
|
156
|
+
d: int,
|
|
157
|
+
) -> Float[Array, 'N']:
|
|
158
|
+
'''Compute the B-spline inference for input `x`.
|
|
159
|
+
Args:
|
|
160
|
+
x: input `[N]`
|
|
161
|
+
c: coefficients `[n]`
|
|
162
|
+
knots: knots `[k]`
|
|
163
|
+
d: degree `int`
|
|
164
|
+
Returns:
|
|
165
|
+
`[N]` spline(xi) for xi in x
|
|
166
|
+
'''
|
|
167
|
+
return jnp.squeeze(design_matrix(jnp.atleast_1d(x), knots, d) @ c)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: bsplx
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A minimal B-spline implementation in (JIT-compatible) Jax.
|
|
5
|
+
License-File: LICENSE
|
|
6
|
+
Requires-Python: >=3.12
|
|
7
|
+
Requires-Dist: beartype>=0.22.9
|
|
8
|
+
Requires-Dist: jax>=0.10.0
|
|
9
|
+
Requires-Dist: jaxtyping>=0.3.9
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
|
|
12
|
+
# bsplx
|
|
13
|
+
|
|
14
|
+
A simple B-splines implementation in (JIT-compatible) Jax.
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
bsplx/__init__.py,sha256=2Jhsa5lDqjmEdLvEQ7n5mmKtUlop79Zrl0HyJLD8Bms,5680
|
|
2
|
+
bsplx-0.1.0.dist-info/METADATA,sha256=b47oWkhPwOJo9R3ZSBTdwd0yB9jPqKDrHKFERLwtPNg,362
|
|
3
|
+
bsplx-0.1.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
|
|
4
|
+
bsplx-0.1.0.dist-info/licenses/LICENSE,sha256=cc3gkagBK0YOIZyIVjzsfBj0xZ3QKJOFiC0qIGwba3w,1075
|
|
5
|
+
bsplx-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Maxime Rochkoulets
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|