diff --git a/.github/workflows/IntegrationTest.yml b/.github/workflows/IntegrationTest.yml index cfee35c..fda7c09 100644 --- a/.github/workflows/IntegrationTest.yml +++ b/.github/workflows/IntegrationTest.yml @@ -8,6 +8,7 @@ on: paths: - 'Project.toml' pull_request: + types: [opened, synchronize, reopened, ready_for_review, converted_to_draft] paths: - 'Project.toml' diff --git a/.github/workflows/Tests.yml b/.github/workflows/Tests.yml index 5a0a306..7f8c559 100644 --- a/.github/workflows/Tests.yml +++ b/.github/workflows/Tests.yml @@ -9,6 +9,7 @@ on: paths-ignore: - 'docs/**' pull_request: + types: [opened, synchronize, reopened, ready_for_review, converted_to_draft] workflow_dispatch: concurrency: diff --git a/Project.toml b/Project.toml index 2784bf4..1d898af 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "SparseArraysBase" uuid = "0d5efcca-f356-4864-8770-e1ed8d78f208" -version = "0.9.2" +version = "0.9.3" authors = ["ITensor developers and contributors"] [workspace] diff --git a/README.md b/README.md index 46718c0..2932aaf 100644 --- a/README.md +++ b/README.md @@ -42,20 +42,9 @@ julia> Pkg.add("SparseArraysBase") ## Examples ````julia -using SparseArraysBase: - SparseArrayDOK, - SparseMatrixDOK, - SparseVectorDOK, - eachstoredindex, - getstoredindex, - getunstoredindex, - isstored, - setstoredindex!, - setunstoredindex!, - storedlength, - storedpairs, - storedvalues, - zero! +using SparseArraysBase: SparseArrayDOK, SparseMatrixDOK, SparseVectorDOK, eachstoredindex, + getstoredindex, getunstoredindex, isstored, setstoredindex!, setunstoredindex!, + storedlength, storedpairs, storedvalues, zero! using Test: @test, @test_throws a = SparseArrayDOK{Float64}(undef, 2, 2) diff --git a/benchmark/benchmarks.jl b/benchmark/benchmarks.jl index 37e4768..7a00fbd 100644 --- a/benchmark/benchmarks.jl +++ b/benchmark/benchmarks.jl @@ -1,5 +1,5 @@ -using SparseArraysBase using BenchmarkTools +using SparseArraysBase SUITE = BenchmarkGroup() SUITE["rand"] = @benchmarkable rand(10) diff --git a/docs/make.jl b/docs/make.jl index 2d780d5..af887ba 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -1,8 +1,8 @@ -using SparseArraysBase: SparseArraysBase using Documenter: Documenter, DocMeta, deploydocs, makedocs +using SparseArraysBase: SparseArraysBase DocMeta.setdocmeta!( - SparseArraysBase, :DocTestSetup, :(using SparseArraysBase); recursive = true + SparseArraysBase, :DocTestSetup, :(using SparseArraysBase); recursive = true, ) include("make_index.jl") @@ -20,5 +20,5 @@ makedocs(; ) deploydocs(; - repo = "github.com/ITensor/SparseArraysBase.jl", devbranch = "main", push_preview = true + repo = "github.com/ITensor/SparseArraysBase.jl", devbranch = "main", push_preview = true, ) diff --git a/examples/README.jl b/examples/README.jl index fbb85fe..4572484 100644 --- a/examples/README.jl +++ b/examples/README.jl @@ -43,20 +43,9 @@ julia> Pkg.add("SparseArraysBase") # ## Examples -using SparseArraysBase: - SparseArrayDOK, - SparseMatrixDOK, - SparseVectorDOK, - eachstoredindex, - getstoredindex, - getunstoredindex, - isstored, - setstoredindex!, - setunstoredindex!, - storedlength, - storedpairs, - storedvalues, - zero! +using SparseArraysBase: SparseArrayDOK, SparseMatrixDOK, SparseVectorDOK, eachstoredindex, + getstoredindex, getunstoredindex, isstored, setstoredindex!, setunstoredindex!, + storedlength, storedpairs, storedvalues, zero! using Test: @test, @test_throws a = SparseArrayDOK{Float64}(undef, 2, 2) diff --git a/ext/SparseArraysBaseNamedDimsArraysExt/SparseArraysBaseNamedDimsArraysExt.jl b/ext/SparseArraysBaseNamedDimsArraysExt/SparseArraysBaseNamedDimsArraysExt.jl index 023a78c..c29bad0 100644 --- a/ext/SparseArraysBaseNamedDimsArraysExt/SparseArraysBaseNamedDimsArraysExt.jl +++ b/ext/SparseArraysBaseNamedDimsArraysExt/SparseArraysBaseNamedDimsArraysExt.jl @@ -1,7 +1,7 @@ module SparseArraysBaseNamedDimsArraysExt -using NamedDimsArrays: AbstractNamedDimsArray, AbstractNamedUnitRange, denamed, inds, - name, nameddims, nameddimsof +using NamedDimsArrays: AbstractNamedDimsArray, AbstractNamedUnitRange, denamed, inds, name, + nameddims, nameddimsof using SparseArraysBase: SparseArraysBase, dense, oneelement function SparseArraysBase.dense(a::AbstractNamedDimsArray) @@ -11,7 +11,7 @@ function SparseArraysBase.dense(a::AbstractNamedDimsArray) end function SparseArraysBase.oneelement( - value, index::NTuple{N, Int}, ax::NTuple{N, AbstractNamedUnitRange} + value, index::NTuple{N, Int}, ax::NTuple{N, AbstractNamedUnitRange}, ) where {N} return nameddims(oneelement(value, index, denamed.(ax)), name.(ax)) end diff --git a/ext/SparseArraysBaseTensorAlgebraExt/SparseArraysBaseTensorAlgebraExt.jl b/ext/SparseArraysBaseTensorAlgebraExt/SparseArraysBaseTensorAlgebraExt.jl index 192fa85..05f9c7a 100644 --- a/ext/SparseArraysBaseTensorAlgebraExt/SparseArraysBaseTensorAlgebraExt.jl +++ b/ext/SparseArraysBaseTensorAlgebraExt/SparseArraysBaseTensorAlgebraExt.jl @@ -9,7 +9,7 @@ struct SparseArrayFusion <: FusionStyle end TensorAlgebra.FusionStyle(::Type{<:AnyAbstractSparseArray}) = SparseArrayFusion() function TensorAlgebra.matricize( - style::SparseArrayFusion, a::AbstractArray, length_codomain::Val + style::SparseArrayFusion, a::AbstractArray, length_codomain::Val, ) m = matricize(ReshapeFusion(), a, length_codomain) return convert(SparseMatrixCSC, m) diff --git a/src/abstractsparsearray.jl b/src/abstractsparsearray.jl index bcd49ea..1444b53 100644 --- a/src/abstractsparsearray.jl +++ b/src/abstractsparsearray.jl @@ -24,8 +24,9 @@ const AnyAbstractSparseVecOrMat{T} = Union{ Base.convert(T::Type{<:AbstractSparseArray}, a::AbstractArray) = a isa T ? a : T(a) using FunctionImplementations: FunctionImplementations -FunctionImplementations.ImplementationStyle(::Type{<:AnyAbstractSparseArray}) = - SparseArrayImplementationStyle() +function FunctionImplementations.ImplementationStyle(::Type{<:AnyAbstractSparseArray}) + return SparseArrayImplementationStyle() +end function Base.copy(a::AnyAbstractSparseArray) return copyto!(similar(a), a) @@ -51,7 +52,7 @@ function Base.similar(a::AnyAbstractSparseArray, T::Type, ax::Tuple{Vararg{Int}} return similar_sparsearray(a, T, ax) end function Base.similar( - a::AnyAbstractSparseArray, T::Type, ax::Tuple{Integer, Vararg{Integer}} + a::AnyAbstractSparseArray, T::Type, ax::Tuple{Integer, Vararg{Integer}}, ) return similar_sparsearray(a, T, ax) end @@ -145,7 +146,7 @@ function Base.print_array(io::IO, a::AnyAbstractSparseArray) return @invoke Base.print_array(io::typeof(io), a′::AbstractArray{<:Any, ndims(a)}) end function Base.replace_in_print_matrix( - a::AnyAbstractSparseVecOrMat, i::Integer, j::Integer, s::AbstractString + a::AnyAbstractSparseVecOrMat, i::Integer, j::Integer, s::AbstractString, ) return isstored(a, i, j) ? s : Base.replace_with_centered_mark(s) end @@ -261,7 +262,7 @@ See also [`sparserand`](@ref). sparserand!(A::AbstractArray; kwargs...) = sparserand!(default_rng(), A; kwargs...) function sparserand!( - rng::AbstractRNG, A::AbstractArray; density::Real = 0.5, randfun::Function = Random.rand + rng::AbstractRNG, A::AbstractArray; density::Real = 0.5, randfun::Function = Random.rand, ) ArrayLayouts.zero!(A) rand_inds = Random.randsubseq(rng, eachindex(A), density) @@ -273,10 +274,14 @@ end using ArrayLayouts: ArrayLayouts, MemoryLayout using LinearAlgebra: LinearAlgebra, Adjoint -function ArrayLayouts.MemoryLayout(::Type{Transpose{T, P}}) where {T, P <: AbstractSparseMatrix} +function ArrayLayouts.MemoryLayout( + ::Type{Transpose{T, P}}, + ) where {T, P <: AbstractSparseMatrix} return MemoryLayout(P) end -function ArrayLayouts.MemoryLayout(::Type{Adjoint{T, P}}) where {T, P <: AbstractSparseMatrix} +function ArrayLayouts.MemoryLayout( + ::Type{Adjoint{T, P}}, + ) where {T, P <: AbstractSparseMatrix} return MemoryLayout(P) end function LinearAlgebra.mul!( diff --git a/src/abstractsparsearraystyle.jl b/src/abstractsparsearraystyle.jl index b68b74e..18cc7cc 100644 --- a/src/abstractsparsearraystyle.jl +++ b/src/abstractsparsearraystyle.jl @@ -133,7 +133,7 @@ function mul!!( end function mul!!( - a_dest::Number, a1::Number, a2::Number, α::Number = true, β::Number = false + a_dest::Number, a1::Number, a2::Number, α::Number = true, β::Number = false, ) return a1 * a2 * α + a_dest * β end @@ -165,7 +165,7 @@ function _mul!_sparse( end function ArrayLayouts.materialize!( - m::MatMulMatAdd{<:AbstractSparseLayout, <:AbstractSparseLayout, <:AbstractSparseLayout} + m::MatMulMatAdd{<:AbstractSparseLayout, <:AbstractSparseLayout, <:AbstractSparseLayout}, ) _mul!_sparse(m.C, m.A, m.B, m.α, m.β) return m.C diff --git a/src/indexing.jl b/src/indexing.jl index 2805675..069eb50 100644 --- a/src/indexing.jl +++ b/src/indexing.jl @@ -144,26 +144,34 @@ for f in (:isstored, :getunstoredindex, :getstoredindex) end # cartesian indexing - @inline function $_f(::IndexCartesian, A::AbstractArray, I::Vararg{Int, M}) where {M} + @inline function $_f( + ::IndexCartesian, + A::AbstractArray, + I::Vararg{Int, M}, + ) where {M} @boundscheck checkbounds(A, I...) return @inbounds $f(A, Base._to_subscript_indices(A, I...)...) end @inline function $_f( - ::IndexCartesian, A::AbstractArray{<:Any, N}, I::Vararg{Int, N} + ::IndexCartesian, A::AbstractArray{<:Any, N}, I::Vararg{Int, N}, ) where {N} return $f(A, I...) end # errors $_f(::IndexStyle, A::AbstractArray, I...) = error( - "`$($f)` for $("$(typeof(A))") with types $("$(typeof(I))") is not supported" + "`$($f)` for $("$(typeof(A))") with types $("$(typeof(I))") is not supported", ) $error_if_canonical(::IndexLinear, A::AbstractArray, ::Int) = throw( - Base.CanonicalIndexError("$($f)", typeof(A)) + Base.CanonicalIndexError("$($f)", typeof(A)), ) - $error_if_canonical(::IndexCartesian, A::AbstractArray{<:Any, N}, ::Vararg{Int, N}) where {N} = throw( - Base.CanonicalIndexError("$($f)", typeof(A)) + $error_if_canonical( + ::IndexCartesian, + A::AbstractArray{<:Any, N}, + ::Vararg{Int, N}, + ) where {N} = throw( + Base.CanonicalIndexError("$($f)", typeof(A)), ) $error_if_canonical(::IndexStyle, A::AbstractArray, ::Any...) = nothing end @@ -183,32 +191,46 @@ for f! in (:setstoredindex!, :setunstoredindex!) # linear indexing @inline $_f!(::IndexLinear, A::AbstractVector, v, i::Int) = $f!(A, v, i) @inline $_f!(::IndexLinear, A::AbstractArray, v, i::Int) = $f!(A, v, i) - @inline function $_f!(::IndexLinear, A::AbstractArray, v, I::Vararg{Int, M}) where {M} + @inline function $_f!( + ::IndexLinear, + A::AbstractArray, + v, + I::Vararg{Int, M}, + ) where {M} @boundscheck checkbounds(A, I...) return @inbounds $f!(A, v, Base._to_linear_index(A, I...)) end # cartesian indexing - @inline function $_f!(::IndexCartesian, A::AbstractArray, v, I::Vararg{Int, M}) where {M} + @inline function $_f!( + ::IndexCartesian, + A::AbstractArray, + v, + I::Vararg{Int, M}, + ) where {M} @boundscheck checkbounds(A, I...) return @inbounds $f!(A, v, Base._to_subscript_indices(A, I...)...) end @inline function $_f!( - ::IndexCartesian, A::AbstractArray{<:Any, N}, v, I::Vararg{Int, N} + ::IndexCartesian, A::AbstractArray{<:Any, N}, v, I::Vararg{Int, N}, ) where {N} return $f!(A, v, I...) end # errors $_f!(::IndexStyle, A::AbstractArray, I...) = error( - "`$f!` for $("$(typeof(A))") with types $("$(typeof(I))") is not supported" + "`$f!` for $("$(typeof(A))") with types $("$(typeof(I))") is not supported", ) $error_if_canonical(::IndexLinear, A::AbstractArray, ::Int) = throw( - Base.CanonicalIndexError("$($(string(f!)))", typeof(A)) + Base.CanonicalIndexError("$($(string(f!)))", typeof(A)), ) - $error_if_canonical(::IndexCartesian, A::AbstractArray{<:Any, N}, ::Vararg{Int, N}) where {N} = throw( - Base.CanonicalIndexError("$($f!)", typeof(A)) + $error_if_canonical( + ::IndexCartesian, + A::AbstractArray{<:Any, N}, + ::Vararg{Int, N}, + ) where {N} = throw( + Base.CanonicalIndexError("$($f!)", typeof(A)), ) $error_if_canonical(::IndexStyle, A::AbstractArray, ::Any...) = nothing end @@ -243,7 +265,11 @@ end function (::Implementation{typeof(eachstoredindex)})(A::AbstractArray, B::AbstractArray...) return eachstoredindex(IndexStyle(A, B...), A, B...) end -function (::Implementation{typeof(eachstoredindex)})(style::IndexStyle, A::AbstractArray, B::AbstractArray...) +function (::Implementation{typeof(eachstoredindex)})( + style::IndexStyle, + A::AbstractArray, + B::AbstractArray..., + ) return eachindex(style, A, B...) end @@ -257,7 +283,7 @@ end # so no errors at this level by defining both IndexLinear and IndexCartesian const getindex_sparse = sparse_style(getindex) function getindex_sparse( - A::AbstractArray{<:Any, N}, I::Vararg{Int, N} + A::AbstractArray{<:Any, N}, I::Vararg{Int, N}, ) where {N} @_propagate_inbounds_meta @boundscheck checkbounds(A, I...) # generally isstored requires bounds checking @@ -276,7 +302,7 @@ function getindex_sparse(A::AbstractVector, I::Int) end # TODO: Make this more general, use `Base.to_index`. function getindex_sparse( - a::AbstractArray{<:Any, N}, I::CartesianIndex{N} + a::AbstractArray{<:Any, N}, I::CartesianIndex{N}, ) where {N} return getindex_sparse(a, Tuple(I)...) end @@ -287,7 +313,7 @@ end const setindex!_sparse = sparse_style(setindex!) function setindex!_sparse( - A::AbstractArray{<:Any, N}, v, I::Vararg{Int, N} + A::AbstractArray{<:Any, N}, v, I::Vararg{Int, N}, ) where {N} @_propagate_inbounds_meta @boundscheck checkbounds(A, I...) @@ -298,7 +324,7 @@ function setindex!_sparse( end end function setindex!_sparse( - A::AbstractArray, v, I::Int + A::AbstractArray, v, I::Int, ) @_propagate_inbounds_meta @boundscheck checkbounds(A, I) @@ -310,7 +336,7 @@ function setindex!_sparse( end # disambiguate vectors function setindex!_sparse( - A::AbstractVector, v, I::Int + A::AbstractVector, v, I::Int, ) @_propagate_inbounds_meta @boundscheck checkbounds(A, I) @@ -322,7 +348,7 @@ function setindex!_sparse( end # TODO: Make this more general, use `Base.to_index`. function setindex!_sparse( - a::AbstractArray{<:Any, N}, value, I::CartesianIndex{N} + a::AbstractArray{<:Any, N}, value, I::CartesianIndex{N}, ) where {N} return setindex!(a, value, Tuple(I)...) end @@ -339,7 +365,7 @@ end # required: one implementation for canonical index style const eachstoredindex_sparse = sparse_style(eachstoredindex) function eachstoredindex_sparse( - style::IndexStyle, A::AbstractArray + style::IndexStyle, A::AbstractArray, ) error_if_canonical_eachstoredindex(style, A) inds = eachstoredindex(A) @@ -356,7 +382,7 @@ end # derived but may be specialized: function eachstoredindex_sparse( - style::IndexStyle, A::AbstractArray, B::AbstractArray... + style::IndexStyle, A::AbstractArray, B::AbstractArray..., ) return union(map(Base.Fix1(eachstoredindex, style), (A, B...))...) end @@ -389,7 +415,7 @@ end const getunstoredindex_sparse = sparse_style(getunstoredindex) function getunstoredindex_sparse( - A::AbstractArray, I::Int... + A::AbstractArray, I::Int..., ) @_propagate_inbounds_meta style = IndexStyle(A) @@ -412,7 +438,7 @@ end const getstoredindex_sparse = sparse_style(getstoredindex) function getstoredindex_sparse( - A::AbstractArray, I::Int... + A::AbstractArray, I::Int..., ) @_propagate_inbounds_meta style = IndexStyle(A) @@ -450,12 +476,12 @@ eg: ArrayLayouts.@layoutgetindex ArrayType TODO: decide if we need the interface approach at all here =# for (Tr, Tc) in Iterators.product( - Iterators.repeated((:Colon, :AbstractUnitRange, :AbstractVector, :Integer), 2)... + Iterators.repeated((:Colon, :AbstractUnitRange, :AbstractVector, :Integer), 2)..., ) Tr === Tc === :Integer && continue @eval begin function getindex_sparse( - A::AbstractMatrix, kr::$Tr, jr::$Tc + A::AbstractMatrix, kr::$Tr, jr::$Tc, ) Base.@inline # needed to make boundschecks work return ArrayLayouts.layout_getindex(A, kr, jr) diff --git a/src/map.jl b/src/map.jl index d44e337..80dced7 100644 --- a/src/map.jl +++ b/src/map.jl @@ -5,14 +5,15 @@ Holy Trait to indicate how a function interacts with abstract zero values: -- `StrongPreserving` : output is guaranteed to be zero if **any** input is. -- `WeakPreserving` : output is guaranteed to be zero if **all** inputs are. -- `NonPreserving` : no guarantees on output. + - `StrongPreserving` : output is guaranteed to be zero if **any** input is. + - `WeakPreserving` : output is guaranteed to be zero if **all** inputs are. + - `NonPreserving` : no guarantees on output. To attempt to automatically determine this, either `ZeroPreserving(f, A::AbstractArray...)` or `ZeroPreserving(f, T::Type...)` can be used/overloaded. !!! warning + incorrectly registering a function to be zero-preserving will lead to silently wrong results. """ abstract type ZeroPreserving <: Function end @@ -69,7 +70,7 @@ end # ------ const map_sparse = sparse_style(map) function map_sparse( - f, A::AbstractArray, Bs::AbstractArray... + f, A::AbstractArray, Bs::AbstractArray..., ) f_pres = ZeroPreserving(f, A, Bs...) return map_sparse(f_pres, A, Bs...) @@ -90,7 +91,7 @@ end const map!_sparse = sparse_style(map!) function map!_sparse( - f, C::AbstractArray, A::AbstractArray, Bs::AbstractArray... + f, C::AbstractArray, A::AbstractArray, Bs::AbstractArray..., ) f_pres = ZeroPreserving(f, A, Bs...) return map!_sparse(f_pres, C, A, Bs...) @@ -98,7 +99,7 @@ end # This isn't an overload of `Base.map!` since that leads to ambiguity errors. function map!_sparse( - f::ZeroPreserving, C::AbstractArray, A::AbstractArray, Bs::AbstractArray... + f::ZeroPreserving, C::AbstractArray, A::AbstractArray, Bs::AbstractArray..., ) checkshape(C, A, Bs...) unaliased = map(Base.Fix1(Base.unalias, C), (A, Bs...)) @@ -125,7 +126,7 @@ end # ----------------- const copyto!_sparse = sparse_style(copyto!) function copyto!_sparse( - dest::AbstractArray, src::AbstractArray + dest::AbstractArray, src::AbstractArray, ) map!_sparse(identity, dest, src) return dest @@ -133,7 +134,7 @@ end const permutedims!_sparse = sparse_style(permutedims!) function permutedims!_sparse( - a_dest::AbstractArray, a_src::AbstractArray, perm + a_dest::AbstractArray, a_src::AbstractArray, perm, ) return map!(identity, a_dest, PermutedDimsArray(a_src, perm)) end @@ -143,7 +144,7 @@ function map_stored! end const map_stored!_sparse = sparse_style(map_stored!) function map_stored!_sparse( - f, a_dest::AbstractArray, as::AbstractArray... + f, a_dest::AbstractArray, as::AbstractArray..., ) map!_sparse(WeakPreserving(f), a_dest, as...) return a_dest @@ -154,7 +155,7 @@ function map_all! end const map_all!_sparse = sparse_style(map_all!) function map_all!_sparse( - f, a_dest::AbstractArray, as::AbstractArray... + f, a_dest::AbstractArray, as::AbstractArray..., ) map!_sparse(NonPreserving(f), a_dest, as...) return a_dest diff --git a/src/oneelementarray.jl b/src/oneelementarray.jl index eda8443..3799406 100644 --- a/src/oneelementarray.jl +++ b/src/oneelementarray.jl @@ -4,12 +4,13 @@ function _OneElementArray end # Like [`FillArrays.OneElement`](https://github.com/JuliaArrays/FillArrays.jl) # and [`OneHotArrays.OneHotArray`](https://github.com/FluxML/OneHotArrays.jl). -struct OneElementArray{T, N, I, Unstored <: AbstractArray{T, N}} <: AbstractSparseArray{T, N} +struct OneElementArray{T, N, I, Unstored <: AbstractArray{T, N}} <: + AbstractSparseArray{T, N} value::T index::I unstored::Unstored global @inline function _OneElementArray( - value::T, index::I, unstored::Unstored + value::T, index::I, unstored::Unstored, ) where {T, N, I, Unstored <: AbstractArray{T, N}} @assert N == length(index) return new{T, N, I, Unstored}(value, index, unstored) @@ -20,66 +21,73 @@ const OneElementVector{T} = OneElementArray{T, 1} const OneElementMatrix{T} = OneElementArray{T, 2} function OneElementArray{T, N}( - value, index::NTuple{N, Int}, axes::NTuple{N, AbstractUnitRange} + value, index::NTuple{N, Int}, axes::NTuple{N, AbstractUnitRange}, ) where {T, N} return _OneElementArray(convert(T, value), index, Zeros{T}(axes)) end function OneElementArray{<:Any, N}( - value::T, index::NTuple{N, Int}, axes::NTuple{N, AbstractUnitRange} + value::T, index::NTuple{N, Int}, axes::NTuple{N, AbstractUnitRange}, ) where {T, N} return OneElementArray{T, N}(value, index, axes) end function OneElementArray( - value::T, index::NTuple{N, Int}, axes::NTuple{N, AbstractUnitRange} + value::T, index::NTuple{N, Int}, axes::NTuple{N, AbstractUnitRange}, ) where {T, N} return OneElementArray{T, N}(value, index, axes) end function OneElementArray{T, N}( - index::NTuple{N, Int}, axes::NTuple{N, AbstractUnitRange} + index::NTuple{N, Int}, axes::NTuple{N, AbstractUnitRange}, ) where {T, N} return OneElementArray{T, N}(one(T), index, axes) end function OneElementArray{<:Any, N}( - index::NTuple{N, Int}, axes::NTuple{N, AbstractUnitRange} + index::NTuple{N, Int}, axes::NTuple{N, AbstractUnitRange}, ) where {N} return OneElementArray{Bool, N}(index, axes) end function OneElementArray{T}( - index::NTuple{N, Int}, axes::NTuple{N, AbstractUnitRange} + index::NTuple{N, Int}, axes::NTuple{N, AbstractUnitRange}, ) where {T, N} return OneElementArray{T, N}(index, axes) end -function OneElementArray(index::NTuple{N, Int}, axes::NTuple{N, AbstractUnitRange}) where {N} +function OneElementArray( + index::NTuple{N, Int}, + axes::NTuple{N, AbstractUnitRange}, + ) where {N} return OneElementArray{Bool, N}(index, axes) end function OneElementArray{T, N}( - value, ax_ind::Vararg{Pair{<:AbstractUnitRange, Int}, N} + value, ax_ind::Vararg{Pair{<:AbstractUnitRange, Int}, N}, ) where {T, N} return OneElementArray{T, N}(value, last.(ax_ind), first.(ax_ind)) end function OneElementArray{<:Any, N}( - value::T, ax_ind::Vararg{Pair{<:AbstractUnitRange, Int}, N} + value::T, ax_ind::Vararg{Pair{<:AbstractUnitRange, Int}, N}, ) where {T, N} return OneElementArray{T, N}(value, ax_ind...) end function OneElementArray{T}( - value, ax_ind::Vararg{Pair{<:AbstractUnitRange, Int}, N} + value, ax_ind::Vararg{Pair{<:AbstractUnitRange, Int}, N}, ) where {T, N} return OneElementArray{T, N}(value, ax_ind...) end function OneElementArray( - value::T, ax_ind::Vararg{Pair{<:AbstractUnitRange, Int}, N} + value::T, ax_ind::Vararg{Pair{<:AbstractUnitRange, Int}, N}, ) where {T, N} return OneElementArray{T, N}(value, ax_ind...) end -function OneElementArray{T, N}(ax_ind::Vararg{Pair{<:AbstractUnitRange, Int}, N}) where {T, N} +function OneElementArray{T, N}( + ax_ind::Vararg{Pair{<:AbstractUnitRange, Int}, N}, + ) where {T, N} return OneElementArray{T, N}(last.(ax_ind), first.(ax_ind)) end -function OneElementArray{<:Any, N}(ax_ind::Vararg{Pair{<:AbstractUnitRange, Int}, N}) where {N} +function OneElementArray{<:Any, N}( + ax_ind::Vararg{Pair{<:AbstractUnitRange, Int}, N}, + ) where {N} return OneElementArray{Bool, N}(ax_ind...) end function OneElementArray{T}(ax_ind::Vararg{Pair{<:AbstractUnitRange, Int}, N}) where {T, N} @@ -144,22 +152,22 @@ function OneElementArray() end function OneElementArray{T, N}( - value, index::NTuple{N, Int}, size::NTuple{N, Integer} + value, index::NTuple{N, Int}, size::NTuple{N, Integer}, ) where {T, N} return OneElementArray{T, N}(value, index, Base.oneto.(size)) end function OneElementArray{<:Any, N}( - value::T, index::NTuple{N, Int}, size::NTuple{N, Integer} + value::T, index::NTuple{N, Int}, size::NTuple{N, Integer}, ) where {T, N} return OneElementArray{T, N}(value, index, size) end function OneElementArray{T}( - value, index::NTuple{N, Int}, size::NTuple{N, Integer} + value, index::NTuple{N, Int}, size::NTuple{N, Integer}, ) where {T, N} return OneElementArray{T, N}(value, index, size) end function OneElementArray( - value::T, index::NTuple{N, Int}, size::NTuple{N, Integer} + value::T, index::NTuple{N, Int}, size::NTuple{N, Integer}, ) where {T, N} return OneElementArray{T, N}(value, index, Base.oneto.(size)) end @@ -167,7 +175,10 @@ end function OneElementArray{T, N}(index::NTuple{N, Int}, size::NTuple{N, Integer}) where {T, N} return OneElementArray{T, N}(one(T), index, size) end -function OneElementArray{<:Any, N}(index::NTuple{N, Int}, size::NTuple{N, Integer}) where {N} +function OneElementArray{<:Any, N}( + index::NTuple{N, Int}, + size::NTuple{N, Integer}, + ) where {N} return OneElementArray{Bool, N}(index, size) end function OneElementArray{T}(index::NTuple{N, Int}, size::NTuple{N, Integer}) where {T, N} @@ -206,12 +217,12 @@ end # Interface to overload for constructing arrays like `OneElementArray`, # that may not be `OneElementArray` (i.e. wrapped versions). function oneelement( - value, index::NTuple{N, Int}, axes::NTuple{N, AbstractUnitRange} + value, index::NTuple{N, Int}, axes::NTuple{N, AbstractUnitRange}, ) where {N} return OneElementArray(value, index, axes) end function oneelement( - eltype::Type, index::NTuple{N, Int}, axes::NTuple{N, AbstractUnitRange} + eltype::Type, index::NTuple{N, Int}, axes::NTuple{N, AbstractUnitRange}, ) where {N} return oneelement(one(eltype), index, axes) end diff --git a/src/sparsearraydok.jl b/src/sparsearraydok.jl index 44217d3..7aa84b1 100644 --- a/src/sparsearraydok.jl +++ b/src/sparsearraydok.jl @@ -1,6 +1,6 @@ using Accessors: @set -using FunctionImplementations: FunctionImplementations, zero! using Dictionaries: Dictionary, IndexError, set! +using FunctionImplementations: FunctionImplementations, zero! const DOKStorage{T, N} = Dictionary{CartesianIndex{N}, T} @@ -16,7 +16,7 @@ struct SparseArrayDOK{T, N, Unstored <: AbstractArray{T, N}} <: AbstractSparseAr storage::DOKStorage{T, N} unstored::Unstored global @inline function _SparseArrayDOK( - storage::DOKStorage{T, N}, unstored::Unstored + storage::DOKStorage{T, N}, unstored::Unstored, ) where {T, N, Unstored <: AbstractArray{T, N}} return new{T, N, Unstored}(storage, unstored) end @@ -87,7 +87,7 @@ end return unstored(a)[I...] end @inline function setstoredindex!( - a::SparseArrayDOK{<:Any, N}, value, I::Vararg{Int, N} + a::SparseArrayDOK{<:Any, N}, value, I::Vararg{Int, N}, ) where {N} # `isstored` includes a boundscheck as well @boundscheck isstored(a, I...) || @@ -97,7 +97,7 @@ end return a end @inline function setunstoredindex!( - a::SparseArrayDOK{<:Any, N}, value, I::Vararg{Int, N} + a::SparseArrayDOK{<:Any, N}, value, I::Vararg{Int, N}, ) where {N} @boundscheck checkbounds(a, I...) insert!(storage(a), CartesianIndex(I), value) diff --git a/src/sparsearrays.jl b/src/sparsearrays.jl index e8f7b7c..8f87bb3 100644 --- a/src/sparsearrays.jl +++ b/src/sparsearrays.jl @@ -21,12 +21,12 @@ function SparseArrayDOK(a::Base.ReshapedArray{<:Any, <:Any, <:AbstractSparseMatr return SparseArrayDOK{eltype(a), ndims(a)}(a) end function SparseArrayDOK{T}( - a::Base.ReshapedArray{<:Any, <:Any, <:AbstractSparseMatrixCSC} + a::Base.ReshapedArray{<:Any, <:Any, <:AbstractSparseMatrixCSC}, ) where {T} return SparseArrayDOK{T, ndims(a)}(a) end function SparseArrayDOK{T, N}( - a::Base.ReshapedArray{<:Any, N, <:AbstractSparseMatrixCSC} + a::Base.ReshapedArray{<:Any, N, <:AbstractSparseMatrixCSC}, ) where {T, N} a′ = SparseArrayDOK{T, N}(undef, size(a)) for I in eachstoredindex(a) diff --git a/src/sparsearraystyle.jl b/src/sparsearraystyle.jl index e4b3b37..8a4ac2f 100644 --- a/src/sparsearraystyle.jl +++ b/src/sparsearraystyle.jl @@ -66,7 +66,7 @@ end const mapreduce_sparse = sparse_style(mapreduce) function mapreduce_sparse( - f, op, as::AbstractArray...; init = reduce_init(f, op, as...), kwargs... + f, op, as::AbstractArray...; init = reduce_init(f, op, as...), kwargs..., ) # TODO: Generalize this. @assert isone(length(as)) @@ -86,7 +86,7 @@ using MapBroadcast: Mapped # TODO: Look into `SparseArrays.capturescalars`: # https://github.com/JuliaSparse/SparseArrays.jl/blob/1beb0e4a4618b0399907b0000c43d9f66d34accc/src/higherorderfns.jl#L1092-L1102 function Base.copyto!( - a_dest::AbstractArray, bc::Base.Broadcast.Broadcasted{<:SparseArrayStyle} + a_dest::AbstractArray, bc::Base.Broadcast.Broadcasted{<:SparseArrayStyle}, ) m = Mapped(bc) map!(m.f, a_dest, m.args...) @@ -94,7 +94,7 @@ function Base.copyto!( end function Base.similar( - bc::Base.Broadcast.Broadcasted{<:SparseArrayStyle}, elt::Type, ax + bc::Base.Broadcast.Broadcasted{<:SparseArrayStyle}, elt::Type, ax, ) return similar(SparseArrayDOK{elt}, ax) end @@ -102,7 +102,8 @@ end using ArrayLayouts: ArrayLayouts const mul!_sparse = sparse_style(mul!) function mul!_sparse( - a_dest::AbstractVecOrMat, a1::AbstractVecOrMat, a2::AbstractVecOrMat, α::Number, β::Number + a_dest::AbstractVecOrMat, a1::AbstractVecOrMat, a2::AbstractVecOrMat, α::Number, + β::Number, ) return ArrayLayouts.mul!(a_dest, a1, a2, α, β) end diff --git a/src/wrappers.jl b/src/wrappers.jl index e94fc95..2c0ed61 100644 --- a/src/wrappers.jl +++ b/src/wrappers.jl @@ -53,10 +53,16 @@ end perm(::PermutedDimsArray{<:Any, <:Any, p}) where {p} = p iperm(::PermutedDimsArray{<:Any, <:Any, <:Any, ip}) where {ip} = ip -function index_to_parentindex(a::PermutedDimsArray{<:Any, N}, I::CartesianIndex{N}) where {N} +function index_to_parentindex( + a::PermutedDimsArray{<:Any, N}, + I::CartesianIndex{N}, + ) where {N} return CartesianIndex(genperm(I, iperm(a))) end -function parentindex_to_index(a::PermutedDimsArray{<:Any, N}, I::CartesianIndex{N}) where {N} +function parentindex_to_index( + a::PermutedDimsArray{<:Any, N}, + I::CartesianIndex{N}, + ) where {N} return CartesianIndex(genperm(I, perm(a))) end @@ -156,34 +162,34 @@ for type in (:Adjoint, :PermutedDimsArray, :ReshapedArray, :SubArray, :Transpose return map(Base.Fix1(parentindex_to_index, a), eachstoredparentindex(a)) end function eachstoredindex_sparse( - style::IndexStyle, a::$type + style::IndexStyle, a::$type, ) # TODO: Make lazy with `Iterators.map`. return map(Base.Fix1(parentindex_to_index, a), eachstoredparentindex(style, a)) end function getstoredindex_sparse(a::$type, I::Int...) return parentvalue_to_value( - a, getstoredindex(parent(a), index_to_parentindex(a, I...)...) + a, getstoredindex(parent(a), index_to_parentindex(a, I...)...), ) end function getunstoredindex_sparse(a::$type, I::Int...) return parentvalue_to_value( - a, getunstoredindex(parent(a), index_to_parentindex(a, I...)...) + a, getunstoredindex(parent(a), index_to_parentindex(a, I...)...), ) end function setstoredindex!_sparse( - a::$type, value, I::Int... + a::$type, value, I::Int..., ) setstoredindex!( - parent(a), value_to_parentvalue(a, value), index_to_parentindex(a, I...)... + parent(a), value_to_parentvalue(a, value), index_to_parentindex(a, I...)..., ) return a end function setunstoredindex!_sparse( - a::$type, value, I::Int... + a::$type, value, I::Int..., ) setunstoredindex!( - parent(a), value_to_parentvalue(a, value), index_to_parentindex(a, I...)... + parent(a), value_to_parentvalue(a, value), index_to_parentindex(a, I...)..., ) return a end diff --git a/test/runtests.jl b/test/runtests.jl index 0008050..fe78bec 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -13,11 +13,16 @@ const GROUP = uppercase( end, ) -"match files of the form `test_*.jl`, but exclude `*setup*.jl`" +""" +match files of the form `test_*.jl`, but exclude `*setup*.jl` +""" function istestfile(fn) - return endswith(fn, ".jl") && startswith(basename(fn), "test_") && !contains(fn, "setup") + return endswith(fn, ".jl") && startswith(basename(fn), "test_") && + !contains(fn, "setup") end -"match files of the form `*.jl`, but exclude `*_notest.jl` and `*setup*.jl`" +""" +match files of the form `*.jl`, but exclude `*_notest.jl` and `*setup*.jl` +""" function isexamplefile(fn) return endswith(fn, ".jl") && !endswith(fn, "_notest.jl") && !contains(fn, "setup") end diff --git a/test/test_aqua.jl b/test/test_aqua.jl index 66974b6..6117287 100644 --- a/test/test_aqua.jl +++ b/test/test_aqua.jl @@ -1,5 +1,5 @@ -using SparseArraysBase: SparseArraysBase using Aqua: Aqua +using SparseArraysBase: SparseArraysBase using Test: @testset @testset "Code quality (Aqua.jl)" begin diff --git a/test/test_basics.jl b/test/test_basics.jl index 3145f62..89aa331 100644 --- a/test/test_basics.jl +++ b/test/test_basics.jl @@ -1,16 +1,7 @@ using Adapt: adapt -using JLArrays: JLArray, @allowscalar -using SparseArraysBase: - SparseArraysBase, - eachstoredindex, - getstoredindex, - getunstoredindex, - isstored, - setstoredindex!, - setunstoredindex!, - storedlength, - storedpairs, - storedvalues +using JLArrays: @allowscalar, JLArray +using SparseArraysBase: SparseArraysBase, eachstoredindex, getstoredindex, getunstoredindex, + isstored, setstoredindex!, setunstoredindex!, storedlength, storedpairs, storedvalues using Test: @test, @test_throws, @testset elts = (Float32, Float64, Complex{Float32}, Complex{Float64}) diff --git a/test/test_dense.jl b/test/test_dense.jl index 8b61b1d..920eb24 100644 --- a/test/test_dense.jl +++ b/test/test_dense.jl @@ -33,7 +33,8 @@ arrayts = (Array, JLArray) @testset "Custom sparse array" begin s = TestDenseUtils.MySparseArrayDOK( - dev(elt[2, 4]), Dict([CartesianIndex(1, 2) => 1, CartesianIndex(3, 4) => 2]), (3, 4) + dev(elt[2, 4]), + Dict([CartesianIndex(1, 2) => 1, CartesianIndex(3, 4) => 2]), (3, 4), ) d = dense(s) @test d isa arrayt{elt, 2} diff --git a/test/test_diagonal.jl b/test/test_diagonal.jl index 8e85539..3a5155f 100644 --- a/test/test_diagonal.jl +++ b/test/test_diagonal.jl @@ -1,14 +1,6 @@ using LinearAlgebra: Diagonal, diagind -using SparseArraysBase: - eachstoredindex, - getstoredindex, - getunstoredindex, - setstoredindex!, - isstored, - storedlength, - storedpairs, - storedvalues - +using SparseArraysBase: eachstoredindex, getstoredindex, getunstoredindex, isstored, + setstoredindex!, storedlength, storedpairs, storedvalues using Test: @test, @testset # compat with LTS: diff --git a/test/test_linalg.jl b/test/test_linalg.jl index 4426fb9..1af8807 100644 --- a/test/test_linalg.jl +++ b/test/test_linalg.jl @@ -1,5 +1,5 @@ -using SparseArraysBase: sparserand using LinearAlgebra: mul! +using SparseArraysBase: sparserand using StableRNGs: StableRNG using Test: @test, @testset diff --git a/test/test_nameddimsarraysext.jl b/test/test_nameddimsarraysext.jl index 2178982..85de589 100644 --- a/test/test_nameddimsarraysext.jl +++ b/test/test_nameddimsarraysext.jl @@ -1,4 +1,4 @@ -using NamedDimsArrays: NamedDimsArray, denamed, nameddims, namedoneto, inds +using NamedDimsArrays: NamedDimsArray, denamed, inds, nameddims, namedoneto using SparseArraysBase: dense, oneelement, sparsezeros using Test: @test, @testset diff --git a/test/test_oneelementarray.jl b/test/test_oneelementarray.jl index b2ace03..e8acb7d 100644 --- a/test/test_oneelementarray.jl +++ b/test/test_oneelementarray.jl @@ -1,13 +1,5 @@ -using SparseArraysBase: - OneElementArray, - OneElementMatrix, - OneElementVector, - eachstoredindex, - isstored, - oneelement, - storedlength, - storedpairs, - storedvalues +using SparseArraysBase: OneElementArray, OneElementMatrix, OneElementVector, + eachstoredindex, isstored, oneelement, storedlength, storedpairs, storedvalues using Test: @test, @test_broken, @testset elts = (Float32, Float64, Complex{Float32}, Complex{Float64}) diff --git a/test/test_sparse_style.jl b/test/test_sparse_style.jl index 103ac15..1e3fbab 100644 --- a/test/test_sparse_style.jl +++ b/test/test_sparse_style.jl @@ -10,20 +10,33 @@ module TestSparseImplementationStyleUtils struct MySparseArray{T, N} <: AbstractSparseArray{T, N} size::NTuple{N, Int} end - FunctionImplementations.ImplementationStyle(::Type{<:MySparseArray}) = - MySparseArrayImplementationStyle() + function FunctionImplementations.ImplementationStyle(::Type{<:MySparseArray}) + return MySparseArrayImplementationStyle() + end end @testset "Combine Sparse Styles" begin @test sparse_style ≡ SparseArrayImplementationStyle() - @test ImplementationStyle(SparseArrayImplementationStyle(), SparseArrayImplementationStyle()) ≡ SparseArrayImplementationStyle() - @test ImplementationStyle(TestSparseImplementationStyleUtils.MySparseArrayImplementationStyle(), SparseArrayImplementationStyle()) ≡ + @test ImplementationStyle( + SparseArrayImplementationStyle(), + SparseArrayImplementationStyle(), + ) ≡ SparseArrayImplementationStyle() + @test ImplementationStyle( + TestSparseImplementationStyleUtils.MySparseArrayImplementationStyle(), + SparseArrayImplementationStyle(), + ) ≡ SparseArrayImplementationStyle() - @test ImplementationStyle(SparseArrayImplementationStyle(), TestSparseImplementationStyleUtils.MySparseArrayImplementationStyle()) ≡ + @test ImplementationStyle( + SparseArrayImplementationStyle(), + TestSparseImplementationStyleUtils.MySparseArrayImplementationStyle(), + ) ≡ SparseArrayImplementationStyle() @test style(TestSparseImplementationStyleUtils.MySparseArray{Float64, 2}((2, 2))) ≡ TestSparseImplementationStyleUtils.MySparseArrayImplementationStyle() - @test style(sparsezeros(2, 2), TestSparseImplementationStyleUtils.MySparseArray{Float64, 2}((2, 2))) ≡ + @test style( + sparsezeros(2, 2), + TestSparseImplementationStyleUtils.MySparseArray{Float64, 2}((2, 2)), + ) ≡ SparseArrayImplementationStyle() # Regression tests for ambiguity caused by combining AbstractSparseArrayStyle with # DefaultArrayStyle. @@ -32,6 +45,9 @@ end DefaultArrayImplementationStyle(), ) ≡ TestSparseImplementationStyleUtils.MySparseArrayImplementationStyle() - @test style(TestSparseImplementationStyleUtils.MySparseArray{Float64, 2}((2, 2)), randn(2, 2)) ≡ + @test style( + TestSparseImplementationStyleUtils.MySparseArray{Float64, 2}((2, 2)), + randn(2, 2), + ) ≡ TestSparseImplementationStyleUtils.MySparseArrayImplementationStyle() end diff --git a/test/test_sparsearraydok.jl b/test/test_sparsearraydok.jl index bd1b20c..3162d89 100644 --- a/test/test_sparsearraydok.jl +++ b/test/test_sparsearraydok.jl @@ -2,23 +2,10 @@ using Adapt: adapt using ArrayLayouts: zero! using Dictionaries: Dictionary using FillArrays: Zeros -using JLArrays: JLArray, @allowscalar -using SparseArraysBase: - SparseArraysBase, - SparseArrayDOK, - SparseMatrixDOK, - eachstoredindex, - getstoredindex, - getunstoredindex, - isstored, - setstoredindex!, - setunstoredindex!, - sparse, - sparserand, - sparsezeros, - storedlength, - storedpairs, - storedvalues +using JLArrays: @allowscalar, JLArray +using SparseArraysBase: SparseArraysBase, SparseArrayDOK, SparseMatrixDOK, eachstoredindex, + getstoredindex, getunstoredindex, isstored, setstoredindex!, setunstoredindex!, sparse, + sparserand, sparsezeros, storedlength, storedpairs, storedvalues using StableRNGs: StableRNG using Test: @test, @test_throws, @testset diff --git a/test/test_tensoralgebraext.jl b/test/test_tensoralgebraext.jl index 8921e31..59b5fcd 100644 --- a/test/test_tensoralgebraext.jl +++ b/test/test_tensoralgebraext.jl @@ -1,8 +1,8 @@ using SparseArrays: SparseMatrixCSC, findnz, nnz -using SparseArraysBase: SparseMatrixDOK, eachstoredindex, isstored, sparsezeros, - storedlength +using SparseArraysBase: + SparseMatrixDOK, eachstoredindex, isstored, sparsezeros, storedlength using TensorAlgebra: contract, matricize -using Test: @testset, @test +using Test: @test, @testset @testset "TensorAlgebraExt (eltype = $elt)" for elt in (Float32, ComplexF64) a = sparsezeros(elt, (2, 2, 2)) diff --git a/test/test_unstored.jl b/test/test_unstored.jl index 9125056..349a044 100644 --- a/test/test_unstored.jl +++ b/test/test_unstored.jl @@ -1,5 +1,5 @@ -using SparseArraysBase: Unstored using FillArrays: Zeros +using SparseArraysBase: Unstored using Test: @test, @testset @testset "Unstored" begin