Skip to content

Commit a7a91fe

Browse files
authored
Split linalg.norm into separate vector and matrix APIs (#236)
1 parent 3002279 commit a7a91fe

File tree

1 file changed

+92
-71
lines changed

1 file changed

+92
-71
lines changed

spec/extensions/linear_algebra_functions.md

+92-71
Original file line numberDiff line numberDiff line change
@@ -264,123 +264,98 @@ Computes the multiplicative inverse of a square matrix (or a stack of square mat
264264

265265
Alias for {ref}`function-matmul`.
266266

267-
(function-linalg-matrix_power)=
268-
### linalg.matrix_power(x, n, /)
267+
(function-linalg-matrix-norm)=
268+
### linalg.matrix_norm(x, /, *, axis=(-2, -1), keepdims=False, ord='fro')
269269

270-
Raises a square matrix (or a stack of square matrices) `x` to an integer power `n`.
270+
Computes the matrix norm of a matrix (or a stack of matrices) `x`.
271271

272272
#### Parameters
273273

274274
- **x**: _<array>_
275275

276-
- input array having shape `(..., M, M)` and whose innermost two dimensions form square matrices. Should have a floating-point data type.
276+
- input array. Must have at least `2` dimensions. Should have a floating-point data type.
277277

278-
- **n**: _int_
278+
- **axis**: _Tuple\[ int, int ]_
279279

280-
- integer exponent.
280+
- a 2-tuple which specifies the axes (dimensions) defining two-dimensional matrices for which to compute matrix norms. Negative indices must be supported. Default: `(-2, -1)` (i.e., the last two-dimensions).
281281

282-
#### Returns
282+
- **keepdims**: _bool_
283283

284-
- **out**: _<array>_
284+
- 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`.
285285

286-
- 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`.
286+
- **ord**: _Optional\[ Union\[ int, float, Literal\[ inf, -inf, 'fro', 'nuc' ] ] ]_
287287

288-
(function-linalg-matrix_rank)=
289-
### linalg.matrix_rank(x, /, *, rtol=None)
288+
- order of the norm. The following mathematical norms must be supported:
289+
| ord | description |
290+
| ---------------- | ------------------------------- |
291+
| 'fro' | Frobenius norm |
292+
| 'nuc' | nuclear norm |
293+
| 1 | max(sum(abs(x), axis=0)) |
294+
| 2 | largest singular value |
295+
| inf | max(sum(abs(x), axis=1)) |
290296

291-
Computes the rank (i.e., number of non-zero singular values) of a matrix (or a stack of matrices).
297+
The following non-mathematical "norms" must be supported:
298+
| ord | description |
299+
| ---------------- | ------------------------------- |
300+
| -1 | min(sum(abs(x), axis=0)) |
301+
| -2 | smallest singular value |
302+
| -inf | min(sum(abs(x), axis=1)) |
292303

293-
#### Parameters
304+
If `ord=1`, the norm corresponds to the induced matrix norm where `p=1` (i.e., the maximum absolute value column sum).
294305

295-
- **x**: _<array>_
306+
If `ord=2`, the norm corresponds to the induced matrix norm where `p=inf` (i.e., the maximum absolute value row sum).
296307

297-
- input array having shape `(..., M, N)` and whose innermost two dimensions form `MxN` matrices. Should have a floating-point data type.
308+
If `ord=inf`, the norm corresponds to the induced matrix norm where `p=2` (i.e., the largest singular value).
298309

299-
- **rtol**: _Optional\[ Union\[ float, <array> ] ]_
300-
301-
- relative tolerance for small singular values. Singular values 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 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 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 floating-point data type determined by {ref}`type-promotion` (as applied to `x`). Default: `None`.
310+
Default: `'fro'`.
302311

303312
#### Returns
304313

305314
- **out**: _<array>_
306315

307-
- an array containing the ranks. The returned array must have a floating-point data type determined by {ref}`type-promotion` and must have shape `(...)` (i.e., must have a shape equal to `shape(x)[:-2]`).
316+
- an array containing the norms. If `keepdims` is `False`, the returned array must have a rank which is two less than the rank of `x`. The returned array must have a floating-point data type determined by {ref}`type-promotion`.
308317

309-
(function-linalg-norm)=
310-
### linalg.norm(x, /, *, axis=None, keepdims=False, ord=None)
318+
(function-linalg-matrix_power)=
319+
### linalg.matrix_power(x, n, /)
311320

312-
Computes the matrix or vector norm of `x`.
321+
Raises a square matrix (or a stack of square matrices) `x` to an integer power `n`.
313322

314323
#### Parameters
315324

316325
- **x**: _<array>_
317326

318-
- input array. Should have a floating-point data type.
319-
320-
- **axis**: _Optional\[ Union\[ int, Tuple\[ int, int ] ] ]_
321-
322-
- If an integer, `axis` specifies the axis (dimension) along which to compute vector norms.
323-
324-
If a 2-tuple, `axis` specifies the axes (dimensions) defining two-dimensional matrices for which to compute matrix norms.
325-
326-
If `None`,
327-
328-
- if `x` is one-dimensional, the function must compute the vector norm.
329-
- if `x` is two-dimensional, the function must compute the matrix norm.
330-
- if `x` has more than two dimensions, the function must compute the vector norm over all array values (i.e., equivalent to computing the vector norm of a flattened array).
331-
332-
Negative indices must be supported. Default: `None`.
327+
- input array having shape `(..., M, M)` and whose innermost two dimensions form square matrices. Should have a floating-point data type.
333328

334-
- **keepdims**: _bool_
329+
- **n**: _int_
335330

336-
- 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`.
331+
- integer exponent.
337332

338-
- **ord**: _Optional\[ Union\[ int, float, Literal\[ inf, -inf, 'fro', 'nuc' ] ] ]_
333+
#### Returns
339334

340-
- order of the norm. The following mathematical norms must be supported:
341-
| ord | matrix | vector |
342-
| ---------------- | ------------------------------- | -------------------------- |
343-
| 'fro' | 'fro' | - |
344-
| 'nuc' | 'nuc' | - |
345-
| 1 | max(sum(abs(x), axis=0)) | L1-norm (Manhattan) |
346-
| 2 | largest singular value | L2-norm (Euclidean) |
347-
| inf | max(sum(abs(x), axis=1)) | infinity norm |
348-
| (int,float >= 1) | - | p-norm |
335+
- **out**: _<array>_
349336

350-
The following non-mathematical "norms" must be supported:
351-
| ord | matrix | vector |
352-
| ---------------- | ------------------------------- | ------------------------------ |
353-
| 0 | - | sum(a != 0) |
354-
| -1 | min(sum(abs(x), axis=0)) | 1./sum(1./abs(a)) |
355-
| -2 | smallest singular value | 1./sqrt(sum(1./abs(a)\*\*2)) |
356-
| -inf | min(sum(abs(x), axis=1)) | min(abs(a)) |
357-
| (int,float < 1) | - | sum(abs(a)\*\*ord)\*\*(1./ord) |
337+
- 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`.
358338

359-
When `ord` is `None`, the following norms must be the default norms:
360-
| ord | matrix | vector |
361-
| ---------------- | ------------------------------- | -------------------------- |
362-
| None | 'fro' | L2-norm (Euclidean) |
339+
(function-linalg-matrix_rank)=
340+
### linalg.matrix_rank(x, /, *, rtol=None)
363341

364-
where `fro` corresponds to the **Frobenius norm**, `nuc` corresponds to the **nuclear norm**, and `-` indicates that the norm is **not** supported.
342+
Computes the rank (i.e., number of non-zero singular values) of a matrix (or a stack of matrices).
365343

366-
For matrices,
344+
#### Parameters
367345

368-
- if `ord=1`, the norm corresponds to the induced matrix norm where `p=1` (i.e., the maximum absolute value column sum).
369-
- if `ord=2`, the norm corresponds to the induced matrix norm where `p=inf` (i.e., the maximum absolute value row sum).
370-
- if `ord=inf`, the norm corresponds to the induced matrix norm where `p=2` (i.e., the largest singular value).
346+
- **x**: _&lt;array&gt;_
371347

372-
If `None`,
348+
- input array having shape `(..., M, N)` and whose innermost two dimensions form `MxN` matrices. Should have a floating-point data type.
373349

374-
- if matrix (or matrices), the function must compute the Frobenius norm.
375-
- if vector (or vectors), the function must compute the L2-norm (Euclidean norm).
350+
- **rtol**: _Optional\[ Union\[ float, &lt;array&gt; ] ]_
376351

377-
Default: `None`.
352+
- relative tolerance for small singular values. Singular values 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 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 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 floating-point data type determined by {ref}`type-promotion` (as applied to `x`). Default: `None`.
378353

379354
#### Returns
380355

381356
- **out**: _&lt;array&gt;_
382357

383-
- an array containing the 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 2-tuple, the returned array must have a rank which is two less than the rank of `x`. The returned array must have a floating-point data type determined by {ref}`type-promotion`.
358+
- an array containing the ranks. The returned array must have a floating-point data type determined by {ref}`type-promotion` and must have shape `(...)` (i.e., must have a shape equal to `shape(x)[:-2]`).
384359

385360
(function-linalg-outer)=
386361
### linalg.outer(x1, x2, /)
@@ -596,3 +571,49 @@ Alias for {ref}`function-transpose`.
596571
### linalg.vecdot(x1, x2, /, *, axis=None)
597572
598573
Alias for {ref}`function-vecdot`.
574+
575+
(function-linalg-vector-norm)=
576+
### linalg.vector_norm(x, /, *, axis=None, keepdims=False, ord=2)
577+
578+
Computes the vector norm of a vector (or batch of vectors) `x`.
579+
580+
#### Parameters
581+
582+
- **x**: _&lt;array&gt;_
583+
584+
- input array. Should have a floating-point data type.
585+
586+
- **axis**: _Optional\[ Union\[ int, Tuple\[ int, int ] ] ]_
587+
588+
- 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`.
589+
590+
- **keepdims**: _bool_
591+
592+
- 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`.
593+
594+
- **ord**: _Optional\[ Union\[ int, float, Literal\[ inf, -inf ] ] ]_
595+
596+
- order of the norm. The following mathematical norms must be supported:
597+
| ord | description |
598+
| ---------------- | -------------------------- |
599+
| 1 | L1-norm (Manhattan) |
600+
| 2 | L2-norm (Euclidean) |
601+
| inf | infinity norm |
602+
| (int,float >= 1) | p-norm |
603+
604+
The following non-mathematical "norms" must be supported:
605+
| ord | description |
606+
| ---------------- | ------------------------------ |
607+
| 0 | sum(a != 0) |
608+
| -1 | 1./sum(1./abs(a)) |
609+
| -2 | 1./sqrt(sum(1./abs(a)\*\*2)) |
610+
| -inf | min(abs(a)) |
611+
| (int,float < 1) | sum(abs(a)\*\*ord)\*\*(1./ord) |
612+
613+
Default: `2`.
614+
615+
#### Returns
616+
617+
- **out**: _&lt;array&gt;_
618+
619+
- 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`. The returned array must have a floating-point data type determined by {ref}`type-promotion`.

0 commit comments

Comments
 (0)