From 9fa4ff8567f591d6f8e5941d5956ddf243d0df44 Mon Sep 17 00:00:00 2001 From: Balaji Veeramani Date: Wed, 13 Apr 2022 22:20:08 -0700 Subject: [PATCH 1/2] Implement `BlockArray.T` --- .../array-manipulation-routines.rst | 2 +- nums/core/array/blockarray.py | 24 ++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/docs/source/api-reference/array-manipulation-routines.rst b/docs/source/api-reference/array-manipulation-routines.rst index 25c0a856..d71fcc38 100644 --- a/docs/source/api-reference/array-manipulation-routines.rst +++ b/docs/source/api-reference/array-manipulation-routines.rst @@ -24,7 +24,7 @@ Transpose-like operations :toctree: generated/ swapaxes - ndarray.T + BlockArray.T transpose Changing number of dimensions diff --git a/nums/core/array/blockarray.py b/nums/core/array/blockarray.py index 40277e99..c69dee4e 100644 --- a/nums/core/array/blockarray.py +++ b/nums/core/array/blockarray.py @@ -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 @@ -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) From 2ac2f2d87efe0ba9b2e858fea84b7caa65a281d7 Mon Sep 17 00:00:00 2001 From: Balaji Veeramani Date: Wed, 13 Apr 2022 22:23:28 -0700 Subject: [PATCH 2/2] Fix example --- nums/core/array/blockarray.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nums/core/array/blockarray.py b/nums/core/array/blockarray.py index c69dee4e..6b80097d 100644 --- a/nums/core/array/blockarray.py +++ b/nums/core/array/blockarray.py @@ -259,11 +259,11 @@ def T(self) -> BlockArray: >>> x.T.get() array([[1., 3.], [2., 4.]]) - >>> x = np.array([[1., 2., 3., 4.]]) + >>> x = np.array([1., 2., 3., 4.]) >>> x.get() array([1., 2., 3., 4.]) >>> x.T.get() - array([ 1., 2., 3., 4.]) + array([1., 2., 3., 4.]) """ return self.transpose()