Skip to content

Commit d98abe4

Browse files
authored
add out to sin function (#727)
1 parent e750d48 commit d98abe4

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
lines changed

dpnp/dpnp_algo/dpnp_algo_trigonometric.pyx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,32 @@ cpdef dparray dpnp_radians(dparray x1):
142142
return call_fptr_1in_1out(DPNP_FN_RADIANS, x1, x1.shape)
143143

144144

145-
cpdef dparray dpnp_sin(dparray x1):
146-
return call_fptr_1in_1out(DPNP_FN_SIN, x1, x1.shape)
145+
cpdef dparray dpnp_sin(dparray x1, dparray out=None):
146+
147+
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(x1.dtype)
148+
149+
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_SIN, param1_type, param1_type)
150+
151+
result_type = dpnp_DPNPFuncType_to_dtype(< size_t > kernel_data.return_type)
152+
153+
shape_result = x1.shape
154+
155+
cdef dparray result
156+
157+
if out is not None:
158+
if out.dtype != result_type:
159+
checker_throw_value_error('sin', 'out.dtype', out.dtype, result_type)
160+
if out.shape != shape_result:
161+
checker_throw_value_error('sin', 'out.shape', out.shape, shape_result)
162+
result = out
163+
else:
164+
result = dparray(shape_result, dtype=result_type)
165+
166+
cdef fptr_1in_1out_t func = <fptr_1in_1out_t > kernel_data.ptr
167+
168+
func(x1.get_data(), result.get_data(), x1.size)
169+
170+
return result
147171

148172

149173
cpdef dparray dpnp_sinh(dparray x1):

dpnp/dpnp_iface_trigonometric.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -881,15 +881,18 @@ def radians(x1):
881881
return dpnp_radians(x1)
882882

883883

884-
def sin(x1):
884+
def sin(x1, out=None, **kwargs):
885885
"""
886886
Trigonometric sine, element-wise.
887887
888888
For full documentation refer to :obj:`numpy.sin`.
889889
890890
Limitations
891891
-----------
892-
Input array is supported as :obj:`dpnp.ndarray`.
892+
Parameters ``x1`` is supported as :obj:`dpnp.ndarray`.
893+
Parameter ``out`` is supported as default value ``None``.
894+
Keyword arguments ``kwargs`` are currently unsupported.
895+
Otherwise the functions will be executed sequentially on CPU.
893896
Input array data types are limited by supported DPNP :ref:`Data types`.
894897
895898
See Also
@@ -908,14 +911,15 @@ def sin(x1):
908911
[0.0, 1.0, 1.2246467991473532e-16]
909912
910913
"""
914+
if not use_origin_backend(x1) and not kwargs:
915+
if not isinstance(x1, dparray):
916+
pass
917+
elif out is not None and not isinstance(out, dparray):
918+
pass
919+
else:
920+
return dpnp_sin(x1, out=out)
911921

912-
if (use_origin_backend(x1)):
913-
return numpy.sin(x1)
914-
915-
if not isinstance(x1, dparray):
916-
raise TypeError(f"DPNP sin(): Unsupported x1={type(x1)}")
917-
918-
return dpnp_sin(x1)
922+
return call_origin(numpy.sin, x1, out=out, **kwargs)
919923

920924

921925
def sinh(x1):

tests/test_umath.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,19 @@ def test_umaths(test_cases):
7171
result = getattr(dpnp, umath)(*iargs)
7272

7373
numpy.testing.assert_allclose(result, expected, rtol=1e-6)
74+
75+
76+
def test_sin():
77+
array_data = numpy.arange(10)
78+
out = numpy.empty(10, dtype=numpy.float64)
79+
80+
# DPNP
81+
dp_array = dpnp.array(array_data, dtype=dpnp.float64)
82+
dp_out = dpnp.array(out, dtype=dpnp.float64)
83+
result = dpnp.sin(dp_array, out=dp_out)
84+
85+
# original
86+
np_array = numpy.array(array_data, dtype=numpy.float64)
87+
expected = numpy.sin(np_array, out=out)
88+
89+
numpy.testing.assert_array_equal(expected, result)

0 commit comments

Comments
 (0)