Skip to content
Closed
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
1 change: 1 addition & 0 deletions cpp/src/arrow/compute/api_scalar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ SCALAR_ARITHMETIC_BINARY(Add, "add", "add_checked")
SCALAR_ARITHMETIC_BINARY(Subtract, "subtract", "subtract_checked")
SCALAR_ARITHMETIC_BINARY(Multiply, "multiply", "multiply_checked")
SCALAR_ARITHMETIC_BINARY(Divide, "divide", "divide_checked")
SCALAR_ARITHMETIC_BINARY(Power, "power", "power_checked")

// ----------------------------------------------------------------------
// Set-related operations
Expand Down
14 changes: 14 additions & 0 deletions cpp/src/arrow/compute/api_scalar.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,20 @@ Result<Datum> Divide(const Datum& left, const Datum& right,
ArithmeticOptions options = ArithmeticOptions(),
ExecContext* ctx = NULLPTR);

/// \brief Raise the values of base array to the power of the exponent array values.
/// Array values must be the same length. If either base or exponent is null the result
/// will be null.
///
/// \param[in] left the base
/// \param[in] right the exponent
/// \param[in] options arithmetic options (enable/disable overflow checking), optional
/// \param[in] ctx the function execution context, optional
/// \return the elementwise base value raised to the power of exponent
ARROW_EXPORT
Result<Datum> Power(const Datum& left, const Datum& right,
ArithmeticOptions options = ArithmeticOptions(),
ExecContext* ctx = NULLPTR);

/// \brief Compare a numeric array with a scalar.
///
/// \param[in] left datum to compare, must be an Array
Expand Down
87 changes: 87 additions & 0 deletions cpp/src/arrow/compute/kernels/scalar_arithmetic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// specific language governing permissions and limitations
// under the License.

#include <cmath>

#include "arrow/compute/kernels/common.h"
#include "arrow/util/int_util_internal.h"
#include "arrow/util/macros.h"
Expand Down Expand Up @@ -233,6 +235,70 @@ struct DivideChecked {
}
};

struct Power {
ARROW_NOINLINE
static uint64_t IntegerPower(uint64_t base, uint64_t exp) {
// right to left O(logn) power
uint64_t pow = 1;
while (exp) {
pow *= (exp & 1) ? base : 1;
base *= base;
exp >>= 1;
}
return pow;
}

template <typename T>
static enable_if_integer<T> Call(KernelContext* ctx, T base, T exp) {
if (exp < 0) {
ctx->SetStatus(
Status::Invalid("integers to negative integer powers are not allowed"));
return 0;
}
return static_cast<T>(IntegerPower(base, exp));
}

template <typename T>
static enable_if_floating_point<T> Call(KernelContext* ctx, T base, T exp) {
return std::pow(base, exp);
}
};

struct PowerChecked {
template <typename T, typename Arg0, typename Arg1>
static enable_if_integer<T> Call(KernelContext* ctx, Arg0 base, Arg1 exp) {
if (exp < 0) {
ctx->SetStatus(
Status::Invalid("integers to negative integer powers are not allowed"));
return 0;
} else if (exp == 0) {
return 1;
}
// left to right O(logn) power with overflow checks
bool overflow = false;
uint64_t bitmask =
1ULL << (63 - BitUtil::CountLeadingZeros(static_cast<uint64_t>(exp)));
T pow = 1;
while (bitmask) {
overflow |= MultiplyWithOverflow(pow, pow, &pow);
if (exp & bitmask) {
overflow |= MultiplyWithOverflow(pow, base, &pow);
}
bitmask >>= 1;
}
if (overflow) {
ctx->SetStatus(Status::Invalid("overflow"));
}
return pow;
}

template <typename T, typename Arg0, typename Arg1>
static enable_if_floating_point<T> Call(KernelContext* ctx, Arg0 base, Arg1 exp) {
static_assert(std::is_same<T, Arg0>::value && std::is_same<T, Arg1>::value, "");
return std::pow(base, exp);
}
};

// Generate a kernel given an arithmetic functor
template <template <typename... Args> class KernelGenerator, typename Op>
ArrayKernelExec NumericEqualTypesBinary(detail::GetTypeId get_id) {
Expand Down Expand Up @@ -359,6 +425,18 @@ const FunctionDoc div_checked_doc{
"integer overflow is encountered."),
{"dividend", "divisor"}};

const FunctionDoc pow_doc{
"Raise arguments to power element-wise",
("Integer to negative integer power returns an error. However, integer overflow\n"
"wraps around. If either base or exponent is null the result will be null."),
{"base", "exponent"}};

const FunctionDoc pow_checked_doc{
"Raise arguments to power element-wise",
("An error is returned when integer to negative integer power is encountered,\n"
"or integer overflow is encountered."),
{"base", "exponent"}};

} // namespace

void RegisterScalarArithmetic(FunctionRegistry* registry) {
Expand Down Expand Up @@ -407,6 +485,15 @@ void RegisterScalarArithmetic(FunctionRegistry* registry) {
auto divide_checked =
MakeArithmeticFunctionNotNull<DivideChecked>("divide_checked", &div_checked_doc);
DCHECK_OK(registry->AddFunction(std::move(divide_checked)));

// ----------------------------------------------------------------------
auto power = MakeArithmeticFunction<Power>("power", &pow_doc);
DCHECK_OK(registry->AddFunction(std::move(power)));

// ----------------------------------------------------------------------
auto power_checked =
MakeArithmeticFunctionNotNull<PowerChecked>("power_checked", &pow_checked_doc);
DCHECK_OK(registry->AddFunction(std::move(power_checked)));
}

} // namespace internal
Expand Down
110 changes: 109 additions & 1 deletion cpp/src/arrow/compute/kernels/scalar_arithmetic_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,114 @@ TYPED_TEST(TestBinaryArithmeticSigned, DivideOverflowRaises) {
this->AssertBinop(Divide, MakeArray(min), MakeArray(-1), "[0]");
}

TYPED_TEST(TestBinaryArithmeticFloating, Power) {
using CType = typename TestFixture::CType;
auto max = std::numeric_limits<CType>::max();
this->SetNansEqual(true);

for (auto check_overflow : {false, true}) {
this->SetOverflowCheck(check_overflow);

// Empty arrays
this->AssertBinop(Power, "[]", "[]", "[]");
// Ordinary arrays
this->AssertBinop(Power, "[3.4, 16, 0.64, 1.2, 0]", "[1, 0.5, 2, 4, 0]",
"[3.4, 4, 0.4096, 2.0736, 1]");
// Array with nulls
this->AssertBinop(Power, "[null, 1, 3.3, null, 2]", "[1, 4, 2, 5, 0.1]",
"[null, 1, 10.89, null, 1.07177346]");
// Scalar exponentiated by array
this->AssertBinop(Power, 10.0F, "[null, 1, 2.5, null, 2, 5]",
"[null, 10, 316.227766017, null, 100, 100000]");
// Array exponentiated by scalar
this->AssertBinop(Power, "[null, 1, 2.5, null, 2, 5]", 10.0F,
"[null, 1, 9536.74316406, null, 1024, 9765625]");
// Array with infinity
this->AssertBinop(Power, "[3.4, Inf, -Inf, 1.1, 100000]", "[1, 2, 3, Inf, 100000]",
"[3.4, Inf, -Inf, Inf, Inf]");
// Array with NaN
this->AssertBinop(Power, "[3.4, NaN, 2.0]", "[1, 2, 2.0]", "[3.4, NaN, 4.0]");
// Scalar exponentiated by scalar
this->AssertBinop(Power, 21.0F, 3.0F, 9261.0F);
// Divide by zero
this->AssertBinop(Power, "[0.0, 0.0]", "[-1.0, -3.0]", "[Inf, Inf]");
// Check overflow behaviour
this->AssertBinop(Power, max, 10, INFINITY);
}

// Edge cases - removing NaNs
this->AssertBinop(Power, "[1, NaN, 0, null, 1.2, -Inf, Inf, 1.1, 1, 0, 1, 0]",
"[NaN, 0, NaN, 1, null, 1, 2, -Inf, Inf, 0, 0, 42]",
"[1, 1, NaN, null, null, -Inf, Inf, 0, 1, 1, 1, 0]");
}

TYPED_TEST(TestBinaryArithmeticIntegral, Power) {
using CType = typename TestFixture::CType;
auto max = std::numeric_limits<CType>::max();

for (auto check_overflow : {false, true}) {
this->SetOverflowCheck(check_overflow);

// Empty arrays
this->AssertBinop(Power, "[]", "[]", "[]");
// Ordinary arrays
this->AssertBinop(Power, "[3, 2, 6, 2]", "[1, 1, 2, 0]", "[3, 2, 36, 1]");
// Array with nulls
this->AssertBinop(Power, "[null, 2, 3, null, 20]", "[1, 6, 2, 5, 1]",
"[null, 64, 9, null, 20]");
// Scalar exponentiated by array
this->AssertBinop(Power, 3, "[null, 3, 4, null, 2]", "[null, 27, 81, null, 9]");
// Array exponentiated by scalar
this->AssertBinop(Power, "[null, 10, 3, null, 2]", 2, "[null, 100, 9, null, 4]");
// Scalar exponentiated by scalar
this->AssertBinop(Power, 4, 3, 64);
// Edge cases
this->AssertBinop(Power, "[0, 1, 0]", "[0, 0, 42]", "[1, 1, 0]");
}

// Overflow raises
this->SetOverflowCheck(true);
this->AssertBinopRaises(Power, MakeArray(max), MakeArray(10), "overflow");
// Disable overflow check
this->SetOverflowCheck(false);
this->AssertBinop(Power, max, 10, 1);
}

TYPED_TEST(TestBinaryArithmeticSigned, Power) {
using CType = typename TestFixture::CType;
auto max = std::numeric_limits<CType>::max();

for (auto check_overflow : {false, true}) {
this->SetOverflowCheck(check_overflow);

// Empty arrays
this->AssertBinop(Power, "[]", "[]", "[]");
// Ordinary arrays
this->AssertBinop(Power, "[-3, 2, -6, 2]", "[3, 1, 2, 0]", "[-27, 2, 36, 1]");
// Array with nulls
this->AssertBinop(Power, "[null, 10, 127, null, -20]", "[1, 2, 1, 5, 1]",
"[null, 100, 127, null, -20]");
// Scalar exponentiated by array
this->AssertBinop(Power, 11, "[null, 1, null, 2]", "[null, 11, null, 121]");
// Array exponentiated by scalar
this->AssertBinop(Power, "[null, 1, 3, null, 2]", 3, "[null, 1, 27, null, 8]");
// Scalar exponentiated by scalar
this->AssertBinop(Power, 16, 1, 16);
// Edge cases
this->AssertBinop(Power, "[1, 0, -1, 2]", "[0, 42, 0, 1]", "[1, 0, 1, 2]");
// Divide by zero raises
this->AssertBinopRaises(Power, MakeArray(0), MakeArray(-1),
"integers to negative integer powers are not allowed");
}

// Overflow raises
this->SetOverflowCheck(true);
this->AssertBinopRaises(Power, MakeArray(max), MakeArray(10), "overflow");
// Disable overflow check
this->SetOverflowCheck(false);
this->AssertBinop(Power, max, 10, 1);
}

TYPED_TEST(TestBinaryArithmeticFloating, Sub) {
this->AssertBinop(Subtract, "[]", "[]", "[]");

Expand Down Expand Up @@ -638,7 +746,7 @@ TYPED_TEST(TestBinaryArithmeticFloating, Mul) {
}

TEST(TestBinaryArithmetic, DispatchBest) {
for (std::string name : {"add", "subtract", "multiply", "divide"}) {
for (std::string name : {"add", "subtract", "multiply", "divide", "power"}) {
for (std::string suffix : {"", "_checked"}) {
name += suffix;

Expand Down
4 changes: 4 additions & 0 deletions docs/source/cpp/compute.rst
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ an ``Invalid`` :class:`Status` when overflow is detected.
+--------------------------+------------+--------------------+---------------------+
| divide_checked | Binary | Numeric | Numeric |
+--------------------------+------------+--------------------+---------------------+
| power | Binary | Numeric | Numeric |
+--------------------------+------------+--------------------+---------------------+
| power_checked | Binary | Numeric | Numeric |
+--------------------------+------------+--------------------+---------------------+
| multiply | Binary | Numeric | Numeric |
+--------------------------+------------+--------------------+---------------------+
| multiply_checked | Binary | Numeric | Numeric |
Expand Down
2 changes: 2 additions & 0 deletions docs/source/python/api/compute.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ throws an ``ArrowInvalid`` exception when overflow is detected.
multiply_checked
subtract
subtract_checked
power
power_checked

Comparisons
-----------
Expand Down