-
Notifications
You must be signed in to change notification settings - Fork 30
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
add unstack, moveaxis, swapaxes #1137
Conversation
View rendered docs @ https://intelpython.github.io/dpctl/pulls/1137/index.html |
default value is axis=0. | ||
|
||
Returns: | ||
out (usm_narray): A tuple of arrays. |
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.
out (usm_narray): A tuple of arrays. | |
Tuple[usm_narray,...]: A tuple of arrays. |
ind = list(range(0, X.ndim)) | ||
ind[axis1] = axis2 | ||
ind[axis2] = axis1 | ||
return dpt.permute_dims(X, tuple(ind)) |
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.
Instead of creating list
to be able to mutate it only to then copy it into tuple
, use generator to create the right tuple directly. Here is an example:
In [1]: def permuted_range(n, a1, a2):
...: for i in range(n):
...: if i == a1:
...: yield a2
...: elif i == a2:
...: yield a1
...: else:
...: yield i
...:
In [2]: tuple(permuted_range(5, 1, 3))
Out[2]: (0, 3, 2, 1, 4)
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.
Actually, the way it is currently implement is faster:
In [13]: def way1(n, a1, a2):
...: ind = list(range(n))
...: ind[a1] = a2
...: ind[a2] = a1
...: return tuple(ind)
...:
In [14]: def permuted_range(n, a1, a2):
...: for i in range(n):
...: if i == a1:
...: yield a2
...: elif i == a2:
...: yield a1
...: else:
...: yield i
...:
In [15]: def way2(n, a1, a2):
...: return tuple(permuted_range(n, a1, a2))
...:
In [16]: way1(5, 1, 3) == way2(5, 1, 3)
Out[16]: True
In [17]: %timeit way1(5, 1, 3)
434 ns ± 23 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
In [18]: %timeit way2(5, 1, 3)
865 ns ± 98 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
in the half-open interval `[-N, N)`. | ||
|
||
Returns: | ||
out (usm_narray): Array with moved axes. |
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.
out (usm_narray): Array with moved axes. | |
usm_narray: Array with moved axes. |
Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_23 ran successfully. |
a valid `axis` must reside in the half-open interval `[-N, N)`. | ||
|
||
Returns: | ||
out (usm_narray): Swapped array. |
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.
out (usm_narray): Swapped array. | |
usm_narray: Array with swapped axes. |
Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_24 ran successfully. |
Deleted rendered PR docs from intelpython.github.com/dpctl, latest should be updated shortly. 🤞 |
Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_24 ran successfully. |
In this task, unstack, moveaxis, and swapaxes functions are added.
This PR closes #1121