Skip to content
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

Implement BlockArray.T #556

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion docs/source/api-reference/array-manipulation-routines.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Transpose-like operations
:toctree: generated/

swapaxes
ndarray.T
BlockArray.T
transpose

Changing number of dimensions
Expand Down
24 changes: 21 additions & 3 deletions nums/core/array/blockarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import warnings
import itertools
Expand Down Expand Up @@ -247,14 +247,32 @@ def transpose(self, defer=False, redistribute=False):
)
return rarrT

@property
def T(self) -> BlockArray:
"""The transposed array.

Examples:
>>> x = nps.array([[1., 2.], [3., 4.]])
>>> x.get()
array([[1., 2.],
[3., 4.]])
>>> x.T.get()
array([[1., 3.],
[2., 4.]])
>>> x = np.array([1., 2., 3., 4.])
>>> x.get()
array([1., 2., 3., 4.])
>>> x.T.get()
array([1., 2., 3., 4.])
"""
return self.transpose()

def __getattr__(self, item):
if item == "__array_priority__" or item == "__array_struct__":
# This is triggered by a numpy array on the LHS.
raise TypeError("Unexpected conversion attempt from BlockArray to ndarray.")
elif item == "ndim":
return len(self.shape)
elif item == "T":
return self.transpose()
else:
raise NotImplementedError(item)

Expand Down