# Low rank tensor computation

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)

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

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

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

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

with …

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

``````2×1×1 Array{Matrix{Float64}, 3}:
[:, :, 1] =
[6.0 9.0; 12.0 18.0]
[4.0 6.0; 8.0 12.0]
``````
``````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:

``````T .+= 0.2 .* rand(size(T))
``````
1 Like