Skip to content

Commit

Permalink
Make sparsearrays into a weak dep ext
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisRackauckas committed Aug 10, 2024
1 parent 3da405c commit cc22efc
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 13 deletions.
6 changes: 4 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
name = "SciMLOperators"
uuid = "c0aeaf25-5076-4817-a8d5-81caf7dfa961"
authors = ["Vedant Puri <[email protected]>"]
version = "0.3.8"
version = "0.3.9"

[deps]
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StaticArraysCore = "1e83bf80-4336-4d27-bf5d-d5a4f845583c"

[weakdeps]
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[compat]
ArrayInterface = "7"
DocStringExtensions = "0.8, 0.9"
Expand Down
17 changes: 17 additions & 0 deletions ext/SciMLOperatorsSparseArraysExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module SciMLOperatorsSparseArraysExt

import SciMLOperators
import SparseArrays: sparse, issparse
import SparseArrays
import LinearAlgebra

SparseArrays.sparse(L::SciMLOperators.MatrixOperator) = sparse(L.A)
SparseArrays.issparse(L::SciMLOperators.MatrixOperator) = issparse(L.A)
SparseArrays.sparse(L::SciMLOperators.ScaledOperator) = L.λ * sparse(L.L)
SparseArrays.sparse(L::SciMLOperators.AddedOperator) = sum(sparse, L.ops)
SparseArrays.sparse(L::SciMLOperators.ComposedOperator) = prod(sparse, L.ops)
function SparseArrays.sparse(L::SciMLOperators.TensorProductOperator)
LinearAlgebra.kron(sparse.(AbstractMatrix, L.ops)...)
end

end
4 changes: 2 additions & 2 deletions src/SciMLOperators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module SciMLOperators
using DocStringExtensions

using LinearAlgebra
import SparseArrays

import StaticArraysCore
import ArrayInterface
import MacroTools: @forward
Expand All @@ -19,7 +19,7 @@ import Base: +, -, *, /, \, ∘, ==, conj, exp, kron
import Base: iszero, inv, adjoint, transpose, size, convert
import LinearAlgebra: mul!, ldiv!, lmul!, rmul!, factorize
import LinearAlgebra: Matrix, Diagonal
import SparseArrays: sparse, issparse


"""
$(TYPEDEF)
Expand Down
3 changes: 0 additions & 3 deletions src/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ Base.:+(L::AbstractSciMLOperator) = L
function Base.convert(::Type{AbstractMatrix}, L::ScaledOperator)
convert(Number, L.λ) * convert(AbstractMatrix, L.L)
end
SparseArrays.sparse(L::ScaledOperator) = L.λ * sparse(L.L)

# traits
function Base.show(io::IO, L::ScaledOperator{T}) where {T}
Expand Down Expand Up @@ -382,7 +381,6 @@ end
function Base.convert(::Type{AbstractMatrix}, L::AddedOperator)
sum(op -> convert(AbstractMatrix, op), L.ops)
end
SparseArrays.sparse(L::AddedOperator) = sum(sparse, L.ops)

# traits
function Base.show(io::IO, L::AddedOperator)
Expand Down Expand Up @@ -540,7 +538,6 @@ end
function Base.convert(::Type{AbstractMatrix}, L::ComposedOperator)
prod(op -> convert(AbstractMatrix, op), L.ops)
end
SparseArrays.sparse(L::ComposedOperator) = prod(sparse, L.ops)

# traits
function Base.show(io::IO, L::ComposedOperator)
Expand Down
3 changes: 0 additions & 3 deletions src/matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,6 @@ function update_coefficients!(L::MatrixOperator, u, p, t; kwargs...)
L.update_func!(L.A, u, p, t; kwargs...)
end

SparseArrays.sparse(L::MatrixOperator) = sparse(L.A)
SparseArrays.issparse(L::MatrixOperator) = issparse(L.A)

# TODO - add tests for MatrixOperator indexing
# propagate_inbounds here for the getindex fallback
Base.@propagate_inbounds Base.convert(::Type{AbstractMatrix}, L::MatrixOperator) = convert(
Expand Down
4 changes: 1 addition & 3 deletions src/tensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ function Base.convert(::Type{AbstractMatrix}, L::TensorProductOperator)
kron(convert.(AbstractMatrix, L.ops)...)
end

function SparseArrays.sparse(L::TensorProductOperator)
kron(sparse.(AbstractMatrix, L.ops)...)
end


#LinearAlgebra.opnorm(L::TensorProductOperator) = prod(opnorm, L.ops)

Expand Down

0 comments on commit cc22efc

Please sign in to comment.