2
$\begingroup$

I'm trying to create a boolean expression from a list of lists of variables such that the elements of one list are not equal to the elements of all other lists. I thought this might work:

list = {{a, b}, {c}, {d}};
Outer[Unequal, list]
(*{{True, True}, {True}, {True}} *)

I was hoping to generate: {{a!=c,b!=c},{c!=d}}

The larger problem I'm trying to solve is to generate a list of expressions where elements in the same list are set equal and elements in different lists are set not equal: {a==b,c==c,d==d,a!=c,b!=c,a!=d,b!=d,c!=d}

Is there a way to use Unequal so that it does not evaluate the expression when used with outer?

$\endgroup$
2
  • 1
    $\begingroup$ Do these give you any ideas? x={a,b,c};y={d,e,f,g};List@@LogicalExpand[Equal@@x] and Flatten[Outer[Unequal,x,y]] $\endgroup$
    – Bill
    Sep 3, 2023 at 4:37
  • 2
    $\begingroup$ Try: Outer[Unequal, Sequence @@ list] $\endgroup$ Sep 3, 2023 at 8:23

1 Answer 1

5
$\begingroup$

Let's augment list to help us verify:

list = {{a, b}, {c}, {d}, {e, f, g}}

In two steps:

equalities = Equal @@@ list
(* {a == b, True, True, e == f == g} *)

You can figure out what to do with the Trues. They might not hurt anything at all.

inequalities = Flatten[Outer[Unequal, Sequence @@ list]]
(* {a != c != d != e, a != c != d != f, a != c != d != g, b != c != d != e, b != c != d != f, b != c != d != g} *)

This is close to what you tried. Outer expects a sequence of lists, but you packed your separate lists together into a single list.

You can join them:

Join[equalities, inequalities]

Or join and remove the True:

DeleteCases[Join[equalities, inequalities], True]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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