-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathabstractarray.jl
More file actions
343 lines (309 loc) · 12 KB
/
abstractarray.jl
File metadata and controls
343 lines (309 loc) · 12 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# AbstractArray Interface
# -----------------------
# mostly pass everything through to the parent array, but with additional features for slicing
Base.eltype(t::AbstractBlockTensorMap) = eltype(typeof(t))
Base.ndims(t::AbstractBlockTensorMap) = numind(t)
Base.size(t::AbstractBlockTensorMap) = size(eachspace(t))
Base.size(t::AbstractBlockTensorMap, i::Int) = size(t)[i]
Base.length(t::AbstractBlockTensorMap) = prod(size(t))
Base.axes(t::AbstractBlockTensorMap) = map(Base.OneTo, size(t))
Base.axes(t::AbstractBlockTensorMap, i::Int) = Base.OneTo(i ≤ ndims(t) ? size(t, i) : 1)
Base.first(t::AbstractBlockTensorMap) = first(parent(t))
Base.last(t::AbstractBlockTensorMap) = last(parent(t))
Base.firstindex(t::AbstractBlockTensorMap) = 1
Base.firstindex(t::AbstractBlockTensorMap, i::Int) = 1
Base.lastindex(t::AbstractBlockTensorMap) = length(t)
Base.lastindex(t::AbstractBlockTensorMap, i::Int) = size(t, i)
Base.IndexStyle(::AbstractBlockTensorMap) = IndexCartesian()
Base.CartesianIndices(t::AbstractBlockTensorMap) = CartesianIndices(size(t))
Base.LinearIndices(t::AbstractBlockTensorMap) = LinearIndices(size(t))
Base.eachindex(t::AbstractBlockTensorMap) = eachindex(IndexStyle(t), t)
Base.eachindex(::IndexCartesian, t::AbstractBlockTensorMap) = CartesianIndices(t)
Base.eachindex(::IndexLinear, t::AbstractBlockTensorMap) = Base.OneTo(length(t))
Base.keys(l::Base.IndexStyle, t::AbstractBlockTensorMap) = keys(l, parent(t))
Base.haskey(t::AbstractBlockTensorMap, args...) = haskey(parent(t), args...)
Base.only(t::AbstractBlockTensorMap) = only(parent(t))
Base.isempty(t::AbstractBlockTensorMap) = isempty(parent(t))
# index checking
@inline function Base.checkbounds(t::AbstractBlockTensorMap, I...)
checkbounds(Bool, t, I...) || Base.throw_boundserror(t, I)
return nothing
end
@inline function Base.checkbounds(::Type{Bool}, t::AbstractBlockTensorMap, I...)
return Base.checkbounds_indices(Bool, axes(t), I)
end
@inline function Base.checkbounds(::Type{Bool}, t::AbstractBlockTensorMap, i)
return Base.checkindex(Bool, eachindex(IndexLinear(), t), i)
end
# TODO: make this also have Bool as first argument
function checkspaces(t::AbstractBlockTensorMap, v::AbstractTensorMap, I...)
space(v) == eachspace(t)[I...] || throw(
SpaceMismatch(
"inserting a tensor of space $(space(v)) at index $I into a tensor of space $(eachspace(t)[I...])",
),
)
return nothing
end
function checkspaces(t::AbstractBlockTensorMap, v::AbstractBlockTensorMap, I...)
V_slice = eachspace(t)[I...]
return if V_slice isa SumSpaceIndices
space(v) == space(V_slice) || throw(
SpaceMismatch(
"inserting a tensor of space $(space(v)) at index $I into a tensor of space $(space(V_slice))",
),
)
else
space(only(v)) == V_slice || throw(
SpaceMismatch(
"inserting a tensor of space $(space(only(v))) at index $I into a tensor of space $(V_slice)",
),
)
end
end
function checkspaces(t::AbstractBlockTensorMap)
iter = SumSpaceIndices(space(t))
for I in eachindex(iter)
iter[I] == space(t[I]) || throw(
SpaceMismatch(
"index $I has space $(iter[I]) but tensor has space $(space(t[I]))"
),
)
end
return nothing
end
# scalar indexing is dispatched through:
@propagate_inbounds Base.getindex(t::AbstractBlockTensorMap, I::Vararg{Int, N}) where {N} =
getindex(parent(t), I...)
@propagate_inbounds Base.getindex(t::AbstractBlockTensorMap, I::CartesianIndex{N}) where {N} =
getindex(parent(t), I)
@propagate_inbounds getindex!(t::AbstractBlockTensorMap, I::Vararg{Int, N}) where {N} =
getindex!(parent(t), I...)
@propagate_inbounds getindex!(t::AbstractBlockTensorMap, I::CartesianIndex{N}) where {N} =
getindex!(parent(t), I)
# slicing getindex needs to correctly allocate output blocktensor:
const SliceIndex = Union{Strided.SliceIndex, AbstractVector{<:Union{Integer, Bool}}}
Base.@propagate_inbounds function Base.getindex(
t::AbstractBlockTensorMap, indices::Vararg{SliceIndex}
)
V = space(eachspace(t)[indices...])
tdst = similar(t, V)
length(tdst) == 0 && return tdst
# prevent discarding of singleton dimensions
indices′ = map(indices) do ind
return ind isa Int ? (ind:ind) : ind
end
Rsrc = CartesianIndices(t)[indices′...]
Rdst = CartesianIndices(tdst)
for (I, v) in nonzero_pairs(t)
j = findfirst(==(I), Rsrc)
isnothing(j) && continue
tdst[Rdst[j]] = v
end
return tdst
end
# disambiguate:
@propagate_inbounds function Base.getindex(
t::AbstractBlockTensorMap, indices::Vararg{Strided.SliceIndex}
)
V = space(eachspace(t)[indices...])
tdst = similar(t, V)
length(tdst) == 0 && return tdst
# prevent discarding of singleton dimensions
indices′ = map(indices) do ind
return ind isa Int ? (ind:ind) : ind
end
Rsrc = CartesianIndices(t)[indices′...]
Rdst = CartesianIndices(tdst)
for (I, v) in nonzero_pairs(t)
j = findfirst(==(I), Rsrc)
isnothing(j) && continue
tdst[Rdst[j]] = v
end
return tdst
end
# TODO: check if this fallback is fair
@propagate_inbounds Base.setindex!(t::AbstractBlockTensorMap, v::AbstractTensorMap, args...) = (
setindex!(parent(t), v, args...); t
)
# ambiguity fix
function Base.setindex!(::AbstractBlockTensorMap, ::AbstractTensorMap, sectors::Tuple{I, Vararg{I}}) where {I <: Sector}
error("invalid indexing for blocktensormap")
end
function Base.setindex!(::AbstractBlockTensorMap, ::AbstractTensorMap, ::FusionTree, ::FusionTree)
error("invalid indexing for blocktensormap")
end
# setindex verifies structure is correct
@inline function Base.setindex!(
t::AbstractBlockTensorMap, v::AbstractTensorMap, indices::Vararg{SliceIndex}
)
@boundscheck begin
checkbounds(t, indices...)
checkspaces(t, v, indices...)
end
@inbounds parent(t)[indices...] = v
return t
end
# setindex with blocktensor needs to correctly slice-assign
@inline function Base.setindex!(
t::AbstractBlockTensorMap, v::AbstractBlockTensorMap, indices::Vararg{SliceIndex}
)
@boundscheck begin
checkbounds(t, indices...)
checkspaces(t, v, indices...)
end
@inbounds copyto!(view(parent(t), indices...), parent(v))
return t
end
# disambiguate
@inline function Base.setindex!(
t::AbstractBlockTensorMap, v::AbstractTensorMap, indices::Vararg{Strided.SliceIndex}
)
@boundscheck begin
checkbounds(t, indices...)
checkspaces(t, v, indices...)
end
@inbounds parent(t)[indices...] = v
return t
end
# disambiguate
@inline function Base.setindex!(
t::AbstractBlockTensorMap, v::AbstractBlockTensorMap, indices::Vararg{Strided.SliceIndex},
)
@boundscheck begin
checkbounds(t, indices...)
checkspaces(t, v, indices...)
end
@inbounds copyto!(view(parent(t), indices...), parent(v))
return t
end
@inline function Base.get(t::AbstractBlockTensorMap, key, default)
@boundscheck checkbounds(t, key)
return @inbounds get(parent(t), key, default)
end
function Base.copy(t::AbstractBlockTensorMap)
tdst = if eltype(t) <: AdjointTensorMap
similar(t, Base.promote_op(copy, eltype(t)), space(t))
else
similar(t)
end
return copy!(tdst, t)
end
function Base.copy!(tdst::AbstractBlockTensorMap, tsrc::AbstractBlockTensorMap)
space(tdst) == space(tsrc) || throw(SpaceMismatch("$(space(tdst)) ≠ $(space(tsrc))"))
@inbounds for (key, value) in nonzero_pairs(tsrc)
tdst[key] = copy!(tdst[key], value)
end
return tdst
end
function Base.copyto!(
tdst::AbstractBlockTensorMap, Rdest::CartesianIndices,
tsrc::AbstractBlockTensorMap, Rsrc::CartesianIndices,
)
copyto!(parent(tdst), Rdest, parent(tsrc), Rsrc)
return tdst
end
# generic implementation for AbstractTensorMap with Sumspace -> returns `BlockTensorMap`
# function Base.similar(
# ::AbstractTensorMap, ::Type{TorA}, P::TensorMapSumSpace{S}
# ) where {TorA<:TensorKit.MatOrNumber,S}
# N₁ = length(codomain(P))
# N₂ = length(domain(P))
# TT = blocktensormaptype(S, N₁, N₂, TorA)
# return TT(undef, codomain(P), domain(P))
# end
# disambiguate
# function Base.similar(
# t::TensorKit.AdjointTensorMap, T::Type{TorA}, P::TensorMapSumSpace{S}
# ) where {TorA<:TensorKit.MatOrNumber,S}
# @invoke Base.similar(t::TensorKit.AdjointTensorMap, T::Type{TorA}, P::TensorMapSpace)
# end
Base.similar(t::AbstractBlockTensorMap) = similar(t, eltype(t), space(t))
Base.similar(t::AbstractBlockTensorMap, P::TensorMapSumSpace) = similar(t, eltype(t), P)
# make sure tensormap specializations are not used for sumspaces:
function Base.similar(
t::AbstractTensorMap, ::Type{TorA}, P::TensorMapSumSpace{S}
) where {S, TorA}
return issparse(t) ? sparse_similar(t, TorA, P) : dense_similar(t, TorA, P)
end
dense_similar(t::AbstractTensorMap, P::TensorMapSumSpace) = dense_similar(t, TK.similarstoragetype(t), P)
function dense_similar(t::AbstractTensorMap, ::Type{TorA}, P::TensorMapSumSpace) where {TorA}
TT = similar_tensormaptype(t, TorA, P)
return BlockTensorMap{TT}(undef, P)
end
sparse_similar(t::AbstractTensorMap, P::TensorMapSumSpace) = sparse_similar(t, TK.similarstoragetype(t), P)
function sparse_similar(t::AbstractTensorMap, ::Type{TorA}, P::TensorMapSumSpace) where {TorA}
TT = similar_tensormaptype(t, TorA, P)
return SparseBlockTensorMap{TT}(undef, P)
end
function similar_tensormaptype(
::AbstractTensorMap, T::Type{<:AbstractTensorMap}, P::TensorMapSumSpace{S}
) where {S}
if isconcretetype(T)
return tensormaptype(S, numout(P), numin(P), storagetype(T))
else
return AbstractTensorMap{scalartype(T), S, numout(P), numin(P)}
end
end
function similar_tensormaptype(
::AbstractTensorMap, T::Type{<:AbstractVector}, P::TensorMapSumSpace{S}
) where {S}
return tensormaptype(S, numout(P), numin(P), T)
end
function similar_tensormaptype(
::AbstractTensorMap, T::Type{<:Number}, P::TensorMapSumSpace{S}
) where {S}
return tensormaptype(S, numout(P), numin(P), T)
end
function similar_tensormaptype(
t::AbstractBlockTensorMap, T::Type{<:AbstractTensorMap}, P::TensorMapSumSpace{S}
) where {S}
if eltype(t) === T && typeof(space(t)) === typeof(P)
return T
elseif isconcretetype(T)
return tensormaptype(S, numout(P), numin(P), storagetype(T))
else
return AbstractTensorMap{scalartype(T), S, numout(P), numin(P)}
end
end
function similar_tensormaptype(
t::AbstractBlockTensorMap, M::Type{<:AbstractVector}, P::TensorMapSumSpace{S}
) where {S}
if isconcretetype(eltype(t))
return tensormaptype(S, numout(P), numin(P), M)
else
return AbstractTensorMap{scalartype(M), S, numout(P), numin(P)}
end
end
function similar_tensormaptype(
t::AbstractBlockTensorMap, T::Type{<:Number}, P::TensorMapSumSpace{S}
) where {S}
if isconcretetype(eltype(t))
M = TensorKit.similarstoragetype(t, T)
return tensormaptype(S, numout(P), numin(P), M)
else
return AbstractTensorMap{T, S, numout(P), numin(P)}
end
end
# implementation in type domain
function Base.similar(::Type{T}, P::TensorMapSumSpace) where {T <: AbstractBlockTensorMap}
return T(undef, P)
end
# Cat
# ---
Base.eltypeof(t::AbstractBlockTensorMap) = eltype(t)
@inline function Base._cat_t(
dims, ::Type{T}, ts::AbstractBlockTensorMap...
) where {T <: AbstractTensorMap}
catdims = Base.dims2cat(dims)
V = space(Base._cat(dims, eachspace.(ts)...))
A = similar(ts[1], T, V)
shape = size(A)
if count(!iszero, catdims)::Int > 1
zerovector!(A)
end
return Base.__cat(A, shape, catdims, ts...)
end
Base._copy_or_fill!(A, inds, x::AbstractBlockTensorMap) = (A[inds...] = x)
# WHY DOES BASE NOT DEFAULT TO AXES
Base.cat_indices(A::AbstractBlockTensorMap, d) = axes(A, d)
Base.cat_size(A::AbstractBlockTensorMap, d) = size(A, d)