efficient programming problem

*To*: mathgroup at smc.vnet.net*Subject*: [mg84143] efficient programming problem*From*: "Michael Weyrauch" <michael.weyrauch at gmx.de>*Date*: Tue, 11 Dec 2007 06:12:35 -0500 (EST)

Hello, I am generating a sum of a product of "pairs" using the following rules (partly stolen from an earlier post of Carl Woll in this group.) wick[a_, b_] := pair[a, b]; wick[a_, b__] := Sum[Expand[pair[a, {b}[[i]]]*Delete[Unevaluated[wick[b]], i]], {i, Length[{b}]}]; pair[c[_], c[_]] := 0 pair[d[_], d[_]] := 0 So, when I evaluate wick[c[1], c[2], d[3], d[4]] I get pair[c[1], d[4]]*pair[c[2], d[3]] + pair[c[1], d[3]]*pair[c[2], d[4]] which is the desired sum of product of pairs. Now, I would like to implement an additional rule which holds for my pairs, namely that it is allowed to interchange the parameters (1,2) or (3,4), so that the above sum of pairs simplifies e.g. to 2*pair[c[1], d[4]]*pair[c[2], d[3]] A more complicated example would be to evaluate wick[c[1], c[2], d[3], d[4], c[5], c[6], d[7], d[8]] which produces a sum of 24 products of pairs. Now if I again implement the additional rule that one is allowed to interchange (1,2) or (3,4) or (5,6) ( and so on) one obtains only a sum of 3 products of pairs which reads 4*pair[c[1], d[3]]*pair[c[2], d[4]]*pair[c[5], d[7]]*pair[c[6], d[8]] + 16*pair[c[1], d[7]]*pair[c[2], d[3]]*pair[c[5], d[8]]*pair[d[4], c[6]] + 4*pair[c[1], d[7]]*pair[c[2], d[8]]*pair[d[3], c[5]]*pair[d[4], c[6]] I am looking for a method which implements this (1,2) (3,4) (5,6)... interchange symmetry in a memory and time efficient way, so that Mathematica directly produces the correctly "reduced" sum of products of pairs as exemplified above for simple cases. I have now tried two implementations, but both use too much memory and time (this is relevant of course only for much longer examples which contain say 4 times as many c[] and d[] in wick[] than the examples given), since my methods essentially first generate the some of all products of pairs generated by the rules given in the beginning of this post and then analyze the sum of terms and reduce them according to the additional interchange symmetry. It would be better if this reduction of terms could be done during the recursive generation of the sum of products of pairs. Ideas in this direction would be welcome... Thanks, Michael