Skip to content

Add order agrument to generate_random_numpy_array() #2237

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 4 commits into from
Dec 17, 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
17 changes: 15 additions & 2 deletions dpnp/tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,13 @@ def get_all_dtypes(


def generate_random_numpy_array(
shape, dtype=None, hermitian=False, seed_value=None, low=-10, high=10
shape,
dtype=None,
order="C",
hermitian=False,
seed_value=None,
low=-10,
high=10,
):
"""
Generate a random numpy array with the specified shape and dtype.
Expand All @@ -178,6 +184,9 @@ def generate_random_numpy_array(
Desired data-type for the output array.
If not specified, data type will be determined by numpy.
Default : ``None``
order : {"C", "F"}, optional
Specify the memory layout of the output array.
Default: ``"C"``.
hermitian : bool, optional
If True, generates a Hermitian (symmetric if `dtype` is real) matrix.
Default : ``False``
Expand All @@ -194,7 +203,7 @@ def generate_random_numpy_array(
Returns
-------
out : numpy.ndarray
A random numpy array of the specified shape and dtype.
A random numpy array of the specified shape, dtype and memory layout.
The array is Hermitian or symmetric if `hermitian` is True.

Note:
Expand Down Expand Up @@ -224,6 +233,10 @@ def generate_random_numpy_array(
a = a.reshape(orig_shape)
else:
a = numpy.conj(a.T) @ a

# a.reshape(shape) returns an array in C order by default
if order != "C" and a.ndim > 1:
a = numpy.array(a, order=order)
return a


Expand Down
13 changes: 5 additions & 8 deletions dpnp/tests/test_fft.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def setup_method(self):
@pytest.mark.parametrize("norm", [None, "forward", "backward", "ortho"])
@pytest.mark.parametrize("order", ["C", "F"])
def test_fft2(self, dtype, axes, norm, order):
a_np = generate_random_numpy_array((2, 3, 4), dtype)
a_np = generate_random_numpy_array((2, 3, 4), dtype, order)
a = dpnp.array(a_np)

result = dpnp.fft.fft2(a, axes=axes, norm=norm)
Expand Down Expand Up @@ -442,7 +442,7 @@ def setup_method(self):
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
@pytest.mark.parametrize("order", ["C", "F"])
def test_fftn(self, dtype, axes, norm, order):
a_np = generate_random_numpy_array((2, 3, 4, 5), dtype)
a_np = generate_random_numpy_array((2, 3, 4, 5), dtype, order)
a = dpnp.array(a_np)

result = dpnp.fft.fftn(a, axes=axes, norm=norm)
Expand Down Expand Up @@ -696,8 +696,7 @@ def test_irfft_1D_on_2D_array(self, dtype, n, axis, norm, order):
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
@pytest.mark.parametrize("order", ["C", "F"])
def test_irfft_1D_on_3D_array(self, dtype, n, axis, norm, order):
x = generate_random_numpy_array((4, 5, 6), dtype)
a_np = numpy.array(x, order=order)
a_np = generate_random_numpy_array((4, 5, 6), dtype, order)
# each 1-D array of input should be Hermitian
if axis == 0:
a_np[0].imag = 0
Expand Down Expand Up @@ -934,8 +933,7 @@ def setup_method(self):
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
@pytest.mark.parametrize("order", ["C", "F"])
def test_rfft2(self, dtype, axes, norm, order):
x = generate_random_numpy_array((2, 3, 4), dtype)
a_np = numpy.array(x, order=order)
a_np = generate_random_numpy_array((2, 3, 4), dtype, order)
a = dpnp.asarray(a_np)

result = dpnp.fft.rfft2(a, axes=axes, norm=norm)
Expand Down Expand Up @@ -999,8 +997,7 @@ def setup_method(self):
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
@pytest.mark.parametrize("order", ["C", "F"])
def test_rfftn(self, dtype, axes, norm, order):
x = generate_random_numpy_array((2, 3, 4, 5), dtype)
a_np = numpy.array(x, order=order)
a_np = generate_random_numpy_array((2, 3, 4, 5), dtype, order)
a = dpnp.asarray(a_np)

result = dpnp.fft.rfftn(a, axes=axes, norm=norm)
Expand Down
9 changes: 4 additions & 5 deletions dpnp/tests/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,24 +503,23 @@ def test_eigenvalues(self, func, shape, dtype, order):
# non-symmetric for eig() and eigvals()
is_hermitian = func in ("eigh, eigvalsh")
a = generate_random_numpy_array(
shape, dtype, hermitian=is_hermitian, low=-4, high=4
shape, dtype, order, hermitian=is_hermitian, low=-4, high=4
)
a_order = numpy.array(a, order=order)
a_dp = dpnp.array(a, order=order)
a_dp = dpnp.array(a)

# NumPy with OneMKL and with rocSOLVER sorts in ascending order,
# so w's should be directly comparable.
# However, both OneMKL and rocSOLVER pick a different convention for
# constructing eigenvectors, so v's are not directly comparable and
# we verify them through the eigen equation A*v=w*v.
if func in ("eig", "eigh"):
w, _ = getattr(numpy.linalg, func)(a_order)
w, _ = getattr(numpy.linalg, func)(a)
w_dp, v_dp = getattr(dpnp.linalg, func)(a_dp)

self.assert_eigen_decomposition(a_dp, w_dp, v_dp)

else: # eighvals or eigvalsh
w = getattr(numpy.linalg, func)(a_order)
w = getattr(numpy.linalg, func)(a)
w_dp = getattr(dpnp.linalg, func)(a_dp)

assert_dtype_allclose(w_dp, w, factor=24)
Expand Down
Loading