-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Native implementation of the sign function (#4147)
- Loading branch information
Showing
11 changed files
with
130 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use super::*; | ||
use polars_core::export::num; | ||
use DataType::*; | ||
|
||
pub(super) fn sign(s: &Series) -> Result<Series> { | ||
match s.dtype() { | ||
Float32 => { | ||
let ca = s.f32().unwrap(); | ||
sign_float(ca) | ||
} | ||
Float64 => { | ||
let ca = s.f64().unwrap(); | ||
sign_float(ca) | ||
} | ||
dt if dt.is_numeric() => { | ||
let s = s.cast(&Float64)?; | ||
sign(&s) | ||
} | ||
dt => Err(PolarsError::ComputeError( | ||
format!("cannot use 'sign' on Series of dtype: {:?}", dt).into(), | ||
)), | ||
} | ||
} | ||
|
||
fn sign_float<T>(ca: &ChunkedArray<T>) -> Result<Series> | ||
where | ||
T: PolarsFloatType, | ||
T::Native: num::Float, | ||
ChunkedArray<T>: IntoSeries, | ||
{ | ||
ca.apply(signum_improved).into_series().cast(&Int64) | ||
} | ||
|
||
// Wrapper for the signum function that handles +/-0.0 inputs differently | ||
// See discussion here: https://github.com/rust-lang/rust/issues/57543 | ||
fn signum_improved<F: num::Float>(v: F) -> F { | ||
if v.is_zero() { | ||
v | ||
} else { | ||
v.signum() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters