Skip to content

Commit 00289ce

Browse files
authored
algo module free of dparray (#871)
1 parent 3e813d1 commit 00289ce

File tree

4 files changed

+77
-81
lines changed

4 files changed

+77
-81
lines changed

dpnp/dpnp_algo/dpnp_algo.pyx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import dpnp.dpnp_utils as utils_py
3939
import numpy
4040

4141
cimport cpython
42-
from dpnp.dparray cimport dparray
4342
cimport dpnp.dpnp_utils as utils
4443
cimport numpy
4544

dpnp/dpnp_algo/dpnp_algo_mathematical.pyx

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,18 @@ ctypedef void(*ftpr_custom_trapz_2in_1out_with_2size_t)(void *, void * , void *
8080
ctypedef void(*ftpr_custom_around_1in_1out_t)(const void * , void * , const size_t, const int)
8181

8282

83-
cpdef dparray dpnp_absolute(utils.dpnp_descriptor input):
83+
cpdef utils.dpnp_descriptor dpnp_absolute(utils.dpnp_descriptor input):
8484
cdef shape_type_c input_shape = input.shape
8585
cdef size_t input_shape_size = input.ndim
8686

87-
# convert string type names (dparray.dtype) to C enum DPNPFuncType
87+
# convert string type names (array.dtype) to C enum DPNPFuncType
8888
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(input.dtype)
8989

9090
# get the FPTR data structure
9191
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_ABSOLUTE, param1_type, param1_type)
9292

93-
result_type = dpnp_DPNPFuncType_to_dtype( < size_t > kernel_data.return_type)
9493
# ceate result array with type given by FPTR data
95-
cdef dparray result = dparray(input_shape, dtype=result_type)
94+
cdef utils.dpnp_descriptor result = utils.create_output_descriptor(input_shape, kernel_data.return_type, None)
9695

9796
cdef fptr_custom_elemwise_absolute_1in_1out_t func = <fptr_custom_elemwise_absolute_1in_1out_t > kernel_data.ptr
9897
# call FPTR function
@@ -117,15 +116,15 @@ cpdef utils.dpnp_descriptor dpnp_arctan2(utils.dpnp_descriptor x1_obj,
117116
return call_fptr_2in_1out(DPNP_FN_ARCTAN2, x1_obj, x2_obj, dtype, out, where, func_name="arctan2")
118117

119118

120-
cpdef dpnp_around(utils.dpnp_descriptor x1, int decimals):
119+
cpdef utils.dpnp_descriptor dpnp_around(utils.dpnp_descriptor x1, int decimals):
121120

122121
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(x1.dtype)
123122

124123
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_AROUND, param1_type, param1_type)
125124

126-
result_type = dpnp_DPNPFuncType_to_dtype(< size_t > kernel_data.return_type)
127-
128-
cdef dparray result = dparray(x1.shape, dtype=result_type)
125+
# ceate result array with type given by FPTR data
126+
cdef shape_type_c result_shape = x1.shape
127+
cdef utils.dpnp_descriptor result = utils.create_output_descriptor(result_shape, kernel_data.return_type, None)
129128

130129
cdef ftpr_custom_around_1in_1out_t func = <ftpr_custom_around_1in_1out_t > kernel_data.ptr
131130

@@ -182,11 +181,11 @@ cpdef utils.dpnp_descriptor dpnp_cumsum(utils.dpnp_descriptor x1):
182181
return call_fptr_1in_1out(DPNP_FN_CUMSUM, x1, (x1.size,))
183182

184183

185-
cpdef dparray dpnp_diff(object input, int n):
184+
cpdef object dpnp_diff(utils.dpnp_descriptor input, int n):
186185
if n == 0:
187-
return input
186+
return input.get_pyobj()
188187
if n < input.shape[-1]:
189-
arr = input
188+
arr = input.get_pyobj()
190189
for _ in range(n):
191190
list_shape_i = list(arr.shape)
192191
list_shape_i[-1] = list_shape_i[-1] - 1
@@ -249,23 +248,25 @@ cpdef utils.dpnp_descriptor dpnp_fmod(utils.dpnp_descriptor x1_obj,
249248
return call_fptr_2in_1out(DPNP_FN_FMOD, x1_obj, x2_obj, dtype, out, where)
250249

251250

252-
cpdef dparray dpnp_gradient(object y1, int dx=1):
251+
cpdef utils.dpnp_descriptor dpnp_gradient(utils.dpnp_descriptor y1, int dx=1):
253252

254-
size = y1.size
253+
cdef size_t size = y1.size
255254

256-
cdef dparray result = dparray(size, dtype=dpnp.float64)
255+
# ceate result array with type given by FPTR data
256+
cdef shape_type_c result_shape = utils._object_to_tuple(size)
257+
cdef utils.dpnp_descriptor result = utils_py.create_output_descriptor_py(result_shape, dpnp.float64, None)
257258

258-
cur = (y1[1] - y1[0]) / dx
259+
cdef double cur = (y1.get_pyobj()[1] - y1.get_pyobj()[0]) / dx
259260

260-
result._setitem_scalar(0, cur)
261+
result.get_pyobj().flat[0] = cur
261262

262-
cur = (y1[-1] - y1[-2]) / dx
263+
cur = (y1.get_pyobj()[-1] - y1.get_pyobj()[-2]) / dx
263264

264-
result._setitem_scalar(size - 1, cur)
265+
result.get_pyobj().flat[size - 1] = cur
265266

266267
for i in range(1, size - 1):
267-
cur = (y1[i + 1] - y1[i - 1]) / (2 * dx)
268-
result._setitem_scalar(i, cur)
268+
cur = (y1.get_pyobj()[i + 1] - y1.get_pyobj()[i - 1]) / (2 * dx)
269+
result.get_pyobj().flat[i] = cur
269270

270271
return result
271272

@@ -295,22 +296,22 @@ cpdef utils.dpnp_descriptor dpnp_minimum(utils.dpnp_descriptor x1_obj,
295296

296297

297298
cpdef tuple dpnp_modf(utils.dpnp_descriptor x1):
298-
""" Convert string type names (dparray.dtype) to C enum DPNPFuncType """
299+
""" Convert string type names (array.dtype) to C enum DPNPFuncType """
299300
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(x1.dtype)
300301

301302
""" get the FPTR data structure """
302303
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_MODF, param1_type, DPNP_FT_NONE)
303304

304-
result_type = dpnp_DPNPFuncType_to_dtype( < size_t > kernel_data.return_type)
305-
""" Create result arrays with type given by FPTR data """
306-
cdef dparray result1 = dparray(x1.shape, dtype=result_type)
307-
cdef dparray result2 = dparray(x1.shape, dtype=result_type)
305+
# ceate result array with type given by FPTR data
306+
cdef shape_type_c result_shape = x1.shape
307+
cdef utils.dpnp_descriptor result1 = utils.create_output_descriptor(result_shape, kernel_data.return_type, None)
308+
cdef utils.dpnp_descriptor result2 = utils.create_output_descriptor(result_shape, kernel_data.return_type, None)
308309

309310
cdef fptr_1in_2out_t func = <fptr_1in_2out_t > kernel_data.ptr
310311
""" Call FPTR function """
311312
func(x1.get_data(), result1.get_data(), result2.get_data(), x1.size)
312313

313-
return result1, result2
314+
return (result1.get_pyobj(), result2.get_pyobj())
314315

315316

316317
cpdef utils.dpnp_descriptor dpnp_multiply(utils.dpnp_descriptor x1_obj,
@@ -343,38 +344,32 @@ cpdef utils.dpnp_descriptor dpnp_nancumsum(utils.dpnp_descriptor x1):
343344
return dpnp_cumsum(x1_desc)
344345

345346

346-
cpdef dpnp_nanprod(object x1):
347-
cdef dparray result = dparray(x1.shape, dtype=x1.dtype)
347+
cpdef utils.dpnp_descriptor dpnp_nanprod(utils.dpnp_descriptor x1):
348+
cdef utils.dpnp_descriptor result = utils_py.create_output_descriptor_py(x1.shape, x1.dtype, None)
348349

349350
for i in range(result.size):
350-
input_elem = x1.item(i)
351+
input_elem = x1.get_pyobj().flat[i]
351352

352353
if dpnp.isnan(input_elem):
353-
result._setitem_scalar(i, 1)
354+
result.get_pyobj().flat[i] = 1
354355
else:
355-
result._setitem_scalar(i, input_elem)
356+
result.get_pyobj().flat[i] = input_elem
356357

357-
result_desc = dpnp.get_dpnp_descriptor(result) # TODO remove it later
358-
return dpnp_prod(result_desc)
358+
return dpnp_prod(result)
359359

360360

361-
cpdef dpnp_nansum(object x1):
362-
cdef dparray result = dparray(x1.shape, dtype=x1.dtype)
361+
cpdef utils.dpnp_descriptor dpnp_nansum(utils.dpnp_descriptor x1):
362+
cdef utils.dpnp_descriptor result = utils_py.create_output_descriptor_py(x1.shape, x1.dtype, None)
363363

364364
for i in range(result.size):
365-
input_elem = x1.item(i)
365+
input_elem = x1.get_pyobj().flat[i]
366366

367367
if dpnp.isnan(input_elem):
368-
result._setitem_scalar(i, 0)
368+
result.get_pyobj().flat[i] = 0
369369
else:
370-
result._setitem_scalar(i, input_elem)
370+
result.get_pyobj().flat[i] = input_elem
371371

372-
# due to bug in dpnp_sum need this workaround
373-
# return dpnp_sum(result)
374-
375-
result_desc = dpnp.get_dpnp_descriptor(result) # TODO remove it later
376-
sum_result = dpnp_sum(result_desc).get_pyobj()
377-
return x1.dtype.type(sum_result[0])
372+
return dpnp_sum(result)
378373

379374

380375
cpdef utils.dpnp_descriptor dpnp_negative(dpnp_descriptor x1):
@@ -476,20 +471,20 @@ cpdef utils.dpnp_descriptor dpnp_sum(utils.dpnp_descriptor input,
476471
return result
477472

478473

479-
cpdef dpnp_trapz(utils.dpnp_descriptor y1, dparray x1, double dx):
474+
cpdef utils.dpnp_descriptor dpnp_trapz(utils.dpnp_descriptor y1, utils.dpnp_descriptor x1, double dx):
480475

481476
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(y1.dtype)
482477
cdef DPNPFuncType param2_type = dpnp_dtype_to_DPNPFuncType(x1.dtype)
483478
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_TRAPZ, param1_type, param2_type)
484479

485-
result_type = dpnp_DPNPFuncType_to_dtype( < size_t > kernel_data.return_type)
486-
487-
cdef dparray result = dparray((1,), dtype=result_type)
480+
# ceate result array with type given by FPTR data
481+
cdef shape_type_c result_shape = (1,)
482+
cdef utils.dpnp_descriptor result = utils.create_output_descriptor(result_shape, kernel_data.return_type, None)
488483

489484
cdef ftpr_custom_trapz_2in_1out_with_2size_t func = <ftpr_custom_trapz_2in_1out_with_2size_t > kernel_data.ptr
490485
func(y1.get_data(), x1.get_data(), result.get_data(), dx, y1.size, x1.size)
491486

492-
return result[0]
487+
return result
493488

494489

495490
cpdef utils.dpnp_descriptor dpnp_trunc(utils.dpnp_descriptor x1, utils.dpnp_descriptor out):

dpnp/dpnp_iface_mathematical.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def absolute(x1, **kwargs):
148148
if not x1_desc.ndim:
149149
pass
150150
else:
151-
result = dpnp_absolute(x1_desc)
151+
result = dpnp_absolute(x1_desc).get_pyobj()
152152

153153
return result
154154

@@ -246,7 +246,7 @@ def around(x1, decimals=0, out=None):
246246
elif decimals != 0:
247247
pass
248248
else:
249-
return dpnp_around(x1_desc, decimals)
249+
return dpnp_around(x1_desc, decimals).get_pyobj()
250250

251251
return call_origin(numpy.around, x1, decimals=decimals, out=out)
252252

@@ -529,7 +529,7 @@ def diff(x1, n=1, axis=-1, prepend=None, append=None):
529529
elif append is not None:
530530
pass
531531
else:
532-
return dpnp_diff(x1, n)
532+
return dpnp_diff(x1_desc, n)
533533

534534
return call_origin(numpy.diff, x1, n, axis, prepend, append)
535535

@@ -903,9 +903,9 @@ def gradient(x1, *varargs, **kwargs):
903903
pass
904904
else:
905905
if len(varargs) == 0:
906-
return dpnp_gradient(x1)
906+
return dpnp_gradient(x1_desc).get_pyobj()
907907

908-
return dpnp_gradient(x1, varargs[0])
908+
return dpnp_gradient(x1_desc, varargs[0]).get_pyobj()
909909

910910
return call_origin(numpy.gradient, x1, *varargs, **kwargs)
911911

@@ -1045,7 +1045,7 @@ def mod(*args, **kwargs):
10451045
return dpnp.remainder(*args, **kwargs)
10461046

10471047

1048-
def modf(x, **kwargs):
1048+
def modf(x1, **kwargs):
10491049
"""
10501050
Return the fractional and integral parts of an array, element-wise.
10511051
@@ -1069,11 +1069,11 @@ def modf(x, **kwargs):
10691069
10701070
"""
10711071

1072-
dpnp_desc = dpnp.get_dpnp_descriptor(x)
1073-
if dpnp_desc and not kwargs:
1074-
return dpnp_modf(dpnp_desc)
1072+
x1_desc = dpnp.get_dpnp_descriptor(x1)
1073+
if x1_desc and not kwargs:
1074+
return dpnp_modf(x1_desc)
10751075

1076-
return call_origin(numpy.modf, x, **kwargs)
1076+
return call_origin(numpy.modf, x1, **kwargs)
10771077

10781078

10791079
def multiply(x1, x2, dtype=None, out=None, where=True, **kwargs):
@@ -1224,7 +1224,7 @@ def nanprod(x1, **kwargs):
12241224

12251225
x1_desc = dpnp.get_dpnp_descriptor(x1)
12261226
if x1_desc and not kwargs:
1227-
return dpnp_nanprod(x1)
1227+
return dpnp_nanprod(x1_desc).get_pyobj()
12281228

12291229
return call_origin(numpy.nanprod, x1, **kwargs)
12301230

@@ -1254,7 +1254,9 @@ def nansum(x1, **kwargs):
12541254

12551255
x1_desc = dpnp.get_dpnp_descriptor(x1)
12561256
if x1_desc and not kwargs:
1257-
return dpnp_nansum(x1)
1257+
result_obj = dpnp_nansum(x1_desc).get_pyobj()
1258+
result = dpnp.convert_single_elem_array_to_scalar(result_obj)
1259+
return result
12581260

12591261
return call_origin(numpy.nansum, x1, **kwargs)
12601262

@@ -1586,7 +1588,7 @@ def sum(x1, axis=None, dtype=None, out=None, keepdims=False, initial=None, where
15861588
return call_origin(numpy.sum, x1, axis=axis, dtype=dtype, out=out, keepdims=keepdims, initial=initial, where=where)
15871589

15881590

1589-
def trapz(y, x=None, dx=1.0, axis=-1):
1591+
def trapz(y1, x1=None, dx=1.0, axis=-1):
15901592
"""
15911593
Integrate along the given axis using the composite trapezoidal rule.
15921594
@@ -1613,23 +1615,23 @@ def trapz(y, x=None, dx=1.0, axis=-1):
16131615
16141616
"""
16151617

1616-
y_desc = dpnp.get_dpnp_descriptor(y)
1618+
y_desc = dpnp.get_dpnp_descriptor(y1)
16171619
if y_desc:
1618-
if not isinstance(x, dparray) and x is not None:
1619-
pass
1620-
elif x is not None and y_desc.size != x.size:
1621-
pass
1622-
elif x is not None and y_desc.shape != x.shape:
1623-
pass
1624-
elif y_desc.ndim > 1:
1620+
if y_desc.ndim > 1:
16251621
pass
16261622
else:
1627-
if x is None:
1628-
x = dpnp.empty(0, dtype=y_desc.dtype)
1629-
1630-
return dpnp_trapz(y_desc, x, dx)
1631-
1632-
return call_origin(numpy.trapz, y, x, dx, axis)
1623+
x_obj = dpnp.empty(y_desc.shape, dtype=y_desc.dtype) if x1 is None else x1
1624+
x_desc = dpnp.get_dpnp_descriptor(x_obj)
1625+
if x_desc:
1626+
pass
1627+
elif y_desc.size != x_desc.size:
1628+
pass
1629+
elif y_desc.shape != x_desc.shape:
1630+
pass
1631+
else:
1632+
return dpnp_trapz(y_desc, x_desc, dx).get_pyobj()
1633+
1634+
return call_origin(numpy.trapz, y1, x1, dx, axis)
16331635

16341636

16351637
def true_divide(*args, **kwargs):

tests/test_mathematical.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,17 +331,17 @@ def test_trapz_with_x_params(self, y_array, x_array, data_type_y, data_type_x):
331331
np_x = numpy.array(x_array, dtype=data_type_x)
332332
dpnp_x = dpnp.array(x_array, dtype=data_type_x)
333333

334-
result = dpnp.trapz(dpnp_y, x=dpnp_x)
335-
expected = numpy.trapz(np_y, x=np_x)
334+
result = dpnp.trapz(dpnp_y, dpnp_x)
335+
expected = numpy.trapz(np_y, np_x)
336336
numpy.testing.assert_array_equal(expected, result)
337337

338338
@pytest.mark.parametrize("array", [[1, 2, 3], [4, 5, 6]])
339339
def test_trapz_with_x_param_2ndim(self, array):
340340
np_a = numpy.array(array)
341341
dpnp_a = dpnp.array(array)
342342

343-
result = dpnp.trapz(dpnp_a, x=dpnp_a)
344-
expected = numpy.trapz(np_a, x=np_a)
343+
result = dpnp.trapz(dpnp_a, dpnp_a)
344+
expected = numpy.trapz(np_a, np_a)
345345
numpy.testing.assert_array_equal(expected, result)
346346

347347
@pytest.mark.parametrize("y_array", [[1, 2, 4, 5],

0 commit comments

Comments
 (0)