Skip to content

Commit ff049f9

Browse files
Add implementation of dpnp.isfortran() (#2122)
* Add implementation of dpnp.isfortran() * Enable cupy test for dpnp.isfortran() * Add more tests * Address a comment --------- Co-authored-by: Anton <100830759+antonwolfy@users.noreply.github.com>
1 parent 0fbf329 commit ff049f9

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

dpnp/dpnp_iface_logic.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"iscomplex",
6565
"iscomplexobj",
6666
"isfinite",
67+
"isfortran",
6768
"isinf",
6869
"isnan",
6970
"isneginf",
@@ -991,6 +992,76 @@ def iscomplexobj(x):
991992
)
992993

993994

995+
def isfortran(a):
996+
"""
997+
Check if the array is Fortran contiguous but *not* C contiguous.
998+
999+
This function is obsolete. If you only want to check if an array is Fortran
1000+
contiguous use ``a.flags.f_contiguous`` instead.
1001+
1002+
For full documentation refer to :obj:`numpy.isfortran`.
1003+
1004+
Parameters
1005+
----------
1006+
a : {dpnp.ndarray, usm_ndarray}
1007+
Input array.
1008+
1009+
Returns
1010+
-------
1011+
isfortran : bool
1012+
Returns ``True`` if the array is Fortran contiguous
1013+
but *not* C contiguous.
1014+
1015+
Examples
1016+
--------
1017+
:obj:`dpnp.array` allows to specify whether the array is written in
1018+
C-contiguous order (last index varies the fastest), or FORTRAN-contiguous
1019+
order in memory (first index varies the fastest).
1020+
1021+
>>> import dpnp as np
1022+
>>> a = np.array([[1, 2, 3], [4, 5, 6]], order='C')
1023+
>>> a
1024+
array([[1, 2, 3],
1025+
[4, 5, 6]])
1026+
>>> np.isfortran(a)
1027+
False
1028+
1029+
>>> b = np.array([[1, 2, 3], [4, 5, 6]], order='F')
1030+
>>> b
1031+
array([[1, 2, 3],
1032+
[4, 5, 6]])
1033+
>>> np.isfortran(b)
1034+
True
1035+
1036+
The transpose of a C-ordered array is a FORTRAN-ordered array.
1037+
1038+
>>> a = np.array([[1, 2, 3], [4, 5, 6]], order='C')
1039+
>>> a
1040+
array([[1, 2, 3],
1041+
[4, 5, 6]])
1042+
>>> np.isfortran(a)
1043+
False
1044+
>>> b = a.T
1045+
>>> b
1046+
array([[1, 4],
1047+
[2, 5],
1048+
[3, 6]])
1049+
>>> np.isfortran(b)
1050+
True
1051+
1052+
C-ordered arrays evaluate as ``False`` even if they are also
1053+
FORTRAN-ordered.
1054+
1055+
>>> np.isfortran(np.array([1, 2], order='F'))
1056+
False
1057+
1058+
"""
1059+
1060+
dpnp.check_supported_arrays_type(a)
1061+
1062+
return a.flags.fnc
1063+
1064+
9941065
_ISINF_DOCSTRING = """
9951066
Test if each element of input array is an infinity.
9961067

tests/test_logic.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,34 @@ def test_finite(op, data, dtype):
431431
assert_equal(dpnp_res, np_res)
432432

433433

434+
class TestIsFortran:
435+
@pytest.mark.parametrize(
436+
"array, expected",
437+
[
438+
(dpnp.ones((2, 4), order="C"), True),
439+
(dpnp.ones((2, 4), order="F"), False),
440+
],
441+
)
442+
def test_isfortran_transpose(self, array, expected):
443+
assert dpnp.isfortran(array.T) == expected
444+
445+
@pytest.mark.parametrize(
446+
"array, expected",
447+
[
448+
(dpnp.ones((2, 4), order="C"), False),
449+
(dpnp.ones((2, 4), order="F"), True),
450+
],
451+
)
452+
def test_isfortran_usm_ndarray(self, array, expected):
453+
assert dpnp.isfortran(array.get_array()) == expected
454+
455+
def test_isfortran_errors(self):
456+
# unsupported type
457+
a_np = numpy.ones((2, 3))
458+
assert_raises(TypeError, dpnp.isfortran, a_np)
459+
assert_raises(TypeError, dpnp.isfortran, [1, 2, 3])
460+
461+
434462
@pytest.mark.parametrize("func", ["isneginf", "isposinf"])
435463
@pytest.mark.parametrize(
436464
"data",

tests/third_party/cupy/logic_tests/test_type_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ def setUp(self):
7272
)
7373
class TestIsFortran(unittest.TestCase):
7474

75-
@pytest.mark.skip("isfortran not implemented")
7675
@testing.numpy_cupy_equal()
7776
def test(self, xp):
7877
return xp.isfortran(xp.asarray(self.value))

0 commit comments

Comments
 (0)