Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/api/fexpr/__invert__.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

.. xmethod:: datatable.FExpr.__invert__
:src: src/core/expr/fexpr.cc PyFExpr::nb__invert__
:src: src/core/expr/funary/unary_invert.cc PyFExpr::nb__invert__

__invert__(x)
--
Expand Down
2 changes: 1 addition & 1 deletion docs/api/fexpr/__neg__.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

.. xmethod:: datatable.FExpr.__neg__
:src: src/core/expr/fexpr.cc PyFExpr::nb__neg__
:src: src/core/expr/funary/unary_minus.cc PyFExpr::nb__neg__

__neg__(x)
--
Expand Down
2 changes: 1 addition & 1 deletion docs/api/fexpr/__pos__.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

.. xmethod:: datatable.FExpr.__pos__
:src: src/core/expr/fexpr.cc PyFExpr::nb__pos__
:src: src/core/expr/funary/unary_plus.cc PyFExpr::nb__pos__

__pos__(x)
--
Expand Down
22 changes: 0 additions & 22 deletions src/core/expr/fexpr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,6 @@ oobj PyFExpr::m__getitem__(py::robj item) {


//----- Basic arithmetics ------------------------------------------------------

static oobj make_unexpr(dt::expr::Op op, const PyObject* self) {
return robj(Expr_Type).call({
oint(static_cast<int>(op)),
otuple{oobj(self)}});
}

static oobj make_binexpr(dt::expr::Op op, robj lhs, robj rhs) {
return robj(Expr_Type).call({
oint(static_cast<int>(op)),
Expand Down Expand Up @@ -215,21 +208,6 @@ bool PyFExpr::nb__bool__() {
" f.B != 0\n";
}

oobj PyFExpr::nb__invert__() {
return make_unexpr(dt::expr::Op::UINVERT, this);
}

oobj PyFExpr::nb__neg__() {
return make_unexpr(dt::expr::Op::UMINUS, this);
}

oobj PyFExpr::nb__pos__() {
return make_unexpr(dt::expr::Op::UPLUS, this);
}




//----- Other methods ----------------------------------------------------------

oobj PyFExpr::extend(const XArgs& args) {
Expand Down
6 changes: 3 additions & 3 deletions src/core/expr/fexpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ class PyFExpr : public py::XObject<PyFExpr> {
static py::oobj nb__sub__ (py::robj, py::robj);
static py::oobj nb__mul__ (py::robj, py::robj);
static py::oobj nb__floordiv__(py::robj, py::robj);
static py::oobj nb__invert__ (py::robj);
static py::oobj nb__neg__ (py::robj);
static py::oobj nb__pos__ (py::robj);
static py::oobj nb__truediv__ (py::robj, py::robj);
static py::oobj nb__mod__ (py::robj, py::robj);
static py::oobj nb__pow__ (py::robj, py::robj, py::robj);
Expand All @@ -172,9 +175,6 @@ class PyFExpr : public py::XObject<PyFExpr> {
static py::oobj nb__lshift__ (py::robj, py::robj);
static py::oobj nb__rshift__ (py::robj, py::robj);
bool nb__bool__();
py::oobj nb__invert__();
py::oobj nb__neg__();
py::oobj nb__pos__();

py::oobj len(); // [DEPRECATED]
py::oobj re_match(const py::XArgs&); // [DEPRECATED]
Expand Down
5 changes: 0 additions & 5 deletions src/core/expr/funary/umaker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ static const umaker_ptr& get_umaker(Op opcode, SType stype) {
umaker_ptr resolve_op(Op opcode, SType stype)
{
switch (opcode) {
// Basic
case Op::UPLUS: return resolve_op_uplus(stype);
case Op::UMINUS: return resolve_op_uminus(stype);
case Op::UINVERT: return resolve_op_uinvert(stype);

// Math: trigonometric
case Op::SIN: return resolve_op_sin(stype);
case Op::COS: return resolve_op_cos(stype);
Expand Down
5 changes: 0 additions & 5 deletions src/core/expr/funary/umaker.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ using umaker_ptr = std::unique_ptr<umaker>;
// Main resolver, calls individual-op resolvers below
umaker_ptr resolve_op(Op, SType);

// Basic
umaker_ptr resolve_op_uplus(SType);
umaker_ptr resolve_op_uminus(SType);
umaker_ptr resolve_op_uinvert(SType);

// Trigonometric
umaker_ptr resolve_op_sin(SType);
umaker_ptr resolve_op_cos(SType);
Expand Down
92 changes: 92 additions & 0 deletions src/core/expr/funary/unary_invert.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//------------------------------------------------------------------------------
// Copyright 2022-2023 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 "column/const.h"
#include "column/func_unary.h"
#include "expr/fexpr_func_unary.h"
#include "python/xargs.h"
#include "stype.h"
namespace dt {
namespace expr {


class FExpr_UInvert : public FExpr_FuncUnary {
public:
using FExpr_FuncUnary::FExpr_FuncUnary;


std::string name() const override {
return "uinvert";
}

template <typename T>
static inline T op_invert(T x) {
return ~x;
}

static inline int8_t op_invert_bool(int8_t x) {
return !x;
}
/**
* Unary operator `~` acts as logical NOT on a boolean column,
* and as a bitwise inverse on integer columns. Integer promotions
* are not applied. The operator is not applicable to floating-point
* or string columns.
*/
Column evaluate1(Column&& col) const override{
SType stype = col.stype();

switch (stype) {
case SType::VOID:
return Column(new ConstNa_ColumnImpl(col.nrows(), SType::VOID));
case SType::BOOL:
return Column(new FuncUnary1_ColumnImpl<int8_t, int8_t>(
std::move(col), op_invert_bool, col.nrows(), SType::BOOL
));
case SType::INT8:
return Column(new FuncUnary1_ColumnImpl<int8_t, int8_t>(
std::move(col), op_invert<int8_t>, col.nrows(), SType::INT8
));
case SType::INT16:
return Column(new FuncUnary1_ColumnImpl<int16_t, int16_t>(
std::move(col), op_invert<int16_t>, col.nrows(), SType::INT16
));
case SType::INT32:
return Column(new FuncUnary1_ColumnImpl<int32_t, int32_t>(
std::move(col), op_invert<int32_t>, col.nrows(), SType::INT32
));
case SType::INT64:
return Column(new FuncUnary1_ColumnImpl<int64_t, int64_t>(
std::move(col), op_invert<int64_t>, col.nrows(), SType::INT64
));
default:
throw TypeError() << "Cannot apply unary `operator ~` to a column with "
"stype `" << stype << "`";
}
}
};

py::oobj PyFExpr::nb__invert__(py::robj lhs) {
return PyFExpr::make(
new FExpr_UInvert(as_fexpr(lhs)));
}

}} // dt::expr
86 changes: 86 additions & 0 deletions src/core/expr/funary/unary_minus.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//------------------------------------------------------------------------------
// Copyright 2022-2023 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 "column/const.h"
#include "column/func_unary.h"
#include "expr/fexpr_func_unary.h"
#include "python/xargs.h"
#include "stype.h"
namespace dt {
namespace expr {


class FExpr_UnaryMinus : public FExpr_FuncUnary {
public:
using FExpr_FuncUnary::FExpr_FuncUnary;


std::string name() const override {
return "uminus";
}

template <typename T>
static inline T op_minus(T x) {
return -x;
}

Column evaluate1(Column&& col) const override{
SType stype = col.stype();

switch (stype) {
case SType::VOID:
return Column(new ConstNa_ColumnImpl(col.nrows(), SType::VOID));
case SType::BOOL:
case SType::INT8:
case SType::INT16:
col.cast_inplace(SType::INT32);
return Column(new FuncUnary1_ColumnImpl<int32_t, int32_t>(
std::move(col), op_minus<int32_t>, col.nrows(), SType::INT32
));
case SType::INT32:
return Column(new FuncUnary1_ColumnImpl<int32_t, int32_t>(
std::move(col), op_minus<int32_t>, col.nrows(), SType::INT32
));
case SType::INT64:
return Column(new FuncUnary1_ColumnImpl<int64_t, int64_t>(
std::move(col), op_minus<int64_t>, col.nrows(), SType::INT64
));
case SType::FLOAT32:
return Column(new FuncUnary1_ColumnImpl<float, float>(
std::move(col), op_minus<float>, col.nrows(), SType::FLOAT32
));
case SType::FLOAT64:
return Column(new FuncUnary1_ColumnImpl<double, double>(
std::move(col), op_minus<double>, col.nrows(), SType::FLOAT64
));
default:
throw TypeError() << "Cannot apply unary `operator -` to a column with "
"stype `" << stype << "`";
}
}
};

py::oobj PyFExpr::nb__neg__(py::robj src) {
return PyFExpr::make(
new FExpr_UnaryMinus(as_fexpr(src)));
}

}} // dt::expr
73 changes: 73 additions & 0 deletions src/core/expr/funary/unary_plus.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//------------------------------------------------------------------------------
// Copyright 2022-2023 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 "column/const.h"
#include "expr/fexpr_column.h"
#include "expr/fexpr_func_unary.h"
#include "python/xargs.h"
#include "stype.h"
namespace dt {
namespace expr {


class FExpr_UPlus : public FExpr_FuncUnary {
public:
using FExpr_FuncUnary::FExpr_FuncUnary;


std::string name() const override {
return "uplus";
}
/**
* Unary operator `+` upcasts each numeric column to INT32, but
* otherwise keeps unmodified. The operator cannot be applied to
* string columns.
*/
Column evaluate1(Column&& col) const override{
SType stype = col.stype();

switch (stype) {
case SType::VOID:
return Column(new ConstNa_ColumnImpl(col.nrows(), SType::VOID));
case SType::BOOL:
case SType::INT8:
case SType::INT16:
col.cast_inplace(SType::INT32);
return std::move(col);
case SType::INT32:
case SType::INT64:
case SType::FLOAT32:
case SType::FLOAT64:
return std::move(col);

default:
throw TypeError() << "Cannot apply unary `operator +` to a column with "
"stype `" << stype << "`";
}
}
};

py::oobj PyFExpr::nb__pos__(py::robj src) {
return PyFExpr::make(
new FExpr_UPlus(as_fexpr(src)));
}

}} // dt::expr
17 changes: 14 additions & 3 deletions src/core/python/xobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,17 @@ PyObject* _safe_unary(PyObject* self) noexcept {
}
}

template <py::oobj(*METH)(py::robj), int OP>
PyObject* _safe_uunary(PyObject* self) noexcept {
auto cl = dt::CallLogger::unaryfn(self, OP);
try {
return METH(py::robj(self)).release();
} catch (const std::exception& e) {
exception_to_python(e);
return nullptr;
}
}


template <py::oobj(*METH)(py::robj, py::robj), int OP>
PyObject* _safe_binary(PyObject* self, PyObject* other) noexcept {
Expand Down Expand Up @@ -763,12 +774,12 @@ PyObject* _safe_cmp(PyObject* x, PyObject* y, int op) noexcept {
*/

#define METHOD__NEG__(METH) \
py::_safe_unary<CLASS_OF(METH), METH, dt::CallLogger::Op::__neg__>, \
py::_safe_uunary<METH, dt::CallLogger::Op::__neg__>, \
py::XTypeMaker::nb_negative_tag


#define METHOD__POS__(METH) \
py::_safe_unary<CLASS_OF(METH), METH, dt::CallLogger::Op::__pos__>, \
py::_safe_uunary<METH, dt::CallLogger::Op::__pos__>, \
py::XTypeMaker::nb_positive_tag


Expand All @@ -778,7 +789,7 @@ PyObject* _safe_cmp(PyObject* x, PyObject* y, int op) noexcept {


#define METHOD__INVERT__(METH) \
py::_safe_unary<CLASS_OF(METH), METH, dt::CallLogger::Op::__invert__>, \
py::_safe_uunary<METH, dt::CallLogger::Op::__invert__>, \
py::XTypeMaker::nb_invert_tag


Expand Down