sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.31k stars 451 forks source link

Segmentation fault on calling .change_ring() for a polynomial over AA #36101

Open maxale opened 1 year ago

maxale commented 1 year ago

Steps To Reproduce

The following code causes Segmentation fault:

RR.<x, y, r, s0, c0, s1, c1> = AA[]
f = -4*r^2+(((1+2*AA(cos(pi/6)))*c0*r+2*c1*r+(1+2*AA(cos(pi/6)))*s0*r+2*s1*r)/2-1/2)^2+((1-(1+2*AA(cos(pi/6)))*c0*r-2*c1*r+(1+2*AA(cos(pi/6)))*s0*r+2*s1*r)/2-1/2)^2
f.change_ring( QuadraticField(3) )

Expected Behavior

The ring change should be done without an issue, since individually each coefficient is converted into an element of QuadraticField(3) without an issue:

sage: print( [QuadraticField(3)(c) for c in f.coefficients()] )
[a + 2, a + 2, 2*a + 2, 2, 2*a + 2, 2, -4, -1/2*a - 1/2, -1/2*a - 1/2, -1, -1, 1/4]

Actual Behavior

There is a long trace produced along with the fault, which I can provide upon request.

Additional Information

No response

Environment

- **OS**: Ubuntu 22.04.3 LTS
- **Sage Version**: 10.1.beta7

Checklist

mezzarobba commented 1 year ago

And this works:

sage: RR.<x, y, r, s0, c0, s1, c1> = AA[]
sage: f = -4*r^2+(((1+2*AA(cos(pi/6)))*c0*r+2*c1*r+(1+2*AA(cos(pi/6)))*s0*r+2*s1*r)/2-1/2)^2+((1-(1+2*AA(cos(p
....: i/6)))*c0*r-2*c1*r+(1+2*AA(cos(pi/6)))*s0*r+2*s1*r)/2-1/2)^2
sage: [QuadraticField(3)(c) for c in f.coefficients()]
[a + 2,
 a + 2,
 2*a + 2,
 2,
 2*a + 2,
 2,
 -4,
 -1/2*a - 1/2,
 -1/2*a - 1/2,
 -1,
 -1,
 1/4]
sage: f.change_ring( QuadraticField(3) )
(a + 2)*r^2*s0^2 + (a + 2)*r^2*c0^2 + (2*a + 2)*r^2*s0*s1 + 2*r^2*s1^2 + (2*a + 2)*r^2*c0*c1 + 2*r^2*c1^2 - 4*r^2 + (-1/2*a - 1/2)*r*s0 + (-1/2*a - 1/2)*r*c0 - r*s1 - r*c1 + 1/4

(but just converting one of the non-rational coefficients is not enough).

I suppose there is something wrong with the state of the Singular interface when change_ring() attempts to create the new polynomial, but I'm not familiar enough with Singular to understand what is going on. (@nbruin, @malb, do you have any idea?)

Here is the relevant part (I think) of the backtrace:

#8  0x00007f72eed14220 in __pyx_f_4sage_4libs_8singular_8singular_sa2si_NF () at /home/marc/co/sage/src/sage/libs/singular/singular.cpp:13195
  13190     *
  13191     *     nMapFuncPtr =  naSetMap(_ring.cf, currRing.cf) # choose correct mapping function             # <<<<<<<<<<<<<<
  13192     *
  13193     *     if nMapFuncPtr is NULL:
  13194     */
> 13195      __pyx_v_nMapFuncPtr = naSetMap(__pyx_v__ring->cf, currRing->cf);
  13196
  13197      /* "sage/libs/singular/singular.pyx":1423
  13198     *     nMapFuncPtr =  naSetMap(_ring.cf, currRing.cf) # choose correct mapping function
  13199     *
#9  0x00007f72ef88cd50 in __pyx_pf_4sage_5rings_10polynomial_28multi_polynomial_libsingular_27MPolynomialRing_libsingular_12_element_constructor_ () at /home/marc/co/sage/src/sage/rings/polynomial/multi_polynomial_libsingular.cpp:9966
  9961     *                             p_SetCoeff(mon, sa2si(c , _ring), _ring)             # <<<<<<<<<<<<<<
  9962     *                             for pos in m.nonzero_positions():
  9963     *                                 overflow_check(m[pos], _ring)
  9964     */
  9965                  if (!(likely(((__pyx_v_c) == Py_None) || likely(__Pyx_TypeTest(__pyx_v_c, __pyx_ptype_4sage_9structure_7element_Element))))) __PYX_ERR(0, 904, __pyx_L54_error)
> 9966                  (void)(p_SetCoeff(__pyx_v_mon, __pyx_f_4sage_4libs_8singular_8singular_sa2si(((struct __pyx_obj_4sage_9structure_7element_Element *)__pyx_v_c), __pyx_v__ring), __pyx_v__ring));
  9967
  9968                  /* "sage/rings/polynomial/multi_polynomial_libsingular.pyx":905
  9969     *                             mon = p_Init(_ring)
  9970     *                             p_SetCoeff(mon, sa2si(c , _ring), _ring)
#10 0x0000000000544b70 in cfunction_call () at /build/python3.11-6qBZ2b/python3.11-3.11.4/build-static/../Objects/methodobject.c:542
#11 0x00007f733c19ddd5 in __Pyx_PyObject_Call () at /home/marc/co/sage/src/sage/structure/coerce_maps.c:14540
  14535        ternaryfunc call = Py_TYPE(func)->tp_call;
  14536        if (unlikely(!call))
  14537            return PyObject_Call(func, arg, kw);
  14538        if (unlikely(Py_EnterRecursiveCall((char*)" while calling a Python object")))
  14539            return NULL;
> 14540        result = (*call)(func, arg, kw);
  14541        Py_LeaveRecursiveCall();
  14542        if (unlikely(!result) && unlikely(!PyErr_Occurred())) {
  14543            PyErr_SetString(
  14544                PyExc_SystemError,
#12 0x00007f733c19ddb0 in __Pyx__PyObject_CallOneArg () at /home/marc/co/sage/src/sage/structure/coerce_maps.c:14743
  14738        PyObject *result;
  14739        PyObject *args = PyTuple_New(1);
  14740        if (unlikely(!args)) return NULL;
  14741        Py_INCREF(arg);
  14742        PyTuple_SET_ITEM(args, 0, arg);
> 14743        result = __Pyx_PyObject_Call(func, args, NULL);
  14744        Py_DECREF(args);
  14745        return result;
  14746    }
  14747    static CYTHON_INLINE PyObject* __Pyx_PyObject_CallOneArg(PyObject *func, PyObject *arg) {
#13 0x00007f733c1ab478 in __Pyx_PyObject_CallOneArg () at /home/marc/co/sage/src/sage/structure/coerce_maps.c:14762
  14757            } else if (__Pyx_PyFastCFunction_Check(func)) {
  14758                return __Pyx_PyCFunction_FastCall(func, &arg, 1);
  14759    #endif
  14760            }
  14761        }
> 14762        return __Pyx__PyObject_CallOneArg(func, arg);
  14763    }
  14764    #else
  14765    static CYTHON_INLINE PyObject* __Pyx_PyObject_CallOneArg(PyObject *func, PyObject *arg) {
  14766        PyObject *result;
#14 0x00007f733c1ab2e0 in __pyx_f_4sage_9structure_11coerce_maps_24DefaultConvertMap_unique__call_ () at /home/marc/co/sage/src/sage/structure/coerce_maps.c:4719
  4714              __Pyx_INCREF(__pyx_t_3);
  4715              __Pyx_INCREF(function);
  4716              __Pyx_DECREF_SET(__pyx_t_2, function);
  4717            }
  4718          }
> 4719          __pyx_t_1 = (__pyx_t_3) ? __Pyx_PyObject_Call2Args(__pyx_t_2, __pyx_t_3, __pyx_v_x) : __Pyx_PyObject_CallOneArg(__pyx_t_2, __pyx_v_x);
  4720          __Pyx_XDECREF(__pyx_t_3); __pyx_t_3 = 0;
  4721          if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 158, __pyx_L3_error)
  4722          __Pyx_GOTREF(__pyx_t_1);
  4723          __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;