I have attached a file containing the code I have written for Boolean
function generation using Q-M method in python. I would like to incorporate
SymPy into its working. I would like suggestions if its possible . One
problem I am facing is dis..
>>>from sympy import *
>>>from sympy.abc import *
>>>b=Not(B)
>>>b
Not(B)
>>>B=True
>>>Not(B)
False
>>>b
Not(B)
Is there any way we could tweak the Not function so that b gets recognised
as being dependant on B and change its value accordingly?
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/sympy/-/bGAl0mphr34J.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sympy?hl=en.
def check_pair(minterm1,minterm2):
"""This function is intended to check whether theres a difference of only one bit between two minterms.
If yes, it returns the index of the different bit. Otherwise, it returns -1."""
if abs(minterm1.count(1)-minterm2.count(1))==1:
i=0
count=0
index=0
while i<=(len(minterm1)-1):
if minterm1[i] != minterm2[i]:
index=i
count += 1
i+=1
if count==1:
return index
else:
return -1
else:
return -1
def convert_to_vars(minterm,variables):
"""This function is intended to convert a binary number of type [1,0,3,1] to its variable form. Here, '3' means 'do not include in term'
So if the variables were a,b,c,d respectively, the above term would be returned as ab'd."""
string=""
i=0
while i<=(len(minterm)-1):
if minterm[i]== 0:
string=string+variables[i]+"'"
elif minterm[i] == 1:
string=string+(variables[i])
i+=1
return string
def simplified_pairs(l):
"""This function does one round of simplification on the given set of minterms. If its possible to reduce the function to terms with one less variable, this function does that."""
simplified_l=[]
i=0
done_list=[]
while i<=(len(l)-2):
k=1
for x in l[(i+1):]:
index=check_pair(l[i],x)
if index != -1:
done_list.append(i)
done_list.append(i+k)
temporary=l[i][:index]
temporary.append(3)
temporary.extend(l[i][(index+1):])
if temporary not in simplified_l:
simplified_l.append(temporary)
k += 1
i+=1
done_list=list(set(done_list))
i=0
while i<= (len(l)-1):
if i not in done_list:
simplified_l.append(l[i])
i+=1
return simplified_l
def convert_to_function(l,variables):
"""This function uses simplified_pairs and a redundant group-eliminating algorithm to convert the list of all input combos that generate '1' into the smallest SOP form.
Example
-------
>>> convert_to_function([[0,0,0,1],[0,0,1,1],[1,1,0,1],[1,0,0,1],[1,0,1,1],[1,0,1,0]],['w','x','y','z'])
"x'z+wy'z+wx'y"
>>>
"""
l1=[]
l2=[1]
while (l1 != l2):
l1=simplified_pairs(l)
l2=simplified_pairs(l1)
l=l1[:]
i=0
to_remove=[]
while i<=(len(l1)-1):
compare=[]
k=0
while k<=(len(l1[i])-1):
count=0
if l1[i][k]==3:
k+=1
else:
m=0
while m<=(len(l1)-1):
if m==i:
m+=1
elif l1[m].count(3)==l1[i].count(3):
if l1[m][k]==l1[i][k]:
count+=1
m+=1
else:
m+=1
if count>=1:
compare.append(k)
k+=1
if len(compare)==len(l1[i])-l1[i].count(3):
to_remove.append(l1[i])
i+=1
for x in to_remove:
l1.remove(x)
string=""
for x in l1:
string=string+convert_to_vars(x,variables)+'+'
if string=='':
string='1+'
return string[:len(string)-1]