2
$\begingroup$

Here is part of a code I wrote:

Select[Subsets[Range[16]], 
   MemberQ[#, 2] && ! 
       ContainsAll[
         Level[Position[ζ[[All, 2]], 1], {2}]][#] ∨ 
     MemberQ[#, 3] && ! 
       ContainsAll[
         Level[Position[ζ[[All, 3]], 1], {2}]][#] ∨ 
     MemberQ[#, 4] && ! 
       ContainsAll[
         Level[Position[ζ[[All, 4]], 1], {2}]][#] ∨ 
     MemberQ[#, 5] && ! 
       ContainsAll[
         Level[Position[ζ[[All, 5]], 1], {2}]][#] ∨ 
     MemberQ[#, 6] && ! 
       ContainsAll[
         Level[Position[ζ[[All, 6]], 1], {2}]][#] ∨ 
     MemberQ[#, 7] && ! 
       ContainsAll[
         Level[Position[ζ[[All, 7]], 1], {2}]][#] ∨ 
     MemberQ[#, 8] && ! 
       ContainsAll[
         Level[Position[ζ[[All, 8]], 1], {2}]][#] ∨ 
     MemberQ[#, 9] && ! 
       ContainsAll[
         Level[Position[ζ[[All, 9]], 1], {2}]][#] ∨ 
     MemberQ[#, 10] && ! 
       ContainsAll[
         Level[Position[ζ[[All, 10]], 1], {2}]][#] ∨ 
     MemberQ[#, 11] && ! 
       ContainsAll[
         Level[Position[ζ[[All, 11]], 1], {2}]][#] ∨ 
     MemberQ[#, 12] && ! 
       ContainsAll[
         Level[Position[ζ[[All, 12]], 1], {2}]][#] ∨ 
     MemberQ[#, 13] && ! 
       ContainsAll[
         Level[Position[ζ[[All, 13]], 1], {2}]][#] ∨ 
     MemberQ[#, 14] && ! 
       ContainsAll[
         Level[Position[ζ[[All, 14]], 1], {2}]][#] ∨ 
     MemberQ[#, 15] && ! 
       ContainsAll[
         Level[Position[ζ[[All, 15]], 1], {2}]][#] ∨ 
     MemberQ[#, 16] && ! 
       ContainsAll[
         Level[Position[ζ[[All, 16]], 1], {2}]][#] &]];

Is there a way to condense all the "or" commands into a single line?

$\endgroup$
5
  • $\begingroup$ Could you add an explanation of your logic in words? What properties are you trying to express to retain some subsets? $\endgroup$
    – MarcoB
    Commented Jan 15, 2021 at 13:29
  • $\begingroup$ Yes, Thank you. Here $\zeta$ is a 16 X 16, {0,1} matrix representing a poset. This part of the code gives me the (complement of ) order ideal of $\zeta$. I just don't want to have to write essentially the same line multiple times when applying the code to various sizes of posets. $\endgroup$
    – geoffrey
    Commented Jan 15, 2021 at 14:15
  • 1
    $\begingroup$ Sounds like one would use some sparse matrix arithmetric or Graph... A poset is basically a tree (or a forest), right? Unfortunately I do not have the time to find out what an order ideal is. Can you please explain that in short, non expert terms? And a concrete example for ζ to play with would be great. $\endgroup$ Commented Jan 15, 2021 at 14:20
  • $\begingroup$ I got the faint idea that TopologicalSort[Graph[ζ]] might bring you closer to the solution... $\endgroup$ Commented Jan 15, 2021 at 14:23
  • 2
    $\begingroup$ @geoffrey thank you for the explanation, but unfortunately that was completely over my head. What I meant was for you to tell us what properties of each subset you are targeting in plain language. Something like, it should contain this and have that other simple property, but not that. $\endgroup$
    – MarcoB
    Commented Jan 15, 2021 at 14:29

1 Answer 1

5
$\begingroup$
nzp = SparseArray[Transpose @ ζ]["AdjacencyLists"];

selector1 = Function[x, Or @@
 (MemberQ[x, #] && Length[Complement[nzp[[#]], x]] >= 1 & /@ Range[2, 16])];

Select[Subsets[Range[16]], selector1]

Alternatively,

selector2 = Function[x,  Or @@ 
 (MemberQ[x, #] && ! ContainsAll[x, nzp[[#]]] & /@ Range[2, 16])];

Select[Subsets[Range[16]], selector2] == Select[Subsets[Range[16]], selector1]
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.