# How to sequentially select, from a large set of tuples (of matrices), those with a certain property [closed]

I'm interested in finding a computationally efficient way for selecting all tuples of matrices which have a certain property.

The property I'm interested in is that I want the column sum, of f[the sum of matrices in the tuple], to contain only distinct elements.

f[x_] = Piecewise[{{1, x >= 2}}, 0]


The below code specifies this property

h[{{{x1_, x2_, x3_}, {x4_, x5_, x6_}, {x7_, x8_, x9_}}, {{y1_, y2_,
y3_}, {y4_, y5_, y6_}, {y7_, y8_, y9_}}, {{z1_, z2_, z3_}, {z4_,
z5_, z6_}, {z7_, z8_, z9_}}}] = Piecewise[{{1,
f[x1 + y1 + z1] + f[x4 + y4 + z4] + f[x7 + y7 + z7] !=
f[x2 + y2 + z2] + f[x5 + y5 + z5] + f[x8 + y8 + z8] &&
f[x2 + y2 + z2] + f[x5 + y5 + z5] + f[x8 + y8 + z8]  !=
f[x3 + y3 + z3] + f[x6 + y6 + z6] + f[x9 + y9 + z9] &&
f[x1 + y1 + z1] + f[x4 + y4 + z4] + f[x7 + y7 + z7] !=
f[x3 + y3 + z3] + f[x6 + y6 + z6] + f[x9 + y9 + z9]}}, 0]


However, there are certain restrictions on the sorts of matrices I want to collect into these triples. The below code specifies these restrictions:

g[{{x1_, x2_, x3_}, {x4_, x5_, x6_}, {x7_, x8_, x9_}}] = Piecewise[{{1,
x1 + x4 + x7 != x2 + x5 + x8 && x2 + x5 + x8 != x3 + x6 + x9 &&
x1 + x4 + x7 != x3 + x6 + x9 }}, 0]


Each individual matrix in the triple must itself have a column sum with only distinct elements. And moreover, contain only elements of {0,1}, their trace equal to 0, and the sum of any symmetrical elements w.r.t the main diagonal must be 1

W = Tuples[Select[Tuples[Tuples[{0, 1}, 3], 3], Tr[#] ==
0 && #[[1, 2]] != #[[2, 1]] && #[[1, 3]] != #[[3, 1]] && #[[2,
3]] != #[[3, 2]] && g[#] == 1 &], 3]


W creates triples of such matrices

Q = Select[W, h[#] == 0 &]


Q selects the triples which have the desired property.

However, when I try to apply the same method to either larger matrices (5x5 rather than 3x3 for example), or larger tuples (5-tuples), or both, this code crashes mathematica. I suspect this is because W is too large for my computer's memory (for example if I create 5-tuples of 4x4 elements W should have a cardinality of (4!)^5, in general the set is (N!)^r, where N is the number of matrices and r the size of the tuples drawn from them)

I suspect that there is a much more efficient way of generating Q (which is much smaller than W). Rather than generating the entirety of W, and then selecting from it, I would prefer to construct each element of W one by one, and check for the property. I only need to output the matrices with the property, those who fail to meet it can be discarded.

Any help would be greatly appreciated.

• You might consider simplifying your code in order to bring more attention to your question. You might not need to take into account x1 ... x9, simplifying to a smaller example would help. – Öskå Jun 27 '14 at 23:51
• Thank you for the suggestion. Unfortunately this is the simplest situation that is of interest for me. Also, this is already a very simple example (a 3x3 matrix). x1 to x9 stand for the 9 elements in the 3x3 matrix. And the above code gives the right output for this example. However, I am looking for an alternative way of coding this which can work for larger matrices or tuples of more matrices. – user16153 Jun 29 '14 at 11:32
• I'm voting to close this question as off-topic because it's of value in a very narrow situation, probably of use only to the OP – Dr. belisarius Sep 24 '15 at 15:35