# Extending a tensor

**URL:** <https://itensor.discourse.group/t/extending-a-tensor/914>\
**Category:** ITensor Julia Questions\
**Created:** [June 5, 2023, 12:45pm UTC](https://itensor.discourse.group/t/extending-a-tensor/914 "2023-06-05T12:45:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kemal](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/kemal/32/286_2.png) [@Kemal](https://itensor.discourse.group/u/Kemal)\
**Post date:** [June 5, 2023, 12:45pm UTC](https://itensor.discourse.group/t/extending-a-tensor/914/1 "2023-06-05T12:45:09Z")

</div>

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).

```julia
   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

---

<div class="post-metadata">

**Author:** ![JanReimers](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/janreimers/32/171_2.png) [@JanReimers](https://itensor.discourse.group/u/JanReimers)\
**Post date:** [June 5, 2023, 2:33pm UTC](https://itensor.discourse.group/t/extending-a-tensor/914/2 "2023-06-05T14:33:56Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![Kemal](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/kemal/32/286_2.png) [@Kemal](https://itensor.discourse.group/u/Kemal)\
**Post date:** [June 5, 2023, 2:48pm UTC](https://itensor.discourse.group/t/extending-a-tensor/914/3 "2023-06-05T14:48:05Z")

</div>

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.

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

```
