Extending a tensor

Hello! Thanks a lot for your great package and feedback on the forum and github.

Currently I’m stuck in a relatively simple issue, but can not solve it.

I would like to combine two tensors, A(i, j) and B(i, k), which have the same index “i”, into a new tensor C(i, m), such that dim(m) = dim(j) + dim(k).

   if m <= dim(j)
      C(i, m) = A(i, j)
   else
      C(i,m) = B(i, k)
end

how can I do that easily ? I can iterate over every element of C and element wise copy A and B to C, but probably there is a better with using “combiner”, slicing or smth else ?

Thanks and best regards,
Kemal

1 Like

Hi Kemal, Welcome to ITensors. The directsum function should do what you want.

using ITensors
i,j,k=Index(3,"i"),Index(2,"j"),Index(4,"k")
A=ITensor(1.0,i,j)
B=ITensor(2.0,i,k)
C=directsum(A=>j,B=>k;tags="m")
ITensor ord=2
Dim 1: (dim=3|id=46|"i")
Dim 2: (dim=6|id=600|"m")
NDTensors.Dense{Float64, Vector{Float64}}
 3×6
 1.0  1.0  2.0  2.0  2.0  2.0
 1.0  1.0  2.0  2.0  2.0  2.0
 1.0  1.0  2.0  2.0  2.0  2.0 => (dim=6|id=600|"m")

Regards
Jan

2 Likes

Great, thanks a lot.

Actually I found that directsum(A=>j,B=>k;tags="m") returns not only C, but Pair{C, Index} with a new index.

C, extended_indC  =  directsum(A=>j,B=>k;tags="m")