It evaluates its arguments in order, giving False immediately
if any of them are False, and True if they are all True.
Examples
>>> from..coreimportsymbols>>> from..abcimportx,y>>> from.boolalgimportAnd>>> x&yx & y
Notes
The & operator is provided as a convenience, but note that its use
here is different from its normal use in Python, which is bitwise
and. Hence, And(a,b) and a&b will return different things if
a and b are integers.
SymPy version of False, a singleton that can be accessed via S.false.
This is the SymPy version of False, for use in the logic module. The
primary advantage of using false instead of False is that shorthand boolean
operations like ~ and >> will work as expected on this class, whereas with
False they act bitwise on 0. Functions in the logic module will return this
class when they evaluate to false.
Notes
See note in :py:class`sympy.logic.boolalg.BooleanTrue`
SymPy version of True, a singleton that can be accessed via S.true.
This is the SymPy version of True, for use in the logic module. The
primary advantage of using true instead of True is that shorthand boolean
operations like ~ and >> will work as expected on this class, whereas with
True they act bitwise on 1. Functions in the logic module will return this
class when they evaluate to true.
Notes
There is liable to be some confusion as to when True should
be used and when S.true should be used in various contexts
throughout SymPy. An important thing to remember is that
sympify(True) returns S.true. This means that for the most
part, you can just use True and it will automatically be converted
to S.true when necessary, similar to how you can generally use 1
instead of S.One.
The rule of thumb is:
“If the boolean in question can be replaced by an arbitrary symbolic
Boolean, like Or(x,y) or x>1, use S.true.
Otherwise, use True”
In other words, use S.true only on those contexts where the
boolean is being used as a symbolic representation of truth.
For example, if the object ends up in the .args of any expression,
then it must necessarily be S.true instead of True, as
elements of .args must be Basic. On the other hand,
== is not a symbolic operation in SymPy, since it always returns
True or False, and does so in terms of structural equality
rather than mathematical, so it should return True. The assumptions
system should use True and False. Aside from not satisfying
the above rule of thumb, the
assumptions system uses a three-valued logic (True, False, None),
whereas S.true and S.false represent a two-valued logic. When in
doubt, use True.
“S.true==TrueisTrue.”
While “S.trueisTrue” is False, “S.true==True”
is True, so if there is any doubt over whether a function or
expression will return S.true or True, just use ==
instead of is to do the comparison, and it will work in either
case. Finally, for boolean flags, it’s better to just use ifx
instead of ifxisTrue. To quote PEP 8:
Don’t compare boolean values to True or False
using ==.
Never use self._args, always use self.args.
Only use _args in __new__ when creating a new function.
Don’t override .args() from Basic (so that it’s easy to
change the interface in the future if needed).
Returns a canonical form of cls applied to arguments args.
The eval() method is called when the class cls is about to be
instantiated and it should return either some simplified instance
(possible of some other class), or if the class cls should be
unmodified, return None.
The >> and << operators are provided as a convenience, but note
that their use here is different from their normal use in Python, which is
bit shifts. Hence, Implies(a,b) and a>>b will return different
things if a and b are integers. In particular, since Python
considers True and False to be integers, True>>True will be
the same as 1>>1, i.e., 0, which has a truth value of False. To
avoid this issue, use the SymPy objects true and false.
Returns a canonical form of cls applied to arguments args.
The eval() method is called when the class cls is about to be
instantiated and it should return either some simplified instance
(possible of some other class), or if the class cls should be
unmodified, return None.
Returns a canonical form of cls applied to arguments args.
The eval() method is called when the class cls is about to be
instantiated and it should return either some simplified instance
(possible of some other class), or if the class cls should be
unmodified, return None.
Returns a canonical form of cls applied to arguments args.
The eval() method is called when the class cls is about to be
instantiated and it should return either some simplified instance
(possible of some other class), or if the class cls should be
unmodified, return None.
The ~ operator is provided as a convenience, but note that its use
here is different from its normal use in Python, which is bitwise
not. In particular, ~a and Not(a) will be different if a is
an integer. Furthermore, since bools in Python subclass from int,
~True is the same as ~1 which is -2, which has a boolean
value of True. To avoid this issue, use the SymPy boolean types
true and false.
Returns a canonical form of cls applied to arguments args.
The eval() method is called when the class cls is about to be
instantiated and it should return either some simplified instance
(possible of some other class), or if the class cls should be
unmodified, return None.
It evaluates its arguments in order, giving True immediately
if any of them are True, and False if they are all False.
Examples
>>> from..coreimportsymbols>>> from..abcimportx,y>>> from.boolalgimportOr>>> x|yx | y
Notes
The | operator is provided as a convenience, but note that its use
here is different from its normal use in Python, which is bitwise
or. Hence, Or(a,b) and a|b will return different things if
a and b are integers.
The POSform function uses simplified_pairs and a redundant-group
eliminating algorithm to convert the list of all input combinations
that generate ‘1’ (the minterms) into the smallest Product of Sums form.
The variables must be given as the first argument.
Return a logical And function (i.e., the “product of sums” or “POS”
form) that gives the desired outcome. If there are inputs that can
be ignored, pass them as a list, too.
The result will be one of the (perhaps many) functions that satisfy
the conditions.
Examples
>>> from..logicimportPOSform>>> from..importsymbols>>> w,x,y,z=symbols('w x y z')>>> minterms=[[0,0,0,1],[0,0,1,1],[0,1,1,1],... [1,0,1,1],[1,1,1,1]]>>> dontcares=[[0,0,0,0],[0,0,1,0],[0,1,0,1]]>>> POSform([w,x,y,z],minterms,dontcares)z & (y | ~w)
The SOPform function uses simplified_pairs and a redundant group-
eliminating algorithm to convert the list of all input combos that
generate ‘1’ (the minterms) into the smallest Sum of Products form.
The variables must be given as the first argument.
Return a logical Or function (i.e., the “sum of products” or “SOP”
form) that gives the desired outcome. If there are inputs that can
be ignored, pass them as a list, too.
The result will be one of the (perhaps many) functions that satisfy
the conditions.
Examples
>>> from..logicimportSOPform>>> from..importsymbols>>> w,x,y,z=symbols('w x y z')>>> minterms=[[0,0,0,1],[0,0,1,1],... [0,1,1,1],[1,0,1,1],[1,1,1,1]]>>> dontcares=[[0,0,0,0],[0,0,1,0],[0,1,0,1]]>>> SOPform([w,x,y,z],minterms,dontcares)(y & z) | (z & ~w)
Returns a canonical form of cls applied to arguments args.
The eval() method is called when the class cls is about to be
instantiated and it should return either some simplified instance
(possible of some other class), or if the class cls should be
unmodified, return None.
The ^ operator is provided as a convenience, but note that its use
here is different from its normal use in Python, which is bitwise xor. In
particular, a^b and Xor(a,b) will be different if a and
b are integers.
Never use self._args, always use self.args.
Only use _args in __new__ when creating a new function.
Don’t override .args() from Basic (so that it’s easy to
change the interface in the future if needed).
Return the simplified version of bool1, and the mapping of variables
that makes the two expressions bool1 and bool2 represent the same
logical behaviour for some correspondence between the variables
of each.
If more than one mappings of this sort exist, one of them
is returned.
For example, And(x, y) is logically equivalent to And(a, b) for
the mapping {x: a, y:b} or {x: b, y:a}.
If no such mapping exists, return False.
Checks if expr is in Negation Normal Form.
A logical expression is in Negation Normal Form (NNF) if it
contains only And, Or and Not, and Not is applied only to literals.
If simpified is True, checks if result contains no redundant clauses.
This function simplifies a boolean function to its simplified version
in SOP or POS form. The return type is an Or or And object in SymPy.
Parameters:
expr (string or boolean expression) –
form (string ('cnf' or 'dnf') or None (default).) – If ‘cnf’ or ‘dnf’, the simplest expression in the corresponding
normal form is returned; if None, the answer is returned
according to the form with fewest args (in CNF by default).
deep (boolean (default True)) – indicates whether to recursively simplify any
non-boolean functions contained within the input.
Convert a propositional logical sentence s to conjunctive normal form.
That is, of the form ((A | ~B | …) & (B | C | …) & …)
If simplify is True, the expr is evaluated to its simplest CNF form.
Convert a propositional logical sentence s to disjunctive normal form.
That is, of the form ((A & ~B & …) | (B & C & …) | …)
If simplify is True, the expr is evaluated to its simplest DNF form.
Examples
>>> from.boolalgimportto_dnf>>> from..abcimportA,B,C>>> to_dnf(B&(A|C))(A & B) | (B & C)>>> to_dnf((A&B)|(A&~B)|(B&C)|(~B&C),True)A | C
Converts expr to Negation Normal Form.
A logical expression is in Negation Normal Form (NNF) if it
contains only And, Or and Not, and Not is applied only to literals.
If simplify is True, the result contains no redundant clauses.
If input is false, truth_table returns only a list of truth values.
In this case, the corresponding input values of variables can be
deduced from the index of a given output.
Returns whether the given assignment is a model or not.
If the assignment does not specify the value for every proposition,
this may return None to indicate ‘not obvious’.
Parameters:
model (dict, optional, default: {}) – Mapping of symbols to boolean values to indicate assignment.
deep (boolean, optional, default: False) – Gives the value of the expression under partial assignments
correctly. May still return None to indicate ‘not obvious’.
Check satisfiability of a propositional sentence.
Returns a model when it succeeds.
Returns {true: true} for trivially true expressions.
On setting all_models to True, if given expr is satisfiable then
returns a generator of models. However, if expr is unsatisfiable
then returns a generator containing the single element False.
Examples
>>> from..abcimportA,B>>> from.inferenceimportsatisfiable>>> satisfiable(A&~B){A: True, B: False}>>> satisfiable(A&~A)False>>> satisfiable(True){True: True}>>> next(satisfiable(A&~A,all_models=True))False>>> models=satisfiable((A>>B)&B,all_models=True)>>> next(models){A: False, B: True}>>> next(models){A: True, B: True}>>> defuse_models(models):... formodelinmodels:... ifmodel:... # Do something with the model.... print(model)... else:... # Given expr is unsatisfiable.... print("UNSAT")>>> use_models(satisfiable(A>>~A,all_models=True)){A: False}>>> use_models(satisfiable(A^A,all_models=True))UNSAT