Skip to content
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

Documentation update for LinearAlgebra functions #52934

Merged
merged 19 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
fb9bdd9
Addnl LA Documentation: Dummy commit with TODOs to get a PR started.
aravindh-krishnamoorthy Jan 16, 2024
aace7ac
LinearAlgebra: Remove exceptions for exported but undocumented functi…
aravindh-krishnamoorthy Jan 19, 2024
8c8edbb
Update LinearAlgebra test to ensure no undocumented exported symbols
aravindh-krishnamoorthy Jan 20, 2024
7bee3ae
Update stdlib/LinearAlgebra/test/runtests.jl
aravindh-krishnamoorthy Jan 20, 2024
0f89207
Update stdlib/LinearAlgebra/test/runtests.jl
aravindh-krishnamoorthy Jan 20, 2024
9267ce2
Merge branch 'master' into la_addnl_docs
aravindh-krishnamoorthy Jan 24, 2024
4b4587c
DocStrings for copy_transpose!
aravindh-krishnamoorthy Jan 24, 2024
c0acaad
DocStrings for RankDeficientException and LAPACKException.
aravindh-krishnamoorthy Jan 26, 2024
f27751d
Initial text on pivoting
aravindh-krishnamoorthy Jan 27, 2024
de24482
Added DocStrings to copyto!
aravindh-krishnamoorthy Jan 28, 2024
6f3338e
Documentation for pivoting and pivoting strategies.
aravindh-krishnamoorthy Jan 29, 2024
61bf648
Incorporate changes from self review.
aravindh-krishnamoorthy Feb 3, 2024
d47f20e
index.md: Remove trailing whitespace.
aravindh-krishnamoorthy Feb 3, 2024
72a188a
Apply suggestions from code review by @dkarrasch
aravindh-krishnamoorthy Feb 7, 2024
e3985dd
Apply suggestions from code review by @stevengj and @dkarrasch
aravindh-krishnamoorthy Feb 7, 2024
296ecd5
Update stdlib/LinearAlgebra/src/matmul.jl
aravindh-krishnamoorthy Feb 7, 2024
a653963
Remove non-breaking space
aravindh-krishnamoorthy Feb 7, 2024
3cb092e
fix adjoint case in copyto!
dkarrasch Feb 11, 2024
a7fd509
fix typo
dkarrasch Feb 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion stdlib/LinearAlgebra/docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,35 @@ generally broadcasting over elements in the matrix representation fail because t
be highly inefficient. For such use cases, consider computing the matrix representation
up front and cache it for future reuse.

## [Pivoting Strategies](@id man-linalg-pivoting-strategies)

Several of Julia's [matrix factorizations](@ref man-linalg-factorizations) support
[pivoting](https://en.wikipedia.org/wiki/Pivot_element), which can be used to improve their
numerical stability. In fact, some matrix factorizations, such as the LU
factorization, may fail without pivoting.

In pivoting, first, a [pivot element](https://en.wikipedia.org/wiki/Pivot_element)
with good numerical properties is chosen based on a pivoting strategy. Next, the rows and
columns of the original matrix are permuted to bring the chosen element in place for
subsequent computation. Furthermore, the process is repeated for each stage of the factorization.

Consequently, besides the conventional matrix factors, the outputs of
pivoted factorization schemes also include permutation matrices.

In the following, the pivoting strategies implemented in Julia are briefly described. Note
that not all matrix factorizations may support them. Consult the documentation of the
respective [matrix factorization](@ref man-linalg-factorizations) for details on the
supported pivoting strategies.

See also [`LinearAlgebra.ZeroPivotException`](@ref).

```@docs
LinearAlgebra.NoPivot
LinearAlgebra.RowNonZero
LinearAlgebra.RowMaximum
LinearAlgebra.ColumnNorm
```

## Standard functions

Linear algebra functions in Julia are largely implemented by calling functions from [LAPACK](https://www.netlib.org/lapack/).
Expand All @@ -417,6 +446,8 @@ Base.:/(::AbstractVecOrMat, ::AbstractVecOrMat)
LinearAlgebra.SingularException
LinearAlgebra.PosDefException
LinearAlgebra.ZeroPivotException
LinearAlgebra.RankDeficientException
LinearAlgebra.LAPACKException
LinearAlgebra.dot
LinearAlgebra.dot(::Any, ::Any, ::Any)
LinearAlgebra.cross
Expand Down Expand Up @@ -570,6 +601,7 @@ LinearAlgebra.checksquare
LinearAlgebra.peakflops
LinearAlgebra.hermitianpart
LinearAlgebra.hermitianpart!
LinearAlgebra.copy_transpose!
```

## Low-level matrix operations
Expand Down Expand Up @@ -761,7 +793,7 @@ LinearAlgebra.BLAS.trsm!
LinearAlgebra.BLAS.trsm
```

## LAPACK functions
## [LAPACK functions](@id man-linalg-lapack-functions)

`LinearAlgebra.LAPACK` provides wrappers for some of the LAPACK functions for linear algebra.
Those functions that overwrite one of the input arrays have names ending in `'!'`.
Expand Down
35 changes: 35 additions & 0 deletions stdlib/LinearAlgebra/src/LinearAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,45 @@ abstract type Algorithm end
struct DivideAndConquer <: Algorithm end
struct QRIteration <: Algorithm end

# Pivoting strategies for matrix factorization algorithms.
abstract type PivotingStrategy end

"""
NoPivot

Pivoting is not performed. Matrix factorizations such as the LU factorization
may fail without pivoting.
aravindh-krishnamoorthy marked this conversation as resolved.
Show resolved Hide resolved
"""
struct NoPivot <: PivotingStrategy end

"""
RowNonZero

First non-zero element in the remaining rows is chosen as the pivot element.
aravindh-krishnamoorthy marked this conversation as resolved.
Show resolved Hide resolved

Note that the [element type](@ref eltype) of the matrix must support the [`iszero`](@ref)
aravindh-krishnamoorthy marked this conversation as resolved.
Show resolved Hide resolved
method.
"""
struct RowNonZero <: PivotingStrategy end

"""
RowMaximum

The maximum element in the remaining rows is chosen as the pivot element.
aravindh-krishnamoorthy marked this conversation as resolved.
Show resolved Hide resolved

Note that the [element type](@ref eltype) of the matrix must support the [`abs`](@ref)
and [`<`](@ref) methods.
aravindh-krishnamoorthy marked this conversation as resolved.
Show resolved Hide resolved
"""
struct RowMaximum <: PivotingStrategy end

"""
ColumnNorm

The column with the maximum norm is used for subsequent computation.
aravindh-krishnamoorthy marked this conversation as resolved.
Show resolved Hide resolved

Note that the [element type](@ref eltype) of the matrix must support the [`norm`](@ref),
[`abs`](@ref), and [`<`](@ref) methods.
aravindh-krishnamoorthy marked this conversation as resolved.
Show resolved Hide resolved
"""
struct ColumnNorm <: PivotingStrategy end

# Check that stride of matrix/vector is 1
Expand Down
14 changes: 14 additions & 0 deletions stdlib/LinearAlgebra/src/exceptions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ export LAPACKException,
RankDeficientException,
ZeroPivotException

"""
LAPACKException

Generic LAPACK exception thrown either during direct calls to the [LAPACK functions](@ref man-linalg-lapack-functions)
or during calls to other functions that use the LAPACK functions internally but lack specialized error handling. The `info` field
contains additional information on the underlying error and depends on the LAPACK function that was invoked.
"""
struct LAPACKException <: Exception
info::BlasInt
end
Expand Down Expand Up @@ -41,6 +48,13 @@ function Base.showerror(io::IO, ex::PosDefException)
print(io, "; Factorization failed.")
end

"""
RankDeficientException

Exception thrown when the input matrix is [rank deficient](https://en.wikipedia.org/wiki/Rank_(linear_algebra)). Some
linear algebra functions, such as the Cholesky decomposition, are only applicable to matrices that are not rank
deficient. The `info` field indicates the computed rank of the matrix.
"""
struct RankDeficientException <: Exception
info::BlasInt
end
Expand Down
40 changes: 40 additions & 0 deletions stdlib/LinearAlgebra/src/matmul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,26 @@ end

lapack_size(t::AbstractChar, M::AbstractVecOrMat) = (size(M, t=='N' ? 1 : 2), size(M, t=='N' ? 2 : 1))

"""
copyto!(B::AbstractMatrix, ir_dest::AbstractUnitRange, jr_dest::AbstractUnitRange,
tM::AbstractChar,
M::AbstractVecOrMat, ir_src::AbstractUnitRange, jr_src::AbstractUnitRange) -> B

Efficiently copy elements of matrix `M` to `B` conditioned on the character
parameter `tM` as follows:

| `tM` | Destination | Source |
| --- | :--- | :--- |
| `'N'` | `B[ir_dest, jr_dest]` | `M[ir_src, jr_src]` |
| `'T'` | `B[ir_dest, jr_dest]` | `transpose(M)[ir_src, jr_src]` |
| `'C'` | `B[ir_dest, jr_dest]` | `conj(transpose(M))[ir_src, jr_src]` |
aravindh-krishnamoorthy marked this conversation as resolved.
Show resolved Hide resolved

The elements `B[ir_dest, jr_dest]` are overwritten. Furthermore, the index range
parameters must satisfy `length(ir_dest) == length(ir_src)` and
`length(jr_dest) == length(jr_src)`.

See also [`copy_transpose!`](@ref).
"""
function copyto!(B::AbstractVecOrMat, ir_dest::AbstractUnitRange{Int}, jr_dest::AbstractUnitRange{Int}, tM::AbstractChar, M::AbstractVecOrMat, ir_src::AbstractUnitRange{Int}, jr_src::AbstractUnitRange{Int})
if tM == 'N'
copyto!(B, ir_dest, jr_dest, M, ir_src, jr_src)
Expand All @@ -692,6 +712,26 @@ function copyto!(B::AbstractVecOrMat, ir_dest::AbstractUnitRange{Int}, jr_dest::
B
end

"""
copy_transpose!(B::AbstractMatrix, ir_dest::AbstractUnitRange, jr_dest::AbstractUnitRange,
tM::AbstractChar,
M::AbstractVecOrMat, ir_src::AbstractUnitRange, jr_src::AbstractUnitRange) -> B

Efficiently copy elements of matrix `M` to `B` conditioned on the character
parameter `tM` as follows:

| `tM` | Destination | Source |
| --- | :--- | :--- |
| `'N'` | `B[ir_dest, jr_dest]` | `transpose(M)[jr_src, ir_src]` |
| `'T'` | `B[ir_dest, jr_dest]` | `M[jr_src, ir_src]` |
| `'C'` | `B[ir_dest, jr_dest]` | `conj(M)[jr_src, ir_src]` |

The elements `B[ir_dest, jr_dest]` are overwritten. Furthermore, the index
range parameters must satisfy `length(ir_dest) == length(jr_src)` and
`length(jr_dest) == length(ir_src)`.

See also [`copyto!`](@ref).
"""
function copy_transpose!(B::AbstractMatrix, ir_dest::AbstractUnitRange{Int}, jr_dest::AbstractUnitRange{Int}, tM::AbstractChar, M::AbstractVecOrMat, ir_src::AbstractUnitRange{Int}, jr_src::AbstractUnitRange{Int})
if tM == 'N'
LinearAlgebra.copy_transpose!(B, ir_dest, jr_dest, M, ir_src, jr_src)
Expand Down
12 changes: 12 additions & 0 deletions stdlib/LinearAlgebra/src/transpose.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ copy(::Union{Transpose,Adjoint})
Base.copy(A::TransposeAbsMat) = transpose!(similar(A.parent, reverse(axes(A.parent))), A.parent)
Base.copy(A::AdjointAbsMat) = adjoint!(similar(A.parent, reverse(axes(A.parent))), A.parent)

"""
copy_transpose!(B::AbstractVecOrMat, ir_dest::AbstractRange{Int}, jr_dest::AbstractRange{Int},
A::AbstractVecOrMat, ir_src::AbstractRange{Int}, jr_src::AbstractRange{Int}) -> B

Efficiently copy elements of matrix `A` to `B` with transposition as follows:

B[ir_dest, jr_dest] = transpose(A)[jr_src, ir_src]

The elements `B[ir_dest, jr_dest]` are overwritten. Furthermore,
the index range parameters must satisfy `length(ir_dest) == length(jr_src)` and
`length(jr_dest) == length(ir_src)`.
"""
function copy_transpose!(B::AbstractVecOrMat, ir_dest::AbstractRange{Int}, jr_dest::AbstractRange{Int},
A::AbstractVecOrMat, ir_src::AbstractRange{Int}, jr_src::AbstractRange{Int})
if length(ir_dest) != length(jr_src)
Expand Down
4 changes: 1 addition & 3 deletions stdlib/LinearAlgebra/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@ for file in readlines(joinpath(@__DIR__, "testgroups"))
end

@testset "Docstrings" begin
undoc = Docs.undocumented_names(LinearAlgebra)
@test_broken isempty(undoc)
@test undoc == [:ColumnNorm, :LAPACKException, :NoPivot, :RankDeficientException, :RowMaximum, :RowNonZero, :copy_transpose!]
@test isempty(Docs.undocumented_names(LinearAlgebra))
end