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_vec_psd_dim(self):
self.assertEqual(cone_lib.vec_psd_dim(10), (10) * (10 + 1) / 2)
def test_vec_symm(self):
np.random.seed(0)
n = 5
x = np.random.randn(cone_lib.vec_psd_dim(n))
np.testing.assert_allclose(
cone_lib.vec_symm(cone_lib.unvec_symm(x, n)), x)
cone_lib._proj(x[:zero_dim], cone_lib.ZERO, dual=dual))
offset += zero_dim
np.testing.assert_allclose(proj[offset:offset + pos_dim],
cone_lib._proj(x[offset:offset + pos_dim], cone_lib.POS,
dual=dual))
offset += pos_dim
for dim in soc_dim:
np.testing.assert_allclose(proj[offset:offset + dim],
cone_lib._proj(x[offset:offset + dim], cone_lib.SOC,
dual=dual))
offset += dim
for dim in psd_dim:
dim = cone_lib.vec_psd_dim(dim)
np.testing.assert_allclose(proj[offset:offset + dim],
cone_lib._proj(x[offset:offset + dim], cone_lib.PSD,
dual=dual))
offset += dim
dim = 3 * exp_dim
np.testing.assert_allclose(proj[offset:offset + dim],
cone_lib._proj(x[offset:offset + dim], cone_lib.EXP, dual=dual))
offset += dim
np.testing.assert_allclose(proj[offset:],
cone_lib._proj(x[offset:], cone_lib.EXP_DUAL, dual=dual))
def test_dpi(self):
np.random.seed(0)
for _ in range(10):
zero_dim = np.random.randint(1, 10)
pos_dim = np.random.randint(1, 10)
soc_dim = [np.random.randint(1, 10) for _ in range(
np.random.randint(1, 10))]
psd_dim = [np.random.randint(1, 10) for _ in range(
np.random.randint(1, 10))]
exp_dim = np.random.randint(3, 18)
cones = [(cone_lib.ZERO, zero_dim), (cone_lib.POS, pos_dim),
(cone_lib.SOC, soc_dim), (cone_lib.PSD, psd_dim),
(cone_lib.EXP, exp_dim), (cone_lib.EXP_DUAL, exp_dim)]
size = zero_dim + pos_dim + sum(soc_dim) + sum(
[cone_lib.vec_psd_dim(d) for d in psd_dim]) + 2 * 3 * exp_dim
x = np.random.randn(size)
for dual in [False, True]:
cone_list_cpp = cone_lib.parse_cone_dict_cpp(cones)
proj_x = cone_lib.pi(x, cones, dual=dual)
dx = 1e-7 * np.random.randn(size)
z = cone_lib.pi(x + dx, cones, dual=dual)
Dpi = _diffcp.dprojection(x, cone_list_cpp, dual)
np.testing.assert_allclose(
Dpi.matvec(dx), z - proj_x, atol=1e-6)
Dpi = _diffcp.dprojection_dense(x, cone_list_cpp, dual)
np.testing.assert_allclose(Dpi @ dx, z - proj_x, atol=1e-6)
def test_pi(self):
np.random.seed(0)
for _ in range(10):
zero_dim = np.random.randint(1, 10)
pos_dim = np.random.randint(1, 10)
soc_dim = [np.random.randint(1, 10) for _ in range(
np.random.randint(1, 10))]
psd_dim = [np.random.randint(1, 10) for _ in range(
np.random.randint(1, 10))]
exp_dim = np.random.randint(3, 18)
cones = [(cone_lib.ZERO, zero_dim), (cone_lib.POS, pos_dim),
(cone_lib.SOC, soc_dim), (cone_lib.PSD, psd_dim),
(cone_lib.EXP, exp_dim), (cone_lib.EXP_DUAL, exp_dim)]
size = zero_dim + pos_dim + sum(soc_dim) + sum(
[cone_lib.vec_psd_dim(d) for d in psd_dim]) + 2 * 3 * exp_dim
x = np.random.randn(size)
for dual in [False, True]:
proj = cone_lib.pi(x, cones, dual=dual)
offset = 0
np.testing.assert_allclose(proj[:zero_dim],
cone_lib._proj(x[:zero_dim], cone_lib.ZERO, dual=dual))
offset += zero_dim
np.testing.assert_allclose(proj[offset:offset + pos_dim],
cone_lib._proj(x[offset:offset + pos_dim], cone_lib.POS,
dual=dual))
offset += pos_dim
for dim in soc_dim:
np.testing.assert_allclose(proj[offset:offset + dim],