-
Notifications
You must be signed in to change notification settings - Fork 23
implement dpnp.piecewise
#2550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
implement dpnp.piecewise
#2550
Conversation
View rendered docs @ https://intelpython.github.io/dpnp/pull/2550/index.html |
Array API standard conformance tests for dpnp=0.19.0dev3=py313h509198e_25 ran successfully. |
if isinstance(condlist, dpnp.ndarray) and condlist.ndim in [0, 1]: | ||
condlist = [condlist] | ||
elif dpnp.isscalar(condlist) or ( | ||
dpnp.isscalar(condlist[0]) and x.ndim != 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no check that condlist
is exactly a list. Do we need that or it is assuming to accept any sequence?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In practice, condlist
can be a list, tuple or ndarray in both NumPy and CuPy. And dpnp
follows the same practice.
import numpy
a = numpy.linspace(-2.5, 2.5, 6)
numpy.piecewise(a, [a < 0, a >= 0], [-1, 1]) # condlist is list
# array([-1., -1., -1., 1., 1., 1.])
numpy.piecewise(a, (a < 0, a >= 0), [-1, 1]) # condlist is tuple
# array([-1., -1., -1., 1., 1., 1.])
numpy.piecewise(a, numpy.array([a < 0, a >= 0]), [-1, 1]) # condlist is array
# array([-1., -1., -1., 1., 1., 1.])
import cupy
x = cupy.array(a)
cupy.piecewise(x, [x < 0, x >= 0], [-1, 1]) # condlist is list
# array([-1., -1., -1., 1., 1., 1.])
cupy.piecewise(x, (x < 0, x >= 0), [-1, 1]) # condlist is tuple
# array([-1., -1., -1., 1., 1., 1.])
cupy.piecewise(x, cupy.array([x < 0, x >= 0]), [-1, 1]) # condlist is array
# array([-1., -1., -1., 1., 1., 1.])
import dpnp
ia = dpnp.array(a)
dpnp.piecewise(ia, [ia < 0, ia >= 0], [-1, 1]) # condlist is list
# array([-1., -1., -1., 1., 1., 1.])
dpnp.piecewise(ia, (ia < 0, ia >= 0), [-1, 1]) # condlist is tuple
# array([-1., -1., -1., 1., 1., 1.])
dpnp.piecewise(ia, dpnp.array([ia < 0, ia >= 0]), [-1, 1]) # condlist is array
# array([-1., -1., -1., 1., 1., 1.])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I mean condlist
might not be a subscriptable object and so condlist[0]
would raise TypeError
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No more comments from my side
LGTM
Thank you @vtavana
condlen = len(condlist) | ||
try: | ||
if isinstance(funclist, str): | ||
raise TypeError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to add exception text
x.shape, condlist, usm_type=x.usm_type, sycl_queue=x.sycl_queue | ||
) | ||
] | ||
elif not isinstance(condlist[0], (dpnp.ndarray)): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we check for usm_ndarray
here?
In this PR,
dpnp.piecewise
is implemented.