Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _compute_symm_attr(self):
"""Determine whether the constant is symmetric/Hermitian.
"""
# Set DCP attributes.
is_symm, is_herm = intf.is_hermitian(self.value)
self._symm = is_symm
self._herm = is_herm
The variables or constraints where the values will be stored.
offset_map : dict
A map of object id to offset in the results vector.
"""
if len(result_vec) > 0:
# Cast to desired matrix type.
result_vec = intf.DEFAULT_INTF.const_to_matrix(result_vec)
for obj in objects:
rows, cols = obj.size
if obj.id in offset_map:
offset = offset_map[obj.id]
# Handle scalars
if (rows, cols) == (1, 1):
value = intf.index(result_vec, (offset, 0))
else:
value = intf.DEFAULT_INTF.zeros(rows, cols)
intf.DEFAULT_INTF.block_add(
value, result_vec[offset:offset + rows * cols], 0, 0,
rows, cols)
offset += rows * cols
else: # The variable was multiplied by zero.
value = intf.DEFAULT_INTF.zeros(rows, cols)
obj.save_value(value)
def apply(self, problem):
"""Returns a new problem and data for inverting the new solution.
Returns
-------
tuple
(dict of arguments needed for the solver, inverse data)
"""
data, inv_data = super(CVXOPT, self).apply(problem)
# Convert A, b, G, h, c to CVXOPT matrices.
if data[s.A] is not None:
data[s.A] = intf.sparse2cvxopt(data[s.A])
if data[s.G] is not None:
data[s.G] = intf.sparse2cvxopt(data[s.G])
if data[s.B] is not None:
data[s.B] = intf.dense2cvxopt(data[s.B])
if data[s.H] is not None:
data[s.H] = intf.dense2cvxopt(data[s.H])
if data[s.C] is not None:
data[s.C] = intf.dense2cvxopt(data[s.C])
return data, inv_data
def vec_intf(self):
"""The interface for vectors passed to the solver.
"""
return intf.DEFAULT_INTF
def numeric(self, values):
"""Sums the entries of value.
"""
if intf.is_sparse(values[0]):
result = np.sum(values[0], axis=self.axis)
if not self.keepdims and self.axis is not None:
result = result.A.flatten()
else:
result = np.sum(values[0], axis=self.axis, keepdims=self.keepdims)
return result
The root linear operator.
rh_val : NDArray
The vector being convolved.
transpose : bool
Is the transpose of convolution being applied?
is_abs : bool
Is the absolute value of convolution being applied?
Returns
-------
NumPy NDArray
The convolution.
"""
constant = mul(lin_op.data, {}, is_abs)
# Convert to 2D
constant, rh_val = map(intf.from_1D_to_2D, [constant, rh_val])
if transpose:
constant = np.flipud(constant)
# rh_val always larger than constant.
return fftconvolve(rh_val, constant, mode='valid')
else:
# First argument must be larger.
if constant.size >= rh_val.size:
return fftconvolve(constant, rh_val, mode='full')
else:
return fftconvolve(rh_val, constant, mode='full')
Q = Q[:, rows_to_keep]
# Invert P from col -> var to var -> col.
Pinv = np.zeros(P.size, dtype='int')
for i in range(P.size):
Pinv[P[i]] = i
# Rearrage R.
R = R[:, Pinv]
A = R
b_old = b
b = Q.T.dot(b)
# If b is not in the range of Q,
# the problem is infeasible.
if not np.allclose(b_old, Q.dot(b)):
return s.INFEASIBLE
dims[s.EQ_DIM] = int(b.shape[0])
data["Q"] = intf.dense2cvxopt(Q)
# Remove obviously redundant rows in G's <= constraints.
if G is not None:
G = G.tocsr()
G_leq = G[:dims[s.LEQ_DIM], :]
h_leq = h[:dims[s.LEQ_DIM]].ravel()
G_other = G[dims[s.LEQ_DIM]:, :]
h_other = h[dims[s.LEQ_DIM]:].ravel()
G_leq, h_leq, P_leq = compress_matrix(G_leq, h_leq)
dims[s.LEQ_DIM] = int(h_leq.shape[0])
data["P_leq"] = intf.sparse2cvxopt(P_leq)
G = sp.vstack([G_leq, G_other])
h = np.hstack([h_leq, h_other])
# Convert A, b, G, h to CVXOPT matrices.
data[s.A] = A
data[s.G] = G
data[s.B] = b
# If there are no constraints, the problem is unbounded
# if any of the coefficients are non-zero.
# If all the coefficients are zero then return the constant term
# and set all variables to 0.
if not any(constr_map.values()):
str(objective) # TODO
# Remove constraints with no variables or parameters.
for key in [s.EQ, s.LEQ]:
new_constraints = []
for constr in constr_map[key]:
vars_ = lu.get_expr_vars(constr.expr)
if len(vars_) == 0 and not lu.get_expr_params(constr.expr):
V, I, J, coeff = canonInterface.get_problem_matrix([constr])
is_pos, is_neg = intf.sign(coeff)
# For equality constraint, coeff must be zero.
# For inequality (i.e. <= 0) constraint,
# coeff must be negative.
if key == s.EQ and not (is_pos and is_neg) or \
key == s.LEQ and not is_neg:
return s.INFEASIBLE
else:
new_constraints.append(constr)
constr_map[key] = new_constraints
return None
block = self.vec_intf.reshape(
block,
(block_size[0]*block_size[1], 1)
)
mat_cache.const_vec[vert_start:vert_end, :] += block
else:
horiz_offset = self.sym_data.var_offsets[id_]
if intf.is_scalar(block):
block = intf.scalar_value(block)
V.append(block)
I.append(vert_start)
J.append(horiz_offset)
else:
# Block is a numpy matrix or
# scipy CSC sparse matrix.
if not intf.is_sparse(block):
block = intf.DEFAULT_SPARSE_INTF.const_to_matrix(
block
)
block = block.tocoo()
V.extend(block.data)
I.extend(block.row + vert_start)
J.extend(block.col + horiz_offset)