Skip to content

Commit f735906

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

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-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

+36-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,41 @@ 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([0, 4, 8])
166+
167+
>>> xpx.create_diagonal(x, xp=xp)
168+
Array([[-2.1, 0. , 0. ],
169+
[ 0. , -1. , 0. ],
170+
[ 0. , 0. , 4.3]], dtype=array_api_strict.float64)
171+
>>> xpx.create_diagonal(x, offset=-2, xp=xp)
172+
Array([[ 0. , 0. , 0. , 0. , 0. ],
173+
[ 0. , 0. , 0. , 0. , 0. ],
174+
[-2.1, 0. , 0. , 0. , 0. ],
175+
[ 0. , -1. , 0. , 0. , 0. ],
176+
[ 0. , 0. , 4.3, 0. , 0. ]], dtype=array_api_strict.float64)
177+
178+
"""
144179
if x.ndim != 1:
145180
err_msg = "`x` must be 1-dimensional."
146181
raise ValueError(err_msg)

0 commit comments

Comments
 (0)