Exercises · SymPy · Topics: logical equivalence, simplification, CNF, DNF, De Morgan, absorption, distributivity, satisfiability, counting functions
Pr. Zouhair el Hadiq
Verify the equivalence $\neg(p \land q) \equiv \neg p \lor \neg q$ by comparing truth table outputs row by row.
are_equivalent(expr1, expr2, variables) -> bool using truth_table.Verify the equivalence $\neg(p \lor q) \equiv \neg p \land \neg q$ using the same are_equivalent helper from Exercise 1.
Use simplify_logic(Not(Not(p))) and verify that the result equals $p$.
simplify_logic(Not(Not(p))) == p.Verify both absorption laws using simplify_logic:
Assert that both expressions simplify to the symbol $p$ alone. Why is this called an "absorption" law?
Verify both distributive laws using are_equivalent over three variables $(p, q, r)$:
How many rows does a truth table with 3 variables have?
Simplify the following compound formulas using simplify_logic and verify each result equals $p$:
Convert each of the following formulas to CNF using sympy.to_cnf. A CNF is a conjunction (AND) of clauses, where each clause is a disjunction (OR) of literals.
Print the CNF form of each and verify it is logically equivalent to the original using are_equivalent.
Convert each formula to DNF using sympy.to_dnf. A DNF is a disjunction (OR) of terms, where each term is a conjunction (AND) of literals.
Print the DNF form and verify equivalence with the original formula.
Use sympy.logic.inference.satisfiable to determine whether each formula below is satisfiable, unsatisfiable, or a tautology. satisfiable returns a model (dictionary) if satisfiable, or False if not.
For each satisfiable formula, print the model (assignment that makes it True).
Implement at_least_k_of(variables, k) that returns a SymPy formula which is True iff at least $k$ of the given variables are True.
itertools.combinations — "at least $k$ variables are True" iff some $k$-element subset is simultaneously True: Or(*[And(*combo) for combo in combinations(variables, k)]).at_least_2_of_5.simplify_logic.Verify each of the following equivalences using are_equivalent:
| Name | Equivalence |
|---|---|
| Implication as disjunction | $p \to q \;\equiv\; \neg p \lor q$ |
| Contrapositive | $p \to q \;\equiv\; \neg q \to \neg p$ |
| Negation of implication | $\neg(p \to q) \;\equiv\; p \land \neg q$ |
For each equivalence, assert the result and print a confirmation message.