/
rowminmax.cc
227 lines (181 loc) · 5.69 KB
/
rowminmax.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
//------------------------------------------------------------------------------
// Copyright 2019-2021 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 <algorithm>
#include "column/const.h"
#include "column/func_nary.h"
#include "expr/fnary/fnary.h"
#include "python/xargs.h"
namespace dt {
namespace expr {
template <bool MIN>
std::string FExpr_RowMinMax<MIN>::name() const {
return MIN? "rowmin" : "rowmax";
}
template <typename T, bool MIN>
static bool op_rowminmax(size_t i, T* out, const colvec& columns) {
bool minmax_valid = false;
T minmax = 0;
for (const auto& col : columns) {
T x;
bool xvalid = col.get_element(i, &x);
if (!xvalid) continue;
if (minmax_valid) {
if (MIN) { if (x < minmax) minmax = x; }
else { if (x > minmax) minmax = x; }
} else {
minmax = x;
minmax_valid = true;
}
}
*out = minmax;
return minmax_valid;
}
template <typename T, bool MIN>
static inline Column _rowminmax(colvec&& columns) {
auto fn = op_rowminmax<T, MIN>;
size_t nrows = columns[0].nrows();
return Column(new FuncNary_ColumnImpl<T>(
std::move(columns), fn, nrows, stype_from<T>));
}
template <bool MIN>
Column FExpr_RowMinMax<MIN>::apply_function(colvec&& columns) const {
if (columns.empty()) {
return Const_ColumnImpl::make_na_column(1);
}
SType res_stype = common_numeric_stype(columns);
promote_columns(columns, res_stype);
switch (res_stype) {
case SType::INT32: return _rowminmax<int32_t, MIN>(std::move(columns));
case SType::INT64: return _rowminmax<int64_t, MIN>(std::move(columns));
case SType::FLOAT32: return _rowminmax<float, MIN>(std::move(columns));
case SType::FLOAT64: return _rowminmax<double, MIN>(std::move(columns));
default: throw RuntimeError()
<< "Wrong `res_stype` in `naryop_rowminmax()`: "
<< res_stype; // LCOV_EXCL_LINE
}
}
template class FExpr_RowMinMax<true>;
template class FExpr_RowMinMax<false>;
static const char* doc_rowmin =
R"(rowmin(*cols)
--
For each row, find the smallest value among the columns from `cols`,
excluding missing values.
Parameters
----------
cols: FExpr
Input columns.
return: FExpr
f-expression consisting of one column that has the same number of rows
as in `cols`. The column stype is the smallest common stype
for `cols`, but not less than `int32`.
except: TypeError
The exception is raised when `cols` has non-numeric columns.
Examples
--------
::
>>> from datatable import dt, f
>>> DT = dt.Frame({"A": [1, 1, 2, 1, 2],
... "B": [None, 2, 3, 4, None],
... "C":[True, False, False, True, True]})
>>> DT
| A B C
| int32 int32 bool8
-- + ----- ----- -----
0 | 1 NA 1
1 | 1 2 0
2 | 2 3 0
3 | 1 4 1
4 | 2 NA 1
[5 rows x 3 columns]
::
>>> DT[:, dt.rowmin(f[:])]
| C0
| int32
-- + -----
0 | 1
1 | 0
2 | 0
3 | 1
4 | 1
[5 rows x 1 column]
See Also
--------
- :func:`rowmax()` -- find the largest element row-wise.
)";
static const char* doc_rowmax =
R"(rowmax(*cols)
--
For each row, find the largest value among the columns from `cols`.
Parameters
----------
cols: FExpr
Input columns.
return: FExpr
f-expression consisting of one column that has the same number of rows
as in `cols`. The column stype is the smallest common stype
for `cols`, but not less than `int32`.
except: TypeError
The exception is raised when `cols` has non-numeric columns.
Examples
--------
::
>>> from datatable import dt, f
>>> DT = dt.Frame({"A": [1, 1, 2, 1, 2],
... "B": [None, 2, 3, 4, None],
... "C":[True, False, False, True, True]})
>>> DT
| A B C
| int32 int32 bool8
-- + ----- ----- -----
0 | 1 NA 1
1 | 1 2 0
2 | 2 3 0
3 | 1 4 1
4 | 2 NA 1
[5 rows x 3 columns]
::
>>> DT[:, dt.rowmax(f[:])]
| C0
| int32
-- + -----
0 | 1
1 | 2
2 | 3
3 | 4
4 | 2
[5 rows x 1 column]
See Also
--------
- :func:`rowmin()` -- find the smallest element row-wise.
)";
DECLARE_PYFN(&py_rowfn)
->name("rowmin")
->docs(doc_rowmin)
->allow_varargs()
->add_info(FN_ROWMIN);
DECLARE_PYFN(&py_rowfn)
->name("rowmax")
->docs(doc_rowmax)
->allow_varargs()
->add_info(FN_ROWMAX);
}} // namespace dt::expr