Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_proj_psd(self):
np.random.seed(0)
n = 10
for _ in range(15):
x = np.random.randn(n, n)
x = x + x.T
x_vec = cone_lib.vec_symm(x)
z = cp.Variable((n, n), PSD=True)
objective = cp.Minimize(cp.sum_squares(z - x))
prob = cp.Problem(objective)
prob.solve(solver="SCS", eps=1e-10)
p = cone_lib.unvec_symm(
cone_lib._proj(x_vec, cone_lib.PSD, dual=False), n)
np.testing.assert_allclose(p, z.value, atol=1e-5, rtol=1e-5)
np.testing.assert_allclose(p, cone_lib.unvec_symm(
cone_lib._proj(x_vec, cone_lib.PSD, dual=True), n))
mn_plus_m_plus_n = A.size + b.size + c.size
n_plus_2n = c.size + 2 * b.size
entries_in_derivative = mn_plus_m_plus_n * n_plus_2n
print(f"""n={n}, p={p}, A.shape={A.shape}, nnz in A={A.nnz}, derivative={mn_plus_m_plus_n}x{n_plus_2n} ({entries_in_derivative} entries)""")
# Compute solution and derivative maps
start = time.perf_counter()
x, y, s, derivative, adjoint_derivative = diffcp.solve_and_derivative(
A, b, c, cone_dims, eps=1e-5)
end = time.perf_counter()
print("Compute solution and set up derivative: %.2f s." % (end - start))
# Derivative
lsqr_args = dict(atol=1e-5, btol=1e-5)
start = time.perf_counter()
dA, db, dc = adjoint_derivative(diffcp.cones.vec_symm(
C), np.zeros(y.size), np.zeros(s.size), **lsqr_args)
end = time.perf_counter()
print("Evaluate derivative: %.2f s." % (end - start))
# Adjoint of derivative
start = time.perf_counter()
dx, dy, ds = derivative(A, b, c, **lsqr_args)
end = time.perf_counter()
print("Evaluate adjoint of derivative: %.2f s." % (end - start))