|
1 | 1 | # AdjointTensorMap: lazy adjoint |
2 | 2 | #==========================================================# |
3 | 3 | """ |
4 | | - struct AdjointTensorMap{T, S, N₁, N₂, ...} <: AbstractTensorMap{T, S, N₁, N₂} |
| 4 | + struct AdjointTensorMap{T, S, N₁, N₂, TT<:AbstractTensorMap} <: AbstractTensorMap{T, S, N₁, N₂} |
5 | 5 |
|
6 | 6 | Specific subtype of [`AbstractTensorMap`](@ref) that is a lazy wrapper for representing the |
7 | | -adjoint of an instance of [`TensorMap`](@ref). |
| 7 | +adjoint of an instance of [`AbstractTensorMap`](@ref). |
8 | 8 | """ |
9 | | -struct AdjointTensorMap{T,S,N₁,N₂,I,A,F₁,F₂} <: |
| 9 | +struct AdjointTensorMap{T,S,N₁,N₂,TT<:AbstractTensorMap{T,S,N₂,N₁}} <: |
10 | 10 | AbstractTensorMap{T,S,N₁,N₂} |
11 | | - parent::TensorMap{T,S,N₂,N₁,I,A,F₂,F₁} |
| 11 | + parent::TT |
12 | 12 | end |
13 | 13 |
|
14 | 14 | #! format: off |
15 | | -const AdjointTrivialTensorMap{T,S,N₁,N₂,A<:DenseMatrix} = |
16 | | - AdjointTensorMap{T,S,N₁,N₂,Trivial,A,Nothing,Nothing} |
| 15 | +const AdjointTrivialTensorMap{T,S,N₁,N₂,TT<:TrivialTensorMap{T,S,N₂,N₁}} = |
| 16 | + AdjointTensorMap{T,S,N₁,N₂,TT} |
17 | 17 | #! format: on |
18 | 18 |
|
19 | 19 | # Constructor: construct from taking adjoint of a tensor |
20 | | -Base.adjoint(t::TensorMap) = AdjointTensorMap(t) |
21 | | -Base.adjoint(t::AdjointTensorMap) = t.parent |
| 20 | +Base.adjoint(t::AbstractTensorMap) = AdjointTensorMap(t) |
| 21 | +Base.adjoint(t::AdjointTensorMap) = parent(t) |
| 22 | + |
| 23 | +Base.parent(t::AdjointTensorMap) = t.parent |
| 24 | +parenttype(::Type{<:AdjointTensorMap{T,S,N₁,N₂,TT}}) where {T,S,N₁,N₂,TT} = TT |
22 | 25 |
|
23 | 26 | function Base.similar(t::AdjointTensorMap, ::Type{TorA}, |
24 | 27 | P::TensorMapSpace) where {TorA<:MatOrNumber} |
25 | 28 | return similar(t', TorA, P) |
26 | 29 | end |
27 | 30 |
|
28 | 31 | # Properties |
29 | | -codomain(t::AdjointTensorMap) = domain(t.parent) |
30 | | -domain(t::AdjointTensorMap) = codomain(t.parent) |
| 32 | +codomain(t::AdjointTensorMap) = domain(parent(t)) |
| 33 | +domain(t::AdjointTensorMap) = codomain(parent(t)) |
31 | 34 |
|
32 | | -blocksectors(t::AdjointTensorMap) = blocksectors(t.parent) |
| 35 | +blocksectors(t::AdjointTensorMap) = blocksectors(parent(t)) |
33 | 36 |
|
34 | | -#! format: off |
35 | | -storagetype(::Type{<:AdjointTrivialTensorMap{T,S,N₁,N₂,A}}) where {T,S,N₁,N₂,A<:DenseMatrix} = A |
36 | | -storagetype(::Type{<:AdjointTensorMap{T,S,N₁,N₂,I,<:SectorDict{I,A}}}) where {T,S,N₁,N₂,I<:Sector,A<:DenseMatrix} = A |
37 | | -#! format: on |
| 37 | +storagetype(::Type{TT}) where {TT<:AdjointTensorMap} = storagetype(parenttype(TT)) |
38 | 38 |
|
39 | | -dim(t::AdjointTensorMap) = dim(t.parent) |
| 39 | +dim(t::AdjointTensorMap) = dim(parent(t)) |
40 | 40 |
|
41 | 41 | # Indexing |
42 | 42 | #---------- |
43 | | -hasblock(t::AdjointTensorMap, s::Sector) = hasblock(t.parent, s) |
44 | | -block(t::AdjointTensorMap, s::Sector) = block(t.parent, s)' |
45 | | -blocks(t::AdjointTensorMap) = (c => b' for (c, b) in blocks(t.parent)) |
| 43 | +hasblock(t::AdjointTensorMap, s::Sector) = hasblock(parent(t), s) |
| 44 | +block(t::AdjointTensorMap, s::Sector) = block(parent(t), s)' |
| 45 | +blocks(t::AdjointTensorMap) = (c => b' for (c, b) in blocks(parent(t))) |
46 | 46 |
|
47 | 47 | fusiontrees(::AdjointTrivialTensorMap) = ((nothing, nothing),) |
48 | | -fusiontrees(t::AdjointTensorMap) = TensorKeyIterator(t.parent.colr, t.parent.rowr) |
| 48 | +function fusiontrees(t::AdjointTensorMap{T,S,N₁,N₂,TT}) where {T,S,N₁,N₂,TT<:TensorMap} |
| 49 | + return TensorKeyIterator(parent(t).colr, parent(t).rowr) |
| 50 | +end |
49 | 51 |
|
50 | | -function Base.getindex(t::AdjointTensorMap{T,S,N₁,N₂,I}, |
| 52 | +function Base.getindex(t::AdjointTensorMap{T,S,N₁,N₂,<:TensorMap{T,S,N₁,N₂,I}}, |
51 | 53 | f₁::FusionTree{I,N₁}, f₂::FusionTree{I,N₂}) where {T,S,N₁,N₂,I} |
52 | 54 | c = f₁.coupled |
53 | 55 | @boundscheck begin |
54 | 56 | c == f₂.coupled || throw(SectorMismatch()) |
55 | 57 | hassector(codomain(t), f₁.uncoupled) && hassector(domain(t), f₂.uncoupled) |
56 | 58 | end |
57 | | - return sreshape((StridedView(t.parent.data[c])[t.parent.rowr[c][f₂], |
58 | | - t.parent.colr[c][f₁]])', |
| 59 | + return sreshape((StridedView(parent(t).data[c])[parent(t).rowr[c][f₂], |
| 60 | + parent(t).colr[c][f₁]])', |
59 | 61 | (dims(codomain(t), f₁.uncoupled)..., dims(domain(t), f₂.uncoupled)...)) |
60 | 62 | end |
| 63 | +@propagate_inbounds function Base.getindex(t::AdjointTensorMap{T,S,N₁,N₂}, |
| 64 | + f₁::FusionTree{I,N₁}, |
| 65 | + f₂::FusionTree{I,N₂}) where {T,S,N₁,N₂,I} |
| 66 | + d_cod = dims(codomain(t), f₁.uncoupled) |
| 67 | + d_dom = dims(domain(t), f₂.uncoupled) |
| 68 | + return sreshape(sreshape(StridedView(parent(t)[f₂, f₁]), (prod(d_dom), prod(d_cod)))', |
| 69 | + (d_cod..., d_dom...)) |
| 70 | +end |
| 71 | + |
61 | 72 | @propagate_inbounds function Base.setindex!(t::AdjointTensorMap{T,S,N₁,N₂,I}, v, |
62 | 73 | f₁::FusionTree{I,N₁}, |
63 | 74 | f₂::FusionTree{I,N₂}) where {T,S,N₁,N₂,I} |
64 | 75 | return copy!(getindex(t, f₁, f₂), v) |
65 | 76 | end |
66 | 77 |
|
67 | 78 | @inline function Base.getindex(t::AdjointTrivialTensorMap) |
68 | | - return sreshape(StridedView(t.parent.data)', (dims(codomain(t))..., dims(domain(t))...)) |
| 79 | + return sreshape(StridedView(parent(t).data)', |
| 80 | + (dims(codomain(t))..., dims(domain(t))...)) |
69 | 81 | end |
70 | 82 | @inline Base.setindex!(t::AdjointTrivialTensorMap, v) = copy!(getindex(t), v) |
71 | 83 |
|
|
0 commit comments