( expr: Expression, ce: ComputeEngine )
| 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 | |
no test coverage detected