MCPcopy Create free account
hub / github.com/cortex-js/compute-engine / generateTruthTable

Function generateTruthTable

src/compute-engine/library/logic-analysis.ts:627–659  ·  view source on GitHub ↗
(
  expr: Expression,
  ce: ComputeEngine
)

Source from the content-addressed store, hash-verified

625 }
626
627 // Limit the number of variables to prevent explosion
628 if (variables.length > 20) {
629 // Too many variables, return undefined
630 return ce._fn('IsTautology', [expr]);
631 }
632
633 // Check all possible assignments
634 for (const assignment of generateAssignments(variables)) {
635 const result = evaluateWithAssignment(expr, assignment, ce);
636 if (sym(result) !== 'True') {
637 return ce.False;
638 }
639 }
640
641 return ce.True;
642}
643
644/**
645 * Generate a truth table for a boolean expression.
646 *
647 * Returns a `List` of `List`s where the first row contains column headers
648 * (variable names followed by "Result") and subsequent rows contain the
649 * truth values for each assignment.
650 *
651 * ## Algorithm
652 *
653 * Generates all 2^n possible truth assignments and evaluates the expression
654 * for each. This has **O(2^n) time and space complexity**.
655 *
656 * ## Performance Characteristics
657 *
658 * | Variables | Rows Generated | Output Size |
659 * |-----------|----------------|-------------|
660 * | 5 | 32 | ~1 KB |
661 * | 8 | 256 | ~8 KB |
662 * | 10 | 1,024 | ~32 KB |

Callers 1

logic.tsFile · 0.90

Calls 6

extractVariablesFunction · 0.90
generateAssignmentsFunction · 0.90
evaluateWithAssignmentFunction · 0.90
_fnMethod · 0.65
mapMethod · 0.65
stringMethod · 0.65

Tested by

no test coverage detected