Skip to content

numpy.full_like not triggered with (array,quantity)  #26

@mocquin

Description

@mocquin

Recall the signature of numpy.full_like:

numpy.full_like(a, fill_value,...)

On the other hand, physipy uses the __array_function__ interface to overload numpy calls on quantity-arrays. This works well for the following cases:

import numpy
from physipy import m, rad
assert (np.full_like(np.arange(10)*m, 3)     == np.full(10, 3)).all()
assert (np.full_like(np.arange(10)*m, 3*m)   == np.full(10, 3)*m).all()
assert (np.full_like(np.arange(10)*m, 3*rad) == np.full(10, 3)*rad).all()

But in the case where only the fill_value is a quantity, the __array__function__ interface is not triggered, so the following relies on numpy's full_like, which is basically:

    res = empty_like(
        a, dtype=dtype, order=order, subok=subok, shape=shape, device=device
    )
    multiarray.copyto(res, fill_value, casting='unsafe')
    return res

since a is a regular array, empty_like also returns a regular array. But then copyto triggers the __array_interface__ and delegates to physipy's implementation, which checks for dimension equality.

Overall :

>>> np.full_like(np.arange(10), 3*m)
...
DimensionError: Dimension error : dimensions of operands are no-dimension and L, and are differents (dimensionless vs length).

raises a DimensionError.

Which raises 2 questions:

  1. why does multiarray.copyto(np_array, quantity) triggers the __array_interface__ but not np.full_like(np_array, quantity) ?
  2. should physipy's implementation of np.copyto enforce dimension equality ?

Similar issue with np.full :

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions