|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// Copyright 2022-2023 H2O.ai |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | +// copy of this software and associated documentation files (the "Software"), |
| 6 | +// to deal in the Software without restriction, including without limitation |
| 7 | +// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | +// and/or sell copies of the Software, and to permit persons to whom the |
| 9 | +// Software is furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in |
| 12 | +// all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 | +// IN THE SOFTWARE. |
| 21 | +//------------------------------------------------------------------------------ |
| 22 | +#include "column/const.h" |
| 23 | +#include "column/func_unary.h" |
| 24 | +#include "expr/fexpr_column.h" |
| 25 | +#include "expr/fexpr_func_unary.h" |
| 26 | +#include "expr/eval_context.h" |
| 27 | +#include "expr/workframe.h" |
| 28 | +#include "expr/fexpr.h" |
| 29 | +#include "python/xargs.h" |
| 30 | +#include "stype.h" |
| 31 | +#include <iostream> |
| 32 | +namespace dt { |
| 33 | +namespace expr { |
| 34 | + |
| 35 | + |
| 36 | +class FExpr_UnaryMinus : public FExpr_FuncUnary { |
| 37 | + public: |
| 38 | + using FExpr_FuncUnary::FExpr_FuncUnary; |
| 39 | + |
| 40 | + |
| 41 | + std::string name() const override { |
| 42 | + return "uminus"; |
| 43 | + } |
| 44 | + |
| 45 | + template <typename T> |
| 46 | + static inline T op_minus(T x) { |
| 47 | + return -x; |
| 48 | + } |
| 49 | + /** |
| 50 | + * Unary operator `+` upcasts each numeric column to INT32, but |
| 51 | + * otherwise keeps unmodified. The operator cannot be applied to |
| 52 | + * string columns. |
| 53 | + */ |
| 54 | + Column evaluate1(Column&& col) const override{ |
| 55 | + SType stype = col.stype(); |
| 56 | + |
| 57 | + switch (stype) { |
| 58 | + case SType::VOID: |
| 59 | + return Column(new ConstNa_ColumnImpl(col.nrows(), SType::VOID)); |
| 60 | + case SType::BOOL: |
| 61 | + case SType::INT8: |
| 62 | + case SType::INT16: |
| 63 | + col.cast_inplace(SType::INT32); |
| 64 | + return Column(new FuncUnary1_ColumnImpl<int32_t, int32_t>( |
| 65 | + std::move(col), op_minus<int32_t>, col.nrows(), SType::INT32 |
| 66 | + )); |
| 67 | + case SType::INT32: |
| 68 | + return Column(new FuncUnary1_ColumnImpl<int32_t, int32_t>( |
| 69 | + std::move(col), op_minus<int32_t>, col.nrows(), SType::INT32 |
| 70 | + )); |
| 71 | + case SType::INT64: |
| 72 | + return Column(new FuncUnary1_ColumnImpl<int64_t, int64_t>( |
| 73 | + std::move(col), op_minus<int64_t>, col.nrows(), SType::INT64 |
| 74 | + )); |
| 75 | + case SType::FLOAT32: |
| 76 | + return Column(new FuncUnary1_ColumnImpl<float, float>( |
| 77 | + std::move(col), op_minus<float>, col.nrows(), SType::FLOAT32 |
| 78 | + )); |
| 79 | + case SType::FLOAT64: |
| 80 | + return Column(new FuncUnary1_ColumnImpl<double, double>( |
| 81 | + std::move(col), op_minus<double>, col.nrows(), SType::FLOAT64 |
| 82 | + )); |
| 83 | + default: |
| 84 | + throw TypeError() << "Cannot apply unary `operator -` to a column with " |
| 85 | + "stype `" << stype << "`"; |
| 86 | + } |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | + |
| 91 | +// gets the count of all rows - nulls are not checked |
| 92 | +class FExpr_UnaryMinus_Node : public FExpr_Func { |
| 93 | + private: |
| 94 | + ptrExpr arg_; |
| 95 | + public: |
| 96 | + FExpr_UnaryMinus_Node(ptrExpr &&arg) |
| 97 | + : arg_(std::move(arg)) |
| 98 | + {} |
| 99 | + |
| 100 | + std::string repr() const override { |
| 101 | + std::string out = "uminus"; |
| 102 | + out += '('; |
| 103 | + out += arg_->repr(); |
| 104 | + out += ')'; |
| 105 | + return out; |
| 106 | + } |
| 107 | + |
| 108 | + Workframe evaluate_n(EvalContext &ctx) const override { |
| 109 | + Workframe wf(ctx); |
| 110 | + |
| 111 | + return wf; |
| 112 | + } |
| 113 | + |
| 114 | +}; |
| 115 | + |
| 116 | +py::oobj PyFExpr::nb__neg__(py::robj src) { |
| 117 | + std::cout<<src.is_by_node()<<std::endl; |
| 118 | + std::cout<<src.is_sort_node()<<std::endl; |
| 119 | + if (src.is_by_node()){ |
| 120 | + return PyFExpr::make( |
| 121 | + new FExpr_UnaryMinus_Node(as_fexpr(src))); |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + return PyFExpr::make( |
| 126 | + new FExpr_UnaryMinus(as_fexpr(src))); |
| 127 | +} |
| 128 | + |
| 129 | +}} // dt::expr |
0 commit comments