Skip to content

Commit 58bb8ad

Browse files
committed
update structure and documentation
1 parent 7fce14e commit 58bb8ad

File tree

5 files changed

+120
-15
lines changed

5 files changed

+120
-15
lines changed

src/interp1d.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//!
1111
//! # Strategies
1212
//! - [`Linear`] Linear interpolation strategy
13-
//! - [`CubicSpline`] Cubic spline interpolation strategy
13+
//! - [`cubic_spline`] Cubic spline interpolation strategy
1414
1515
use std::{any::TypeId, fmt::Debug, ops::Sub};
1616

@@ -30,10 +30,14 @@ use crate::{
3030
mod aliases;
3131
mod strategies;
3232
pub use aliases::*;
33-
pub use strategies::{
34-
BoundaryCondition, CubicSpline, Interp1DStrategy, Interp1DStrategyBuilder, Linear, RowBoundary,
35-
SingleBoundary,
36-
};
33+
pub use strategies::linear::Linear;
34+
pub use strategies::{Interp1DStrategy, Interp1DStrategyBuilder};
35+
36+
pub mod cubic_spline {
37+
pub use super::strategies::cubic_spline::{
38+
BoundaryCondition, CubicSpline, RowBoundary, SingleBoundary,
39+
};
40+
}
3741

3842
/// One dimensional interpolator
3943
#[derive(Debug)]

src/interp1d/strategies.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@ use num_traits::Num;
66
use super::Interp1D;
77
use crate::{BuilderError, InterpolateError};
88

9-
mod cubic_spline;
10-
mod linear;
11-
12-
pub use cubic_spline::{BoundaryCondition, CubicSpline, RowBoundary, SingleBoundary};
13-
pub use linear::Linear;
9+
pub mod cubic_spline;
10+
pub mod linear;
1411

1512
pub trait Interp1DStrategyBuilder<Sd, Sx, D>
1613
where

src/interp1d/strategies/cubic_spline.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ impl<T> SplineNum for T where
5252
/// ```
5353
/// # use ndarray_interp::*;
5454
/// # use ndarray_interp::interp1d::*;
55+
/// # use ndarray_interp::interp1d::cubic_spline::*;
5556
/// # use ndarray::*;
5657
/// # use approx::*;
5758
///
@@ -97,6 +98,7 @@ pub struct CubicSpline<T, D: Dimension> {
9798
/// ``` rust
9899
/// # use ndarray_interp::*;
99100
/// # use ndarray_interp::interp1d::*;
101+
/// # use ndarray_interp::interp1d::cubic_spline::*;
100102
/// # use ndarray::*;
101103
/// # use approx::*;
102104
///

src/lib.rs

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,114 @@
55
//! The ndarray-interp crate provides interpolation algorithms
66
//! for interpolating _n_-dimesional data.
77
//!
8-
//! 1D and 2D interpolation is supported. See the modules [interp1d] and [interp2d]
8+
//! # 1D Interpolation
9+
//! The [interp1d] module provides the [`Interp1D`](interp1d::Interp1D) interpolator
10+
//! and different interpolation strategies
11+
//!
12+
//! **1D Strategies**
13+
//! - [`interp1d::Linear`] - Linear interpolation and extrapolation
14+
//! - [`interp1d::cubic_spline`] - Cubic Spline interpolation with different boundary conditions.
15+
//!
16+
//! # 2D Interpolation
17+
//! The [interp2d] module provides the [`Interp2D`](interp2d::Interp2D) interpolator
18+
//! and different interpolation strategies
19+
//!
20+
//! **2D Strategies**
21+
//! - [`interp2d::Biliniar`] - Biliniar interpolation and extrapolation
922
//!
1023
//! # Custom interpolation strategy
1124
//! This crate defines traits to allow implementation of user
1225
//! defined interpolation algorithms.
13-
//! see the `custom_strategy.rs` example.
26+
//! A 1D interpolation strategy can be created by implementing the
27+
//! [`Interp1DStrategy`](interp1d::Interp1DStrategy) and
28+
//! [`Interp1DStrategyBuilder`](interp1d::Interp1DStrategyBuilder) traits.
29+
//! A 2D interpolation strategy can be created by implementing the
30+
//! [`Interp2DStrategy`](interp2d::Interp2DStrategy) and
31+
//! [`Interp2DStrategyBuilder`](interp2d::Interp2DStrategyBuilder) traits.
32+
//!
33+
//! See also the `custom_strategy.rs` example.
34+
//!
35+
//! # Examples
36+
//! **1D Example**
37+
//! ``` rust
38+
//! use ndarray_interp::interp1d::*;
39+
//! use ndarray::*;
40+
//!
41+
//! let data = array![0.0, 1.0, 1.5, 1.0, 0.0 ];
42+
//! let interp = Interp1DBuilder::new(data).build().unwrap();
43+
//!
44+
//! let result = interp.interp_scalar(3.5).unwrap();
45+
//! assert!(result == 0.5);
46+
//! let result = interp.interp_array(&array![0.0, 0.5, 1.5]).unwrap();
47+
//! assert!(result == array![0.0, 0.5, 1.25])
48+
//! ```
49+
//!
50+
//! **1D Example with multidimensional data**
51+
//! ```rust
52+
//! use ndarray_interp::interp1d::*;
53+
//! use ndarray::*;
54+
//!
55+
//! let data = array![
56+
//! [0.0, 1.0],
57+
//! [1.0, 2.0],
58+
//! [1.5, 2.5],
59+
//! [1.0, 2.0],
60+
//! ];
61+
//! let x = array![1.0, 2.0, 3.0, 4.0];
62+
//!
63+
//! let interp = Interp1D::builder(data)
64+
//! .strategy(Linear::new().extrapolate(true))
65+
//! .x(x)
66+
//! .build().unwrap();
67+
//!
68+
//! let result = interp.interp(0.5).unwrap();
69+
//! assert!(result == array![-0.5, 0.5]);
70+
//! let result = interp.interp_array(&array![0.5, 4.0]).unwrap();
71+
//! assert!(result == array![[-0.5, 0.5], [1.0, 2.0]]);
72+
//! ```
73+
//!
74+
//! **2D Example**
75+
//! ```rust
76+
//! use ndarray_interp::interp2d::*;
77+
//! use ndarray::*;
78+
//!
79+
//! let data = array![
80+
//! [1.0, 2.0, 2.5],
81+
//! [3.0, 4.0, 3.5],
82+
//! ];
83+
//! let interp = Interp2D::builder(data).build().unwrap();
84+
//!
85+
//! let result = interp.interp_scalar(0.0, 0.5).unwrap();
86+
//! assert!(result == 1.5);
87+
//! let result = interp.interp_array(&array![0.0, 1.0], &array![0.5, 2.0]).unwrap();
88+
//! assert!(result == array![1.5, 3.5]);
89+
//! ```
90+
//!
91+
//! **1D Example with multidimensional data**
92+
//! ``` rust
93+
//! use ndarray_interp::interp2d::*;
94+
//! use ndarray::*;
95+
//!
96+
//! let data = array![
97+
//! // ---------------------------------> y
98+
//! [[1.0, -1.0], [2.0, -2.0], [3.0, -3.0]], // |
99+
//! [[4.0, -4.0], [5.0, -5.0], [6.0, -6.0]], // |
100+
//! [[7.0, -7.0], [8.0, -8.0], [9.0, -9.0]], // V
101+
//! [[7.5, -7.5], [8.5, -8.5], [9.5, -9.5]], // x
102+
//! ];
103+
//! let x = array![1.0, 2.0, 3.0, 4.0];
104+
//! let y = array![1.0, 2.0, 3.0];
105+
//!
106+
//! let interp = Interp2D::builder(data)
107+
//! .x(x)
108+
//! .y(y)
109+
//! .build().unwrap();
14110
//!
111+
//! let result = interp.interp(1.5, 2.0).unwrap();
112+
//! assert!(result == array![3.5, -3.5]);
113+
//! let result = interp.interp_array(&array![1.5, 1.5], &array![2.0, 2.5]).unwrap();
114+
//! assert!(result == array![[3.5, -3.5],[4.0, -4.0]]);
115+
//! ```
15116
16117
use std::mem::ManuallyDrop;
17118

tests/cubic_spline_strat.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use approx::assert_relative_eq;
22
use ndarray::{array, Array1};
3-
use ndarray_interp::interp1d::{
4-
BoundaryCondition, CubicSpline, Interp1D, Interp1DBuilder, RowBoundary, SingleBoundary,
3+
use ndarray_interp::interp1d::cubic_spline::{
4+
BoundaryCondition, CubicSpline, RowBoundary, SingleBoundary,
55
};
6+
use ndarray_interp::interp1d::{Interp1D, Interp1DBuilder};
67
use ndarray_interp::{BuilderError, InterpolateError};
78

89
#[test]
@@ -202,7 +203,7 @@ fn multidim_multi_bounds() {
202203
}
203204
],];
204205
let strat = CubicSpline::new().boundary(BoundaryCondition::Individual(boundaries));
205-
let interpolator = Interp1DBuilder::new(y)
206+
let _interpolator = Interp1DBuilder::new(y)
206207
.x(x)
207208
.strategy(strat)
208209
.build()

0 commit comments

Comments
 (0)