I have written a function (version 8) that takes as an input a list of indices such as {i,j,k} and outputs a fully symmetric tensor function containing $p_i$ and $g_{ij}$ ($g_{ij}$ is itself understood to be symmetric under $i\leftrightarrow j$).
myFunc[tensorList_?ListQ] := Module[{rank = Length[tensorList], perms, i, j, k, r},
perms = Select[Permutations[tensorList, {rank}], Signature[#] == 1 &];
Return[Sum[If[rank <= 1, 1, 2^(1 - r)/(r! (rank - 2 r)!)]*
Factor[Sum[
Product[Subscript[p, perms[[i]][[j]]],
{j, 1, rank - 2 r}]
Product[Subscript[g, Times @@ perms[[i]][[2 k - 1 ;; 2 k]]],
{k, rank/2 - r + 1, rank/2}], {i, 1, Max[1, rank!/2]}]]
f[2 r, rank], {r, 0, rank/2}]]
]
Example of usage:
myFunc[{i}] returns $f[0, 1]p_i$
myFunc[{i,j}] returns $f[2, 2]g_{ij}+f[0,2]p_i p_j$.
etc.
Problem: My function only works when all elements of input list have unique names like {i,j,k}. I don't know how to modify it so that it can accept lists like {i,j,j}.
for example, I'd like myFunc[{i,i}] to return $f[2,2]\,g_{i^2}+f[0,2]p_i p_i$.
and yes, I know that I'm multiplying indices of g, which I'd like to keep.
Any hints please? (and any pointers on ethics of function construction would be nice!)