I'm trying to become cognizant of your caveats about conversion. What I've tried to write is a recursive function to convert polynomials (statements) in the Free BooleanPolynomialRing() to corresponding probability polynomials (statements) over QQ. I haven't convinced myself that it's correct, but it checks out with some examples and also for basic forms however what I've just discovered is that as in below
EX7.<P,Q,R,S,V,W> = BooleanPolynomialRing(6,order='lex')
Prob(P + Q + R + S).ring() #yields an error message -- it doesn't seem to know that it's parent ring should be ProbRing
Traceback (most recent call last):
File "/projects/5511fe15-8085-4d1d-bdc7-c6bf6c99e693/.sagemathcloud/sage_server.py", line 733, in execute
exec compile(block+'\n', '', 'single') in namespace, locals
File "", line 1, in <module>
File "element.pyx", line 344, in sage.structure.element.Element.__getattr__ (sage/structure/element.c:4022)
File "misc.pyx", line 257, in sage.structure.misc.getattr_from_other_class (sage/structure/misc.c:1775)
AttributeError: 'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular' object has no attribute 'ring'
#code
def ProbRecurse(myBP,BPR,ProbRing):
MyString = str(myBP)
if MyString == '1':
return 1
elif MyString == '0':
return 0
elif MyString.find(' 1') != -1:
return 1 - ProbRecurse(myBP + 1,BPR,ProbRing)
elif MyString.find('+') == -1:
return ProbRing(MyString.lower())
else:
kk = MyString.find(' + ')
myPoly1 = BPR(MyString[:kk])
myPoly2 = BPR(MyString[kk+3:])
return ProbRecurse(myPoly1,BPR,ProbRing) + ProbRecurse(myPoly2,BPR,ProbRing) - 2*ProbRecurse(myPoly1*myPoly2,BPR,ProbRing)
def Prob(myBP):
MyString = str(myBP)
if MyString == '1':
return 1
if MyString == '0':
return 0
BPR = myBP.ring() # seems to know it's parent ring
NewGens = str(BPR.gens()).lower()
NewGens=NewGens[1:len(NewGens) - 1]
ProbRing = PolynomialRing(QQ, len(myBP.args()) , NewGens.replace(', ',','), order=BPR.term_order()) # Create parent ProbRing -- inherit term-order and change gens case to lower
return ProbRecurse(myBP,BPR,ProbRing)
Prob(0)
Prob(1)
Prob(P*Q*R) # P and Q and R
Prob(P*Q + P + Q) # P or Q
Prob(P*Q + P + 1) # if P then Q
Prob(P + Q) # P xor Q
Prob(P + Q + R) # P xor Q xor R
Prob(P + Q + R + S) # etc
Prob(P*Q*R + P + Q*R) # premise 1 of exercise 7
Prob(Q*R*V*W + Q*R*V + 1) # premise 2 of exercise 7
Prob(P*S + P + S*W + S + 1) # premise 3 of exercise 7
Prob(W + 1) # not but also premise 4 of exercise 7
0
1
p*q*r # these look like polynomials base_ring() identifies as QQ
-p*q + p + q
p*q - p + 1
-2*p*q + p + q
4*p*q*r - 2*p*q - 2*p*r + p - 2*q*r + q + r # not convinced yet
-8*p*q*r*s + 4*p*q*r + 4*p*q*s - 2*p*q + 4*p*r*s - 2*p*r - 2*p*s + p + 4*q*r*s - 2*q*r - 2*q*s + q - 2*r*s + r + s # not convinced yet
-p*q*r + p + q*r
q*r*v*w - q*r*v + 1
p*s - p + s*w - s + 1
-w + 1
#Then do stuff like this in EX7 = P.ring()
Pr1 = (P + R + P*R)*(P + Q + P*Q) # premises of argument exercise 7
Pr2 = 1 + Q*R + Q*R*(1 + V + V*W)
Pr3 = 1 + P + P*S + (1 + P + P*S)*(S + S*W)
Pr4 = 1 + W
Concl = 1 + V + V*S
Idl = ideal(Pr1 + 1, Pr2 + 1, Pr3 + 1, Pr4 + 1, Concl + 0) # assume conclusion false for indirect proof
proof = Idl.groebner_basis()
proof
[1] # this mean 1 = 0 i.e. inconsistent therefore indirect proof
ideal(Pr1 + 1,Pr2 + 1, Pr3 + 1, Pr4 + 1).groebner_basis()
[P, Q + 1, R + 1, S, V, W] # a unique set of truth values [P,Q,R,S,V,W]=[0,1,1,0,0,0]
#and generalize to probabilities with
ProbRing = PolynomialRing(QQ, len(Prob(P).args()) , Prob(P).args(), order=BPR.term_order()) # have to create the parent outside function
PRI=(Prob(P*Q*R + P + Q*R) - 7/10, Prob(Q*R*V*W + Q*R*V + 1) - 1/2, Prob(P*S + P + S*W + S + 1) - 1/3, Prob(W + 1) - 1/2)*ProbRing
PRI.groebner_basis()
[p*q*r - p - q*r + 7/10, p*s - p - 1/2*s + 2/3, p*v - p - 7/10*v + 1, q*r*s - 2/3*q*r - 2/5*s + 1/15, q*r*v - 1, s*v - 5/2*s - 1/6*v + 5/3, w - 1/2]
# of course solving a system may result in values unacceptable as probabilities outside interval [0,1]