Skip to content

Commit be3664d

Browse files
committed
DOC: create_diagonal: add docs
1 parent 0a53e57 commit be3664d

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

docs/api-reference.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
99
atleast_nd
1010
cov
11+
create_diagonal
1112
expand_dims
1213
kron
1314
```

src/array_api_extra/_funcs.py

+37-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
if TYPE_CHECKING:
77
from ._typing import Array, ModuleType
88

9-
__all__ = ["atleast_nd", "cov", "expand_dims", "kron"]
9+
__all__ = ["atleast_nd", "cov", "create_diagonal", "expand_dims", "kron"]
1010

1111

1212
def atleast_nd(x: Array, /, *, ndim: int, xp: ModuleType) -> Array:
@@ -141,6 +141,42 @@ def cov(m: Array, /, *, xp: ModuleType) -> Array:
141141

142142

143143
def create_diagonal(x: Array, /, *, offset: int = 0, xp: ModuleType) -> Array:
144+
"""
145+
Construct a diagonal array.
146+
147+
Parameters
148+
----------
149+
x : array
150+
A 1-D array
151+
offset : int, optional
152+
Offset from the leading diagonal (default is ``0``).
153+
Use positive ints for diagonals above the leading diagonal,
154+
and negative ints for diagonals below the leading diagonal.
155+
156+
Returns
157+
-------
158+
res : array
159+
A 2-D array with `x` on the diagonal (offset by `offset`).
160+
161+
Examples
162+
--------
163+
>>> import array_api_strict as xp
164+
>>> import array_api_extra as xpx
165+
>>> x = xp.asarray([2, 4, 8])
166+
167+
>>> xpx.create_diagonal(x, xp=xp)
168+
Array([[2, 0, 0],
169+
[0, 4, 0],
170+
[0, 0, 8]], dtype=array_api_strict.int64)
171+
172+
>>> xpx.create_diagonal(x, offset=-2, xp=xp)
173+
Array([[0, 0, 0, 0, 0],
174+
[0, 0, 0, 0, 0],
175+
[2, 0, 0, 0, 0],
176+
[0, 4, 0, 0, 0],
177+
[0, 0, 8, 0, 0]], dtype=array_api_strict.int64)
178+
179+
"""
144180
if x.ndim != 1:
145181
err_msg = "`x` must be 1-dimensional."
146182
raise ValueError(err_msg)

0 commit comments

Comments
 (0)