Skip to content

Commit db0e4f3

Browse files
Merge pull request #448 from JuliaArrays/sparse_ext
Make sparsearrays an ext
2 parents ac0b31a + cb614b5 commit db0e4f3

File tree

3 files changed

+42
-35
lines changed

3 files changed

+42
-35
lines changed

Project.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ version = "7.12.0"
55
[deps]
66
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
8-
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
9-
SuiteSparse = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9"
108

119
[weakdeps]
1210
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
@@ -16,6 +14,7 @@ CUDSS = "45b445bb-4962-46a0-9369-b4df9d0f772e"
1614
ChainRules = "082447d4-558c-5d27-93f4-14fc19e9eca2"
1715
GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527"
1816
ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
17+
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1918
StaticArraysCore = "1e83bf80-4336-4d27-bf5d-d5a4f845583c"
2019
Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"
2120

@@ -27,6 +26,7 @@ ArrayInterfaceCUDSSExt = "CUDSS"
2726
ArrayInterfaceChainRulesExt = "ChainRules"
2827
ArrayInterfaceGPUArraysCoreExt = "GPUArraysCore"
2928
ArrayInterfaceReverseDiffExt = "ReverseDiff"
29+
ArrayInterfaceSparseArraysExt = "SparseArrays"
3030
ArrayInterfaceStaticArraysCoreExt = "StaticArraysCore"
3131
ArrayInterfaceTrackerExt = "Tracker"
3232

@@ -42,7 +42,6 @@ LinearAlgebra = "1.10"
4242
ReverseDiff = "1"
4343
SparseArrays = "1.10"
4444
StaticArraysCore = "1"
45-
SuiteSparse = "1.10"
4645
Tracker = "0.2"
4746
julia = "1.10"
4847

ext/ArrayInterfaceSparseArraysExt.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module ArrayInterfaceSparseArraysExt
2+
3+
import ArrayInterface: buffer, has_sparsestruct, issingular, findstructralnz, bunchkaufman_instance, DEFAULT_CHOLESKY_PIVOT, cholesky_instance, ldlt_instance, lu_instance, qr_instance
4+
using ArrayInterface.LinearAlgebra
5+
using SparseArrays
6+
7+
buffer(x::SparseMatrixCSC) = getfield(x, :nzval)
8+
buffer(x::SparseVector) = getfield(x, :nzval)
9+
has_sparsestruct(::Type{<:SparseMatrixCSC}) = true
10+
issingular(A::AbstractSparseMatrix) = !issuccess(lu(A, check = false))
11+
12+
function findstructralnz(x::SparseMatrixCSC)
13+
rowind, colind, _ = findnz(x)
14+
(rowind, colind)
15+
end
16+
17+
function bunchkaufman_instance(A::SparseMatrixCSC)
18+
bunchkaufman(sparse(similar(A, 1, 1)), check = false)
19+
end
20+
21+
function cholesky_instance(A::Union{SparseMatrixCSC,Symmetric{<:Number,<:SparseMatrixCSC}}, pivot = DEFAULT_CHOLESKY_PIVOT)
22+
cholesky(sparse(similar(A, 1, 1)), check = false)
23+
end
24+
25+
function ldlt_instance(A::SparseMatrixCSC)
26+
ldlt(sparse(similar(A, 1, 1)), check=false)
27+
end
28+
29+
# Could be optimized but this should work for any real case.
30+
function lu_instance(jac_prototype::SparseMatrixCSC, pivot = DEFAULT_CHOLESKY_PIVOT)
31+
lu(sparse(rand(1,1)))
32+
end
33+
34+
function qr_instance(jac_prototype::SparseMatrixCSC, pivot = DEFAULT_CHOLESKY_PIVOT)
35+
qr(sparse(rand(1,1)))
36+
end
37+
38+
end

src/ArrayInterface.jl

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module ArrayInterface
22

33
using LinearAlgebra
4-
using SparseArrays
5-
using SuiteSparse
64

75
@static if isdefined(Base, Symbol("@assume_effects"))
86
using Base: @assume_effects
@@ -121,8 +119,6 @@ Return the buffer data that `x` points to. Unlike `parent(x::AbstractArray)`, `b
121119
may not return another array type.
122120
"""
123121
buffer(x) = parent(x)
124-
buffer(x::SparseMatrixCSC) = getfield(x, :nzval)
125-
buffer(x::SparseVector) = getfield(x, :nzval)
126122
buffer(@nospecialize x::Union{Base.Slice, Base.IdentityUnitRange}) = getfield(x, :indices)
127123

128124
"""
@@ -308,7 +304,6 @@ Determine whether `findstructralnz` accepts the parameter `x`.
308304
has_sparsestruct(x) = has_sparsestruct(typeof(x))
309305
has_sparsestruct(::Type) = false
310306
has_sparsestruct(::Type{<:AbstractArray}) = false
311-
has_sparsestruct(::Type{<:SparseMatrixCSC}) = true
312307
has_sparsestruct(::Type{<:Diagonal}) = true
313308
has_sparsestruct(::Type{<:Bidiagonal}) = true
314309
has_sparsestruct(::Type{<:Tridiagonal}) = true
@@ -320,7 +315,6 @@ has_sparsestruct(::Type{<:SymTridiagonal}) = true
320315
Determine whether a given abstract matrix is singular.
321316
"""
322317
issingular(A::AbstractMatrix) = issingular(Matrix(A))
323-
issingular(A::AbstractSparseMatrix) = !issuccess(lu(A, check = false))
324318
issingular(A::Matrix) = !issuccess(lu(A, check = false))
325319
issingular(A::UniformScaling) = A.λ == 0
326320
issingular(A::Diagonal) = any(iszero, A.diag)
@@ -359,11 +353,6 @@ function findstructralnz(x::Union{Tridiagonal, SymTridiagonal})
359353
(rowind, colind)
360354
end
361355

362-
function findstructralnz(x::SparseMatrixCSC)
363-
rowind, colind, _ = findnz(x)
364-
(rowind, colind)
365-
end
366-
367356
abstract type ColoringAlgorithm end
368357

369358
"""
@@ -403,9 +392,6 @@ cheaply.
403392
function bunchkaufman_instance(A::Matrix{T}) where T
404393
return bunchkaufman(similar(A, 0, 0), check = false)
405394
end
406-
function bunchkaufman_instance(A::SparseMatrixCSC)
407-
bunchkaufman(sparse(similar(A, 1, 1)), check = false)
408-
end
409395

410396
"""
411397
bunchkaufman_instance(a::Number) -> a
@@ -429,14 +415,10 @@ cholesky_instance(A, pivot = LinearAlgebra.RowMaximum()) -> cholesky_factorizati
429415
Returns an instance of the Cholesky factorization object with the correct type
430416
cheaply.
431417
"""
432-
function cholesky_instance(A::Matrix{T}, pivot = DEFAULT_CHOLESKY_PIVOT) where {T}
418+
function cholesky_instance(A::Matrix{T}, pivot = DEFAULT_CHOLESKY_PIVOT) where {T}
433419
return cholesky(similar(A, 0, 0), pivot, check = false)
434420
end
435421

436-
function cholesky_instance(A::Union{SparseMatrixCSC,Symmetric{<:Number,<:SparseMatrixCSC}}, pivot = DEFAULT_CHOLESKY_PIVOT)
437-
cholesky(sparse(similar(A, 1, 1)), check = false)
438-
end
439-
440422
"""
441423
cholesky_instance(a::Number, pivot = LinearAlgebra.RowMaximum()) -> a
442424
@@ -458,14 +440,10 @@ ldlt_instance(A) -> ldlt_factorization_instance
458440
Returns an instance of the LDLT factorization object with the correct type
459441
cheaply.
460442
"""
461-
function ldlt_instance(A::Matrix{T}) where {T}
443+
function ldlt_instance(A::Matrix{T}) where {T}
462444
return ldlt_instance(SymTridiagonal(similar(A, 0, 0)))
463445
end
464446

465-
function ldlt_instance(A::SparseMatrixCSC)
466-
ldlt(sparse(similar(A, 1, 1)), check=false)
467-
end
468-
469447
function ldlt_instance(A::SymTridiagonal{T,V}) where {T,V}
470448
return LinearAlgebra.LDLt{T,SymTridiagonal{T,V}}(A)
471449
end
@@ -498,9 +476,6 @@ function lu_instance(A::Matrix{T}) where {T}
498476
info = zero(LinearAlgebra.BlasInt)
499477
return LU{luT}(similar(A, 0, 0), ipiv, info)
500478
end
501-
function lu_instance(jac_prototype::SparseMatrixCSC)
502-
SuiteSparse.UMFPACK.UmfpackLU(similar(jac_prototype, 1, 1))
503-
end
504479

505480
function lu_instance(A::Symmetric{T}) where {T}
506481
noUnitT = typeof(zero(T))
@@ -557,11 +532,6 @@ function qr_instance(A::Matrix{BigFloat},pivot = DEFAULT_CHOLESKY_PIVOT)
557532
LinearAlgebra.QR(zeros(BigFloat,0,0),zeros(BigFloat,0))
558533
end
559534

560-
# Could be optimized but this should work for any real case.
561-
function qr_instance(jac_prototype::SparseMatrixCSC, pivot = DEFAULT_CHOLESKY_PIVOT)
562-
qr(sparse(rand(1,1)))
563-
end
564-
565535
"""
566536
qr_instance(a::Number) -> a
567537

0 commit comments

Comments
 (0)