Path: blob/main/crates/polars-plan/src/dsl/arithmetic.rs
6939 views
use std::ops::{Add, Div, Mul, Neg, Rem, Sub};12use super::*;34// Arithmetic ops5impl Add for Expr {6type Output = Expr;78fn add(self, rhs: Self) -> Self::Output {9binary_expr(self, Operator::Plus, rhs)10}11}1213impl Sub for Expr {14type Output = Expr;1516fn sub(self, rhs: Self) -> Self::Output {17binary_expr(self, Operator::Minus, rhs)18}19}2021impl Div for Expr {22type Output = Expr;2324fn div(self, rhs: Self) -> Self::Output {25binary_expr(self, Operator::Divide, rhs)26}27}2829impl Mul for Expr {30type Output = Expr;3132fn mul(self, rhs: Self) -> Self::Output {33binary_expr(self, Operator::Multiply, rhs)34}35}3637impl Rem for Expr {38type Output = Expr;3940fn rem(self, rhs: Self) -> Self::Output {41binary_expr(self, Operator::Modulus, rhs)42}43}4445impl Neg for Expr {46type Output = Expr;4748fn neg(self) -> Self::Output {49self.map_unary(FunctionExpr::Negate)50}51}5253impl Expr {54/// Floor divide `self` by `rhs`.55pub fn floor_div(self, rhs: Self) -> Self {56binary_expr(self, Operator::FloorDivide, rhs)57}5859/// Raise expression to the power `exponent`60pub fn pow<E: Into<Expr>>(self, exponent: E) -> Self {61self.map_binary(PowFunction::Generic, exponent.into())62}6364/// Compute the square root of the given expression65pub fn sqrt(self) -> Self {66self.map_unary(PowFunction::Sqrt)67}6869/// Compute the cube root of the given expression70pub fn cbrt(self) -> Self {71self.map_unary(PowFunction::Cbrt)72}7374/// Compute the cosine of the given expression75#[cfg(feature = "trigonometry")]76pub fn cos(self) -> Self {77self.map_unary(TrigonometricFunction::Cos)78}7980/// Compute the cotangent of the given expression81#[cfg(feature = "trigonometry")]82pub fn cot(self) -> Self {83self.map_unary(TrigonometricFunction::Cot)84}8586/// Compute the sine of the given expression87#[cfg(feature = "trigonometry")]88pub fn sin(self) -> Self {89self.map_unary(TrigonometricFunction::Sin)90}9192/// Compute the tangent of the given expression93#[cfg(feature = "trigonometry")]94pub fn tan(self) -> Self {95self.map_unary(TrigonometricFunction::Tan)96}9798/// Compute the inverse cosine of the given expression99#[cfg(feature = "trigonometry")]100pub fn arccos(self) -> Self {101self.map_unary(TrigonometricFunction::ArcCos)102}103104/// Compute the inverse sine of the given expression105#[cfg(feature = "trigonometry")]106pub fn arcsin(self) -> Self {107self.map_unary(TrigonometricFunction::ArcSin)108}109110/// Compute the inverse tangent of the given expression111#[cfg(feature = "trigonometry")]112pub fn arctan(self) -> Self {113self.map_unary(TrigonometricFunction::ArcTan)114}115116/// Compute the inverse tangent of the given expression, with the angle expressed as the argument of a complex number117#[cfg(feature = "trigonometry")]118pub fn arctan2(self, x: Self) -> Self {119self.map_binary(FunctionExpr::Atan2, x)120}121122/// Compute the hyperbolic cosine of the given expression123#[cfg(feature = "trigonometry")]124pub fn cosh(self) -> Self {125self.map_unary(TrigonometricFunction::Cosh)126}127128/// Compute the hyperbolic sine of the given expression129#[cfg(feature = "trigonometry")]130pub fn sinh(self) -> Self {131self.map_unary(TrigonometricFunction::Sinh)132}133134/// Compute the hyperbolic tangent of the given expression135#[cfg(feature = "trigonometry")]136pub fn tanh(self) -> Self {137self.map_unary(TrigonometricFunction::Tanh)138}139140/// Compute the inverse hyperbolic cosine of the given expression141#[cfg(feature = "trigonometry")]142pub fn arccosh(self) -> Self {143self.map_unary(TrigonometricFunction::ArcCosh)144}145146/// Compute the inverse hyperbolic sine of the given expression147#[cfg(feature = "trigonometry")]148pub fn arcsinh(self) -> Self {149self.map_unary(TrigonometricFunction::ArcSinh)150}151152/// Compute the inverse hyperbolic tangent of the given expression153#[cfg(feature = "trigonometry")]154pub fn arctanh(self) -> Self {155self.map_unary(TrigonometricFunction::ArcTanh)156}157158/// Convert from radians to degrees159#[cfg(feature = "trigonometry")]160pub fn degrees(self) -> Self {161self.map_unary(TrigonometricFunction::Degrees)162}163164/// Convert from degrees to radians165#[cfg(feature = "trigonometry")]166pub fn radians(self) -> Self {167self.map_unary(TrigonometricFunction::Radians)168}169170/// Compute the sign of the given expression171#[cfg(feature = "sign")]172pub fn sign(self) -> Self {173self.map_unary(FunctionExpr::Sign)174}175}176177178