Skip to main content

A boolean algebra toolkit to: evaluate expressions, truth tables, and produce logic diagrams

Project description

Boolean Algebra Toolkit

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

Installation

pip install balg-MustafaAamir==0.0.1

Usage

Token Equivalent
& AND
^ XOR
+ OR
~ NOT
[A-z] Variable
from 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)

Example Diagrams

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

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

Other diagrams can be found in diagrams/

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 are ambiguous.
  2. The precedence is as follows, starting from the highest: NOT -> OR -> (AND, XOR)
  3. Modify BooleanExpression.tt to produce markdown tables for a better UI

Documentation (for developers)

class TruthTableSynthesizer(variables: List[str], minterms: List[int])
class BooleanExpression(expression: str)
class Boolean()
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.truth_table() -> List[Tuple[Dict[str, bool], bool]]
BooleanExpression.tt() -> str
BooleanExpression.generate_logic_diagram() -> graphviz.Digraph

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

balg-0.0.2.tar.gz (192.1 kB view details)

Uploaded Source

Built Distribution

balg-0.0.2-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file balg-0.0.2.tar.gz.

File metadata

  • Download URL: balg-0.0.2.tar.gz
  • Upload date:
  • Size: 192.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for balg-0.0.2.tar.gz
Algorithm Hash digest
SHA256 c6a353cab5d6737e9cad0057a2515b337cf5b1ff3e5876cbd1126f70d4a520a3
MD5 e1e1f754a91a77f52df9c384da87ca63
BLAKE2b-256 138cdf3423f1952f3fd4221251a99ad8c81830f22b6c2b0040fe68751733b411

See more details on using hashes here.

File details

Details for the file balg-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: balg-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for balg-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c735c314a5ed0d9225126bbd9dc6c650f1ec302c243336aa1e66bc30be3c20c6
MD5 cb1c44f2e5e78dd8d7e751e50d589b35
BLAKE2b-256 95bf017bfc5374795e6ff442414188f393b796cf752a24bd2d36fd7b4d7c89b5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page