Suppose $A$ is an random $n\times n$ matrix with standard normal entries. A user was able to obtain the following formula by enumerating all diagrams by hand: $$E[\operatorname{Tr}(A^3 (A^T)^3]=n^4+10n^2+4n$$
There's also this formula $$E[AA'AA'AA']=4n^2+6n^3+5n^4$$
Then
$$E[Tr[A^6 (A^T)^6]] = ???$$
How could I use Mathematica to discover these formulas automatically?
A user "Ben" provided magically compact Mathematica code to obtain the following formula for independent $A,B$: $$E||ABB^TA^T||^2_F=3 n^3 \left(n^2+n+1\right)$$
However it doesn't work out of the box for the problem at hand, perhaps someone fluent in both Mathematica and Wicks contractions can fix this -
Wick[x_Times] :=
Block[{L = List @@ x},
Sum[c[L[[1]], L[[i]]] Wick[Times @@ (Drop[L, {i}][[2 ;;]])], {i, 2,
Length[L]}]]
Wick[1] := 1
c[A_[i_, j_], A_[k_, l_]] := [Delta][i, k] [Delta][j, l]
c[__] := 0
rules = {[Delta][a1_, a1_] -> n,
[Delta][a1_, a2_]^2 -> n,
[Delta][a1_, a2_] a3_ :> (a3 /. a1 -> a2) /; Not@FreeQ[a3, a1]};
res = Wick[
A[i1, i2] B[i2, i3] B[i4, i3] A[i5, i4] A[i5, i6] B[i6, i7] B[i8,
i7] A[i1, i8]] //. rules // Simplify
Table[{n, res}, {n, 1, 5}] // TableForm

41312 n + 58312 n^2 + 25512 n^3 + 9046 n^4 + 748 n^5 + 204 n^6 + n^8. I'll add a speculative answer about this a bit later. – JimB Feb 22 '24 at 00:17