Skip to content

Commit

Permalink
simplify, remove "dead" unsigned interpolate method
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Oct 10, 2023
1 parent 352de5e commit 780dc13
Showing 1 changed file with 28 additions and 39 deletions.
67 changes: 28 additions & 39 deletions crates/polars-ops/src/chunked_array/interpolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,6 @@ where
}
}

#[inline]
fn unsigned_interp<T>(low: T, high: T, steps: IdxSize, steps_n: T, av: &mut Vec<T>)
where
T: Sub<Output = T>
+ Mul<Output = T>
+ Add<Output = T>
+ Div<Output = T>
+ NumCast
+ PartialOrd
+ Copy,
{
if high >= low {
signed_interp::<T>(low, high, steps, steps_n, av)
} else {
let diff = low - high;
for step_i in (1..steps).rev() {
let step_i: T = NumCast::from(step_i).unwrap();
let v = linear_itp(high, step_i, diff, steps_n);
av.push(v)
}
}
}

fn interpolate_impl<T, I>(chunked_arr: &ChunkedArray<T>, interpolation_branch: I) -> ChunkedArray<T>
where
T: PolarsNumericType,
Expand Down Expand Up @@ -196,16 +173,34 @@ fn interpolate_linear(s: &Series) -> Series {
let logical = s.dtype();

let s = s.to_physical_repr();
let out = match s.dtype() {
DataType::Float32 => linear_interp_signed(s.f32().unwrap()),
DataType::Float64 => linear_interp_signed(s.f64().unwrap()),
DataType::UInt8 | DataType::UInt16 | DataType::UInt32 | DataType::UInt64 => {
linear_interp_unsigned(s.cast(&DataType::Float64).unwrap().f64().unwrap())
},
DataType::Int8 | DataType::Int16 | DataType::Int32 | DataType::Int64 => {
linear_interp_signed(s.cast(&DataType::Float64).unwrap().f64().unwrap())
},
_ => s.as_ref().clone(),

let out = if matches!(
logical,
DataType::Date | DataType::Datetime(_, _) | DataType::Duration(_) | DataType::Time
) {
match s.dtype() {
// Datetime, Time, or Duration
DataType::Int64 => linear_interp_signed(s.i64().unwrap()),
// Date
DataType::Int32 => linear_interp_signed(s.i32().unwrap()),
_ => unreachable!(),
}
} else {
match s.dtype() {
DataType::Float32 => linear_interp_signed(s.f32().unwrap()),
DataType::Float64 => linear_interp_signed(s.f64().unwrap()),
DataType::Int8
| DataType::Int16
| DataType::Int32
| DataType::Int64
| DataType::UInt8
| DataType::UInt16
| DataType::UInt32
| DataType::UInt64 => {
linear_interp_signed(s.cast(&DataType::Float64).unwrap().f64().unwrap())
},
_ => s.as_ref().clone(),
}
};
match logical {
DataType::Date
Expand All @@ -218,12 +213,6 @@ fn interpolate_linear(s: &Series) -> Series {
}
}

fn linear_interp_unsigned<T: PolarsNumericType>(ca: &ChunkedArray<T>) -> Series
where
ChunkedArray<T>: IntoSeries,
{
interpolate_impl(ca, unsigned_interp::<T::Native>).into_series()
}
fn linear_interp_signed<T: PolarsNumericType>(ca: &ChunkedArray<T>) -> Series
where
ChunkedArray<T>: IntoSeries,
Expand Down

0 comments on commit 780dc13

Please sign in to comment.