Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Yes, sometimes one has to oblige the machine to simplify! Consider the following code:

mat = matrix( [[1,0], [0,c]] )
xy0 = vector( [c,1] )
r0  = -mat*xy0
p0  = r0

for k in range(2):
    la  = r0*p0/(p0*mat*p0)
    xy1 = xy0 + la*p0

    # xy1 = xy1.simplify_full()   # <<<<<<<<< inserted this command
    for j in range(2):
        print ( "k=%(k)s j=%(j)s bool(xy1[%(j)s])=%(bool1)s bool(xy1[%(j)s] != 0)=%(bool2)s"
                % {'k': k, 'j': j, 'bool1': bool(xy1[j]), 'bool2': bool(xy1[j] != 0)} ) 
        if xy1[j]:    # changed!
            xy1[j] = xy1[j].factor()

    r1  = r0 - la*mat*p0
    p1  = r1 + r1*r1/(r0.dot_product(r0))*p0
    p1  = p1.simplify_full()
    p0  = p1
    r0  = r1
    xy0 = xy1


k=0 j=0 bool(xy1[0])=True bool(xy1[0] != 0)=True
k=0 j=1 bool(xy1[1])=True bool(xy1[1] != 0)=True

k=1 j=0 bool(xy1[0])=False bool(xy1[0] != 0)=True
k=1 j=1 bool(xy1[1])=False bool(xy1[1] != 0)=True

So, for k=1 the condition xy1[0] != 0 is above True, since "at the first glance" the code is not completely evaluated. But using xy1[j] instead in the if-condition makes sage convert it to a boolean, and this conversion computes bool(xy1[0]), which is False. (The error comes in the if-block than immediately from factoring the zero.)

Moral: Write the code in a "solid way", understand and test the if-conditions for the given purpose. Sage was at some places educated to do the "quick thing", since always applying the full simplification may not be what we want.