|
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,41 @@ 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([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 | + """ |
144 | 179 | if x.ndim != 1:
|
145 | 180 | err_msg = "`x` must be 1-dimensional."
|
146 | 181 | raise ValueError(err_msg)
|
|
0 commit comments