Skip to content

Commit 1d071be

Browse files
committed
Add required by specification methods
1 parent 0afb92e commit 1d071be

File tree

1 file changed

+76
-19
lines changed

1 file changed

+76
-19
lines changed

arrayfire/array_api/_array_object.py

Lines changed: 76 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import array as py_array
44
import ctypes
5+
import enum
56
from dataclasses import dataclass
67
from typing import Any
78

@@ -125,21 +126,6 @@ def __init__(
125126
ctypes.pointer(_cshape.c_array), ctypes.pointer(strides_cshape), dtype.c_api_value,
126127
pointer_source.value))
127128

128-
def __str__(self) -> str:
129-
# TODO change the look of array str. E.g., like np.array
130-
if not _in_display_dims_limit(self.shape):
131-
return _metadata_string(self.dtype, self.shape)
132-
133-
return _metadata_string(self.dtype) + _array_as_str(self)
134-
135-
def __repr__(self) -> str:
136-
# return _metadata_string(self.dtype, self.shape)
137-
# TODO change the look of array representation. E.g., like np.array
138-
return _array_as_str(self)
139-
140-
def __len__(self) -> int:
141-
return self.shape[0] if self.shape else 0
142-
143129
# Arithmetic Operators
144130

145131
def __pos__(self) -> Array:
@@ -441,6 +427,36 @@ def __irshift__(self, other: int | Array, /) -> Array:
441427
"""
442428
return _process_c_function(self, other, backend.get().af_bitshiftr)
443429

430+
# Methods
431+
432+
def __abs__(self) -> Array:
433+
# TODO
434+
return NotImplemented
435+
436+
def __array_namespace__(self, *, api_version: None | str = None) -> Any:
437+
# TODO
438+
return NotImplemented
439+
440+
def __bool__(self) -> bool:
441+
# TODO
442+
return NotImplemented
443+
444+
def __complex__(self) -> complex:
445+
# TODO
446+
return NotImplemented
447+
448+
def __dlpack__(self, *, stream: None | int | Any = None): # type: ignore[no-untyped-def]
449+
# TODO implementation and expected return type -> PyCapsule
450+
return NotImplemented
451+
452+
def __dlpack_device__(self) -> tuple[enum.Enum, int]:
453+
# TODO
454+
return NotImplemented
455+
456+
def __float__(self) -> float:
457+
# TODO
458+
return NotImplemented
459+
444460
def __getitem__(self, key: int | slice | tuple[int | slice] | Array, /) -> Array:
445461
# TODO: API Specification - key: int | slice | ellipsis | tuple[int | slice] | Array
446462
# TODO: refactor
@@ -456,6 +472,40 @@ def __getitem__(self, key: int | slice | tuple[int | slice] | Array, /) -> Array
456472
ctypes.pointer(out.arr), self.arr, c_dim_t(ndims), _get_indices(key).pointer))
457473
return out
458474

475+
def __index__(self) -> int:
476+
# TODO
477+
return NotImplemented
478+
479+
def __int__(self) -> int:
480+
# TODO
481+
return NotImplemented
482+
483+
def __len__(self) -> int:
484+
return self.shape[0] if self.shape else 0
485+
486+
def __setitem__(
487+
self, key: int | slice | tuple[int | slice, ...] | Array, value: int | float | bool | Array, /) -> None:
488+
# TODO
489+
return NotImplemented # type: ignore[return-value] # FIXME
490+
491+
def __str__(self) -> str:
492+
# TODO change the look of array str. E.g., like np.array
493+
if not _in_display_dims_limit(self.shape):
494+
return _metadata_string(self.dtype, self.shape)
495+
496+
return _metadata_string(self.dtype) + _array_as_str(self)
497+
498+
def __repr__(self) -> str:
499+
# return _metadata_string(self.dtype, self.shape)
500+
# TODO change the look of array representation. E.g., like np.array
501+
return _array_as_str(self)
502+
503+
def to_device(self, device: Any, /, *, stream: None | int | Any = None) -> Array:
504+
# TODO implementation and change device type from Any to Device
505+
return NotImplemented
506+
507+
# Attributes
508+
459509
@property
460510
def dtype(self) -> Dtype:
461511
out = ctypes.c_int()
@@ -464,17 +514,23 @@ def dtype(self) -> Dtype:
464514

465515
@property
466516
def device(self) -> Any:
467-
raise NotImplementedError
517+
# TODO
518+
return NotImplemented
468519

469520
@property
470521
def mT(self) -> Array:
471522
# TODO
472-
raise NotImplementedError
523+
return NotImplemented
473524

474525
@property
475526
def T(self) -> Array:
476-
# TODO
477-
raise NotImplementedError
527+
"""
528+
Transpose of the array.
529+
"""
530+
out = Array()
531+
# NOTE conj support is removed because it is never used
532+
safe_call(backend.get().af_transpose(ctypes.pointer(out.arr), self.arr, False))
533+
return out
478534

479535
@property
480536
def size(self) -> int:
@@ -507,6 +563,7 @@ def scalar(self) -> None | int | float | bool | complex:
507563
"""
508564
Return the first element of the array
509565
"""
566+
# TODO change the logic of this method
510567
if self.is_empty():
511568
return None
512569

0 commit comments

Comments
 (0)