Skip to content

Commit 1679775

Browse files
committed
fixed partialord impl
1 parent 953de74 commit 1679775

File tree

18 files changed

+155
-25
lines changed

18 files changed

+155
-25
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ninterp"
3-
version = "0.6.1"
3+
version = "0.6.2"
44
edition = "2021"
55
description = "Numerical interpolation for N-dimensional rectilinear grids"
66
repository = "https://github.com/NREL/ninterp"

examples/dynamic_interpolator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn using_enum() {
1717
array![0., 1.],
1818
array![0., 1.],
1919
array![[2., 4.], [4., 16.]],
20-
strategy::Linear.into(),
20+
strategy::Linear,
2121
Extrapolate::Enable,
2222
)
2323
.unwrap();
@@ -40,7 +40,7 @@ fn using_enum() {
4040
array![0., 1.],
4141
array![0., 1.],
4242
array![[[0., 1.], [0.1, 1.1]], [[0.2, 1.2], [0.3, 1.3]]],
43-
strategy::Nearest.into(),
43+
strategy::Nearest,
4444
Extrapolate::Error,
4545
)
4646
.unwrap(); // `.into()` converts the `Interp1D` into an `InterpolatorEnum::Interp1D(...)`

src/interpolator/data.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub use one::{InterpData1D, InterpData1DOwned, InterpData1DViewed};
55
pub use three::{InterpData3D, InterpData3DOwned, InterpData3DViewed};
66
pub use two::{InterpData2D, InterpData2DOwned, InterpData2DViewed};
77

8-
#[derive(Debug, Clone, PartialEq)]
8+
#[derive(Debug, Clone)]
99
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
1010
#[cfg_attr(
1111
feature = "serde",
@@ -28,6 +28,18 @@ where
2828
pub type InterpDataViewed<T, const N: usize> = InterpData<ndarray::ViewRepr<T>, N>;
2929
pub type InterpDataOwned<T, const N: usize> = InterpData<ndarray::ViewRepr<T>, N>;
3030

31+
impl<D, const N: usize> PartialEq for InterpData<D, N>
32+
where
33+
Dim<[Ix; N]>: Dimension,
34+
D: Data + RawDataClone + Clone,
35+
D::Elem: PartialEq + Debug,
36+
ArrayBase<D, Ix1>: PartialEq,
37+
{
38+
fn eq(&self, other: &Self) -> bool {
39+
self.grid == other.grid && self.values == other.values
40+
}
41+
}
42+
3143
impl<D, const N: usize> InterpData<D, N>
3244
where
3345
Dim<[Ix; N]>: Dimension,

src/interpolator/enums.rs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,28 @@ where
7575
Interp3D(Interp3D<D, Strategy3DEnum>),
7676
InterpND(InterpND<D, StrategyNDEnum>),
7777
}
78+
/// [`InterpolatorEnum`] that views data.
79+
pub type InterpolatorEnumViewed<T> = InterpolatorEnum<ndarray::ViewRepr<T>>;
80+
/// [`InterpolatorEnum`] that owns data.
81+
pub type InterpolatorEnumOwned<T> = InterpolatorEnum<ndarray::OwnedRepr<T>>;
82+
83+
impl<D> PartialEq for InterpolatorEnum<D>
84+
where
85+
D: Data + RawDataClone + Clone,
86+
D::Elem: Num + PartialOrd + Copy + Debug,
87+
ArrayBase<D, Ix1>: PartialEq,
88+
{
89+
fn eq(&self, other: &Self) -> bool {
90+
match (self, other) {
91+
(Self::Interp0D(l), Self::Interp0D(r)) => l == r,
92+
(Self::Interp1D(l), Self::Interp1D(r)) => l == r,
93+
(Self::Interp2D(l), Self::Interp2D(r)) => l == r,
94+
(Self::Interp3D(l), Self::Interp3D(r)) => l == r,
95+
(Self::InterpND(l), Self::InterpND(r)) => l == r,
96+
_ => false,
97+
}
98+
}
99+
}
78100

79101
impl<D> InterpolatorEnum<D>
80102
where
@@ -92,13 +114,13 @@ where
92114
pub fn new_1d(
93115
x: ArrayBase<D, Ix1>,
94116
f_x: ArrayBase<D, Ix1>,
95-
strategy: Strategy1DEnum,
117+
strategy: impl Into<Strategy1DEnum>,
96118
extrapolate: Extrapolate<D::Elem>,
97119
) -> Result<Self, ValidateError> {
98120
Ok(Self::Interp1D(Interp1D::new(
99121
x,
100122
f_x,
101-
strategy,
123+
strategy.into(),
102124
extrapolate,
103125
)?))
104126
}
@@ -109,14 +131,14 @@ where
109131
x: ArrayBase<D, Ix1>,
110132
y: ArrayBase<D, Ix1>,
111133
f_xy: ArrayBase<D, Ix2>,
112-
strategy: Strategy2DEnum,
134+
strategy: impl Into<Strategy2DEnum>,
113135
extrapolate: Extrapolate<D::Elem>,
114136
) -> Result<Self, ValidateError> {
115137
Ok(Self::Interp2D(Interp2D::new(
116138
x,
117139
y,
118140
f_xy,
119-
strategy,
141+
strategy.into(),
120142
extrapolate,
121143
)?))
122144
}
@@ -128,15 +150,15 @@ where
128150
y: ArrayBase<D, Ix1>,
129151
z: ArrayBase<D, Ix1>,
130152
f_xyz: ArrayBase<D, Ix3>,
131-
strategy: Strategy3DEnum,
153+
strategy: impl Into<Strategy3DEnum>,
132154
extrapolate: Extrapolate<D::Elem>,
133155
) -> Result<Self, ValidateError> {
134156
Ok(Self::Interp3D(Interp3D::new(
135157
x,
136158
y,
137159
z,
138160
f_xyz,
139-
strategy,
161+
strategy.into(),
140162
extrapolate,
141163
)?))
142164
}
@@ -146,13 +168,13 @@ where
146168
pub fn new_nd(
147169
grid: Vec<ArrayBase<D, Ix1>>,
148170
values: ArrayBase<D, IxDyn>,
149-
strategy: StrategyNDEnum,
171+
strategy: impl Into<StrategyNDEnum>,
150172
extrapolate: Extrapolate<D::Elem>,
151173
) -> Result<Self, ValidateError> {
152174
Ok(Self::InterpND(InterpND::new(
153175
grid,
154176
values,
155-
strategy,
177+
strategy.into(),
156178
extrapolate,
157179
)?))
158180
}
@@ -251,3 +273,12 @@ where
251273
InterpolatorEnum::InterpND(interpolator)
252274
}
253275
}
276+
277+
mod tests {
278+
#[test]
279+
fn test_partialeq() {
280+
#[derive(PartialEq)]
281+
#[allow(unused)]
282+
struct MyStruct(super::InterpolatorEnumOwned<f64>);
283+
}
284+
}

src/interpolator/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,22 @@ macro_rules! extrapolate_impl {
111111
};
112112
}
113113
pub(crate) use extrapolate_impl;
114+
115+
macro_rules! partialeq_impl {
116+
($InterpType:ident, $Data:ident, $Strategy:ident) => {
117+
impl<D, S> PartialEq for $InterpType<D, S>
118+
where
119+
D: Data + RawDataClone + Clone,
120+
D::Elem: PartialEq + Debug,
121+
S: $Strategy<D> + Clone + PartialEq,
122+
$Data<D>: PartialEq,
123+
{
124+
fn eq(&self, other: &Self) -> bool {
125+
self.data == other.data
126+
&& self.strategy == other.strategy
127+
&& self.extrapolate == other.extrapolate
128+
}
129+
}
130+
};
131+
}
132+
pub(crate) use partialeq_impl;

src/interpolator/n/mod.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod strategies;
99
mod tests;
1010

1111
/// Interpolator data where N is determined at runtime
12-
#[derive(Debug, Clone, PartialEq)]
12+
#[derive(Debug, Clone)]
1313
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
1414
#[cfg_attr(
1515
feature = "serde",
@@ -31,6 +31,17 @@ pub type InterpDataNDViewed<T> = InterpDataND<ndarray::ViewRepr<T>>;
3131
/// [`InterpDataND`] that owns data.
3232
pub type InterpDataNDOwned<T> = InterpDataND<ndarray::OwnedRepr<T>>;
3333

34+
impl<D> PartialEq for InterpDataND<D>
35+
where
36+
D: Data + RawDataClone + Clone,
37+
D::Elem: PartialEq + Debug,
38+
ArrayBase<D, Ix1>: PartialEq,
39+
{
40+
fn eq(&self, other: &Self) -> bool {
41+
self.grid == other.grid && self.values == other.values
42+
}
43+
}
44+
3445
impl<D> InterpDataND<D>
3546
where
3647
D: Data + RawDataClone + Clone,
@@ -84,7 +95,7 @@ where
8495
}
8596

8697
/// N-D interpolator
87-
#[derive(Debug, Clone, PartialEq)]
98+
#[derive(Debug, Clone)]
8899
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
89100
#[cfg_attr(
90101
feature = "serde",
@@ -115,6 +126,7 @@ pub type InterpNDViewed<T, S> = InterpND<ndarray::ViewRepr<T>, S>;
115126
pub type InterpNDOwned<T, S> = InterpND<ndarray::OwnedRepr<T>, S>;
116127

117128
extrapolate_impl!(InterpND, StrategyND);
129+
partialeq_impl!(InterpND, InterpDataND, StrategyND);
118130

119131
impl<D, S> InterpND<D, S>
120132
where

src/interpolator/n/tests.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,14 @@ fn test_mismatched_grid() {
371371
ValidateError::Other(_)
372372
));
373373
}
374+
375+
#[test]
376+
fn test_partialeq() {
377+
#[derive(PartialEq)]
378+
#[allow(unused)]
379+
struct MyStruct(InterpDataNDOwned<f64>);
380+
381+
#[derive(PartialEq)]
382+
#[allow(unused)]
383+
struct MyStruct2(InterpNDOwned<f64, strategy::Linear>);
384+
}

src/interpolator/one/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ where
3030
}
3131

3232
/// 1-D interpolator
33-
#[derive(Debug, Clone, PartialEq)]
33+
#[derive(Debug, Clone)]
3434
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
3535
#[cfg_attr(
3636
feature = "serde",
@@ -61,6 +61,7 @@ pub type Interp1DViewed<T, S> = Interp1D<ndarray::ViewRepr<T>, S>;
6161
pub type Interp1DOwned<T, S> = Interp1D<ndarray::OwnedRepr<T>, S>;
6262

6363
extrapolate_impl!(Interp1D, Strategy1D);
64+
partialeq_impl!(Interp1D, InterpData1D, Strategy1D);
6465

6566
impl<D, S> Interp1D<D, S>
6667
where

src/interpolator/one/tests.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,14 @@ fn test_extrapolate() {
162162
assert_approx_eq!(interp.interpolate(&[-0.75]).unwrap(), 0.05);
163163
assert_eq!(interp.interpolate(&[5.]).unwrap(), 1.2);
164164
}
165+
166+
#[test]
167+
fn test_partialeq() {
168+
#[derive(PartialEq)]
169+
#[allow(unused)]
170+
struct MyStruct(InterpData1DOwned<f64>);
171+
172+
#[derive(PartialEq)]
173+
#[allow(unused)]
174+
struct MyStruct2(Interp1DOwned<f64, strategy::Linear>);
175+
}

src/interpolator/three/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ where
3535
}
3636

3737
/// 3-D interpolator
38-
#[derive(Debug, Clone, PartialEq)]
38+
#[derive(Debug, Clone)]
3939
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
4040
#[cfg_attr(
4141
feature = "serde",
@@ -66,6 +66,7 @@ pub type Interp3DViewed<T, S> = Interp3D<ndarray::ViewRepr<T>, S>;
6666
pub type Interp3DOwned<T, S> = Interp3D<ndarray::OwnedRepr<T>, S>;
6767

6868
extrapolate_impl!(Interp3D, Strategy3D);
69+
partialeq_impl!(Interp3D, InterpData3D, Strategy3D);
6970

7071
impl<D, S> Interp3D<D, S>
7172
where

0 commit comments

Comments
 (0)