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]



On 5/29/2014 2:47 PM, William Stein wrote:
> On Thu, May 29, 2014 at 11:37 AM, Simon King <simon.k...@uni-jena.de> wrote:
>> Hi Stephen
>>
>> On 2014-05-29, Stephen Kauffman <strangerl...@gmail.com> wrote:
>>> Specifically I have a polynomial such as mypoly = X*Y + 1 in
>>> BooleanPoynomialRing() and str(mypoly) converts it to the string 'X*Y + 1'
>>> then I do some stuff with re pattern matching to construct the string '1 -
>>> x*y' which I now want to convert to a polynomial in 'x' and 'y' in QQ. Are
>>> there any built in functions or methods to do that?
>>
>> Generally, if you have an algebraic structure Y (often called "parent") in
>> Sage, and have some element x of some other algebraic structure x, then
>> Y(x) tries to convert x into an element of Y. The same holds for
>> conversion of a string.
>>
>> Note, however, that a conversion is not necessarily making mathematical
>> sense, if the structures X and Y are too different (i.e., there may be
>> no homomorphism from X to Y). So, use conversions only if you know what
>> you are doing.
>>
>> What you describe would be as follows:
>>   sage: P.<x,y> = BooleanPolynomialRing()
>>   sage: mypoly = x*y+1
>>   sage: R = QQ['x','y']
>>   sage: R(mypoly)
>>   x*y + 1
>>   sage: R('1-x*y')  # replacing + by - in str(mypoly)
>>   x*y - 1
>>
>> Best regards,
>> Simon
> 
> Simon  -- great answer -- like my second one but even better.  This
> seems like the sort of thing our FAQ should have.  Believe it or not,
> we have a Sage FAQ:
> 
>   http://sagemath.org/doc/faq/
> 
> I doubt it has been touched in years, and I doubt anybody even knows
> how to change it...
> 
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups 
>> "sage-support" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to sage-support+unsubscr...@googlegroups.com.
>> To post to this group, send email to sage-support@googlegroups.com.
>> Visit this group at http://groups.google.com/group/sage-support.
>> For more options, visit https://groups.google.com/d/optout.
> 
> 
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.

Reply via email to