Skip to content

Commit 5c46710

Browse files
vlad-perevezentsevvtavana
authored andcommitted
Add order agrument to generate_random_numpy_array() (#2237)
This PR suggests updating the helper function `generate_random_numpy_array()` by adding an argument `order` to generate arrays in different orders Additionally Input generation for tests was changed in #2227 , now `test_fft2` and `test_fftn` do not use `order` parameter. This PR suggests to update these tests to use input data with different orders.
1 parent fc64fa4 commit 5c46710

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

dpnp/tests/helper.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,13 @@ def not_excluded(dtype):
191191

192192

193193
def generate_random_numpy_array(
194-
shape, dtype=None, hermitian=False, seed_value=None, low=-10, high=10
194+
shape,
195+
dtype=None,
196+
order="C",
197+
hermitian=False,
198+
seed_value=None,
199+
low=-10,
200+
high=10,
195201
):
196202
"""
197203
Generate a random numpy array with the specified shape and dtype.
@@ -207,6 +213,9 @@ def generate_random_numpy_array(
207213
Desired data-type for the output array.
208214
If not specified, data type will be determined by numpy.
209215
Default : ``None``
216+
order : {"C", "F"}, optional
217+
Specify the memory layout of the output array.
218+
Default: ``"C"``.
210219
hermitian : bool, optional
211220
If True, generates a Hermitian (symmetric if `dtype` is real) matrix.
212221
Default : ``False``
@@ -223,7 +232,7 @@ def generate_random_numpy_array(
223232
Returns
224233
-------
225234
out : numpy.ndarray
226-
A random numpy array of the specified shape and dtype.
235+
A random numpy array of the specified shape, dtype and memory layout.
227236
The array is Hermitian or symmetric if `hermitian` is True.
228237
229238
Note:
@@ -256,6 +265,10 @@ def generate_random_numpy_array(
256265
a = a.reshape(orig_shape)
257266
else:
258267
a = numpy.conj(a.T) @ a
268+
269+
# a.reshape(shape) returns an array in C order by default
270+
if order != "C" and a.ndim > 1:
271+
a = numpy.array(a, order=order)
259272
return a
260273

261274

dpnp/tests/test_fft.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ def setup_method(self):
380380
@pytest.mark.parametrize("norm", [None, "forward", "backward", "ortho"])
381381
@pytest.mark.parametrize("order", ["C", "F"])
382382
def test_fft2(self, dtype, axes, norm, order):
383-
a_np = generate_random_numpy_array((2, 3, 4), dtype)
383+
a_np = generate_random_numpy_array((2, 3, 4), dtype, order)
384384
a = dpnp.array(a_np)
385385

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

448448
result = dpnp.fft.fftn(a, axes=axes, norm=norm)
@@ -696,8 +696,7 @@ def test_irfft_1D_on_2D_array(self, dtype, n, axis, norm, order):
696696
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
697697
@pytest.mark.parametrize("order", ["C", "F"])
698698
def test_irfft_1D_on_3D_array(self, dtype, n, axis, norm, order):
699-
x = generate_random_numpy_array((4, 5, 6), dtype)
700-
a_np = numpy.array(x, order=order)
699+
a_np = generate_random_numpy_array((4, 5, 6), dtype, order)
701700
# each 1-D array of input should be Hermitian
702701
if axis == 0:
703702
a_np[0].imag = 0
@@ -937,8 +936,7 @@ def setup_method(self):
937936
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
938937
@pytest.mark.parametrize("order", ["C", "F"])
939938
def test_rfft2(self, dtype, axes, norm, order):
940-
x = generate_random_numpy_array((2, 3, 4), dtype)
941-
a_np = numpy.array(x, order=order)
939+
a_np = generate_random_numpy_array((2, 3, 4), dtype, order)
942940
a = dpnp.asarray(a_np)
943941

944942
result = dpnp.fft.rfft2(a, axes=axes, norm=norm)
@@ -1002,8 +1000,7 @@ def setup_method(self):
10021000
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
10031001
@pytest.mark.parametrize("order", ["C", "F"])
10041002
def test_rfftn(self, dtype, axes, norm, order):
1005-
x = generate_random_numpy_array((2, 3, 4, 5), dtype)
1006-
a_np = numpy.array(x, order=order)
1003+
a_np = generate_random_numpy_array((2, 3, 4, 5), dtype, order)
10071004
a = dpnp.asarray(a_np)
10081005

10091006
result = dpnp.fft.rfftn(a, axes=axes, norm=norm)

dpnp/tests/test_linalg.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -503,24 +503,23 @@ def test_eigenvalues(self, func, shape, dtype, order):
503503
# non-symmetric for eig() and eigvals()
504504
is_hermitian = func in ("eigh, eigvalsh")
505505
a = generate_random_numpy_array(
506-
shape, dtype, hermitian=is_hermitian, low=-4, high=4
506+
shape, dtype, order, hermitian=is_hermitian, low=-4, high=4
507507
)
508-
a_order = numpy.array(a, order=order)
509-
a_dp = dpnp.array(a, order=order)
508+
a_dp = dpnp.array(a)
510509

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

520519
self.assert_eigen_decomposition(a_dp, w_dp, v_dp)
521520

522521
else: # eighvals or eigvalsh
523-
w = getattr(numpy.linalg, func)(a_order)
522+
w = getattr(numpy.linalg, func)(a)
524523
w_dp = getattr(dpnp.linalg, func)(a_dp)
525524

526525
assert_dtype_allclose(w_dp, w, factor=24)

0 commit comments

Comments
 (0)