Skip to content

Add implementation of dpnp.isfortran() #2122

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

Merged
merged 5 commits into from
Oct 24, 2024
Merged
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
71 changes: 71 additions & 0 deletions dpnp/dpnp_iface_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"iscomplex",
"iscomplexobj",
"isfinite",
"isfortran",
"isinf",
"isnan",
"isneginf",
Expand Down Expand Up @@ -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
--------
: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')
>>> 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.

Expand Down
28 changes: 28 additions & 0 deletions tests/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion tests/third_party/cupy/logic_tests/test_type_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Loading