Skip to content

Commit

Permalink
Move SparseArrays to an extension
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub committed Jul 27, 2023
1 parent fafa4d6 commit 9358ec1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 34 deletions.
11 changes: 9 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand All @@ -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"]
38 changes: 38 additions & 0 deletions ext/BandedMatricesSparseArraysExt.jl
Original file line number Diff line number Diff line change
@@ -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
9 changes: 6 additions & 3 deletions src/BandedMatrices.jl
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
3 changes: 0 additions & 3 deletions src/banded/BandedMatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 0 additions & 26 deletions src/generic/AbstractBandedMatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9358ec1

Please sign in to comment.