Skip to content

Commit 827c603

Browse files
authored
Add parameter out to four trigonometric functions (#786)
* change call_fptr_1in_1out function
1 parent 5c5804c commit 827c603

File tree

5 files changed

+184
-23
lines changed

5 files changed

+184
-23
lines changed

dpnp/dpnp_algo/dpnp_algo.pxd

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ cpdef dparray dpnp_init_val(shape, dtype, value)
287287
Mathematical functions
288288
"""
289289
cpdef dparray dpnp_add(object x1_obj, object x2_obj, object dtype=*, dparray out=*, object where=*)
290-
cpdef dparray dpnp_arctan2(object x1_obj, object x2_obj, object dtype=*, dparray out=*, object where=*)
290+
cpdef dparray dpnp_arctan2(dpnp_descriptor x1_obj, dpnp_descriptor x2_obj, object dtype=*, dparray out=*, object where=*)
291291
cpdef dparray dpnp_divide(object x1_obj, object x2_obj, object dtype=*, dparray out=*, object where=*)
292292
cpdef dparray dpnp_hypot(object x1_obj, object x2_obj, object dtype=*, dparray out=*, object where=*)
293293
cpdef dparray dpnp_maximum(object x1_obj, object x2_obj, object dtype=*, dparray out=*, object where=*)
@@ -331,9 +331,9 @@ Trigonometric functions
331331
"""
332332
cpdef dparray dpnp_arccos(dpnp_descriptor array1)
333333
cpdef dparray dpnp_arccosh(dpnp_descriptor array1)
334-
cpdef dparray dpnp_arcsin(dpnp_descriptor array1)
334+
cpdef dparray dpnp_arcsin(dpnp_descriptor array1, dparray out)
335335
cpdef dparray dpnp_arcsinh(dpnp_descriptor array1)
336-
cpdef dparray dpnp_arctan(dpnp_descriptor array1)
336+
cpdef dparray dpnp_arctan(dpnp_descriptor array1, dparray out)
337337
cpdef dparray dpnp_arctanh(dpnp_descriptor array1)
338338
cpdef dparray dpnp_cbrt(dpnp_descriptor array1)
339339
cpdef dparray dpnp_cos(dpnp_descriptor array1, dparray out)
@@ -352,5 +352,5 @@ cpdef dparray dpnp_sin(dpnp_descriptor array1, dparray out)
352352
cpdef dparray dpnp_sinh(dpnp_descriptor array1)
353353
cpdef dparray dpnp_sqrt(dpnp_descriptor array1)
354354
cpdef dparray dpnp_square(dpnp_descriptor array1)
355-
cpdef dparray dpnp_tan(dpnp_descriptor array1)
355+
cpdef dparray dpnp_tan(dpnp_descriptor array1, dparray out)
356356
cpdef dparray dpnp_tanh(dpnp_descriptor array1)

dpnp/dpnp_algo/dpnp_algo_mathematical.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ cpdef dparray dpnp_add(object x1_obj, object x2_obj, object dtype=None, dparray
105105
return call_fptr_2in_1out(DPNP_FN_ADD, x1_obj, x2_obj, dtype=dtype, out=out, where=where)
106106

107107

108-
cpdef dparray dpnp_arctan2(object x1_obj, object x2_obj, object dtype=None, dparray out=None, object where=True):
109-
return call_fptr_2in_1out(DPNP_FN_ARCTAN2, x1_obj, x2_obj, dtype=dtype, out=out, where=where)
108+
cpdef dparray dpnp_arctan2(utils.dpnp_descriptor x1_obj, utils.dpnp_descriptor x2_obj, object dtype=None, dparray out=None, object where=True):
109+
return call_fptr_2in_1out(DPNP_FN_ARCTAN2, x1_obj, x2_obj, dtype=dtype, out=out, where=where, func_name="arctan2")
110110

111111

112112
cpdef dpnp_around(utils.dpnp_descriptor x1, int decimals):

dpnp/dpnp_algo/dpnp_algo_trigonometric.pyx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,16 @@ cpdef dparray dpnp_arccosh(utils.dpnp_descriptor x1):
7272
return call_fptr_1in_1out(DPNP_FN_ARCCOSH, x1, x1.shape)
7373

7474

75-
cpdef dparray dpnp_arcsin(utils.dpnp_descriptor x1):
76-
return call_fptr_1in_1out(DPNP_FN_ARCSIN, x1, x1.shape)
75+
cpdef dparray dpnp_arcsin(utils.dpnp_descriptor x1, dparray out):
76+
return call_fptr_1in_1out(DPNP_FN_ARCSIN, x1, x1.shape, out=out, func_name='arcsin')
7777

7878

7979
cpdef dparray dpnp_arcsinh(utils.dpnp_descriptor x1):
8080
return call_fptr_1in_1out(DPNP_FN_ARCSINH, x1, x1.shape)
8181

8282

83-
cpdef dparray dpnp_arctan(utils.dpnp_descriptor x1):
84-
return call_fptr_1in_1out(DPNP_FN_ARCTAN, x1, x1.shape)
83+
cpdef dparray dpnp_arctan(utils.dpnp_descriptor x1, dparray out):
84+
return call_fptr_1in_1out(DPNP_FN_ARCTAN, x1, x1.shape, out=out, func_name='arctan')
8585

8686

8787
cpdef dparray dpnp_arctanh(utils.dpnp_descriptor x1):
@@ -156,8 +156,8 @@ cpdef dparray dpnp_square(utils.dpnp_descriptor x1):
156156
return call_fptr_1in_1out(DPNP_FN_SQUARE, x1, x1.shape)
157157

158158

159-
cpdef dparray dpnp_tan(utils.dpnp_descriptor x1):
160-
return call_fptr_1in_1out(DPNP_FN_TAN, x1, x1.shape)
159+
cpdef dparray dpnp_tan(utils.dpnp_descriptor x1, dparray out):
160+
return call_fptr_1in_1out(DPNP_FN_TAN, x1, x1.shape, out=out, func_name='tan')
161161

162162

163163
cpdef dparray dpnp_tanh(utils.dpnp_descriptor x1):

dpnp/dpnp_iface_trigonometric.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def arccosh(x1):
153153
return call_origin(numpy.arccosh, x1, **kwargs)
154154

155155

156-
def arcsin(x1):
156+
def arcsin(x1, out=None, **kwargs):
157157
"""
158158
Inverse sine, element-wise.
159159
@@ -162,6 +162,7 @@ def arcsin(x1):
162162
Limitations
163163
-----------
164164
Input array is supported as :obj:`dpnp.ndarray`.
165+
Keyword arguments ``kwargs`` are currently unsupported.
165166
Input array data types are limited by supported DPNP :ref:`Data types`.
166167
167168
See Also
@@ -186,9 +187,9 @@ def arcsin(x1):
186187

187188
x1_desc = dpnp.get_dpnp_descriptor(x1)
188189
if x1_desc:
189-
return dpnp_arcsin(x1_desc)
190+
return dpnp_arcsin(x1_desc, out)
190191

191-
return call_origin(numpy.arcsin, x1, **kwargs)
192+
return call_origin(numpy.arcsin, x1, out=out, **kwargs)
192193

193194

194195
def arcsinh(x1):
@@ -220,7 +221,7 @@ def arcsinh(x1):
220221
return call_origin(numpy.arcsinh, x1, **kwargs)
221222

222223

223-
def arctan(x1):
224+
def arctan(x1, out=None, **kwargs):
224225
"""
225226
Trigonometric inverse tangent, element-wise.
226227
@@ -229,6 +230,7 @@ def arctan(x1):
229230
Limitations
230231
-----------
231232
Input array is supported as :obj:`dpnp.ndarray`.
233+
Keyword arguments ``kwargs`` are currently unsupported.
232234
Input array data types are limited by supported DPNP :ref:`Data types`.
233235
234236
See Also
@@ -249,9 +251,9 @@ def arctan(x1):
249251

250252
x1_desc = dpnp.get_dpnp_descriptor(x1)
251253
if x1_desc:
252-
return dpnp_arctan(x1_desc)
254+
return dpnp_arctan(x1_desc, out)
253255

254-
return call_origin(numpy.arctan, x1, **kwargs)
256+
return call_origin(numpy.arctan, x1, out=out, **kwargs)
255257

256258

257259
def arctanh(x1):
@@ -361,8 +363,6 @@ def arctan2(x1, x2, dtype=None, out=None, where=True, **kwargs):
361363
pass
362364
elif dtype is not None:
363365
pass
364-
elif out is not None:
365-
pass
366366
elif not where:
367367
pass
368368
else:
@@ -974,7 +974,7 @@ def square(x1):
974974
return call_origin(numpy.square, x1, **kwargs)
975975

976976

977-
def tan(x1):
977+
def tan(x1, out=None, **kwargs):
978978
"""
979979
Compute tangent element-wise.
980980
@@ -983,6 +983,7 @@ def tan(x1):
983983
Limitations
984984
-----------
985985
Input array is supported as :obj:`dpnp.ndarray`.
986+
Keyword arguments ``kwargs`` are currently unsupported.
986987
Input array data types are limited by supported DPNP :ref:`Data types`.
987988
988989
Examples
@@ -998,9 +999,9 @@ def tan(x1):
998999

9991000
x1_desc = dpnp.get_dpnp_descriptor(x1)
10001001
if x1_desc:
1001-
return dpnp_tan(x1_desc)
1002+
return dpnp_tan(x1_desc, out)
10021003

1003-
return call_origin(numpy.tan, x1, **kwargs)
1004+
return call_origin(numpy.tan, x1, out=out, **kwargs)
10041005

10051006

10061007
def tanh(x1):

tests/test_umath.py

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,163 @@ def test_invalid_shape(self, shape):
230230

231231
with pytest.raises(ValueError):
232232
dpnp.exp(dp_array, out=dp_out)
233+
234+
235+
class TestArcsin:
236+
237+
def test_arcsin(self):
238+
array_data = numpy.arange(10)
239+
out = numpy.empty(10, dtype=numpy.float64)
240+
241+
# DPNP
242+
dp_array = dpnp.array(array_data, dtype=dpnp.float64)
243+
dp_out = dpnp.array(out, dtype=dpnp.float64)
244+
result = dpnp.arcsin(dp_array, out=dp_out)
245+
246+
# original
247+
np_array = numpy.array(array_data, dtype=numpy.float64)
248+
expected = numpy.arcsin(np_array, out=out)
249+
250+
numpy.testing.assert_array_equal(expected, result)
251+
252+
@pytest.mark.parametrize("dtype",
253+
[numpy.float32, numpy.int64, numpy.int32],
254+
ids=['numpy.float32', 'numpy.int64', 'numpy.int32'])
255+
def test_invalid_dtype(self, dtype):
256+
257+
dp_array = dpnp.arange(10, dtype=dpnp.float64)
258+
dp_out = dpnp.empty(10, dtype=dtype)
259+
260+
with pytest.raises(ValueError):
261+
dpnp.arcsin(dp_array, out=dp_out)
262+
263+
@pytest.mark.parametrize("shape",
264+
[(0,), (15, ), (2,2)],
265+
ids=['(0,)', '(15, )', '(2,2)'])
266+
def test_invalid_shape(self, shape):
267+
268+
dp_array = dpnp.arange(10, dtype=dpnp.float64)
269+
dp_out = dpnp.empty(shape, dtype=dpnp.float64)
270+
271+
with pytest.raises(ValueError):
272+
dpnp.arcsin(dp_array, out=dp_out)
273+
274+
275+
class TestArctan:
276+
277+
def test_arctan(self):
278+
array_data = numpy.arange(10)
279+
out = numpy.empty(10, dtype=numpy.float64)
280+
281+
# DPNP
282+
dp_array = dpnp.array(array_data, dtype=dpnp.float64)
283+
dp_out = dpnp.array(out, dtype=dpnp.float64)
284+
result = dpnp.arctan(dp_array, out=dp_out)
285+
286+
# original
287+
np_array = numpy.array(array_data, dtype=numpy.float64)
288+
expected = numpy.arctan(np_array, out=out)
289+
290+
numpy.testing.assert_array_equal(expected, result)
291+
292+
@pytest.mark.parametrize("dtype",
293+
[numpy.float32, numpy.int64, numpy.int32],
294+
ids=['numpy.float32', 'numpy.int64', 'numpy.int32'])
295+
def test_invalid_dtype(self, dtype):
296+
297+
dp_array = dpnp.arange(10, dtype=dpnp.float64)
298+
dp_out = dpnp.empty(10, dtype=dtype)
299+
300+
with pytest.raises(ValueError):
301+
dpnp.arctan(dp_array, out=dp_out)
302+
303+
@pytest.mark.parametrize("shape",
304+
[(0,), (15, ), (2,2)],
305+
ids=['(0,)', '(15, )', '(2,2)'])
306+
def test_invalid_shape(self, shape):
307+
308+
dp_array = dpnp.arange(10, dtype=dpnp.float64)
309+
dp_out = dpnp.empty(shape, dtype=dpnp.float64)
310+
311+
with pytest.raises(ValueError):
312+
dpnp.arctan(dp_array, out=dp_out)
313+
314+
315+
class TestTan:
316+
317+
def test_tan(self):
318+
array_data = numpy.arange(10)
319+
out = numpy.empty(10, dtype=numpy.float64)
320+
321+
# DPNP
322+
dp_array = dpnp.array(array_data, dtype=dpnp.float64)
323+
dp_out = dpnp.array(out, dtype=dpnp.float64)
324+
result = dpnp.tan(dp_array, out=dp_out)
325+
326+
# original
327+
np_array = numpy.array(array_data, dtype=numpy.float64)
328+
expected = numpy.tan(np_array, out=out)
329+
330+
numpy.testing.assert_array_equal(expected, result)
331+
332+
@pytest.mark.parametrize("dtype",
333+
[numpy.float32, numpy.int64, numpy.int32],
334+
ids=['numpy.float32', 'numpy.int64', 'numpy.int32'])
335+
def test_invalid_dtype(self, dtype):
336+
337+
dp_array = dpnp.arange(10, dtype=dpnp.float64)
338+
dp_out = dpnp.empty(10, dtype=dtype)
339+
340+
with pytest.raises(ValueError):
341+
dpnp.tan(dp_array, out=dp_out)
342+
343+
@pytest.mark.parametrize("shape",
344+
[(0,), (15, ), (2,2)],
345+
ids=['(0,)', '(15, )', '(2,2)'])
346+
def test_invalid_shape(self, shape):
347+
348+
dp_array = dpnp.arange(10, dtype=dpnp.float64)
349+
dp_out = dpnp.empty(shape, dtype=dpnp.float64)
350+
351+
with pytest.raises(ValueError):
352+
dpnp.tan(dp_array, out=dp_out)
353+
354+
355+
class TestArctan2:
356+
357+
def test_arctan2(self):
358+
array_data = numpy.arange(10)
359+
out = numpy.empty(10, dtype=numpy.float64)
360+
361+
# DPNP
362+
dp_array = dpnp.array(array_data, dtype=dpnp.float64)
363+
dp_out = dpnp.array(out, dtype=dpnp.float64)
364+
result = dpnp.arctan2(dp_array, dp_array, out=dp_out)
365+
366+
# original
367+
np_array = numpy.array(array_data, dtype=numpy.float64)
368+
expected = numpy.arctan2(np_array, np_array, out=out)
369+
370+
numpy.testing.assert_array_equal(expected, result)
371+
372+
@pytest.mark.parametrize("dtype",
373+
[numpy.float32, numpy.int64, numpy.int32],
374+
ids=['numpy.float32', 'numpy.int64', 'numpy.int32'])
375+
def test_invalid_dtype(self, dtype):
376+
377+
dp_array = dpnp.arange(10, dtype=dpnp.float64)
378+
dp_out = dpnp.empty(10, dtype=dtype)
379+
380+
with pytest.raises(ValueError):
381+
dpnp.arctan2(dp_array, dp_array, out=dp_out)
382+
383+
@pytest.mark.parametrize("shape",
384+
[(0,), (15, ), (2,2)],
385+
ids=['(0,)', '(15, )', '(2,2)'])
386+
def test_invalid_shape(self, shape):
387+
388+
dp_array = dpnp.arange(10, dtype=dpnp.float64)
389+
dp_out = dpnp.empty(shape, dtype=dpnp.float64)
390+
391+
with pytest.raises(ValueError):
392+
dpnp.arctan2(dp_array, dp_array, out=dp_out)

0 commit comments

Comments
 (0)