# Generate symmetric random tensor

I would like to generate a table $$T$$ of random values of rank $$p$$ such that my table is fully symmetric: If I swap any indices I get the same value. For example when $$p=3$$ I would like $$T_{ijk}$$ to be random with the following symmetry: $$T_{ijk}=T_{ikj}=T_{jki}=T_{jik}=T_{kij}=T_{kji}$$

For the case $$p=2$$, it boils down to generate random matrices and I can simply take the upper triangular part and take its transpose.

I would like something in those lines for example:

T=RandomVariate[NormalDistribution[0, 1],{n,n,n}];


But here $$T$$ is not symmetric. How could I obtain $$T$$ such that for any permutations of its indices I get the same value?

Along the same lines of the answer by Daniel Huber, I think what you need here is this combination:

RandomSymmetrizedArray[dims_, sym_, dist_] := Normal@ SymmetrizedArray[_ :> RandomVariate[dist], dims, sym]


This will give you a random array of the specified dimensions, with the given symmetry (in your case Symmetric[All]) and with the independent components of the array distributed according to the given distribution. So what you are looking for is

RandomSymmetrizedArray[{2, 2, 2}, Symmetric[All], NormalDistribution[0, 1]]


but you could also want a rank 4 antisymmetric array in dimension 5 with independent components distributed uniformly between 3 and 7:

RandomSymmetrizedArray[{5, 5, 5, 5}, Antisymmetric[All], UniformDistribution[{3, 7}]]


If you want an array which is only partially symmetric you can specify the levels to symmetrize with something like Symmetric[{1, 3, 4}]. Recall that you can always check the symmetry of the result with TensorSymmetry.

Here is an example for a total symmetric rank p tensor with dimensions: {n,n,..}. For the example we choose p=3 and n=4:

p = 3; n = 4;


We first create some data to fill in the tensor. As we only need the symmetric components, we write a nested Table where each index runs only up the the preceding index. "iter" contains the iteration specifications and "vars" the belonging variables. Finally, values contains the different symmetric values:

iter = Table[{Symbol["i" <> ToString[i]], Symbol["i" <> ToString[i - 1]]}, {i, p}] /. i0 -> n;
vars = iter[[All, 1]];
values = Flatten[Table[vars -> RandomReal[{-1, 1}], Evaluate[Sequence @@ iter]]];


With the values we can now define the symmtric tensor, using SymmetrizedArray:

arr = SymmetrizedArray[values, Table[n, p], Symmetric[Range[p]] ];
arr // Normal // MatrixForm 