I have a rank four SparseArray of dimension {40, 40, 40, 40}. I wish to apply a linear operator to it, whose form I know abstractly. That is, I have a simple closed form for the action of the operator on each basis vector. Currently, I am implementing the action of this operator as:
operator[s_SparseArray] := DeleteCases[ArrayRules[s], HoldPattern[{_, _} -> 0]] // Map[operator] // Total // SparseArray;
operator[{a_, b_, c_, d_} -> e_] :=
Total[SparseArray[#, {40,40,40,40}] & /@ {
{a, b, c, d} -> e/4,
{b, a, c, d} -> -e/4,
{b, c, a, d} -> -e/4,
{b, c, d, a} -> -e/4,
{c, b, a, d} -> e/4,
{c, b, d, a} -> e/4,
{c, d, b, a} -> e/4,
{d, c, b, a} -> -e/4
}];
This approach, with a matrix with 73695 non-zero elements is taking ~ 6 seconds on my laptop. Are there any better ideas for implementing something like this? I do this thousands of times, so any order of magnitude games would be important.