-
Notifications
You must be signed in to change notification settings - Fork 6
Split svd_trunc #116
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
Split svd_trunc #116
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,10 +42,10 @@ See also [`svd_full(!)`](@ref svd_full), [`svd_vals(!)`](@ref svd_vals) and | |
| @functiondef svd_compact | ||
|
|
||
| """ | ||
| svd_trunc(A; [trunc], kwargs...) -> U, S, Vᴴ, ϵ | ||
| svd_trunc(A, alg::AbstractAlgorithm) -> U, S, Vᴴ, ϵ | ||
| svd_trunc!(A, [USVᴴ]; [trunc], kwargs...) -> U, S, Vᴴ, ϵ | ||
| svd_trunc!(A, [USVᴴ], alg::AbstractAlgorithm) -> U, S, Vᴴ, ϵ | ||
| svd_trunc_with_err(A; [trunc], kwargs...) -> U, S, Vᴴ, ϵ | ||
| svd_trunc_with_err(A, alg::AbstractAlgorithm) -> U, S, Vᴴ, ϵ | ||
| svd_trunc_with_err!(A, [USVᴴ]; [trunc], kwargs...) -> U, S, Vᴴ, ϵ | ||
| svd_trunc_with_err!(A, [USVᴴ], alg::AbstractAlgorithm) -> U, S, Vᴴ, ϵ | ||
|
|
||
| Compute a partial or truncated singular value decomposition (SVD) of `A`, such that | ||
| `A * (Vᴴ)' ≈ U * S`. Here, `U` is an isometric matrix (orthonormal columns) of size | ||
|
|
@@ -81,6 +81,54 @@ for the default algorithm selection behavior. | |
| When `alg` is a [`TruncatedAlgorithm`](@ref), the `trunc` keyword cannot be specified as the | ||
| truncation strategy is already embedded in the algorithm. | ||
|
|
||
| !!! note | ||
| The bang method `svd_trunc!` optionally accepts the output structure and | ||
| possibly destroys the input matrix `A`. Always use the return value of the function | ||
| as it may not always be possible to use the provided `USVᴴ` as output. | ||
|
|
||
| See also [`svd_trunc(!)`](@ref svd_trunc), [`svd_full(!)`](@ref svd_full), | ||
| [`svd_compact(!)`](@ref svd_compact), [`svd_vals(!)`](@ref svd_vals), | ||
| and [Truncations](@ref) for more information on truncation strategies. | ||
| """ | ||
| @functiondef svd_trunc_with_err | ||
|
|
||
| """ | ||
| svd_trunc(A; [trunc], kwargs...) -> U, S, Vᴴ | ||
| svd_trunc(A, alg::AbstractAlgorithm) -> U, S, Vᴴ | ||
| svd_trunc!(A, [USVᴴ]; [trunc], kwargs...) -> U, S, Vᴴ | ||
| svd_trunc!(A, [USVᴴ], alg::AbstractAlgorithm) -> U, S, Vᴴ | ||
|
|
||
| Compute a partial or truncated singular value decomposition (SVD) of `A`, such that | ||
| `A * (Vᴴ)' ≈ U * S`. Here, `U` is an isometric matrix (orthonormal columns) of size | ||
| `(m, k)`, whereas `Vᴴ` is a matrix of size `(k, n)` with orthonormal rows and `S` is a | ||
| square diagonal matrix of size `(k, k)`, with `k` is set by the truncation strategy. | ||
|
|
||
| ## Truncation | ||
| The truncation strategy can be controlled via the `trunc` keyword argument. This can be | ||
| either a `NamedTuple` or a [`TruncationStrategy`](@ref). If `trunc` is not provided or | ||
| nothing, all values will be kept. | ||
|
|
||
| ### `trunc::NamedTuple` | ||
| The supported truncation keyword arguments are: | ||
|
|
||
| $docs_truncation_kwargs | ||
|
|
||
| ### `trunc::TruncationStrategy` | ||
| For more control, a truncation strategy can be supplied directly. | ||
| By default, MatrixAlgebraKit supplies the following: | ||
|
|
||
| $docs_truncation_strategies | ||
|
|
||
| ## Keyword arguments | ||
| Other keyword arguments are passed to the algorithm selection procedure. If no explicit | ||
| `alg` is provided, these keywords are used to select and configure the algorithm through | ||
| [`MatrixAlgebraKit.select_algorithm`](@ref). The remaining keywords after algorithm | ||
| selection are passed to the algorithm constructor. See [`MatrixAlgebraKit.default_algorithm`](@ref) | ||
| for the default algorithm selection behavior. | ||
|
|
||
| When `alg` is a [`TruncatedAlgorithm`](@ref), the `trunc` keyword cannot be specified as the | ||
| truncation strategy is already embedded in the algorithm. | ||
|
|
||
| !!! note | ||
| The bang method `svd_trunc!` optionally accepts the output structure and | ||
| possibly destroys the input matrix `A`. Always use the return value of the function | ||
|
|
@@ -125,13 +173,15 @@ for f in (:svd_full!, :svd_compact!, :svd_vals!) | |
| end | ||
| end | ||
|
|
||
| function select_algorithm(::typeof(svd_trunc!), A, alg; trunc = nothing, kwargs...) | ||
| if alg isa TruncatedAlgorithm | ||
| isnothing(trunc) || | ||
| throw(ArgumentError("`trunc` can't be specified when `alg` is a `TruncatedAlgorithm`")) | ||
| return alg | ||
| else | ||
| alg_svd = select_algorithm(svd_compact!, A, alg; kwargs...) | ||
| return TruncatedAlgorithm(alg_svd, select_truncation(trunc)) | ||
| for f in (:svd_trunc!, :svd_trunc_with_err!) | ||
| @eval function select_algorithm(::typeof($f), A, alg; trunc = nothing, kwargs...) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we do the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I did it this way mostly to follow the convention above, not sure it matters much? |
||
| if alg isa TruncatedAlgorithm | ||
| isnothing(trunc) || | ||
| throw(ArgumentError("`trunc` can't be specified when `alg` is a `TruncatedAlgorithm`")) | ||
| return alg | ||
| else | ||
| alg_svd = select_algorithm(svd_compact!, A, alg; kwargs...) | ||
| return TruncatedAlgorithm(alg_svd, select_truncation(trunc)) | ||
| end | ||
| end | ||
| end | ||
Uh oh!
There was an error while loading. Please reload this page.