Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"

[weakdeps]
CliqueTrees = "60701a23-6482-424a-84db-faee86b9b1f8"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[extensions]
BandedMatricesSparseArraysExt = "SparseArrays"
CliqueTreesExt = "CliqueTrees"

[compat]
Aqua = "0.8"
ArrayLayouts = "1.6.0"
CliqueTrees = "0.5"
Documenter = "1"
FillArrays = "1.3"
GenericLinearAlgebra = "0.3"
Expand All @@ -31,6 +34,7 @@ julia = "1.10"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
CliqueTrees = "60701a23-6482-424a-84db-faee86b9b1f8"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
GenericLinearAlgebra = "14197337-ba66-59df-a3e3-ca00e7dcff7a"
InfiniteArrays = "4858937d-0d70-526a-a4dd-2d5cb5dd786c"
Expand All @@ -40,4 +44,4 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Aqua", "Documenter", "GenericLinearAlgebra", "InfiniteArrays", "Random", "SparseArrays", "Test", "Quaternions"]
test = ["Aqua", "CliqueTrees", "Documenter", "GenericLinearAlgebra", "InfiniteArrays", "Random", "SparseArrays", "Test", "Quaternions"]
38 changes: 38 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,44 @@ To loop over the nonzero elements of a BandedMatrix, you can use `colrange(A, c)

Use `Symmetric(::BandedMatrix)` to work with symmetric banded matrices.

## Bandwidth minimization

The [reverse Cuthill-McKee algorithm](https://en.wikipedia.org/wiki/Cuthill–McKee_algorithm) permutes a symmetric matrix into a band matrix with small bandwidth.
This algorithm is implemented by the function `symrcm`.

```julia
julia> using BandedMatrices, CliqueTrees

julia> matrix = [
1 0 1 0 0 0 0 1 1
0 1 1 0 0 1 1 1 0
1 1 1 0 0 0 0 0 0
0 0 0 1 0 0 0 1 1
0 0 0 0 1 0 1 1 0
0 1 0 0 0 1 1 0 0
0 1 0 0 1 1 1 0 0
1 1 0 1 1 0 0 1 0
1 0 0 1 0 0 0 0 1
];

julia> bandedmatrix, perm, invp = symrcm(matrix);

julia> bandedmatrix
9×9 LinearAlgebra.Symmetric{Int64, BandedMatrix{Int64, Matrix{Int64}, Base.OneTo{Int64}}}:
1 1 1 0 ⋅ ⋅ ⋅ ⋅ ⋅
1 1 1 1 0 ⋅ ⋅ ⋅ ⋅
1 1 1 0 1 1 ⋅ ⋅ ⋅
0 1 0 1 0 1 0 ⋅ ⋅
⋅ 0 1 0 1 0 1 0 ⋅
⋅ ⋅ 1 1 0 1 1 1 0
⋅ ⋅ ⋅ 0 1 1 1 0 1
⋅ ⋅ ⋅ ⋅ 0 1 0 1 1
⋅ ⋅ ⋅ ⋅ ⋅ 0 1 1 1

julia> bandedmatrix == matrix[perm, perm]
true
```

## Banded matrix interface

Banded matrices go beyond the type `BandedMatrix`: one can also create
Expand Down
35 changes: 35 additions & 0 deletions ext/CliqueTreesExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module CliqueTreesExt

using BandedMatrices
using Base.Sort: Algorithm
using CliqueTrees: CliqueTrees, permutation, RCMGL
using CliqueTrees.SparseArrays
using LinearAlgebra

function BandedMatrices.symrcm(matrix::AbstractMatrix, alg::Algorithm)
BandedMatrices.symrcm(sparse(matrix), alg)
end

function BandedMatrices.symrcm(matrix::SparseMatrixCSC{T}, alg::Algorithm) where T
order, index = permutation(matrix; alg=RCMGL(alg))
lower = tril!(permute(matrix, order, order))

bandwidth = maximum(axes(lower, 2)) do j
p = last(nzrange(lower, j))
return rowvals(lower)[p] - j
end

banded = BandedMatrix{T}(undef, size(lower), (bandwidth, 0))
fill!(banded.data, zero(T))

@inbounds for j in axes(lower, 2)
for p in nzrange(lower, j)
i = rowvals(lower)[p]
banded.data[i - j + 1, j] = nonzeros(lower)[p]
end
end

return Symmetric(banded, :L), order, index
end

end
2 changes: 2 additions & 0 deletions src/BandedMatrices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export BandedMatrix,
bandwidth,
BandError,
band,
symrcm,
Band,
BandRange,
bandwidths,
Expand Down Expand Up @@ -87,6 +88,7 @@ include("symbanded/BandedCholesky.jl")
include("symbanded/SplitCholesky.jl")
include("symbanded/bandedeigen.jl")
include("symbanded/tridiagonalize.jl")
include("symbanded/symrcm.jl")

include("tribanded.jl")

Expand Down
42 changes: 42 additions & 0 deletions src/symbanded/symrcm.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
symrcm(matrix[, alg::Algorithm])

Use the Reverse Cuthill-Mckee algorithm to reduce the bandwith of a matrix.

# Examples
```julia
julia> using BandedMatrices, CliqueTrees

julia> matrix = [
1 0 1 0 0 0 0 1 1
0 1 1 0 0 1 1 1 0
1 1 1 0 0 0 0 0 0
0 0 0 1 0 0 0 1 1
0 0 0 0 1 0 1 1 0
0 1 0 0 0 1 1 0 0
0 1 0 0 1 1 1 0 0
1 1 0 1 1 0 0 1 0
1 0 0 1 0 0 0 0 1
];

julia> bandedmatrix, perm, invp = symrcm(matrix);

julia> bandedmatrix
9×9 LinearAlgebra.Symmetric{Int64, BandedMatrix{Int64, Matrix{Int64}, Base.OneTo{Int64}}}:
1 1 1 0 ⋅ ⋅ ⋅ ⋅ ⋅
1 1 1 1 0 ⋅ ⋅ ⋅ ⋅
1 1 1 0 1 1 ⋅ ⋅ ⋅
0 1 0 1 0 1 0 ⋅ ⋅
⋅ 0 1 0 1 0 1 0 ⋅
⋅ ⋅ 1 1 0 1 1 1 0
⋅ ⋅ ⋅ 0 1 1 1 0 1
⋅ ⋅ ⋅ ⋅ 0 1 0 1 1
⋅ ⋅ ⋅ ⋅ ⋅ 0 1 1 1

julia> bandedmatrix == matrix[perm, perm]
true
```
"""
function symrcm(matrix::AbstractMatrix)
symrcm(matrix, Base.Sort.DEFAULT_UNSTABLE)
end
43 changes: 43 additions & 0 deletions test/test_symbanded.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module TestSymBanded
using ArrayLayouts
using BandedMatrices
import BandedMatrices: MemoryLayout, SymmetricLayout, HermitianLayout, BandedColumns, isbanded
using CliqueTrees: CliqueTrees
using FillArrays
using GenericLinearAlgebra
using LinearAlgebra
Expand Down Expand Up @@ -421,4 +422,46 @@ end
end
end

@testset "symrcm" begin
matrix = Int16[
1 0 1 0 0 0 0 1 1
0 1 1 0 0 1 1 1 0
1 1 1 0 0 0 0 0 0
0 0 0 1 0 0 0 1 1
0 0 0 0 1 0 1 1 0
0 1 0 0 0 1 1 0 0
0 1 0 0 1 1 1 0 0
1 1 0 1 1 0 0 1 0
1 0 0 1 0 0 0 0 1
]

bandedmatrix, perm, invp = symrcm(matrix)
@test invp == invperm(perm)
@test bandedmatrix == matrix[perm, perm]
@test eltype(bandedmatrix) == eltype(matrix)

# LFAT5
matrix = Float32[
0.608806 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -0.304403 0.0
0.0 1.57088 0.0 -94.2528 0.78544 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 15080.4 0.0 0.0 -94.2528 0.0 0.0 94.2528 0.0 -7540.22 0.0 0.0 0.0
0.0 -94.2528 0.0 15080.4 0.0 94.2528 0.0 0.0 0.0 0.0 -7540.22 0.0 0.0 0.0
0.0 0.78544 0.0 0.0 3.14176 0.78544 0.0 0.0 0.0 0.0 -94.2528 0.0 0.0 0.0
0.0 0.0 -94.2528 94.2528 0.78544 3.14176 0.0 0.0 0.0 0.78544 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 1.25664f7 0.0 0.0 0.0 0.0 -6.2832f6 0.0 -6.2832f6
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.608806 0.0 0.0 0.0 0.0 -0.304403 0.0
0.0 0.0 94.2528 0.0 0.0 0.0 0.0 0.0 1.57088 0.78544 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.78544 0.0 0.0 0.78544 3.14176 94.2528 0.0 0.0 0.0
0.0 0.0 -7540.22 -7540.22 -94.2528 0.0 0.0 0.0 0.0 94.2528 15080.4 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 -6.2832f6 0.0 0.0 0.0 0.0 1.25664f7 0.0 0.0
-0.304403 0.0 0.0 0.0 0.0 0.0 0.0 -0.304403 0.0 0.0 0.0 0.0 0.608806 0.0
0.0 0.0 0.0 0.0 0.0 0.0 -6.2832f6 0.0 0.0 0.0 0.0 0.0 0.0 1.25664f7
]

bandedmatrix, perm, invp = symrcm(matrix)
@test invp == invperm(perm)
@test bandedmatrix == matrix[perm, perm]
@test eltype(bandedmatrix) == eltype(matrix)
end

end # module
Loading