|
6 | 6 | if TYPE_CHECKING:
|
7 | 7 | from ._typing import Array, ModuleType
|
8 | 8 |
|
9 |
| -__all__ = ["atleast_nd", "cov", "expand_dims", "kron"] |
| 9 | +__all__ = ["atleast_nd", "cov", "create_diagonal", "expand_dims", "kron"] |
10 | 10 |
|
11 | 11 |
|
12 | 12 | def atleast_nd(x: Array, /, *, ndim: int, xp: ModuleType) -> Array:
|
@@ -141,6 +141,42 @@ def cov(m: Array, /, *, xp: ModuleType) -> Array:
|
141 | 141 |
|
142 | 142 |
|
143 | 143 | 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 | + """ |
144 | 180 | if x.ndim != 1:
|
145 | 181 | err_msg = "`x` must be 1-dimensional."
|
146 | 182 | raise ValueError(err_msg)
|
|
0 commit comments