Circuits and Karnaugh maps
A circuit is a formula with its parts shared: gates wired together, with no loops. Two numbers describe its cost, the number of gates (size) and the longest path from an input to an output (depth, roughly the time a signal takes).
Building and running a circuit
Gates are named, and each lists its kind and what it reads. The kinds are and, or, xor, nand, nor, xnor (two inputs or more), not and buf. A wiring that loops is refused.
import { circuit } from "@ratmath/logic";
const half = circuit({ inputs: ["a", "b"],
gates: { s: ["xor", "a", "b"], c: ["and", "a", "b"] }, outputs: ["s", "c"] });
half.evaluate([1, 1]); // { s: 0, c: 1 }
half.size, half.depth; // 2, 1
String(half.formula("s")); // "a ⊕ b"
half.truthTable()[3]; // { inputs: [1, 1], outputs: { s: 0, c: 1 } }
Adders
A full adder adds three bits: and . Chaining of them, each carry feeding the next, gives a ripple-carry adder for -bit numbers. Its depth grows with , because the last carry waits for all the others; faster adders look ahead at the carries.
import { adders } from "@ratmath/logic";
const fa = adders.full();
fa.size, fa.depth, fa.counts; // 5, 3, { xor: 2, and: 2, or: 1 }
const r4 = adders.ripple(4); // inputs a3 a2 a1 a0 b3 b2 b1 b0, outputs c3 s3 s2 s1 s0
r4.evaluate([0,1,1,1, 0,1,0,1]); // { c3: 0, s3: 1, s2: 1, s1: 0, s0: 0 }: 7 + 5 = 12
r4.size, r4.depth; // 17, 7
The tests check the 4-bit adder on all 256 pairs of inputs against ordinary addition.
One kind of gate is enough
NAND alone can build everything: , , and . NOR is the same with the roles of and and or swapped. circuitFrom builds a circuit for a formula in standard gates, or in one of these, sharing equal parts and cancelling double negations.
import { circuitFrom, circuitsEquivalent } from "@ratmath/logic";
const n = circuitFrom("(a & b) | ~c", { gates: "nand" });
n.counts; // { nand: 2 }: (a ↑ b) ↑ c
circuitsEquivalent(n, "(a & b) | ~c"); // { status: "equivalent" }
circuitsEquivalent(n, "(a & b) | c");
// { status: "different", output: "out", inputs: { a: 1, b: 0, c: 0 }, values: [1, 0] }
Equivalence is decided by the solver, and a difference comes with an input where the two disagree, checked by running both.
Karnaugh maps
A Karnaugh map lays out a truth table of up to four atoms as a grid whose rows and columns are in Gray code order (00, 01, 11, 10), so neighbouring cells, including across the edges, differ in one atom. A block of 1, 2, 4 or 8 true cells in a rectangle is then a single product term: the atoms that stay the same across the block. Covering every 1 with as few, as large, blocks as possible gives a shortest sum of products.
import { karnaugh } from "@ratmath/logic";
const k = karnaugh("(a & b) | (~a & c) | (b & c) | (d & ~b & a)");
k.toString();
// ab\cd 00 01 11 10
// 00 0 0 1 1
// 01 0 0 1 1
// 11 1 1 1 1
// 10 0 1 1 0
k.groups.map((g) => String(g.formula)); // ["a ∧ b", "a ∧ d", "¬a ∧ c"], each a block of 4
String(k.formula); // "(a ∧ b) ∨ (a ∧ d) ∨ (¬a ∧ c)"
String(karnaugh({ minterms: [0, 2, 8, 10], atoms: ["a", "b", "c", "d"] }).formula);
// "¬b ∧ ¬d": the four corners are one block, because the edges wrap
Cells marked x are rows that do not matter (a dontCares list); a block may use them or not. The blocks are chosen by the same Quine–McCluskey method as Simplification, so the map and the minimiser always agree.
karnaugh({ minterms: [1, 3, 7], dontCares: [5], atoms: ["a", "b", "c"] }).formula; // c
Reference
| Export | What it does |
|---|---|
circuit({ inputs, gates, outputs }) | A Circuit: evaluate(values), truthTable(), formula(output), size, depth, counts. |
circuitFrom(f, { gates, output }) | gates: "standard", "nand" or "nor". |
circuitsEquivalent(a, b) | Circuits or formulas, outputs matched by position. "equivalent", or "different" with the inputs that show it. |
adders.half(), adders.full(), adders.ripple(n) | Adders built from gates. |
karnaugh(f | { minterms, atoms, dontCares }) | Two to four atoms. cells, rowLabels, colLabels, groups (each with formula, cells, size, essential), formula, toString(). |
@ratmath/logic and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.