Skip to content

Commit

Permalink
More trigonometry (#4047)
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego committed Jul 17, 2022
1 parent 8776d7b commit 3c64b5c
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 51 deletions.
2 changes: 1 addition & 1 deletion polars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ timezones = ["polars-core/timezones"]
string_justify = ["polars-lazy/string_justify", "polars-ops/string_justify"]
arg_where = ["polars-lazy/arg_where"]
date_offset = ["polars-lazy/date_offset"]
trigo = ["polars-lazy/trigo"]
trigonometry = ["polars-lazy/trigonometry"]

test = [
"lazy",
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-lazy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ dtype-categorical = ["polars-core/dtype-categorical"]
dtype-struct = ["polars-core/dtype-struct"]
object = ["polars-core/object"]
date_offset = []
trigo = []
trigonometry = []

true_div = []

Expand Down
25 changes: 14 additions & 11 deletions polars/polars-lazy/src/dsl/function_expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ mod pow;
mod strings;
#[cfg(any(feature = "temporal", feature = "date_offset"))]
mod temporal;
#[cfg(feature = "trigo")]
mod trigo;
#[cfg(feature = "trigonometry")]
mod trigonometry;

use super::*;
use polars_core::prelude::*;
Expand Down Expand Up @@ -37,14 +37,14 @@ pub enum FunctionExpr {
StringEndsWith(String),
#[cfg(feature = "date_offset")]
DateOffset(Duration),
#[cfg(feature = "trigo")]
Trigo(TrigoType),
#[cfg(feature = "trigonometry")]
Trigonometry(TrigonometricFunction),
}

#[cfg(feature = "trigo")]
#[cfg(feature = "trigonometry")]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, PartialEq, Debug, Eq, Hash)]
pub enum TrigoType {
pub enum TrigonometricFunction {
Sin,
Cos,
Tan,
Expand All @@ -54,6 +54,9 @@ pub enum TrigoType {
Sinh,
Cosh,
Tanh,
ArcSinh,
ArcCosh,
ArcTanh,
}

impl FunctionExpr {
Expand Down Expand Up @@ -94,8 +97,8 @@ impl FunctionExpr {
}
#[cfg(feature = "date_offset")]
DateOffset(_) => same_type(),
#[cfg(feature = "trigo")]
Trigo(_) => float_dtype(),
#[cfg(feature = "trigonometry")]
Trigonometry(_) => float_dtype(),
}
}
}
Expand Down Expand Up @@ -174,9 +177,9 @@ impl From<FunctionExpr> for SpecialEq<Arc<dyn SeriesUdf>> {
DateOffset(offset) => {
map_owned_with_args!(temporal::date_offset, offset)
}
#[cfg(feature = "trigo")]
Trigo(trigotype) => {
map_with_args!(trigo::trigo, trigotype)
#[cfg(feature = "trigonometry")]
Trigonometry(trig_function) => {
map_with_args!(trigonometry::apply_trigonometric_function, trig_function)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,49 @@ use super::*;
use num::Float;
use polars_core::export::num;

pub(super) fn trigo(s: &Series, trigotype: TrigoType) -> Result<Series> {
pub(super) fn apply_trigonometric_function(
s: &Series,
trig_function: TrigonometricFunction,
) -> Result<Series> {
use DataType::*;
match s.dtype() {
Float32 => {
let ca = s.f32().unwrap();
trigo_float(ca, trigotype)
apply_trigonometric_function_to_float(ca, trig_function)
}
Float64 => {
let ca = s.f64().unwrap();
trigo_float(ca, trigotype)
apply_trigonometric_function_to_float(ca, trig_function)
}
_ => {
let s = s.cast(&DataType::Float64)?;
trigo(&s, trigotype)
apply_trigonometric_function(&s, trig_function)
}
}
}

fn trigo_float<T>(ca: &ChunkedArray<T>, trigotype: TrigoType) -> Result<Series>
fn apply_trigonometric_function_to_float<T>(
ca: &ChunkedArray<T>,
trig_function: TrigonometricFunction,
) -> Result<Series>
where
T: PolarsFloatType,
T::Native: num::Float,
ChunkedArray<T>: IntoSeries,
{
match trigotype {
TrigoType::Sin => sin(ca),
TrigoType::Cos => cos(ca),
TrigoType::Tan => tan(ca),
TrigoType::ArcSin => arcsin(ca),
TrigoType::ArcCos => arccos(ca),
TrigoType::ArcTan => arctan(ca),
TrigoType::Sinh => sinh(ca),
TrigoType::Cosh => cosh(ca),
TrigoType::Tanh => tanh(ca),
match trig_function {
TrigonometricFunction::Sin => sin(ca),
TrigonometricFunction::Cos => cos(ca),
TrigonometricFunction::Tan => tan(ca),
TrigonometricFunction::ArcSin => arcsin(ca),
TrigonometricFunction::ArcCos => arccos(ca),
TrigonometricFunction::ArcTan => arctan(ca),
TrigonometricFunction::Sinh => sinh(ca),
TrigonometricFunction::Cosh => cosh(ca),
TrigonometricFunction::Tanh => tanh(ca),
TrigonometricFunction::ArcSinh => arcsinh(ca),
TrigonometricFunction::ArcCosh => arccosh(ca),
TrigonometricFunction::ArcTanh => arctanh(ca),
}
}

Expand Down Expand Up @@ -119,3 +128,30 @@ where
{
Ok(ca.apply(|v| v.tanh()).into_series())
}

fn arcsinh<T>(ca: &ChunkedArray<T>) -> Result<Series>
where
T: PolarsFloatType,
T::Native: num::Float,
ChunkedArray<T>: IntoSeries,
{
Ok(ca.apply(|v| v.asinh()).into_series())
}

fn arccosh<T>(ca: &ChunkedArray<T>) -> Result<Series>
where
T: PolarsFloatType,
T::Native: num::Float,
ChunkedArray<T>: IntoSeries,
{
Ok(ca.apply(|v| v.acosh()).into_series())
}

fn arctanh<T>(ca: &ChunkedArray<T>) -> Result<Series>
where
T: PolarsFloatType,
T::Native: num::Float,
ChunkedArray<T>: IntoSeries,
{
Ok(ca.apply(|v| v.atanh()).into_series())
}
85 changes: 65 additions & 20 deletions polars/polars-lazy/src/dsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ pub use options::*;

use crate::dsl::function_expr::FunctionExpr;

#[cfg(feature = "trigo")]
use crate::dsl::function_expr::TrigoType;
#[cfg(feature = "trigonometry")]
use crate::dsl::function_expr::TrigonometricFunction;

use polars_arrow::array::default_arrays::FromData;
#[cfg(feature = "diff")]
Expand Down Expand Up @@ -1203,11 +1203,11 @@ impl Expr {
}

/// Compute the sine of the given expression
#[cfg(feature = "trigo")]
#[cfg(feature = "trigonometry")]
pub fn sin(self) -> Self {
Expr::Function {
input: vec![self],
function: FunctionExpr::Trigo(TrigoType::Sin),
function: FunctionExpr::Trigonometry(TrigonometricFunction::Sin),
options: FunctionOptions {
collect_groups: ApplyOptions::ApplyFlat,
input_wildcard_expansion: false,
Expand All @@ -1218,11 +1218,11 @@ impl Expr {
}

/// Compute the cosine of the given expression
#[cfg(feature = "trigo")]
#[cfg(feature = "trigonometry")]
pub fn cos(self) -> Self {
Expr::Function {
input: vec![self],
function: FunctionExpr::Trigo(TrigoType::Cos),
function: FunctionExpr::Trigonometry(TrigonometricFunction::Cos),
options: FunctionOptions {
collect_groups: ApplyOptions::ApplyFlat,
input_wildcard_expansion: false,
Expand All @@ -1233,11 +1233,11 @@ impl Expr {
}

/// Compute the tangent of the given expression
#[cfg(feature = "trigo")]
#[cfg(feature = "trigonometry")]
pub fn tan(self) -> Self {
Expr::Function {
input: vec![self],
function: FunctionExpr::Trigo(TrigoType::Tan),
function: FunctionExpr::Trigonometry(TrigonometricFunction::Tan),
options: FunctionOptions {
collect_groups: ApplyOptions::ApplyFlat,
input_wildcard_expansion: false,
Expand All @@ -1248,11 +1248,11 @@ impl Expr {
}

/// Compute the arcsine of the given expression
#[cfg(feature = "trigo")]
#[cfg(feature = "trigonometry")]
pub fn arcsin(self) -> Self {
Expr::Function {
input: vec![self],
function: FunctionExpr::Trigo(TrigoType::ArcSin),
function: FunctionExpr::Trigonometry(TrigonometricFunction::ArcSin),
options: FunctionOptions {
collect_groups: ApplyOptions::ApplyFlat,
input_wildcard_expansion: false,
Expand All @@ -1263,11 +1263,11 @@ impl Expr {
}

/// Compute the arccosine of the given expression
#[cfg(feature = "trigo")]
#[cfg(feature = "trigonometry")]
pub fn arccos(self) -> Self {
Expr::Function {
input: vec![self],
function: FunctionExpr::Trigo(TrigoType::ArcCos),
function: FunctionExpr::Trigonometry(TrigonometricFunction::ArcCos),
options: FunctionOptions {
collect_groups: ApplyOptions::ApplyFlat,
input_wildcard_expansion: false,
Expand All @@ -1278,11 +1278,11 @@ impl Expr {
}

/// Compute the arctangent of the given expression
#[cfg(feature = "trigo")]
#[cfg(feature = "trigonometry")]
pub fn arctan(self) -> Self {
Expr::Function {
input: vec![self],
function: FunctionExpr::Trigo(TrigoType::ArcTan),
function: FunctionExpr::Trigonometry(TrigonometricFunction::ArcTan),
options: FunctionOptions {
collect_groups: ApplyOptions::ApplyFlat,
input_wildcard_expansion: false,
Expand All @@ -1293,11 +1293,11 @@ impl Expr {
}

/// Compute the hyperbolic sine of the given expression
#[cfg(feature = "trigo")]
#[cfg(feature = "trigonometry")]
pub fn sinh(self) -> Self {
Expr::Function {
input: vec![self],
function: FunctionExpr::Trigo(TrigoType::Sinh),
function: FunctionExpr::Trigonometry(TrigonometricFunction::Sinh),
options: FunctionOptions {
collect_groups: ApplyOptions::ApplyFlat,
input_wildcard_expansion: false,
Expand All @@ -1308,11 +1308,11 @@ impl Expr {
}

/// Compute the hyperbolic cosine of the given expression
#[cfg(feature = "trigo")]
#[cfg(feature = "trigonometry")]
pub fn cosh(self) -> Self {
Expr::Function {
input: vec![self],
function: FunctionExpr::Trigo(TrigoType::Cosh),
function: FunctionExpr::Trigonometry(TrigonometricFunction::Cosh),
options: FunctionOptions {
collect_groups: ApplyOptions::ApplyFlat,
input_wildcard_expansion: false,
Expand All @@ -1323,11 +1323,11 @@ impl Expr {
}

/// Compute the hyperbolic tangent of the given expression
#[cfg(feature = "trigo")]
#[cfg(feature = "trigonometry")]
pub fn tanh(self) -> Self {
Expr::Function {
input: vec![self],
function: FunctionExpr::Trigo(TrigoType::Tanh),
function: FunctionExpr::Trigonometry(TrigonometricFunction::Tanh),
options: FunctionOptions {
collect_groups: ApplyOptions::ApplyFlat,
input_wildcard_expansion: false,
Expand All @@ -1337,6 +1337,51 @@ impl Expr {
}
}

/// Compute the hyperbolic sine of the given expression
#[cfg(feature = "trigonometry")]
pub fn arcsinh(self) -> Self {
Expr::Function {
input: vec![self],
function: FunctionExpr::Trigonometry(TrigonometricFunction::ArcSinh),
options: FunctionOptions {
collect_groups: ApplyOptions::ApplyFlat,
input_wildcard_expansion: false,
auto_explode: false,
fmt_str: "arcsinh",
},
}
}

/// Compute the hyperbolic cosine of the given expression
#[cfg(feature = "trigonometry")]
pub fn arccosh(self) -> Self {
Expr::Function {
input: vec![self],
function: FunctionExpr::Trigonometry(TrigonometricFunction::ArcCosh),
options: FunctionOptions {
collect_groups: ApplyOptions::ApplyFlat,
input_wildcard_expansion: false,
auto_explode: false,
fmt_str: "arccosh",
},
}
}

/// Compute the hyperbolic tangent of the given expression
#[cfg(feature = "trigonometry")]
pub fn arctanh(self) -> Self {
Expr::Function {
input: vec![self],
function: FunctionExpr::Trigonometry(TrigonometricFunction::ArcTanh),
options: FunctionOptions {
collect_groups: ApplyOptions::ApplyFlat,
input_wildcard_expansion: false,
auto_explode: false,
fmt_str: "arctanh",
},
}
}

/// Filter a single column
/// Should be used in aggregation context. If you want to filter on a DataFrame level, use
/// [LazyFrame::filter](LazyFrame::filter)
Expand Down
2 changes: 1 addition & 1 deletion polars/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
//! - `cumulative_eval` - Apply expressions over cumulatively increasing windows.
//! - `argwhere` Get indices where condition holds.
//! - `date_offset` Add an offset to dates that take months and leap years into account.
//! - `trigo` Add trigonometry functionality.
//! - `trigonometry` Trigonometric functions.
//! * `DataFrame` pretty printing
//! - `fmt` - Activate DataFrame formatting
//!
Expand Down
2 changes: 1 addition & 1 deletion py-polars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ features = [
"arg_where",
"timezones",
"date_offset",
"trigo",
"trigonometry",
]

# [patch.crates-io]
Expand Down
3 changes: 3 additions & 0 deletions py-polars/docs/source/reference/expression.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,11 @@ Computations

Expr.abs
Expr.arccos
Expr.arccosh
Expr.arcsin
Expr.arcsinh
Expr.arctan
Expr.arctanh
Expr.arg_unique
Expr.cos
Expr.cosh
Expand Down

0 comments on commit 3c64b5c

Please sign in to comment.