Repository navigation
Description
Bug: Unary Negation (-a) on ak.uint64 Does Not Match NumPy Semantics
Summary
Arkouda’s implementation of unary negation (-a) for unsigned 64-bit arrays (ak.uint64) does not match NumPy behavior.
NumPy defines unary negation for unsigned integers as a modular wrap-around operation in the same unsigned dtype:
np.uint64(x) → (-x) % 2**64
Arkouda instead returns a signed int64 result with negative values, breaking NumPy alignment tests and producing incorrect, dtype-inconsistent output.
This causes 100% mismatches in:
test_unary_ops_alignment[neg-neg-uint64]
Expected NumPy Behavior
In NumPy, unary negation on uint64:
- Preserves dtype (
uint64) - Computes result modulo 2⁶⁴:
>>> import numpy as np
>>> x = np.uint64(5)
>>> -x
18446744073709551611 # 2**64 - 5
>>> np.array([-x], dtype=np.uint64)
array([18446744073709551611], dtype=uint64)Arkouda’s Current Behavior
Arkouda produces signed negative values instead of uint64 wraparound:
>>> import arkouda as ak
>>> a = ak.array([5], dtype=ak.uint64)
>>> (-a).to_ndarray()
array([-5]) # WRONG — treated as signed int64Across random arrays, every element differs from NumPy:
[left]: array([-1830621171, -1957544328, -1935796560, ...])
[right]: [18446744071878930445, 18446744071752007288, ...]
These pairs differ by exactly 2**64, proving Arkouda is performing signed negation, not uint64 modulo negation.
This leads to:
- 100% mismatch rate in alignment tests
- Incorrect dtype (signed instead of unsigned)
- Incorrect numerical results
Why This Matters
1. NumPy Compatibility
Arkouda aims to match NumPy semantics for distributed arrays.
Unsigned negation is a well-defined operation in NumPy and widely relied upon.
2. Dtype Preservation
Users expect unary operations to preserve dtype unless NumPy explicitly promotes.
Arkouda violates this expectation.
3. Downstream Bitwise Logic
Unsigned wraparound negation is used in hashing, bitboard logic, cryptographic ops, bloom filters, etc. Negation returning signed incorrect values breaks these algorithms.
Root Cause
Arkouda appears to interpret uint64 arrays as signed when applying unary minus:
- Likely calling a signed
-xoperator on the underlying Chapel array. - No modulo operation or dtype preservation is performed.
- Result pdarray inherits the backend’s signed numerical interpretation.
NumPy, in contrast, uses fixed-width modular arithmetic for all unsigned integer operations.
Proposed Fix
Correct Implementation for Unary Minus on ak.uint64:
(-x) → (2**64 - x) % 2**64
Backend logic should:
-
Detect dtype ==
uint64 -
Compute modular negation:
const mask:uint(64) = 0xFFFFFFFFFFFFFFFF; result = (~x + 1) & mask; // equivalent to (-x) mod 2**64
-
Return a
uint64pdarray, preserving dtype.
Alternative (If Full Support Is Undesired)
If Arkouda chooses not to support unary negation for unsigned integers (less ideal for NumPy parity), then:
- Raise a
TypeError("Unary minus not supported for uint64") - Mirror NumPy error semantics for unsupported operations (if any)
But given NumPy does define the operation, best practice is to implement it correctly.
Acceptance Criteria
-
Unary negation on
ak.uint64matches NumPy exactly:assert_arkouda_array_equivalent(-a_ak, -a_np)
-
Result dtype remains
uint64 -
Modular negation works for all values, including large ones
-
Alignment test:
test_unary_ops_alignment[neg-neg-uint64]passes without modification.
Minimal Reproduction
>>> import numpy as np, arkouda as ak
>>> a_np = np.random.randint(0, 2**64, 5, dtype=np.uint64)
>>> a_ak = ak.array(a_np)
>>> np_res = -a_np
>>> ak_res = (-a_ak).to_ndarray()
>>> print(np_res)
>>> print(ak_res)Expected: arrays match exactly
Actual: Arkouda produces negative signed values
Conclusion
Unary negation on uint64 is currently misimplemented.
Arkouda must use unsigned modular negation rather than signed negation to achieve full NumPy compatibility and correct numeric behavior.
Activity
Closes Bears-R-Us#5117: Unary Negation on ak.uint64 Does Not Match Nu…
Closes Bears-R-Us#5117: Unary Negation on ak.uint64 Does Not Match Nu…
Closes Bears-R-Us#5117: Unary Negation on ak.uint64 Does Not Match Nu…
Closes Bears-R-Us#5117: Unary Negation on ak.uint64 Does Not Match Nu…
Closes Bears-R-Us#5117: Unary Negation on ak.uint64 Does Not Match Nu…
Closes Bears-R-Us#5117: Unary Negation on ak.uint64 Does Not Match Nu…
Closes Bears-R-Us#5117: Unary Negation on ak.uint64 Does Not Match Nu…
Closes Bears-R-Us#5117: Unary Negation on ak.uint64 Does Not Match Nu…
Closes Bears-R-Us#5117: Unary Negation on ak.uint64 Does Not Match Nu…
Closes Bears-R-Us#5117: Unary Negation on ak.uint64 Does Not Match Nu…
Closes Bears-R-Us#5117: Unary Negation on ak.uint64 Does Not Match Nu…
Closes Bears-R-Us#5117: Unary Negation on ak.uint64 Does Not Match Nu…
Closes Bears-R-Us#5117: Unary Negation on ak.uint64 Does Not Match Nu…