1
$\begingroup$

I tried searching but all similar questions seemed to be based around one Boolean logical statement.

I'm dealing with a nested list of pairs partitions of integers, such that the sum of the two integers equal another known integer $n$. I wish to select out of this list pairs of partitions that satisfy known inequalities between their elements. Representing a partition pair as say $Y_1=(Y_{1,1},Y_{1,2},...)$ and $Y_2=(Y_{2,1},Y_{2,2},...)$ an example of inequalities would be a pair $Y_{1,i}\geq Y_{2,i+3}$ and $Y_{2,i}\geq Y_{1,i}$. Since I need all of these statements to be true to select a pair I've been trying to combine TrueAll with Select to not much success.

An example of the code I've tried so far, for the case of partitions that add to 3, where one partition is empty is:

    Select[{{{3}, {0}}, {{2, 1}, {0}}, {{1, 1, 1}, {0}}}, 
     AllTrue[Flatten[{Table[
          Indexed[#, {2, l}] >= 
           If[l + 3 <= Length[Indexed[#, 1]], Indexed[#, {1, l + 3}], 0],
          {l, 1, Length[Indexed[#, 2]]}],
         Table[
          Indexed[#, {1, k}] >= 
           If[k <= Length[Indexed[#, 2]], Indexed[#, {2, k}], 0],
          {k, 1, Length[Indexed[#, 1]]}]}]] &]

Which is returning an empty list, but if I'm not mistaken I believe it should return all of the said lists. I think I'm using AllTrue wrong since isolating that part of the code gives me:

    AllTrue[Flatten[{Table[
        Indexed[#, {2, l}] >= 
         If[l + 3 <= Length[Indexed[#, 1]], Indexed[#, {1, l + 3}], 0],
        {l, 1, Length[Indexed[#, 2]]}],
       Table[
        Indexed[#, {1, k}] >= 
         If[k <= Length[Indexed[#, 2]], Indexed[#, {2, k}], 0],
        {k, 1, Length[Indexed[#, 1]]}]}]] & /@ {{{3}, {0}}, {{2, 
      1}, {0}}, {{1, 1, 1}, {0}}}
{AllTrue[{True, True}], AllTrue[{True, True, True}], 
 AllTrue[{True, True, True, True}]}

I'm not particularly familiar with AllTrue, or Mathematica in general but I could not find a solution in the documentation. Any help would be appreciated and if there's a better way to approach the problem.

Thank you

$\endgroup$
0

1 Answer 1

0
$\begingroup$

Replace AllTrue with Apply[And]:

Select[{{{3}, {0}}, {{2, 1}, {0}}, {{1, 1, 1}, {0}}}, 
      Apply[And][Flatten[{Table[
                Indexed[#, {2, l}] >=                       
       If[l + 3 <= Length[Indexed[#, 1]], Indexed[#, {1, l + 3}], 0],
                {l, 1, Length[Indexed[#, 2]]}],
              Table[
                Indexed[#, {1, k}] >=                       
       If[k <= Length[Indexed[#, 2]], Indexed[#, {2, k}], 0],
                {k, 1, Length[Indexed[#, 1]]}]}]] &]

{{{3}, {0}}, {{2, 1}, {0}}, {{1, 1, 1}, {0}}}

Note: AllTrue[list, test] checks if a single boolean function (test) yields true when applied to every element of list. In your case, you have multiple boolean functions applied to a single object.

$\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.