-
Notifications
You must be signed in to change notification settings - Fork 19
ENH: add diag_indices, tril_indices, triu_indices
#692
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: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -28,13 +28,16 @@ | |||||||||||||
| "broadcast_shapes", | ||||||||||||||
| "cov", | ||||||||||||||
| "create_diagonal", | ||||||||||||||
| "diag_indices", | ||||||||||||||
| "expand_dims", | ||||||||||||||
| "kron", | ||||||||||||||
| "nunique", | ||||||||||||||
| "pad", | ||||||||||||||
| "searchsorted", | ||||||||||||||
| "setdiff1d", | ||||||||||||||
| "sinc", | ||||||||||||||
| "tril_indices", | ||||||||||||||
| "triu_indices", | ||||||||||||||
| ] | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
|
|
@@ -346,6 +349,59 @@ def create_diagonal( | |||||||||||||
| return xp.reshape(diag, (*batch_dims, n, n)) | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def diag_indices( | ||||||||||||||
| n: int, /, *, ndim: int = 2, device: Device | None = None, xp: ModuleType | ||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| ) -> tuple[Array, ...]: # numpydoc ignore=PR01,RT01 | ||||||||||||||
| """See docstring in array_api_extra._delegation.""" | ||||||||||||||
| idx = xp.arange(n, device=device) | ||||||||||||||
| return (idx,) * ndim | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def _tri_indices( | ||||||||||||||
| n: int, | ||||||||||||||
| *, | ||||||||||||||
| offset: int, | ||||||||||||||
| m: int | None, | ||||||||||||||
| upper: bool, | ||||||||||||||
| device: Device | None, | ||||||||||||||
| xp: ModuleType, | ||||||||||||||
| ) -> tuple[Array, Array]: # numpydoc ignore=PR01,RT01 | ||||||||||||||
| """Shared implementation for `tril_indices` and `triu_indices`.""" | ||||||||||||||
| cols = n if m is None else m | ||||||||||||||
| rows = xp.arange(n, device=device)[:, None] | ||||||||||||||
| cols_a = xp.arange(cols, device=device)[None, :] | ||||||||||||||
|
Comment on lines
+371
to
+372
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| delta = cols_a - rows | ||||||||||||||
| mask = delta >= offset if upper else delta <= offset | ||||||||||||||
| r, c = xp.nonzero(mask) | ||||||||||||||
| return (r, c) | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def tril_indices( | ||||||||||||||
| n: int, | ||||||||||||||
| /, | ||||||||||||||
| *, | ||||||||||||||
| offset: int = 0, | ||||||||||||||
| m: int | None = None, | ||||||||||||||
| device: Device | None = None, | ||||||||||||||
|
Comment on lines
+383
to
+385
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| xp: ModuleType, | ||||||||||||||
| ) -> tuple[Array, Array]: # numpydoc ignore=PR01,RT01 | ||||||||||||||
| """See docstring in array_api_extra._delegation.""" | ||||||||||||||
| return _tri_indices(n, offset=offset, m=m, upper=False, device=device, xp=xp) | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def triu_indices( | ||||||||||||||
| n: int, | ||||||||||||||
| /, | ||||||||||||||
| *, | ||||||||||||||
| offset: int = 0, | ||||||||||||||
| m: int | None = None, | ||||||||||||||
| device: Device | None = None, | ||||||||||||||
|
Comment on lines
+396
to
+398
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| xp: ModuleType, | ||||||||||||||
| ) -> tuple[Array, Array]: # numpydoc ignore=PR01,RT01 | ||||||||||||||
| """See docstring in array_api_extra._delegation.""" | ||||||||||||||
| return _tri_indices(n, offset=offset, m=m, upper=True, device=device, xp=xp) | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def default_dtype( | ||||||||||||||
| xp: ModuleType, | ||||||||||||||
| kind: Literal[ | ||||||||||||||
|
|
||||||||||||||
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.