sagemath / sage

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

libsingular interface crashes when calling certain functions with integer coeffficients #36675

Open miguelmarco opened 1 year ago

miguelmarco commented 1 year ago

Steps To Reproduce

In a fresh sage session, run

sage: R.<x,y,z> = ZZ[]
sage: M = matrix(R,[[x,y,x],[x^2+z,y^2,x+y-z]])
sage: from sage.libs.singular.function import singular_function
sage: std = singular_function("std")
sage: std(M)

Expected Behavior

A result similar to what you get with rational coefficients:

sage: S.<x,y,z> = QQ[]
sage: N = matrix(S,[[x,y,x],[x^2+z,y^2,x+y-z]])
sage: std(N)
[(x, x + y - z), (y, y^2), (x^2 - x*y + x*z - x + y, 2*y*z - z^2 - z), (x*y*z - x*y + y^2 - 3*y*z, -y*z^2 - y*z), (y^3 - 2*y^2*z - 2*y^2 + 5*y*z, y*z^2 + y*z), (x*y^2 - x*y - y^2 + y*z, 0)]

Actual Behavior

Get this error message

---------------------------------------------------------------------------
SignalError                               Traceback (most recent call last)
Cell In [5], line 1
----> 1 std(M)

File ~/sage/src/sage/libs/singular/function.pyx:1312, in sage.libs.singular.function.SingularFunction.__call__()
   1310     if not (isinstance(ring, MPolynomialRing_libsingular) or isinstance(ring, NCPolynomialRing_plural)):
   1311         raise TypeError("cannot call Singular function '%s' with ring parameter of type '%s'" % (self._name, type(ring)))
-> 1312     return call_function(self, args, ring, interruptible, attributes)
   1313 
   1314 def _instancedoc_(self):

File ~/sage/src/sage/libs/singular/function.pyx:1491, in sage.libs.singular.function.call_function()
   1489     error_messages.pop()
   1490 
-> 1491 with opt_ctx:  # we are preserving the global options state here
   1492     if signal_handler:
   1493         sig_on()

File ~/sage/src/sage/libs/singular/function.pyx:1493, in sage.libs.singular.function.call_function()
   1491 with opt_ctx:  # we are preserving the global options state here
   1492     if signal_handler:
-> 1493         sig_on()
   1494         _res = self.call_handler.handle_call(argument_list, si_ring)
   1495         sig_off()

SignalError: Segmentation fault

Additional Information

No response

Environment

- **OS**: gentoo linux
- **Sage Version**: 10.2.rc0

Checklist

mkoeppe commented 1 year ago

config.log please so we can see where your libraries are coming from

kwankyu commented 1 year ago

Is it supposed to work? Does it work on singular directly? I have no experience of working with singular for polynomials with integer coefficients.

miguelmarco commented 12 months ago

Is it supposed to work? Does it work on singular directly? I have no experience of working with singular for polynomials with integer coefficients.

Yes, it does work in singular. In fact, it does work with the old (pexpect-based) interface:

sage: R.<x,y,z> = ZZ[]
sage: M = matrix(R,[[x,y,x],[x^2+z,y^2,x+y-z]])
sage: M._singular_().std()._sage_()
[                            x                             y       x^2 - x*y + x*z - x + y     x*y*z - x*y + y^2 - 3*y*z y^3 - 2*y^2*z - 2*y^2 + 5*y*z       x*y^2 - x*y - y^2 + y*z]
[                    x + y - z                           y^2               2*y*z - z^2 - z                  -y*z^2 - y*z                   y*z^2 + y*z                             0]
miguelmarco commented 12 months ago

config.log please so we can see where your libraries are coming from

config.log.txt

miguelmarco commented 12 months ago

Does it work for you? It crashes in every system I tested.

miguelmarco commented 11 months ago

ping @kwankyu @mkoeppe

kwankyu commented 11 months ago

It works with Singular

                     SINGULAR                                 /  Development
 A Computer Algebra System for Polynomial Computations       /   version 4.3.2
                                                           0<
 by: W. Decker, G.-M. Greuel, G. Pfister, H. Schoenemann     \   Feb 2023
FB Mathematik der Universitaet, D-67653 Kaiserslautern        \
> ring R = integer,(x,y,z), dp;
> vector s1 = [x,y,x];
> vector s2 = [x^2+z,y^2,x+y-z];
> module m = s1,s2;
> std(m);
_[1]=x*gen(3)+x*gen(1)+y*gen(2)
_[2]=x2*gen(1)+y2*gen(2)-x*gen(1)+y*gen(3)-y*gen(2)-z*gen(3)+z*gen(1)

In Sage,

sage: R.<x,y,z> = ZZ[]
sage: M = matrix(R,[[x,y,x],[x^2+z,y^2,x+y-z]])
sage: std(M,ring=R)
[(x, x + y - z), (y, y^2), (x^2 - x*y + x*z - x + y, 2*y*z - z^2 - z), (x*y*z - x*y + y^2 - 3*y*z, -y*z^2 - y*z), (y^3 - 2*y^2*z - 2*y^2 + 5*y*z, y*z^2 + y*z), (x*y^2 - x*y - y^2 + y*z, 0)]

which seems wrong. For std(M), it seems to have never worked. I checked with 9.7 and later.

So if you want this "feature", you are on your own.

miguelmarco commented 11 months ago

I guess what happens in the example you posted has to do with considering the module as represented by the rows of the matrix vs the one corresponding to the columns:

sage: std(M.T,ring=R)
[(x, y, x), (x^2 - x + z, y^2 - y, y - z)]

So it seems that the problem comes from not using the ring keyword. That is interesting, since that keyword is not needed when the polynomial ring is defined over a field.

kwankyu commented 11 months ago

Right... I didn't see that. So the "fix" may be easy.

kwankyu commented 11 months ago

You may need to look into src/sage/libs/singular/function.pyx.

miguelmarco commented 11 months ago

I see. It seems that the difference from the field case is the type of the matrix:

sage: type(M)
<class 'sage.matrix.matrix_generic_dense.Matrix_generic_dense'>

whereas the code that tries to guess the ring of the input checks for more specific types:

https://github.com/sagemath/sage/blob/f10820ff877ea61a2fdbf0d7fc3c1445ead77d72/src/sage/libs/singular/function.pyx#L1388C1-L1391C38

miguelmarco commented 11 months ago

So I guess this could be fixed by using Matrix_mpolynomial_dense for matrices with coefficients in a integer polynomial ring.

In the meantime, just specifying the ring seems to solve it. Thank you!