-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathblocksparsearray.jl
More file actions
238 lines (201 loc) · 7.32 KB
/
blocksparsearray.jl
File metadata and controls
238 lines (201 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using BlockArrays: BlockArrays, Block, BlockedUnitRange, blockedrange, blocklength
using DerivableInterfaces: @interface
using Dictionaries: Dictionary
using SparseArraysBase: SparseArrayDOK
# TODO: Delete this.
## using BlockArrays: blocks
struct BlockSparseArray{
T,
N,
A<:AbstractArray{T,N},
Blocks<:AbstractArray{A,N},
Axes<:Tuple{Vararg{AbstractUnitRange,N}},
} <: AbstractBlockSparseArray{T,N}
blocks::Blocks
axes::Axes
end
# TODO: Can this definition be shortened?
const BlockSparseMatrix{T,A<:AbstractMatrix{T},Blocks<:AbstractMatrix{A},Axes<:Tuple{AbstractUnitRange,AbstractUnitRange}} = BlockSparseArray{
T,2,A,Blocks,Axes
}
# TODO: Can this definition be shortened?
const BlockSparseVector{T,A<:AbstractVector{T},Blocks<:AbstractVector{A},Axes<:Tuple{AbstractUnitRange}} = BlockSparseArray{
T,1,A,Blocks,Axes
}
function BlockSparseArray(
block_data::Dictionary{<:Block{N},<:AbstractArray{<:Any,N}},
axes::Tuple{Vararg{AbstractUnitRange,N}},
) where {N}
blocks = default_blocks(block_data, axes)
return BlockSparseArray(blocks, axes)
end
function BlockSparseArray(
block_indices::Vector{<:Block{N}},
block_data::Vector{<:AbstractArray{<:Any,N}},
axes::Tuple{Vararg{AbstractUnitRange,N}},
) where {N}
return BlockSparseArray(Dictionary(block_indices, block_data), axes)
end
function BlockSparseArray{T,N,A,Blocks}(
blocks::AbstractArray{<:AbstractArray{T,N},N}, axes::Tuple{Vararg{AbstractUnitRange,N}}
) where {T,N,A<:AbstractArray{T,N},Blocks<:AbstractArray{A,N}}
return BlockSparseArray{T,N,A,Blocks,typeof(axes)}(blocks, axes)
end
function BlockSparseArray{T,N,A}(
blocks::AbstractArray{<:AbstractArray{T,N},N}, axes::Tuple{Vararg{AbstractUnitRange,N}}
) where {T,N,A<:AbstractArray{T,N}}
return BlockSparseArray{T,N,A,typeof(blocks)}(blocks, axes)
end
function BlockSparseArray{T,N}(
blocks::AbstractArray{<:AbstractArray{T,N},N}, axes::Tuple{Vararg{AbstractUnitRange,N}}
) where {T,N}
return BlockSparseArray{T,N,eltype(blocks),typeof(blocks),typeof(axes)}(blocks, axes)
end
function BlockSparseArray{T,N}(
block_data::Dictionary{Block{N,Int},<:AbstractArray{T,N}},
axes::Tuple{Vararg{AbstractUnitRange,N}},
) where {T,N}
blocks = default_blocks(block_data, axes)
return BlockSparseArray{T,N}(blocks, axes)
end
function BlockSparseArray{T,N,A}(
axes::Tuple{Vararg{AbstractUnitRange,N}}
) where {T,N,A<:AbstractArray{T,N}}
blocks = default_blocks(A, axes)
return BlockSparseArray{T,N,A}(blocks, axes)
end
function BlockSparseArray{T,N,A}(
axes::Vararg{AbstractUnitRange,N}
) where {T,N,A<:AbstractArray{T,N}}
return BlockSparseArray{T,N,A}(axes)
end
function BlockSparseArray{T,N,A}(
dims::Tuple{Vararg{Vector{Int},N}}
) where {T,N,A<:AbstractArray{T,N}}
return BlockSparseArray{T,N,A}(blockedrange.(dims))
end
# Fix ambiguity error.
function BlockSparseArray{T,0,A}(axes::Tuple{}) where {T,A<:AbstractArray{T,0}}
blocks = default_blocks(A, axes)
return BlockSparseArray{T,0,A}(blocks, axes)
end
function BlockSparseArray{T,N,A}(
dims::Vararg{Vector{Int},N}
) where {T,N,A<:AbstractArray{T,N}}
return BlockSparseArray{T,N,A}(dims)
end
function BlockSparseArray{T,N}(axes::Tuple{Vararg{AbstractUnitRange,N}}) where {T,N}
return BlockSparseArray{T,N,default_arraytype(T, axes)}(axes)
end
function BlockSparseArray{T,N}(axes::Vararg{AbstractUnitRange,N}) where {T,N}
return BlockSparseArray{T,N}(axes)
end
function BlockSparseArray{T,0}(axes::Tuple{}) where {T}
return BlockSparseArray{T,0,default_arraytype(T, axes)}(axes)
end
function BlockSparseArray{T,N}(dims::Tuple{Vararg{Vector{Int},N}}) where {T,N}
return BlockSparseArray{T,N}(blockedrange.(dims))
end
function BlockSparseArray{T,N}(dims::Vararg{Vector{Int},N}) where {T,N}
return BlockSparseArray{T,N}(dims)
end
function BlockSparseArray{T}(dims::Tuple{Vararg{Vector{Int}}}) where {T}
return BlockSparseArray{T,length(dims)}(dims)
end
function BlockSparseArray{T}(axes::Tuple{Vararg{AbstractUnitRange}}) where {T}
return BlockSparseArray{T,length(axes)}(axes)
end
function BlockSparseArray{T}(axes::Tuple{}) where {T}
return BlockSparseArray{T,length(axes)}(axes)
end
function BlockSparseArray{T}(dims::Vararg{Vector{Int}}) where {T}
return BlockSparseArray{T}(dims)
end
function BlockSparseArray{T}(axes::Vararg{AbstractUnitRange}) where {T}
return BlockSparseArray{T}(axes)
end
function BlockSparseArray{T}() where {T}
return BlockSparseArray{T}(())
end
# undef
function BlockSparseArray{T,N,A,Blocks}(
::UndefInitializer, args...
) where {T,N,A<:AbstractArray{T,N},Blocks<:AbstractArray{A,N}}
return BlockSparseArray{T,N,A,Blocks}(args...)
end
function BlockSparseArray{T,N,A}(
::UndefInitializer, args...
) where {T,N,A<:AbstractArray{T,N}}
return BlockSparseArray{T,N,A}(args...)
end
function BlockSparseArray{T,N}(::UndefInitializer, args...) where {T,N}
return BlockSparseArray{T,N}(args...)
end
function BlockSparseArray{T}(::UndefInitializer, args...) where {T}
return BlockSparseArray{T}(args...)
end
# Base `AbstractArray` interface
Base.axes(a::BlockSparseArray) = a.axes
# BlockArrays `AbstractBlockArray` interface.
# This is used by `blocks(::AnyAbstractBlockSparseArray)`.
@interface ::AbstractBlockSparseArrayInterface BlockArrays.blocks(a::BlockSparseArray) =
a.blocks
# TODO: Use `TypeParameterAccessors`.
function blockstype(
arraytype::Type{<:BlockSparseArray{T,N,A,Blocks}}
) where {T,N,A<:AbstractArray{T,N},Blocks<:AbstractArray{A,N}}
return Blocks
end
function blockstype(
arraytype::Type{<:BlockSparseArray{T,N,A}}
) where {T,N,A<:AbstractArray{T,N}}
return SparseArrayDOK{A,N}
end
function blockstype(arraytype::Type{<:BlockSparseArray{T,N}}) where {T,N}
return SparseArrayDOK{AbstractArray{T,N},N}
end
function blockstype(arraytype::Type{<:BlockSparseArray{T}}) where {T}
return SparseArrayDOK{AbstractArray{T}}
end
blockstype(arraytype::Type{<:BlockSparseArray}) = SparseArrayDOK{AbstractArray}
## # Base interface
## function Base.similar(
## a::AbstractBlockSparseArray, elt::Type, axes::Tuple{Vararg{BlockedUnitRange}}
## )
## # TODO: Preserve GPU data!
## return BlockSparseArray{elt}(undef, axes)
## end
# TypeParameterAccessors.jl interface
using TypeParameterAccessors: TypeParameterAccessors, Position, set_type_parameters
TypeParameterAccessors.position(::Type{BlockSparseArray}, ::typeof(eltype)) = Position(1)
TypeParameterAccessors.position(::Type{BlockSparseArray}, ::typeof(ndims)) = Position(2)
TypeParameterAccessors.position(::Type{BlockSparseArray}, ::typeof(blocktype)) = Position(3)
function TypeParameterAccessors.position(::Type{BlockSparseArray}, ::typeof(blockstype))
return Position(4)
end
# TODO: Make this generic to `AbstractBlockSparseVector` using
# TypeParameterAccessors.jl, for example using:
# `set_ndims(unspecify_type_parameters(typeof(a)), 1)`.
function show_typeof_blocksparse(io::IO, a::BlockSparseVector)
print(io, "BlockSparseVector")
print(io, '{')
show(io, eltype(a))
print(io, ", ")
show(io, blocktype(a))
print(io, ", …")
print(io, '}')
return nothing
end
# TODO: Make this generic to `AbstractBlockSparseMatrix` using
# TypeParameterAccessors.jl, for example using:
# `set_ndims(unspecify_type_parameters(typeof(a)), 2)`.
function show_typeof_blocksparse(io::IO, a::BlockSparseMatrix)
print(io, "BlockSparseMatrix")
print(io, '{')
show(io, eltype(a))
print(io, ", ")
show(io, blocktype(a))
print(io, ", …")
print(io, '}')
return nothing
end