Problem understanding coercions.

asked 2016-01-17 11:17:32 -0500

Emmanuel Charpentier gravatar image

Let :

sage:  w = x^4 - (1+3*i)*x^3 - (2-4*i)*x^2 + (6-2*i)*x - 4 - 4*i

We can try to get its roots by (at least) two means : 1) Solution of a quartic :

sage: S1=[t.rhs() for t in solve(w,x)];S1
[-1/2*sqrt(2*I) + 3/2*I - 1/2, 1/2*sqrt(2*I) + 3/2*I - 1/2, -I + 1, I + 1]
sage: bool(sqrt(2*I)==1+I)
sage: S1R=[t.subs({sqrt(2*I):1+I}) for t in S1];S1R
[I - 1, 2*I, -I + 1, I + 1]

2) Roots of a polynom

sage: S2=[SR(t[0]) for t in w.roots(ring=QQbar)];S2
[-1 + 1*I, 2*I, 1 - 1*I, 1 + 1*I]

I am convinced tjat those two solutions are equal. But I fail to convince Sage :

sage: map(lambda t,u:t-u, S1R, S2)
TypeError                                 Traceback (most recent call last)
<ipython-input-7-be2e4183b522> in <module>()
----> 1 map(lambda t,u:t-u, S1R, S2)

<ipython-input-7-be2e4183b522> in <lambda>(t, u)
----> 1 map(lambda t,u:t-u, S1R, S2)

/usr/local/sage-7.0/src/sage/structure/element.pyx in sage.structure.element.RingElement.__sub__ (/usr/local/sage-7.0/src/build/cythonized/sage/structure/element.c:15995)()
   1665         cdef long n
   1666         if have_same_parent_c(left, right):
-> 1667             return (<ModuleElement>left)._sub_(<ModuleElement>right)
   1668         if PyInt_CheckExact(right):
   1669             n = PyInt_AS_LONG(right)

/usr/local/sage-7.0/src/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression._sub_ (/usr/local/sage-7.0/src/build/cythonized/sage/symbolic/expression.cpp:20844)()
   2950                            relational_operator(_right._gobj))
   2951         else:
-> 2952             x = gsub(left._gobj, _right._gobj)
   2953         return new_Expression_from_GEx(left._parent, x)

/usr/local/sage-7.0/src/sage/structure/element.pyx in sage.structure.element.RingElement.__add__ (/usr/local/sage-7.0/src/build/cythonized/sage/structure/element.c:15852)()
   1649         elif PyInt_CheckExact(left):
   1650             return (<RingElement>right)._add_long(PyInt_AS_LONG(left))
-> 1651         return coercion_model.bin_op(left, right, add)
   1653     cdef RingElement _add_long(self, long n):

/usr/local/sage-7.0/src/sage/structure/coerce.pyx in sage.structure.coerce.CoercionModel_cache_maps.bin_op (/usr/local/sage-7.0/src/build/cythonized/sage/structure/coerce.c:9736)()
   1067         # We should really include the underlying error.
   1068         # This causes so much headache.
-> 1069         raise TypeError(arith_error_message(x,y,op))
   1071     cpdef canonical_coercion(self, x, y):

TypeError: unsupported operand parent(s) for '+': 'Number Field in I with defining polynomial x^2 + 1' and 'Algebraic Field'

However, both lists are composed of some things seemongly belonging to SR :

sage: map(lambda t:type(t), S1R)
[<type 'sage.symbolic.expression.Expression'>,
 <type 'sage.symbolic.expression.Expression'>,
 <type 'sage.symbolic.expression.Expression'>,
 <type 'sage.symbolic.expression.Expression'>]
sage: map(lambda t:type(t), S2)
[<type 'sage.symbolic.expression.Expression'>,
 <type 'sage.symbolic.expression.Expression'>,
 <type 'sage.symbolic.expression.Expression'>,
 <type 'sage.symbolic.expression.Expression'>]

Worse, if I try to directly test for inequality, Sage CRASHES :

sage: S1R==S2
terminate called after throwing an instance of 'std::runtime_error'
/usr ...
edit retag flag offensive close merge delete


Yeah, my guess is that the underlying coefficients in SR to these two guys are still in different rings (SR does keep track of that even though it's all "symbolic") so that one is just plain old SR while the other one is using QQbar stuff in the algebraic closure of the rationals. Hopefully someone can give you details soon, though.

kcrisman gravatar imagekcrisman ( 2016-01-19 10:26:28 -0500 )edit