Permute association key and generate a sparse array
Maybe with SymmetrizedArray
?
size = 4;
rank = 4;
Av = <|{1, 4, 2, 3} -> a, {2, 4, 2, 4} -> b|>;
symmetries = {
{{4, 2, 3, 1}, 1},
{{4, 3, 2, 1}, 1},
{{1, 3, 2, 4}, 1},
{{2, 1, 4, 3}, 1},
{{3, 1, 4, 2}, 1},
{{3, 4, 1, 2}, 1},
{{2, 4, 1, 3}, 1}
};
A = SparseArray[
SymmetrizedArray[Normal[Av], ConstantArray[n, rank], symmetries]
]
In the list symmetries
, each entry is a pair {p,s}
of a permutation p
that can be applied and a sign s
(1
, -1
) that tells us whether the array is meant to be symmetric (s = 1
means symmetric, s = -1
means antisymmetric).
In fact, it suffices to provide only a set of generators of the symmetry group. For example,
symmetries = {
{{4, 2, 3, 1}, 1},
{{1, 3, 2, 4}, 1},
{{2, 4, 1, 3}, 1}
};
would lead to the same result
pg = PermutationGroup[{{1, 2, 3, 4}, {4, 2, 3, 1}, {4, 3, 2, 1}, {1,
3, 2, 4}, {2, 1, 4, 3}, {3, 1, 4, 2}, {3, 4, 1, 2}, {2, 4, 1, 3}}];
ClearAll[sAbuild]
sAbuild = SparseArray[KeyValueMap[Alternatives @@
GroupOrbits[pg, {#}, Permute][[1]] -> #2 &]@#, #2] &;
dims = {4, 4, 4, 4};
sAbuild[Av, dims]
Based on Henrik's answer, we can use a smaller set of generators to get the same result:
pg = PermutationGroup @ {{4, 2, 3, 1}, {1, 3, 2, 4}, {2, 4, 1, 3}};