({{, (RefLink[BooleanCountingFunction, paclet:ref/BooleanCountingFunction])[(Subscript[k, max]),n]
represents a Boolean function of n variables that gives (RefLink[True, paclet:ref/True]) if at most (Subscript[k, max]) variables are (RefLink[True, paclet:ref/True]).}, {, (RefLink[BooleanCountingFunction, paclet:ref/BooleanCountingFunction])[{k},n]
represents a function of n variables that gives (RefLink[True, paclet:ref/True]) if exactly k variables are (RefLink[True, paclet:ref/True]).}, {, (RefLink[BooleanCountingFunction, paclet:ref/BooleanCountingFunction])[{(Subscript[k, min]),(Subscript[k, max])},n]
represents a function that gives (RefLink[True, paclet:ref/True]) if between (Subscript[k, min]) and (Subscript[k, max]) variables are (RefLink[True, paclet:ref/True]).}, {, (RefLink[BooleanCountingFunction, paclet:ref/BooleanCountingFunction])[{{(Subscript[k, 1]),(Subscript[k, 2]),…}},n]
represents a function that gives (RefLink[True, paclet:ref/True]) if exactly (Subscript[k, i]) variables are (RefLink[True, paclet:ref/True]).}, {, (RefLink[BooleanCountingFunction, paclet:ref/BooleanCountingFunction])[spec,{(Subscript[a, 1]),(Subscript[a, 2]),…}]
gives the Boolean expression in variables (Subscript[a, i]) corresponding to the Boolean counting function specified by spec.}, {, (RefLink[BooleanCountingFunction, paclet:ref/BooleanCountingFunction])[spec,{(Subscript[a, 1]),(Subscript[a, 2]),…},form]
gives the Boolean expression in the form specified by form.}})

OpenersNotesSection(Spacer1)Details

(RefLink[BooleanCountingFunction, paclet:ref/BooleanCountingFunction])[spec] gives a Boolean function object that works like (RefLink[Function, paclet:ref/Function]).

(RefLink[BooleanCountingFunction, paclet:ref/BooleanCountingFunction])[spec][(Subscript[a, 1]),(Subscript[a, 2]),…] gives an implicit representation equivalent to the explicit Boolean expression (RefLink[BooleanCountingFunction, paclet:ref/BooleanCountingFunction])[spec,{(Subscript[a, 1]),(Subscript[a, 2]),…}].

(RefLink[BooleanConvert, paclet:ref/BooleanConvert]) converts (RefLink[BooleanCountingFunction, paclet:ref/BooleanCountingFunction])[spec][vars] to an explicit Boolean expression.

(RefLink[BooleanCountingFunction, paclet:ref/BooleanCountingFunction])[{(Subscript[k, min]),(Subscript[k, max]),s},…] represents a function that gives (RefLink[True, paclet:ref/True]) if (Subscript[k, min]), (Subscript[k, min])+s, …, (Subscript[k, max]) variables are (RefLink[True, paclet:ref/True]).

Any symmetric Boolean function can be represented uniquely using (RefLink[BooleanCountingFunction, paclet:ref/BooleanCountingFunction]).

In (RefLink[BooleanCountingFunction, paclet:ref/BooleanCountingFunction])[spec,{(Subscript[a, 1]),(Subscript[a, 2]),…},form], the possible forms are as given for (RefLink[BooleanConvert, paclet:ref/BooleanConvert]).

(RefLink[BooleanCountingFunction, paclet:ref/BooleanCountingFunction])[spec,{(Subscript[a, 1]),(Subscript[a, 2]),…}] by default gives an expression in disjunctive normal form (DNF).

OpenersPrimaryExamplesSection(Spacer1)Examples  (18)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

At most two conditions are true:

BooleanCountingFunction[2,4][a,b,c,d]

Convert to a disjunctive normal form:

BooleanConvert[%]

OpenersExampleSection1(Spacer1)Scope  (6)

Specify that f is true when at most 2 arguments are true:

f=BooleanCountingFunction[2,4]

{f[True,True,False,False],f[True,True,False,True]}

Exactly 2 arguments are true:

g=BooleanCountingFunction[{2},4]

{g[True,False,True,False],g[False,True,False,False]}

Between 2 and 3 arguments are true:

h=BooleanCountingFunction[{2,3},4]

{h[True,False,True,True],h[True,False,False,False]}

1, 3, or 5 arguments are true:

i=BooleanCountingFunction[{1,5,2},5]

{i[True,False,True,False,True],i[True,False,False,False,True]}

Specify that f is true when exactly 1, 4, or 5 arguments are true:

f=BooleanCountingFunction[{{1,4,5}},5]

{f[False,True,True,True,True],f[False,True,False,False,True]}

(RefLink[BooleanCountingFunction, paclet:ref/BooleanCountingFunction]) is by default preserved in function form:

f=BooleanCountingFunction[{2,3},5][a,b,c,d,e]

Use (RefLink[BooleanConvert, paclet:ref/BooleanConvert]) to convert to other forms:

BooleanConvert[f,"DNF"]

BooleanConvert[f,"CNF"]

(RefLink[BooleanCountingFunction, paclet:ref/BooleanCountingFunction]) is automatically converted when given an explicit list of variables:

BooleanCountingFunction[{2,3},{a,b,c,d,e}]

The expanded forms can be large when the number of variables grows:

Length@BooleanCountingFunction[{5,15},Array[a,20]]

The performance gain in evaluating the function form can be substantial:

f=BooleanCountingFunction[{5,15},Array[a,20]];

g=BooleanCountingFunction[{5,15},20]@@Array[a,20];

rules=Thread[Array[a,20]->RandomChoice[{True,False},20]];

Timing@(f/.rules)

Timing@(g/.rules)

Constant arguments are reduced:

BooleanCountingFunction[{3,4},6]@@{a,True,b,False,c,d}

BooleanCountingFunction[{3,4},6]@@{a,True,b,True,c,d}

Extreme cases are automatically converted to formulas:

BooleanCountingFunction[0,4]@@{a,b,c,d}

BooleanCountingFunction[4,4]@@{a,b,c,d}

BooleanCountingFunction[{4},4]@@{a,b,c,d}

OpenersExampleSection2(Spacer1)Applications  (4)

Create new primitives that are true when at most, at least, or exactly k arguments are true:

AtMostK[k_,v_]:=BooleanCountingFunction[{0,k},Length[v]]@@v

AtLeastK[k_,v_]:=BooleanCountingFunction[{k,Length[v]},Length[v]]@@v

ExactlyK[k_,v_]:=BooleanCountingFunction[{k},Length[v]]@@v

Create a number of disk regions along the unit circle:

ineqs=Table[(x-Cos[i 2Pi/6])^2+(y-Sin[i 2Pi/6])^2<1,{i,0,5}]

Show the newly combined regions:

{RegionPlot[AtMostK[2,ineqs],{x,-2,2},{y,-2,2},PlotPoints->35],
RegionPlot[AtLeastK[2,ineqs],{x,-2,2},{y,-2,2},PlotPoints->35],RegionPlot[ExactlyK[2,ineqs],{x,-2,2},{y,-2,2},PlotPoints->35]}

Integrate over these regions:

NIntegrate[Boole[AtLeastK[2,ineqs]],{x,-2,2},{y,-2,2}]

Integrate[Boole[AtLeastK[2,ineqs]],{x,-2,2},{y,-2,2}]

Define a Boolean function that is true when the number of true arguments is k modulo m:

BooleanModCount[{k_,m_},v_]:=BooleanCountingFunction[{Mod[k,m],Length[v],m},Length[v]]@@v

When k=0 and m=2, you get (RefLink[Xnor, paclet:ref/Xnor]):

TautologyQ[Equivalent[Xnor@@Array[a,10],BooleanModCount[{0,2},Array[a,10]]]]

When k=1 and m=2, you get (RefLink[Xor, paclet:ref/Xor]):

TautologyQ[Equivalent[Xor@@Array[a,10],BooleanModCount[{1,2},Array[a,10]]]]

For other values of k and m, you get new functionality:

Table[BooleanModCount[{1,3},Join[ConstantArray[True,{k}],ConstantArray[False,{10-k}]]],{k,0,10}]

Boole[%]

The 2D truth table:

ArrayPlot[BooleanTable[BooleanModCount[{1,3},Array[a,10]],Array[a,5],Array[a,5,6]],ColorRules->{False->White,True->Black}]

Define a Boolean function that sorts a list of truth values:

BooleanSort[v_]:=Table[BooleanCountingFunction[{k,Length[v]},Length[v]]@@v,{k,Length[v],1,-1}]

The resulting list is always in sorted order:

BooleanTable[BooleanSort[{a,b}],{a,b}]

BooleanTable[BooleanSort[{a,b,c}],{a,b,c}]

Find the mean time to failure for a system that needs two out of three components to work:

{𝒟1,𝒟2,𝒟3}={ExponentialDistribution[(Subscript[λ, 1])],ExponentialDistribution[(Subscript[λ, 2])],ExponentialDistribution[(Subscript[λ, 3])]};

ℛ=ReliabilityDistribution[BooleanCountingFunction[{2,3},{x,y,z}],{{x,𝒟1},{y,𝒟2},{z,𝒟3}}];

Mean[ℛ]

OpenersExampleSection3(Spacer1)Properties & Relations  (6)

(RefLink[BooleanCountingFunction, paclet:ref/BooleanCountingFunction]) is symmetric in its arguments:

f=BooleanCountingFunction[{1,2},3]

f@@@Permutations[{True,False,True}]

Logical combinations of (RefLink[BooleanCountingFunction, paclet:ref/BooleanCountingFunction]) correspond to set operations on indices:

e1=BooleanCountingFunction[{{1,3}},4][a,b,c,d]∨BooleanCountingFunction[{{1,4}},4][a,b,c,d];

e2=BooleanCountingFunction[{Union[{1,3},{1,4}]},4][a,b,c,d];

TautologyQ[Equivalent[e1,e2]]

The basic specification can equivalently be specified using (RefLink[Range, paclet:ref/Range]):

f=BooleanCountingFunction[{1,5,2},5]

g=BooleanCountingFunction[{Range[1,5,2]},5]

TautologyQ[Equivalent[f@@Array[a,5],g@@Array[a,5]],Array[a,5]]

Many primitives can be expressed in terms of (RefLink[BooleanCountingFunction, paclet:ref/BooleanCountingFunction]):

vars=Array[x,10];

(RefLink[And, paclet:ref/And]):

and[v__]:=BooleanCountingFunction[{Length[{v}]},Length[{v}]][v]

TautologyQ[Equivalent[And@@vars,and@@vars]]

(RefLink[Or, paclet:ref/Or]):

or[v__]:=BooleanCountingFunction[{1,Length[{v}]},Length[{v}]][v]

TautologyQ[Equivalent[Or@@vars,or@@vars]]

(RefLink[Nand, paclet:ref/Nand]):

nand[v__]:=BooleanCountingFunction[Length[{v}]-1,Length[{v}]][v]

TautologyQ[Equivalent[Nand@@vars,nand@@vars]]

(RefLink[Nor, paclet:ref/Nor]):

nor[v__]:=BooleanCountingFunction[0,Length[{v}]][v]

TautologyQ[Equivalent[Nor@@vars,nor@@vars]]

(RefLink[Xor, paclet:ref/Xor]):

xor[v__]:=BooleanCountingFunction[{1,Length[{v}],2},Length[{v}]][v]

TautologyQ[Equivalent[Xor@@vars,xor@@vars]]

(RefLink[Xnor, paclet:ref/Xnor]):

xnor[v__]:=BooleanCountingFunction[{0,Length[{v}],2},Length[{v}]][v]

TautologyQ[Equivalent[Xnor@@vars,xnor@@vars]]

(RefLink[Equivalent, paclet:ref/Equivalent]):

equivalent[v__]:=BooleanCountingFunction[{{0,Length[{v}]}},Length[{v}]][v]

TautologyQ[Equivalent[Equivalent@@vars,equivalent@@vars]]

(RefLink[Majority, paclet:ref/Majority]):

majority[v__]:=BooleanCountingFunction[{Floor[Length[{v}]/2]+1,Length[{v}]},Length[{v}]][v]

TautologyQ[Equivalent[Majority@@vars,majority@@vars]]

The size of the truth set for (RefLink[BooleanCountingFunction, paclet:ref/BooleanCountingFunction]) is the length of (RefLink[Subsets, paclet:ref/Subsets]):

SatisfiabilityCount[BooleanCountingFunction[{5,10},15]]

Length[Subsets[Range[15],{5,10}]]

The size of the truth set for (RefLink[BooleanCountingFunction, paclet:ref/BooleanCountingFunction]) can be given by a combinatorial sum:

SatisfiabilityCount[BooleanCountingFunction[{300,700},1000]@@Array[a,1000]]

Sum[Binomial[1000,k],{k,300,700}]

%%==%

OpenersExampleSection4(Spacer1)Neat Examples  (1)

(RefLink[BooleanCountingFunction, paclet:ref/BooleanCountingFunction]) for when exactly i variables are true has disjoint truth sets:

Table[ArrayPlot[BooleanTable[BooleanCountingFunction[{i},10]@@Array[x,10],Array[x,5],Array[x,5,6]],ColorRules->{False->None,True->ColorData["Rainbow"][i/11]}],{i,0,10}]

Show[%]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Majority, paclet:ref/Majority])  ▪  (RefLink[Xor, paclet:ref/Xor])  ▪  (RefLink[Xnor, paclet:ref/Xnor])  ▪  (RefLink[Equivalent, paclet:ref/Equivalent])  ▪  (RefLink[BooleanFunction, paclet:ref/BooleanFunction])  ▪  (RefLink[BooleanConsecutiveFunction, paclet:ref/BooleanConsecutiveFunction])  ▪  (RefLink[BooleanConvert, paclet:ref/BooleanConvert])  ▪  (RefLink[SymmetricPolynomial, paclet:ref/SymmetricPolynomial])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Boolean Computation, paclet:guide/BooleanComputation])}, {▪(RefLinkPlain[Reliability, paclet:guide/Reliability])}})}})