Skip to content

Commit e7c7a1c

Browse files
committed
change api for using slice of ArrayView1 instead of AsArray
because it is currently impossible. AsRef<_> trait should be implemented for ArrayBase
1 parent cbb7925 commit e7c7a1c

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

src/ndg.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,12 @@ impl<'a, T, D> GridCubicSmoothingSpline<'a, T, D>
130130
D: Dimension
131131
{
132132
/// Creates `NdGridCubicSmoothingSpline` struct from the given `X` data sites and `Y` data values
133-
pub fn new<X, Y>(x: &'a [X], y: Y) -> Self
134-
where X: AsArray<'a, T> + AsRef<[T]>,
135-
Y: AsArray<'a, T, D>
133+
pub fn new<Y>(x: &[ArrayView1<'a, T>], y: Y) -> Self
134+
where
135+
Y: AsArray<'a, T, D>
136136
{
137137
GridCubicSmoothingSpline {
138-
x: x.iter().map_into().collect(),
138+
x: x.to_vec(),
139139
y: y.into(),
140140
weights: None,
141141
smooth: None,
@@ -147,18 +147,9 @@ impl<'a, T, D> GridCubicSmoothingSpline<'a, T, D>
147147
///
148148
/// `weights.len()` must be equal to `x.len()`
149149
///
150-
pub fn with_weights<W>(mut self, weights: &'a [Option<W>]) -> Self
151-
where W: AsArray<'a, T> + AsRef<[T]>
152-
{
153-
let weights = weights.iter().map(|w| {
154-
match w {
155-
Some(w) => Some(w.into()),
156-
None => None,
157-
}
158-
}).collect();
159-
150+
pub fn with_weights(mut self, weights: &[Option<ArrayView1<'a, T>>]) -> Self {
160151
self.invalidate();
161-
self.weights = Some(weights);
152+
self.weights = Some(weights.to_vec());
162153
self
163154
}
164155

@@ -199,13 +190,21 @@ impl<'a, T, D> GridCubicSmoothingSpline<'a, T, D>
199190
/// - If the `xi` data is invalid
200191
/// - If the spline yet has not been computed
201192
///
202-
pub fn evaluate<X>(&self, xi: &'a [X]) -> Result<Array<T, D>>
203-
where X: AsArray<'a, T> + AsRef<[T]>
204-
{
205-
let xi: Vec<ArrayView1<'a, T>> = xi.iter().map_into().collect();
193+
pub fn evaluate(&self, xi: &[ArrayView1<'a, T>]) -> Result<Array<T, D>> {
194+
let xi = xi.to_vec();
206195
self.evaluate_spline(&xi)
207196
}
208197

198+
/// Returns the ref to smoothing parameters vector or None
199+
pub fn smooth(&self) -> Option<&Vec<Option<T>>> {
200+
self.smooth.as_ref()
201+
}
202+
203+
/// Returns ref to `NdGridSpline` struct with data of computed spline or None
204+
pub fn spline(&self) -> Option<&NdGridSpline<'a, T, D>> {
205+
self.spline.as_ref()
206+
}
207+
209208
/// Invalidate computed spline
210209
fn invalidate(&mut self) {
211210
self.spline = None;

src/ndg/validate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub(super) fn validate_xy<T, D>(x: &[ArrayView1<'_, T>], y: ArrayView<'_, T, D>)
4242
if x.len() != y.ndim() {
4343
return Err(
4444
InvalidInputData(
45-
format!("The number of `x` data sites ({}) is not equal to `y` data dimensionality ({})",
45+
format!("The number of `x` data site vectors ({}) is not equal to `y` data dimensionality ({})",
4646
x.len(), y.ndim())
4747
)
4848
)

tests/ndg_make.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ fn test_make_surface() {
88
let x0 = array![1., 2., 3.];
99
let x1 = array![1., 2., 3., 4.];
1010

11-
let x = vec![&x0, &x1];
11+
let x = vec![x0.view(), x1.view()];
1212

1313
let y = array![
1414
[1., 2., 3., 4.],
1515
[5., 6., 7., 8.],
1616
[9., 10., 11., 12.],
1717
];
1818

19-
let spline = GridCubicSmoothingSpline::new(&x, &y)
19+
let s = GridCubicSmoothingSpline::new(&x, &y)
2020
.make().unwrap();
21+
22+
assert!(s.spline().is_some())
2123
}

0 commit comments

Comments
 (0)