Primary Operators¶
-
pyeda.boolalg.expr.
Not
(x, simplify=True)[source]¶ Expression negation operator
If simplify is
True
, return a simplified expression.
pyeda.boolalg.expr
— Expressions¶The pyeda.boolalg.expr
module implements
Boolean functions represented as expressions.
Data Types:
A nested tuple of entries that represents an expression. It is defined recursively:
ast := ('const', bool)
| ('var', names, indices)
| ('not', ast)
| ('implies', ast, ast)
| ('ite', ast, ast, ast)
| (func, ast, ...)
bool := 0 | 1
names := (name, ...)
indices := (index, ...)
func := 'or'
| 'nor'
| 'and'
| 'nand'
| 'xor'
| 'xnor'
| 'equal'
| 'unequal'
| 'onehot0'
| 'onehot'
| 'majority'
| 'achillesheel'
Interface Functions:
exprvar()
— Return a unique expression variableexpr()
— Convert an arbitrary object into an Expressionast2expr()
— Convert an abstract syntax tree to an Expressionexpr2dimacscnf()
— Convert an expression into an equivalent DIMACS CNFupoint2exprpoint()
— Convert an untyped point to an Expression pointNot()
— Expression negation operatorOr()
— Expression disjunction (sum, OR) operatorAnd()
— Expression conjunction (product, AND) operatorNor()
— Expression NOR (not OR) operatorNand()
— Expression NAND (not AND) operatorXor()
— Expression exclusive or (XOR) operatorXnor()
— Expression exclusive nor (XNOR) operatorEqual()
— Expression equality operatorUnequal()
— Expression inequality operatorImplies()
— Expression implication operatorITE()
— Expression If-Then-Else (ITE) operatorOneHot0()
OneHot()
Majority()
AchillesHeel()
Mux()
Interface Classes:
pyeda.boolalg.expr.
exprvar
(name, index=None)[source]¶Return a unique Expression variable.
A Boolean variable is an abstract numerical quantity that may assume any
value in the set \(B = \{0, 1\}\).
The exprvar
function returns a unique Boolean variable instance
represented by an expression.
Variable instances may be used to symbolically construct larger expressions.
A variable is defined by one or more names,
and zero or more indices.
Multiple names establish hierarchical namespaces,
and multiple indices group several related variables.
If the name
parameter is a single str
,
it will be converted to (name, )
.
The index
parameter is optional;
when empty, it will be converted to an empty tuple ()
.
If the index
parameter is a single int
,
it will be converted to (index, )
.
Given identical names and indices, the exprvar
function will always
return the same variable:
>>> exprvar('a', 0) is exprvar('a', 0)
True
To create several single-letter variables:
>>> a, b, c, d = map(exprvar, 'abcd')
To create variables with multiple names (inner-most first):
>>> fifo_push = exprvar(('push', 'fifo'))
>>> fifo_pop = exprvar(('pop', 'fifo'))
See also
For creating arrays of variables with incremental indices,
use the pyeda.boolalg.bfarray.exprvars()
function.
pyeda.boolalg.expr.
expr
(obj, simplify=True)[source]¶Convert an arbitrary object into an Expression.
pyeda.boolalg.expr.
expr2dimacscnf
(expr)[source]¶Convert an expression into an equivalent DIMACS CNF.
pyeda.boolalg.expr.
upoint2exprpoint
(upoint)[source]¶Convert an untyped point to an Expression point.
pyeda.boolalg.expr.
Not
(x, simplify=True)[source]¶Expression negation operator
If simplify is True
, return a simplified expression.
pyeda.boolalg.expr.
Nor
(*xs, simplify=True)[source]¶Expression NOR (not OR) operator
If simplify is True
, return a simplified expression.
pyeda.boolalg.expr.
Nand
(*xs, simplify=True)[source]¶Expression NAND (not AND) operator
If simplify is True
, return a simplified expression.
pyeda.boolalg.expr.
Xor
(*xs, simplify=True)[source]¶Expression exclusive or (XOR) operator
If simplify is True
, return a simplified expression.
pyeda.boolalg.expr.
Xnor
(*xs, simplify=True)[source]¶Expression exclusive nor (XNOR) operator
If simplify is True
, return a simplified expression.
pyeda.boolalg.expr.
Equal
(*xs, simplify=True)[source]¶Expression equality operator
If simplify is True
, return a simplified expression.
pyeda.boolalg.expr.
Unequal
(*xs, simplify=True)[source]¶Expression inequality operator
If simplify is True
, return a simplified expression.
pyeda.boolalg.expr.
OneHot0
(*xs, simplify=True, conj=True)[source]¶Return an expression that means “at most one input function is true”.
If simplify is True
, return a simplified expression.
If conj is True
, return a CNF.
Otherwise, return a DNF.
pyeda.boolalg.expr.
OneHot
(*xs, simplify=True, conj=True)[source]¶Return an expression that means “exactly one input function is true”.
If simplify is True
, return a simplified expression.
If conj is True
, return a CNF.
Otherwise, return a DNF.
pyeda.boolalg.expr.
Majority
(*xs, simplify=True, conj=False)[source]¶Return an expression that means “the majority of input functions are true”.
If simplify is True
, return a simplified expression.
If conj is True
, return a CNF.
Otherwise, return a DNF.
pyeda.boolalg.expr.
Expression
[source]¶Boolean function represented by a logic expression
See also
This is a subclass of pyeda.boolalg.boolfunc.Function
An expression is a tree data structure with operators at the branches, and constants/literals at the leaves. A literal is a variable or its complement.
There are two ways to construct an expression:
Use the exprvar
function to create expression variables,
and use the Python ~|&^
operators for NOT, OR, AND, XOR.
Additionally, expressions overload >>
for IMPLIES.
For example:
>>> a, b, c, d, p, q = map(exprvar, 'abcdpq')
>>> f = ~a | b & c ^ d
>>> g = p >> q
To create unsimplified expressions, use class constructors:
>>> ExprOr(0, a)
Or(0, a)
To create simplified expressions,
use function form operators with simplify=True
,
or Python ~|&^
operators:
>>> Or(0, a)
a
>>> 0 | a
a
__rshift__
(g)[source]¶Boolean implication operator
\(f\) | \(g\) | \(f \implies g\) |
---|---|---|
0 | 0 | 1 |
0 | 1 | 1 |
1 | 0 | 0 |
1 | 1 | 1 |
simplified
¶Return True if the expression is simplified.
depth
¶Return the depth of the expression tree.
Expression depth is defined recursively:
expand
(vs=None, conj=False)[source]¶Return the Shannon expansion with respect to a list of variables.
to_nnf
(conj=False)[source]¶Return the expression in negation normal form (NNF).
An NNF expression is one of the following:
Parameters
flatten
(op)[source]¶Return a factored, flattened expression.
Use the distributive law to flatten all nested expressions:
cover
¶Return the DNF expression as a cover of cubes.