From 83474a80f51d245a9d1c667e513304353e5341ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Sat, 13 Jul 2024 15:06:41 +0200 Subject: [PATCH 1/2] add access to printlevel in libsingular --- src/sage/libs/singular/decl.pxd | 1 + src/sage/libs/singular/function.pyx | 22 ++++++++++++++ .../function_field/function_field_polymod.py | 13 +++----- .../multi_polynomial_libsingular.pyx | 13 ++++---- src/sage/schemes/curves/projective_curve.py | 30 +++++++++++++------ 5 files changed, 53 insertions(+), 26 deletions(-) diff --git a/src/sage/libs/singular/decl.pxd b/src/sage/libs/singular/decl.pxd index 320044a9372..0c9f2cc993c 100644 --- a/src/sage/libs/singular/decl.pxd +++ b/src/sage/libs/singular/decl.pxd @@ -425,6 +425,7 @@ cdef extern from "singular/Singular/libsingular.h": cdef int si_opt_2 # previously 'verbose' cdef void * currentVoice cdef int myynest + cdef int printlevel ctypedef char * const_char_ptr "const char *" cdef extern void (*WerrorS_callback)(const_char_ptr) diff --git a/src/sage/libs/singular/function.pyx b/src/sage/libs/singular/function.pyx index a3b13cac604..f1e1572d44a 100644 --- a/src/sage/libs/singular/function.pyx +++ b/src/sage/libs/singular/function.pyx @@ -1824,6 +1824,28 @@ def lib(name): raise NameError("Singular library {!r} not found".format(name)) +def get_printlevel(): + """ + Return the value of the variable ``printlevel``. + + This is useful to switch off and back the comments. + + EXAMPLES:: + + sage: from sage.libs.singular.function import singular_function + sage: from sage.libs.singular.function import get_printlevel + sage: l = get_printlevel() + sage: exec = singular_function('execute') + sage: exec("printlevel=-1") + sage: get_printlevel() + -1 + sage: exec(f"printlevel={l}") + """ + global printlevel + cdef int pl = printlevel + return pl + + def list_of_functions(packages=False): """ Return a list of all function names currently available. diff --git a/src/sage/rings/function_field/function_field_polymod.py b/src/sage/rings/function_field/function_field_polymod.py index fd8c975e682..25255d7b5a8 100644 --- a/src/sage/rings/function_field/function_field_polymod.py +++ b/src/sage/rings/function_field/function_field_polymod.py @@ -2390,22 +2390,17 @@ def _singular_normal(ideal): sage: _singular_normal(ideal(f)) [[1]] """ - from sage.libs.singular.function import singular_function, lib + from sage.libs.singular.function import (singular_function, lib, + get_printlevel) lib('normal.lib') normal = singular_function('normal') execute = singular_function('execute') - try: - get_printlevel = singular_function('get_printlevel') - except NameError: - execute('proc get_printlevel {return (printlevel);}') - get_printlevel = singular_function('get_printlevel') - - # It's fairly verbose unless printlevel is -1. + # verbose unless printlevel is -1. saved_printlevel = get_printlevel() execute('printlevel=-1') nor = normal(ideal) - execute('printlevel={}'.format(saved_printlevel)) + execute(f'printlevel={saved_printlevel}') return nor[1] diff --git a/src/sage/rings/polynomial/multi_polynomial_libsingular.pyx b/src/sage/rings/polynomial/multi_polynomial_libsingular.pyx index 6b838190c52..766f9eb88f8 100644 --- a/src/sage/rings/polynomial/multi_polynomial_libsingular.pyx +++ b/src/sage/rings/polynomial/multi_polynomial_libsingular.pyx @@ -5857,20 +5857,17 @@ cdef class MPolynomial_libsingular(MPolynomial_libsingular_base): """ R = self.parent() algorithm = algorithm.lower() - from sage.libs.singular.function import singular_function, lib as singular_lib + from sage.libs.singular.function import (singular_function, + get_printlevel, + lib as singular_lib) singular_lib('algebra.lib') if algorithm == "algebra_containment": execute = singular_function('execute') - try: - get_printlevel = singular_function('get_printlevel') - except NameError: - execute('proc get_printlevel {return (printlevel);}') - get_printlevel = singular_function('get_printlevel') - # It's fairly verbose unless printlevel is -1. + # verbose unless printlevel is -1. saved_printlevel = get_printlevel() execute('printlevel=-1') contains = singular_function('algebra_containment')(self, R.ideal(J)) == 1 - execute('printlevel={}'.format(saved_printlevel)) + execute(f'printlevel={saved_printlevel}') return contains elif algorithm == "insubring": return singular_function('inSubring')(self, R.ideal(J))[0] == 1 diff --git a/src/sage/schemes/curves/projective_curve.py b/src/sage/schemes/curves/projective_curve.py index 6045cc3ea70..b3584e6caf7 100644 --- a/src/sage/schemes/curves/projective_curve.py +++ b/src/sage/schemes/curves/projective_curve.py @@ -144,6 +144,7 @@ from sage.categories.number_fields import NumberFields from sage.interfaces.singular import singular +from sage.libs.singular.function import singular_function, lib as singular_lib, get_printlevel from sage.matrix.constructor import matrix from builtins import sum as add from sage.misc.sage_eval import sage_eval @@ -1663,10 +1664,19 @@ def is_complete_intersection(self): sage: C.is_complete_intersection() False """ - singular.lib("sing.lib") - id = singular.simplify(self.defining_ideal(), 10) - L = singular.is_ci(id).sage() - return len(self.ambient_space().gens()) - len(id.sage().gens()) == L[-1] + singular_lib("sing.lib") + simplify = singular_function("simplify") + is_ci = singular_function("is_ci") + execute = singular_function('execute') + + # verbose unless printlevel is -1. + saved_printlevel = get_printlevel() + execute('printlevel=-1') + id = simplify(self.defining_ideal(), 10) + L = is_ci(id)[-1] + execute(f'printlevel={saved_printlevel}') + + return len(self.ambient_space().gens()) - len(id) == L def tangent_line(self, p): """ @@ -1864,11 +1874,13 @@ def rational_parameterization(self): raise TypeError("this curve must have geometric genus zero") if not isinstance(self.base_ring(), RationalField): raise TypeError("this curve must be defined over the rational field") + singular.lib("paraplanecurves.lib") - R = singular.paraPlaneCurve(self.defining_polynomial()) - singular.setring(R) - param = singular('PARA').sage().gens() + R = singular.paraPlaneCurve(self.defining_polynomial()) # ring + R.set_ring() + param = singular('PARA').sage().gens() # ideal R = R.sage() + C = self.change_ring(R.base_ring()) H = Hom(ProjectiveSpace(R.base_ring(), 1, R.gens()), C) return H(param) @@ -2053,7 +2065,7 @@ def _points_via_singular(self, sort=True): X2 = singular.NSplaces(1, X1) R = X2[5][1][1] - singular.set_ring(R) + R.set_ring() # We use sage_flattened_str_list since iterating through # the entire list through the sage/singular interface directly @@ -2137,7 +2149,7 @@ def riemann_roch_basis(self, D): pnts = [(int(v[i][0]), int(v[i][2])-1) for i in range(len(v))] # retrieve coordinates of rational points R = X2[5][1][1] - singular.set_ring(R) + R.set_ring() v = singular('POINTS').sage_flattened_str_list() coords = [self(int(v[3*i]), int(v[3*i+1]), int(v[3*i+2])) for i in range(len(v)//3)] # build correct representation of D for singular From 6d4273a6dee5b784dbf297a02a7f8e0adb8bd5b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Tue, 16 Jul 2024 10:23:25 +0200 Subject: [PATCH 2/2] new method set_printlevel --- src/sage/libs/singular/function.pyx | 27 +++++++++++++++---- .../function_field/function_field_polymod.py | 12 ++++----- .../multi_polynomial_libsingular.pyx | 6 ++--- src/sage/schemes/curves/projective_curve.py | 7 +++-- 4 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/sage/libs/singular/function.pyx b/src/sage/libs/singular/function.pyx index f1e1572d44a..4481e9b29b9 100644 --- a/src/sage/libs/singular/function.pyx +++ b/src/sage/libs/singular/function.pyx @@ -1832,20 +1832,37 @@ def get_printlevel(): EXAMPLES:: - sage: from sage.libs.singular.function import singular_function - sage: from sage.libs.singular.function import get_printlevel + sage: from sage.libs.singular.function import get_printlevel, set_printlevel sage: l = get_printlevel() - sage: exec = singular_function('execute') - sage: exec("printlevel=-1") + sage: set_printlevel(-1) sage: get_printlevel() -1 - sage: exec(f"printlevel={l}") + sage: set_printlevel(l) """ global printlevel cdef int pl = printlevel return pl +def set_printlevel(l): + """ + Set the value of the variable ``printlevel``. + + This is useful to switch off and back the comments. + + EXAMPLES:: + + sage: from sage.libs.singular.function import get_printlevel, set_printlevel + sage: l = get_printlevel() + sage: set_printlevel(2) + sage: get_printlevel() + 2 + sage: set_printlevel(l) + """ + global printlevel + printlevel = l + + def list_of_functions(packages=False): """ Return a list of all function names currently available. diff --git a/src/sage/rings/function_field/function_field_polymod.py b/src/sage/rings/function_field/function_field_polymod.py index 25255d7b5a8..40ccc0345d8 100644 --- a/src/sage/rings/function_field/function_field_polymod.py +++ b/src/sage/rings/function_field/function_field_polymod.py @@ -2390,17 +2390,17 @@ def _singular_normal(ideal): sage: _singular_normal(ideal(f)) [[1]] """ - from sage.libs.singular.function import (singular_function, lib, - get_printlevel) - lib('normal.lib') + from sage.libs.singular.function import (singular_function, + lib as singular_lib, + get_printlevel, set_printlevel) + singular_lib('normal.lib') normal = singular_function('normal') - execute = singular_function('execute') # verbose unless printlevel is -1. saved_printlevel = get_printlevel() - execute('printlevel=-1') + set_printlevel(-1) nor = normal(ideal) - execute(f'printlevel={saved_printlevel}') + set_printlevel(saved_printlevel) return nor[1] diff --git a/src/sage/rings/polynomial/multi_polynomial_libsingular.pyx b/src/sage/rings/polynomial/multi_polynomial_libsingular.pyx index 766f9eb88f8..9febaa8c592 100644 --- a/src/sage/rings/polynomial/multi_polynomial_libsingular.pyx +++ b/src/sage/rings/polynomial/multi_polynomial_libsingular.pyx @@ -5859,15 +5859,15 @@ cdef class MPolynomial_libsingular(MPolynomial_libsingular_base): algorithm = algorithm.lower() from sage.libs.singular.function import (singular_function, get_printlevel, + set_printlevel, lib as singular_lib) singular_lib('algebra.lib') if algorithm == "algebra_containment": - execute = singular_function('execute') # verbose unless printlevel is -1. saved_printlevel = get_printlevel() - execute('printlevel=-1') + set_printlevel(-1) contains = singular_function('algebra_containment')(self, R.ideal(J)) == 1 - execute(f'printlevel={saved_printlevel}') + set_printlevel(saved_printlevel) return contains elif algorithm == "insubring": return singular_function('inSubring')(self, R.ideal(J))[0] == 1 diff --git a/src/sage/schemes/curves/projective_curve.py b/src/sage/schemes/curves/projective_curve.py index b3584e6caf7..ff78916830a 100644 --- a/src/sage/schemes/curves/projective_curve.py +++ b/src/sage/schemes/curves/projective_curve.py @@ -144,7 +144,7 @@ from sage.categories.number_fields import NumberFields from sage.interfaces.singular import singular -from sage.libs.singular.function import singular_function, lib as singular_lib, get_printlevel +from sage.libs.singular.function import singular_function, lib as singular_lib, get_printlevel, set_printlevel from sage.matrix.constructor import matrix from builtins import sum as add from sage.misc.sage_eval import sage_eval @@ -1667,14 +1667,13 @@ def is_complete_intersection(self): singular_lib("sing.lib") simplify = singular_function("simplify") is_ci = singular_function("is_ci") - execute = singular_function('execute') # verbose unless printlevel is -1. saved_printlevel = get_printlevel() - execute('printlevel=-1') + set_printlevel(-1) id = simplify(self.defining_ideal(), 10) L = is_ci(id)[-1] - execute(f'printlevel={saved_printlevel}') + set_printlevel(saved_printlevel) return len(self.ambient_space().gens()) - len(id) == L