Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

option handling, fix #808 #810

Merged
merged 12 commits into from
Jun 29, 2024
39 changes: 19 additions & 20 deletions deps/src/ideals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ auto id_Slimgb_helper(ideal a, ring b, bool complete_reduction = false)
// bool complete_reduction= false;
unsigned int crbit;
if (complete_reduction)
auto crbit = Sy_bit(OPT_REDSB);
crbit = Sy_bit(OPT_REDSB);
else
crbit = 0;
ideal id = NULL;
Expand All @@ -144,11 +144,11 @@ auto id_Slimgb_helper(ideal a, ring b, bool complete_reduction = false)
tHomog h = testHomog;
const ring origin = currRing;
unsigned int save_opt = si_opt_1;
si_opt_1 |= crbit;
rChangeCurrRing(b);
si_opt_1 |= crbit;
id = t_rep_gb(b, a, a->rank);
si_opt_1 = save_opt;
rChangeCurrRing(origin);
si_opt_1 = save_opt;
if (n != NULL)
delete n;
}
Expand Down Expand Up @@ -187,11 +187,11 @@ auto id_Std_helper(ideal a, ring b, bool complete_reduction = false)
tHomog h = testHomog;
const ring origin = currRing;
unsigned int save_opt = si_opt_1;
si_opt_1 |= crbit;
rChangeCurrRing(b);
si_opt_1 |= crbit;
id = kStd(a, b->qideal, h, &n);
si_opt_1 = save_opt;
rChangeCurrRing(origin);
si_opt_1 = save_opt;
if (n != NULL)
delete n;
}
Expand Down Expand Up @@ -238,11 +238,11 @@ auto id_MinStd_helper(ideal a, ring b, bool complete_reduction = false)
tHomog h = testHomog;
const ring origin = currRing;
unsigned int save_opt = si_opt_1;
si_opt_1 |= crbit;
rChangeCurrRing(b);
si_opt_1 |= crbit;
id = kMin_std(a, b->qideal, h, NULL, m);
si_opt_1 = save_opt;
rChangeCurrRing(origin);
si_opt_1 = save_opt;
return std::make_tuple(id, m);
}

Expand Down Expand Up @@ -275,11 +275,11 @@ auto id_StdHilb_helper(ideal a,
tHomog h = testHomog;
const ring origin = currRing;
unsigned int save_opt = si_opt_1;
si_opt_1 |= crbit;
rChangeCurrRing(b);
si_opt_1 |= crbit;
id = kStd(a, b->qideal, h, &n, hilb);
si_opt_1 = save_opt;
rChangeCurrRing(origin);
si_opt_1 = save_opt;
if (n != NULL)
delete n;
}
Expand Down Expand Up @@ -310,15 +310,15 @@ auto id_StdHilbWeighted_helper(ideal a,
tHomog h = testHomog;
const ring origin = currRing;
unsigned int save_opt = si_opt_1;
si_opt_1 |= crbit;
rChangeCurrRing(b);
si_opt_1 |= crbit;
id = kStd(a, currRing->qideal, h,
&n, // module weights
hilb, // hilbert series
0, 0, // syzComp, newIdeal
varweights); // weights of vars
si_opt_1 = save_opt;
rChangeCurrRing(origin);
si_opt_1 = save_opt;
if (n != NULL)
delete n;
}
Expand Down Expand Up @@ -528,37 +528,36 @@ void singular_define_ideals(jlcxx::Module & Singular)

Singular.method("id_LiftStd", [](ideal m, ring o, bool complete_reduction = false) {
const ring origin = currRing;
rChangeCurrRing(o);
matrix ma = mpNew(1, 1);
unsigned int crbit;
if (complete_reduction)
crbit = Sy_bit(OPT_REDSB);
else
crbit = 0;
unsigned int save_opt = si_opt_1;
rChangeCurrRing(o);
matrix ma = mpNew(1, 1);
si_opt_1 |= crbit;
ideal res = idLiftStd(m, &ma, testHomog, NULL);
si_opt_1 = save_opt;
rChangeCurrRing(origin);
si_opt_1 = save_opt;
return std::make_tuple(res, ma);
});

Singular.method("id_LiftStdSyz", [](ideal m, ring o, bool complete_reduction = false) {
const ring origin = currRing;
rChangeCurrRing(o);
matrix ma = mpNew(1, 1);
ideal syz = idInit(1, 1);
unsigned int crbit;
if (complete_reduction)
crbit = Sy_bit(OPT_REDSB);
else
crbit = 0;
unsigned int save_opt = si_opt_1;
const ring origin = currRing;
rChangeCurrRing(o);
matrix ma = mpNew(1, 1);
ideal syz = idInit(1, 1);
si_opt_1 |= crbit;
ideal res = idLiftStd(m, &ma, testHomog, &syz);
si_opt_1 = save_opt;

rChangeCurrRing(origin);
si_opt_1 = save_opt;
return std::make_tuple(res, ma, syz);
});

Expand Down
145 changes: 78 additions & 67 deletions deps/src/singular.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,82 @@ static void WerrorS_and_reset(const char * s)
singular_error_log.emplace_back(s);
}

#define SETTER(A, B) \
if (opt == #B) \
{ \
old_value = (A & Sy_bit(B)) != 0; \
A = value ? (A | Sy_bit(B)) : (A & ~Sy_bit(B)); \
} else

// all of the global setters return the previous value
auto set_option_helper(std::string opt, bool value, ring r) {
bool old_value = false;
ring oldring=currRing;
if (r!=NULL) rChangeCurrRing(r);
SETTER(si_opt_2, V_QUIET)
SETTER(si_opt_2, V_QRING)
SETTER(si_opt_2, V_SHOW_MEM)
SETTER(si_opt_2, V_YACC)
SETTER(si_opt_2, V_REDEFINE)
SETTER(si_opt_2, V_LOAD_LIB)
SETTER(si_opt_2, V_DEBUG_LIB)
SETTER(si_opt_2, V_LOAD_PROC)
SETTER(si_opt_2, V_DEF_RES)
SETTER(si_opt_2, V_SHOW_USE)
SETTER(si_opt_2, V_IMAP)
SETTER(si_opt_2, V_PROMPT)
SETTER(si_opt_2, V_NSB)
SETTER(si_opt_2, V_CONTENTSB)
SETTER(si_opt_2, V_CANCELUNIT)
SETTER(si_opt_2, V_MODPSOLVSB)
SETTER(si_opt_2, V_UPTORADICAL)
SETTER(si_opt_2, V_FINDMONOM)
SETTER(si_opt_2, V_COEFSTRAT)
SETTER(si_opt_2, V_IDLIFT)
SETTER(si_opt_2, V_LENGTH)
SETTER(si_opt_2, V_ALLWARN)
SETTER(si_opt_2, V_INTERSECT_ELIM)
SETTER(si_opt_2, V_INTERSECT_SYZ)
SETTER(si_opt_2, V_DEG_STOP)

SETTER(si_opt_1, OPT_PROT)
SETTER(si_opt_1, OPT_REDSB)
SETTER(si_opt_1, OPT_NOT_BUCKETS)
SETTER(si_opt_1, OPT_NOT_SUGAR)
SETTER(si_opt_1, OPT_INTERRUPT)
SETTER(si_opt_1, OPT_SUGARCRIT)
SETTER(si_opt_1, OPT_DEBUG)
SETTER(si_opt_1, OPT_REDTHROUGH)
SETTER(si_opt_1, OPT_NO_SYZ_MINIM)
SETTER(si_opt_1, OPT_RETURN_SB)
SETTER(si_opt_1, OPT_FASTHC)
SETTER(si_opt_1, OPT_OLDSTD)
SETTER(si_opt_1, OPT_STAIRCASEBOUND)
SETTER(si_opt_1, OPT_MULTBOUND)
SETTER(si_opt_1, OPT_DEGBOUND)
SETTER(si_opt_1, OPT_REDTAIL)
SETTER(si_opt_1, OPT_INTSTRATEGY)
SETTER(si_opt_1, OPT_FINDET)
SETTER(si_opt_1, OPT_INFREDTAIL)
SETTER(si_opt_1, OPT_SB_1)
SETTER(si_opt_1, OPT_NOTREGULARITY)
SETTER(si_opt_1, OPT_WEIGHTM)
// else <--- not needed, already "provided" by SETTER
{
std::cerr << "unknown option " << opt << std::endl;
}
if (r!=NULL)
{
r->options=si_opt_1;
rChangeCurrRing(oldring);
}
return old_value;
}
auto set_option_helper2(std::string opt, bool value) {
return set_option_helper(opt,value,NULL);
}
#undef SETTER

JLCXX_MODULE define_julia_module(jlcxx::Module & Singular)
{
Singular.add_type<n_Procs_s>("coeffs");
Expand Down Expand Up @@ -140,74 +216,9 @@ JLCXX_MODULE define_julia_module(jlcxx::Module & Singular)
return ss.str();
});

#define SETTER(A, B) \
else if (opt == #B) \
{ \
old_value = (A & Sy_bit(B)) != 0; \
A = value ? (A | Sy_bit(B)) : (A & ~Sy_bit(B)); \
}
Singular.method("set_option", &set_option_helper2);
Singular.method("set_option", &set_option_helper);

// all of the global setters return the previous value
Singular.method("set_option", [](std::string opt, bool value) {
bool old_value = false;
if (false)
;
SETTER(si_opt_2, V_QUIET)
SETTER(si_opt_2, V_QRING)
SETTER(si_opt_2, V_SHOW_MEM)
SETTER(si_opt_2, V_YACC)
SETTER(si_opt_2, V_REDEFINE)
SETTER(si_opt_2, V_LOAD_LIB)
SETTER(si_opt_2, V_DEBUG_LIB)
SETTER(si_opt_2, V_LOAD_PROC)
SETTER(si_opt_2, V_DEF_RES)
SETTER(si_opt_2, V_SHOW_USE)
SETTER(si_opt_2, V_IMAP)
SETTER(si_opt_2, V_PROMPT)
SETTER(si_opt_2, V_NSB)
SETTER(si_opt_2, V_CONTENTSB)
SETTER(si_opt_2, V_CANCELUNIT)
SETTER(si_opt_2, V_MODPSOLVSB)
SETTER(si_opt_2, V_UPTORADICAL)
SETTER(si_opt_2, V_FINDMONOM)
SETTER(si_opt_2, V_COEFSTRAT)
SETTER(si_opt_2, V_IDLIFT)
SETTER(si_opt_2, V_LENGTH)
SETTER(si_opt_2, V_ALLWARN)
SETTER(si_opt_2, V_INTERSECT_ELIM)
SETTER(si_opt_2, V_INTERSECT_SYZ)
SETTER(si_opt_2, V_DEG_STOP)

SETTER(si_opt_1, OPT_PROT)
SETTER(si_opt_1, OPT_REDSB)
SETTER(si_opt_1, OPT_NOT_BUCKETS)
SETTER(si_opt_1, OPT_NOT_SUGAR)
SETTER(si_opt_1, OPT_INTERRUPT)
SETTER(si_opt_1, OPT_SUGARCRIT)
SETTER(si_opt_1, OPT_DEBUG)
SETTER(si_opt_1, OPT_REDTHROUGH)
SETTER(si_opt_1, OPT_NO_SYZ_MINIM)
SETTER(si_opt_1, OPT_RETURN_SB)
SETTER(si_opt_1, OPT_FASTHC)
SETTER(si_opt_1, OPT_OLDSTD)
SETTER(si_opt_1, OPT_STAIRCASEBOUND)
SETTER(si_opt_1, OPT_MULTBOUND)
SETTER(si_opt_1, OPT_DEGBOUND)
SETTER(si_opt_1, OPT_REDTAIL)
SETTER(si_opt_1, OPT_INTSTRATEGY)
SETTER(si_opt_1, OPT_FINDET)
SETTER(si_opt_1, OPT_INFREDTAIL)
SETTER(si_opt_1, OPT_SB_1)
SETTER(si_opt_1, OPT_NOTREGULARITY)
SETTER(si_opt_1, OPT_WEIGHTM)
else
{
std::cerr << "unknown option " << opt << std::endl;
}
return old_value;
});

#undef SETTER

// the "printlevel" system variable in Singular
Singular.method("set_printlevel", [](int level) {
Expand Down
30 changes: 21 additions & 9 deletions src/MessyHacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ Singular and this is the starting value.
"""
function with_degBound(f, degb::Integer)
old_degb = libSingular.set_degBound(Cint(degb))
local g = nothing
try
g = f()
return f()
finally
libSingular.set_degBound(old_degb)
end
return g
end

@doc raw"""
Expand All @@ -35,13 +33,11 @@ bound in Singular and this is the starting value.
"""
function with_multBound(f, mu::Integer)
old_mu = libSingular.set_multBound(Cint(mu))
local g = nothing
try
g = f()
return f()
finally
libSingular.set_multBound(old_mu)
end
return g
end

for (name, str) in [(:with_fastHC, "OPT_FASTHC")
Expand All @@ -56,19 +52,35 @@ for (name, str) in [(:with_fastHC, "OPT_FASTHC")
@eval begin
function ($name)(f, flag::Bool)
old_flag = libSingular.set_option($str, flag)
local g = nothing
try
g = f()
return f()
finally
libSingular.set_option($str, old_flag)
end
return g
end

export $name
end
end

function with_redTail(f, flag::Bool, R::PolyRingUnion)
old_flag = libSingular.set_option("OPT_REDTAIL", flag, R.ptr)
try
return f()
finally
libSingular.set_option("OPT_REDTAIL", old_flag, R.ptr)
end
end

function with_redThrough(f, flag::Bool, R::PolyRingUnion)
old_flag = libSingular.set_option("OPT_REDTHROUGH", flag, R.ptr)
try
return f()
finally
libSingular.set_option("OPT_REDTHROUGH", old_flag, R.ptr)
end
end

#=
messy hack #1:

Expand Down
8 changes: 4 additions & 4 deletions src/ideal/ideal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -847,12 +847,12 @@ from the identity matrix only for local ring orderings.
function divrem(I::sideal{S}, G::sideal{S}; complete_reduction::Bool = false) where S <: SPolyUnion
check_parent(I, G)
R = base_ring(I)
old_redsb=libSingular.set_option("OPT_REDSB",complete_reduction)
old_redtail=libSingular.set_option("OPT_REDTAIL",complete_reduction)
old_redsb=libSingular.set_option("OPT_REDSB",complete_reduction,R.ptr)
old_redtail=libSingular.set_option("OPT_REDTAIL",complete_reduction,R.ptr)
ptr_T,ptr_Rest,ptr_U = GC.@preserve I G R libSingular.id_Lift(G.ptr, I.ptr, true,
false, true, R.ptr)
libSingular.set_option("OPT_REDSB",old_redsb)
libSingular.set_option("OPT_REDTAIL",old_redtail)
libSingular.set_option("OPT_REDSB",old_redsb,R.ptr)
libSingular.set_option("OPT_REDTAIL",old_redtail,R.ptr)
return (smodule{S}(R,ptr_T), sideal{S}(R,ptr_Rest), smodule{S}(R,ptr_U))
end

Expand Down
8 changes: 4 additions & 4 deletions src/module/module.jl
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,12 @@ from the identity matrix only for local ring orderings.
function divrem(I::smodule{S}, G::smodule{S}; complete_reduction::Bool = false) where S <: SPolyUnion
check_parent(I, G)
R = base_ring(I)
old_redsb=libSingular.set_option("OPT_REDSB",complete_reduction)
old_redtail=libSingular.set_option("OPT_REDTAIL",complete_reduction)
old_redsb=libSingular.set_option("OPT_REDSB",complete_reduction,R.ptr)
old_redtail=libSingular.set_option("OPT_REDTAIL",complete_reduction,R.ptr)
ptr_T,ptr_Rest,ptr_U = GC.@preserve I G R libSingular.id_Lift(G.ptr, I.ptr, true,
false, true, R.ptr)
libSingular.set_option("OPT_REDSB",old_redsb)
libSingular.set_option("OPT_REDTAIL",old_redtail)
libSingular.set_option("OPT_REDSB",old_redsb,R.ptr)
libSingular.set_option("OPT_REDTAIL",old_redtail,R.ptr)
return (smodule{S}(R,ptr_T), smodule{S}(R,ptr_Rest), smodule{S}(R,ptr_U))
end

Expand Down
1 change: 1 addition & 0 deletions test/ideal-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ include("ideal/sideal-test.jl")

include("ideal/quotient-test.jl")

include("ideal/freealg-test.jl")
Loading
Loading