Skip to content

Instantly share code, notes, and snippets.

@silgon
Last active May 17, 2023 09:47
Show Gist options
  • Save silgon/52cf14d38015969f2e02a7469fbb699c to your computer and use it in GitHub Desktop.
Save silgon/52cf14d38015969f2e02a7469fbb699c to your computer and use it in GitHub Desktop.

Sanity check for convex optimization set

image

import numpy as np
import matplotlib.pyplot as plt
import cvxpy as cp
import scipy
np.random.seed(1)
b = np.array([[.5, 1.]]).T
α = 0.3
A = np.array([[1., α], [α, 1.5]])
A_inv = np.linalg.inv(A)
A_sqrt = scipy.linalg.sqrtm(A)
n_pts = 10000
pts_base = (np.random.rand(2, n_pts)-.5)*5
pts = pts_base+b
ε_1 = pts[:, [(pts-b)[:,i].T@A_inv@(pts-b)[:,i]<=1 for i in range(n_pts)]]
plt.scatter(*ε_1, c="C1", alpha=.5, label=r"$\mathcal{E} = \left \{ x | (x-x_c)^\mathsf{T}P^{-1}(x-x_c) \leq 1 \right \}$")
pts = pts_base[:, np.linalg.norm(pts_base, axis=0) <= 1]
ε_2 = b+A_sqrt@pts
plt.scatter(*ε_2, c="C2", alpha=.5, label=r"$\mathcal{E} = \left \{ x_c+Au \,|\, \| u \| \leq 1 \right \}$")
plt.legend()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment