Skip to content

Commit 8cef714

Browse files
committed
Add method for embedding tensors
1 parent 980fe55 commit 8cef714

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

src/TensorKit.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export leftorth, rightorth, leftnull, rightnull,
7676
isposdef, isposdef!, ishermitian, sylvester, rank, cond
7777
export braid, braid!, permute, permute!, transpose, transpose!, twist, twist!, repartition,
7878
repartition!
79-
export catdomain, catcodomain
79+
export catdomain, catcodomain, embed!
8080

8181
export OrthogonalFactorizationAlgorithm, QR, QRpos, QL, QLpos, LQ, LQpos, RQ, RQpos,
8282
SVD, SDD, Polar

src/spaces/homspace.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ function dim(W::HomSpace)
125125
return d
126126
end
127127

128+
"""
129+
fusiontrees(W::HomSpace)
130+
131+
Return the fusiontrees corresponding to all valid fusion channels of a given `HomSpace`.
132+
"""
133+
fusiontrees(W::HomSpace) = fusionblockstructure(W).fusiontreelist
134+
128135
# Operations on HomSpaces
129136
# -----------------------
130137
"""

src/tensors/linalg.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,36 @@ function catcodomain(t1::TT, t2::TT) where {S,N₂,TT<:AbstractTensorMap{<:Any,S
512512
return t
513513
end
514514

515+
"""
516+
embed!(tdst::AbstactTensorMap, tsrc::AbstractTensorMap)
517+
518+
Embed the contents of `tsrc` into `tdst`, which may have different sizes of data.
519+
This is equivalent to the following operation on dense arrays, but also works for symmetric
520+
tensors. Note also that this only overwrites the regions that are shared, and will do
521+
nothing on the ones that are not, so it is up to the user to properly initialize the
522+
destination.
523+
524+
```julia
525+
sub_axes = map((x, y) -> 1:min(x, y), size(tdst), size(tsrc))
526+
tdst[sub_axes...] .= tsrc[sub_axes...]
527+
```
528+
"""
529+
function embed!(tdst::AbstractTensorMap, tsrc::AbstractTensorMap)
530+
numin(tdst) == numin(tsrc) && numout(tdst) == numout(tsrc) ||
531+
throw(DimensionError("Incompatible number of indices for source and destination"))
532+
S = spacetype(tdst)
533+
S == spacetype(tsrc) || throw(SpaceMismatch("incompatible spacetypes"))
534+
dom = mapreduce(infimum, , domain(tdst), domain(tsrc); init=one(S))
535+
cod = mapreduce(infimum, , codomain(tdst), codomain(tsrc); init=one(S))
536+
for (f1, f2) in fusiontrees(cod dom)
537+
@inbounds data_dst = tdst[f1, f2]
538+
@inbounds data_src = tsrc[f1, f2]
539+
sub_axes = map(Base.OneTo min, size(data_dst), size(data_src))
540+
data_dst[sub_axes...] .= data_src[sub_axes...]
541+
end
542+
return tdst
543+
end
544+
515545
# tensor product of tensors
516546
"""
517547
⊗(t1::AbstractTensorMap, t2::AbstractTensorMap, ...) -> TensorMap

0 commit comments

Comments
 (0)