Skip to content

Commit 3ba8693

Browse files
committed
start cubic init
1 parent ba4548e commit 3ba8693

File tree

2 files changed

+80
-6
lines changed

2 files changed

+80
-6
lines changed

src/one/strategies.rs

+23
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,29 @@ where
3636
}
3737
}
3838

39+
impl<D> Strategy1D<D> for Cubic<D::Elem>
40+
where
41+
D: Data + RawDataClone + Clone,
42+
D::Elem: Num + PartialOrd + Copy + Default + Debug,
43+
{
44+
fn init(&self, data: &InterpData1D<D>) -> Result<(), ValidateError> {
45+
Ok(())
46+
}
47+
48+
fn interpolate(
49+
&self,
50+
data: &InterpData1D<D>,
51+
point: &[<D>::Elem; 1],
52+
) -> Result<<D>::Elem, InterpolateError> {
53+
todo!()
54+
}
55+
56+
/// Returns `true`
57+
fn allow_extrapolate(&self) -> bool {
58+
true
59+
}
60+
}
61+
3962
impl<D> Strategy1D<D> for Nearest
4063
where
4164
D: Data + RawDataClone + Clone,

src/strategy.rs

+57-6
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,65 @@ pub fn find_nearest_index<T: PartialOrd>(arr: ArrayView1<T>, target: &T) -> usiz
207207
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
208208
pub struct Linear;
209209

210-
// TODO: `pub struct Quadratic;`
211-
// Maybe `pub struct Polynomial(usize);` as well?
212-
// with `pub type Quadratic = Polynomial(2)` and `pub type Cubic = Polynomial(3)`
213-
214210
/// Cubic spline interpolation: TODO
215-
#[derive(Debug, Clone, PartialEq)]
211+
#[derive(Clone, Debug, Default, PartialEq)]
216212
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
217-
pub struct Cubic;
213+
pub struct Cubic<T: Default> {
214+
pub boundary_cond: CubicBC<T>,
215+
pub coeffs: ArrayD<T>,
216+
}
217+
218+
impl<T> Cubic<T>
219+
where
220+
T: Default,
221+
{
222+
pub fn new(bc: CubicBC<T>) -> Self {
223+
Self {
224+
boundary_cond: bc,
225+
coeffs: <ArrayD<T> as Default>::default(),
226+
}
227+
}
228+
229+
pub fn natural() -> Self {
230+
Self::new(CubicBC::Natural)
231+
}
232+
233+
pub fn clamped(a: T, b: T) -> Self {
234+
Self::new(CubicBC::Clamped(a, b))
235+
}
236+
237+
pub fn not_a_knot() -> Self {
238+
Self::new(CubicBC::NotAKnot)
239+
}
240+
241+
pub fn periodic() -> Self {
242+
Self::new(CubicBC::Periodic)
243+
}
244+
245+
pub fn solve_coeffs(&mut self) {
246+
match &self.boundary_cond {
247+
CubicBC::Natural => {
248+
todo!()
249+
}
250+
_ => todo!(),
251+
}
252+
}
253+
}
254+
255+
/// Cubic boundary conditions.
256+
#[derive(Copy, Clone, Debug, Default, PartialEq)]
257+
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
258+
pub enum CubicBC<T> {
259+
/// Second derivatives at endpoints are 0, thus extrapolation is linear.
260+
// https://www.math.ntnu.no/emner/TMA4215/2008h/cubicsplines.pdf
261+
#[default]
262+
Natural,
263+
/// Specific first derivatives at endpoints.
264+
Clamped(T, T),
265+
NotAKnot,
266+
// https://math.ou.edu/~npetrov/project-5093-s11.pdf
267+
Periodic,
268+
}
218269

219270
/// Nearest value interpolation: <https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation>
220271
///

0 commit comments

Comments
 (0)