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:
- why does
multiarray.copyto(np_array, quantity) triggers the __array_interface__ but not np.full_like(np_array, quantity) ?
- should
physipy's implementation of np.copyto enforce dimension equality ?
Similar issue with np.full :
Recall the signature of
numpy.full_like:On the other hand,
physipyuses the__array_function__interface to overload numpy calls on quantity-arrays. This works well for the following cases:But in the case where only the
fill_valueis a quantity, the__array__function__interface is not triggered, so the following relies on numpy'sfull_like, which is basically:since
ais a regular array,empty_likealso returns a regular array. But thencopytotriggers the__array_interface__and delegates tophysipy's implementation, which checks for dimension equality.Overall :
raises a
DimensionError.Which raises 2 questions:
multiarray.copyto(np_array, quantity)triggers the__array_interface__but notnp.full_like(np_array, quantity)?physipy's implementation ofnp.copytoenforce dimension equality ?Similar issue with
np.full:np.fullwithlikeargument does not trigger__array_function__interface numpy/numpy#21033