Skip to content

Commit

Permalink
Add reshape to cosmo_quantity to enable some functions like meshgrid.
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleaoman committed Dec 19, 2024
1 parent 44a6f59 commit 3d87708
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion swiftsimio/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@ def __new__(
units = getattr(input_scalar, "units", None)
else:
units = input_units
ret = cosmo_array.__new__(
ret = super().__new__(
cls,
np.asarray(input_scalar),
units,
Expand All @@ -1432,3 +1432,16 @@ def __new__(
if ret.size > 1:
raise RuntimeError("unyt_quantity instances must be scalars")
return ret

def reshape(self, *shape, order="C"):
# this is necessary to support some numpy operations
# natively, like numpy.meshgrid, which internally performs
# reshaping, e.g., arr.reshape(1, -1), which doesn't affect the size,
# but does change the object's internal representation to a >0D array
# see https://github.com/yt-project/unyt/issues/224
if len(shape) == 1:
shape = shape[0]
if shape == () or shape is None:
return super().reshape(shape, order=order)
else:
return cosmo_array(self).reshape(shape, order=order)

0 comments on commit 3d87708

Please sign in to comment.