# Is there an easy way to do set counting in Mathematica?

I'm using counts of elements in sets, along with logic. So, for instance, I'd like to determine if

Count[$a \land \neg b$] = Count [$a$] - Count[$a \land b$]

...where Count[x] counts the total number of elements in a generic set. I don't want to really create the set $x$, I just want to be able to use the rules of logic alongside addition and subtraction of counts of elements of generic sets.

In general, I'd like to be able to use all of the rules of propositional logic. I'm really wondering, then, if there is some way to streamline the process of counting elements of sets. I've been doing the math by pencil and paper, and I sometimes run into problems with logic errors. Plus, it seems like this should be an easy problem for veteran Mathematica users. So can someone find an easy way to do this "set arithmetic" for generic sets?

• I don't think Mathematica has this functionality builtin (the function Count[] does something else entirely), but it would be interesting to create something like this. Technically, your Count would be a functor since it acts on the category of sets. Currently, you can't set Length, so you'd need something like SetCardinality to do operations. Brief googling suggests no one has done this yet... – barrycarter Jan 27 '16 at 20:09
• I'm guessing that setting the cardinality may be all that we need. We essentially have 2 cardinalities that we work with. We have the total number of variables in the system, say $n$. Then we have the total number of variables in SetCount[x], which we can say is $m$. We can determine SetCount[x], assuming $m$ variables, and then multiply this by $2^{n-m}$, to give us the total cardinality in the system. Anyways, it's just a thought... That still seems hard to work with. – Matt Groff Jan 27 '16 at 20:21
• @Matt Groff Could you edit your post to show simple typical example sets 'a' and 'b' and a description of how you think each step of your Count[a∧¬b] = Count [a] - Count[a∧b] should evaluate? – Bill Jan 28 '16 at 1:51
• Have you looked at SatisfiabilityCount? – kirma Jan 28 '16 at 5:20
• @kirma No, that's almost exactly what I'm looking for, so thanks! I can fairly easily modify it to get the counts I'm after. – Matt Groff Jan 28 '16 at 16:42

I am now experimenting with this at: https://github.com/barrycarter/bcapps/blob/master/MATHEMATICA/bc-solve-mathematica-104993.m which is currently just one line:

card[HoldPattern[Union[a_,b_]]] = card[a] + card[b] - card[Intersection[a,b]]

which yields this error:

Intersection::normal: Nonatomic expression expected at position 1 in Intersection[a, b].

but still kind of sort of maybe works, with lots of errors:

Out= card[a] + card[b] - card[Intersection[a, b]]

In:= card[s] = 5

Out= 5

In:= card[t] = 7

Out= 7

In:= card[Intersection[s,t]] = 2

Intersection::normal:
Nonatomic expression expected at position 1 in Intersection[s, t].

Out= 2

In:= card[Union[s,t]]

Union::normal: Nonatomic expression expected at position 1 in Union[s, t].

Intersection::normal:
Nonatomic expression expected at position 1 in Intersection[s, t].

Out= 10


This only works because Mathematica doesn't recognize Union[s,t], so I'm not sure this is a good idea. Contributors welcomed!