6
$\begingroup$

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!

$\endgroup$
7
$\begingroup$

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

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Alex Reinking Oct 15 '14 at 1:12
  • $\begingroup$ Look at documentation pages for Pattern, Blank, BlankSequence, and BlankNullSequence. $\endgroup$ – Bob Hanlon Oct 15 '14 at 1:20
5
$\begingroup$

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.