Skip to content

minor details in boolean_polynomials #39605

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

Merged
merged 1 commit into from
Mar 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 23 additions & 33 deletions src/sage/sat/boolean_polynomials.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@
Functions
^^^^^^^^^
"""
##############################################################################
# ###########################################################################
# Copyright (C) 2012 Martin Albrecht <[email protected]>
# Distributed under the terms of the GNU General Public License (GPL)
# The full text of the GPL is available at:
# http://www.gnu.org/licenses/
##############################################################################

from sage.sat.solvers import SatSolver
from sage.sat.converters import ANF2CNFConverter

# https://www.gnu.org/licenses/
# ###########################################################################
from sage.rings.polynomial.multi_polynomial_sequence import PolynomialSequence
from sage.sat.converters import ANF2CNFConverter
from sage.sat.solvers import SatSolver


def solve(F, converter=None, solver=None, n=1, target_variables=None, **kwds):
Expand Down Expand Up @@ -220,9 +218,9 @@

.. NOTE::

Although supported, passing converter and solver objects
instead of classes is discouraged because these objects are
stateful.
Although supported, passing converter and solver objects
instead of classes is discouraged because these objects are
stateful.
"""
assert n > 0

Expand All @@ -247,10 +245,8 @@
from sage.sat.solvers import CryptoMiniSat as solver

if not isinstance(solver, SatSolver):
solver_kwds = {}
for k, v in kwds.items():
if k.startswith("s_"):
solver_kwds[k[2:]] = v
solver_kwds = {k[2:]: v for k, v in kwds.items()

Check warning on line 248 in src/sage/sat/boolean_polynomials.py

View check run for this annotation

Codecov / codecov/patch

src/sage/sat/boolean_polynomials.py#L248

Added line #L248 was not covered by tests
if k.startswith("s_")}

solver = solver(**solver_kwds)

Expand All @@ -260,23 +256,21 @@
from sage.sat.converters.polybori import CNFEncoder as converter

if not isinstance(converter, ANF2CNFConverter):
converter_kwds = {}
for k, v in kwds.items():
if k.startswith("c_"):
converter_kwds[k[2:]] = v
converter_kwds = {k[2:]: v for k, v in kwds.items()

Check warning on line 259 in src/sage/sat/boolean_polynomials.py

View check run for this annotation

Codecov / codecov/patch

src/sage/sat/boolean_polynomials.py#L259

Added line #L259 was not covered by tests
if k.startswith("c_")}

converter = converter(solver, P, **converter_kwds)

phi = converter(F)
rho = dict((phi[i], i) for i in range(len(phi)))
rho = {phi[i]: i for i in range(len(phi))}

Check warning on line 265 in src/sage/sat/boolean_polynomials.py

View check run for this annotation

Codecov / codecov/patch

src/sage/sat/boolean_polynomials.py#L265

Added line #L265 was not covered by tests

S = []

while True:
s = solver()

if s:
S.append(dict((x, K(s[rho[x]])) for x in target_variables))
S.append({x: K(s[rho[x]]) for x in target_variables})

Check warning on line 273 in src/sage/sat/boolean_polynomials.py

View check run for this annotation

Codecov / codecov/patch

src/sage/sat/boolean_polynomials.py#L273

Added line #L273 was not covered by tests

if n is not None and len(S) == n:
break
Expand All @@ -288,7 +282,7 @@
try:
learnt = solver.learnt_clauses(unitary_only=True)
if learnt:
S.append(dict((phi[abs(i) - 1], K(i < 0)) for i in learnt))
S.append({phi[abs(i) - 1]: K(i < 0) for i in learnt})

Check warning on line 285 in src/sage/sat/boolean_polynomials.py

View check run for this annotation

Codecov / codecov/patch

src/sage/sat/boolean_polynomials.py#L285

Added line #L285 was not covered by tests
else:
S.append(s)
break
Expand Down Expand Up @@ -331,9 +325,9 @@

.. NOTE::

More parameters can be passed to the converter and the solver by prefixing them with ``c_`` and
``s_`` respectively. For example, to increase CryptoMiniSat's verbosity level, pass
``s_verbosity=1``.
More parameters can be passed to the converter and the solver by prefixing them with ``c_`` and
``s_`` respectively. For example, to increase CryptoMiniSat's verbosity level, pass
``s_verbosity=1``.

OUTPUT: a sequence of Boolean polynomials

Expand Down Expand Up @@ -364,10 +358,8 @@
if solver is None:
from sage.sat.solvers.cryptominisat import CryptoMiniSat as solver

solver_kwds = {}
for k, v in kwds.items():
if k.startswith("s_"):
solver_kwds[k[2:]] = v
solver_kwds = {k[2:]: v for k, v in kwds.items()

Check warning on line 361 in src/sage/sat/boolean_polynomials.py

View check run for this annotation

Codecov / codecov/patch

src/sage/sat/boolean_polynomials.py#L361

Added line #L361 was not covered by tests
if k.startswith("s_")}

solver = solver(**solver_kwds)

Expand All @@ -376,15 +368,13 @@
if converter is None:
from sage.sat.converters.polybori import CNFEncoder as converter

converter_kwds = {}
for k, v in kwds.items():
if k.startswith("c_"):
converter_kwds[k[2:]] = v
converter_kwds = {k[2:]: v for k, v in kwds.items()

Check warning on line 371 in src/sage/sat/boolean_polynomials.py

View check run for this annotation

Codecov / codecov/patch

src/sage/sat/boolean_polynomials.py#L371

Added line #L371 was not covered by tests
if k.startswith("c_")}

converter = converter(solver, P, **converter_kwds)

phi = converter(F)
rho = dict((phi[i], i) for i in range(len(phi)))
rho = {phi[i]: i for i in range(len(phi))}

Check warning on line 377 in src/sage/sat/boolean_polynomials.py

View check run for this annotation

Codecov / codecov/patch

src/sage/sat/boolean_polynomials.py#L377

Added line #L377 was not covered by tests

s = solver()

Expand Down
Loading