AUTHORS:
This interface is extremely flexible, since it’s exactly like typing into the Singular interpreter, and anything that works there should work here.
The Singular interface will only work if Singular is installed on your computer; this should be the case, since Singular is included with Sage. The interface offers three pieces of functionality:
EXAMPLES: First we illustrate multivariate polynomial factorization:
sage: R1 = singular.ring(0, '(x,y)', 'dp')
sage: R1
// characteristic : 0
// number of vars : 2
// block 1 : ordering dp
// : names x y
// block 2 : ordering C
sage: f = singular('9x16 - 18x13y2 - 9x12y3 + 9x10y4 - 18x11y2 + 36x8y4 + 18x7y5 - 18x5y6 + 9x6y4 - 18x3y6 - 9x2y7 + 9y8')
sage: f
9*x^16-18*x^13*y^2-9*x^12*y^3+9*x^10*y^4-18*x^11*y^2+36*x^8*y^4+18*x^7*y^5-18*x^5*y^6+9*x^6*y^4-18*x^3*y^6-9*x^2*y^7+9*y^8
sage: f.parent()
Singular
sage: F = f.factorize(); F
[1]:
_[1]=9
_[2]=x^6-2*x^3*y^2-x^2*y^3+y^4
_[3]=-x^5+y^2
[2]:
1,1,2
sage: F[1]
9,
x^6-2*x^3*y^2-x^2*y^3+y^4,
-x^5+y^2
sage: F[1][2]
x^6-2*x^3*y^2-x^2*y^3+y^4
We can convert and each exponent back to Sage objects as well.
sage: R.<x, y> = PolynomialRing(QQ,2)
sage: g = eval(f.sage_polystring()); g
9*x^16 - 18*x^13*y^2 - 9*x^12*y^3 + 9*x^10*y^4 - 18*x^11*y^2 + 36*x^8*y^4 + 18*x^7*y^5 - 18*x^5*y^6 + 9*x^6*y^4 - 18*x^3*y^6 - 9*x^2*y^7 + 9*y^8
sage: eval(F[1][2].sage_polystring())
x^6 - 2*x^3*y^2 - x^2*y^3 + y^4
This example illustrates polynomial GCD’s:
sage: R2 = singular.ring(0, '(x,y,z)', 'lp')
sage: a = singular.new('3x2*(x+y)')
sage: b = singular.new('9x*(y2-x2)')
sage: g = a.gcd(b)
sage: g
x^2+x*y
This example illustrates computation of a Groebner basis:
sage: R3 = singular.ring(0, '(a,b,c,d)', 'lp')
sage: I = singular.ideal(['a + b + c + d', 'a*b + a*d + b*c + c*d', 'a*b*c + a*b*d + a*c*d + b*c*d', 'a*b*c*d - 1'])
sage: I2 = I.groebner()
sage: I2
c^2*d^6-c^2*d^2-d^4+1,
c^3*d^2+c^2*d^3-c-d,
b*d^4-b+d^5-d,
b*c-b*d^5+c^2*d^4+c*d-d^6-d^2,
b^2+2*b*d+d^2,
a+b+c+d
The following example is the same as the one in the Singular - Gap interface documentation:
sage: R = singular.ring(0, '(x0,x1,x2)', 'lp')
sage: I1 = singular.ideal(['x0*x1*x2 -x0^2*x2', 'x0^2*x1*x2-x0*x1^2*x2-x0*x1*x2^2', 'x0*x1-x0*x2-x1*x2'])
sage: I2 = I1.groebner()
sage: I2
x1^2*x2^2,
x0*x2^3-x1^2*x2^2+x1*x2^3,
x0*x1-x0*x2-x1*x2,
x0^2*x2-x0*x2^2-x1*x2^2
This example illustrates moving a polynomial from one ring to another. It also illustrates calling a method of an object with an argument.
sage: R = singular.ring(0, '(x,y,z)', 'dp')
sage: f = singular('x3+y3+(x-y)*x2y2+z2')
sage: f
x^3*y^2-x^2*y^3+x^3+y^3+z^2
sage: R1 = singular.ring(0, '(x,y,z)', 'ds')
sage: f = R.fetch(f)
sage: f
z^2+x^3+y^3+x^3*y^2-x^2*y^3
We can calculate the Milnor number of :
sage: _=singular.LIB('sing.lib') # assign to _ to suppress printing
sage: f.milnor()
4
The Jacobian applied twice yields the Hessian matrix of , with which we can compute.
sage: H = f.jacob().jacob()
sage: H
6*x+6*x*y^2-2*y^3,6*x^2*y-6*x*y^2, 0,
6*x^2*y-6*x*y^2, 6*y+2*x^3-6*x^2*y,0,
0, 0, 2
sage: H.det()
72*x*y+24*x^4-72*x^3*y+72*x*y^3-24*y^4-48*x^4*y^2+64*x^3*y^3-48*x^2*y^4
The 1x1 and 2x2 minors:
sage: H.minor(1)
2,
6*y+2*x^3-6*x^2*y,
6*x^2*y-6*x*y^2,
6*x^2*y-6*x*y^2,
6*x+6*x*y^2-2*y^3
sage: H.minor(2)
12*y+4*x^3-12*x^2*y,
12*x^2*y-12*x*y^2,
12*x^2*y-12*x*y^2,
12*x+12*x*y^2-4*y^3,
-36*x*y-12*x^4+36*x^3*y-36*x*y^3+12*y^4+24*x^4*y^2-32*x^3*y^3+24*x^2*y^4
sage: _=singular.eval('option(redSB)')
sage: H.minor(1).groebner()
1
We compute the projective genus of ideals that define curves over . It is very important to load the normal.lib library before calling the genus command, or you’ll get an error message.
EXAMPLE:
sage: singular.lib('normal.lib')
sage: R = singular.ring(0,'(x,y)','dp')
sage: i2 = singular.ideal('y9 - x2*(x-1)^9 + x')
sage: i2.genus()
40
Note that the genus can be much smaller than the degree:
sage: i = singular.ideal('y9 - x2*(x-1)^9')
sage: i.genus()
0
AUTHORS:
The following illustrates an important concept: how Sage interacts with the data being used and returned by Singular. Let’s compute a Groebner basis for some ideal, using Singular through Sage.
sage: singular.lib('poly.lib')
sage: singular.ring(32003, '(a,b,c,d,e,f)', 'lp')
// characteristic : 32003
// number of vars : 6
// block 1 : ordering lp
// : names a b c d e f
// block 2 : ordering C
sage: I = singular.ideal('cyclic(6)')
sage: g = singular('groebner(I)')
...
TypeError: Singular error:
...
We restart everything and try again, but correctly.
sage: singular.quit()
sage: singular.lib('poly.lib'); R = singular.ring(32003, '(a,b,c,d,e,f)', 'lp')
sage: I = singular.ideal('cyclic(6)')
sage: I.groebner()
f^48-2554*f^42-15674*f^36+12326*f^30-12326*f^18+15674*f^12+2554*f^6-1,
...
It’s important to understand why the first attempt at computing a basis failed. The line where we gave singular the input ‘groebner(I)’ was useless because Singular has no idea what ‘I’ is! Although ‘I’ is an object that we computed with calls to Singular functions, it actually lives in Sage. As a consequence, the name ‘I’ means nothing to Singular. When we called I.groebner(), Sage was able to call the groebner function on’I’ in Singular, since ‘I’ actually means something to Sage.
The Singular interface reads in even very long input (using files) in a robust manner, as long as you are creating a new object.
sage: t = '"%s"'%10^15000 # 15 thousand character string (note that normal Singular input must be at most 10000)
sage: a = singular.eval(t)
sage: a = singular(t)
TESTS: We test an automatic coercion:
sage: a = 3*singular('2'); a
6
sage: type(a)
<class 'sage.interfaces.singular.SingularElement'>
sage: a = singular('2')*3; a
6
sage: type(a)
<class 'sage.interfaces.singular.SingularElement'>
Bases: sage.interfaces.expect.Expect
Interface to the Singular interpreter.
EXAMPLES: A Groebner basis example.
sage: R = singular.ring(0, '(x0,x1,x2)', 'lp')
sage: I = singular.ideal([ 'x0*x1*x2 -x0^2*x2', 'x0^2*x1*x2-x0*x1^2*x2-x0*x1*x2^2', 'x0*x1-x0*x2-x1*x2'])
sage: I.groebner()
x1^2*x2^2,
x0*x2^3-x1^2*x2^2+x1*x2^3,
x0*x1-x0*x2-x1*x2,
x0^2*x2-x0*x2^2-x1*x2^2
AUTHORS:
Load the Singular library named lib.
Note that if the library was already loaded during this session it is not reloaded unless the optional reload argument is True (the default is False).
EXAMPLES:
sage: singular.lib('sing.lib')
sage: singular.lib('sing.lib', reload=True)
Clear the variable named var.
EXAMPLES:
sage: singular.set('int', 'x', '2')
sage: singular.get('x')
'2'
sage: singular.clear('x')
sage: singular.get('x')
'`x`'
EXAMPLES:
sage: singular_console() #not tested
SINGULAR / Development
A Computer Algebra System for Polynomial Computations / version 3-0-4
0<
by: G.-M. Greuel, G. Pfister, H. Schoenemann \ Nov 2007
FB Mathematik der Universitaet, D-67653 Kaiserslautern
Returns the amount of CPU time that the Singular session has used. If t is not None, then it returns the difference between the current CPU time and t.
EXAMPLES:
sage: t = singular.cputime()
sage: R = singular.ring(0, '(x0,x1,x2)', 'lp')
sage: I = singular.ideal([ 'x0*x1*x2 -x0^2*x2', 'x0^2*x1*x2-x0*x1^2*x2-x0*x1*x2^2', 'x0*x1-x0*x2-x1*x2'])
sage: gb = I.groebner()
sage: singular.cputime(t) #random
0.02
Returns the current ring of the running Singular session.
EXAMPLES:
sage: r = PolynomialRing(GF(127),3,'xyz', order='invlex')
sage: r._singular_()
// characteristic : 127
// number of vars : 3
// block 1 : ordering rp
// : names x y z
// block 2 : ordering C
sage: singular.current_ring()
// characteristic : 127
// number of vars : 3
// block 1 : ordering rp
// : names x y z
// block 2 : ordering C
Returns the Singular name of the currently active ring in Singular.
OUTPUT: currently active ring’s name
EXAMPLES:
sage: r = PolynomialRing(GF(127),3,'xyz')
sage: r._singular_().name() == singular.current_ring_name()
True
Send the code x to the Singular interpreter and return the output as a string.
INPUT:
EXAMPLES:
sage: singular.eval('2 > 1')
'1'
sage: singular.eval('2 + 2')
'4'
if the verbosity level is comments are also printed and not only returned.
sage: r = singular.ring(0,'(x,y,z)','dp')
sage: i = singular.ideal(['x^2','y^2','z^2'])
sage: s = i.std()
sage: singular.eval('hilb(%s)'%(s.name()))
'// 1 t^0\n// -3 t^2\n// 3 t^4\n// -1 t^6\n\n// 1 t^0\n//
3 t^1\n// 3 t^2\n// 1 t^3\n// dimension (affine) = 0\n//
degree (affine) = 8'
sage: set_verbose(1)
sage: o = singular.eval('hilb(%s)'%(s.name()))
// 1 t^0
// -3 t^2
// 3 t^4
// -1 t^6
// 1 t^0
// 3 t^1
// 3 t^2
// 1 t^3
// dimension (affine) = 0
// degree (affine) = 8
This is mainly useful if this method is called implicitly. Because then intermediate results, debugging outputs and printed statements are printed
sage: o = s.hilb()
// 1 t^0
// -3 t^2
// 3 t^4
// -1 t^6
// 1 t^0
// 3 t^1
// 3 t^2
// 1 t^3
// dimension (affine) = 0
// degree (affine) = 8
// ** right side is not a datum, assignment ignored
rather than ignored
sage: set_verbose(0)
sage: o = s.hilb()
Get string representation of variable named var.
EXAMPLES:
sage: singular.set('int', 'x', '2')
sage: singular.get('x')
'2'
Return the ideal generated by gens.
INPUT:
OUTPUT: the Singular ideal generated by the given list of gens
EXAMPLES: A Groebner basis example done in a different way.
sage: _ = singular.eval("ring R=0,(x0,x1,x2),lp")
sage: i1 = singular.ideal([ 'x0*x1*x2 -x0^2*x2', 'x0^2*x1*x2-x0*x1^2*x2-x0*x1*x2^2', 'x0*x1-x0*x2-x1*x2'])
sage: i1
-x0^2*x2+x0*x1*x2,
x0^2*x1*x2-x0*x1^2*x2-x0*x1*x2^2,
x0*x1-x0*x2-x1*x2
sage: i2 = singular.ideal('groebner(%s);'%i1.name())
sage: i2
x1^2*x2^2,
x0*x2^3-x1^2*x2^2+x1*x2^3,
x0*x1-x0*x2-x1*x2,
x0^2*x2-x0*x2^2-x1*x2^2
Load the Singular library named lib.
Note that if the library was already loaded during this session it is not reloaded unless the optional reload argument is True (the default is False).
EXAMPLES:
sage: singular.lib('sing.lib')
sage: singular.lib('sing.lib', reload=True)
Creates a list in Singular from a Sage list x.
EXAMPLES:
sage: singular.list([1,2])
[1]:
1
[2]:
2
Load the Singular library named lib.
Note that if the library was already loaded during this session it is not reloaded unless the optional reload argument is True (the default is False).
EXAMPLES:
sage: singular.lib('sing.lib')
sage: singular.lib('sing.lib', reload=True)
EXAMPLES:
sage: singular.lib("matrix")
sage: R = singular.ring(0, '(x,y,z)', 'dp')
sage: A = singular.matrix(3,2,'1,2,3,4,5,6')
sage: A
1,2,
3,4,
5,6
sage: A.gauss_col()
2,-1,
1,0,
0,1
AUTHORS:
Access to Singular’s options as follows:
Syntax: option() Returns a string of all defined options.
Syntax: option( ‘option_name’ ) Sets an option. Note to disable an option, use the prefix no.
Syntax: option( ‘get’ ) Returns an intvec of the state of all options.
Syntax: option( ‘set’, intvec_expression ) Restores the state of all options from an intvec (produced by option(‘get’)).
EXAMPLES:
sage: singular.option()
//options: redefine loadLib usage prompt
sage: singular.option('get')
0,
10321
sage: old_options = _
sage: singular.option('noredefine')
sage: singular.option()
//options: loadLib usage prompt
sage: singular.option('set', old_options)
sage: singular.option('get')
0,
10321
Create a Singular ring and makes it the current ring.
INPUT:
OUTPUT: a Singular ring
Note
This function is not identical to calling the Singular ring function. In particular, it also attempts to “kill” the variable names, so they can actually be used without getting errors, and it sets printing of elements for this range to short (i.e., with *’s and carets).
EXAMPLES: We first declare with degree reverse lexicographic ordering.
sage: R = singular.ring(0, '(x,y,z)', 'dp')
sage: R
// characteristic : 0
// number of vars : 3
// block 1 : ordering dp
// : names x y z
// block 2 : ordering C
sage: R1 = singular.ring(32003, '(x,y,z)', 'dp')
sage: R2 = singular.ring(32003, '(a,b,c,d)', 'lp')
This is a ring in variables named x(1) through x(10) over the finite field of order :
sage: R3 = singular.ring(7, '(x(1..10))', 'ds')
This is a polynomial ring over the transcendental extension of :
sage: R4 = singular.ring('(0,a)', '(mu,nu)', 'lp')
This is a ring over the field of single-precision floats:
sage: R5 = singular.ring('real', '(a,b)', 'lp')
This is over 50-digit floats:
sage: R6 = singular.ring('(real,50)', '(a,b)', 'lp')
sage: R7 = singular.ring('(complex,50,i)', '(a,b)', 'lp')
To use a ring that you’ve defined, use the set_ring() method on the ring. This sets the ring to be the “current ring”. For example,
sage: R = singular.ring(7, '(a,b)', 'ds')
sage: S = singular.ring('real', '(a,b)', 'lp')
sage: singular.new('10*a')
1.000e+01*a
sage: R.set_ring()
sage: singular.new('10*a')
3*a
Set the variable with given name to the given value.
EXAMPLES:
sage: singular.set('int', 'x', '2')
sage: singular.get('x')
'2'
Sets the current Singular ring to R.
EXAMPLES:
sage: R = singular.ring(7, '(a,b)', 'ds')
sage: S = singular.ring('real', '(a,b)', 'lp')
sage: singular.current_ring()
// characteristic : 0 (real)
// number of vars : 2
// block 1 : ordering lp
// : names a b
// block 2 : ordering C
sage: singular.set_ring(R)
sage: singular.current_ring()
// characteristic : 7
// number of vars : 2
// block 1 : ordering ds
// : names a b
// block 2 : ordering C
Sets the current Singular ring to R.
EXAMPLES:
sage: R = singular.ring(7, '(a,b)', 'ds')
sage: S = singular.ring('real', '(a,b)', 'lp')
sage: singular.current_ring()
// characteristic : 0 (real)
// number of vars : 2
// block 1 : ordering lp
// : names a b
// block 2 : ordering C
sage: singular.set_ring(R)
sage: singular.current_ring()
// characteristic : 7
// number of vars : 2
// block 1 : ordering ds
// : names a b
// block 2 : ordering C
Creates a Singular string from a Sage string. Note that the Sage string has to be “double-quoted”.
EXAMPLES:
sage: singular.string('"Sage"')
Sage
Return a list of all Singular commands.
EXAMPLES:
sage: singular.trait_names()
['exteriorPower',
...
'stdfglm']
Bases: sage.interfaces.expect.ExpectElement
Get and set attributes for self.
INPUT:
VALUES: isSB - the standard basis property is set by all commands computing a standard basis like groebner, std, stdhilb etc.; used by lift, dim, degree, mult, hilb, vdim, kbase isHomog - the weight vector for homogeneous or quasihomogeneous ideals/modules isCI - complete intersection property isCM - Cohen-Macaulay property rank - set the rank of a module (see nrows) withSB - value of type ideal, resp. module, is std withHilb - value of type intvec is hilb(_,1) (see hilb) withRes - value of type list is a free resolution withDim - value of type int is the dimension (see dim) withMult - value of type int is the multiplicity (see mult)
EXAMPLE:
sage: P.<x,y,z> = PolynomialRing(QQ)
sage: I = Ideal([z^2, y*z, y^2, x*z, x*y, x^2])
sage: Ibar = I._singular_()
sage: Ibar.attrib('isSB')
0
sage: singular.eval('vdim(%s)'%Ibar.name()) # sage7 name is random
// ** sage7 is no standard basis
4
sage: Ibar.attrib('isSB',1)
sage: singular.eval('vdim(%s)'%Ibar.name())
'4'
EXAMPLES:
sage: R=singular.ring(0,'(x,y)','dp')
sage: RL = R.ringlist()
sage: RL.sage_flattened_str_list()
['0', 'x', 'y', 'dp', '1,1', 'C', '0', '_[1]=0']
Returns Sage matrix for self
EXAMPLES:
sage: R = singular.ring(0, '(x,y,z)', 'dp')
sage: A = singular.matrix(2,2)
sage: A.sage_matrix(ZZ)
[0 0]
[0 0]
sage: A.sage_matrix(RDF)
[0.0 0.0]
[0.0 0.0]
Returns a Sage polynomial in the ring r matching the provided poly which is a singular polynomial.
INPUT:
OUTPUT: MPolynomial
EXAMPLES:
sage: R = PolynomialRing(GF(2^8,'a'),2,'xy')
sage: f=R('a^20*x^2*y+a^10+x')
sage: f._singular_().sage_poly(R)==f
True
sage: R = PolynomialRing(GF(2^8,'a'),1,'x')
sage: f=R('a^20*x^3+x^2+a^10')
sage: f._singular_().sage_poly(R)==f
True
sage: P.<x,y> = PolynomialRing(QQ, 2)
sage: f = x*y**3 - 1/9 * x + 1; f
x*y^3 - 1/9*x + 1
sage: singular(f)
x*y^3-1/9*x+1
sage: P(singular(f))
x*y^3 - 1/9*x + 1
AUTHOR:
Note
For very simple polynomials eval(SingularElement.sage_polystring()) is faster than SingularElement.sage_poly(R), maybe we should detect the crossover point (in dependence of the string length) and choose an appropriate conversion strategy
If this Singular element is a polynomial, return a string representation of this polynomial that is suitable for evaluation in Python. Thus * is used for multiplication and ** for exponentiation. This function is primarily used internally.
The short=0 option must be set for the parent ring or this function will not work as expected. This option is set by default for rings created using singular.ring or set using ring_name.set_ring().
EXAMPLES:
sage: R = singular.ring(0,'(x,y)')
sage: f = singular('x^3 + 3*y^11 + 5')
sage: f
x^3+3*y^11+5
sage: f.sage_polystring()
'x**3+3*y**11+5'
If self is a Singular list of lists of Singular elements, returns corresponding Sage list of lists of strings.
EXAMPLES:
sage: R=singular.ring(0,'(x,y)','dp')
sage: RL=R.ringlist()
sage: RL
[1]:
0
[2]:
[1]:
x
[2]:
y
[3]:
[1]:
[1]:
dp
[2]:
1,1
[2]:
[1]:
C
[2]:
0
[4]:
_[1]=0
sage: RL.sage_structured_str_list()
['0', ['x', 'y'], [['dp', '1,\n1 '], ['C', '0 ']], '0']
Sets the current ring in Singular to be self.
EXAMPLES:
sage: R = singular.ring(7, '(a,b)', 'ds')
sage: S = singular.ring('real', '(a,b)', 'lp')
sage: singular.current_ring()
// characteristic : 0 (real)
// number of vars : 2
// block 1 : ordering lp
// : names a b
// block 2 : ordering C
sage: R.set_ring()
sage: singular.current_ring()
// characteristic : 7
// number of vars : 2
// block 1 : ordering ds
// : names a b
// block 2 : ordering C
Returns the possible tab-completions for self. In this case, we just return all the tab completions for the Singular object.
EXAMPLES:
sage: R = singular.ring(0,'(x,y)','dp')
sage: R.trait_names()
['exteriorPower',
...
'stdfglm']
Returns the internal type of this element.
EXAMPLES:
sage: R = PolynomialRing(GF(2^8,'a'),2,'x')
sage: R._singular_().type()
'ring'
sage: fs = singular('x0^2','poly')
sage: fs.type()
'poly'
Generate global dictionaries which hold the docstrings for Singular functions.
EXAMPLE:
sage: from sage.interfaces.singular import generate_docstring_dictionary
sage: generate_docstring_dictionary()
Return the docstring for the function name.
INPUT:
EXAMPLE:
sage: from sage.interfaces.singular import get_docstring
sage: 'groebner' in get_docstring('groebner')
True
sage: 'standard.lib' in get_docstring('groebner')
True
Returns True is x is of type SingularElement.
EXAMPLES:
sage: from sage.interfaces.singular import is_SingularElement
sage: is_SingularElement(singular(2))
True
sage: is_SingularElement(2)
False
Note that this returns an invalid Singular object!
EXAMPLES:
sage: from sage.interfaces.singular import reduce_load
sage: reduce_load()
(invalid object -- defined in terms of closed session)
EXAMPLES:
sage: from sage.interfaces.singular import reduce_load_Singular
sage: reduce_load_Singular()
Singular
Spawn a new Singular command-line session.
EXAMPLES:
sage: singular_console() #not tested
SINGULAR / Development
A Computer Algebra System for Polynomial Computations / version 3-0-4
0<
by: G.-M. Greuel, G. Pfister, H. Schoenemann \ Nov 2007
FB Mathematik der Universitaet, D-67653 Kaiserslautern
Returns the version of Singular being used.
EXAMPLES: