Skip to content

Summing over array dimensions does not work properly #92

@masonproffitt

Description

@masonproffitt

Summing doesn't seem supported at all with numpy arrays:

>>> np.sum(vector.array({"x": [1], "y": [1]}))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<__array_function__ internals>", line 5, in sum
TypeError: no implementation found for 'numpy.sum' on types that implement __array_function__: [<class 'vector._backends.numpy_.VectorNumpy2D'>]
>>> vector.array({"x": [1], "y": [1]}).sum()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/miniconda3/envs/iris-hep/lib/python3.9/site-packages/numpy/core/_methods.py", line 47, in _sum
    return umr_sum(a, axis, dtype, out, keepdims, initial, where)
TypeError: operand type(s) all returned NotImplemented from __array_ufunc__(<ufunc 'add'>, 'reduce', VectorNumpy2D([(1., 1.)], dtype=[('x', '<f8'), ('y', '<f8')]), axis=None, dtype=None, keepdims=False, where=True): 'VectorNumpy2D'

With ak.Array, you can get results with ak.sum(), but they don't make sense for vectors:

>>> ak.sum(vector.Array({"x": [1], "y": [1]}))
2

Using the axis parameter works fine for Cartesian coordinates:

>>> ak.sum(vector.Array({"x": [1, 2], "y": [3, 4]}), axis=0)
<Record {x: 3, y: 7} type='{"x": int64, "y": int64}'>

but not for any other cases:

>>> ak.sum(vector.Array({"pt": [1, 1], "phi": [0, np.pi]}), axis=0)
<Record {rho: 2, phi: 3.14} type='{"rho": int64, "phi": float64}'>

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions