# BinPy.Algorithms package¶

## BinPy.Algorithms.AnalogFormulas module¶

class BinPy.Algorithms.AnalogFormulas.OhmsLaw[source]

This class implements Ohm’s law for circuit analysis It requires any two parameters and it will calculate the other two.

```>>> myCalc = OhmsLaw()
>>> myCalc.evaluate(p=1254.8,i=7.5)
{'i': 7.5, 'p': 1254.8, 'r': 22.307555555555556, 'v': 167.30666666666667}
```
Methods:
evaluate(i=None,v=None,r=None,p=None)
evaluate(i=None, v=None, r=None, p=None)[source]

This method returns a dictionary of current, voltage, power, and resistance DictKeys: ‘i’, ‘v’, ‘r’, ‘p’

class BinPy.Algorithms.AnalogFormulas.OhmsLaw_AC[source]

This class implements Ohm’s law for circuit analysis using AC current It requires any three parameters and it will calculate the other two.

How to use:
```>>> myCalc = OhmsLaw_AC()
>>> myCalc.evaluate(p=1254.8,i=7.5,cos=2.0)
>>> {'i': 7.5, 'p': 1254.8, 'r': 11.15, 'v': 83.625}
```
Methods:
evaluate(i=None,v=None,z=None,p=None,cos=None)
evaluate(i=None, v=None, z=None, p=None, c=None)[source]

This method returns a dictionary of current, voltage, power, resistance and cosine DictKeys: ‘i’, ‘v’, ‘z’, ‘p’,’c’

## BinPy.Algorithms.ExpressionConvert module¶

BinPy.Algorithms.ExpressionConvert.convertExpression(expr, two_input=0, only_nand=0, only_nor=0, only_and_or_not=0)[source]

Converts logical expression to an implementable form. Make two_input 1 if only two input gates must be used. Make only_nand 1 if only 2 input nand gates must be used. Make only_nor 1 if only 2 input nor gates must be used. Make only_and_or_not 1 if only 2 input AND, OR and NOTs be used. Error occurs if more than one variable is put to 1.

convertExpression(‘( NOT(a) and NOT(b)) or (C and Not(d) and E and F)’) OR(AND(NOT(a), NOT(b)), AND(C, NOT(d), E, F))

convertExpression(‘( NOT(a) and NOT(b)) or (C and Not(d) and E and F)’, two_input=1) OR(AND(NOT(a), NOT(b)), AND(C, AND(NOT(d), E)))

convertExpression(‘( NOT(a) and NOT(b)) or (C and Not(d) and E and F)’, only_nand=1) NAND(NAND(NAND(a, a), NAND(b, b)), NAND(C, NAND(NAND(NAND(d, d), E), NAND(NAND(d, d), E))))

convertExpression(‘( NOT(a) and NOT(b)) or (C and Not(d) and E and F)’, only_nor=1) NOR(NOR(NOR(a, b), NOR(NOR(C, C), NOR(NOR(d, NOR(E, E)),... NOR(d, NOR(E, E))))), NOR(NOR(a, b), NOR(NOR(C, C), NOR(NOR(d, NOR(E, E)), NOR(d, NOR(E, E))))))

convertExpression(‘( NOT(a) and NOT(b)) or (C and Not(d) and E and F)’, only_and_or_not=1) OR(AND(NOT(a), NOT(b)), AND(C, AND(NOT(d), AND(E, F))))

BinPy.Algorithms.ExpressionConvert.createList(expr)[source]

Creates a list which can be used by convertExpression for conversion.

BinPy.Algorithms.ExpressionConvert.makeCompatible(expr)[source]

Used by convertExpression to convert logical operators to english words.

BinPy.Algorithms.ExpressionConvert.mergeNot(case, expr)[source]

Combines NOR gate with othes to minimize the number of gates used.

BinPy.Algorithms.ExpressionConvert.remove_not(gate, exp)[source]

Converts a NOT gate and its operand to use the specified gate only. The input gate must be NAND or NOR only.

BinPy.Algorithms.ExpressionConvert.to_and_or_not(gate, op1, op2)[source]

Converts a general two input gate and two of its operands to use only OR, NOT, or AND gates

BinPy.Algorithms.ExpressionConvert.to_nand(gate, op1, op2)[source]

Converts a general two input gate and two of its operands to use only NAND gates

BinPy.Algorithms.ExpressionConvert.to_nor(gate, op1, op2)[source]

Converts a general two input gate and two of its operands to use only NOR gates

## BinPy.Algorithms.ImplementBooleanFunction module¶

BinPy.Algorithms.ImplementBooleanFunction.ImplementBooleanFn()[source]

An interactive function which takes in minterms/maxterms and prints the Boolean Function and implementable form. Don’t Care Conditions can also be provided (optional) Eg: Enter the list of variables A,B,C Do you want to enter minterms (m) or maxterms(M)? m Enter list of minterms 1,4,7 Enter list of Don’t Care terms Enter X if there are none 2,5 The logical expression is (((NOT B) AND C) OR (A AND C) OR (A AND (NOT B))) Can be implemented as OR(AND(NOT(B), C), AND(A, C), AND(A, NOT(B)))

## BinPy.Algorithms.MooreOptimizer module¶

This class implements a Moore state machine solver. Using the Quine-McCluskey algorithm it minimizes the necessary next state and output functions for a given state machine.

class BinPy.Algorithms.MooreOptimizer.StateMachineOptimizer(state_tran, state_word_len, variables, outputs, **kwargs)[source]

This class is the base for creating a Moore state machine optimizer.

calc_total()[source]

Calculate the total count of possible permutations of state configurations.

class BinPy.Algorithms.MooreOptimizer.StateMachineOptimizer_AllPermutations(state_tran, state_word_len, variables, outputs, **kwargs)[source]

This class implements a Moore state machine optimizer that tries all possible permutations for assignment of state word values to states.

optimize()[source]
class BinPy.Algorithms.MooreOptimizer.StateMachineOptimizer_FileAndVerify(state_tran, state_word_len, variables, outputs, **kwargs)[source]

This class is used for testing the state machine optimizer.

optimize(file)[source]
class BinPy.Algorithms.MooreOptimizer.StateMachineOptimizer_Random(state_tran, state_word_len, variables, outputs, **kwargs)[source]

This class implements a Moore state machine optimizer that tries permutations at random.

optimize(tries=1000)[source]
class BinPy.Algorithms.MooreOptimizer.StateMachineSolver(state_tran, state_word_len, variables, outputs)[source]
class InternalOptimizer(state_word_len, variables)[source]
class State[source]

This class provides access to the state word from the lambda expressions.

class StateMachineSolver.InternalOptimizer.Variables(variables)[source]

This class provides access to the input variables from the lambda expressions.

StateMachineSolver.InternalOptimizer.evaluate(f_array)[source]

Evaluates a list of lambda expressions in the state and variables environment. The lambda expressions are terms of an OR expression.

f_array: a list of lambda expressions

returns: the logical OR after evaluate the lambda expression in the setup environment

StateMachineSolver.InternalOptimizer.get_function(minterms)[source]

Retrieve a human readable form of the given function.

StateMachineSolver.InternalOptimizer.solve(f_on, f_off=None)[source]

Returns a function that satisfies the conditions given.

f_on: a list of lambda expressions; if one of the lambda expressions evaluates to True then the requested function should evaluate to True f_off: a list of lambda expressions; if one of them evaluates to True then the requested function whould evaluate to False

returns: a tuple a,b; a is the complexity of the function and b is the function

StateMachineSolver.print_solution(state_map, solution)[source]

Print a solution.

StateMachineSolver.solve(state_map)[source]

Given a state map return the transition and output functions.

state_map: a dictionary; key is the state and value is the value of the state word that identifies this state

returns: a tuple a,b,c; a is the sum of the functions’ complexities, b is the next state functions (one for each state word bit) and c is the output functions

BinPy.Algorithms.MooreOptimizer.main()[source]

## BinPy.Algorithms.QuineMcCluskey module¶

This class implements the Quine-McCluskey algorithm for minimization of boolean functions.

Based on code from Robert Dick <dickrp@eecs.umich.edu> and Pat Maupin <pmaupin@gmail.com>. Most of the original code was re-written for performance reasons.

```>>> qm = QM(['A','B'])
```
```>>> qm.get_function(qm.solve([])[1])
'0'
>>> qm.get_function(qm.solve([1,3],[0,2])[1])
'1'
>>> qm.get_function(qm.solve([0,1,2,3])[1])
'1'
>>> qm.get_function(qm.solve([3])[1])
'(A AND B)'
>>> qm.get_function(qm.solve([0])[1])
'((NOT A) AND (NOT B))'
>>> qm.get_function(qm.solve([1,3])[1])
'A'ls
```
```>>> qm.get_function(qm.solve([1],[3])[1])
'A'
>>> qm.get_function(qm.solve([2,3])[1])
'B'
>>> qm.get_function(qm.solve([0,2])[1])
'(NOT A)'
>>> qm.get_function(qm.solve([0,1])[1])
'(NOT B)'
>>> qm.get_function(qm.solve([1,2,3])[1])
'(A OR B)'
>>> qm.get_function(qm.solve([0,1,2])[1])
'((NOT B) OR (NOT A))'
```
class BinPy.Algorithms.QuineMcCluskey.QM(variables)[source]
calculate_complexity(minterms)[source]

Calculate the complexity of the given function. The complexity is calculated based on the following rules: A NOT gate adds 1 to the complexity. A n-input AND or OR gate adds n to the complexity.

minterms: a list of minterms that form the function

returns: an integer that is the complexity of the function

```>>> qm = QM(['A','B','C'])
```
```>>> qm.calculate_complexity([(1,6)])
0
>>> qm.calculate_complexity([(0,6)])
1
>>> qm.calculate_complexity([(3,4)])
2
>>> qm.calculate_complexity([(7,0)])
3
>>> qm.calculate_complexity([(1,6),(2,5),(4,3)])
3
>>> qm.calculate_complexity([(0,6),(2,5),(4,3)])
4
>>> qm.calculate_complexity([(0,6),(0,5),(4,3)])
5
>>> qm.calculate_complexity([(0,6),(0,5),(0,3)])
6
>>> qm.calculate_complexity([(3,4),(7,0),(5,2)])
10
>>> qm.calculate_complexity([(1,4),(7,0),(5,2)])
11
>>> qm.calculate_complexity([(2,4),(7,0),(5,2)])
11
>>> qm.calculate_complexity([(0,4),(7,0),(5,2)])
12
>>> qm.calculate_complexity([(0,4),(0,0),(5,2)])
15
>>> qm.calculate_complexity([(0,4),(0,0),(0,2)])
17
```
compute_primes(cubes)[source]

Find all prime implicants of the function.

cubes: a list of indices for the minterms for which the function evaluates to 1 or don’t-care.

get_function(minterms)[source]

Return in human readable form a sum of products function.

minterms: a list of minterms that form the function

returns: a string that represents the function using operators AND, OR and NOT.

solve(ones, dont_care=[])[source]

Executes the Quine-McCluskey algorithm and returns its results.

ones: a list of indices for the minterms for which the function evaluates to 1 dc: a list of indices for the minterms for which we do not care about the function evaluation

returns: a tuple a,b; a is the complexity of the result and b is a list of minterms which is the minified boolean function expressed as a sum of products

unate_cover(primes, ones)[source]

Use the prime implicants to find the essential prime implicants of the function, as well as other prime implicants that are necessary to cover the function. This method uses the Petrick’s method, which is a technique for determining all minimum sum-of-products solutions from a prime implicant chart.

primes: the prime implicants that we want to minimize. ones: a list of indices for the minterms for which we want the function to evaluate to 1.

BinPy.Algorithms.QuineMcCluskey.bitcount(i)[source]

Count set bits of the input.

BinPy.Algorithms.QuineMcCluskey.is_power_of_two_or_zero(x)[source]

Determine if an input is zero or a power of two. Alternative, determine if an input has at most 1 bit set.

BinPy.Algorithms.QuineMcCluskey.merge(i, j)[source]

Combine two minterms.

## Module contents¶

### Table Of Contents

BinPy package

#### Next topic

BinPy.Analog package