Simplification
simplify returns a shortest "or of ands" or "and of ors" with the same truth table as the formula it is given. minimize shows the working: every prime implicant, which are essential, and which were chosen.
What it does
import { simplify, minimize, primeImplicants } from "@ratmath/logic";
String(simplify("(p & q) | (p & ~q)")); // "p"
String(simplify("(a & b) | (~a & c) | (b & c)")); // "(a ∧ b) ∨ (¬a ∧ c)": the third term was redundant
String(simplify("(p -> q) & (q -> r)", { form: "cnf" })); // "(¬p ∨ q) ∧ (¬q ∨ r)"
The second line is the consensus theorem: whenever holds, one of and holds already, whichever way falls.
Implicants and primes
A term (an "and" of literals) is an implicant of if is true whenever the term is. Each true row of the table is an implicant with every atom mentioned. Two implicants that differ in one atom, such as and , merge into one that leaves the atom out, , because . An implicant that cannot be merged further is prime.
The Quine–McCluskey method finds all the primes by merging in rounds, starting from the true rows, until nothing merges. A shortest disjunctive form is always a choice of primes, so what is left is a covering problem: pick the fewest primes so that every true row lies under at least one.
- A prime is essential if some true row lies under it and under no other prime. Essential primes are in every solution.
- For the rows not yet covered, the library searches for the smallest set of the remaining primes, fewest terms first and fewest literals among those.
const m = minimize("(p & q) | (~p & r)");
m.primes.map((x) => `${x.formula}${x.essential ? " essential" : ""}${x.chosen ? "" : " not needed"}`);
// ["p ∧ q essential", "q ∧ r not needed", "¬p ∧ r essential"]
String(m.formula); // "(p ∧ q) ∨ (¬p ∧ r)"
m.terms; m.literals; // 2, 4
When no prime is essential the choice is real. The function true on rows 0, 1, 2, 5, 6, 7 of three atoms has six primes arranged in a ring, each row under exactly two of them, and two different shortest forms of three terms each.
Products of sums
The shortest conjunctive form comes from the same method applied to the negation: minimise as an "or of ands", then negate, and De Morgan's laws turn it into an "and of ors" for . simplify with form: "auto" (the default) computes both and returns the shorter.
Rows that do not matter
Sometimes certain inputs cannot occur, and the formula may take either value there. Naming those rows with dontCare lets the primes grow through them, and the result is right on every row that matters.
// A digit 0 to 9 in four bits, a the leading bit: "is the digit at least 5?" Rows 10 to 15 never occur.
const atLeast5 = "(~a & b & (c | d)) | (a & ~b & ~c)"; // true on rows 5, 6, 7, 8, 9
const never = "a & (b | c)"; // true on rows 10 to 15
String(simplify(atLeast5)); // "(a ∧ ¬b ∧ ¬c) ∨ (¬a ∧ b ∧ c) ∨ (¬a ∧ b ∧ d)"
String(simplify(atLeast5, { dontCare: never, form: "dnf" })); // "a ∨ (b ∧ c) ∨ (b ∧ d)"
String(simplify(atLeast5, { dontCare: never })); // "(a ∨ b) ∧ (a ∨ c ∨ d)": the product of sums is shorter still
Limits
The method works from the table, so it takes at most 14 atoms. The number of primes can itself be large, and the covering step is a hard problem in general; it runs within a budget and minimize(...).exact is false if the budget ran out, in which case the answer is correct and short but perhaps not the shortest. "Shortest" here means shortest in two levels: a formula like has no short two-level form at all.
The functions
| Call | Returns |
|---|---|
simplify(f, { form, dontCare }) | A shortest formula. form: "auto" (default), "dnf" or "cnf". |
minimize(f, { form, dontCare }) | { formula, form, terms, literals, exact, primes }; each prime has formula, rows, essential, chosen. |
primeImplicants(f) | The prime implicants, as formulas. |
@ratmath/logic and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.