Skip to content

add several ndarray method/properties #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 4, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion torch_np/_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from . import _binary_ufuncs, _dtypes, _funcs, _helpers, _unary_ufuncs
from ._detail import _dtypes_impl, _util
from ._normalizations import ArrayLike, normalizer

newaxis = None

Expand Down Expand Up @@ -117,6 +118,14 @@ def flags(self):
}
)

@property
def data(self):
return self.tensor.data_ptr()

@property
def nbytes(self):
return self.tensor.storage().nbytes()

@property
def T(self):
return self.transpose()
Expand Down Expand Up @@ -157,7 +166,8 @@ def view(self, dtype):
tview = self.tensor.view(torch_dtype)
return ndarray(tview)

def fill(self, value):
@normalizer
def fill(self, value: ArrayLike):
self.tensor.fill_(value)

def tolist(self):
Expand Down Expand Up @@ -395,6 +405,15 @@ def sort(self, axis=-1, kind=None, order=None):
cumprod = _funcs.cumprod

### indexing ###
def item(self, *args):
if args == ():
return self.tensor.item()
elif len(args) == 1:
# int argument
return self.ravel()[args[0]]
else:
return self.__getitem__(args)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have tests for this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not at the moment. Don't see numpy tests in numpy proper either. Behavior with arrays seems legacy TBH, so I'd prefer to put it to the priority queue.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you pulled this from the NumPy implementation, let's just add a link to it and roll with it.

@staticmethod
def _upcast_int_indices(index):
if isinstance(index, torch.Tensor):
Expand Down