/
floating.cc
578 lines (441 loc) · 16.1 KB
/
floating.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//------------------------------------------------------------------------------
// Copyright 2019-2020 H2O.ai
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//------------------------------------------------------------------------------
#include <cmath>
#include "column/isna.h"
#include "expr/funary/pyfn.h"
#include "expr/funary/umaker.h"
#include "expr/funary/umaker_impl.h"
namespace dt {
namespace expr {
//------------------------------------------------------------------------------
// Op::ABS
//------------------------------------------------------------------------------
static const char* doc_abs =
R"(abs(x)
--
Return the absolute value of ``x``. This function can only be applied
to numeric arguments (i.e. boolean, integer, or real).
This function upcasts columns of types `bool8`, `int8` and `int16` into
`int32`; for columns of other types the stype is kept.
Parameters
----------
x: FExpr
Column expression producing one or more numeric columns.
return: FExpr
The resulting FExpr evaluates absolute values in all elements
in all columns of `x`.
Examples
--------
.. code-block::
>>> DT = dt.Frame(A=[-3, 2, 4, -17, 0])
>>> DT[:, abs(f.A)]
| A
| int32
-- + -----
0 | 3
1 | 2
2 | 4
3 | 17
4 | 0
[5 rows x 1 column]
See also
--------
- :func:`fabs()`
)";
py::PKArgs args_abs(1, 0, 0, false, false, {"x"}, "abs", doc_abs);
template <typename T>
static T op_abs(T x) {
return (x >= 0)? x : -x;
}
template <typename T>
static inline umaker_ptr _abs(SType uptype = SType::AUTO) {
return umaker1<T, T>::make(op_abs<T>, uptype, stype_from<T>);
}
umaker_ptr resolve_op_abs(SType stype) {
switch (stype) {
case SType::VOID: return umaker_ptr(new umaker_nacol());
case SType::BOOL: return umaker_ptr(new umaker_cast(SType::INT32));
case SType::INT8:
case SType::INT16: return _abs<int32_t>(/* uptype= */ SType::INT32);
case SType::INT32: return _abs<int32_t>();
case SType::INT64: return _abs<int64_t>();
case SType::FLOAT32: return _abs<float>();
case SType::FLOAT64: return _abs<double>();
default:
throw TypeError() << "Function `abs` cannot be applied to a "
"column of type `" << stype << "`";
}
}
//------------------------------------------------------------------------------
// Op::FABS
//------------------------------------------------------------------------------
static const char* doc_fabs =
R"(fabs(x)
--
The absolute value of `x`, returned as float.
)";
py::PKArgs args_fabs(1, 0, 0, false, false, {"x"}, "fabs", doc_fabs);
umaker_ptr resolve_op_fabs(SType stype) {
switch (stype) {
case SType::VOID: return umaker_ptr(new umaker_nacol());
case SType::BOOL: return umaker_ptr(new umaker_cast(SType::FLOAT64));
case SType::INT8:
case SType::INT16:
case SType::INT32:
case SType::INT64: return _abs<double>(/* uptype= */ SType::FLOAT64);
case SType::FLOAT32: return _abs<float>();
case SType::FLOAT64: return _abs<double>();
default:
throw TypeError() << "Function `fabs` cannot be applied to a "
"column of type `" << stype << "`";
}
}
//------------------------------------------------------------------------------
// Op::SIGN
//------------------------------------------------------------------------------
static const char* doc_sign =
R"(sign(x)
--
The sign of x, returned as float.
This function returns `1.0` if `x` is positive (including positive
infinity), `-1.0` if `x` is negative, `0.0` if `x` is zero, and NA if
`x` is NA.
)";
py::PKArgs args_sign(1, 0, 0, false, false, {"x"}, "sign", doc_sign);
template <typename T>
static T op_sign(T x) {
return (x > 0)? T(1) : (x < 0)? T(-1) : T(0);
}
template <typename T>
static inline umaker_ptr _sign(SType uptype = SType::AUTO) {
return umaker1<T, T>::make(op_sign<T>, uptype, stype_from<T>);
}
umaker_ptr resolve_op_sign(SType stype) {
switch (stype) {
case SType::VOID: return umaker_ptr(new umaker_nacol());
case SType::BOOL: return umaker_ptr(new umaker_cast(SType::FLOAT64));
case SType::INT8:
case SType::INT16:
case SType::INT32:
case SType::INT64: return _sign<double>(SType::FLOAT64);
case SType::FLOAT32: return _sign<float>();
case SType::FLOAT64: return _sign<double>();
default:
throw TypeError() << "Function `sign` cannot be applied to a "
"column of type `" << stype << "`";
}
}
//------------------------------------------------------------------------------
// Op::ISNA
//------------------------------------------------------------------------------
static const char* doc_isna =
R"(isna(x)
--
Returns `True` if the argument is NA, and `False` otherwise.
)";
py::PKArgs args_isna(1, 0, 0, false, false, {"x"}, "isna", doc_isna);
template <typename T>
class isna_umaker : public umaker {
public:
Column compute(Column&& col) const override {
return Column(new Isna_ColumnImpl<T>(std::move(col)));
}
};
umaker_ptr resolve_op_isna(SType stype) {
switch (stype) {
case SType::VOID: {
return umaker_ptr(new umaker_const(
Const_ColumnImpl::make_bool_column(1, true)));
}
case SType::BOOL:
case SType::INT8: return umaker_ptr(new isna_umaker<int8_t>());
case SType::INT16: return umaker_ptr(new isna_umaker<int16_t>());
case SType::DATE32:
case SType::INT32: return umaker_ptr(new isna_umaker<int32_t>());
case SType::TIME64:
case SType::INT64: return umaker_ptr(new isna_umaker<int64_t>());
case SType::FLOAT32: return umaker_ptr(new isna_umaker<float>());
case SType::FLOAT64: return umaker_ptr(new isna_umaker<double>());
case SType::STR32:
case SType::STR64: return umaker_ptr(new isna_umaker<CString>());
default:
throw TypeError() << "Function `isna` cannot be applied to a "
"column of type `" << stype << "`";
}
}
//------------------------------------------------------------------------------
// Op::ISINF
//------------------------------------------------------------------------------
static const char* doc_isinf =
R"(isinf(x)
--
Returns True if the argument is +/- infinity, and False otherwise.
Note that `isinf(NA) == False`.
)";
py::PKArgs args_isinf(1, 0, 0, false, false, {"x"}, "isinf", doc_isinf);
template <typename T>
static bool op_isinf(T x, bool isvalid, int8_t* out) {
*out = isvalid && std::isinf(x);
return true;
}
template <typename T>
static inline umaker_ptr _isinf() {
return umaker2<T, int8_t>::make(op_isinf<T>, SType::AUTO, SType::BOOL);
}
umaker_ptr resolve_op_isinf(SType stype) {
switch (stype) {
case SType::VOID:
case SType::BOOL:
case SType::INT8:
case SType::INT16:
case SType::INT32:
case SType::INT64: {
return umaker_ptr(new umaker_const(
Const_ColumnImpl::make_bool_column(1, false)));
}
case SType::FLOAT32: return _isinf<float>();
case SType::FLOAT64: return _isinf<double>();
default:
throw TypeError() << "Function `isinf` cannot be applied to a "
"column of type `" << stype << "`";
}
}
//------------------------------------------------------------------------------
// Op::ISFINITE
//------------------------------------------------------------------------------
static const char* doc_isfinite =
R"(isfinite(x)
--
Returns True if `x` has a finite value, and False if `x` is infinity
or NaN. This function is equivalent to ``!(isna(x) or isinf(x))``.
See also
--------
- :func:`isna()`
- :func:`isinf()`
)";
py::PKArgs args_isfinite(1, 0, 0, false, false, {"x"}, "isfinite", doc_isfinite);
template <typename T>
static bool op_isfinite(T x, bool xvalid, int8_t* out) {
*out = xvalid && std::isfinite(x);
return true;
}
template <typename T>
static bool op_notna(T, bool xvalid, int8_t* out) {
*out = xvalid;
return true;
}
template <typename T>
static umaker_ptr _isfinite_int(SType uptype = SType::AUTO) {
return umaker2<T, int8_t>::make(op_notna<T>, uptype, SType::BOOL);
}
template <typename T>
static umaker_ptr _isfinite_float() {
return umaker2<T, int8_t>::make(op_isfinite<T>, SType::AUTO, SType::BOOL);
}
umaker_ptr resolve_op_isfinite(SType stype) {
switch (stype) {
case SType::VOID: {
return umaker_ptr(new umaker_const(
Const_ColumnImpl::make_bool_column(1, false)));
}
case SType::BOOL:
case SType::INT8:
case SType::INT16: return _isfinite_int<int32_t>(SType::INT32);
case SType::INT32: return _isfinite_int<int32_t>();
case SType::INT64: return _isfinite_int<int64_t>();
case SType::FLOAT32: return _isfinite_float<float>();
case SType::FLOAT64: return _isfinite_float<double>();
default:
throw TypeError() << "Function `isfinite` cannot be applied to a "
"column of type `" << stype << "`";
}
}
//------------------------------------------------------------------------------
// Op::CEIL
//------------------------------------------------------------------------------
static const char* doc_ceil =
R"(ceil(x)
--
The smallest integer value not less than `x`, returned as float.
This function produces a ``float32`` column if the input is of type
``float32``, or ``float64`` columns for inputs of all other numeric
stypes.
Parameters
----------
x: FExpr
One or more numeric columns.
return: FExpr
Expression that computes the ``ceil()`` function for each row and
column in `x`.
)";
py::PKArgs args_ceil(1, 0, 0, false, false, {"x"}, "ceil", doc_ceil);
template <typename T>
static umaker_ptr _ceil() {
return umaker1<T, T>::make(std::ceil, SType::AUTO, stype_from<T>);
}
umaker_ptr resolve_op_ceil(SType stype) {
switch (stype) {
case SType::VOID: return umaker_ptr(new umaker_nacol());
case SType::BOOL:
case SType::INT8:
case SType::INT16:
case SType::INT32:
case SType::INT64: return umaker_ptr(new umaker_cast(SType::FLOAT64));
case SType::FLOAT32: return _ceil<float>();
case SType::FLOAT64: return _ceil<double>();
default:
throw TypeError() << "Function `ceil` cannot be applied to a "
"column of type `" << stype << "`";
}
}
//------------------------------------------------------------------------------
// Op::FLOOR
//------------------------------------------------------------------------------
static const char* doc_floor =
R"(floor(x)
--
The largest integer value not greater than `x`, returned as float.
This function produces a ``float32`` column if the input is of type
``float32``, or ``float64`` columns for inputs of all other numeric
stypes.
Parameters
----------
x: FExpr
One or more numeric columns.
return: FExpr
Expression that computes the ``floor()`` function for each row and
column in `x`.
)";
py::PKArgs args_floor(1, 0, 0, false, false, {"x"}, "floor", doc_floor);
template <typename T>
static umaker_ptr _floor() {
return umaker1<T, T>::make(std::floor, SType::AUTO, stype_from<T>);
}
umaker_ptr resolve_op_floor(SType stype) {
switch (stype) {
case SType::VOID: return umaker_ptr(new umaker_nacol());
case SType::BOOL:
case SType::INT8:
case SType::INT16:
case SType::INT32:
case SType::INT64: return umaker_ptr(new umaker_cast(SType::FLOAT64));
case SType::FLOAT32: return _floor<float>();
case SType::FLOAT64: return _floor<double>();
default:
throw TypeError() << "Function `floor` cannot be applied to a "
"column of type `" << stype << "`";
}
}
//------------------------------------------------------------------------------
// Op::RINT
//------------------------------------------------------------------------------
static const char* doc_rint =
R"(rint(x)
--
Round the value `x` to the nearest integer.
)";
py::PKArgs args_rint(1, 0, 0, false, false, {"x"}, "rint", doc_rint);
template <typename T>
static umaker_ptr _rint() {
return umaker1<T, T>::make(std::rint, SType::AUTO, stype_from<T>);
}
umaker_ptr resolve_op_rint(SType stype) {
switch (stype) {
case SType::VOID: return umaker_ptr(new umaker_nacol());
case SType::BOOL:
case SType::INT8:
case SType::INT16:
case SType::INT32:
case SType::INT64: return umaker_ptr(new umaker_cast(SType::FLOAT64));
case SType::FLOAT32: return _rint<float>();
case SType::FLOAT64: return _rint<double>();
default:
throw TypeError() << "Function `rint` cannot be applied to a "
"column of type `" << stype << "`";
}
}
//------------------------------------------------------------------------------
// Op::TRUNC
//------------------------------------------------------------------------------
static const char* doc_trunc =
R"(trunc(x)
--
The nearest integer value not greater than `x` in magnitude.
If x is integer or boolean, then trunc() will return this value
converted to float64. If x is floating-point, then trunc(x) acts as
floor(x) for positive values of x, and as ceil(x) for negative values
of x. This rounding mode is known as rounding towards zero.
)";
py::PKArgs args_trunc(1, 0, 0, false, false, {"x"}, "trunc", doc_trunc);
template <typename T>
static umaker_ptr _trunc() {
return umaker1<T, T>::make(std::trunc, SType::AUTO, stype_from<T>);
}
umaker_ptr resolve_op_trunc(SType stype) {
switch (stype) {
case SType::VOID: return umaker_ptr(new umaker_nacol());
case SType::BOOL:
case SType::INT8:
case SType::INT16:
case SType::INT32:
case SType::INT64: return umaker_ptr(new umaker_cast(SType::FLOAT64));
case SType::FLOAT32: return _trunc<float>();
case SType::FLOAT64: return _trunc<double>();
default:
throw TypeError() << "Function `trunc` cannot be applied to a "
"column of type `" << stype << "`";
}
}
//------------------------------------------------------------------------------
// Op::SIGNBIT
//------------------------------------------------------------------------------
static const char* doc_signbit =
R"(signbit(x)
--
Returns `True` if `x` is negative (its sign bit is set), and `False` if
`x` is positive. This function is able to distinguish between `-0.0` and
`+0.0`, returning `True`/`False` respectively. If `x` is an NA value, this
function will also return NA.
)";
py::PKArgs args_signbit(1, 0, 0, false, false, {"x"}, "signbit", doc_signbit);
template <typename T>
static int8_t op_signbit(T x) { return std::signbit(x); }
template <typename T>
static umaker_ptr _signbit(SType uptype = SType::AUTO) {
return umaker1<T, int8_t>::make(op_signbit<T>, uptype, SType::BOOL);
}
umaker_ptr resolve_op_signbit(SType stype) {
switch (stype) {
case SType::VOID: return umaker_ptr(new umaker_nacol());
case SType::BOOL:
case SType::INT8:
case SType::INT16:
case SType::INT32:
case SType::INT64: return _signbit<double>(SType::FLOAT64);
case SType::FLOAT32: return _signbit<float>();
case SType::FLOAT64: return _signbit<double>();
default:
throw TypeError() << "Function `signbit` cannot be applied to a "
"column of type `" << stype << "`";
}
}
}} // namespace dt::expr