Skip to content

Commit

Permalink
Add NHot function to expr module
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdrake committed Mar 4, 2015
1 parent ca4ca4b commit 5f69ac7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
26 changes: 26 additions & 0 deletions pyeda/boolalg/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
* :func:`OneHot0`
* :func:`OneHot`
* :func:`NHot`
* :func:`Majority`
* :func:`AchillesHeel`
* :func:`Mux`
Expand Down Expand Up @@ -495,6 +496,31 @@ def OneHot(*xs, simplify=True, conj=True):
return _expr(y)


def NHot(n, *xs, simplify=True):
"""
Return an expression that means
"exactly N input functions are true".
If *simplify* is ``True``, return a simplified expression.
"""
if not isinstance(n, int):
raise TypeError("expected n to be an int")
if not 0 <= n <= len(xs):
fstr = "expected 0 <= n <= {}, got {}"
raise ValueError(fstr.format(len(xs), n))

xs = {Expression.box(x).node for x in xs}
terms = list()
for hots in itertools.combinations(xs, n):
colds = xs - set(hots)
not_colds = tuple(map(exprnode.not_, colds))
terms.append(exprnode.and_(*(hots + not_colds)))
y = exprnode.or_(*terms)
if simplify:
y = y.simplify()
return _expr(y)


def Majority(*xs, simplify=True, conj=False):
"""
Return an expression that means
Expand Down
2 changes: 1 addition & 1 deletion pyeda/inter.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
exprvar, expr,
ast2expr, expr2dimacscnf, expr2dimacssat, upoint2exprpoint,
Not, Or, And, Nor, Nand, Xor, Xnor, Equal, Unequal, Implies, ITE,
Nor, Nand, OneHot0, OneHot, Majority, AchillesHeel,
Nor, Nand, OneHot0, OneHot, NHot, Majority, AchillesHeel,
Mux, ForAll, Exists,
Expression,
NormalForm, DisjNormalForm, ConjNormalForm, DimacsCNF,
Expand Down

0 comments on commit 5f69ac7

Please sign in to comment.