-
Notifications
You must be signed in to change notification settings - Fork 53
Add initial linear algebra function specifications #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 18 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
bc73de3
Add cross signature
kgryte 7d9be38
Add det specification
kgryte 88090b4
Add diagonal specification
kgryte 02f80be
Add inv specification
kgryte 41697d9
Add norm specification
kgryte 20164af
Add outer product specification
kgryte f046571
Add outer specification
kgryte e0b1e18
Add trace specification
kgryte 95194aa
Add transpose
kgryte 5095031
Update index
kgryte 6a7fde8
Fix type annotation
kgryte 83593dc
Update norm behavior for multi-dimensional arrays
kgryte 16fc53c
Support all of NumPy's norms
kgryte 4745713
Switch order
kgryte 46f0bfc
Split into separate tables
kgryte 8f65e94
Escape markup
kgryte 2ab06d5
Escape markup
kgryte fdc07e1
Add matrix_transpose interface
kgryte cd076e5
Rename parameters
kgryte 1bff1b5
Merge branch 'master' of https://github.com/pydata-apis/array-api int…
kgryte 4015620
Remove matrix_transpose signature
kgryte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ API specification | |
broadcasting | ||
out_keyword | ||
elementwise_functions | ||
linear_algebra_functions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,286 @@ | ||
# Linear Algebra Functions | ||
|
||
> Array API specification for linear algebra functions. | ||
|
||
A conforming implementation of the array API standard must provide and support the following functions adhering to the following conventions. | ||
|
||
- Positional parameters must be [positional-only](https://www.python.org/dev/peps/pep-0570/) parameters. Positional-only parameters have no externally-usable name. When a function accepting positional-only parameters is called, positional arguments are mapped to these parameters based solely on their order. | ||
- Optional parameters must be [keyword-only](https://www.python.org/dev/peps/pep-3102/) arguments. | ||
- Broadcasting semantics must follow the semantics defined in :ref:`broadcasting`. | ||
- Unless stated otherwise, functions must support the data types defined in :ref:`data-types`. | ||
- Unless stated otherwise, functions must adhere to the type promotion rules defined in :ref:`type-promotion`. | ||
- Unless stated otherwise, floating-point operations must adhere to IEEE 754-2019. | ||
|
||
<!-- NOTE: please keep the functions in alphabetical order --> | ||
|
||
### <a name="cross" href="#cross">#</a> cross(a, b, /, *, axis=-1) | ||
|
||
Returns the cross product of 3-element vectors. If `a` and `b` are multi-dimensional arrays (i.e., both have a rank greater than `1`), then the cross-product of each pair of corresponding 3-element vectors is independently computed. | ||
|
||
#### Parameters | ||
|
||
- **a**: _<array>_ | ||
|
||
- first input array. | ||
|
||
- **b**: _<array>_ | ||
|
||
- second input array. Must have the same shape as `a`. | ||
|
||
- **axis**: _int_ | ||
|
||
- the axis (dimension) of `a` and `b` containing the vectors for which to compute the cross product. If set to `-1`, the function computes the cross product for vectors defined by the last axis (dimension). Default: `-1`. | ||
|
||
#### Returns | ||
|
||
- **out**: _<array>_ | ||
|
||
- an array containing the cross products. | ||
|
||
### <a name="det" href="#det">#</a> det(a, /) | ||
|
||
Returns the determinant of a square matrix (or stack of square matrices) `a`. | ||
|
||
#### Parameters | ||
|
||
- **a**: _<array>_ | ||
|
||
- input array having shape `(..., M, M)` and whose innermost two dimensions form square matrices. | ||
|
||
#### Returns | ||
|
||
- **out**: _<array>_ | ||
|
||
- if `a` 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. | ||
|
||
### <a name="diagonal" href="#diagonal">#</a> diagonal(a, /, *, axis1=0, axis2=1, offset=0) | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Returns the specified diagonals. If `a` has more than two dimensions, then the axes (dimensions) specified by `axis1` and `axis2` are used to determine the two-dimensional sub-arrays from which to return diagonals. | ||
|
||
#### Parameters | ||
|
||
- **a**: _<array>_ | ||
|
||
- input array. Must have at least `2` dimensions. | ||
|
||
- **axis1**: _int_ | ||
|
||
- first axis (dimension) with respect to which to take diagonal. Default: `0`. | ||
|
||
- **axis2**: _int_ | ||
|
||
- second axis (dimension) with respect to which to take diagonal. Default: `1`. | ||
|
||
- **offset**: _int_ | ||
|
||
- offset specifying the off-diagonal relative to the main diagonal. | ||
|
||
- `offset = 0`: the main diagonal. | ||
- `offset > 0`: off-diagonal above the main diagonal. | ||
- `offset < 0`: off-diagonal below the main diagonal. | ||
|
||
Default: `0`. | ||
|
||
#### Returns | ||
|
||
- **out**: _<array>_ | ||
|
||
- if `a` is a two-dimensional array, a one-dimensional array containing the diagonal; otherwise, a multi-dimensional array containing the diagonals and whose shape is determined by removing `axis1` and `axis2` and appending a dimension equal to the size of the resulting diagonals. Must have the same data type as `a`. | ||
|
||
### <a name="inv" href="#inv">#</a> inv(a, /) | ||
|
||
Computes the multiplicative inverse of a square matrix (or stack of square matrices) `a`. | ||
|
||
#### Parameters | ||
|
||
- **a**: _<array>_ | ||
|
||
- input array having shape `(..., M, M)` and whose innermost two dimensions form square matrices. | ||
|
||
#### Returns | ||
|
||
- **out**: _<array>_ | ||
|
||
- an array containing the multiplicative inverses. Must have the same data type and shape as `a`. | ||
|
||
### <a name="matrix_transpose" href="#matrix_transpose">#</a> matrix_transpose(a, /, *, axis1=0, axis2=1) | ||
|
||
Transposes the axes (dimensions) specified by `axis1` and `axis2`. If `a` has more than two dimensions, then the axes (dimensions) specified by `axis1` and `axis2` are used to determine the two-dimensional sub-arrays to be transposed. | ||
|
||
#### Parameters | ||
|
||
- **a**: _<array>_ | ||
|
||
- input array. Must have at least `2` dimensions. | ||
|
||
- **axis1**: _int_ | ||
|
||
- first axis (dimension). Default: `0`. | ||
|
||
- **axis2**: _int_ | ||
|
||
- second axis (dimension). Default: `1`. | ||
|
||
#### Returns | ||
|
||
- **out**: _<array>_ | ||
|
||
- an array containing the matrix transpose(s). | ||
|
||
### <a name="norm" href="#norm">#</a> norm(a, /, *, axis=None, keepdims=False, ord=None) | ||
|
||
Computes the matrix or vector norm of `a`. | ||
|
||
#### Parameters | ||
|
||
- **a**: _<array>_ | ||
|
||
- input array. | ||
|
||
- **axis**: _Optional\[ Union\[ int, Tuple\[ int, int ] ] ]_ | ||
|
||
- If an integer, `axis` specifies the axis (dimension) along which to compute vector norms. | ||
|
||
If a 2-tuple, `axis` specifies the axes (dimensions) defining two-dimensional matrices for which to compute matrix norms. | ||
|
||
If `None`, | ||
|
||
- if `a` is one-dimensional, the function computes the vector norm. | ||
- if `a` is two-dimensional, the function computes the matrix norm. | ||
- if `a` has more than two dimensions, the function computes the vector norm over all array values (i.e., equivalent to computing the vector norm of a flattened array). | ||
|
||
Negative indices must be supported. Default: `None`. | ||
|
||
- **keepdims**: _bool_ | ||
|
||
- 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`. | ||
|
||
- **ord**: _Optional\[ int, float, Literal\[ inf, -inf, 'fro', 'nuc' ] ]_ | ||
|
||
- order of the norm. The following mathematical norms must be supported: | ||
|
||
| ord | matrix | vector | | ||
| ---------------- | ------------------------------- | -------------------------- | | ||
| 'fro' | 'fro' | - | | ||
| 'nuc' | 'nuc' | - | | ||
| 1 | max(sum(abs(x), axis=0)) | L1-norm (Manhattan) | | ||
| 2 | largest singular value | L2-norm (Euclidean) | | ||
| inf | max(sum(abs(x), axis=1)) | infinity norm | | ||
| (int,float >= 1) | - | p-norm | | ||
|
||
The following non-mathematical "norms" must be supported: | ||
|
||
| ord | matrix | vector | | ||
| ---------------- | ------------------------------- | ------------------------------ | | ||
| 0 | - | sum(a != 0) | | ||
| -1 | min(sum(abs(x), axis=0)) | 1./sum(1./abs(a)) | | ||
| -2 | smallest singular value | 1./sqrt(sum(1./abs(a)\*\*2)) | | ||
| -inf | min(sum(abs(x), axis=1)) | min(abs(a)) | | ||
| (int,float < 1) | - | sum(abs(a)\*\*ord)\*\*(1./ord) | | ||
|
||
When `ord` is `None`, the following norms must be the default norms: | ||
|
||
| ord | matrix | vector | | ||
| ---------------- | ------------------------------- | -------------------------- | | ||
| None | 'fro' | L2-norm (Euclidean) | | ||
|
||
where `fro` corresponds to the **Frobenius norm**, `nuc` corresponds to the **nuclear norm**, and `-` indicates that the norm is **not** supported. | ||
|
||
For matrices, | ||
|
||
- if `ord=1`, the norm corresponds to the induced matrix norm where `p=1` (i.e., the maximum absolute value column sum). | ||
- if `ord=2`, the norm corresponds to the induced matrix norm where `p=inf` (i.e., the maximum absolute value row sum). | ||
- if `ord=inf`, the norm corresponds to the induced matrix norm where `p=2` (i.e., the largest singular value). | ||
|
||
If `None`, | ||
|
||
- if matrix (or matrices), the function computes the Frobenius norm. | ||
- if vector (or vectors), the function computes the L2-norm (Euclidean norm). | ||
|
||
Default: `None`. | ||
|
||
#### Returns | ||
|
||
- **out**: _<array>_ | ||
|
||
- an array containing the norms. Must have the same data type as `a`. If `axis` is `None`, the output array is a zero-dimensional array containing a vector norm. If `axis` is a scalar value (`int` or `float`), the output array has a rank which is one less than the rank of `a`. If `axis` is a 2-tuple, the output array has a rank which is two less than the rank of `a`. | ||
|
||
### <a name="outer" href="#outer">#</a> outer(a, b, /) | ||
|
||
Computes the outer product of two vectors `a` and `b`. | ||
|
||
#### Parameters | ||
|
||
- **a**: _<array>_ | ||
|
||
- first one-dimensional input array of size `N`. | ||
|
||
- **b**: _<array>_ | ||
|
||
- second one-dimensional input array of size `M`. | ||
|
||
#### Returns | ||
|
||
- **out**: _<array>_ | ||
|
||
- a two-dimensional array containing the outer product and whose shape is `NxM`. | ||
|
||
### <a name="trace" href="#trace">#</a> trace(a, /, *, axis1=0, axis2=1, offset=0) | ||
|
||
Returns the sum along the specified diagonals. If `a` has more than two dimensions, then the axes (dimensions) specified by `axis1` and `axis2` are used to determine the two-dimensional sub-arrays for which to compute the trace. | ||
|
||
#### Parameters | ||
|
||
- **a**: _<array>_ | ||
|
||
- input array. Must have at least `2` dimensions. | ||
|
||
- **axis1**: _int_ | ||
|
||
- first axis (dimension) with respect to which to compute the trace. Default: `0`. | ||
|
||
- **axis2**: _int_ | ||
|
||
- second axis (dimension) with respect to which to compute the trace. Default: `1`. | ||
|
||
- **offset**: _int_ | ||
|
||
- offset specifying the off-diagonal relative to the main diagonal. | ||
|
||
- `offset = 0`: the main diagonal. | ||
- `offset > 0`: off-diagonal above the main diagonal. | ||
- `offset < 0`: off-diagonal below the main diagonal. | ||
|
||
Default: `0`. | ||
|
||
#### Returns | ||
|
||
- **out**: _<array>_ | ||
|
||
- if `a` is a two-dimensional array, a zero-dimensional array containing the trace; otherwise, a multi-dimensional array containing the traces. | ||
|
||
The shape of a multi-dimensional output array is determined by removing `axis1` and `axis2` and storing the traces in the last array dimension. For example, if `a` has rank `k` and shape `(I, J, K, ..., L, M, N)` and `axis1=-2` and `axis1=-1`, then a multi-dimensional output array has rank `k-2` and shape `(I, J, K, ..., L)` where | ||
|
||
```text | ||
out[i, j, k, ..., l] = trace(a[i, j, k, ..., l, :, :]) | ||
``` | ||
|
||
### <a name="transpose" href="#transpose">#</a> transpose(a, /, *, axes=None) | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Transposes (or permutes the axes (dimensions)) of an array `a`. | ||
|
||
#### Parameters | ||
|
||
- **a**: _<array>_ | ||
|
||
- input array. | ||
|
||
- **axes**: _Optional\[ Tuple\[ int, ... ] ]_ | ||
|
||
- tuple containing a permutation of `(0, 1, ..., N-1)` where `N` is the number of axes (dimensions) of `a`. If `None`, the axes (dimensions) are permuted in reverse order (i.e., equivalent to setting `axes=(N-1, ..., 1, 0)`). Default: `None`. | ||
|
||
#### Returns | ||
|
||
- **out**: _<array>_ | ||
|
||
- an array containing the transpose. Must have the same data type as `a`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.