Skip to content

Commit 466b1c5

Browse files
authored
Move SparseArrays to an extension (#385)
1 parent fafa4d6 commit 466b1c5

File tree

5 files changed

+53
-34
lines changed

5 files changed

+53
-34
lines changed

Project.toml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "BandedMatrices"
22
uuid = "aae01518-5342-5314-be14-df237901396f"
3-
version = "0.17.32"
3+
version = "0.17.33"
44

55
[deps]
66
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
@@ -9,6 +9,12 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
99
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
1010
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1111

12+
[weakdeps]
13+
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
14+
15+
[extensions]
16+
BandedMatricesSparseArraysExt = "SparseArrays"
17+
1218
[compat]
1319
Aqua = "0.6"
1420
ArrayLayouts = "1"
@@ -25,7 +31,8 @@ Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
2531
GenericLinearAlgebra = "14197337-ba66-59df-a3e3-ca00e7dcff7a"
2632
InfiniteArrays = "4858937d-0d70-526a-a4dd-2d5cb5dd786c"
2733
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
34+
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
2835
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2936

3037
[targets]
31-
test = ["Aqua", "Base64", "Documenter", "GenericLinearAlgebra", "InfiniteArrays", "Random", "Test"]
38+
test = ["Aqua", "Base64", "Documenter", "GenericLinearAlgebra", "InfiniteArrays", "Random", "SparseArrays", "Test"]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module BandedMatricesSparseArraysExt
2+
3+
using BandedMatrices
4+
using BandedMatrices: _banded_rowval, _banded_colval, _banded_nzval
5+
using SparseArrays
6+
import SparseArrays: sparse
7+
8+
function sparse(B::BandedMatrix)
9+
sparse(_banded_rowval(B), _banded_colval(B), _banded_nzval(B), size(B)...)
10+
end
11+
12+
function BandedMatrices.bandwidths(A::SparseMatrixCSC)
13+
l,u = -size(A,1),-size(A,2)
14+
15+
m,n = size(A)
16+
rows = rowvals(A)
17+
vals = nonzeros(A)
18+
for j = 1:n
19+
for ind in nzrange(A, j)
20+
i = rows[ind]
21+
# We skip non-structural zeros when computing the
22+
# bandwidths.
23+
iszero(vals[ind]) && continue
24+
ij = abs(i-j)
25+
if i j
26+
l = max(l, ij)
27+
u = max(u, -ij)
28+
elseif i < j
29+
l = max(l, -ij)
30+
u = max(u, ij)
31+
end
32+
end
33+
end
34+
35+
l,u
36+
end
37+
38+
end

src/BandedMatrices.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module BandedMatrices
2-
using Base, FillArrays, ArrayLayouts, LinearAlgebra, SparseArrays
2+
using Base, FillArrays, ArrayLayouts, LinearAlgebra
33

44
using Base: require_one_based_indexing, reindex, checkbounds, @propagate_inbounds,
55
oneto, promote_op, MultiplicativeInverses, OneTo, ReshapedArray, Slice
@@ -24,8 +24,6 @@ import LinearAlgebra: axpy!, _chol!, rot180, dot, cholcopy, _apply_ipiv_rows!,
2424
using LinearAlgebra.LAPACK
2525
using LinearAlgebra.LAPACK: chkuplo, chktrans
2626

27-
import SparseArrays: sparse
28-
2927
import ArrayLayouts: MemoryLayout, transposelayout, triangulardata,
3028
conjlayout, symmetriclayout, symmetricdata,
3129
triangularlayout, MatLdivVec, hermitianlayout, hermitiandata,
@@ -94,6 +92,11 @@ include("tribanded.jl")
9492

9593
include("interfaceimpl.jl")
9694

95+
if !isdefined(Base, :get_extension)
96+
include("../ext/BandedMatricesSparseArraysExt.jl")
97+
end
98+
9799
include("precompile.jl")
98100

101+
99102
end #module

src/banded/BandedMatrix.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -782,9 +782,6 @@ function _banded_nzval(B::AbstractMatrix)
782782
end
783783

784784

785-
sparse(B::BandedMatrix) = sparse(_banded_rowval(B), _banded_colval(B), _banded_nzval(B), size(B)...)
786-
787-
788785

789786

790787
function _bidiagonalize!(A::AbstractMatrix{T}, M::BandedColumnMajor) where T

src/generic/AbstractBandedMatrix.jl

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,6 @@ Returns a tuple containing the lower and upper bandwidth of `A`, in order.
2222
bandwidths(A::AbstractVecOrMat) = bandwidths(MemoryLayout(A), A)
2323
bandwidths(_, A) = (size(A,1)-1 , size(A,2)-1)
2424

25-
function BandedMatrices.bandwidths(A::SparseMatrixCSC)
26-
l,u = -size(A,1),-size(A,2)
27-
28-
m,n = size(A)
29-
rows = rowvals(A)
30-
vals = nonzeros(A)
31-
for j = 1:n
32-
for ind in nzrange(A, j)
33-
i = rows[ind]
34-
# We skip non-structural zeros when computing the
35-
# bandwidths.
36-
iszero(vals[ind]) && continue
37-
ij = abs(i-j)
38-
if i j
39-
l = max(l, ij)
40-
u = max(u, -ij)
41-
elseif i < j
42-
l = max(l, -ij)
43-
u = max(u, ij)
44-
end
45-
end
46-
end
47-
48-
l,u
49-
end
50-
5125
"""
5226
bandwidth(A,i)
5327

0 commit comments

Comments
 (0)