Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/sql/test_trigonometric.py
6939 views
1
from __future__ import annotations
2
3
import math
4
5
import polars as pl
6
from polars.testing import assert_frame_equal
7
8
9
def test_arctan2() -> None:
10
twoRootTwo = math.sqrt(2) / 2.0
11
df = pl.DataFrame( # noqa: F841
12
{
13
"y": [twoRootTwo, -twoRootTwo, twoRootTwo, -twoRootTwo],
14
"x": [twoRootTwo, twoRootTwo, -twoRootTwo, -twoRootTwo],
15
}
16
)
17
res = pl.sql(
18
"""
19
SELECT
20
ATAN2D(y,x) as "atan2d",
21
ATAN2(y,x) as "atan2"
22
FROM df
23
""",
24
eager=True,
25
)
26
df_result = pl.DataFrame({"atan2d": [45.0, -45.0, 135.0, -135.0]})
27
df_result = df_result.with_columns(pl.col("atan2d").cast(pl.Float64))
28
df_result = df_result.with_columns(pl.col("atan2d").radians().alias("atan2"))
29
30
assert_frame_equal(df_result, res)
31
32
33
def test_trig() -> None:
34
df = pl.DataFrame(
35
{
36
"a": [-4.0, -3.0, -2.0, -1.00001, 0.0, 1.00001, 2.0, 3.0, 4.0],
37
}
38
)
39
40
ctx = pl.SQLContext(df=df)
41
res = ctx.execute(
42
"""
43
SELECT
44
asin(1.0)/a as "pi values",
45
cos(asin(1.0)/a) AS "cos",
46
cot(asin(1.0)/a) AS "cot",
47
sin(asin(1.0)/a) AS "sin",
48
tan(asin(1.0)/a) AS "tan",
49
50
cosd(asind(1.0)/a) AS "cosd",
51
cotd(asind(1.0)/a) AS "cotd",
52
sind(asind(1.0)/a) AS "sind",
53
tand(asind(1.0)/a) AS "tand",
54
55
1.0/a as "inverse pi values",
56
acos(1.0/a) AS "acos",
57
asin(1.0/a) AS "asin",
58
atan(1.0/a) AS "atan",
59
60
acosd(1.0/a) AS "acosd",
61
asind(1.0/a) AS "asind",
62
atand(1.0/a) AS "atand"
63
FROM df
64
""",
65
eager=True,
66
)
67
68
df_result = pl.DataFrame(
69
{
70
"pi values": [
71
-0.392699,
72
-0.523599,
73
-0.785398,
74
-1.570781,
75
float("inf"),
76
1.570781,
77
0.785398,
78
0.523599,
79
0.392699,
80
],
81
"cos": [
82
0.92388,
83
0.866025,
84
0.707107,
85
0.000016,
86
float("nan"),
87
0.000016,
88
0.707107,
89
0.866025,
90
0.92388,
91
],
92
"cot": [
93
-2.414214,
94
-1.732051,
95
-1.0,
96
-0.000016,
97
float("nan"),
98
0.000016,
99
1.0,
100
1.732051,
101
2.414214,
102
],
103
"sin": [
104
-0.382683,
105
-0.5,
106
-0.707107,
107
-1.0,
108
float("nan"),
109
1,
110
0.707107,
111
0.5,
112
0.382683,
113
],
114
"tan": [
115
-0.414214,
116
-0.57735,
117
-1,
118
-63662.613851,
119
float("nan"),
120
63662.613851,
121
1,
122
0.57735,
123
0.414214,
124
],
125
"cosd": [
126
0.92388,
127
0.866025,
128
0.707107,
129
0.000016,
130
float("nan"),
131
0.000016,
132
0.707107,
133
0.866025,
134
0.92388,
135
],
136
"cotd": [
137
-2.414214,
138
-1.732051,
139
-1.0,
140
-0.000016,
141
float("nan"),
142
0.000016,
143
1.0,
144
1.732051,
145
2.414214,
146
],
147
"sind": [
148
-0.382683,
149
-0.5,
150
-0.707107,
151
-1.0,
152
float("nan"),
153
1,
154
0.707107,
155
0.5,
156
0.382683,
157
],
158
"tand": [
159
-0.414214,
160
-0.57735,
161
-1,
162
-63662.613851,
163
float("nan"),
164
63662.613851,
165
1,
166
0.57735,
167
0.414214,
168
],
169
"inverse pi values": [
170
-0.25,
171
-0.333333,
172
-0.5,
173
-0.99999,
174
float("inf"),
175
0.99999,
176
0.5,
177
0.333333,
178
0.25,
179
],
180
"acos": [
181
1.823477,
182
1.910633,
183
2.094395,
184
3.137121,
185
float("nan"),
186
0.004472,
187
1.047198,
188
1.230959,
189
1.318116,
190
],
191
"asin": [
192
-0.25268,
193
-0.339837,
194
-0.523599,
195
-1.566324,
196
float("nan"),
197
1.566324,
198
0.523599,
199
0.339837,
200
0.25268,
201
],
202
"atan": [
203
-0.244979,
204
-0.321751,
205
-0.463648,
206
-0.785393,
207
1.570796,
208
0.785393,
209
0.463648,
210
0.321751,
211
0.244979,
212
],
213
"acosd": [
214
104.477512,
215
109.471221,
216
120.0,
217
179.743767,
218
float("nan"),
219
0.256233,
220
60.0,
221
70.528779,
222
75.522488,
223
],
224
"asind": [
225
-14.477512,
226
-19.471221,
227
-30.0,
228
-89.743767,
229
float("nan"),
230
89.743767,
231
30.0,
232
19.471221,
233
14.477512,
234
],
235
"atand": [
236
-14.036243,
237
-18.434949,
238
-26.565051,
239
-44.999714,
240
90.0,
241
44.999714,
242
26.565051,
243
18.434949,
244
14.036243,
245
],
246
}
247
)
248
249
assert_frame_equal(left=df_result, right=res, abs_tol=1e-5)
250
251