Skip to content

Commit

Permalink
sagemathgh-36567: sage.numerical: Update # needs
Browse files Browse the repository at this point in the history
    
<!-- ^^^^^
Please provide a concise, informative and self-explanatory title.
Don't put issue numbers in there, do this in the PR body below.
For example, instead of "Fixes sagemath#1234" use "Introduce new method to
calculate 1+1"
-->
<!-- Describe your changes here in detail -->

Also removing a function deprecated in sagemath#32226

<!-- Why is this change required? What problem does it solve? -->
<!-- If this PR resolves an open issue, please link to it here. For
example "Fixes sagemath#12345". -->
- In part cherry picked from sagemath#35095
- Part of sagemath#29705
<!-- If your change requires a documentation PR, please link it
appropriately. -->

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->
<!-- If your change requires a documentation PR, please link it
appropriately -->
<!-- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
<!-- Feel free to remove irrelevant items. -->

- [x] The title is concise, informative, and self-explanatory.
- [ ] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation accordingly.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on
- sagemath#12345: short description why this is a dependency
- sagemath#34567: ...
-->

<!-- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
    
URL: sagemath#36567
Reported by: Matthias Köppe
Reviewer(s): David Coudert, Matthias Köppe
  • Loading branch information
Release Manager committed Oct 31, 2023
2 parents 8f4a406 + 1f0c2a2 commit 159cb85
Show file tree
Hide file tree
Showing 16 changed files with 999 additions and 1,000 deletions.
2 changes: 1 addition & 1 deletion src/sage/numerical/all.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from sage.misc.lazy_import import lazy_import
lazy_import("sage.numerical.optimize",
["find_fit", "find_local_maximum", "find_local_minimum",
"find_root", "linear_program", "minimize", "minimize_constrained"])
"find_root", "minimize", "minimize_constrained"])
lazy_import("sage.numerical.mip", ["MixedIntegerLinearProgram"])
lazy_import("sage.numerical.sdp", ["SemidefiniteProgram"])
lazy_import("sage.numerical.backends.generic_backend", ["default_mip_solver"])
Expand Down
406 changes: 208 additions & 198 deletions src/sage/numerical/backends/cvxopt_backend.pyx

Large diffs are not rendered by default.

137 changes: 69 additions & 68 deletions src/sage/numerical/backends/cvxopt_sdp_backend.pyx

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/sage/numerical/backends/cvxpy_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ cdef class CVXPYBackend:
Open-source solvers provided by optional packages::
sage: p = MixedIntegerLinearProgram(solver="CVXPY/GLPK"); p.solve() # optional - cvxopt
sage: p = MixedIntegerLinearProgram(solver="CVXPY/GLPK"); p.solve() # needs cvxopt
0.0
sage: p = MixedIntegerLinearProgram(solver="CVXPY/GLPK_MI"); p.solve() # optional - cvxopt
sage: p = MixedIntegerLinearProgram(solver="CVXPY/GLPK_MI"); p.solve() # needs cvxopt
0.0
sage: p = MixedIntegerLinearProgram(solver="CVXPY/CVXOPT"); p.solve() # optional - cvxopt
sage: p = MixedIntegerLinearProgram(solver="CVXPY/CVXOPT"); p.solve() # needs cvxopt
0.0
sage: p = MixedIntegerLinearProgram(solver="CVXPY/GLOP"); p.solve() # optional - ortools
0.0
Expand Down
628 changes: 337 additions & 291 deletions src/sage/numerical/backends/generic_backend.pyx

Large diffs are not rendered by default.

302 changes: 159 additions & 143 deletions src/sage/numerical/backends/generic_sdp_backend.pyx

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion src/sage/numerical/backends/glpk_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,7 @@ cdef class GLPKBackend(GenericBackend):
the result is not optimal. To do this, we try to compute the maximum
number of disjoint balls (of diameter 1) in a hypercube::
sage: # needs sage.graphs
sage: g = graphs.CubeGraph(9)
sage: p = MixedIntegerLinearProgram(solver="GLPK")
sage: p.solver_parameter("mip_gap_tolerance",100)
Expand All @@ -1110,6 +1111,7 @@ cdef class GLPKBackend(GenericBackend):
Same, now with a time limit::
sage: # needs sage.graphs
sage: p.solver_parameter("mip_gap_tolerance",1)
sage: p.solver_parameter("timelimit",3.0)
sage: p.solve() # rel tol 100
Expand Down Expand Up @@ -1197,6 +1199,7 @@ cdef class GLPKBackend(GenericBackend):
EXAMPLES::
sage: # needs sage.graphs
sage: g = graphs.CubeGraph(9)
sage: p = MixedIntegerLinearProgram(solver="GLPK")
sage: p.solver_parameter("mip_gap_tolerance",100)
Expand Down Expand Up @@ -1231,6 +1234,7 @@ cdef class GLPKBackend(GenericBackend):
EXAMPLES::
sage: # needs sage.graphs
sage: g = graphs.CubeGraph(9)
sage: p = MixedIntegerLinearProgram(solver="GLPK")
sage: p.solver_parameter("mip_gap_tolerance",100)
Expand All @@ -1250,7 +1254,7 @@ cdef class GLPKBackend(GenericBackend):
Just make sure that the variable *has* been defined, and is not just
undefined::
sage: backend.get_relative_objective_gap() > 1
sage: backend.get_relative_objective_gap() > 1 # needs sage.graphs
True
"""
return self.search_tree_data.mip_gap
Expand Down
1 change: 1 addition & 0 deletions src/sage/numerical/backends/glpk_graph_backend.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# sage.doctest: needs sage.graphs
"""
GLPK Backend for access to GLPK graph functions
Expand Down
17 changes: 9 additions & 8 deletions src/sage/numerical/backends/interactivelp_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,18 @@ cdef class InteractiveLPBackend:
This backend can work with irrational algebraic numbers::
sage: poly = polytopes.dodecahedron(base_ring=AA) # optional - sage.rings.number_field
sage: lp, x = poly.to_linear_program(solver='InteractiveLP', return_variable=True) # optional - sage.rings.number_field
sage: lp.set_objective(x[0] + x[1] + x[2]) # optional - sage.rings.number_field
sage: lp.solve() # optional - sage.rings.number_field
sage: # needs sage.rings.number_field
sage: poly = polytopes.dodecahedron(base_ring=AA)
sage: lp, x = poly.to_linear_program(solver='InteractiveLP', return_variable=True)
sage: lp.set_objective(x[0] + x[1] + x[2])
sage: lp.solve()
2.291796067500631?
sage: lp.get_values(x[0], x[1], x[2]) # optional - sage.rings.number_field
sage: lp.get_values(x[0], x[1], x[2])
[0.763932022500211?, 0.763932022500211?, 0.763932022500211?]
sage: lp.set_objective(x[0] - x[1] - x[2]) # optional - sage.rings.number_field
sage: lp.solve() # optional - sage.rings.number_field
sage: lp.set_objective(x[0] - x[1] - x[2])
sage: lp.solve()
2.291796067500631?
sage: lp.get_values(x[0], x[1], x[2]) # optional - sage.rings.number_field
sage: lp.get_values(x[0], x[1], x[2])
[0.763932022500211?, -0.763932022500211?, -0.763932022500211?]
"""

Expand Down
3 changes: 2 additions & 1 deletion src/sage/numerical/backends/ppl_backend.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# sage.doctest: optional - pplpy
"""
PPL Backend
Expand Down Expand Up @@ -61,7 +62,7 @@ cdef class PPLBackend(GenericBackend):
Raise an error if a ``base_ring`` is requested that is not supported::
sage: p = MixedIntegerLinearProgram(solver = "PPL", base_ring=AA)
sage: p = MixedIntegerLinearProgram(solver="PPL", base_ring=AA) # needs sage.rings.number_field
Traceback (most recent call last):
...
TypeError: The PPL backend only supports rational data.
Expand Down
26 changes: 13 additions & 13 deletions src/sage/numerical/gauss_legendre.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ def nodes_uncached(degree, prec):
sage: from sage.numerical.gauss_legendre import nodes_uncached
sage: L1 = nodes_uncached(24, 53)
sage: P = RR['x'](sage.functions.orthogonal_polys.legendre_P(24, x))
sage: Pdif = P.diff()
sage: L2 = [((r + 1)/2, 1/(1 - r^2)/Pdif(r)^2)
sage: P = RR['x'](sage.functions.orthogonal_polys.legendre_P(24, x)) # needs sage.symbolic
sage: Pdif = P.diff() # needs sage.symbolic
sage: L2 = [((r + 1)/2, 1/(1 - r^2)/Pdif(r)^2) # needs sage.symbolic
....: for r, _ in RR['x'](P).roots()]
sage: all((a[0] - b[0]).abs() < 1e-15 and (a[1] - b[1]).abs() < 1e-9
sage: all((a[0] - b[0]).abs() < 1e-15 and (a[1] - b[1]).abs() < 1e-9 # needs sage.symbolic
....: for a, b in zip(L1, L2))
True
Expand Down Expand Up @@ -188,11 +188,11 @@ def nodes(degree, prec):
sage: from sage.numerical.gauss_legendre import nodes
sage: L1 = nodes(24, 53)
sage: P = RR['x'](sage.functions.orthogonal_polys.legendre_P(24, x))
sage: Pdif = P.diff()
sage: L2 = [((r + 1)/2, 1/(1 - r^2)/Pdif(r)^2)
sage: P = RR['x'](sage.functions.orthogonal_polys.legendre_P(24, x)) # needs sage.symbolic
sage: Pdif = P.diff() # needs sage.symbolic
sage: L2 = [((r + 1)/2, 1/(1 - r^2)/Pdif(r)^2) # needs sage.symbolic
....: for r, _ in RR['x'](P).roots()]
sage: all((a[0] - b[0]).abs() < 1e-15 and (a[1] - b[1]).abs() < 1e-9
sage: all((a[0] - b[0]).abs() < 1e-15 and (a[1] - b[1]).abs() < 1e-9 # needs sage.symbolic
....: for a, b in zip(L1, L2))
True
Expand Down Expand Up @@ -343,8 +343,8 @@ def integrate_vector(f, prec, epsilon=None):
sage: epsilon = K(2^(-prec + 4))
sage: f = lambda t:V((1 + t^2, 1/(1 + t^2)))
sage: I = integrate_vector(f, prec, epsilon=epsilon)
sage: J = V((4/3, pi/4))
sage: max(c.abs() for c in (I - J)) < epsilon
sage: J = V((4/3, pi/4)) # needs sage.symbolic
sage: max(c.abs() for c in (I - J)) < epsilon # needs sage.symbolic
True
We can also use complex-valued integrands::
Expand All @@ -354,10 +354,10 @@ def integrate_vector(f, prec, epsilon=None):
sage: K = ComplexField(prec)
sage: V = VectorSpace(K, 2)
sage: epsilon = Kreal(2^(-prec + 4))
sage: f = lambda t: V((t, K(exp(2*pi*t*K.0))))
sage: I = integrate_vector(f, prec, epsilon=epsilon)
sage: f = lambda t: V((t, K(exp(2*pi*t*K.0)))) # needs sage.symbolic
sage: I = integrate_vector(f, prec, epsilon=epsilon) # needs sage.symbolic
sage: J = V((1/2, 0))
sage: max(c.abs() for c in (I - J)) < epsilon
sage: max(c.abs() for c in (I - J)) < epsilon # needs sage.symbolic
True
"""
results = []
Expand Down
24 changes: 12 additions & 12 deletions src/sage/numerical/interactive_simplex_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
Since it has only two variables, we can solve it graphically::
sage: P.plot() # optional - sage.plot
sage: P.plot() # needs sage.plot
Graphics object consisting of 19 graphics primitives
Expand Down Expand Up @@ -298,9 +298,9 @@ def _latex_product(coefficients, variables,
sage: from sage.numerical.interactive_simplex_method import \
....: _latex_product
sage: var("x, y")
sage: var("x, y") # needs sage.symbolic
(x, y)
sage: print(_latex_product([-1, 3], [x, y]))
sage: print(_latex_product([-1, 3], [x, y])) # needs sage.symbolic
- \mspace{-6mu}&\mspace{-6mu} x \mspace{-6mu}&\mspace{-6mu} + \mspace{-6mu}&\mspace{-6mu} 3 y
"""
entries = []
Expand Down Expand Up @@ -1534,19 +1534,19 @@ def plot(self, *args, **kwds):
sage: b = (1000, 1500)
sage: c = (10, 5)
sage: P = InteractiveLPProblem(A, b, c, ["C", "B"], variable_type=">=")
sage: p = P.plot() # optional - sage.plot
sage: p.show() # optional - sage.plot
sage: p = P.plot() # needs sage.plot
sage: p.show() # needs sage.plot
In this case the plot works better with the following axes ranges::
sage: p = P.plot(0, 1000, 0, 1500) # optional - sage.plot
sage: p.show() # optional - sage.plot
sage: p = P.plot(0, 1000, 0, 1500) # needs sage.plot
sage: p.show() # needs sage.plot
TESTS:
We check that zero objective can be dealt with::
sage: InteractiveLPProblem(A, b, (0, 0), ["C", "B"], variable_type=">=").plot() # optional - sage.plot
sage: InteractiveLPProblem(A, b, (0, 0), ["C", "B"], variable_type=">=").plot() # needs sage.plot
Graphics object consisting of 8 graphics primitives
"""
FP = self.plot_feasible_set(*args, **kwds)
Expand Down Expand Up @@ -1611,13 +1611,13 @@ def plot_feasible_set(self, xmin=None, xmax=None, ymin=None, ymax=None,
sage: b = (1000, 1500)
sage: c = (10, 5)
sage: P = InteractiveLPProblem(A, b, c, ["C", "B"], variable_type=">=")
sage: p = P.plot_feasible_set() # optional - sage.plot
sage: p.show() # optional - sage.plot
sage: p = P.plot_feasible_set() # needs sage.plot
sage: p.show() # needs sage.plot
In this case the plot works better with the following axes ranges::
sage: p = P.plot_feasible_set(0, 1000, 0, 1500) # optional - sage.plot
sage: p.show() # optional - sage.plot
sage: p = P.plot_feasible_set(0, 1000, 0, 1500) # needs sage.plot
sage: p.show() # needs sage.plot
"""
if self.n() != 2:
raise ValueError("only problems with 2 variables can be plotted")
Expand Down
1 change: 1 addition & 0 deletions src/sage/numerical/knapsack.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ def is_superincreasing(self, seq=None):
The sequence must contain only integers::
sage: # needs sage.symbolic
sage: from sage.numerical.knapsack import Superincreasing
sage: L = [1.0, 2.1, pi, 21, 69, 189, 376, 919]
sage: Superincreasing(L).is_superincreasing()
Expand Down
33 changes: 20 additions & 13 deletions src/sage/numerical/mip.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ cdef class MixedIntegerLinearProgram(SageObject):
Computation of a maximum stable set in Petersen's graph::
sage: # needs sage.graphs
sage: g = graphs.PetersenGraph()
sage: p = MixedIntegerLinearProgram(maximization=True, solver='GLPK')
sage: b = p.new_variable(binary=True)
Expand Down Expand Up @@ -659,13 +660,15 @@ cdef class MixedIntegerLinearProgram(SageObject):
sage: p = MixedIntegerLinearProgram(solver='ppl')
sage: p.base_ring()
Rational Field
sage: from sage.rings.qqbar import AA # optional - sage.rings.number_field
sage: p = MixedIntegerLinearProgram(solver='InteractiveLP', base_ring=AA) # optional - sage.rings.number_field
sage: p.base_ring() # optional - sage.rings.number_field
sage: from sage.rings.qqbar import AA # needs sage.rings.number_field
sage: p = MixedIntegerLinearProgram(solver='InteractiveLP', base_ring=AA) # needs sage.rings.number_field
sage: p.base_ring() # needs sage.rings.number_field
Algebraic Real Field
sage: d = polytopes.dodecahedron() # optional - sage.rings.number_field
sage: p = MixedIntegerLinearProgram(base_ring=d.base_ring()) # optional - sage.rings.number_field
sage: p.base_ring() # optional - sage.rings.number_field
sage: # needs sage.groups sage.rings.number_field
sage: d = polytopes.dodecahedron()
sage: p = MixedIntegerLinearProgram(base_ring=d.base_ring())
sage: p.base_ring()
Number Field in sqrt5 with defining polynomial x^2 - 5 with sqrt5 = 2.236067977499790?
"""
return self._backend.base_ring()
Expand Down Expand Up @@ -2629,6 +2632,7 @@ cdef class MixedIntegerLinearProgram(SageObject):
Computation of a maximum stable set in Petersen's graph::
sage: # needs sage.graphs
sage: g = graphs.PetersenGraph()
sage: p = MixedIntegerLinearProgram(maximization=True, solver='GLPK')
sage: b = p.new_variable(nonnegative=True)
Expand Down Expand Up @@ -2823,14 +2827,15 @@ cdef class MixedIntegerLinearProgram(SageObject):
are not recorded, and we can disable this feature providing an empty
filename. This is currently working with CPLEX and Gurobi::
sage: p = MixedIntegerLinearProgram(solver="CPLEX") # optional - CPLEX
sage: p.solver_parameter("logfile") # optional - CPLEX
sage: # optional - cplex
sage: p = MixedIntegerLinearProgram(solver="CPLEX")
sage: p.solver_parameter("logfile")
''
sage: p.solver_parameter("logfile", "/dev/null") # optional - CPLEX
sage: p.solver_parameter("logfile") # optional - CPLEX
sage: p.solver_parameter("logfile", "/dev/null")
sage: p.solver_parameter("logfile")
'/dev/null'
sage: p.solver_parameter("logfile", '') # optional - CPLEX
sage: p.solver_parameter("logfile") # optional - CPLEX
sage: p.solver_parameter("logfile", '')
sage: p.solver_parameter("logfile")
''
Solver-specific parameters:
Expand Down Expand Up @@ -2983,6 +2988,7 @@ cdef class MixedIntegerLinearProgram(SageObject):
EXAMPLES::
sage: # needs sage.graphs
sage: g = graphs.CubeGraph(9)
sage: p = MixedIntegerLinearProgram(solver="GLPK")
sage: p.solver_parameter("mip_gap_tolerance",100)
Expand Down Expand Up @@ -3017,6 +3023,7 @@ cdef class MixedIntegerLinearProgram(SageObject):
EXAMPLES::
sage: # needs sage.graphs
sage: g = graphs.CubeGraph(9)
sage: p = MixedIntegerLinearProgram(solver="GLPK")
sage: p.solver_parameter("mip_gap_tolerance",100)
Expand All @@ -3035,7 +3042,7 @@ cdef class MixedIntegerLinearProgram(SageObject):
Just make sure that the variable *has* been defined, and is not just
undefined::
sage: p.get_relative_objective_gap() > 1
sage: p.get_relative_objective_gap() > 1 # needs sage.graphs
True
"""
return self._backend.get_relative_objective_gap()
Expand Down
Loading

0 comments on commit 159cb85

Please sign in to comment.