Skip to content

Commit 8f05542

Browse files
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 904227e commit 8f05542

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
@@ -162,7 +162,13 @@ def get_all_dtypes(
162162

163163

164164
def generate_random_numpy_array(
165-
shape, dtype=None, hermitian=False, seed_value=None, low=-10, high=10
165+
shape,
166+
dtype=None,
167+
order="C",
168+
hermitian=False,
169+
seed_value=None,
170+
low=-10,
171+
high=10,
166172
):
167173
"""
168174
Generate a random numpy array with the specified shape and dtype.
@@ -178,6 +184,9 @@ def generate_random_numpy_array(
178184
Desired data-type for the output array.
179185
If not specified, data type will be determined by numpy.
180186
Default : ``None``
187+
order : {"C", "F"}, optional
188+
Specify the memory layout of the output array.
189+
Default: ``"C"``.
181190
hermitian : bool, optional
182191
If True, generates a Hermitian (symmetric if `dtype` is real) matrix.
183192
Default : ``False``
@@ -194,7 +203,7 @@ def generate_random_numpy_array(
194203
Returns
195204
-------
196205
out : numpy.ndarray
197-
A random numpy array of the specified shape and dtype.
206+
A random numpy array of the specified shape, dtype and memory layout.
198207
The array is Hermitian or symmetric if `hermitian` is True.
199208
200209
Note:
@@ -224,6 +233,10 @@ def generate_random_numpy_array(
224233
a = a.reshape(orig_shape)
225234
else:
226235
a = numpy.conj(a.T) @ a
236+
237+
# a.reshape(shape) returns an array in C order by default
238+
if order != "C" and a.ndim > 1:
239+
a = numpy.array(a, order=order)
227240
return a
228241

229242

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
@@ -934,8 +933,7 @@ def setup_method(self):
934933
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
935934
@pytest.mark.parametrize("order", ["C", "F"])
936935
def test_rfft2(self, dtype, axes, norm, order):
937-
x = generate_random_numpy_array((2, 3, 4), dtype)
938-
a_np = numpy.array(x, order=order)
936+
a_np = generate_random_numpy_array((2, 3, 4), dtype, order)
939937
a = dpnp.asarray(a_np)
940938

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

10061003
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)