On Sun, Jan 17, 2016 at 9:39 AM, Emmanuel Charpentier
<emanuel.charpent...@gmail.com> wrote:
> (Note : Question also asked on ask.sagemath.org, but crossposted because I
> found a way to CRASH sage...).
> I'm trying to understand coercions, and I'm hitting (repeatedly) something
> that I do not understand.
>
> Let's try to find thge roots of a polynom. We can try equation solving (of a
> quartic, no less) :
>
> sage:  w = x^4 - (1+3*i)*x^3 - (2-4*i)*x^2 + (6-2*i)*x - 4 - 4*i
> 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)
> True
>
> Or we can try the roots of a polynomial :
>
> sage: S2=[SR(t[0]) for t in w.roots(ring=QQbar)];S2
> [-1 + 1*I, 2*I, 1 - 1*I, 1 + 1*I]
> sage: bool(sqrt(2*I)==1+I)
> True
> sage: S1R=[t.subs({sqrt(2*I):1+I}) for t in S1];S1R
> [I - 1, 2*I, -I + 1, I + 1]
>
> So far, so good. I'm convinced that these two solution lists are one and the
> same. But I can't find a way to convince Sage:

They aren't the same.    The "I" you're using everywhere is a number
field element.  However, when you do SR(...root in QQbar...), you're
making a completely different and incompatible I.   The notation

     SomeParent(...)

is potentially dangerous: <rant> it means "make something in
SomeParent from the input possibly without respecting the coercion
model".  It's what people do when the get frustrated -- it's a sort of
automated version of copy/paste.  It's like Magma's "SomeParent![crazy
thing]", and is by far the most likely source of bugs in code.
</rant>

If you do:

reset()
a = (x^2+1).roots(ring=QQbar)[1][0]
b = I.pyobject()
a, b
parent(a), parent(b)

you'll see the two underlying elements that you're trying to mix.
One is a number field element, and the other is an element of QQbar.
Number fields elements don't mix with elements of QQbar without
explicitly applying an embedding morphism.

That said, I vaguely recall at some point that a fixed choice of
embedding was something maybe added to number fields.  It would likely
make a lot more sense also for the I in SR to the one from QQbar
rather than the one from NumberField(x^2+1).  However, the one from
NumberField(x^2+1) is a *lot* faster (orders of magnitude!), so it's
no so clear.

I'm not at all saying you haven't hit on a serious bug.  However, the
above remarks might help whoever works on it, maybe.

William

>
> sage: map(lambda t,u:t-u, S1R, S2)
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call last)
> <ipython-input-6-be2e4183b522> in <module>()
> ----> 1 map(lambda t,u:t-u, S1R, S2)
>
> <ipython-input-6-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)
>    2954
>
> /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)
>    1652
>    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))
>    1070
>    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'
>
> I do not understand this error : both S1R and S2 are composed of things
> 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'>]
>
> The reverse conversion works, however :
>
> sage: map(lambda t,u:t-u, [QQbar(t) for t in S1R], [QQbar(t) for t in S2])
> [0, 0, 0, 0]
>
> Worse : testing directly for equality CRASHES Sage :
>
> sage: map(lambda t,u:bool(t==u), S1R, S2)
> terminate called after throwing an instance of 'std::runtime_error'
>   what():
> ------------------------------------------------------------------------
> /usr/local/sage-7.0/local/lib/python2.7/site-packages/sage/ext/interrupt/interrupt.so(+0x3c35)[0x7f3d47047c35]
> /usr/local/sage-7.0/local/lib/python2.7/site-packages/sage/ext/interrupt/interrupt.so(+0x3c87)[0x7f3d47047c87]
> /usr/local/sage-7.0/local/lib/python2.7/site-packages/sage/ext/interrupt/interrupt.so(+0x608c)[0x7f3d4704a08c]
> /lib/x86_64-linux-gnu/libpthread.so.0(+0x10670)[0x7f3d4edc4670]
> /lib/x86_64-linux-gnu/libc.so.6(gsignal+0x37)[0x7f3d4e337657]
> /lib/x86_64-linux-gnu/libc.so.6(abort+0x16a)[0x7f3d4e338a2a]
> /usr/lib/x86_64-linux-gnu/libstdc++.so.6(_ZN9__gnu_cxx27__verbose_terminate_handlerEv+0x15d)[0x7f3d3a9ad35d]
> /usr/lib/x86_64-linux-gnu/libstdc++.so.6(+0x8d3b6)[0x7f3d3a9ab3b6]
> /usr/lib/x86_64-linux-gnu/libstdc++.so.6(+0x8d401)[0x7f3d3a9ab401]
> /usr/lib/x86_64-linux-gnu/libstdc++.so.6(+0x8d619)[0x7f3d3a9ab619]
> /usr/local/sage-7.0/local/lib/libpynac.so.2(_Z8py_errorPKc+0x49)[0x7f3d214029c9]
> /usr/local/sage-7.0/local/lib/libpynac.so.2(_ZN5GiNaC7numericC1EP7_objectb+0x1b8)[0x7f3d21403508]
> /usr/local/sage-7.0/local/lib/libpynac.so.2(_ZNK5GiNaC7numeric3addERKS0_+0x1c2)[0x7f3d214094f2]
> /usr/local/sage-7.0/local/lib/libpynac.so.2(_ZN5GiNaCplERKNS_7numericES2_+0x9)[0x7f3d2140c569]
> /usr/local/sage-7.0/local/lib/libpynac.so.2(_ZNK5GiNaC7numeric7add_dynERKS0_+0x2d)[0x7f3d2140405d]
> /usr/local/sage-7.0/local/lib/libpynac.so.2(_ZN5GiNaC9expairseq21combine_overall_coeffERKNS_2exE+0x15)[0x7f3d2136f945]
> /usr/local/sage-7.0/local/lib/libpynac.so.2(_ZN5GiNaC9expairseq19construct_from_2_exERKNS_2exES3_+0x27d)[0x7f3d21374bed]
> /usr/local/sage-7.0/local/lib/libpynac.so.2(_ZN5GiNaC3addC1ERKNS_2exES3_+0x63)[0x7f3d21346633]
> /usr/local/sage-7.0/local/lib/libpynac.so.2(_ZN5GiNaCmiERKNS_2exES2_+0x5f)[0x7f3d2140d14f]
> /usr/local/sage-7.0/local/lib/libpynac.so.2(_ZNK5GiNaC10relational6decideEv+0x3e)[0x7f3d2141bede]
> /usr/local/sage-7.0/local/lib/python2.7/site-packages/sage/symbolic/expression.so(+0xa7108)[0x7f3d21039108]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyObject_IsTrue+0x37)[0x7f3d4f06fe17]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(+0x54eae)[0x7f3d4f025eae]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(+0xbc623)[0x7f3d4f08d623]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyObject_Call+0x43)[0x7f3d4f023a73]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x3a6e)[0x7f3d4f0d634e]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyEval_EvalCodeEx+0x81c)[0x7f3d4f0d928c]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(+0x835ac)[0x7f3d4f0545ac]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyObject_Call+0x43)[0x7f3d4f023a73]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyEval_CallObjectWithKeywords+0x47)[0x7f3d4f0d22e7]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(+0xfd564)[0x7f3d4f0ce564]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x5c8e)[0x7f3d4f0d856e]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyEval_EvalCodeEx+0x81c)[0x7f3d4f0d928c]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyEval_EvalCode+0x19)[0x7f3d4f0d93a9]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x5234)[0x7f3d4f0d7b14]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyEval_EvalCodeEx+0x81c)[0x7f3d4f0d928c]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x5a42)[0x7f3d4f0d8322]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyEval_EvalCodeEx+0x81c)[0x7f3d4f0d928c]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x5a42)[0x7f3d4f0d8322]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyEval_EvalCodeEx+0x81c)[0x7f3d4f0d928c]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x5a42)[0x7f3d4f0d8322]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyEval_EvalCodeEx+0x81c)[0x7f3d4f0d928c]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x5a42)[0x7f3d4f0d8322]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyEval_EvalCodeEx+0x81c)[0x7f3d4f0d928c]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x5a42)[0x7f3d4f0d8322]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyEval_EvalCodeEx+0x81c)[0x7f3d4f0d928c]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x5a42)[0x7f3d4f0d8322]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyEval_EvalCodeEx+0x81c)[0x7f3d4f0d928c]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyEval_EvalCode+0x19)[0x7f3d4f0d93a9]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyRun_FileExFlags+0x8a)[0x7f3d4f0fca9a]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(PyRun_SimpleFileExFlags+0xd7)[0x7f3d4f0fde47]
> /usr/local/sage-7.0/local/lib/libpython2.7.so.1.0(Py_Main+0xc3e)[0x7f3d4f1143ee]
> /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f3d4e324870]
> python(_start+0x29)[0x4006f9]
> ------------------------------------------------------------------------
> Attaching gdb to process id 11241.
>
> Saved trace to /home/charpent/.sage/crash_logs/sage_crash_qQFZhv.log
> ------------------------------------------------------------------------
> Unhandled SIGABRT: An abort() occurred in Sage.
> This probably occurred because a *compiled* component of Sage has a bug
> in it and is not properly wrapped with sig_on(), sig_off().
> Sage will now terminate.
> ------------------------------------------------------------------------
> Abandon
>
>
> --
> 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 https://groups.google.com/group/sage-support.
> For more options, visit https://groups.google.com/d/optout.



-- 
William (http://wstein.org)

-- 
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 https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.

Reply via email to