From 18ec3ebbd2a7b2d95934ebc73231f9e830a113ec Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Wed, 23 Oct 2024 14:34:14 +0200 Subject: [PATCH 1/4] Add implementation of dpnp.isfortran() --- dpnp/dpnp_iface_logic.py | 71 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/dpnp/dpnp_iface_logic.py b/dpnp/dpnp_iface_logic.py index 2f8c40206daf..f800a9793645 100644 --- a/dpnp/dpnp_iface_logic.py +++ b/dpnp/dpnp_iface_logic.py @@ -64,6 +64,7 @@ "iscomplex", "iscomplexobj", "isfinite", + "isfortran", "isinf", "isnan", "isneginf", @@ -991,6 +992,76 @@ def iscomplexobj(x): ) +def isfortran(a): + """ + Check if the array is Fortran contiguous but *not* C contiguous. + + This function is obsolete. If you only want to check if an array is Fortran + contiguous use ``a.flags.f_contiguous`` instead. + + For full documentation refer to :obj:`numpy.isfortran`. + + Parameters + ---------- + a : {dpnp.ndarray, usm_ndarray} + Input array. + + Returns + ------- + isfortran : bool + Returns ``True`` if the array is Fortran contiguous + but *not* C contiguous. + + Examples + -------- + dpnp.array allows to specify whether the array is written in C-contiguous + order (last index varies the fastest), or FORTRAN-contiguous order in + memory (first index varies the fastest). + + >>> import dpnp as np + >>> a = np.array([[1, 2, 3], [4, 5, 6]], order='C') + >>> a + array([[1, 2, 3], + [4, 5, 6]]) + >>> np.isfortran(a) + False + + >>> b = np.array([[1, 2, 3], [4, 5, 6]], order='F') + >>> b + array([[1, 2, 3], + [4, 5, 6]]) + >>> np.isfortran(b) + True + + The transpose of a C-ordered array is a FORTRAN-ordered array. + + >>> a = np.array([[1, 2, 3], [4, 5, 6]], order='C') + >>> a + array([[1, 2, 3], + [4, 5, 6]]) + >>> np.isfortran(a) + False + >>> b = a.T + >>> b + array([[1, 4], + [2, 5], + [3, 6]]) + >>> np.isfortran(b) + True + + C-ordered arrays evaluate as ``False`` even if they are also + FORTRAN-ordered. + + >>> np.isfortran(np.array([1, 2], order='F')) + False + + """ + + dpnp.check_supported_arrays_type(a) + + return a.flags.fnc + + _ISINF_DOCSTRING = """ Test if each element of input array is an infinity. From 94dccbc1d23e6d8ce146f97df3733851369d6892 Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Wed, 23 Oct 2024 14:35:23 +0200 Subject: [PATCH 2/4] Enable cupy test for dpnp.isfortran() --- tests/third_party/cupy/logic_tests/test_type_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/third_party/cupy/logic_tests/test_type_test.py b/tests/third_party/cupy/logic_tests/test_type_test.py index 229b3808a65c..c5304e134fd9 100644 --- a/tests/third_party/cupy/logic_tests/test_type_test.py +++ b/tests/third_party/cupy/logic_tests/test_type_test.py @@ -72,7 +72,6 @@ def setUp(self): ) class TestIsFortran(unittest.TestCase): - @pytest.mark.skip("isfortran not implemented") @testing.numpy_cupy_equal() def test(self, xp): return xp.isfortran(xp.asarray(self.value)) From 5f681bc554d7c64cd5156dbcc089563f4d2283cd Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Wed, 23 Oct 2024 15:13:18 +0200 Subject: [PATCH 3/4] Add more tests --- tests/test_logic.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/test_logic.py b/tests/test_logic.py index c52aba89aa78..44fc8baaf2e4 100644 --- a/tests/test_logic.py +++ b/tests/test_logic.py @@ -431,6 +431,34 @@ def test_finite(op, data, dtype): assert_equal(dpnp_res, np_res) +class TestIsFortran: + @pytest.mark.parametrize( + "array, expected", + [ + (dpnp.ones((2, 4), order="C"), True), + (dpnp.ones((2, 4), order="F"), False), + ], + ) + def test_isfortran_transpose(self, array, expected): + assert dpnp.isfortran(array.T) == expected + + @pytest.mark.parametrize( + "array, expected", + [ + (dpnp.ones((2, 4), order="C"), False), + (dpnp.ones((2, 4), order="F"), True), + ], + ) + def test_isfortran_usm_ndarray(self, array, expected): + assert dpnp.isfortran(array.get_array()) == expected + + def test_isfortran_errors(self): + # unsupported type + a_np = numpy.ones((2, 3)) + assert_raises(TypeError, dpnp.isfortran, a_np) + assert_raises(TypeError, dpnp.isfortran, [1, 2, 3]) + + @pytest.mark.parametrize("func", ["isneginf", "isposinf"]) @pytest.mark.parametrize( "data", From 3683693b38d12f37c93189c69971a18eeed85046 Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Thu, 24 Oct 2024 15:43:24 +0200 Subject: [PATCH 4/4] Address a comment --- dpnp/dpnp_iface_logic.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dpnp/dpnp_iface_logic.py b/dpnp/dpnp_iface_logic.py index f800a9793645..4c43b2fb98ac 100644 --- a/dpnp/dpnp_iface_logic.py +++ b/dpnp/dpnp_iface_logic.py @@ -1014,9 +1014,9 @@ def isfortran(a): Examples -------- - dpnp.array allows to specify whether the array is written in C-contiguous - order (last index varies the fastest), or FORTRAN-contiguous order in - memory (first index varies the fastest). + :obj:`dpnp.array` allows to specify whether the array is written in + C-contiguous order (last index varies the fastest), or FORTRAN-contiguous + order in memory (first index varies the fastest). >>> import dpnp as np >>> a = np.array([[1, 2, 3], [4, 5, 6]], order='C')