diff --git a/Project.toml b/Project.toml index dd19abb9..638d40da 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "BandedMatrices" uuid = "aae01518-5342-5314-be14-df237901396f" -version = "0.17.32" +version = "0.17.33" [deps] ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a" @@ -9,6 +9,12 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" +[weakdeps] +SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" + +[extensions] +BandedMatricesSparseArraysExt = "SparseArrays" + [compat] Aqua = "0.6" ArrayLayouts = "1" @@ -25,7 +31,8 @@ Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" GenericLinearAlgebra = "14197337-ba66-59df-a3e3-ca00e7dcff7a" InfiniteArrays = "4858937d-0d70-526a-a4dd-2d5cb5dd786c" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" +SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Aqua", "Base64", "Documenter", "GenericLinearAlgebra", "InfiniteArrays", "Random", "Test"] +test = ["Aqua", "Base64", "Documenter", "GenericLinearAlgebra", "InfiniteArrays", "Random", "SparseArrays", "Test"] diff --git a/ext/BandedMatricesSparseArraysExt.jl b/ext/BandedMatricesSparseArraysExt.jl new file mode 100644 index 00000000..d5f371d0 --- /dev/null +++ b/ext/BandedMatricesSparseArraysExt.jl @@ -0,0 +1,38 @@ +module BandedMatricesSparseArraysExt + +using BandedMatrices +using BandedMatrices: _banded_rowval, _banded_colval, _banded_nzval +using SparseArrays +import SparseArrays: sparse + +function sparse(B::BandedMatrix) + sparse(_banded_rowval(B), _banded_colval(B), _banded_nzval(B), size(B)...) +end + +function BandedMatrices.bandwidths(A::SparseMatrixCSC) + l,u = -size(A,1),-size(A,2) + + m,n = size(A) + rows = rowvals(A) + vals = nonzeros(A) + for j = 1:n + for ind in nzrange(A, j) + i = rows[ind] + # We skip non-structural zeros when computing the + # bandwidths. + iszero(vals[ind]) && continue + ij = abs(i-j) + if i ≥ j + l = max(l, ij) + u = max(u, -ij) + elseif i < j + l = max(l, -ij) + u = max(u, ij) + end + end + end + + l,u +end + +end diff --git a/src/BandedMatrices.jl b/src/BandedMatrices.jl index 8a3a0f15..a2920f51 100644 --- a/src/BandedMatrices.jl +++ b/src/BandedMatrices.jl @@ -1,5 +1,5 @@ module BandedMatrices -using Base, FillArrays, ArrayLayouts, LinearAlgebra, SparseArrays +using Base, FillArrays, ArrayLayouts, LinearAlgebra using Base: require_one_based_indexing, reindex, checkbounds, @propagate_inbounds, oneto, promote_op, MultiplicativeInverses, OneTo, ReshapedArray, Slice @@ -24,8 +24,6 @@ import LinearAlgebra: axpy!, _chol!, rot180, dot, cholcopy, _apply_ipiv_rows!, using LinearAlgebra.LAPACK using LinearAlgebra.LAPACK: chkuplo, chktrans -import SparseArrays: sparse - import ArrayLayouts: MemoryLayout, transposelayout, triangulardata, conjlayout, symmetriclayout, symmetricdata, triangularlayout, MatLdivVec, hermitianlayout, hermitiandata, @@ -94,6 +92,11 @@ include("tribanded.jl") include("interfaceimpl.jl") +if !isdefined(Base, :get_extension) + include("../ext/BandedMatricesSparseArraysExt.jl") +end + include("precompile.jl") + end #module diff --git a/src/banded/BandedMatrix.jl b/src/banded/BandedMatrix.jl index 7b9c0cb7..1ab285b5 100644 --- a/src/banded/BandedMatrix.jl +++ b/src/banded/BandedMatrix.jl @@ -782,9 +782,6 @@ function _banded_nzval(B::AbstractMatrix) end -sparse(B::BandedMatrix) = sparse(_banded_rowval(B), _banded_colval(B), _banded_nzval(B), size(B)...) - - function _bidiagonalize!(A::AbstractMatrix{T}, M::BandedColumnMajor) where T diff --git a/src/generic/AbstractBandedMatrix.jl b/src/generic/AbstractBandedMatrix.jl index 15d05d84..336ae83e 100644 --- a/src/generic/AbstractBandedMatrix.jl +++ b/src/generic/AbstractBandedMatrix.jl @@ -22,32 +22,6 @@ Returns a tuple containing the lower and upper bandwidth of `A`, in order. bandwidths(A::AbstractVecOrMat) = bandwidths(MemoryLayout(A), A) bandwidths(_, A) = (size(A,1)-1 , size(A,2)-1) -function BandedMatrices.bandwidths(A::SparseMatrixCSC) - l,u = -size(A,1),-size(A,2) - - m,n = size(A) - rows = rowvals(A) - vals = nonzeros(A) - for j = 1:n - for ind in nzrange(A, j) - i = rows[ind] - # We skip non-structural zeros when computing the - # bandwidths. - iszero(vals[ind]) && continue - ij = abs(i-j) - if i ≥ j - l = max(l, ij) - u = max(u, -ij) - elseif i < j - l = max(l, -ij) - u = max(u, ij) - end - end - end - - l,u -end - """ bandwidth(A,i)