Skip to content

Commit

Permalink
Trac #24741: py3: numerous additional fixes for sage.numerical
Browse files Browse the repository at this point in the history
URL: https://trac.sagemath.org/24741
Reported by: embray
Ticket author(s): Erik Bray
Reviewer(s): David Coudert, Travis Scrimshaw
  • Loading branch information
Release Manager authored and vbraun committed Mar 5, 2019
2 parents ac9a885 + 2ebacb6 commit 0c83040
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 37 deletions.
21 changes: 17 additions & 4 deletions src/sage/numerical/backends/coin_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ cdef class CoinBackend(GenericBackend):
return (lb[i] if lb[i] != - self.si.getInfinity() else None,
ub[i] if ub[i] != + self.si.getInfinity() else None)

cpdef add_col(self, list indices, list coeffs):
cpdef add_col(self, indices, coeffs):
r"""
Adds a column.
Expand Down Expand Up @@ -701,14 +701,27 @@ cdef class CoinBackend(GenericBackend):
5
"""

cdef int n = len(indices)
cdef list list_indices
cdef list list_coeffs

if type(indices) is not list:
list_indices = list(indices)
else:
list_indices = <list>indices

if type(coeffs) is not list:
list_coeffs = list(coeffs)
else:
list_coeffs = <list>coeffs

cdef int n = len(list_indices)
cdef int * c_indices = <int*>check_malloc(n*sizeof(int))
cdef double * c_values = <double*>check_malloc(n*sizeof(double))
cdef int i

for 0<= i< n:
c_indices[i] = indices[i]
c_values[i] = coeffs[i]
c_indices[i] = list_indices[i]
c_values[i] = list_coeffs[i]

self.si.addCol (n, c_indices, c_values, 0, self.si.getInfinity(), 0)

Expand Down
21 changes: 17 additions & 4 deletions src/sage/numerical/backends/cplex_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ cdef class CPLEXBackend(GenericBackend):
return (None if lb <= -int(CPX_INFBOUND) else lb,
None if ub >= +int(CPX_INFBOUND) else ub)

cpdef add_col(self, list indices, list coeffs):
cpdef add_col(self, indices, coeffs):
r"""
Adds a column.
Expand Down Expand Up @@ -835,9 +835,22 @@ cdef class CPLEXBackend(GenericBackend):
5
"""

cdef list list_indices
cdef list list_coeffs

if type(indices) is not list:
list_indices = list(indices)
else:
list_indices = <list>indices

if type(coeffs) is not list:
list_coeffs = list(coeffs)
else:
list_coeffs = <list>coeffs

cdef int status
cdef int i
cdef int n = len(indices)
cdef int n = len(list_indices)
cdef int ncols = self.ncols()

status = CPXnewcols(self.env, self.lp, 1, NULL, NULL, NULL, NULL, NULL)
Expand All @@ -850,8 +863,8 @@ cdef class CPLEXBackend(GenericBackend):
cdef int * c_col = <int *> sig_malloc(n * sizeof(int))

for 0<= i < n:
c_coeff[i] = coeffs[i]
c_indices[i] = indices[i]
c_coeff[i] = list_coeffs[i]
c_indices[i] = list_indices[i]
c_col[i] = ncols


Expand Down
8 changes: 4 additions & 4 deletions src/sage/numerical/backends/cvxopt_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ cdef class CVXOPTBackend(GenericBackend):



cpdef add_col(self, list indices, list coeffs):
cpdef add_col(self, indices, coeffs):
"""
Add a column.
Expand Down Expand Up @@ -351,11 +351,11 @@ cdef class CVXOPTBackend(GenericBackend):
5
"""
column = []
for i in range(len(indices)):
for _ in indices:
column.append(0.0)

for i in range(len(indices)):
column[indices[i]] = coeffs[i]
for idx, ind in enumerate(indices):
column[ind] = coeffs[idx]

self.G_matrix.append(column)

Expand Down
2 changes: 1 addition & 1 deletion src/sage/numerical/backends/cvxopt_sdp_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ cdef class CVXOPTSDPBackend(GenericSDPBackend):
[0.0 0.0]
sage: B1.is_positive_definite()
True
sage: x = p.get_values(x).values()
sage: x = sorted(p.get_values(x).values())
sage: x[0]*b1 + x[1]*b2 - b3 + B1 # tol 1e-09
[0.0 0.0]
[0.0 0.0]
Expand Down
2 changes: 1 addition & 1 deletion src/sage/numerical/backends/generic_backend.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ cdef class GenericBackend (SageObject):
cpdef add_linear_constraint_vector(self, degree, coefficients, lower_bound, upper_bound, name=*)
cpdef remove_constraint(self, int)
cpdef remove_constraints(self, constraints)
cpdef add_col(self, list indices, list coeffs)
cpdef add_col(self, indices, coeffs)
cpdef add_linear_constraints(self, int number, lower_bound, upper_bound, names=*)
cpdef int solve(self) except -1
cpdef get_objective_value(self)
Expand Down
2 changes: 1 addition & 1 deletion src/sage/numerical/backends/generic_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ cdef class GenericBackend:
p.add_linear_constraint_vector(2, coeffs, lower, upper, 'foo')
# FIXME: Tests here. Careful what we expect regarding ranged constraints with some solvers.

cpdef add_col(self, list indices, list coeffs):
cpdef add_col(self, indices, coeffs):
"""
Add a column.
Expand Down
14 changes: 11 additions & 3 deletions src/sage/numerical/backends/glpk_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ cdef class GLPKBackend(GenericBackend):
(ub if ub != +DBL_MAX else None)
)

cpdef add_col(self, list indices, list coeffs):
cpdef add_col(self, indices, coeffs):
"""
Add a column.
Expand Down Expand Up @@ -1466,10 +1466,14 @@ cdef class GLPKBackend(GenericBackend):
sage: p.add_variable()
0
sage: p.variable_upper_bound(0, 'hey!')
sage: p.variable_upper_bound(0, 'hey!') # py2
Traceback (most recent call last):
...
TypeError: a float is required
sage: p.variable_upper_bound(0, 'hey!') # py3
Traceback (most recent call last):
...
TypeError: must be real number, not str
"""
cdef double x
cdef double min
Expand Down Expand Up @@ -1557,10 +1561,14 @@ cdef class GLPKBackend(GenericBackend):
sage: p.add_variable()
0
sage: p.variable_lower_bound(0, 'hey!')
sage: p.variable_lower_bound(0, 'hey!') # py2
Traceback (most recent call last):
...
TypeError: a float is required
sage: p.variable_lower_bound(0, 'hey!') # py3
Traceback (most recent call last):
...
TypeError: must be real number, not str
"""
cdef double x
cdef double max
Expand Down
2 changes: 1 addition & 1 deletion src/sage/numerical/backends/gurobi_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ cdef class GurobiBackend(GenericBackend):

return self.ncols()-1

cpdef add_col(self, list indices, list coeffs):
cpdef add_col(self, indices, coeffs):
"""
Add a column.
Expand Down
4 changes: 2 additions & 2 deletions src/sage/numerical/backends/interactivelp_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ cdef class InteractiveLPBackend:
problem_type, ring, objective_constant_term=d)


cpdef add_col(self, list indices, list coeffs):
cpdef add_col(self, indices, coeffs):
"""
Add a column.
Expand Down Expand Up @@ -598,7 +598,7 @@ cdef class InteractiveLPBackend:
sage: p.nrows()
5
"""
self.add_variable(coefficients = zip(indices, coeffs))
self.add_variable(coefficients=zip(indices, coeffs))

cpdef int solve(self) except -1:
"""
Expand Down
2 changes: 1 addition & 1 deletion src/sage/numerical/backends/logging_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def m(self, *args, **kwdargs):
update_wrapper(m, getattr(backend, attr))
return m

class LoggingBackend (GenericBackend):
class LoggingBackend(GenericBackend):

"""
See :class:`LoggingBackendFactory` for documentation.
Expand Down
4 changes: 2 additions & 2 deletions src/sage/numerical/backends/ppl_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ cdef class PPLBackend(GenericBackend):
mip_obj = mip_obj + Linear_Expression(coeff * Variable(i))
self.mip.set_objective_function(mip_obj)
self.obj_denominator = denom

# Constraints
for i in range(len(self.Matrix)):
l = Linear_Expression(0)
Expand Down Expand Up @@ -599,7 +599,7 @@ cdef class PPLBackend(GenericBackend):
self.row_upper_bound.append(upper_bound)
self.row_name_var.append(name)

cpdef add_col(self, list indices, list coeffs):
cpdef add_col(self, indices, coeffs):
"""
Add a column.
Expand Down
4 changes: 2 additions & 2 deletions src/sage/numerical/interactive_simplex_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -2001,13 +2001,13 @@ def __init__(self, A, b, c, x="x", problem_type="max",
slack_variables = ["{}{:d}".format(slack_variables, i)
for i in indices]
else:
slack_variables = list(map(str, slack_variables))
slack_variables = [str(s) for s in slack_variables]
if len(slack_variables) != m:
raise ValueError("wrong number of slack variables")
if auxiliary_variable is None:
auxiliary_variable = x + "0" if isinstance(x, str) else "x0"
names = [str(auxiliary_variable)]
names.extend(map(str, self.x()))
names.extend([str(s) for s in self.x()])
names.extend(slack_variables)
if names[0] == names[1]:
names.pop(0)
Expand Down
10 changes: 5 additions & 5 deletions src/sage/numerical/mip.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ The following example shows all these steps::
x_3 is an integer variable (min=0.0, max=+oo)
sage: print('Objective Value: {}'.format(p.solve()))
Objective Value: 2.0
sage: for i, v in p.get_values(w).iteritems():
sage: for i, v in sorted(p.get_values(w).items()):
....: print('w_%s = %s' % (i, int(round(v))))
w_0 = 15
w_1 = 10
Expand Down Expand Up @@ -1421,7 +1421,7 @@ cdef class MixedIntegerLinearProgram(SageObject):
values for the corresponding variables ::
sage: x_sol = p.get_values(x)
sage: sorted(x_sol.keys())
sage: sorted(x_sol)
[3, 5]
Obviously, it also works with variables of higher dimension::
Expand Down Expand Up @@ -3156,7 +3156,7 @@ cdef class MIPVariable(SageObject):
sage: p = MixedIntegerLinearProgram(solver='GLPK')
sage: v = p.new_variable(nonnegative=True)
sage: p.set_objective(v[0] + v[1])
sage: list(v.keys())
sage: sorted(v.keys())
[0, 1]
"""
return self._dict.keys()
Expand All @@ -3170,7 +3170,7 @@ cdef class MIPVariable(SageObject):
sage: p = MixedIntegerLinearProgram(solver='GLPK')
sage: v = p.new_variable(nonnegative=True)
sage: p.set_objective(v[0] + v[1])
sage: list(v.items())
sage: sorted(v.items())
[(0, x_0), (1, x_1)]
"""
return self._dict.items()
Expand All @@ -3184,7 +3184,7 @@ cdef class MIPVariable(SageObject):
sage: p = MixedIntegerLinearProgram(solver='GLPK')
sage: v = p.new_variable(nonnegative=True)
sage: p.set_objective(v[0] + v[1])
sage: list(v.values())
sage: sorted(v.values(), key=str)
[x_0, x_1]
"""
return self._dict.values()
Expand Down
12 changes: 6 additions & 6 deletions src/sage/numerical/sdp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ The following example shows all these steps::
Optimal solution found.
sage: print('Objective Value: {}'.format(round(opt,3)))
Objective Value: 1.0
sage: [round(x,3) for x in p.get_values(x).values()]
sage: [round(x, 3) for x in sorted(p.get_values(x).values())]
[0.0, 1.0]
sage: p.show()
Maximization:
Expand Down Expand Up @@ -728,7 +728,7 @@ cdef class SemidefiniteProgram(SageObject):
values for the corresponding variables ::
sage: x_sol = p.get_values(x)
sage: list(x_sol.keys())
sage: sorted(x_sol)
[3, 5]
Obviously, it also works with variables of higher dimension::
Expand Down Expand Up @@ -1038,7 +1038,7 @@ cdef class SemidefiniteProgram(SageObject):
[0.0 0.0]
sage: B1.is_positive_definite()
True
sage: x = p.get_values(x).values()
sage: x = sorted(p.get_values(x).values())
sage: x[0]*b1 + x[1]*b2 - b3 + B1 # tol 1e-09
[0.0 0.0]
[0.0 0.0]
Expand Down Expand Up @@ -1287,7 +1287,7 @@ cdef class SDPVariable(Element):
sage: p = SemidefiniteProgram()
sage: v = p.new_variable()
sage: p.set_objective(v[0] + v[1])
sage: list(v.keys())
sage: sorted(v.keys())
[0, 1]
"""
return self._dict.keys()
Expand All @@ -1301,7 +1301,7 @@ cdef class SDPVariable(Element):
sage: p = SemidefiniteProgram()
sage: v = p.new_variable()
sage: p.set_objective(v[0] + v[1])
sage: list(v.items())
sage: sorted(v.items())
[(0, x_0), (1, x_1)]
"""
return self._dict.items()
Expand All @@ -1315,7 +1315,7 @@ cdef class SDPVariable(Element):
sage: p = SemidefiniteProgram()
sage: v = p.new_variable()
sage: p.set_objective(v[0] + v[1])
sage: list(v.values())
sage: sorted(v.values(), key=str)
[x_0, x_1]
"""
return self._dict.values()
Expand Down

0 comments on commit 0c83040

Please sign in to comment.