0
$\begingroup$

We have

list = {{1, 1, 0}, {1, 0, 1}, {0, 1, 1}, {-1, 1, 0}, {1,-1, 0}, {-1, -1, 0}, {-1, 0, 1}, {1, 0, -1},{-1, 0, -1},{0, -1, 1},{0, 1, -1},{0, -1, -1}}

list2 ={{2, 0, 0}, {0, 2, 0}, {0, 0, 2}, {-2, 0, 0}, {0,-2, 0}, {0, 0, -2}}

1st Problem: I would like to obtain the list of all possible distinct three vectors from the list where the sum of these vectors gives the zero vector.

For example: {1, 1, 0}+{-1, 0, 1}+{0, -1, -1}=={0,0,0}. Hence, {1, 1, 0},{-1, 0, 1},{0, -1, -1} are one of three vectors which we want in our list.

2nd Problem: I would like to obtain the list of all possible distinct four vectors (no duplication is allowed) such that three vectors will be selected from the list, and one vector will be selected from list2 and the sum of these 4 vectors gives the zero vector.

For example: {{2,0,0},{1,1,0},{-1,-1,0},{-1,0,1},{-1,0,-1}} is in our list, where {2,0,0} is selected from list2, and the other 3 vectors are selected from the list.

$\endgroup$
1
  • 1
    $\begingroup$ For large lists where brute-force enumeration might be impractical, this might be best cast as an Integer Linear Programming problem. $\endgroup$ May 9 at 14:28
2
$\begingroup$

If you don't want duplicates (i.e., pick each 3-list at most once) and don't want permutations (i.e., only ordered solutions),

Select[Subsets[list, {3}], Total[#] == {0, 0, 0} &]
(*    {{{1, 1, 0}, {-1, 0, 1}, {0, -1, -1}},
       {{1, 1, 0}, {-1, 0, -1}, {0, -1, 1}},
       {{1, 0, 1}, {-1, 1, 0}, {0, -1, -1}},
       {{1, 0, 1}, {-1, -1, 0}, {0, 1, -1}},
       {{0, 1, 1}, {1, -1, 0}, {-1, 0, -1}},
       {{0, 1, 1}, {-1, -1, 0}, {1, 0, -1}},
       {{-1, 1, 0}, {1, 0, -1}, {0, -1, 1}},
       {{1, -1, 0}, {-1, 0, 1}, {0, 1, -1}}}    *)

There are only 8 such solutions. Permuting these will give @UlrichNeumann's 48 solutions.

For the second problem, we check if the sum of three vectors is in -list2:

Select[Subsets[list, {3}], MemberQ[-list2, Total[#]] &]
(*    {{{1, 1, 0}, {1, 0, 1}, {0, -1, -1}},
       {{1, 1, 0}, {0, 1, 1}, {-1, 0, -1}},
       {{1, 1, 0}, {-1, 0, 1}, {0, -1, 1}},
       {{1, 1, 0}, {-1, 0, 1}, {0, 1, -1}},
       {{1, 1, 0}, {1, 0, -1}, {0, -1, 1}},
       {{1, 1, 0}, {-1, 0, -1}, {0, -1, -1}},
       {{1, 0, 1}, {0, 1, 1}, {-1, -1, 0}},
       {{1, 0, 1}, {-1, 1, 0}, {0, -1, 1}},
       {{1, 0, 1}, {-1, 1, 0}, {0, 1, -1}},
       {{1, 0, 1}, {1, -1, 0}, {0, 1, -1}},
       {{1, 0, 1}, {-1, -1, 0}, {0, -1, -1}},
       {{0, 1, 1}, {-1, 1, 0}, {1, 0, -1}},
       {{0, 1, 1}, {1, -1, 0}, {-1, 0, 1}},
       {{0, 1, 1}, {1, -1, 0}, {1, 0, -1}},
       {{0, 1, 1}, {-1, -1, 0}, {-1, 0, -1}},
       {{-1, 1, 0}, {-1, 0, 1}, {0, -1, -1}},
       {{-1, 1, 0}, {1, 0, -1}, {0, -1, -1}},
       {{-1, 1, 0}, {-1, 0, -1}, {0, -1, 1}},
       {{1, -1, 0}, {-1, 0, 1}, {0, -1, -1}},
       {{1, -1, 0}, {-1, 0, -1}, {0, -1, 1}},
       {{1, -1, 0}, {-1, 0, -1}, {0, 1, -1}},
       {{-1, -1, 0}, {-1, 0, 1}, {0, 1, -1}},
       {{-1, -1, 0}, {1, 0, -1}, {0, -1, 1}},
       {{-1, -1, 0}, {1, 0, -1}, {0, 1, -1}}}    *)
$\endgroup$
1
  • $\begingroup$ Thanks @Roman, I extended the problem with two lists. How can we adapt your code for the 2nd problem? $\endgroup$
    – gunes
    May 8 at 20:43
2
$\begingroup$

Try

list = {{1, 1, 0}, {1, 0, 1}, {0, 1, 1}, {-1, 1, 0}, {1, -1,0}, {-1, -1, 0}, {-1, 0, 1}, {1, 0, -1}, {-1, 0, -1}, {0, -1,1}, {0, 1, -1}, {0, -1, -1}};

All triple combinations:

triple = Tuples[list, 3]  
Select[triple, Total[#] == {0, 0, 0} &]
(*{{{1, 1, 0}, {-1, 0, 1}, {0, -1, -1}}, 
{{1, 1, 0}, {-1,0, -1}, {0, -1, 1}},
{{1, 1, 0}, {0, -1, 1}, {-1, 0, -1}},...}*)

Mathematica finds 48 possibilities!

Answer second question

slist = Subsets[list, {3}];
Table[{li, Select[slist,Total[ #] == -li &]}, {li, list2}]
$\endgroup$
7
  • $\begingroup$ Thanks @Ulrich Neumann I extended the problem with two lists. What do you suggest for solving the 2nd problem, where duplication is not allowed $\endgroup$
    – gunes
    May 9 at 6:26
  • $\begingroup$ Many thanks for the second answer @Ulrich Neumann. I would like to ask how can we improve your code (in a general sense) for the second answer. For instance, let say we want to take three vectors from list, and two vectors from list2 for which sum of these will give zero vector (duplication is not allowed). In general we choose "a" vectors from list, and "b" (b>1) vectors from list2. So in this case 1st line of your code will be slist = Subsets[list, {a}]; But what will be the second part? $\endgroup$
    – gunes
    May 9 at 9:26
  • 1
    $\begingroup$ Perhaps listb=Subsets[list2,{b}] and Table[{li, Select[slist,Total[ #] == -Total[li] &]}, {li, listb}]? $\endgroup$ May 9 at 9:36
  • 1
    $\begingroup$ In this case you probably have to use Tuples instead of Subsets I think. $\endgroup$ Jul 8 at 6:39
  • 1
    $\begingroup$ @gunes Perhaps Association is what you're looking for. Please ask a new question, if you need further assistance. $\endgroup$ Jul 9 at 6:08

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.