Ask Your Question

Revision history [back]

Need to improve a function in sage which checks trueness of expressions in an example.

Hello !

Here I have made a function in sage which checks equality between consecutive expressions in an example.

This function takes a set of examples as an argument. I want to improve my function and I really need comments and suggestions for correcting this function.

Two examples which I have used are as follows.

Example 1:

(1 - ((1 + sqrt(a))/(sqrt(a)- 1)))

=

(1 + ((1 + sqrt(a))/(-sqrt(a) + 1)))

=

(1 + ((1 + sqrt(a))* (1 + sqrt(a))/((-sqrt(a) + 1) * (sqrt(a) + 1))))

=

(1 + ((1 + 2* sqrt(a) + a)/(1 - a)))

=

(1 + ((1+a + 2* sqrt(a) )/(1-a)))

=

(((1-a)/(1-a)) + ((1+a + 2* sqrt(a))/(1-a)))

=

(1-a+1+a+ 2*sqrt(a))/(1-a)

=

(2+2*sqrt(a))/(1-a)

Example2:

x^2 + 2*x -3 == 0

<=>

x == (-2 + (2^2 -41(-3))^(1/2))/(21) or x == (-2 - (2^2 -41(-3))^(1/2))/(21)

<=>

x == (-2 + (4+12)^(1/2))/(21) or x == (-2 - (4+12)^(1/2))/(21)

<=>

x == (-2 + (16)^(1/2))/(21) or x == (-2 - (16)^(1/2))/(21)

<=>

x == (-2 + 4)/(21) or x == (-2 - 4)/(21)

<=>

x == (2)/(2) or x == (-6)/(2)

<=>

x == 1 or x == -3

The Problem is , following function checks first example correctly but not second example. In the second example, final answer is x == 1 or x == -3 but even if I provide wrong answer x == -1 or x == -3 it gives true. I have already received explanation about how 'or' behaves. see here

---------------Function begins ---------------

var('j,e,exprList,as1,i,lhs,rhs,r,r1,x')

def ComExpr(a):

r1 = []; 

for j in range(0,len(a)):

forget();

    e = a[ZZ(j)]

    exprList = e[0]

    as1 = e[1]
    print len(a)

r = [];

    for i in range(0,len(exprList)-1):

    forget(); 

        lhs = exprList[ZZ(i)]

        rhs = exprList[ZZ(i+1)]

    assume(as1 and lhs);   

    p1 = rhs.full_simplify()

    forget();

        assume(as1 and rhs); 

    p2 = lhs.full_simplify()

    r.append(bool(p1 and p2))

    r1.append(r)    

return r1

e1 is first example containing two elements one is list of expressions and another is assumption. Same for e2.

e1 = [[(1 - ((1 + sqrt(a))/(sqrt(a)- 1))) == (1 + ((1 + sqrt(a))/(-sqrt(a) + 1))), (1 + ((1 + sqrt(a))/(-sqrt(a) + 1))) == (1 + ((1 + sqrt(a))* (1 + sqrt(a))/((-sqrt(a) + 1) * (sqrt(a) + 1)))), (1 + ((1 + sqrt(a))* (1 + sqrt(a))/((-sqrt(a) + 1) * (sqrt(a) + 1)))) == (1 + ((1 + 2* sqrt(a) + a)/(1 - a))), (1 + ((1 + 2* sqrt(a) + a)/(1 - a))) == (1 + ((1+a + 2* sqrt(a) )/(1-a))), (1 + ((1+a + 2* sqrt(a) )/(1-a))) == (((1-a)/(1-a)) + ((1+a + 2* sqrt(a) )/(1-a))), (((1-a)/(1-a)) + ((1+a + 2* sqrt(a))/(1-a))) == (1-a+1+a+ 2*sqrt(a))/(1-a)], a > 1];

sage: ComExpr([e1]) 1 [[True, True, True, True, True]]

e2=[[x^2 + 2x -3 == 0, x == (-2 + (2^2 -41(-3))^(1/2))/(21) or x == (-2 - (2^2 -41(-3))^(1/2))/(21),x == (-2 + (4+12)^(1/2))/(21) or x == (-2 - (4+12)^(1/2))/(21),x == (-2 + (16)^(1/2))/(21) or x == (-2 - (16)^(1/2))/(21),x == (-2 + 4)/(21) or x == (-2 - 4)/(2*1),x == (2)/(2) or x == (-6)/(2),x == -1 or x == -3 ],x];

sage: ComExpr([e2])

1

[[True, True, True, True, True, True]]

sage: ComExpr([e1,e2])

2

2

[[True, True, True, True, True], [True, True, True, True, True, True]]


I want to make this function work correctly for second example and also for other similar kind of examples.

I would like to request you to check this function and provide your suggestions to improve it. I will be highly thankful to you if you could criticize this function.

Many thanks !