# Low rank tensor computation

**URL:** <https://discourse.julialang.org/t/low-rank-tensor-computation/93627>\
**Category:** Teaching & Outreach\
**Tags:** question\
**Created:** [January 27, 2023, 12:06pm UTC](https://discourse.julialang.org/t/low-rank-tensor-computation/93627 "2023-01-27T12:06:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ga72kud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ga72kud/32/32180_2.png) [@ga72kud](https://discourse.julialang.org/u/ga72kud)\
**Post date:** [January 27, 2023, 12:06pm UTC](https://discourse.julialang.org/t/low-rank-tensor-computation/93627/1 "2023-01-27T12:06:02Z")

</div>

I want to generate a rank-1 tensor with Julia for some tensor decomposition. As for SVD and linear combination of singular values and orthogonal basis vectors, one can decompose a matrix by a sum like the sum of singular values \* u \* v’.

I can generate a 10x20x30 matrix like this. (the original code is inspired by [GitHub - yunjhongwu/TensorDecompositions.jl: A Julia implementation of tensor decomposition algorithms](https://github.com/yunjhongwu/TensorDecompositions.jl))

```julia
	u = randn(10); v = randn(20); w = randn(30)
	# Generate a noisy rank-1 tensor
	T = cat(map(x -> x * u * v', w)...; dims=3) + 0.2 * randn(10, 20, 30)

```

I am not sure why I have to use … in the formula. I concatenate over the third dimension.

Following code is only giving me a 10x20 matrix:

```julia
Su = randn(10); Sv = randn(20); Sw = randn(30)
ST = map(x -> x * Su * Sv', Sw)

```

---

<div class="post-metadata">

**Author:** ![ga72kud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ga72kud/32/32180_2.png) [@ga72kud](https://discourse.julialang.org/u/ga72kud)\
**Post date:** [January 27, 2023, 12:17pm UTC](https://discourse.julialang.org/t/low-rank-tensor-computation/93627/2 "2023-01-27T12:17:03Z")

</div>

I tested it with a simpler version. It seems that … is converting the array over matrices to an array over arrays:

```julia
Su = [1; 2.]; Sv = [2; 3.]; Sw = [3.; 2]
ST = cat(map(x -> x * Su * Sv', Sw)..., dims=3)

```

with …

```julia
2×2×2 Array{Float64, 3}:
[:, :, 1] =
  6.0 9.0
 12.0 18.0

[:, :, 2] =
 4.0 6.0
 8.0 12.0

```

without … producing

```julia
2×1×1 Array{Matrix{Float64}, 3}:
[:, :, 1] =
 [6.0 9.0; 12.0 18.0]
 [4.0 6.0; 8.0 12.0]

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [January 27, 2023, 12:30pm UTC](https://discourse.julialang.org/t/low-rank-tensor-computation/93627/3 "2023-01-27T12:30:38Z")

</div>

```julia
T = [a*b*c for a in u, b in v, c in w]

```

is also an option. Then adding the randomness could be done with:

```julia
T .+= 0.2 .* rand(size(T))

```
