# List of tuples without forbidden pair

How do you get a list of tuples in Mathematica that do not include certain pairs of elements? For example, I'm trying to list sequences of matrices of a certain length, and I do not want two adjacent elements in the sequence to be inverses of each other.

I suspect the code will end up looking like:

DropCases[Tuples[{a, b, Inverse[a], Inverse[a]}, 6], ???]


How do I do this in Mathematica?

Thanks!

CORRECTED per input from RunnyKine

Length[Tuples[{a, b, Inverse[a], Inverse[a]}, 6]]


4096

Length[
DeleteCases[
DeleteCases[Tuples[{a, b, Inverse[a], Inverse[a]}, 6],
{___, x_, Inverse[x_], ___}],
{___, Inverse[x_], x_, ___}]]


1204

Length[
DeleteCases[Tuples[{a, b, Inverse[a], Inverse[a]}, 6],
{___, x_, Inverse[x_], ___} | {___, Inverse[x_], x_, ___}]]


1204

• Thank you! Mathematica is incredible! Sorry to bother you again, but where is this documented? I searched around the Wolfram website for at least an hour before asking, and never saw (or recognized) the __ thing. Oct 15, 2014 at 1:12
• Look at documentation pages for Pattern, Blank, BlankSequence, and BlankNullSequence. Oct 15, 2014 at 1:20

Is your application time-critical? The first thing that comes into my 3:27-in-the-morning-mind is to make a pattern that tests whether a*b=1 (or with matrices a.b==IdentityMatrix[n]). Here is an example with numbers that should work for your matrices too when you adapt it

DeleteCases[
Tuples[{1, 2, 1^-1, 2^-1}, 5], {___, x_, y_, ___} /; x*y === 1]


The output looks ok at the first glance.

• OK, reading the answer of Bob I suspect you don't really want to use real matrices but keep the symbolic a and b... Oct 15, 2014 at 1:38
• +1. This looks fine to me. The OP didn't really state whether symbolic or numeric matrices is preferred. Oct 15, 2014 at 1:41
• If the list contains numeric matrices then the Dot product (x.y) would have to be an IdentityMatrix of appropriate dimensions. Oct 15, 2014 at 1:57
• This is neat, but I should have clarified that symbolic matrices are preferred in this case. Thanks for your input, though! Oct 15, 2014 at 2:15