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_private(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_many_private(
63            FunctionExpr::Pow(PowFunction::Generic),
64            &[exponent.into()],
65            false,
66            None,
67        )
68    }
69
70    /// Compute the square root of the given expression
71    pub fn sqrt(self) -> Self {
72        self.map_private(FunctionExpr::Pow(PowFunction::Sqrt))
73    }
74
75    /// Compute the cube root of the given expression
76    pub fn cbrt(self) -> Self {
77        self.map_private(FunctionExpr::Pow(PowFunction::Cbrt))
78    }
79
80    /// Compute the cosine of the given expression
81    #[cfg(feature = "trigonometry")]
82    pub fn cos(self) -> Self {
83        self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::Cos))
84    }
85
86    /// Compute the cotangent of the given expression
87    #[cfg(feature = "trigonometry")]
88    pub fn cot(self) -> Self {
89        self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::Cot))
90    }
91
92    /// Compute the sine of the given expression
93    #[cfg(feature = "trigonometry")]
94    pub fn sin(self) -> Self {
95        self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::Sin))
96    }
97
98    /// Compute the tangent of the given expression
99    #[cfg(feature = "trigonometry")]
100    pub fn tan(self) -> Self {
101        self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::Tan))
102    }
103
104    /// Compute the inverse cosine of the given expression
105    #[cfg(feature = "trigonometry")]
106    pub fn arccos(self) -> Self {
107        self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::ArcCos))
108    }
109
110    /// Compute the inverse sine of the given expression
111    #[cfg(feature = "trigonometry")]
112    pub fn arcsin(self) -> Self {
113        self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::ArcSin))
114    }
115
116    /// Compute the inverse tangent of the given expression
117    #[cfg(feature = "trigonometry")]
118    pub fn arctan(self) -> Self {
119        self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::ArcTan))
120    }
121
122    /// Compute the inverse tangent of the given expression, with the angle expressed as the argument of a complex number
123    #[cfg(feature = "trigonometry")]
124    pub fn arctan2(self, x: Self) -> Self {
125        self.map_many_private(FunctionExpr::Atan2, &[x], false, None)
126    }
127
128    /// Compute the hyperbolic cosine of the given expression
129    #[cfg(feature = "trigonometry")]
130    pub fn cosh(self) -> Self {
131        self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::Cosh))
132    }
133
134    /// Compute the hyperbolic sine of the given expression
135    #[cfg(feature = "trigonometry")]
136    pub fn sinh(self) -> Self {
137        self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::Sinh))
138    }
139
140    /// Compute the hyperbolic tangent of the given expression
141    #[cfg(feature = "trigonometry")]
142    pub fn tanh(self) -> Self {
143        self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::Tanh))
144    }
145
146    /// Compute the inverse hyperbolic cosine of the given expression
147    #[cfg(feature = "trigonometry")]
148    pub fn arccosh(self) -> Self {
149        self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::ArcCosh))
150    }
151
152    /// Compute the inverse hyperbolic sine of the given expression
153    #[cfg(feature = "trigonometry")]
154    pub fn arcsinh(self) -> Self {
155        self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::ArcSinh))
156    }
157
158    /// Compute the inverse hyperbolic tangent of the given expression
159    #[cfg(feature = "trigonometry")]
160    pub fn arctanh(self) -> Self {
161        self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::ArcTanh))
162    }
163
164    /// Convert from radians to degrees
165    #[cfg(feature = "trigonometry")]
166    pub fn degrees(self) -> Self {
167        self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::Degrees))
168    }
169
170    /// Convert from degrees to radians
171    #[cfg(feature = "trigonometry")]
172    pub fn radians(self) -> Self {
173        self.map_private(FunctionExpr::Trigonometry(TrigonometricFunction::Radians))
174    }
175
176    /// Compute the sign of the given expression
177    #[cfg(feature = "sign")]
178    pub fn sign(self) -> Self {
179        self.map_private(FunctionExpr::Sign)
180    }
181}