# Create a tensor from a function that takes indexes as variable

**URL:** <https://itensor.discourse.group/t/create-a-tensor-from-a-function-that-takes-indexes-as-variable/1337>\
**Category:** ITensor Julia Questions\
**Tags:** julia, mps\
**Created:** [December 5, 2023, 2:25pm UTC](https://itensor.discourse.group/t/create-a-tensor-from-a-function-that-takes-indexes-as-variable/1337 "2023-12-05T14:25:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![matinator](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/matinator/32/431_2.png) [@matinator](https://itensor.discourse.group/u/matinator)\
**Post date:** [December 5, 2023, 2:25pm UTC](https://itensor.discourse.group/t/create-a-tensor-from-a-function-that-takes-indexes-as-variable/1337/1 "2023-12-05T14:25:40Z")

</div>

Hello everyone, I am trying to create a tensor with a function that should take the indexes as values to produce a specific result for every element for the tensor.  
I was able to define it using CartesianIndexes, a function and a map for the case of a simple matrix as listed below :

```auto
# fourier transform of call option
function vstate(z, eta, alpha, strike_price)
    z = - eta .* z .- im *alpha
    return -strike_price^(1 + im * sum(z))/((-1)^length(z) * prod(im .* z) .*(1 + im * sum(z)))
end
alpha = 2.5
eta = 0.5
strike_price = 100
#create the matrix
callp = map(x -> vstate(x.I, -eta, -alpha, strike_price), 
CartesianIndices((-25:25, -25:25)))

```

Is it possible to do this in Itensors? And do you perhaps know, in case the first response is positive, how to produce an MPS of it? Thank you.

---

<div class="post-metadata">

**Author:** ![mtfishman](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/mtfishman/32/11_2.png) [@mtfishman](https://itensor.discourse.group/u/mtfishman)\
**Post date:** [December 5, 2023, 3:06pm UTC](https://itensor.discourse.group/t/create-a-tensor-from-a-function-that-takes-indexes-as-variable/1337/2 "2023-12-05T15:06:49Z")

</div>

The best way to do that is just wrap the Julia Array in an ITensor, i.e.

```julia
i = Index(size(callp, 1))
j = Index(size(callp, 2))
t = itensor(callp, i, j) # Creates a view into `callp`
t = ITensor(callp, i, j) # Creates a copy of `callp`

```

Then you can create an MPS with:

```julia
m = MPS(t, [i, j]; cutoff=1e-12) # Create an MPS from the ITensor
m = MPS(callp, [i, j]; cutoff=1e-12) # Create an MPS from the Julia Array

```

---

<div class="post-metadata">

**Author:** ![matinator](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/matinator/32/431_2.png) [@matinator](https://itensor.discourse.group/u/matinator)\
**Post date:** [December 5, 2023, 3:12pm UTC](https://itensor.discourse.group/t/create-a-tensor-from-a-function-that-takes-indexes-as-variable/1337/3 "2023-12-05T15:12:10Z")

</div>

So it is not possible to allocate that tensor directly using the function, but I need to instantiate the array first and then convert it to Tensor at the moment?

---

<div class="post-metadata">

**Author:** ![mtfishman](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/mtfishman/32/11_2.png) [@mtfishman](https://itensor.discourse.group/u/mtfishman)\
**Post date:** [December 5, 2023, 3:45pm UTC](https://itensor.discourse.group/t/create-a-tensor-from-a-function-that-takes-indexes-as-variable/1337/4 "2023-12-05T15:45:28Z")

</div>

Yeah, but I don’t see how that could be avoided (i.e. we could provide an interface for passing a function that constructs elements of an ITensor, but we would have to allocate an array internally at some point anyway).

---

<div class="post-metadata">

**Author:** ![matinator](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/matinator/32/431_2.png) [@matinator](https://itensor.discourse.group/u/matinator)\
**Post date:** [December 5, 2023, 3:50pm UTC](https://itensor.discourse.group/t/create-a-tensor-from-a-function-that-takes-indexes-as-variable/1337/5 "2023-12-05T15:50:36Z")

</div>

Ok, thank you!
