Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" libSingular: Functions
Sage implements a C wrapper around the Singular interpreter which allows to call any function directly from Sage without string parsing or interprocess communication overhead. Users who do not want to call Singular functions directly, usually do not have to worry about this interface, since it is handled by higher level functions in Sage.
AUTHORS:
- Michael Brickenstein (2009-07): initial implementation, overall design - Martin Albrecht (2009-07): clean up, enhancements, etc. - Michael Brickenstein (2009-10): extension to more Singular types - Martin Albrecht (2010-01): clean up, support for attributes - Simon King (2011-04): include the documentation provided by Singular as a code block. - Burcin Erocal, Michael Brickenstein, Oleksandr Motsak, Alexander Dreyer, Simon King (2011-09) plural support
EXAMPLES:
The direct approach for loading a Singular function is to call the function :func:`singular_function` with the function name as parameter::
sage: from sage.libs.singular.function import singular_function sage: P.<a,b,c,d> = PolynomialRing(GF(7)) sage: std = singular_function('std') sage: I = sage.rings.ideal.Cyclic(P) sage: std(I) [a + b + c + d, b^2 + 2*b*d + d^2, b*c^2 + c^2*d - b*d^2 - d^3, b*c*d^2 + c^2*d^2 - b*d^3 + c*d^3 - d^4 - 1, b*d^4 + d^5 - b - d, c^3*d^2 + c^2*d^3 - c - d, c^2*d^4 + b*c - b*d + c*d - 2*d^2]
If a Singular library needs to be loaded before a certain function is available, use the :func:`lib` function as shown below::
sage: from sage.libs.singular.function import singular_function, lib as singular_lib sage: primdecSY = singular_function('primdecSY') Traceback (most recent call last): ... NameError: Singular library function 'primdecSY' is not defined
sage: singular_lib('primdec.lib') sage: primdecSY = singular_function('primdecSY')
There is also a short-hand notation for the above::
sage: import sage.libs.singular.function_factory sage: primdecSY = sage.libs.singular.function_factory.ff.primdec__lib.primdecSY
The above line will load "primdec.lib" first and then load the function ``primdecSY``.
TESTS::
sage: from sage.libs.singular.function import singular_function sage: std = singular_function('std') sage: loads(dumps(std)) == std True """
#***************************************************************************** # Copyright (C) 2009 Michael Brickenstein <brickenstein@mfo.de> # Copyright (C) 2009,2010 Martin Albrecht <M.R.Albrecht@rhul.ac.uk> # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** from __future__ import absolute_import
from libc.string cimport memcpy from cysignals.signals cimport sig_on, sig_off
from sage.cpython.string cimport str_to_bytes, char_to_str
from sage.structure.sage_object cimport SageObject from sage.structure.richcmp cimport richcmp
from sage.rings.integer cimport Integer
from sage.modules.free_module_element cimport FreeModuleElement_generic_dense
from sage.rings.polynomial.multi_polynomial_libsingular cimport MPolynomial_libsingular, new_MP from sage.rings.polynomial.multi_polynomial_libsingular cimport MPolynomialRing_libsingular
from sage.rings.polynomial.plural cimport NCPolynomialRing_plural, NCPolynomial_plural, new_NCP from sage.rings.polynomial.multi_polynomial_ideal import NCPolynomialIdeal
from sage.rings.polynomial.multi_polynomial_ideal import MPolynomialIdeal
from sage.rings.polynomial.multi_polynomial_ideal_libsingular cimport sage_ideal_to_singular_ideal, singular_ideal_to_sage_sequence
from sage.libs.singular.decl cimport *
from sage.libs.singular.option import opt_ctx from sage.libs.singular.polynomial cimport singular_vector_maximal_component, singular_polynomial_check from sage.libs.singular.singular cimport sa2si, si2sa, si2sa_intvec
from sage.libs.singular.singular import error_messages
from sage.interfaces.singular import get_docstring
from sage.misc.misc import get_verbose
from sage.structure.sequence import Sequence, Sequence_generic from sage.rings.polynomial.multi_polynomial_sequence import PolynomialSequence, PolynomialSequence_generic
cdef poly* sage_vector_to_poly(v, ring *r) except <poly*> -1: """ Convert a vector or list of multivariate polynomials to a polynomial by adding them all up. """ cdef poly *poly_component cdef poly *p_iter cdef int component
cdef class RingWrap: """ A simple wrapper around Singular's rings. """ def __repr__(self): """ EXAMPLES::
sage: from sage.libs.singular.function import singular_function sage: P.<x,y,z> = PolynomialRing(QQ) sage: ringlist = singular_function("ringlist") sage: l = ringlist(P) sage: ring = singular_function("ring") sage: ring(l, ring=P) <RingWrap> """
def __dealloc__(self):
def ngens(self): """ Get number of generators.
EXAMPLES::
sage: from sage.libs.singular.function import singular_function sage: P.<x,y,z> = PolynomialRing(QQ) sage: ringlist = singular_function("ringlist") sage: l = ringlist(P) sage: ring = singular_function("ring") sage: ring(l, ring=P).ngens() 3 """
def var_names(self): """ Get names of variables.
EXAMPLES::
sage: from sage.libs.singular.function import singular_function sage: P.<x,y,z> = PolynomialRing(QQ) sage: ringlist = singular_function("ringlist") sage: l = ringlist(P) sage: ring = singular_function("ring") sage: ring(l, ring=P).var_names() ['x', 'y', 'z'] """
def npars(self): """ Get number of parameters.
EXAMPLES::
sage: from sage.libs.singular.function import singular_function sage: P.<x,y,z> = PolynomialRing(QQ) sage: ringlist = singular_function("ringlist") sage: l = ringlist(P) sage: ring = singular_function("ring") sage: ring(l, ring=P).npars() 0 """
def ordering_string(self): """ Get Singular string defining monomial ordering.
EXAMPLES::
sage: from sage.libs.singular.function import singular_function sage: P.<x,y,z> = PolynomialRing(QQ) sage: ringlist = singular_function("ringlist") sage: l = ringlist(P) sage: ring = singular_function("ring") sage: ring(l, ring=P).ordering_string() 'dp(3),C' """
def par_names(self): """ Get parameter names.
EXAMPLES::
sage: from sage.libs.singular.function import singular_function sage: P.<x,y,z> = PolynomialRing(QQ) sage: ringlist = singular_function("ringlist") sage: l = ringlist(P) sage: ring = singular_function("ring") sage: ring(l, ring=P).par_names() [] """
def characteristic(self): """ Get characteristic.
EXAMPLES::
sage: from sage.libs.singular.function import singular_function sage: P.<x,y,z> = PolynomialRing(QQ) sage: ringlist = singular_function("ringlist") sage: l = ringlist(P) sage: ring = singular_function("ring") sage: ring(l, ring=P).characteristic() 0 """
def is_commutative(self): """ Determine whether a given ring is commutative.
EXAMPLES::
sage: from sage.libs.singular.function import singular_function sage: P.<x,y,z> = PolynomialRing(QQ) sage: ringlist = singular_function("ringlist") sage: l = ringlist(P) sage: ring = singular_function("ring") sage: ring(l, ring=P).is_commutative() True """
def _output(self): """ Use Singular output.
EXAMPLES::
sage: from sage.libs.singular.function import singular_function sage: P.<x,y,z> = PolynomialRing(QQ) sage: ringlist = singular_function("ringlist") sage: l = ringlist(P) sage: ring = singular_function("ring") sage: ring(l, ring=P)._output() // coefficients: QQ // number of vars : 3 // block 1 : ordering dp // : names x y z // block 2 : ordering C """
cdef class Resolution: """ A simple wrapper around Singular's resolutions. """ def __init__(self, base_ring): """ EXAMPLES::
sage: from sage.libs.singular.function import singular_function sage: mres = singular_function("mres") sage: syz = singular_function("syz") sage: P.<x,y,z> = PolynomialRing(QQ) sage: I = P.ideal([x+y,x*y-y, y*2,x**2+1]) sage: M = syz(I) sage: resolution = mres(M, 0) """ #FIXME: still not working noncommutative def __repr__(self): """ EXAMPLES::
sage: from sage.libs.singular.function import singular_function sage: mres = singular_function("mres") sage: syz = singular_function("syz") sage: P.<x,y,z> = PolynomialRing(QQ) sage: I = P.ideal([x+y,x*y-y, y*2,x**2+1]) sage: M = syz(I) sage: resolution = mres(M, 0) sage: resolution <Resolution> """ def __dealloc__(self): """ EXAMPLES::
sage: from sage.libs.singular.function import singular_function sage: mres = singular_function("mres") sage: syz = singular_function("syz") sage: P.<x,y,z> = PolynomialRing(QQ) sage: I = P.ideal([x+y,x*y-y, y*2,x**2+1]) sage: M = syz(I) sage: resolution = mres(M, 0) sage: del resolution """
cdef leftv* new_leftv(void *data, res_type): """ INPUT:
- ``data`` - some Singular data this interpreter object points to - ``res_type`` - the type of that data """ cdef leftv* res
cdef free_leftv(leftv *args, ring *r = NULL): """ Kills this ``leftv`` and all ``leftv``s in the tail.
INPUT:
- ``args`` - a list of Singular arguments """
# ===================================== # = Singular/Plural Abstraction Layer = # =====================================
def is_sage_wrapper_for_singular_ring(ring): """ Check whether wrapped ring arises from Singular or Singular/Plural.
EXAMPLES::
sage: from sage.libs.singular.function import is_sage_wrapper_for_singular_ring sage: P.<x,y,z> = QQ[] sage: is_sage_wrapper_for_singular_ring(P) True
::
sage: A.<x,y,z> = FreeAlgebra(QQ, 3) sage: P = A.g_algebra(relations={y*x:-x*y}, order = 'lex') sage: is_sage_wrapper_for_singular_ring(P) True
"""
cdef new_sage_polynomial(ring, poly *p): else: raise ValueError("not a singular or plural ring")
def is_singular_poly_wrapper(p): """ Checks if p is some data type corresponding to some singular ``poly``.
EXAMPLES::
sage: from sage.libs.singular.function import is_singular_poly_wrapper sage: A.<x,y,z> = FreeAlgebra(QQ, 3) sage: H.<x,y,z> = A.g_algebra({z*x:x*z+2*x, z*y:y*z-2*y}) sage: is_singular_poly_wrapper(x+y) True
"""
def all_singular_poly_wrapper(s): """ Tests for a sequence ``s``, whether it consists of singular polynomials.
EXAMPLES::
sage: from sage.libs.singular.function import all_singular_poly_wrapper sage: P.<x,y,z> = QQ[] sage: all_singular_poly_wrapper([x+1, y]) True sage: all_singular_poly_wrapper([x+1, y, 1]) False """
cdef poly* access_singular_poly(p) except <poly*> -1: """ Get the raw ``poly`` pointer from a wrapper object. """ else: raise ValueError("not a singular polynomial wrapper")
cdef ring* access_singular_ring(r) except <ring*> -1: """ Get the singular ``ring`` pointer from a wrapper object. """ raise ValueError("not a singular polynomial ring wrapper")
cdef poly* copy_sage_polynomial_into_singular_poly(p):
def all_vectors(s): """ Checks if a sequence ``s`` consists of free module elements over a singular ring.
EXAMPLES::
sage: from sage.libs.singular.function import all_vectors sage: P.<x,y,z> = QQ[] sage: M = P**2 sage: all_vectors([x]) False sage: all_vectors([(x,y)]) False sage: all_vectors([M(0), M((x,y))]) True sage: all_vectors([M(0), M((x,y)),(0,0)]) False """
cdef class Converter(SageObject): """ A :class:`Converter` interfaces between Sage objects and Singular interpreter objects. """
def __init__(self, args, ring, attributes=None): """ Create a new argument list.
INPUT:
- ``args`` - a list of Python objects - ``ring`` - a multivariate polynomial ring - ``attributes`` - an optional dictionary of Singular attributes (default: ``None``)
EXAMPLES::
sage: from sage.libs.singular.function import Converter sage: P.<a,b,c> = PolynomialRing(GF(127)) sage: Converter([a,b,c],ring=P) Singular Converter in Multivariate Polynomial Ring in a, b, c over Finite Field of size 127 """ cdef leftv *v
# as output ideals get converted to sequences # sequences of polynomials should get converted to ideals # this means, that Singular lists should not be converted to Sequences, # as we do not want ambiguities v = self.append_ideal(ring.ideal(a))
is_intvec = False break else: v = self.append_list(a) v = self.append_number(a)
else: raise TypeError("unknown argument type '%s'"%(type(a),))
else: raise NotImplementedError("Support for attribute '%s' not implemented yet."%attrib)
def ring(self): """ Return the ring in which the arguments of this list live.
EXAMPLES::
sage: from sage.libs.singular.function import Converter sage: P.<a,b,c> = PolynomialRing(GF(127)) sage: Converter([a,b,c],ring=P).ring() Multivariate Polynomial Ring in a, b, c over Finite Field of size 127 """
def _repr_(self): """ EXAMPLES::
sage: from sage.libs.singular.function import Converter sage: P.<a,b,c> = PolynomialRing(GF(127)) sage: Converter([a,b,c],ring=P) # indirect doctest Singular Converter in Multivariate Polynomial Ring in a, b, c over Finite Field of size 127 """
def __dealloc__(self):
def __len__(self): """ EXAMPLES::
sage: from sage.libs.singular.function import Converter sage: P.<a,b,c> = PolynomialRing(GF(127)) sage: len(Converter([a,b,c],ring=P)) 3 """ cdef leftv * v cdef int l
cdef leftv* pop_front(self) except NULL: """ Pop a Singular element from the front of the list. """
cdef leftv *_append_leftv(self, leftv *v): """ Append a new Singular element to the list. """ cdef leftv* last else:
cdef leftv *_append(self, void* data, int res_type): """ Create a new ``leftv`` and append it to the list.
INPUT:
- ``data`` - the raw data - ``res_type`` - the type of the data """
cdef to_sage_matrix(self, matrix* mat): """ Convert singular matrix to matrix over the polynomial ring. """ #cdef ring *singular_ring = (<MPolynomialRing_libsingular>\ # self._sage_ring)._ring
cdef to_sage_vector_destructive(self, poly *p, free_module = None): #cdef ring *r=self._ring._ring cdef int rank else: cdef poly *acc cdef poly *p_iter cdef poly *first cdef poly *previous cdef int i else: else:
cdef object to_sage_module_element_sequence_destructive( self, ideal *i): """ Convert a SINGULAR module to a Sage Sequence (the format Sage stores a Groebner basis in).
INPUT:
- ``i`` -- a SINGULAR ideal - ``r`` -- a SINGULAR ring - ``sage_ring`` -- a Sage ring matching r """ #cdef MPolynomialRing_libsingular sage_ring = self._ring cdef int j
cdef to_sage_integer_matrix(self, intvec* mat): """ Convert Singular matrix to matrix over the polynomial ring. """
cdef leftv *append_polynomial(self, p) except NULL: """ Append the polynomial ``p`` to the list. """ cdef poly* _p
cdef leftv *append_ideal(self, i) except NULL: """ Append the ideal ``i`` to the list. """
cdef leftv *append_module(self, m) except NULL: """ Append sequence ``m`` of vectors over the polynomial ring to the list """ cdef ideal *result cdef ideal *i
cdef leftv *append_number(self, n) except NULL: """ Append the number ``n`` to the list. """ cdef number *_n = sa2si(n, self._singular_ring) return self._append(<void *>_n, NUMBER_CMD)
cdef leftv *append_ring(self, r) except NULL: """ Append the ring ``r`` to the list. """
cdef leftv *append_matrix(self, mat) except NULL:
cdef poly *p #FIXME
cdef leftv *append_int(self, n) except NULL: """ Append the integer ``n`` to the list. """
cdef leftv *append_list(self, l) except NULL: """ Append the list ``l`` to the list. """
cdef leftv* iv
cdef leftv *append_intvec(self, a) except NULL: """ Append ``a`` to the list as intvec. """
cdef leftv *append_vector(self, v) except NULL: """ Append vector ``v`` from free module over polynomial ring. """
cdef leftv *append_resolution(self, Resolution resolution) except NULL: """ Append free resolution ``r`` to the list. """
cdef leftv *append_intmat(self, a) except NULL: """ Append ``a`` to the list as intvec. """
cdef leftv *append_str(self, n) except NULL: """ Append the string ``n`` to the list. """
cdef to_python(self, leftv* to_convert): """ Convert the ``leftv`` to a Python object.
INPUT:
- ``to_convert`` - a Singular ``leftv``
TESTS:
Check that negative integers come through unscathed::
sage: P.<x,y,z> = QQ[] sage: C = Curve((x-y)*(y-z)*(z-x)) sage: I = C.defining_ideal() sage: import sage.libs.singular.function_factory sage: freerank = sage.libs.singular.function_factory.ff.poly__lib.freerank sage: freerank(I, true) [-1, [x^2*y - x*y^2 - x^2*z + y^2*z + x*z^2 - y*z^2]]
Singular's genus function is prone to crashing, see :trac:`12851` and :trac:`19750` ::
sage: sing_genus = sage.libs.singular.function_factory.ff.normal__lib.genus # known bug sage: sing_genus(I) # known bug -2 """ #FIXME cdef MPolynomial_libsingular res_poly cdef lists *singular_list cdef Resolution res_resolution
#FIXME #prevent it getting free, when cleaning the leftv
# TODO: Need to determine what kind of data can be returned by a # STRING_CMD--is it just ASCII strings or can it be an arbitrary # binary? <poly *> to_convert.data)
&(singular_list.m[i])))
) else: raise NotImplementedError("rtyp %d not implemented."%(rtyp))
cdef class BaseCallHandler: """ A call handler is an abstraction which hides the details of the implementation differences between kernel and library functions. """ cdef leftv* handle_call(self, Converter argument_list, ring *_ring=NULL): """ Actual function call. """ return NULL
cdef bint free_res(self): """ Do we need to free the result object. """ return False
cdef class LibraryCallHandler(BaseCallHandler): """ A call handler is an abstraction which hides the details of the implementation differences between kernel and library functions.
This class implements calling a library function.
.. NOTE::
Do not construct this class directly, use :func:`singular_function` instead. """ def __init__(self): """ EXAMPLES::
sage: from sage.libs.singular.function import LibraryCallHandler sage: LibraryCallHandler() <sage.libs.singular.function.LibraryCallHandler object at 0x...> """
cdef leftv* handle_call(self, Converter argument_list, ring *_ring=NULL): cdef leftv * res
cdef bint free_res(self): """ We do not need to free the result object for library functions. """
cdef class KernelCallHandler(BaseCallHandler): """ A call handler is an abstraction which hides the details of the implementation differences between kernel and library functions.
This class implements calling a kernel function.
.. NOTE::
Do not construct this class directly, use :func:`singular_function` instead. """ def __init__(self, cmd_n, arity): """ EXAMPLES::
sage: from sage.libs.singular.function import KernelCallHandler sage: KernelCallHandler(0,0) <sage.libs.singular.function.KernelCallHandler object at 0x...> """
cdef leftv* handle_call(self, Converter argument_list, ring *_ring=NULL): cdef leftv * res cdef leftv *arg1 cdef leftv *arg2 cdef leftv *arg3
# Handle functions with an arbitrary number of arguments, sent # by an argument list.
elif number_of_arguments == 2:
elif number_of_arguments == 3:
global errorreported global error_messages
"Wrong number of arguments (got {} arguments, arity code is {})"
cdef bint free_res(self): """ We need to free the result object for kernel functions. """
# The Sage ring used as a dummy for singular function calls. cdef object dummy_ring
cdef class SingularFunction(SageObject): """ The base class for Singular functions either from the kernel or from the library. """
def __init__(self, name): """ INPUT:
- ``name`` - the name of the function
EXAMPLES::
sage: from sage.libs.singular.function import SingularFunction sage: SingularFunction('foobar') foobar (singular function) """ global currRingHdl
cdef BaseCallHandler get_call_handler(self): """ Return a call handler which does the actual work. """ raise NotImplementedError
cdef bint function_exists(self): """ Return ``True`` if the function exists in this interface. """ raise NotImplementedError
def _repr_(self): """ EXAMPLES::
sage: from sage.libs.singular.function import SingularFunction sage: SingularFunction('foobar') # indirect doctest foobar (singular function) """
def __call__(self, *args, ring=None, bint interruptible=True, attributes=None): """ Call this function with the provided arguments ``args`` in the ``ring``.
INPUT:
- ``args`` -- a list of arguments - ``ring`` -- a multivariate polynomial ring - ``interruptible`` -- if ``True`` pressing Ctrl-C during the execution of this function will interrupt the computation (default: ``True``)
- ``attributes`` -- a dictionary of optional Singular attributes assigned to Singular objects (default: ``None``)
If ``ring`` is not specified, it is guessed from the given arguments. If this is not possible, then a dummy ring, univariate polynomial ring over ``QQ``, is used.
EXAMPLES::
sage: from sage.libs.singular.function import singular_function sage: size = singular_function('size') sage: P.<a,b,c> = PolynomialRing(QQ) sage: size(a, ring=P) 1 sage: size(2r,ring=P) 1 sage: size(2, ring=P) 1 sage: size(2) 1 sage: size(Ideal([a*b + c, a + 1])) 2 sage: size(Ideal([a*b + c, a + 1])) 2 sage: size(1,2) Traceback (most recent call last): ... RuntimeError: error in Singular function call 'size': Wrong number of arguments (got 2 arguments, arity code is 300) sage: size('foobar', ring=P) 6
Show the usage of the optional ``attributes`` parameter::
sage: P.<x,y,z> = PolynomialRing(QQ) sage: I = Ideal([x^3*y^2 + 3*x^2*y^2*z + y^3*z^2 + z^5]) sage: I = Ideal(I.groebner_basis()) sage: hilb = sage.libs.singular.function_factory.ff.hilb sage: hilb(I) # Singular will print // ** _ is no standard basis // ** _ is no standard basis // 1 t^0 // -1 t^5 <BLANKLINE> // 1 t^0 // 1 t^1 // 1 t^2 // 1 t^3 // 1 t^4 // dimension (proj.) = 1 // degree (proj.) = 5
So we tell Singular that ``I`` is indeed a Groebner basis::
sage: hilb(I,attributes={I:{'isSB':1}}) # no complaint from Singular // 1 t^0 // -1 t^5 <BLANKLINE> // 1 t^0 // 1 t^1 // 1 t^2 // 1 t^3 // 1 t^4 // dimension (proj.) = 1 // degree (proj.) = 5
TESTS:
We show that the interface recovers gracefully from errors::
sage: P.<e,d,c,b,a> = PolynomialRing(QQ,5,order='lex') sage: I = sage.rings.ideal.Cyclic(P)
sage: triangL = sage.libs.singular.function_factory.ff.triang__lib.triangL sage: _ = triangL(I) Traceback (most recent call last): ... RuntimeError: error in Singular function call 'triangL': The input is no groebner basis. leaving triang.lib::triangL
sage: G= Ideal(I.groebner_basis()) sage: triangL(G,attributes={G:{'isSB':1}}) [[e + d + c + b + a, ...]] """ global dummy_ring
def _instancedoc_(self): """ EXAMPLES::
sage: from sage.libs.singular.function import singular_function sage: groebner = singular_function('groebner') sage: 'groebner' in groebner.__doc__ True """
prefix = \ """ This function is an automatically generated C wrapper around the Singular function '%s'.
This wrapper takes care of converting Sage datatypes to Singular datatypes and vice versa. In addition to whatever parameters the underlying Singular function accepts when called, this function also accepts the following keyword parameters:
INPUT:
- ``args`` -- a list of arguments - ``ring`` -- a multivariate polynomial ring - ``interruptible`` -- if ``True`` pressing Ctrl-C during the execution of this function will interrupt the computation (default: ``True``) - ``attributes`` -- a dictionary of optional Singular attributes assigned to Singular objects (default: ``None``)
If ``ring`` is not specified, it is guessed from the given arguments. If this is not possible, then a dummy ring, univariate polynomial ring over ``QQ``, is used.
EXAMPLES::
sage: groebner = sage.libs.singular.function_factory.ff.groebner sage: P.<x, y> = PolynomialRing(QQ) sage: I = P.ideal(x^2-y, y+x) sage: groebner(I) [x + y, y^2 - y] sage: triangL = sage.libs.singular.function_factory.ff.triang__lib.triangL sage: P.<x1, x2> = PolynomialRing(QQ, order='lex') sage: f1 = 1/2*((x1^2 + 2*x1 - 4)*x2^2 + 2*(x1^2 + x1)*x2 + x1^2) sage: f2 = 1/2*((x1^2 + 2*x1 + 1)*x2^2 + 2*(x1^2 + x1)*x2 - 4*x1^2) sage: I = Ideal(Ideal(f1,f2).groebner_basis()[::-1]) sage: triangL(I, attributes={I:{'isSB':1}}) [[x2^4 + 4*x2^3 - 6*x2^2 - 20*x2 + 5, 8*x1 - x2^3 + x2^2 + 13*x2 - 5], [x2, x1^2], [x2, x1^2], [x2, x1^2]]
The Singular documentation for '%s' is given below. # Trac ticket #11268: Include the Singular documentation as a block of code else: return prefix + "\n::\n\n"+" Singular documentation not found"
cdef common_ring(self, tuple args, ring=None): """ Return the common ring for the argument list ``args``.
If ``ring`` is not ``None`` this routine checks whether it is the parent/ring of all members of ``args`` instead.
If no common ring was found, None is returned.
INPUT:
- ``args`` -- a list of Python objects - ``ring`` -- an optional ring to check """ #TODO: catch exception, if recursion finds no ring
raise ValueError("Rings do not match up.")
def __reduce__(self): """ EXAMPLES::
sage: from sage.libs.singular.function import singular_function sage: groebner = singular_function('groebner') sage: groebner == loads(dumps(groebner)) True """
def __richcmp__(self, other, op): """ EXAMPLES::
sage: from sage.libs.singular.function import singular_function sage: groebner = singular_function('groebner') sage: groebner == singular_function('groebner') True sage: groebner == singular_function('std') False sage: groebner == 1 False sage: groebner == None False """
cdef inline call_function(SingularFunction self, tuple args, object R, bint signal_handler=True, attributes=None): global currRingHdl global errorreported global currentVoice global myynest global error_messages
cdef ring *si_ring else:
cdef leftv * _res
else: _res = self.call_handler.handle_call(argument_list, si_ring)
myynest = 0
currentVoice = NULL
else:
cdef class SingularLibraryFunction(SingularFunction): """ EXAMPLES::
sage: from sage.libs.singular.function import SingularLibraryFunction sage: R.<x,y> = PolynomialRing(QQ, order='lex') sage: I = R.ideal(x, x+1) sage: f = SingularLibraryFunction("groebner") sage: f(I) [1] """ def __init__(self, name): """ Construct a new Singular kernel function.
EXAMPLES::
sage: from sage.libs.singular.function import SingularLibraryFunction sage: R.<x,y> = PolynomialRing(QQ, order='lex') sage: I = R.ideal(x + 1, x*y + 1) sage: f = SingularLibraryFunction("groebner") sage: f(I) [y - 1, x + 1] """
cdef BaseCallHandler get_call_handler(self): raise ValueError("Not a procedure")
cdef bint function_exists(self): cdef idhdl* singular_idhdl = ggetid(str_to_bytes(self._name)) return singular_idhdl!=NULL
cdef class SingularKernelFunction(SingularFunction): """ EXAMPLES::
sage: from sage.libs.singular.function import SingularKernelFunction sage: R.<x,y> = PolynomialRing(QQ, order='lex') sage: I = R.ideal(x, x+1) sage: f = SingularKernelFunction("std") sage: f(I) [1] """ def __init__(self, name): """ Construct a new Singular kernel function.
EXAMPLES::
sage: from sage.libs.singular.function import SingularKernelFunction sage: R.<x,y> = PolynomialRing(QQ, order='lex') sage: I = R.ideal(x + 1, x*y + 1) sage: f = SingularKernelFunction("std") sage: f(I) [y - 1, x + 1] sage: SingularKernelFunction("no_such_function") Traceback (most recent call last): ... NameError: Singular kernel function 'no_such_function' is not defined """
cdef BaseCallHandler get_call_handler(self):
cdef bint function_exists(self): cdef int cmd_n = -1 arity = IsCmd(str_to_bytes(self._name), cmd_n) # call by reverence for CMD_n return cmd_n != -1
def singular_function(name): """ Construct a new libSingular function object for the given ``name``.
This function works both for interpreter and built-in functions.
INPUT:
- ``name`` -- the name of the function
EXAMPLES::
sage: P.<x,y,z> = PolynomialRing(QQ) sage: f = 3*x*y + 2*z + 1 sage: g = 2*x + 1/2 sage: I = Ideal([f,g])
::
sage: from sage.libs.singular.function import singular_function sage: std = singular_function("std") sage: std(I) [3*y - 8*z - 4, 4*x + 1] sage: size = singular_function("size") sage: size([2, 3, 3]) 3 sage: size("sage") 4 sage: size(["hello", "sage"]) 2 sage: factorize = singular_function("factorize") sage: factorize(f) [[1, 3*x*y + 2*z + 1], (1, 1)] sage: factorize(f, 1) [3*x*y + 2*z + 1]
We give a wrong number of arguments::
sage: factorize() Traceback (most recent call last): ... RuntimeError: error in Singular function call 'factorize': Wrong number of arguments (got 0 arguments, arity code is 303) sage: factorize(f, 1, 2) Traceback (most recent call last): ... RuntimeError: error in Singular function call 'factorize': Wrong number of arguments (got 3 arguments, arity code is 303) sage: factorize(f, 1, 2, 3) Traceback (most recent call last): ... RuntimeError: error in Singular function call 'factorize': Wrong number of arguments (got 4 arguments, arity code is 303)
The Singular function ``list`` can be called with any number of arguments::
sage: singular_list = singular_function("list") sage: singular_list(2, 3, 6) [2, 3, 6] sage: singular_list() [] sage: singular_list(1) [1] sage: singular_list(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
We try to define a non-existing function::
sage: number_foobar = singular_function('number_foobar') Traceback (most recent call last): ... NameError: Singular library function 'number_foobar' is not defined
::
sage: from sage.libs.singular.function import lib as singular_lib sage: singular_lib('general.lib') sage: number_e = singular_function('number_e') sage: number_e(10r) 67957045707/25000000000 sage: RR(number_e(10r)) 2.71828182828000
::
sage: singular_lib('primdec.lib') sage: primdecGTZ = singular_function("primdecGTZ") sage: primdecGTZ(I) [[[y - 8/3*z - 4/3, x + 1/4], [y - 8/3*z - 4/3, x + 1/4]]] sage: singular_list((1,2,3),3,[1,2,3], ring=P) [(1, 2, 3), 3, [1, 2, 3]] sage: ringlist=singular_function("ringlist") sage: l = ringlist(P) sage: l[3].__class__ <class 'sage.rings.polynomial.multi_polynomial_sequence.PolynomialSequence_generic'> sage: l [0, ['x', 'y', 'z'], [['dp', (1, 1, 1)], ['C', (0,)]], [0]] sage: ring=singular_function("ring") sage: ring(l) <RingWrap> sage: matrix = Matrix(P,2,2) sage: matrix.randomize(terms=1) sage: det = singular_function("det") sage: det(matrix) -3/5*x*y*z sage: coeffs = singular_function("coeffs") sage: coeffs(x*y+y+1,y) [ 1] [x + 1] sage: intmat = Matrix(ZZ, 2,2, [100,2,3,4]) sage: det(intmat) 394 sage: random = singular_function("random") sage: A = random(10,2,3); A.nrows(), max(A.list()) <= 10 (2, True) sage: P.<x,y,z> = PolynomialRing(QQ) sage: M=P**3 sage: leadcoef = singular_function("leadcoef") sage: v=M((100*x,5*y,10*z*x*y)) sage: leadcoef(v) 10 sage: v = M([x+y,x*y+y**3,z]) sage: lead = singular_function("lead") sage: lead(v) (0, y^3) sage: jet = singular_function("jet") sage: jet(v, 2) (x + y, x*y, z) sage: syz = singular_function("syz") sage: I = P.ideal([x+y,x*y-y, y*2,x**2+1]) sage: M = syz(I) sage: M [(-2*y, 2, y + 1, 0), (0, -2, x - 1, 0), (x*y - y, -y + 1, 1, -y), (x^2 + 1, -x - 1, -1, -x)] sage: singular_lib("mprimdec.lib") sage: syz(M) [(-x - 1, y - 1, 2*x, -2*y)] sage: GTZmod = singular_function("GTZmod") sage: GTZmod(M) [[[(-2*y, 2, y + 1, 0), (0, x + 1, 1, -y), (0, -2, x - 1, 0), (x*y - y, -y + 1, 1, -y), (x^2 + 1, 0, 0, -x - y)], [0]]] sage: mres = singular_function("mres") sage: resolution = mres(M, 0) sage: resolution <Resolution> sage: singular_list(resolution) [[(-2*y, 2, y + 1, 0), (0, -2, x - 1, 0), (x*y - y, -y + 1, 1, -y), (x^2 + 1, -x - 1, -1, -x)], [(-x - 1, y - 1, 2*x, -2*y)], [(0)]]
sage: A.<x,y> = FreeAlgebra(QQ, 2) sage: P.<x,y> = A.g_algebra({y*x:-x*y}) sage: I= Sequence([x*y,x+y], check=False, immutable=True) sage: twostd = singular_function("twostd") sage: twostd(I) [x + y, y^2] sage: M=syz(I) doctest... sage: M [(x + y, x*y)] sage: syz(M) [(0)] sage: mres(I, 0) <Resolution> sage: M=P**3 sage: v=M((100*x,5*y,10*y*x*y)) sage: leadcoef(v) -10 sage: v = M([x+y,x*y+y**3,x]) sage: lead(v) (0, y^3) sage: jet(v, 2) (x + y, x*y, x) sage: l = ringlist(P) sage: len(l) 6 sage: ring(l) <noncommutative RingWrap> sage: I=twostd(I) sage: l[3]=I sage: ring(l) <noncommutative RingWrap> """
def lib(name): """ Load the Singular library ``name``.
INPUT:
- ``name`` -- a Singular library name
EXAMPLES::
sage: from sage.libs.singular.function import singular_function sage: from sage.libs.singular.function import lib as singular_lib sage: singular_lib('general.lib') sage: primes = singular_function('primes') sage: primes(2,10, ring=GF(127)['x,y,z']) (2, 3, 5, 7) """ global si_opt_2
raise NameError("Singular library {!r} not found".format(name))
def list_of_functions(packages=False): """ Return a list of all function names currently available.
INPUT:
- ``packages`` -- include local functions in packages.
EXAMPLES::
sage: from sage.libs.singular.function import list_of_functions sage: 'groebner' in list_of_functions() True """ ph = IDPACKAGE(h).idroot while ph != NULL: if PROC_CMD == IDTYP(ph): l.append(char_to_str(ph.id)) ph = IDNEXT(ph)
cdef inline RingWrap new_RingWrap(ring* r):
# Add support for _instancedoc_ from sage.docs.instancedoc import instancedoc instancedoc(SingularFunction) instancedoc(SingularLibraryFunction) instancedoc(SingularKernelFunction) |