Skip to content

Commit ffcfd65

Browse files
committed
impl specila handling of cubic spline when len==3
1 parent 3b84cc2 commit ffcfd65

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

src/interp1d/strategies/cubic_spline.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -469,20 +469,33 @@ where
469469

470470
// apply boundary conditions
471471
match (boundary.specialize(), len) {
472-
(InternalBoundary::Periodic, _) => {
473-
let first = data.index_axis(AX0, 0);
474-
let last = data.index_axis(AX0, len - 1);
475-
if first != last {
472+
(InternalBoundary::Periodic, 3) => {
473+
let y0 = data.index_axis(AX0, 0);
474+
let y2 = data.index_axis(AX0, 2);
475+
if y0 != y2 {
476476
if data.ndim() == 1 {
477477
return Err(BuilderError::ValueError(format!("for periodic boundary condition the first and last value must be equal. First: {:?}, last: {:?}", data.first().unwrap_or_else(||unreachable!()), data.last().unwrap_or_else(||unreachable!()))));
478478
} else {
479-
return Err(BuilderError::ValueError(format!("for periodic boundary condition the first and last value must be equal. First: {first:?}, last: {last:?}")));
479+
return Err(BuilderError::ValueError(format!("for periodic boundary condition the first and last value must be equal. First: {y0:?}, last: {y2:?}")));
480480
}
481481
}
482482

483-
if len == 3 {
484-
todo!();
485-
return Ok(());
483+
let y1 = data.index_axis(AX0, 1);
484+
let slope0: Array<T, _D::Smaller> = (&y1 - &y0) / dx0;
485+
let slope1: Array<T, _D::Smaller> = (&y2 - &y1) / dx1;
486+
k.assign(&((slope0 / dx0 + slope1 / dx1) / (one / dx0 + one / dx1)));
487+
return Ok(());
488+
}
489+
490+
(InternalBoundary::Periodic, _) => {
491+
let y0 = data.index_axis(AX0, 0);
492+
let y_1 = data.index_axis(AX0, len - 1);
493+
if y0 != y_1 {
494+
if data.ndim() == 1 {
495+
return Err(BuilderError::ValueError(format!("for periodic boundary condition the first and last value must be equal. First: {:?}, last: {:?}", data.first().unwrap_or_else(||unreachable!()), data.last().unwrap_or_else(||unreachable!()))));
496+
} else {
497+
return Err(BuilderError::ValueError(format!("for periodic boundary condition the first and last value must be equal. First: {y0:?}, last: {y_1:?}")));
498+
}
486499
}
487500

488501
// due to the preriodicity we need to solve one less equation
@@ -496,7 +509,6 @@ where
496509
a_mid[0] = two * (dx_1 + dx0);
497510
a_up[0] = dx_1;
498511

499-
let y0 = data.index_axis(AX0, 0);
500512
let y1 = data.index_axis(AX0, 1);
501513
let slope0: Array<T, _D::Smaller> = (&y1 - &y0) / dx0;
502514

0 commit comments

Comments
 (0)