-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsparsetensorarray.jl
More file actions
210 lines (192 loc) · 7.21 KB
/
sparsetensorarray.jl
File metadata and controls
210 lines (192 loc) · 7.21 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
# SparseBlockTensorMap parent array
# ---------------------------------
struct SparseTensorArray{S, N₁, N₂, T <: AbstractTensorMap{<:Any, S, N₁, N₂}, N} <:
AbstractArray{T, N}
data::Dict{CartesianIndex{N}, T}
space::TensorMapSumSpace{S, N₁, N₂}
function SparseTensorArray{S, N₁, N₂, T, N}(
data::Dict{CartesianIndex{N}, T}, space::TensorMapSumSpace{S, N₁, N₂}
) where {S, N₁, N₂, T, N}
N₁ + N₂ == N || throw(
TypeError(
:SparseTensorArray,
SparseTensorArray{S, N₁, N₂, T, N₁ + N₂},
SparseTensorArray{S, N₁, N₂, T, N},
),
)
return new{S, N₁, N₂, T, N}(data, space)
end
end
function SparseTensorArray{S, N₁, N₂, T, N}(
::UndefInitializer, space::TensorMapSumSpace{S, N₁, N₂}
) where {S, N₁, N₂, T <: AbstractTensorMap{<:Any, S, N₁, N₂}, N}
return SparseTensorArray{S, N₁, N₂, T, N}(Dict{CartesianIndex{N}, T}(), space)
end
function SparseTensorArray(
data::Dict{CartesianIndex{N}, T}, space::TensorMapSumSpace{S, N₁, N₂}
) where {S, N₁, N₂, T, N}
return SparseTensorArray{S, N₁, N₂, T, N}(data, space)
end
Base.pairs(A::SparseTensorArray) = pairs(A.data)
Base.keys(A::SparseTensorArray) = keys(A.data)
Base.values(A::SparseTensorArray) = values(A.data)
TensorKit.space(A::SparseTensorArray) = A.space
TensorKit.codomain(A::SparseTensorArray) = codomain(space(A))
TensorKit.domain(A::SparseTensorArray) = domain(space(A))
TensorKit.numout(A::SparseTensorArray) = numout(eltype(A))
TensorKit.numout(::Type{T}) where {T <: SparseTensorArray} = numout(eltype(A))
TensorKit.numin(A::SparseTensorArray) = numin(eltype(A))
TensorKit.numin(::Type{T}) where {T <: SparseTensorArray} = numin(eltype(A))
# AbstractArray interface
# -----------------------
Base.size(A::SparseTensorArray) = ntuple(Base.Fix1(size, A), ndims(A))
function Base.size(A::SparseTensorArray, i::Int)
1 ≤ i ≤ ndims(A) || throw(ArgumentError("Invalid number of dimensions"))
return i <= numout(A) ? length(codomain(A)[i]) : length(domain(A)[i - numout(A)])
end
@propagate_inbounds function Base.getindex(
A::SparseTensorArray{S, N₁, N₂, T, N}, I::Vararg{Int, N}
) where {S, N₁, N₂, T, N}
@boundscheck checkbounds(A, I...)
return @inbounds get(A.data, CartesianIndex(I)) do
return fill!(similar(T, eachspace(A)[I...]), zero(scalartype(T)))
end
end
@propagate_inbounds function getindex!(
A::SparseTensorArray{S, N₁, N₂, T, N}, I::CartesianIndex{N}
) where {S, N₁, N₂, T, N}
@boundscheck checkbounds(A, I)
return @inbounds get!(A.data, I) do
return fill!(similar(T, eachspace(A)[I]), zero(scalartype(T)))
end
end
@propagate_inbounds function getindex!(
A::SparseTensorArray{S, N₁, N₂, T, N}, I::Vararg{Int, N}
) where {S, N₁, N₂, T, N}
return getindex!(A, CartesianIndex(I))
end
@propagate_inbounds function Base.setindex!(
A::SparseTensorArray{S, N₁, N₂, T, N}, v, I::Vararg{Int, N}
) where {S, N₁, N₂, T, N}
@boundscheck begin
checkbounds(A, I...)
checkspaces(A, v, I...)
end
@inbounds A.data[CartesianIndex(I)] = v # implicit converter
return A
end
function Base.delete!(A::SparseTensorArray, I::Vararg{Int, N}) where {N}
return delete!(A.data, CartesianIndex(I))
end
Base.delete!(A::SparseTensorArray, I::CartesianIndex) = delete!(A.data, I)
Base.empty!(A::SparseTensorArray) = empty!(A.data)
function Base.haskey(A::SparseTensorArray, I::Vararg{Int, N}) where {N}
return haskey(A.data, CartesianIndex(I))
end
Base.haskey(A::SparseTensorArray, I::CartesianIndex) = haskey(A.data, I)
function Base.similar(
::SparseTensorArray, ::Type{T}, spaces::TensorMapSumSpace{S, N₁, N₂}
) where {S, N₁, N₂, T <: AbstractTensorMap{<:Any, S, N₁, N₂}}
N = N₁ + N₂
return SparseTensorArray{S, N₁, N₂, T, N}(Dict{CartesianIndex{N}, T}(), spaces)
end
Base.@propagate_inbounds function Base.copyto!(
t::SparseTensorArray, v::SubArray{T, N, A}
) where {T, N, A <: SparseTensorArray}
undropped_parentindices = map(Base.parentindices(v)) do I
I isa Base.ScalarIndex ? (I:I) : I
end
for I in eachindex(IndexCartesian(), t)
parentI = CartesianIndex(Base.reindex(undropped_parentindices, I.I))
if haskey(parent(v), parentI)
t[I] = parent(v)[parentI]
else
delete!(t, I)
end
end
return t
end
Base.@propagate_inbounds function Base.copyto!(
t::SubArray{T, N, A}, v::SparseTensorArray
) where {T, N, A <: SparseTensorArray}
undropped_parentindices = map(Base.parentindices(t)) do I
I isa Base.ScalarIndex ? (I:I) : I
end
for I in eachindex(IndexCartesian(), v)
if haskey(v, I)
t[I] = v[I]
else
parentI = CartesianIndex(Base.reindex(undropped_parentindices, I.I))
delete!(parent(t), parentI)
end
end
return t
end
Base.@propagate_inbounds function Base.copyto!(
dest::SparseTensorArray, Rdest::CartesianIndices,
src::SparseTensorArray, Rsrc::CartesianIndices,
)
isempty(Rdest) && return dest
if size(Rdest) != size(Rsrc)
throw(
ArgumentError(
"source and destination must have same size (got $(size(Rsrc)) and $(size(Rdest)))",
),
)
end
@boundscheck begin
checkbounds(dest, first(Rdest))
checkbounds(dest, last(Rdest))
checkbounds(src, first(Rsrc))
checkbounds(src, last(Rsrc))
end
CRdest = CartesianIndices(Rdest)
CRsrc = CartesianIndices(Rsrc)
ΔI = first(CRdest) - first(CRsrc)
for I in CRsrc
if Rsrc[I] in nonzero_keys(src)
dest[Rdest[I + ΔI]] = src[Rsrc[I]]
end
end
return dest
end
# non-scalar indexing
# -------------------
# specialisations to have non-scalar indexing behave as expected
_newindex(i::Int, range::Int) = i == range ? (1,) : nothing
function _newindex(i::Int, range::AbstractVector{Int})
k = findfirst(==(i), range)
return k === nothing ? nothing : (k,)
end
_newindices(::Tuple{}, ::Tuple{}) = ()
function _newindices(I::Tuple, indices::Tuple)
i = _newindex(I[1], indices[1])
Itail = _newindices(Base.tail(I), Base.tail(indices))
(i === nothing || Itail === nothing) && return nothing
return (i..., Itail...)
end
function Base._unsafe_getindex(
::IndexCartesian,
t::SparseTensorArray{S, N₁, N₂, T, N}, I::Vararg{Union{Real, AbstractArray}, N},
) where {S, N₁, N₂, T, N}
dest = similar(t, eltype(t), space(eachspace(t)[I...]))
indices = Base.to_indices(t, I)
for (k, v) in t.data
newI = _newindices(k.I, indices)
if newI !== nothing
dest[newI...] = v
end
end
return dest
end
# Space checking
# --------------
eachspace(A::SparseTensorArray) = SumSpaceIndices(A.space)
function checkspaces(A::SparseTensorArray, v::AbstractTensorMap, I...)
return space(v) == eachspace(A)[I...] || throw(
SpaceMismatch(
"inserting a tensor of space $(space(v)) at $(I) into a SparseTensorArray with space $(eachspace(A))",
),
)
return nothing
end