Skip to main content

Boolean Algebra Toolkit

  • Truth Table to Boolean Expression and Logic Diagram Generator
  • Boolean Expression Evaluator and Truth Table Generator

Installation

pip install balg

Usage

Token Equivalent
& AND
^ XOR
+ OR
~ NOT
[A-z] Variable
from balg.boolean import Boolean
booleanObject = Boolean()
  1. To generate an expression's truth table:
input_expression: str = "~(A & B & C)+(A & B)+(B & C)"
tt: str = booleanObject.expr_to_tt(input_expression)
  1. To generate an expression given the minterms and variables:
variables: List[str] = ['A', 'B', 'C']
minterms: List[int]  = [0, 1, 3, 7]
expression: str   = booleanObject.tt_to_expr(variables, minterms)
  1. To generate a logic diagram given an expression:
input_expression: str = "~(A & B & C)+(A & B)+(B & C)"
file_name: str = "logic_diagram12"
format: str = "png"
directory: str = "examples" # stores in the current directory by default
booleanObject.expr_to_dg(input_expression, file_name, directory, format)
  1. To generate a logic diagram given variables and minterms
variables: List[str] = ['A', 'B', 'C']
minterms: List[int]  = [0, 1, 3, 7]
file_name: str = "logic_diagram12"
directory: str = "examples"
format: str = "png"
booleanObject.tt_to_dg(variables, minterms, file_name, directory, format)
  1. To assert equality for expressions
expressions_list = ["(A & ~B) + (~A & B)", "(A ^ B)", "(A + B) & ~(A & B)"]
ret = booleanObject.expr_cmp(expression_list) # returns True
  1. To simplify expressions (ambiguous):
simplified_expr: str = booleanObject.expr_simplify("~(A) + ~(B)")
  1. To generate random expressions:
#generating a single expression
expression: str = boolean.generate_expression(max_depth=4, max_identifiers=5)

#generating multiple expressions
expression: List[str] = boolean.generate_expressions(max_depth=4, max_identifiers=5, count=100)

#max_depth refers to nesting depth:
expression = "A" #nesting_depth = 0
expression = "(((4)))"  #nesting_depth = 3

#max_identifiers refers to the number of identifiers allowed in an expression
expression = "A + B + C" # has 3 identifiers

Example Diagrams

((A & B) & C) + (~C)

logic_diagram

(A & B) + (~(A & B) & ~C) + (C & B)

logic_diagram

Other diagrams can be found in the diagrams/ directory

Explanation of the Quine-McCluskey Algorithm

This section deals with converting a given truth table to a minimized boolean expression using the Quine-McCluskey algorithm and producing a logic diagram.

Overview

  1. Initialize variables & Minterms
  2. Identify essential Prime implicants
  3. Minimize & Synthesize the boolean function

Initialization

  • The synthesizer is initialized with a list of character variables and minterms:
  • Minterms refer to values for which the output is 1.
  • Prime implicants are found by repeatedly combining minterms that differ by only one variable:

The Quine-McCluskey Algorithm


               The Quine-McCluskey Algorithm

+-----------------------------------+
| initialize variables and minterms |
| variables := [A, B, C]            |
| minterms  := [0, 3, 6, 7]         |
| minters   := [000, 011, 110, 111] |
+-----------------------------------+
                |
                /
               /
               |
               V
        +-----------------------+
        | find prime_implicants |
        | | A | B | C |  out |  |
        | |---|---|---|------|  |
        | | 0 | 0 | 0 |  1   |  |
        | | 0 | 0 | 1 |  0   |  |
        | | 0 | 1 | 0 |  0   |  |
        | | 0 | 1 | 1 |  1   |  |
        | | 1 | 0 | 0 |  0   |  |
        | | 1 | 0 | 1 |  0   |  |
        | | 1 | 1 | 0 |  1   |  |
        | | 1 | 1 | 1 |  1   |  |
        +-----------------------+
                 |
                 |
                  \
                   |
                   V
+----------------------------------+
|  | group | minterm | A | B | C | |
|  |-------|---------|---|---|---| |
|  |   0   | m[0]    | 0 | 0 | 0 | |
|  |   2   | m[1]    | 0 | 1 | 1 | |
|  |       | m[2]    | 1 | 1 | 0 | |
|  |   3   | m[3]    | 1 | 1 | 1 | |
|  |-------|---------|---|---|---| |
+----------------------------------+
                    \
                     \
                      |
                      V
        +-------------------------------------------+
        | find pair where only one variable differs |
        | | group | minterm    | A | B | C |  expr  |
        | |-------|------------|---|---|---|--------|
        | |   0   | m[0]       | 0 | 0 | 0 | ~(ABC) |
        | |   2   | m[1]-m[3]  | _ | 1 | 1 |  BC    |
        | |       | m[2]-m[3]  | 1 | 1 | _ |  AB    |
        +-------------------------------------------+
                        |
                       /
                      |
                      V
    +-------------------------------------------+
    |  since the bit-diff between pairs in each |
    |  class is > 1, we move onto the next step |
    |                                           |
    |   |  expr  | m0  | m1  | m2  | m3   |     |
    |   |--------|-----|-----|-----|------|     |
    |   | ~(ABC) | X   |     |     |      |     |
    |   |   BC   |     |  X  |     |      |     |
    |   |   AB   |     |     |  X  |      |     |
    |   |--------|-----|-----|-----|------|     |
    +-------------------------------------------+
                            |
                            |
                           /
                          |
                          V
              +-----------------------------------------+
              | If each column contains one element     |
              | the expression can't be eliminated.     |
              | Therefore, the resulting expression is: |
              |         ~(ABC) + BC + AB                |
              +-----------------------------------------+

Tips

  1. Use parentheses when the order of operations is ambiguous.
  2. The precedence is as follows, starting from the highest: NOT -> OR -> (AND, XOR)

Documentation (for developers)

class TruthTableSynthesizer(variables: List[str], minterms: List[int])
class BooleanExpression(expression: str)
class Boolean()
class BooleanExpressionGenerator(max_identifiers: int = 5, max_depth: int = 5)
TruthTableSynthesizer.decimal_to_binary(num: int) -> str
TruthTableSynthesizer.combine_implicants(implicants: List[Set[str]]) -> Set[str]
TruthTableSynthesizer.get_prime_implicants() -> Set[str]
TruthTableSynthesizer.covers_minterm(implicant: str, minterm: str) -> bool
TruthTableSynthesizer.get_essential_prime_implicants(prime_implicants: Set[str]) -> Set[str]
TruthTableSynthesizer.minimize_function(prime_implicants: Set[str], essential_implicants: Set[str]) -> List[str]
TruthTableSynthesizer.implicant_to_expression(implicant: str) -> str
TruthTableSynthesizer.synthesize() -> str

BooleanExpression.to_postfix(inifx: str) -> List[str]
BooleanExpression.evaluate(values: Dict[str, bool]) -> bool
BooleanExpression.tt() -> List[Tuple[Dict[str, bool], bool]]
BooleanExpression.fmt_tt() -> str
BooleanExpression.generate_logic_diagram() -> graphviz.Digraph

BooleanExpressionGenerator.generate_expression() -> str
BooleanExpressionGenerator.generate_expressions(number: int = 1) -> List[str]

Boolean.expr_to_tt(input_expression: str) -> str
Boolean.tt_to_expr(variables: List[str], minterms: List[int]) -> str
Boolean.tt_to_dg(variables: List[str], minterms: List[int], file: str | None = None, directory: str | None = None, format: str = "png") -> str
Boolean.expr_to_dg(input_expression: str, file: str | None = None, directory: str | None = None, format: str = "png") -> str
Boolean.expr_simplify(input_expression: str) -> str
Boolean.expr_cmp(expressions: List[str]) -> bool
Boolean.generate_expression(max_identifiers: int=5, max_depth: int=4) -> str
Boolean.generate_expressions(max_identifiers: int=5, max_depth: int=4, number: int) -> List[str]

TODO

  1. Optimize functions 0.5. LaTeX interface
  2. NAND, NOR, XNOR
  3. Implication (X -> Y) and bi-implication (X <-> Y)
  4. Add support for constants (1, 0)
  5. Implement functional completeness testing
  6. Expression comparison by comparing minterms (grammar agnostic)
  7. (improbable) implement Quantum Gates
  8. (improbable) potential integration with Verilog systems

Release files for balg 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for balg 0.0.6
File Size Uploaded
balg-0.0.6.tar.gz 193.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for balg 0.0.6
File Interpreter ABI Platform
balg-0.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 203.9 kB

Release files / balg-0.0.6.tar.gz

Download URL balg-0.0.6.tar.gz
Size 193.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b1a53c862497d8a4e952a5aa6bb99f2f336353b781138059a0f84e4d71250bed
BLAKE2b-256 checksum
How to use checksums
61d2450b5ba0631e6496d85e27e6a1bc0680820a189d8dea0e969db1ef4cf3c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.5

Release files / balg-0.0.6-py3-none-any.whl

Download URL balg-0.0.6-py3-none-any.whl
Size 10.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d266cc55b19852f05a6c1231723df0d5b8c547888c3494ce2e98d9369bbb8355
BLAKE2b-256 checksum
How to use checksums
4296b869070690dbc073e50f18cf5aa0c599efefa6c0e66aee5fa6fd43d50146
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.5

Release history Release notifications | RSS feed

This release

0.0.6 This release

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page