xbarray 0.0.1a13__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- array_api_typing/__init__.py +9 -0
- array_api_typing/typing_2024_12/__init__.py +12 -0
- array_api_typing/typing_2024_12/_api_constant.py +32 -0
- array_api_typing/typing_2024_12/_api_fft_typing.py +717 -0
- array_api_typing/typing_2024_12/_api_linalg_typing.py +897 -0
- array_api_typing/typing_2024_12/_api_return_typing.py +103 -0
- array_api_typing/typing_2024_12/_api_typing.py +5855 -0
- array_api_typing/typing_2024_12/_array_typing.py +1265 -0
- array_api_typing/typing_compat/__init__.py +12 -0
- array_api_typing/typing_compat/_api_typing.py +27 -0
- array_api_typing/typing_compat/_array_typing.py +36 -0
- array_api_typing/typing_extra/__init__.py +12 -0
- array_api_typing/typing_extra/_api_typing.py +651 -0
- array_api_typing/typing_extra/_at.py +87 -0
- xbarray/__init__.py +1 -0
- xbarray/backends/_cls_base.py +9 -0
- xbarray/backends/_implementations/_common/implementations.py +87 -0
- xbarray/backends/_implementations/jax/__init__.py +33 -0
- xbarray/backends/_implementations/jax/_extra.py +127 -0
- xbarray/backends/_implementations/jax/_typing.py +15 -0
- xbarray/backends/_implementations/jax/random.py +115 -0
- xbarray/backends/_implementations/numpy/__init__.py +25 -0
- xbarray/backends/_implementations/numpy/_extra.py +98 -0
- xbarray/backends/_implementations/numpy/_typing.py +14 -0
- xbarray/backends/_implementations/numpy/random.py +105 -0
- xbarray/backends/_implementations/pytorch/__init__.py +26 -0
- xbarray/backends/_implementations/pytorch/_extra.py +135 -0
- xbarray/backends/_implementations/pytorch/_typing.py +13 -0
- xbarray/backends/_implementations/pytorch/random.py +101 -0
- xbarray/backends/base.py +218 -0
- xbarray/backends/jax.py +19 -0
- xbarray/backends/numpy.py +19 -0
- xbarray/backends/pytorch.py +22 -0
- xbarray/jax.py +4 -0
- xbarray/numpy.py +4 -0
- xbarray/pytorch.py +4 -0
- xbarray/transformations/pointcloud/__init__.py +1 -0
- xbarray/transformations/pointcloud/base.py +449 -0
- xbarray/transformations/pointcloud/jax.py +24 -0
- xbarray/transformations/pointcloud/numpy.py +23 -0
- xbarray/transformations/pointcloud/pytorch.py +23 -0
- xbarray/transformations/rotation_conversions/__init__.py +1 -0
- xbarray/transformations/rotation_conversions/base.py +713 -0
- xbarray/transformations/rotation_conversions/jax.py +41 -0
- xbarray/transformations/rotation_conversions/numpy.py +41 -0
- xbarray/transformations/rotation_conversions/pytorch.py +41 -0
- xbarray-0.0.1a13.dist-info/METADATA +20 -0
- xbarray-0.0.1a13.dist-info/RECORD +51 -0
- xbarray-0.0.1a13.dist-info/WHEEL +5 -0
- xbarray-0.0.1a13.dist-info/licenses/LICENSE +21 -0
- xbarray-0.0.1a13.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,897 @@
|
|
|
1
|
+
from typing import Protocol, TypeVar, Optional, Any, Tuple, Union, Type, TypedDict, List, Literal, Sequence
|
|
2
|
+
from abc import abstractmethod
|
|
3
|
+
from ._array_typing import *
|
|
4
|
+
from ._api_return_typing import *
|
|
5
|
+
|
|
6
|
+
_NAMESPACE_ARRAY = TypeVar("_NAMESPACE_ARRAY", bound=Array)
|
|
7
|
+
_NAMESPACE_DEVICE = TypeVar("_NAMESPACE_DEVICE", bound=Device)
|
|
8
|
+
_NAMESPACE_DTYPE = TypeVar("_NAMESPACE_DTYPE", bound=DType)
|
|
9
|
+
class ArrayAPILinalgNamespace(Protocol[_NAMESPACE_ARRAY, _NAMESPACE_DEVICE, _NAMESPACE_DTYPE]):
|
|
10
|
+
"""
|
|
11
|
+
Linalg library API Stub
|
|
12
|
+
https://github.com/data-apis/array-api/blob/main/src/array_api_stubs/_2024_12/linalg.py
|
|
13
|
+
"""
|
|
14
|
+
@abstractmethod
|
|
15
|
+
def cholesky(
|
|
16
|
+
self,
|
|
17
|
+
x: _NAMESPACE_ARRAY, /, *, upper: bool = False
|
|
18
|
+
) -> _NAMESPACE_ARRAY:
|
|
19
|
+
r"""
|
|
20
|
+
Returns the lower (upper) Cholesky decomposition of a complex Hermitian or real symmetric positive-definite matrix ``x``.
|
|
21
|
+
|
|
22
|
+
If ``x`` is real-valued, let :math:`\mathbb{K}` be the set of real numbers :math:`\mathbb{R}`, and, if ``x`` is complex-valued, let :math:`\mathbb{K}` be the set of complex numbers :math:`\mathbb{C}`.
|
|
23
|
+
|
|
24
|
+
The lower **Cholesky decomposition** of a complex Hermitian or real symmetric positive-definite matrix :math:`x \in\ \mathbb{K}^{n \times n}` is defined as
|
|
25
|
+
|
|
26
|
+
.. math::
|
|
27
|
+
x = LL^{H} \qquad \text{L $\in\ \mathbb{K}^{n \times n}$}
|
|
28
|
+
|
|
29
|
+
where :math:`L` is a lower triangular matrix and :math:`L^{H}` is the conjugate transpose when :math:`L` is complex-valued and the transpose when :math:`L` is real-valued.
|
|
30
|
+
|
|
31
|
+
The upper Cholesky decomposition is defined similarly
|
|
32
|
+
|
|
33
|
+
.. math::
|
|
34
|
+
x = U^{H}U \qquad \text{U $\in\ \mathbb{K}^{n \times n}$}
|
|
35
|
+
|
|
36
|
+
where :math:`U` is an upper triangular matrix.
|
|
37
|
+
|
|
38
|
+
When ``x`` is a stack of matrices, the function must compute the Cholesky decomposition for each matrix in the stack.
|
|
39
|
+
|
|
40
|
+
.. note::
|
|
41
|
+
Whether an array library explicitly checks whether an input array is Hermitian or a symmetric positive-definite matrix (or a stack of matrices) is implementation-defined.
|
|
42
|
+
|
|
43
|
+
Parameters
|
|
44
|
+
----------
|
|
45
|
+
x: _NAMESPACE_ARRAY
|
|
46
|
+
input array having shape ``(..., M, M)`` and whose innermost two dimensions form square complex Hermitian or real symmetric positive-definite matrices. Should have a floating-point data type.
|
|
47
|
+
upper: bool
|
|
48
|
+
If ``True``, the result must be the upper-triangular Cholesky factor :math:`U`. If ``False``, the result must be the lower-triangular Cholesky factor :math:`L`. Default: ``False``.
|
|
49
|
+
|
|
50
|
+
Returns
|
|
51
|
+
-------
|
|
52
|
+
out: _NAMESPACE_ARRAY
|
|
53
|
+
an array containing the Cholesky factors for each square matrix. If ``upper`` is ``False``, the returned array must contain lower-triangular matrices; otherwise, the returned array must contain upper-triangular matrices. The returned array must have a floating-point data type determined by :ref:`type-promotion` and must have the same shape as ``x``.
|
|
54
|
+
|
|
55
|
+
Notes
|
|
56
|
+
-----
|
|
57
|
+
|
|
58
|
+
.. versionchanged:: 2022.12
|
|
59
|
+
Added complex data type support.
|
|
60
|
+
"""
|
|
61
|
+
|
|
62
|
+
@abstractmethod
|
|
63
|
+
def cross(
|
|
64
|
+
self,
|
|
65
|
+
x1: _NAMESPACE_ARRAY, x2: _NAMESPACE_ARRAY, /, *, axis: int = -1
|
|
66
|
+
) -> _NAMESPACE_ARRAY:
|
|
67
|
+
"""
|
|
68
|
+
Returns the cross product of 3-element vectors.
|
|
69
|
+
|
|
70
|
+
If ``x1`` and/or ``x2`` are multi-dimensional arrays (i.e., the broadcasted result has a rank greater than ``1``), then the cross-product of each pair of corresponding 3-element vectors is independently computed.
|
|
71
|
+
|
|
72
|
+
Parameters
|
|
73
|
+
----------
|
|
74
|
+
x1: _NAMESPACE_ARRAY
|
|
75
|
+
first input array. Must have a numeric data type. The size of the axis over which the cross product is to be computed must be equal to 3.
|
|
76
|
+
x2: _NAMESPACE_ARRAY
|
|
77
|
+
second input array. Must be broadcast compatible with ``x1`` along all axes other than the axis along which the cross-product is computed (see :ref:`broadcasting`). The size of the axis over which the cross product is to be computed must be equal to 3. Must have a numeric data type.
|
|
78
|
+
|
|
79
|
+
.. note::
|
|
80
|
+
The compute axis (dimension) must not be broadcasted.
|
|
81
|
+
|
|
82
|
+
axis: int
|
|
83
|
+
the axis (dimension) of ``x1`` and ``x2`` containing the vectors for which to compute the cross product. Should be an integer on the interval ``[-N, -1]``, where ``N`` is ``min(x1.ndim, x2.ndim)``. The function must determine the axis along which to compute the cross product by counting backward from the last dimension (where ``-1`` refers to the last dimension). By default, the function must compute the cross product over the last axis. Default: ``-1``.
|
|
84
|
+
|
|
85
|
+
Returns
|
|
86
|
+
-------
|
|
87
|
+
out: _NAMESPACE_ARRAY
|
|
88
|
+
an array containing the cross products. The returned array must have a data type determined by :ref:`type-promotion`.
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
Notes
|
|
92
|
+
-----
|
|
93
|
+
|
|
94
|
+
**Raises**
|
|
95
|
+
|
|
96
|
+
- if the size of the axis over which to compute the cross product is not equal to ``3`` (before broadcasting) for both ``x1`` and ``x2``.
|
|
97
|
+
|
|
98
|
+
.. versionchanged:: 2022.12
|
|
99
|
+
Added support for broadcasting.
|
|
100
|
+
|
|
101
|
+
.. versionchanged:: 2022.12
|
|
102
|
+
Added complex data type support.
|
|
103
|
+
|
|
104
|
+
.. versionchanged:: 2023.12
|
|
105
|
+
Restricted broadcasting to only non-compute axes and required that ``axis`` be a negative integer.
|
|
106
|
+
"""
|
|
107
|
+
|
|
108
|
+
@abstractmethod
|
|
109
|
+
def det(
|
|
110
|
+
self,
|
|
111
|
+
x: _NAMESPACE_ARRAY, /
|
|
112
|
+
) -> _NAMESPACE_ARRAY:
|
|
113
|
+
"""
|
|
114
|
+
Returns the determinant of a square matrix (or a stack of square matrices) ``x``.
|
|
115
|
+
|
|
116
|
+
Parameters
|
|
117
|
+
----------
|
|
118
|
+
x: _NAMESPACE_ARRAY
|
|
119
|
+
input array having shape ``(..., M, M)`` and whose innermost two dimensions form square matrices. Should have a floating-point data type.
|
|
120
|
+
|
|
121
|
+
Returns
|
|
122
|
+
-------
|
|
123
|
+
out: _NAMESPACE_ARRAY
|
|
124
|
+
if ``x`` is a two-dimensional array, a zero-dimensional array containing the determinant; otherwise, a non-zero dimensional array containing the determinant for each square matrix. The returned array must have the same data type as ``x``.
|
|
125
|
+
|
|
126
|
+
Notes
|
|
127
|
+
-----
|
|
128
|
+
|
|
129
|
+
.. versionchanged:: 2022.12
|
|
130
|
+
Added complex data type support.
|
|
131
|
+
"""
|
|
132
|
+
|
|
133
|
+
@abstractmethod
|
|
134
|
+
def diagonal(
|
|
135
|
+
self,
|
|
136
|
+
x: _NAMESPACE_ARRAY, /, *, offset: int = 0
|
|
137
|
+
) -> _NAMESPACE_ARRAY:
|
|
138
|
+
"""
|
|
139
|
+
Returns the specified diagonals of a matrix (or a stack of matrices) ``x``.
|
|
140
|
+
|
|
141
|
+
Parameters
|
|
142
|
+
----------
|
|
143
|
+
x: _NAMESPACE_ARRAY
|
|
144
|
+
input array having shape ``(..., M, N)`` and whose innermost two dimensions form ``MxN`` matrices.
|
|
145
|
+
offset: int
|
|
146
|
+
offset specifying the off-diagonal relative to the main diagonal.
|
|
147
|
+
|
|
148
|
+
- ``offset = 0``: the main diagonal.
|
|
149
|
+
- ``offset > 0``: off-diagonal above the main diagonal.
|
|
150
|
+
- ``offset < 0``: off-diagonal below the main diagonal.
|
|
151
|
+
|
|
152
|
+
Default: `0`.
|
|
153
|
+
|
|
154
|
+
Returns
|
|
155
|
+
-------
|
|
156
|
+
out: _NAMESPACE_ARRAY
|
|
157
|
+
an array containing the diagonals and whose shape is determined by removing the last two dimensions and appending a dimension equal to the size of the resulting diagonals. The returned array must have the same data type as ``x``.
|
|
158
|
+
"""
|
|
159
|
+
|
|
160
|
+
@abstractmethod
|
|
161
|
+
def eigh(
|
|
162
|
+
self,
|
|
163
|
+
x: _NAMESPACE_ARRAY, /
|
|
164
|
+
) -> Tuple[_NAMESPACE_ARRAY, _NAMESPACE_ARRAY]:
|
|
165
|
+
r"""
|
|
166
|
+
Returns an eigenvalue decomposition of a complex Hermitian or real symmetric matrix (or a stack of matrices) ``x``.
|
|
167
|
+
|
|
168
|
+
If ``x`` is real-valued, let :math:`\mathbb{K}` be the set of real numbers :math:`\mathbb{R}`, and, if ``x`` is complex-valued, let :math:`\mathbb{K}` be the set of complex numbers :math:`\mathbb{C}`.
|
|
169
|
+
|
|
170
|
+
The **eigenvalue decomposition** of a complex Hermitian or real symmetric matrix :math:`x \in\ \mathbb{K}^{n \times n}` is defined as
|
|
171
|
+
|
|
172
|
+
.. math::
|
|
173
|
+
x = Q \Lambda Q^H
|
|
174
|
+
|
|
175
|
+
with :math:`Q \in \mathbb{K}^{n \times n}` and :math:`\Lambda \in \mathbb{R}^n` and where :math:`Q^H` is the conjugate transpose when :math:`Q` is complex and the transpose when :math:`Q` is real-valued and :math:`\Lambda` is a diagonal matrix whose diagonal elements are the corresponding eigenvalues. When ``x`` is real-valued, :math:`Q` is orthogonal, and, when ``x`` is complex, :math:`Q` is unitary.
|
|
176
|
+
|
|
177
|
+
.. note::
|
|
178
|
+
The eigenvalues of a complex Hermitian or real symmetric matrix are always real.
|
|
179
|
+
|
|
180
|
+
.. warning::
|
|
181
|
+
The eigenvectors of a symmetric matrix are not unique and are not continuous with respect to ``x``. Because eigenvectors are not unique, different hardware and software may compute different eigenvectors.
|
|
182
|
+
|
|
183
|
+
Non-uniqueness stems from the fact that multiplying an eigenvector by :math:`-1` when ``x`` is real-valued and by :math:`e^{\phi j}` (:math:`\phi \in \mathbb{R}`) when ``x`` is complex produces another set of valid eigenvectors.
|
|
184
|
+
|
|
185
|
+
.. note::
|
|
186
|
+
Whether an array library explicitly checks whether an input array is Hermitian or a symmetric matrix (or a stack of matrices) is implementation-defined.
|
|
187
|
+
|
|
188
|
+
.. note::
|
|
189
|
+
The function ``eig`` will be added in a future version of the specification.
|
|
190
|
+
|
|
191
|
+
Parameters
|
|
192
|
+
----------
|
|
193
|
+
x: _NAMESPACE_ARRAY
|
|
194
|
+
input array having shape ``(..., M, M)`` and whose innermost two dimensions form square matrices. Should have a floating-point data type.
|
|
195
|
+
|
|
196
|
+
Returns
|
|
197
|
+
-------
|
|
198
|
+
out: Tuple[array, array]
|
|
199
|
+
a namedtuple (``eigenvalues``, ``eigenvectors``) whose
|
|
200
|
+
|
|
201
|
+
- first element must have the field name ``eigenvalues`` (corresponding to :math:`\operatorname{diag}\Lambda` above) and must be an array consisting of computed eigenvalues. The array containing the eigenvalues must have shape ``(..., M)`` and must have a real-valued floating-point data type whose precision matches the precision of ``x`` (e.g., if ``x`` is ``complex128``, then ``eigenvalues`` must be ``float64``).
|
|
202
|
+
- second element have have the field name ``eigenvectors`` (corresponding to :math:`Q` above) and must be an array where the columns of the inner most matrices contain the computed eigenvectors. These matrices must be orthogonal. The array containing the eigenvectors must have shape ``(..., M, M)`` and must have the same data type as ``x``.
|
|
203
|
+
|
|
204
|
+
Notes
|
|
205
|
+
-----
|
|
206
|
+
|
|
207
|
+
.. note::
|
|
208
|
+
Eigenvalue sort order is left unspecified and is thus implementation-dependent.
|
|
209
|
+
|
|
210
|
+
.. versionchanged:: 2022.12
|
|
211
|
+
Added complex data type support.
|
|
212
|
+
"""
|
|
213
|
+
|
|
214
|
+
@abstractmethod
|
|
215
|
+
def eigvalsh(
|
|
216
|
+
self,
|
|
217
|
+
x: _NAMESPACE_ARRAY, /
|
|
218
|
+
) -> _NAMESPACE_ARRAY:
|
|
219
|
+
r"""
|
|
220
|
+
Returns the eigenvalues of a complex Hermitian or real symmetric matrix (or a stack of matrices) ``x``.
|
|
221
|
+
|
|
222
|
+
If ``x`` is real-valued, let :math:`\mathbb{K}` be the set of real numbers :math:`\mathbb{R}`, and, if ``x`` is complex-valued, let :math:`\mathbb{K}` be the set of complex numbers :math:`\mathbb{C}`.
|
|
223
|
+
|
|
224
|
+
The **eigenvalues** of a complex Hermitian or real symmetric matrix :math:`x \in\ \mathbb{K}^{n \times n}` are defined as the roots (counted with multiplicity) of the polynomial :math:`p` of degree :math:`n` given by
|
|
225
|
+
|
|
226
|
+
.. math::
|
|
227
|
+
p(\lambda) = \operatorname{det}(x - \lambda I_n)
|
|
228
|
+
|
|
229
|
+
where :math:`\lambda \in \mathbb{R}` and where :math:`I_n` is the *n*-dimensional identity matrix.
|
|
230
|
+
|
|
231
|
+
.. note:;
|
|
232
|
+
The eigenvalues of a complex Hermitian or real symmetric matrix are always real.
|
|
233
|
+
|
|
234
|
+
.. note::
|
|
235
|
+
Whether an array library explicitly checks whether an input array is Hermitian or a symmetric matrix (or a stack of matrices) is implementation-defined.
|
|
236
|
+
|
|
237
|
+
.. note::
|
|
238
|
+
The function ``eigvals`` will be added in a future version of the specification.
|
|
239
|
+
|
|
240
|
+
Parameters
|
|
241
|
+
----------
|
|
242
|
+
x: _NAMESPACE_ARRAY
|
|
243
|
+
input array having shape ``(..., M, M)`` and whose innermost two dimensions form square matrices. Should have a floating-point data type.
|
|
244
|
+
|
|
245
|
+
Returns
|
|
246
|
+
-------
|
|
247
|
+
out: _NAMESPACE_ARRAY
|
|
248
|
+
an array containing the computed eigenvalues. The returned array must have shape ``(..., M)`` and have a real-valued floating-point data type whose precision matches the precision of ``x`` (e.g., if ``x`` is ``complex128``, then must have a ``float64`` data type).
|
|
249
|
+
|
|
250
|
+
Notes
|
|
251
|
+
-----
|
|
252
|
+
|
|
253
|
+
.. note::
|
|
254
|
+
Eigenvalue sort order is left unspecified and is thus implementation-dependent.
|
|
255
|
+
|
|
256
|
+
.. versionchanged:: 2022.12
|
|
257
|
+
Added complex data type support.
|
|
258
|
+
"""
|
|
259
|
+
|
|
260
|
+
@abstractmethod
|
|
261
|
+
def inv(
|
|
262
|
+
self,
|
|
263
|
+
x: _NAMESPACE_ARRAY, /
|
|
264
|
+
) -> _NAMESPACE_ARRAY:
|
|
265
|
+
r"""
|
|
266
|
+
Returns the multiplicative inverse of a square matrix (or a stack of square matrices) ``x``.
|
|
267
|
+
|
|
268
|
+
If ``x`` is real-valued, let :math:`\mathbb{K}` be the set of real numbers :math:`\mathbb{R}`, and, if ``x`` is complex-valued, let :math:`\mathbb{K}` be the set of complex numbers :math:`\mathbb{C}`.
|
|
269
|
+
|
|
270
|
+
The **inverse matrix** :math:`x^{-1} \in\ \mathbb{K}^{n \times n}` of a square matrix :math:`x \in\ \mathbb{K}^{n \times n}` is defined as
|
|
271
|
+
|
|
272
|
+
.. math::
|
|
273
|
+
x^{-1}x = xx^{-1} = I_n
|
|
274
|
+
|
|
275
|
+
where :math:`I_n` is the *n*-dimensional identity matrix.
|
|
276
|
+
|
|
277
|
+
The inverse matrix exists if and only if ``x`` is invertible. When ``x`` is invertible, the inverse is unique.
|
|
278
|
+
|
|
279
|
+
When ``x`` is a stack of matrices, the function must compute the inverse for each matrix in the stack.
|
|
280
|
+
|
|
281
|
+
Parameters
|
|
282
|
+
----------
|
|
283
|
+
x: _NAMESPACE_ARRAY
|
|
284
|
+
input array having shape ``(..., M, M)`` and whose innermost two dimensions form square matrices. Should have a floating-point data type.
|
|
285
|
+
|
|
286
|
+
Returns
|
|
287
|
+
-------
|
|
288
|
+
out: _NAMESPACE_ARRAY
|
|
289
|
+
an array containing the multiplicative inverses. The returned array must have a floating-point data type determined by :ref:`type-promotion` and must have the same shape as ``x``.
|
|
290
|
+
|
|
291
|
+
Notes
|
|
292
|
+
-----
|
|
293
|
+
|
|
294
|
+
.. versionchanged:: 2022.12
|
|
295
|
+
Added complex data type support.
|
|
296
|
+
"""
|
|
297
|
+
|
|
298
|
+
@abstractmethod
|
|
299
|
+
def matmul(
|
|
300
|
+
self,
|
|
301
|
+
x1: _NAMESPACE_ARRAY, x2: _NAMESPACE_ARRAY, /
|
|
302
|
+
) -> _NAMESPACE_ARRAY:
|
|
303
|
+
"""Alias for :func:`~array_api.matmul`."""
|
|
304
|
+
|
|
305
|
+
@abstractmethod
|
|
306
|
+
def matrix_norm(
|
|
307
|
+
self,
|
|
308
|
+
x: _NAMESPACE_ARRAY,
|
|
309
|
+
/,
|
|
310
|
+
*,
|
|
311
|
+
keepdims: bool = False,
|
|
312
|
+
ord: Optional[Union[int, float, Literal["fro", "nuc"]]] = "fro",
|
|
313
|
+
) -> _NAMESPACE_ARRAY:
|
|
314
|
+
"""
|
|
315
|
+
Computes the matrix norm of a matrix (or a stack of matrices) ``x``.
|
|
316
|
+
|
|
317
|
+
Parameters
|
|
318
|
+
----------
|
|
319
|
+
x: _NAMESPACE_ARRAY
|
|
320
|
+
input array having shape ``(..., M, N)`` and whose innermost two dimensions form ``MxN`` matrices. Should have a floating-point data type.
|
|
321
|
+
keepdims: bool
|
|
322
|
+
If ``True``, the last two axes (dimensions) must be included in the result as singleton dimensions, and, accordingly, the result must be compatible with the input array (see :ref:`broadcasting`). Otherwise, if ``False``, the last two axes (dimensions) must not be included in the result. Default: ``False``.
|
|
323
|
+
ord: Optional[Union[int, float, Literal[inf, -inf, 'fro', 'nuc']]]
|
|
324
|
+
order of the norm. The following mathematical norms must be supported:
|
|
325
|
+
|
|
326
|
+
+------------------+---------------------------------+
|
|
327
|
+
| ord | description |
|
|
328
|
+
+==================+=================================+
|
|
329
|
+
| 'fro' | Frobenius norm |
|
|
330
|
+
+------------------+---------------------------------+
|
|
331
|
+
| 'nuc' | nuclear norm |
|
|
332
|
+
+------------------+---------------------------------+
|
|
333
|
+
| 1 | max(sum(abs(x), axis=0)) |
|
|
334
|
+
+------------------+---------------------------------+
|
|
335
|
+
| 2 | largest singular value |
|
|
336
|
+
+------------------+---------------------------------+
|
|
337
|
+
| inf | max(sum(abs(x), axis=1)) |
|
|
338
|
+
+------------------+---------------------------------+
|
|
339
|
+
|
|
340
|
+
The following non-mathematical "norms" must be supported:
|
|
341
|
+
|
|
342
|
+
+------------------+---------------------------------+
|
|
343
|
+
| ord | description |
|
|
344
|
+
+==================+=================================+
|
|
345
|
+
| -1 | min(sum(abs(x), axis=0)) |
|
|
346
|
+
+------------------+---------------------------------+
|
|
347
|
+
| -2 | smallest singular value |
|
|
348
|
+
+------------------+---------------------------------+
|
|
349
|
+
| -inf | min(sum(abs(x), axis=1)) |
|
|
350
|
+
+------------------+---------------------------------+
|
|
351
|
+
|
|
352
|
+
If ``ord=1``, the norm corresponds to the induced matrix norm where ``p=1`` (i.e., the maximum absolute value column sum).
|
|
353
|
+
|
|
354
|
+
If ``ord=2``, the norm corresponds to the induced matrix norm where ``p=inf`` (i.e., the maximum absolute value row sum).
|
|
355
|
+
|
|
356
|
+
If ``ord=inf``, the norm corresponds to the induced matrix norm where ``p=2`` (i.e., the largest singular value).
|
|
357
|
+
|
|
358
|
+
Default: ``'fro'``.
|
|
359
|
+
|
|
360
|
+
Returns
|
|
361
|
+
-------
|
|
362
|
+
out: _NAMESPACE_ARRAY
|
|
363
|
+
an array containing the norms for each ``MxN`` matrix. If ``keepdims`` is ``False``, the returned array must have a rank which is two less than the rank of ``x``. If ``x`` has a real-valued data type, the returned array must have a real-valued floating-point data type determined by :ref:`type-promotion`. If ``x`` has a complex-valued data type, the returned array must have a real-valued floating-point data type whose precision matches the precision of ``x`` (e.g., if ``x`` is ``complex128``, then the returned array must have a ``float64`` data type).
|
|
364
|
+
|
|
365
|
+
Notes
|
|
366
|
+
-----
|
|
367
|
+
|
|
368
|
+
.. versionchanged:: 2022.12
|
|
369
|
+
Added complex data type support.
|
|
370
|
+
"""
|
|
371
|
+
|
|
372
|
+
@abstractmethod
|
|
373
|
+
def matrix_power(
|
|
374
|
+
self,
|
|
375
|
+
x: _NAMESPACE_ARRAY, n: int, /
|
|
376
|
+
) -> _NAMESPACE_ARRAY:
|
|
377
|
+
"""
|
|
378
|
+
Raises a square matrix (or a stack of square matrices) ``x`` to an integer power ``n``.
|
|
379
|
+
|
|
380
|
+
Parameters
|
|
381
|
+
----------
|
|
382
|
+
x: _NAMESPACE_ARRAY
|
|
383
|
+
input array having shape ``(..., M, M)`` and whose innermost two dimensions form square matrices. Should have a floating-point data type.
|
|
384
|
+
n: int
|
|
385
|
+
integer exponent.
|
|
386
|
+
|
|
387
|
+
Returns
|
|
388
|
+
-------
|
|
389
|
+
out: _NAMESPACE_ARRAY
|
|
390
|
+
if ``n`` is equal to zero, an array containing the identity matrix for each square matrix. If ``n`` is less than zero, an array containing the inverse of each square matrix raised to the absolute value of ``n``, provided that each square matrix is invertible. If ``n`` is greater than zero, an array containing the result of raising each square matrix to the power ``n``. The returned array must have the same shape as ``x`` and a floating-point data type determined by :ref:`type-promotion`.
|
|
391
|
+
|
|
392
|
+
Notes
|
|
393
|
+
-----
|
|
394
|
+
|
|
395
|
+
.. versionchanged:: 2022.12
|
|
396
|
+
Added complex data type support.
|
|
397
|
+
"""
|
|
398
|
+
|
|
399
|
+
@abstractmethod
|
|
400
|
+
def matrix_rank(
|
|
401
|
+
self,
|
|
402
|
+
x: _NAMESPACE_ARRAY, /, *, rtol: Optional[Union[float, _NAMESPACE_ARRAY]] = None
|
|
403
|
+
) -> _NAMESPACE_ARRAY:
|
|
404
|
+
"""
|
|
405
|
+
Returns the rank (i.e., number of non-zero singular values) of a matrix (or a stack of matrices).
|
|
406
|
+
|
|
407
|
+
When ``x`` is a stack of matrices, the function must compute the number of non-zero singular values for each matrix in the stack.
|
|
408
|
+
|
|
409
|
+
Parameters
|
|
410
|
+
----------
|
|
411
|
+
x: _NAMESPACE_ARRAY
|
|
412
|
+
input array having shape ``(..., M, N)`` and whose innermost two dimensions form ``MxN`` matrices. Should have a floating-point data type.
|
|
413
|
+
rtol: Optional[Union[float, array]]
|
|
414
|
+
relative tolerance for small singular values. Singular values approximately less than or equal to ``rtol * largest_singular_value`` are set to zero. If a ``float``, the value is equivalent to a zero-dimensional array having a real-valued floating-point data type determined by :ref:`type-promotion` (as applied to ``x``) and must be broadcast against each matrix. If an ``array``, must have a real-valued floating-point data type and must be compatible with ``shape(x)[:-2]`` (see :ref:`broadcasting`). If ``None``, the default value is ``max(M, N) * eps``, where ``eps`` must be the machine epsilon associated with the real-valued floating-point data type determined by :ref:`type-promotion` (as applied to ``x``). Default: ``None``.
|
|
415
|
+
|
|
416
|
+
Returns
|
|
417
|
+
-------
|
|
418
|
+
out: _NAMESPACE_ARRAY
|
|
419
|
+
an array containing the ranks. The returned array must have the default integer data type and must have shape ``(...)`` (i.e., must have a shape equal to ``shape(x)[:-2]``).
|
|
420
|
+
|
|
421
|
+
Notes
|
|
422
|
+
-----
|
|
423
|
+
|
|
424
|
+
.. versionchanged:: 2022.12
|
|
425
|
+
Added complex data type support.
|
|
426
|
+
"""
|
|
427
|
+
|
|
428
|
+
@abstractmethod
|
|
429
|
+
def matrix_transpose(
|
|
430
|
+
self,
|
|
431
|
+
x: _NAMESPACE_ARRAY, /
|
|
432
|
+
) -> _NAMESPACE_ARRAY:
|
|
433
|
+
"""Alias for :func:`~array_api.matrix_transpose`."""
|
|
434
|
+
|
|
435
|
+
@abstractmethod
|
|
436
|
+
def outer(
|
|
437
|
+
self,
|
|
438
|
+
x1: _NAMESPACE_ARRAY, x2: _NAMESPACE_ARRAY, /
|
|
439
|
+
) -> _NAMESPACE_ARRAY:
|
|
440
|
+
"""
|
|
441
|
+
Returns the outer product of two vectors ``x1`` and ``x2``.
|
|
442
|
+
|
|
443
|
+
Parameters
|
|
444
|
+
----------
|
|
445
|
+
x1: _NAMESPACE_ARRAY
|
|
446
|
+
first one-dimensional input array of size ``N``. Must have a numeric data type.
|
|
447
|
+
x2: _NAMESPACE_ARRAY
|
|
448
|
+
second one-dimensional input array of size ``M``. Must have a numeric data type.
|
|
449
|
+
|
|
450
|
+
Returns
|
|
451
|
+
-------
|
|
452
|
+
out: _NAMESPACE_ARRAY
|
|
453
|
+
a two-dimensional array containing the outer product and whose shape is ``(N, M)``. The returned array must have a data type determined by :ref:`type-promotion`.
|
|
454
|
+
|
|
455
|
+
Notes
|
|
456
|
+
-----
|
|
457
|
+
|
|
458
|
+
.. versionchanged:: 2022.12
|
|
459
|
+
Added complex data type support.
|
|
460
|
+
"""
|
|
461
|
+
|
|
462
|
+
@abstractmethod
|
|
463
|
+
def pinv(
|
|
464
|
+
self,
|
|
465
|
+
x: _NAMESPACE_ARRAY, /, *, rtol: Optional[Union[float, _NAMESPACE_ARRAY]] = None
|
|
466
|
+
) -> _NAMESPACE_ARRAY:
|
|
467
|
+
r"""
|
|
468
|
+
Returns the (Moore-Penrose) pseudo-inverse of a matrix (or a stack of matrices) ``x``.
|
|
469
|
+
|
|
470
|
+
The pseudo-inverse of a matrix :math:`A`, denoted :math:`A^{+}`, is defined as the matrix that "solves" the least-squares problem :math:`Ax = b` (i.e., if :math:`\overline{x}` is a solution, then :math:`A^{+}` is the matrix such that :math:`\overline{x} = A^{+}b`).
|
|
471
|
+
|
|
472
|
+
While the pseudo-inverse can be defined algebraically, one can understand the pseudo-inverse via singular value decomposition (SVD). Namely, if
|
|
473
|
+
|
|
474
|
+
.. math::
|
|
475
|
+
A = U \Sigma V^H
|
|
476
|
+
|
|
477
|
+
is a singular decomposition of :math:`A`, then
|
|
478
|
+
|
|
479
|
+
.. math::
|
|
480
|
+
A^{+} = U \Sigma^{+} V^H
|
|
481
|
+
|
|
482
|
+
where :math:`U` and :math:`V^H` are orthogonal matrices, :math:`\Sigma` is a diagonal matrix consisting of :math:`A`'s singular values, and :math:`\Sigma^{+}` is then a diagonal matrix consisting of the reciprocals of :math:`A`'s singular values, leaving zeros in place. During numerical computation, only elements larger than a small tolerance are considered nonzero, and all others replaced by zeros.
|
|
483
|
+
|
|
484
|
+
When ``x`` is a stack of matrices, the function must compute the pseudo-inverse for each matrix in the stack.
|
|
485
|
+
|
|
486
|
+
Parameters
|
|
487
|
+
----------
|
|
488
|
+
x: _NAMESPACE_ARRAY
|
|
489
|
+
input array having shape ``(..., M, N)`` and whose innermost two dimensions form ``MxN`` matrices. Should have a floating-point data type.
|
|
490
|
+
rtol: Optional[Union[float, array]]
|
|
491
|
+
relative tolerance for small singular values. Singular values approximately less than or equal to ``rtol * largest_singular_value`` are set to zero. If a ``float``, the value is equivalent to a zero-dimensional array having a real-valued floating-point data type determined by :ref:`type-promotion` (as applied to ``x``) and must be broadcast against each matrix. If an ``array``, must have a real-valued floating-point data type and must be compatible with ``shape(x)[:-2]`` (see :ref:`broadcasting`). If ``None``, the default value is ``max(M, N) * eps``, where ``eps`` must be the machine epsilon associated with the real-valued floating-point data type determined by :ref:`type-promotion` (as applied to ``x``). Default: ``None``.
|
|
492
|
+
|
|
493
|
+
Returns
|
|
494
|
+
-------
|
|
495
|
+
out: _NAMESPACE_ARRAY
|
|
496
|
+
an array containing the pseudo-inverse(s). The returned array must have a floating-point data type determined by :ref:`type-promotion` and must have shape ``(..., N, M)`` (i.e., must have the same shape as ``x``, except the innermost two dimensions must be transposed).
|
|
497
|
+
|
|
498
|
+
Notes
|
|
499
|
+
-----
|
|
500
|
+
|
|
501
|
+
.. versionchanged:: 2022.12
|
|
502
|
+
Added complex data type support.
|
|
503
|
+
"""
|
|
504
|
+
|
|
505
|
+
@abstractmethod
|
|
506
|
+
def qr(
|
|
507
|
+
self,
|
|
508
|
+
x: _NAMESPACE_ARRAY, /, *, mode: Literal["reduced", "complete"] = "reduced"
|
|
509
|
+
) -> Tuple[_NAMESPACE_ARRAY, _NAMESPACE_ARRAY]:
|
|
510
|
+
r"""
|
|
511
|
+
Returns the QR decomposition of a full column rank matrix (or a stack of matrices).
|
|
512
|
+
|
|
513
|
+
If ``x`` is real-valued, let :math:`\mathbb{K}` be the set of real numbers :math:`\mathbb{R}`, and, if ``x`` is complex-valued, let :math:`\mathbb{K}` be the set of complex numbers :math:`\mathbb{C}`.
|
|
514
|
+
|
|
515
|
+
The **complete QR decomposition** of a matrix :math:`x \in\ \mathbb{K}^{n \times n}` is defined as
|
|
516
|
+
|
|
517
|
+
.. math::
|
|
518
|
+
x = QR
|
|
519
|
+
|
|
520
|
+
where :math:`Q \in\ \mathbb{K}^{m \times m}` is orthogonal when ``x`` is real-valued and unitary when ``x`` is complex-valued and where :math:`R \in\ \mathbb{K}^{m \times n}` is an upper triangular matrix with real diagonal (even when ``x`` is complex-valued).
|
|
521
|
+
|
|
522
|
+
When :math:`m \gt n` (tall matrix), as :math:`R` is upper triangular, the last :math:`m - n` rows are zero. In this case, the last :math:`m - n` columns of :math:`Q` can be dropped to form the **reduced QR decomposition**.
|
|
523
|
+
|
|
524
|
+
.. math::
|
|
525
|
+
x = QR
|
|
526
|
+
|
|
527
|
+
where :math:`Q \in\ \mathbb{K}^{m \times n}` and :math:`R \in\ \mathbb{K}^{n \times n}`.
|
|
528
|
+
|
|
529
|
+
The reduced QR decomposition equals with the complete QR decomposition when :math:`n \geq m` (wide matrix).
|
|
530
|
+
|
|
531
|
+
When ``x`` is a stack of matrices, the function must compute the QR decomposition for each matrix in the stack.
|
|
532
|
+
|
|
533
|
+
.. note::
|
|
534
|
+
Whether an array library explicitly checks whether an input array is a full column rank matrix (or a stack of full column rank matrices) is implementation-defined.
|
|
535
|
+
|
|
536
|
+
.. warning::
|
|
537
|
+
The elements in the diagonal of :math:`R` are not necessarily positive. Accordingly, the returned QR decomposition is only unique up to the sign of the diagonal of :math:`R`, and different libraries or inputs on different devices may produce different valid decompositions.
|
|
538
|
+
|
|
539
|
+
.. warning::
|
|
540
|
+
The QR decomposition is only well-defined if the first ``k = min(m,n)`` columns of every matrix in ``x`` are linearly independent.
|
|
541
|
+
|
|
542
|
+
Parameters
|
|
543
|
+
----------
|
|
544
|
+
x: _NAMESPACE_ARRAY
|
|
545
|
+
input array having shape ``(..., M, N)`` and whose innermost two dimensions form ``MxN`` matrices of rank ``N``. Should have a floating-point data type.
|
|
546
|
+
mode: Literal['reduced', 'complete']
|
|
547
|
+
decomposition mode. Should be one of the following modes:
|
|
548
|
+
|
|
549
|
+
- ``'reduced'``: compute only the leading ``K`` columns of ``q``, such that ``q`` and ``r`` have dimensions ``(..., M, K)`` and ``(..., K, N)``, respectively, and where ``K = min(M, N)``.
|
|
550
|
+
- ``'complete'``: compute ``q`` and ``r`` with dimensions ``(..., M, M)`` and ``(..., M, N)``, respectively.
|
|
551
|
+
|
|
552
|
+
Default: ``'reduced'``.
|
|
553
|
+
|
|
554
|
+
Returns
|
|
555
|
+
-------
|
|
556
|
+
out: Tuple[array, array]
|
|
557
|
+
a namedtuple ``(Q, R)`` whose
|
|
558
|
+
|
|
559
|
+
- first element must have the field name ``Q`` and must be an array whose shape depends on the value of ``mode`` and contain matrices with orthonormal columns. If ``mode`` is ``'complete'``, the array must have shape ``(..., M, M)``. If ``mode`` is ``'reduced'``, the array must have shape ``(..., M, K)``, where ``K = min(M, N)``. The first ``x.ndim-2`` dimensions must have the same size as those of the input array ``x``.
|
|
560
|
+
- second element must have the field name ``R`` and must be an array whose shape depends on the value of ``mode`` and contain upper-triangular matrices. If ``mode`` is ``'complete'``, the array must have shape ``(..., M, N)``. If ``mode`` is ``'reduced'``, the array must have shape ``(..., K, N)``, where ``K = min(M, N)``. The first ``x.ndim-2`` dimensions must have the same size as those of the input ``x``.
|
|
561
|
+
|
|
562
|
+
Each returned array must have a floating-point data type determined by :ref:`type-promotion`.
|
|
563
|
+
|
|
564
|
+
Notes
|
|
565
|
+
-----
|
|
566
|
+
|
|
567
|
+
.. versionchanged:: 2022.12
|
|
568
|
+
Added complex data type support.
|
|
569
|
+
"""
|
|
570
|
+
|
|
571
|
+
@abstractmethod
|
|
572
|
+
def slogdet(
|
|
573
|
+
self,
|
|
574
|
+
x: _NAMESPACE_ARRAY, /
|
|
575
|
+
) -> Tuple[_NAMESPACE_ARRAY, _NAMESPACE_ARRAY]:
|
|
576
|
+
r"""
|
|
577
|
+
Returns the sign and the natural logarithm of the absolute value of the determinant of a square matrix (or a stack of square matrices) ``x``.
|
|
578
|
+
|
|
579
|
+
.. note::
|
|
580
|
+
The purpose of this function is to calculate the determinant more accurately when the determinant is either very small or very large, as calling ``det`` may overflow or underflow.
|
|
581
|
+
|
|
582
|
+
The sign of the determinant is given by
|
|
583
|
+
|
|
584
|
+
.. math::
|
|
585
|
+
\operatorname{sign}(\det x) = \begin{cases}
|
|
586
|
+
0 & \textrm{if } \det x = 0 \\
|
|
587
|
+
\frac{\det x}{|\det x|} & \textrm{otherwise}
|
|
588
|
+
\end{cases}
|
|
589
|
+
|
|
590
|
+
where :math:`|\det x|` is the absolute value of the determinant of ``x``.
|
|
591
|
+
|
|
592
|
+
When ``x`` is a stack of matrices, the function must compute the sign and natural logarithm of the absolute value of the determinant for each matrix in the stack.
|
|
593
|
+
|
|
594
|
+
**Special Cases**
|
|
595
|
+
|
|
596
|
+
For real-valued floating-point operands,
|
|
597
|
+
|
|
598
|
+
- If the determinant is zero, the ``sign`` should be ``0`` and ``logabsdet`` should be ``-infinity``.
|
|
599
|
+
|
|
600
|
+
For complex floating-point operands,
|
|
601
|
+
|
|
602
|
+
- If the determinant is ``0 + 0j``, the ``sign`` should be ``0 + 0j`` and ``logabsdet`` should be ``-infinity + 0j``.
|
|
603
|
+
|
|
604
|
+
.. note::
|
|
605
|
+
Depending on the underlying algorithm, when the determinant is zero, the returned result may differ from ``-infinity`` (or ``-infinity + 0j``). In all cases, the determinant should be equal to ``sign * exp(logabsdet)`` (although, again, the result may be subject to numerical precision errors).
|
|
606
|
+
|
|
607
|
+
Parameters
|
|
608
|
+
----------
|
|
609
|
+
x: _NAMESPACE_ARRAY
|
|
610
|
+
input array having shape ``(..., M, M)`` and whose innermost two dimensions form square matrices. Should have a floating-point data type.
|
|
611
|
+
|
|
612
|
+
Returns
|
|
613
|
+
-------
|
|
614
|
+
out: Tuple[array, array]
|
|
615
|
+
a namedtuple (``sign``, ``logabsdet``) whose
|
|
616
|
+
|
|
617
|
+
- first element must have the field name ``sign`` and must be an array containing a number representing the sign of the determinant for each square matrix. Must have the same data type as ``x``.
|
|
618
|
+
- second element must have the field name ``logabsdet`` and must be an array containing the natural logarithm of the absolute value of the determinant for each square matrix. If ``x`` is real-valued, the returned array must have a real-valued floating-point data type determined by :ref:`type-promotion`. If ``x`` is complex, the returned array must have a real-valued floating-point data type having the same precision as ``x`` (e.g., if ``x`` is ``complex64``, ``logabsdet`` must have a ``float32`` data type).
|
|
619
|
+
|
|
620
|
+
Each returned array must have shape ``shape(x)[:-2]``.
|
|
621
|
+
|
|
622
|
+
Notes
|
|
623
|
+
-----
|
|
624
|
+
|
|
625
|
+
.. versionchanged:: 2022.12
|
|
626
|
+
Added complex data type support.
|
|
627
|
+
"""
|
|
628
|
+
|
|
629
|
+
@abstractmethod
|
|
630
|
+
def solve(
|
|
631
|
+
self,
|
|
632
|
+
x1: _NAMESPACE_ARRAY, x2: _NAMESPACE_ARRAY, /
|
|
633
|
+
) -> _NAMESPACE_ARRAY:
|
|
634
|
+
r"""
|
|
635
|
+
Returns the solution of a square system of linear equations with a unique solution.
|
|
636
|
+
|
|
637
|
+
Let ``x1`` equal :math:`A` and ``x2`` equal :math:`B`. If the promoted data type of ``x1`` and ``x2`` is real-valued, let :math:`\mathbb{K}` be the set of real numbers :math:`\mathbb{R}`, and, if the promoted data type of ``x1`` and ``x2`` is complex-valued, let :math:`\mathbb{K}` be the set of complex numbers :math:`\mathbb{C}`.
|
|
638
|
+
|
|
639
|
+
This function computes the solution :math:`X \in\ \mathbb{K}^{m \times k}` of the **linear system** associated to :math:`A \in\ \mathbb{K}^{m \times m}` and :math:`B \in\ \mathbb{K}^{m \times k}` and is defined as
|
|
640
|
+
|
|
641
|
+
.. math::
|
|
642
|
+
AX = B
|
|
643
|
+
|
|
644
|
+
This system of linear equations has a unique solution if and only if :math:`A` is invertible.
|
|
645
|
+
|
|
646
|
+
.. note::
|
|
647
|
+
Whether an array library explicitly checks whether ``x1`` is invertible is implementation-defined.
|
|
648
|
+
|
|
649
|
+
When ``x1`` and/or ``x2`` is a stack of matrices, the function must compute a solution for each matrix in the stack.
|
|
650
|
+
|
|
651
|
+
Parameters
|
|
652
|
+
----------
|
|
653
|
+
x1: _NAMESPACE_ARRAY
|
|
654
|
+
coefficient array ``A`` having shape ``(..., M, M)`` and whose innermost two dimensions form square matrices. Must be of full rank (i.e., all rows or, equivalently, columns must be linearly independent). Should have a floating-point data type.
|
|
655
|
+
x2: _NAMESPACE_ARRAY
|
|
656
|
+
ordinate (or "dependent variable") array ``B``. If ``x2`` has shape ``(M,)``, ``x2`` is equivalent to an array having shape ``(..., M, 1)``. If ``x2`` has shape ``(..., M, K)``, each column ``k`` defines a set of ordinate values for which to compute a solution, and ``shape(x2)[:-2]`` must be compatible with ``shape(x1)[:-2]`` (see :ref:`broadcasting`). Should have a floating-point data type.
|
|
657
|
+
|
|
658
|
+
Returns
|
|
659
|
+
-------
|
|
660
|
+
out: _NAMESPACE_ARRAY
|
|
661
|
+
an array containing the solution to the system ``AX = B`` for each square matrix. If ``x2`` has shape ``(M,)``, the returned array must have shape equal to ``shape(x1)[:-2] + shape(x2)[-1:]``. Otherwise, if ``x2`` has shape ``(..., M, K)```, the returned array must have shape equal to ``(..., M, K)``, where ``...`` refers to the result of broadcasting ``shape(x1)[:-2]`` and ``shape(x2)[:-2]``. The returned array must have a floating-point data type determined by :ref:`type-promotion`.
|
|
662
|
+
|
|
663
|
+
Notes
|
|
664
|
+
-----
|
|
665
|
+
|
|
666
|
+
.. versionchanged:: 2022.12
|
|
667
|
+
Added complex data type support.
|
|
668
|
+
|
|
669
|
+
.. versionchanged:: 2024.12
|
|
670
|
+
Clarified broadcasting semantics and the shape of the output array.
|
|
671
|
+
"""
|
|
672
|
+
|
|
673
|
+
@abstractmethod
|
|
674
|
+
def svd(
|
|
675
|
+
self,
|
|
676
|
+
x: _NAMESPACE_ARRAY, /, *, full_matrices: bool = True
|
|
677
|
+
) -> Tuple[_NAMESPACE_ARRAY, _NAMESPACE_ARRAY, _NAMESPACE_ARRAY]:
|
|
678
|
+
r"""
|
|
679
|
+
Returns a singular value decomposition (SVD) of a matrix (or a stack of matrices) ``x``.
|
|
680
|
+
|
|
681
|
+
If ``x`` is real-valued, let :math:`\mathbb{K}` be the set of real numbers :math:`\mathbb{R}`, and, if ``x`` is complex-valued, let :math:`\mathbb{K}` be the set of complex numbers :math:`\mathbb{C}`.
|
|
682
|
+
|
|
683
|
+
The full **singular value decomposition** of an :math:`m \times n` matrix :math:`x \in\ \mathbb{K}^{m \times n}` is a factorization of the form
|
|
684
|
+
|
|
685
|
+
.. math::
|
|
686
|
+
x = U \Sigma V^H
|
|
687
|
+
|
|
688
|
+
where :math:`U \in\ \mathbb{K}^{m \times m}`, :math:`\Sigma \in\ \mathbb{K}^{m \times\ n}`, :math:`\operatorname{diag}(\Sigma) \in\ \mathbb{R}^{k}` with :math:`k = \operatorname{min}(m, n)`, :math:`V^H \in\ \mathbb{K}^{n \times n}`, and where :math:`V^H` is the conjugate transpose when :math:`V` is complex and the transpose when :math:`V` is real-valued. When ``x`` is real-valued, :math:`U`, :math:`V` (and thus :math:`V^H`) are orthogonal, and, when ``x`` is complex, :math:`U`, :math:`V` (and thus :math:`V^H`) are unitary.
|
|
689
|
+
|
|
690
|
+
When :math:`m \gt n` (tall matrix), we can drop the last :math:`m - n` columns of :math:`U` to form the reduced SVD
|
|
691
|
+
|
|
692
|
+
.. math::
|
|
693
|
+
x = U \Sigma V^H
|
|
694
|
+
|
|
695
|
+
where :math:`U \in\ \mathbb{K}^{m \times k}`, :math:`\Sigma \in\ \mathbb{K}^{k \times\ k}`, :math:`\operatorname{diag}(\Sigma) \in\ \mathbb{R}^{k}`, and :math:`V^H \in\ \mathbb{K}^{k \times n}`. In this case, :math:`U` and :math:`V` have orthonormal columns.
|
|
696
|
+
|
|
697
|
+
Similarly, when :math:`n \gt m` (wide matrix), we can drop the last :math:`n - m` columns of :math:`V` to also form a reduced SVD.
|
|
698
|
+
|
|
699
|
+
This function returns the decomposition :math:`U`, :math:`S`, and :math:`V^H`, where :math:`S = \operatorname{diag}(\Sigma)`.
|
|
700
|
+
|
|
701
|
+
When ``x`` is a stack of matrices, the function must compute the singular value decomposition for each matrix in the stack.
|
|
702
|
+
|
|
703
|
+
.. warning::
|
|
704
|
+
The returned arrays :math:`U` and :math:`V` are neither unique nor continuous with respect to ``x``. Because :math:`U` and :math:`V` are not unique, different hardware and software may compute different singular vectors.
|
|
705
|
+
|
|
706
|
+
Non-uniqueness stems from the fact that multiplying any pair of singular vectors :math:`u_k`, :math:`v_k` by :math:`-1` when ``x`` is real-valued and by :math:`e^{\phi j}` (:math:`\phi \in \mathbb{R}`) when ``x`` is complex produces another two valid singular vectors of the matrix.
|
|
707
|
+
|
|
708
|
+
Parameters
|
|
709
|
+
----------
|
|
710
|
+
x: _NAMESPACE_ARRAY
|
|
711
|
+
input array having shape ``(..., M, N)`` and whose innermost two dimensions form matrices on which to perform singular value decomposition. Should have a floating-point data type.
|
|
712
|
+
full_matrices: bool
|
|
713
|
+
If ``True``, compute full-sized ``U`` and ``Vh``, such that ``U`` has shape ``(..., M, M)`` and ``Vh`` has shape ``(..., N, N)``. If ``False``, compute on the leading ``K`` singular vectors, such that ``U`` has shape ``(..., M, K)`` and ``Vh`` has shape ``(..., K, N)`` and where ``K = min(M, N)``. Default: ``True``.
|
|
714
|
+
|
|
715
|
+
Returns
|
|
716
|
+
-------
|
|
717
|
+
out: Tuple[array, array, array]
|
|
718
|
+
a namedtuple ``(U, S, Vh)`` whose
|
|
719
|
+
|
|
720
|
+
- first element must have the field name ``U`` and must be an array whose shape depends on the value of ``full_matrices`` and contain matrices with orthonormal columns (i.e., the columns are left singular vectors). If ``full_matrices`` is ``True``, the array must have shape ``(..., M, M)``. If ``full_matrices`` is ``False``, the array must have shape ``(..., M, K)``, where ``K = min(M, N)``. The first ``x.ndim-2`` dimensions must have the same shape as those of the input ``x``. Must have the same data type as ``x``.
|
|
721
|
+
- second element must have the field name ``S`` and must be an array with shape ``(..., K)`` that contains the vector(s) of singular values of length ``K``, where ``K = min(M, N)``. For each vector, the singular values must be sorted in descending order by magnitude, such that ``s[..., 0]`` is the largest value, ``s[..., 1]`` is the second largest value, et cetera. The first ``x.ndim-2`` dimensions must have the same shape as those of the input ``x``. Must have a real-valued floating-point data type having the same precision as ``x`` (e.g., if ``x`` is ``complex64``, ``S`` must have a ``float32`` data type).
|
|
722
|
+
- third element must have the field name ``Vh`` and must be an array whose shape depends on the value of ``full_matrices`` and contain orthonormal rows (i.e., the rows are the right singular vectors and the array is the adjoint). If ``full_matrices`` is ``True``, the array must have shape ``(..., N, N)``. If ``full_matrices`` is ``False``, the array must have shape ``(..., K, N)`` where ``K = min(M, N)``. The first ``x.ndim-2`` dimensions must have the same shape as those of the input ``x``. Must have the same data type as ``x``.
|
|
723
|
+
|
|
724
|
+
Notes
|
|
725
|
+
-----
|
|
726
|
+
|
|
727
|
+
.. versionchanged:: 2022.12
|
|
728
|
+
Added complex data type support.
|
|
729
|
+
"""
|
|
730
|
+
|
|
731
|
+
@abstractmethod
|
|
732
|
+
def svdvals(
|
|
733
|
+
self,
|
|
734
|
+
x: _NAMESPACE_ARRAY, /
|
|
735
|
+
) -> _NAMESPACE_ARRAY:
|
|
736
|
+
"""
|
|
737
|
+
Returns the singular values of a matrix (or a stack of matrices) ``x``.
|
|
738
|
+
|
|
739
|
+
When ``x`` is a stack of matrices, the function must compute the singular values for each matrix in the stack.
|
|
740
|
+
|
|
741
|
+
Parameters
|
|
742
|
+
----------
|
|
743
|
+
x: _NAMESPACE_ARRAY
|
|
744
|
+
input array having shape ``(..., M, N)`` and whose innermost two dimensions form matrices on which to perform singular value decomposition. Should have a floating-point data type.
|
|
745
|
+
|
|
746
|
+
Returns
|
|
747
|
+
-------
|
|
748
|
+
out: _NAMESPACE_ARRAY
|
|
749
|
+
an array with shape ``(..., K)`` that contains the vector(s) of singular values of length ``K``, where ``K = min(M, N)``. For each vector, the singular values must be sorted in descending order by magnitude, such that ``s[..., 0]`` is the largest value, ``s[..., 1]`` is the second largest value, et cetera. The first ``x.ndim-2`` dimensions must have the same shape as those of the input ``x``. The returned array must have a real-valued floating-point data type having the same precision as ``x`` (e.g., if ``x`` is ``complex64``, the returned array must have a ``float32`` data type).
|
|
750
|
+
|
|
751
|
+
Notes
|
|
752
|
+
-----
|
|
753
|
+
|
|
754
|
+
.. versionchanged:: 2022.12
|
|
755
|
+
Added complex data type support.
|
|
756
|
+
"""
|
|
757
|
+
|
|
758
|
+
@abstractmethod
|
|
759
|
+
def tensordot(
|
|
760
|
+
self,
|
|
761
|
+
x1: _NAMESPACE_ARRAY,
|
|
762
|
+
x2: _NAMESPACE_ARRAY,
|
|
763
|
+
/,
|
|
764
|
+
*,
|
|
765
|
+
axes: Union[int, Tuple[Sequence[int], Sequence[int]]] = 2,
|
|
766
|
+
) -> _NAMESPACE_ARRAY:
|
|
767
|
+
"""Alias for :func:`~array_api.tensordot`."""
|
|
768
|
+
|
|
769
|
+
@abstractmethod
|
|
770
|
+
def trace(
|
|
771
|
+
self,
|
|
772
|
+
x: _NAMESPACE_ARRAY, /, *, offset: int = 0, dtype: Optional[_NAMESPACE_DTYPE] = None
|
|
773
|
+
) -> _NAMESPACE_ARRAY:
|
|
774
|
+
"""
|
|
775
|
+
Returns the sum along the specified diagonals of a matrix (or a stack of matrices) ``x``.
|
|
776
|
+
|
|
777
|
+
Parameters
|
|
778
|
+
----------
|
|
779
|
+
x: _NAMESPACE_ARRAY
|
|
780
|
+
input array having shape ``(..., M, N)`` and whose innermost two dimensions form ``MxN`` matrices. Should have a numeric data type.
|
|
781
|
+
offset: int
|
|
782
|
+
offset specifying the off-diagonal relative to the main diagonal.
|
|
783
|
+
|
|
784
|
+
- ``offset = 0``: the main diagonal.
|
|
785
|
+
- ``offset > 0``: off-diagonal above the main diagonal.
|
|
786
|
+
- ``offset < 0``: off-diagonal below the main diagonal.
|
|
787
|
+
|
|
788
|
+
Default: ``0``.
|
|
789
|
+
dtype: Optional[dtype]
|
|
790
|
+
data type of the returned array. If ``None``, the returned array must have the same data type as ``x``, unless ``x`` has an integer data type supporting a smaller range of values than the default integer data type (e.g., ``x`` has an ``int16`` or ``uint32`` data type and the default integer data type is ``int64``). In those latter cases:
|
|
791
|
+
|
|
792
|
+
- if ``x`` has a signed integer data type (e.g., ``int16``), the returned array must have the default integer data type.
|
|
793
|
+
- if ``x`` has an unsigned integer data type (e.g., ``uint16``), the returned array must have an unsigned integer data type having the same number of bits as the default integer data type (e.g., if the default integer data type is ``int32``, the returned array must have a ``uint32`` data type).
|
|
794
|
+
|
|
795
|
+
If the data type (either specified or resolved) differs from the data type of ``x``, the input array should be cast to the specified data type before computing the sum (rationale: the ``dtype`` keyword argument is intended to help prevent overflows). Default: ``None``.
|
|
796
|
+
|
|
797
|
+
Returns
|
|
798
|
+
-------
|
|
799
|
+
out: _NAMESPACE_ARRAY
|
|
800
|
+
an array containing the traces and whose shape is determined by removing the last two dimensions and storing the traces in the last array dimension. For example, if ``x`` has rank ``k`` and shape ``(I, J, K, ..., L, M, N)``, then an output array has rank ``k-2`` and shape ``(I, J, K, ..., L)`` where
|
|
801
|
+
|
|
802
|
+
::
|
|
803
|
+
|
|
804
|
+
out[i, j, k, ..., l] = trace(a[i, j, k, ..., l, :, :])
|
|
805
|
+
|
|
806
|
+
The returned array must have a data type as described by the ``dtype`` parameter above.
|
|
807
|
+
|
|
808
|
+
Notes
|
|
809
|
+
-----
|
|
810
|
+
|
|
811
|
+
**Special Cases**
|
|
812
|
+
|
|
813
|
+
Let ``N`` equal the number of elements over which to compute the sum.
|
|
814
|
+
|
|
815
|
+
- If ``N`` is ``0``, the sum is ``0`` (i.e., the empty sum).
|
|
816
|
+
|
|
817
|
+
For both real-valued and complex floating-point operands, special cases must be handled as if the operation is implemented by successive application of :func:`~array_api.add`.
|
|
818
|
+
|
|
819
|
+
.. versionchanged:: 2022.12
|
|
820
|
+
Added complex data type support.
|
|
821
|
+
|
|
822
|
+
.. versionchanged:: 2023.12
|
|
823
|
+
Required the function to return a floating-point array having the same data type as the input array when provided a floating-point array.
|
|
824
|
+
"""
|
|
825
|
+
|
|
826
|
+
@abstractmethod
|
|
827
|
+
def vecdot(
|
|
828
|
+
self,
|
|
829
|
+
x1: _NAMESPACE_ARRAY, x2: _NAMESPACE_ARRAY, /, *, axis: int = -1
|
|
830
|
+
) -> _NAMESPACE_ARRAY:
|
|
831
|
+
"""Alias for :func:`~array_api.vecdot`."""
|
|
832
|
+
|
|
833
|
+
@abstractmethod
|
|
834
|
+
def vector_norm(
|
|
835
|
+
self,
|
|
836
|
+
x: _NAMESPACE_ARRAY,
|
|
837
|
+
/,
|
|
838
|
+
*,
|
|
839
|
+
axis: Optional[Union[int, Tuple[int, ...]]] = None,
|
|
840
|
+
keepdims: bool = False,
|
|
841
|
+
ord: Union[int, float] = 2,
|
|
842
|
+
) -> _NAMESPACE_ARRAY:
|
|
843
|
+
r"""
|
|
844
|
+
Computes the vector norm of a vector (or batch of vectors) ``x``.
|
|
845
|
+
|
|
846
|
+
Parameters
|
|
847
|
+
----------
|
|
848
|
+
x: _NAMESPACE_ARRAY
|
|
849
|
+
input array. Should have a floating-point data type.
|
|
850
|
+
axis: Optional[Union[int, Tuple[int, ...]]]
|
|
851
|
+
If an integer, ``axis`` specifies the axis (dimension) along which to compute vector norms. If an n-tuple, ``axis`` specifies the axes (dimensions) along which to compute batched vector norms. If ``None``, the vector norm must be computed over all array values (i.e., equivalent to computing the vector norm of a flattened array). Negative indices must be supported. Default: ``None``.
|
|
852
|
+
keepdims: bool
|
|
853
|
+
If ``True``, the axes (dimensions) specified by ``axis`` must be included in the result as singleton dimensions, and, accordingly, the result must be compatible with the input array (see :ref:`broadcasting`). Otherwise, if ``False``, the axes (dimensions) specified by ``axis`` must not be included in the result. Default: ``False``.
|
|
854
|
+
ord: Union[int, float, Literal[inf, -inf]]
|
|
855
|
+
order of the norm. The following mathematical norms must be supported:
|
|
856
|
+
|
|
857
|
+
+------------------+----------------------------+
|
|
858
|
+
| ord | description |
|
|
859
|
+
+==================+============================+
|
|
860
|
+
| 1 | L1-norm (Manhattan) |
|
|
861
|
+
+------------------+----------------------------+
|
|
862
|
+
| 2 | L2-norm (Euclidean) |
|
|
863
|
+
+------------------+----------------------------+
|
|
864
|
+
| inf | infinity norm |
|
|
865
|
+
+------------------+----------------------------+
|
|
866
|
+
| (int,float >= 1) | p-norm |
|
|
867
|
+
+------------------+----------------------------+
|
|
868
|
+
|
|
869
|
+
The following non-mathematical "norms" must be supported:
|
|
870
|
+
|
|
871
|
+
+------------------+--------------------------------+
|
|
872
|
+
| ord | description |
|
|
873
|
+
+==================+================================+
|
|
874
|
+
| 0 | sum(a != 0) |
|
|
875
|
+
+------------------+--------------------------------+
|
|
876
|
+
| -1 | 1./sum(1./abs(a)) |
|
|
877
|
+
+------------------+--------------------------------+
|
|
878
|
+
| -2 | 1./sqrt(sum(1./abs(a)\*\*2)) |
|
|
879
|
+
+------------------+--------------------------------+
|
|
880
|
+
| -inf | min(abs(a)) |
|
|
881
|
+
+------------------+--------------------------------+
|
|
882
|
+
| (int,float < 1) | sum(abs(a)\*\*ord)\*\*(1./ord) |
|
|
883
|
+
+------------------+--------------------------------+
|
|
884
|
+
|
|
885
|
+
Default: ``2``.
|
|
886
|
+
|
|
887
|
+
Returns
|
|
888
|
+
-------
|
|
889
|
+
out: _NAMESPACE_ARRAY
|
|
890
|
+
an array containing the vector norms. If ``axis`` is ``None``, the returned array must be a zero-dimensional array containing a vector norm. If ``axis`` is a scalar value (``int`` or ``float``), the returned array must have a rank which is one less than the rank of ``x``. If ``axis`` is a ``n``-tuple, the returned array must have a rank which is ``n`` less than the rank of ``x``. If ``x`` has a real-valued data type, the returned array must have a real-valued floating-point data type determined by :ref:`type-promotion`. If ``x`` has a complex-valued data type, the returned array must have a real-valued floating-point data type whose precision matches the precision of ``x`` (e.g., if ``x`` is ``complex128``, then the returned array must have a ``float64`` data type).
|
|
891
|
+
|
|
892
|
+
Notes
|
|
893
|
+
-----
|
|
894
|
+
|
|
895
|
+
.. versionchanged:: 2022.12
|
|
896
|
+
Added complex data type support.
|
|
897
|
+
"""
|