README
Math::Splines
Raku package for splines (piecewise polynomials), like, Bezier splines or B-splines.
Installation
From Zef ecosystem:
zef install Math::SplinesFrom GitHub:
zef install https://github.com/antononcube/Raku-Math-Splines.gitUsage examples
The subsections below show example usage for the currently implemented functions.
B-spline basis
use Math::Splines;
use Text::Plot;# (Any)Using positional arguments:
b-spline-basis(3, 0, 0.5)# 0.666667Using named arguments:
my $degree = 3;
my @knots = b-spline-knots(:$degree, n => 6);
b-spline-basis(:$degree, :@knots, :1index, argument => 0.5)# 0.03125Plot the basis of degree 3:
my $m = @knots.elems - $degree - 2;
my @points = (0..$m).map( -> $k { (0, 0.01 ... 1).map({ [$_, b-spline-basis(:$degree, :@knots, index => $k, argument => $_)] }) })Ā».Array;
text-list-plot(@points, width => 80, height => 20, title => 'B-spline basis')# B-spline basis
# +---+-------------+-------------+-------------+-------------+-------------+----+
# | |
# + * ā· + 1.00
# | * ā· |
# | * ā· |
# + + 0.80
# | * ā· |
# | * ā· |
# + * ā”ā”ā”ā”ā”ā” ā½ā½ā½ā½ā½ā½ā½ā½ āāāāāāāā āāāāāā ā· + 0.60
# | * ā”ā” ā”ā”ā”ā” ā½ā½ā½ ā½ā½ā½ āāā āāā āāā āā ā· |
# | ā”ā” ā”ā½ā½ ā½āāā āāāā ā· |
# + ā” * ā½ā½ ā”ā” āāā ā½ā½ā½ āā āā ā· ā + 0.40
# | ā” * ā½ā½ ā” āā ā½ā½ āā āā ā· ā |
# | ā” ** ā½ā½ ā”ā”ā” āāā ā½ā½ā½ āāā āā ā·ā· ā |
# + ā” ā½ā½ ā”āā ā½āāā āā· ā + 0.20
# | ā” ā½ā½ā½** āāāā ā”ā”ā”ā” āāā ā½ā½ā½ ā·ā·ā·āāā ā |
# | ā” ā½ā½ā½ ***āāāāāā ā”ā”ā”ā” āāāā ā½ā½ā½ā½ā½ā½ā·ā·ā· āāā ā |
# + ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā·ā½ā½ā½ā½ā½ā½ā½ā½ā½ā½āāāāā + 0.00
# | |
# +---+-------------+-------------+-------------+-------------+-------------+----+
# 0.00 0.20 0.40 0.60 0.80 1.00Functions instead of values
If the argument :arg(:argument(:$x)) of b-spline-basis is Whatever, then a function is returned:
my &bf = b-spline-basis(:3degree, :0index, arg => Whatever);
&bf(0.25)# 0.125Alternatively, the subs b-spline-basis-value and b-spline-basis-function can be used to get values and functions respectively.
Bernstein basis
my @points = (^4).map( -> $k { (0, 0.01 ... 1).map({ [$_, bernstein-basis(3, $k, $_)] }) })Ā».Array;
text-list-plot(@points, width => 80, height => 20, title => 'Bernstein basis')# Bernstein basis
# +---+-------------+-------------+-------------+-------------+-------------+----+
# | |
# + ** ā + 1.00
# | ** āā |
# | *** āāā |
# + ** ā + 0.80
# | ** āā |
# | ** āā |
# + ** āāā + 0.60
# | ** āāā |
# | *** ā”ā”ā”ā”ā”ā”ā” ā½ā½ā½ā½ā½ā½ā½ āāā |
# + ā”ā”ā”ā”ā”ā”ā” ā”ā”ā”ā”ā”ā”ā”ā½ā½ā½ā½ā½ā½ā½ā½ā½ āāāā½ā½ā½ā½ + 0.40
# | ā”ā”ā”ā” *** ā½ā½ā½ā½ā½ā½ā”ā”ā”ā”ā”ā” āāā ā½ā½ā½ā½ |
# | ā”ā”ā”ā” **ā½ā½ā½ā½ ā”ā”āāāā ā½ā½ā½ |
# + ā”ā”ā” ā½ā½ā½ā½ā½ ***** āāāāā ā”ā”ā”ā”ā” ā½ā½ + 0.20
# | ā”ā” ā½ā½ā½ā½ā½ ****āāāāāā ā”ā”ā”ā”ā” ā½ā½ |
# | ā”ā” ā½ā½ā½ā½ā½ā½ āāāāāāāāā ******** ā”ā”ā”ā”ā”ā” ā½ā½ |
# + āāāāāāāāāāāāāāāāāāāāāāāāāāā ****************ā”ā”ā”ā”ā”ā”ā”ā”ā”ā½ + 0.00
# | |
# +---+-------------+-------------+-------------+-------------+-------------+----+
# 0.00 0.20 0.40 0.60 0.80 1.00TODO
TODO Implementation
TODO Bezier curve
TODO Documentation
DONE Notebooks
DONE B-spline basis
DONE B-spline curve
TODO Demo of 2D B-spline curve with control points
References
[AAp1] Anton Antonov, Math::Fitting Raku package, (2024), GitHub/antononcube.
[AAp2] Anton Antonov, Math::Polynomial::Chebyshev Raku package, (2024), GitHub/antononcube.