Skip to content

Commit b91dfd2

Browse files
upstream more traits
1 parent 0985be7 commit b91dfd2

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@ and exported in a future Base Julia there will be no issues with the upgrade.
1616
A trait function for whether `x` is a mutable or immutable array. Used for
1717
dispatching to in-place and out-of-place versions of functions.
1818

19+
## isstructured(x)
20+
21+
A trait function for whether a matrix `x` is a sparse structured matrix.
22+
23+
## can_setindex(x)
24+
25+
A trait function for whether an array `x` can use `setindex!`
26+
27+
## has_sparsestruct(x)
28+
29+
Determine whether `findstructralnz` accepts the parameter `x`
30+
31+
## findstructralnz(x)
32+
33+
Returns iterators `(I,J)` of the non-zeros in the structure of the matrix `x`.
34+
The same as the to first two elements of `findnz(::SparseMatrixCSC)``
35+
1936
## List of things to add
2037

2138
- https://github.com/JuliaLang/julia/issues/22216

src/ArrayInterface.jl

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ using Requires
44
using LinearAlgebra
55
using SparseArrays
66

7-
export findstructralnz,has_sparsestruct
8-
97
function ismutable end
108

119
"""
@@ -20,6 +18,28 @@ ismutable(x) = ismutable(typeof(x))
2018
ismutable(::Type{<:Array}) = true
2119
ismutable(::Type{<:Number}) = false
2220

21+
"""
22+
can_setindex(x::DataType)
23+
24+
Query whether a type can use `setindex!`
25+
"""
26+
can_setindex(x) = true
27+
28+
"""
29+
isstructured(x::DataType)
30+
31+
Query whether a type is a representation of a structured matrix
32+
"""
33+
isstructured(x) = false
34+
isstructured(::Symmetric) = true
35+
isstructured(::Hermitian) = true
36+
isstructured(::UpperTriangular) = true
37+
isstructured(::LowerTriangular) = true
38+
isstructured(::Tridiagonal) = true
39+
isstructured(::SymTridiagonal) = true
40+
isstructured(::Bidiagonal) = true
41+
isstructured(::Diagonal) = true
42+
2343
"""
2444
has_sparsestruct(x::AbstractArray)
2545
@@ -37,7 +57,7 @@ has_sparsestruct(x::SymTridiagonal)=true
3757
findstructralnz(x::AbstractArray)
3858
3959
Return: (I,J) #indexable objects
40-
Find sparsity pattern of special matrices, similar to first two elements of findnz(::SparseMatrixCSC)
60+
Find sparsity pattern of special matrices, the same as the first two elements of findnz(::SparseMatrixCSC)
4161
"""
4262
function findstructralnz(x::Diagonal)
4363
n=size(x,1)
@@ -107,15 +127,26 @@ function __init__()
107127

108128
@require StaticArrays="90137ffa-7385-5640-81b9-e52037218182" begin
109129
ismutable(::Type{<:StaticArrays.StaticArray}) = false
130+
can_setindex(::Type{<:StaticArrays.StaticArray}) = false
110131
ismutable(::Type{<:StaticArrays.MArray}) = true
111132
end
112133

113134
@require LabelledArrays="2ee39098-c373-598a-b85f-a56591580800" begin
114135
ismutable(::Type{<:LabelledArrays.LArray{T,N,Syms}}) where {T,N,Syms} = ismutable(T)
115136
end
116-
137+
117138
@require Flux="587475ba-b771-5e3f-ad9e-33799f191a9c" begin
118139
ismutable(::Type{<:Flux.Tracker.TrackedArray}) = false
140+
can_setindex(::Type{<:Flux.Tracker.TrackedArray}) = false
141+
end
142+
143+
@require BandedMatrices="aae01518-5342-5314-be14-df237901396f" begin
144+
is_structured(::BandedMatrices.BandedMatrix) = true
145+
end
146+
147+
@require BlockBandedMatrices="aae01518-5342-5314-be14-df237901396f" begin
148+
is_structured(::BandedMatrices.BlockBandedMatrix) = true
149+
is_structured(::BandedMatrices.BandedBlockBandedMatrix) = true
119150
end
120151
end
121152

0 commit comments

Comments
 (0)