Skip to content

Commit 12bb79f

Browse files
Meshgrid func (#337)
* meshgrid impl
1 parent 551e61f commit 12bb79f

File tree

4 files changed

+117
-32
lines changed

4 files changed

+117
-32
lines changed

dpnp/backend_arraycreation.pyx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ __all__ += [
4545
"dpnp_geomspace",
4646
"dpnp_linspace",
4747
"dpnp_logspace",
48+
"dpnp_meshgrid",
4849
"dpnp_tri",
4950
"dpnp_tril",
5051
"dpnp_triu",
@@ -141,6 +142,54 @@ cpdef dparray dpnp_logspace(start, stop, num, endpoint, base, dtype, axis):
141142
return dpnp.power(base, temp).astype(dtype)
142143

143144

145+
cpdef list dpnp_meshgrid(xi, copy, sparse, indexing):
146+
cdef dparray res_item
147+
148+
input_count = len(xi)
149+
150+
# simple case
151+
if input_count == 0:
152+
return []
153+
154+
# simple case
155+
if input_count == 1:
156+
return [dpnp.copy(xi[0])]
157+
158+
shape_mult = 1
159+
for i in range(input_count):
160+
shape_mult = shape_mult * xi[i].size
161+
162+
shape_list = []
163+
for i in range(input_count):
164+
shape_list.append(xi[i].size)
165+
if indexing == "xy":
166+
temp = shape_list[0]
167+
shape_list[0] = shape_list[1]
168+
shape_list[1] = temp
169+
170+
steps = []
171+
for i in range(input_count):
172+
shape_mult = shape_mult // shape_list[i]
173+
steps.append(shape_mult)
174+
if indexing == "xy":
175+
temp = steps[0]
176+
steps[0] = steps[1]
177+
steps[1] = temp
178+
179+
shape = tuple(shape_list)
180+
181+
result = []
182+
for i in range(input_count):
183+
res_item = dparray(shape=shape, dtype=xi[i].dtype)
184+
185+
for j in range(res_item.size):
186+
res_item[j] = xi[i][(j // steps[i]) % xi[i].size]
187+
188+
result.append(res_item)
189+
190+
return result
191+
192+
144193
cpdef dparray dpnp_tri(N, M, k, dtype):
145194
cdef dparray result
146195

dpnp/dpnp_iface_arraycreation.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"geomspace",
6464
"linspace",
6565
"logspace",
66+
"meshgrid",
6667
"ones",
6768
"ones_like",
6869
"tri",
@@ -354,6 +355,7 @@ def diag(v, k=0):
354355
355356
Examples
356357
--------
358+
>>> import dpnp as np
357359
>>> x = np.arange(9).reshape((3,3))
358360
>>> x
359361
array([[0, 1, 2],
@@ -391,6 +393,7 @@ def diagflat(v, k=0):
391393
392394
Examples
393395
--------
396+
>>> import dpnp as np
394397
>>> np.diagflat([[1,2], [3,4]])
395398
array([[1, 0, 0, 0],
396399
[0, 2, 0, 0],
@@ -706,6 +709,68 @@ def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0):
706709
return call_origin(numpy.logspace, start, stop, num, endpoint, base, dtype, axis)
707710

708711

712+
def meshgrid(*xi, copy=True, sparse=False, indexing='xy'):
713+
"""
714+
Return coordinate matrices from coordinate vectors.
715+
716+
Make N-D coordinate arrays for vectorized evaluations of
717+
N-D scalar/vector fields over N-D grids, given
718+
one-dimensional coordinate arrays x1, x2,..., xn.
719+
720+
For full documentation refer to :obj:`numpy.meshgrid`.
721+
722+
Limitations
723+
-----------
724+
Parameter ``copy`` is supported only with default value ``True``.
725+
Parameter ``sparse`` is supported only with default value ``False``.
726+
727+
Examples
728+
--------
729+
>>> import dpnp as np
730+
>>> nx, ny = (3, 2)
731+
>>> x = np.linspace(0, 1, nx)
732+
>>> y = np.linspace(0, 1, ny)
733+
>>> xv, yv = np.meshgrid(x, y)
734+
>>> xv
735+
array([[0. , 0.5, 1. ],
736+
[0. , 0.5, 1. ]])
737+
>>> yv
738+
array([[0., 0., 0.],
739+
[1., 1., 1.]])
740+
>>> xv, yv = np.meshgrid(x, y, sparse=True) # make sparse output arrays
741+
>>> xv
742+
array([[0. , 0.5, 1. ]])
743+
>>> yv
744+
array([[0.],
745+
[1.]])
746+
747+
`meshgrid` is very useful to evaluate functions on a grid.
748+
749+
>>> import matplotlib.pyplot as plt
750+
>>> x = np.arange(-5, 5, 0.1)
751+
>>> y = np.arange(-5, 5, 0.1)
752+
>>> xx, yy = np.meshgrid(x, y, sparse=True)
753+
>>> z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2)
754+
>>> h = plt.contourf(x,y,z)
755+
>>> plt.show()
756+
757+
"""
758+
759+
if not use_origin_backend():
760+
# original limitation
761+
if indexing not in ["ij", "xy"]:
762+
checker_throw_value_error("meshgrid", "indexing", indexing, "'ij' or 'xy'")
763+
764+
if copy is not True:
765+
checker_throw_value_error("meshgrid", "copy", copy, True)
766+
if sparse is not False:
767+
checker_throw_value_error("meshgrid", "sparse", sparse, False)
768+
769+
return dpnp_meshgrid(xi, copy, sparse, indexing)
770+
771+
return call_origin(numpy.meshgrid, xi, copy, sparse, indexing)
772+
773+
709774
def ones(shape, dtype=None, order='C'):
710775
"""
711776
Return a new array of given shape and type, filled with ones.
@@ -797,6 +862,7 @@ def tri(N, M=None, k=0, dtype=numpy.float):
797862
798863
Examples
799864
--------
865+
>>> import dpnp as np
800866
>>> np.tri(3, 5, 2, dtype=int)
801867
array([[1, 1, 1, 0, 0],
802868
[1, 1, 1, 1, 0],
@@ -825,6 +891,7 @@ def tril(m, k=0):
825891
826892
Examples
827893
--------
894+
>>> import dpnp as np
828895
>>> np.tril([[1,2,3],[4,5,6],[7,8,9],[10,11,12]], -1)
829896
array([[ 0, 0, 0],
830897
[ 4, 0, 0],
@@ -853,6 +920,7 @@ def triu(m, k=0):
853920
854921
Examples
855922
--------
923+
>>> import dpnp as np
856924
>>> np.triu([[1,2,3],[4,5,6],[7,8,9],[10,11,12]], -1)
857925
array([[ 1, 2, 3],
858926
[ 4, 5, 6],

tests/skipped_tests.tbl

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -315,14 +315,6 @@ tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_12_
315315
tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_13_{copy=False, ndmin=2, xp=dpnp}::test_cupy_array
316316
tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_14_{copy=False, ndmin=3, xp=numpy}::test_cupy_array
317317
tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_15_{copy=False, ndmin=3, xp=dpnp}::test_cupy_array
318-
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange3
319-
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange4
320-
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange5
321-
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange8
322-
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange9
323-
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange_negative_size
324-
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange_no_dtype_float
325-
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange_no_dtype_int
326318
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_array_start_stop
327319
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_array_start_stop_axis1
328320
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_float_underflow
@@ -357,18 +349,10 @@ tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_3_{copy
357349
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_3_{copy=False, indexing='ij', sparse=True}::test_meshgrid1
358350
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_3_{copy=False, indexing='ij', sparse=True}::test_meshgrid2
359351
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_3_{copy=False, indexing='ij', sparse=True}::test_meshgrid3
360-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_4_{copy=True, indexing='xy', sparse=False}::test_meshgrid0
361-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_4_{copy=True, indexing='xy', sparse=False}::test_meshgrid1
362-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_4_{copy=True, indexing='xy', sparse=False}::test_meshgrid2
363-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_4_{copy=True, indexing='xy', sparse=False}::test_meshgrid3
364352
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_5_{copy=True, indexing='xy', sparse=True}::test_meshgrid0
365353
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_5_{copy=True, indexing='xy', sparse=True}::test_meshgrid1
366354
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_5_{copy=True, indexing='xy', sparse=True}::test_meshgrid2
367355
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_5_{copy=True, indexing='xy', sparse=True}::test_meshgrid3
368-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_6_{copy=True, indexing='ij', sparse=False}::test_meshgrid0
369-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_6_{copy=True, indexing='ij', sparse=False}::test_meshgrid1
370-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_6_{copy=True, indexing='ij', sparse=False}::test_meshgrid2
371-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_6_{copy=True, indexing='ij', sparse=False}::test_meshgrid3
372356
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy=True, indexing='ij', sparse=True}::test_meshgrid0
373357
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy=True, indexing='ij', sparse=True}::test_meshgrid1
374358
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy=True, indexing='ij', sparse=True}::test_meshgrid2

tests/skipped_tests_gpu.tbl

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -365,18 +365,10 @@ tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_3_{copy
365365
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_3_{copy=False, indexing='ij', sparse=True}::test_meshgrid1
366366
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_3_{copy=False, indexing='ij', sparse=True}::test_meshgrid2
367367
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_3_{copy=False, indexing='ij', sparse=True}::test_meshgrid3
368-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_4_{copy=True, indexing='xy', sparse=False}::test_meshgrid0
369-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_4_{copy=True, indexing='xy', sparse=False}::test_meshgrid1
370-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_4_{copy=True, indexing='xy', sparse=False}::test_meshgrid2
371-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_4_{copy=True, indexing='xy', sparse=False}::test_meshgrid3
372368
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_5_{copy=True, indexing='xy', sparse=True}::test_meshgrid0
373369
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_5_{copy=True, indexing='xy', sparse=True}::test_meshgrid1
374370
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_5_{copy=True, indexing='xy', sparse=True}::test_meshgrid2
375371
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_5_{copy=True, indexing='xy', sparse=True}::test_meshgrid3
376-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_6_{copy=True, indexing='ij', sparse=False}::test_meshgrid0
377-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_6_{copy=True, indexing='ij', sparse=False}::test_meshgrid1
378-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_6_{copy=True, indexing='ij', sparse=False}::test_meshgrid2
379-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_6_{copy=True, indexing='ij', sparse=False}::test_meshgrid3
380372
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy=True, indexing='ij', sparse=True}::test_meshgrid0
381373
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy=True, indexing='ij', sparse=True}::test_meshgrid1
382374
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy=True, indexing='ij', sparse=True}::test_meshgrid2
@@ -393,14 +385,6 @@ tests/third_party/cupy/creation_tests/test_ranges.py::TestOgrid::test_ogrid2
393385
tests/third_party/cupy/creation_tests/test_ranges.py::TestOgrid::test_ogrid3
394386
tests/third_party/cupy/creation_tests/test_ranges.py::TestOgrid::test_ogrid4
395387
tests/third_party/cupy/creation_tests/test_ranges.py::TestOgrid::test_ogrid5
396-
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange3
397-
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange4
398-
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange5
399-
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange8
400-
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange9
401-
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange_negative_size
402-
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange_no_dtype_float
403-
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange_no_dtype_int
404388
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_array_start_stop
405389
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_array_start_stop_axis1
406390
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_float_underflow

0 commit comments

Comments
 (0)