Skip to content

Commit

Permalink
feat(rust, python, cli): Adds (Most) Remaining Trig Functions to `SQL…
Browse files Browse the repository at this point in the history
…Context` (pola-rs#9453)
  • Loading branch information
SeanTroyUWO authored and c-peters committed Jul 14, 2023
1 parent 270f4a9 commit 1d4156c
Show file tree
Hide file tree
Showing 7 changed files with 506 additions and 52 deletions.
66 changes: 42 additions & 24 deletions polars/polars-lazy/polars-plan/src/dsl/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,28 @@ impl Expr {
}
}

/// Compute the sine of the given expression
#[cfg(feature = "trigonometry")]
pub fn sin(self) -> Self {
self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::Sin))
}

/// Compute the cosine of the given expression
#[cfg(feature = "trigonometry")]
pub fn cos(self) -> Self {
self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::Cos))
}

/// Compute the tangent of the given expression
/// Compute the cotangent of the given expression
#[cfg(feature = "trigonometry")]
pub fn tan(self) -> Self {
self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::Tan))
pub fn cot(self) -> Self {
self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::Cot))
}

/// Compute the inverse sine of the given expression
/// Compute the sine of the given expression
#[cfg(feature = "trigonometry")]
pub fn arcsin(self) -> Self {
self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::ArcSin))
pub fn sin(self) -> Self {
self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::Sin))
}

/// Compute the tangent of the given expression
#[cfg(feature = "trigonometry")]
pub fn tan(self) -> Self {
self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::Tan))
}

/// Compute the inverse cosine of the given expression
Expand All @@ -91,16 +91,16 @@ impl Expr {
self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::ArcCos))
}

/// Compute the inverse tangent of the given expression
/// Compute the inverse sine of the given expression
#[cfg(feature = "trigonometry")]
pub fn arctan(self) -> Self {
self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::ArcTan))
pub fn arcsin(self) -> Self {
self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::ArcSin))
}

/// Compute the hyperbolic sine of the given expression
/// Compute the inverse tangent of the given expression
#[cfg(feature = "trigonometry")]
pub fn sinh(self) -> Self {
self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::Sinh))
pub fn arctan(self) -> Self {
self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::ArcTan))
}

/// Compute the hyperbolic cosine of the given expression
Expand All @@ -109,16 +109,16 @@ impl Expr {
self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::Cosh))
}

/// Compute the hyperbolic tangent of the given expression
/// Compute the hyperbolic sine of the given expression
#[cfg(feature = "trigonometry")]
pub fn tanh(self) -> Self {
self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::Tanh))
pub fn sinh(self) -> Self {
self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::Sinh))
}

/// Compute the inverse hyperbolic sine of the given expression
/// Compute the hyperbolic tangent of the given expression
#[cfg(feature = "trigonometry")]
pub fn arcsinh(self) -> Self {
self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::ArcSinh))
pub fn tanh(self) -> Self {
self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::Tanh))
}

/// Compute the inverse hyperbolic cosine of the given expression
Expand All @@ -127,12 +127,30 @@ impl Expr {
self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::ArcCosh))
}

/// Compute the inverse hyperbolic sine of the given expression
#[cfg(feature = "trigonometry")]
pub fn arcsinh(self) -> Self {
self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::ArcSinh))
}

/// Compute the inverse hyperbolic tangent of the given expression
#[cfg(feature = "trigonometry")]
pub fn arctanh(self) -> Self {
self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::ArcTanh))
}

/// Convert from radians to degrees
#[cfg(feature = "trigonometry")]
pub fn degrees(self) -> Self {
self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::Degrees))
}

/// Convert from degrees to radians
#[cfg(feature = "trigonometry")]
pub fn radians(self) -> Self {
self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::Radians))
}

/// Compute the sign of the given expression
#[cfg(feature = "sign")]
pub fn sign(self) -> Self {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,42 @@ use super::*;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, PartialEq, Debug, Eq, Hash)]
pub enum TrigonometricFunction {
Sin,
Cos,
Cot,
Sin,
Tan,
ArcSin,
ArcCos,
ArcSin,
ArcTan,
Sinh,
Cosh,
Sinh,
Tanh,
ArcSinh,
ArcCosh,
ArcSinh,
ArcTanh,
Degrees,
Radians,
}

impl Display for TrigonometricFunction {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
use self::*;
match self {
TrigonometricFunction::Sin => write!(f, "sin"),
TrigonometricFunction::Cos => write!(f, "cos"),
TrigonometricFunction::Cot => write!(f, "cot"),
TrigonometricFunction::Sin => write!(f, "sin"),
TrigonometricFunction::Tan => write!(f, "tan"),
TrigonometricFunction::ArcSin => write!(f, "arcsin"),
TrigonometricFunction::ArcCos => write!(f, "arccos"),
TrigonometricFunction::ArcSin => write!(f, "arcsin"),
TrigonometricFunction::ArcTan => write!(f, "arctan"),
TrigonometricFunction::Sinh => write!(f, "sinh"),
TrigonometricFunction::Cosh => write!(f, "cosh"),
TrigonometricFunction::Sinh => write!(f, "sinh"),
TrigonometricFunction::Tanh => write!(f, "tanh"),
TrigonometricFunction::ArcSinh => write!(f, "arcsinh"),
TrigonometricFunction::ArcCosh => write!(f, "arccosh"),
TrigonometricFunction::ArcSinh => write!(f, "arcsinh"),
TrigonometricFunction::ArcTanh => write!(f, "arctanh"),
TrigonometricFunction::Degrees => write!(f, "degrees"),
TrigonometricFunction::Radians => write!(f, "radians"),
}
}
}
Expand Down Expand Up @@ -72,55 +78,58 @@ where
ChunkedArray<T>: IntoSeries,
{
match trig_function {
TrigonometricFunction::Sin => sin(ca),
TrigonometricFunction::Cos => cos(ca),
TrigonometricFunction::Cot => cot(ca),
TrigonometricFunction::Sin => sin(ca),
TrigonometricFunction::Tan => tan(ca),
TrigonometricFunction::ArcSin => arcsin(ca),
TrigonometricFunction::ArcCos => arccos(ca),
TrigonometricFunction::ArcSin => arcsin(ca),
TrigonometricFunction::ArcTan => arctan(ca),
TrigonometricFunction::Sinh => sinh(ca),
TrigonometricFunction::Cosh => cosh(ca),
TrigonometricFunction::Sinh => sinh(ca),
TrigonometricFunction::Tanh => tanh(ca),
TrigonometricFunction::ArcSinh => arcsinh(ca),
TrigonometricFunction::ArcCosh => arccosh(ca),
TrigonometricFunction::ArcSinh => arcsinh(ca),
TrigonometricFunction::ArcTanh => arctanh(ca),
TrigonometricFunction::Degrees => degrees(ca),
TrigonometricFunction::Radians => radians(ca),
}
}

fn sin<T>(ca: &ChunkedArray<T>) -> PolarsResult<Series>
fn cos<T>(ca: &ChunkedArray<T>) -> PolarsResult<Series>
where
T: PolarsFloatType,
T::Native: Float,
ChunkedArray<T>: IntoSeries,
{
Ok(ca.apply(|v| v.sin()).into_series())
Ok(ca.apply(|v| v.cos()).into_series())
}

fn cos<T>(ca: &ChunkedArray<T>) -> PolarsResult<Series>
fn cot<T>(ca: &ChunkedArray<T>) -> PolarsResult<Series>
where
T: PolarsFloatType,
T::Native: Float,
ChunkedArray<T>: IntoSeries,
{
Ok(ca.apply(|v| v.cos()).into_series())
Ok(ca.apply(|v| v.cos() / v.sin()).into_series())
}

fn tan<T>(ca: &ChunkedArray<T>) -> PolarsResult<Series>
fn sin<T>(ca: &ChunkedArray<T>) -> PolarsResult<Series>
where
T: PolarsFloatType,
T::Native: Float,
ChunkedArray<T>: IntoSeries,
{
Ok(ca.apply(|v| v.tan()).into_series())
Ok(ca.apply(|v| v.sin()).into_series())
}

fn arcsin<T>(ca: &ChunkedArray<T>) -> PolarsResult<Series>
fn tan<T>(ca: &ChunkedArray<T>) -> PolarsResult<Series>
where
T: PolarsFloatType,
T::Native: Float,
ChunkedArray<T>: IntoSeries,
{
Ok(ca.apply(|v| v.asin()).into_series())
Ok(ca.apply(|v| v.tan()).into_series())
}

fn arccos<T>(ca: &ChunkedArray<T>) -> PolarsResult<Series>
Expand All @@ -132,22 +141,22 @@ where
Ok(ca.apply(|v| v.acos()).into_series())
}

fn arctan<T>(ca: &ChunkedArray<T>) -> PolarsResult<Series>
fn arcsin<T>(ca: &ChunkedArray<T>) -> PolarsResult<Series>
where
T: PolarsFloatType,
T::Native: Float,
ChunkedArray<T>: IntoSeries,
{
Ok(ca.apply(|v| v.atan()).into_series())
Ok(ca.apply(|v| v.asin()).into_series())
}

fn sinh<T>(ca: &ChunkedArray<T>) -> PolarsResult<Series>
fn arctan<T>(ca: &ChunkedArray<T>) -> PolarsResult<Series>
where
T: PolarsFloatType,
T::Native: Float,
ChunkedArray<T>: IntoSeries,
{
Ok(ca.apply(|v| v.sinh()).into_series())
Ok(ca.apply(|v| v.atan()).into_series())
}

fn cosh<T>(ca: &ChunkedArray<T>) -> PolarsResult<Series>
Expand All @@ -159,22 +168,22 @@ where
Ok(ca.apply(|v| v.cosh()).into_series())
}

fn tanh<T>(ca: &ChunkedArray<T>) -> PolarsResult<Series>
fn sinh<T>(ca: &ChunkedArray<T>) -> PolarsResult<Series>
where
T: PolarsFloatType,
T::Native: Float,
ChunkedArray<T>: IntoSeries,
{
Ok(ca.apply(|v| v.tanh()).into_series())
Ok(ca.apply(|v| v.sinh()).into_series())
}

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

fn arccosh<T>(ca: &ChunkedArray<T>) -> PolarsResult<Series>
Expand All @@ -186,6 +195,15 @@ where
Ok(ca.apply(|v| v.acosh()).into_series())
}

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

fn arctanh<T>(ca: &ChunkedArray<T>) -> PolarsResult<Series>
where
T: PolarsFloatType,
Expand All @@ -194,3 +212,21 @@ where
{
Ok(ca.apply(|v| v.atanh()).into_series())
}

fn degrees<T>(ca: &ChunkedArray<T>) -> PolarsResult<Series>
where
T: PolarsFloatType,
T::Native: Float,
ChunkedArray<T>: IntoSeries,
{
Ok(ca.apply(|v| v.to_degrees()).into_series())
}

fn radians<T>(ca: &ChunkedArray<T>) -> PolarsResult<Series>
where
T: PolarsFloatType,
T::Native: Float,
ChunkedArray<T>: IntoSeries,
{
Ok(ca.apply(|v| v.to_radians()).into_series())
}
Loading

0 comments on commit 1d4156c

Please sign in to comment.