polars_plan/dsl/
arithmetic.rs

1use std::ops::{Add, Div, Mul, Neg, Rem, Sub};
2
3use super::*;
4
5// Arithmetic ops
6impl Add for Expr {
7    type Output = Expr;
8
9    fn add(self, rhs: Self) -> Self::Output {
10        binary_expr(self, Operator::Plus, rhs)
11    }
12}
13
14impl Sub for Expr {
15    type Output = Expr;
16
17    fn sub(self, rhs: Self) -> Self::Output {
18        binary_expr(self, Operator::Minus, rhs)
19    }
20}
21
22impl Div for Expr {
23    type Output = Expr;
24
25    fn div(self, rhs: Self) -> Self::Output {
26        binary_expr(self, Operator::Divide, rhs)
27    }
28}
29
30impl Mul for Expr {
31    type Output = Expr;
32
33    fn mul(self, rhs: Self) -> Self::Output {
34        binary_expr(self, Operator::Multiply, rhs)
35    }
36}
37
38impl Rem for Expr {
39    type Output = Expr;
40
41    fn rem(self, rhs: Self) -> Self::Output {
42        binary_expr(self, Operator::Modulus, rhs)
43    }
44}
45
46impl Neg for Expr {
47    type Output = Expr;
48
49    fn neg(self) -> Self::Output {
50        self.map_unary(FunctionExpr::Negate)
51    }
52}
53
54impl Expr {
55    /// Floor divide `self` by `rhs`.
56    pub fn floor_div(self, rhs: Self) -> Self {
57        binary_expr(self, Operator::FloorDivide, rhs)
58    }
59
60    /// Raise expression to the power `exponent`
61    pub fn pow<E: Into<Expr>>(self, exponent: E) -> Self {
62        self.map_binary(PowFunction::Generic, exponent.into())
63    }
64
65    /// Compute the square root of the given expression
66    pub fn sqrt(self) -> Self {
67        self.map_unary(PowFunction::Sqrt)
68    }
69
70    /// Compute the cube root of the given expression
71    pub fn cbrt(self) -> Self {
72        self.map_unary(PowFunction::Cbrt)
73    }
74
75    /// Compute the cosine of the given expression
76    #[cfg(feature = "trigonometry")]
77    pub fn cos(self) -> Self {
78        self.map_unary(TrigonometricFunction::Cos)
79    }
80
81    /// Compute the cotangent of the given expression
82    #[cfg(feature = "trigonometry")]
83    pub fn cot(self) -> Self {
84        self.map_unary(TrigonometricFunction::Cot)
85    }
86
87    /// Compute the sine of the given expression
88    #[cfg(feature = "trigonometry")]
89    pub fn sin(self) -> Self {
90        self.map_unary(TrigonometricFunction::Sin)
91    }
92
93    /// Compute the tangent of the given expression
94    #[cfg(feature = "trigonometry")]
95    pub fn tan(self) -> Self {
96        self.map_unary(TrigonometricFunction::Tan)
97    }
98
99    /// Compute the inverse cosine of the given expression
100    #[cfg(feature = "trigonometry")]
101    pub fn arccos(self) -> Self {
102        self.map_unary(TrigonometricFunction::ArcCos)
103    }
104
105    /// Compute the inverse sine of the given expression
106    #[cfg(feature = "trigonometry")]
107    pub fn arcsin(self) -> Self {
108        self.map_unary(TrigonometricFunction::ArcSin)
109    }
110
111    /// Compute the inverse tangent of the given expression
112    #[cfg(feature = "trigonometry")]
113    pub fn arctan(self) -> Self {
114        self.map_unary(TrigonometricFunction::ArcTan)
115    }
116
117    /// Compute the inverse tangent of the given expression, with the angle expressed as the argument of a complex number
118    #[cfg(feature = "trigonometry")]
119    pub fn arctan2(self, x: Self) -> Self {
120        self.map_binary(FunctionExpr::Atan2, x)
121    }
122
123    /// Compute the hyperbolic cosine of the given expression
124    #[cfg(feature = "trigonometry")]
125    pub fn cosh(self) -> Self {
126        self.map_unary(TrigonometricFunction::Cosh)
127    }
128
129    /// Compute the hyperbolic sine of the given expression
130    #[cfg(feature = "trigonometry")]
131    pub fn sinh(self) -> Self {
132        self.map_unary(TrigonometricFunction::Sinh)
133    }
134
135    /// Compute the hyperbolic tangent of the given expression
136    #[cfg(feature = "trigonometry")]
137    pub fn tanh(self) -> Self {
138        self.map_unary(TrigonometricFunction::Tanh)
139    }
140
141    /// Compute the inverse hyperbolic cosine of the given expression
142    #[cfg(feature = "trigonometry")]
143    pub fn arccosh(self) -> Self {
144        self.map_unary(TrigonometricFunction::ArcCosh)
145    }
146
147    /// Compute the inverse hyperbolic sine of the given expression
148    #[cfg(feature = "trigonometry")]
149    pub fn arcsinh(self) -> Self {
150        self.map_unary(TrigonometricFunction::ArcSinh)
151    }
152
153    /// Compute the inverse hyperbolic tangent of the given expression
154    #[cfg(feature = "trigonometry")]
155    pub fn arctanh(self) -> Self {
156        self.map_unary(TrigonometricFunction::ArcTanh)
157    }
158
159    /// Convert from radians to degrees
160    #[cfg(feature = "trigonometry")]
161    pub fn degrees(self) -> Self {
162        self.map_unary(TrigonometricFunction::Degrees)
163    }
164
165    /// Convert from degrees to radians
166    #[cfg(feature = "trigonometry")]
167    pub fn radians(self) -> Self {
168        self.map_unary(TrigonometricFunction::Radians)
169    }
170
171    /// Compute the sign of the given expression
172    #[cfg(feature = "sign")]
173    pub fn sign(self) -> Self {
174        self.map_unary(FunctionExpr::Sign)
175    }
176}