<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 <a href="https
Step1:  Problem 1
Step2:  Problem 2
Step3:  Problem 3
Step4:  Problem 4
Step5:  Problem 5
Step6:  Problem 6
Step7:  Problem 7
Step8:  Problem 8
<ASSISTANT_TASK:>
Python Code:
import numpy as np # np.array (and used internally in cvxpy)
import cvxpy as cvx
import sys
print("Using CVX version", cvx.__version__)
print(" and python version", sys.version)
A = np.array([[1, 6,11, 5,10, 4, 9, 3, 8, 2],
              [2, 7, 1, 6,11, 5,10, 4, 9, 3],
              [3, 8, 2, 7, 1, 6,11, 5,10, 4],
              [4, 9, 3, 8, 2, 7, 1, 6,11, 5],
              [5,10, 4, 9, 3, 8, 2, 7, 1, 6]])
y = np.array([1,2,3,4,5]).T
tol = 1e-12
def print_status(prob, x):
   print("Problem status: ", prob.status);
   print("Used the solver: ", 
         prob.solver_stats.solver_name, "with", 
         prob.solver_stats.num_iters, "iterations.")
   print("Optimal value:  ", prob.value)
   print("Optimal var:\n", x.value)
Explanation: <a href="https://colab.research.google.com/github/stephenbeckr/convex-optimization-class/blob/master/Demos/CVX_demo/tutorialSolutions.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>
Python solutions for cvxpy exercise
You can see what's in the output of prob by doing vars(prob.solver_stats)
The default solver depends on what kind of problem you're solving, but it's often ECOS.  See advanced cvxpy settings for how to change accuracy options.  For ECOS the relevant options are abstol and reltol (defaults are 1e-8).
This notebook is for https://github.com/stephenbeckr/convex-optimization-class/tree/master/Demos/CVX_demo
End of explanation
x = cvx.Variable(10) # column vector with 10 elements
obj = cvx.Minimize(cvx.norm(x)) # cvx.norm defaults to the 2-norm
constraints = [cvx.norm(A*x-y) <= 0.1] # specify a list of constraints
prob = cvx.Problem(obj, constraints)
prob.solve(abstol=tol,reltol=tol)
print_status(prob, x)
Explanation: Problem 1
End of explanation
x = cvx.Variable(10)
obj = cvx.Minimize(cvx.norm(x)**2) # cvxpy objects implement the standard python ops
constraints = [cvx.norm(A*x-y) <= 0.1]
prob = cvx.Problem(obj, constraints)
prob.solve(abstol=tol,reltol=tol)
print_status(prob, x)
Explanation: Problem 2
End of explanation
x = cvx.Variable(10)
obj = cvx.Minimize(cvx.norm(x, p=1))
constraints = [cvx.norm(A*x-y) <= 0.1]
prob = cvx.Problem(obj, constraints)
prob.solve(abstol=tol,reltol=tol)
print_status(prob, x)
Explanation: Problem 3
End of explanation
def get_problem1_dual_value():
  obj = cvx.Minimize(cvx.norm(x))
  constraints = [cvx.norm(A*x-y) <= 0.1]
  prob = cvx.Problem(obj, constraints)
  prob.solve(abstol=tol,reltol=tol)
  
  return constraints[0].dual_value
x = cvx.Variable(10)
# resolve problem 1 and return dual value for the constraint
l = get_problem1_dual_value()
print("dual variable: ", l)
obj = cvx.Minimize(cvx.norm(x) + l*cvx.norm(A*x-y))
prob = cvx.Problem(obj)
prob.solve(abstol=tol,reltol=tol)
# note that the solution is the same, but the optimal value is different,
# since for problem 1 we form the Lagrangian \|x\|_2 + \lambda(\|Ax-y\|_2-0.1)
print_status(prob, x)
# the optimal value for problem 1 should be
print("problem 1 optimal value: ", prob.value - 0.1*l)
Explanation: Problem 4
End of explanation
# x = cvx.Variable(5)  # we need to make this an explicit column vector
x = cvx.Variable((5,1))
ones = np.ones((10,1))
obj = cvx.Minimize(sum(cvx.norm(A-x@ones.T, axis=0))) # cvx.norm behaves like np.linalg.norm
prob = cvx.Problem(obj)
prob.solve(abstol=tol,reltol=tol)
print_status(prob, x)
Explanation: Problem 5
End of explanation
x = cvx.Variable((5,1)) # again, be careful here
ones = np.ones((10,1))
obj = cvx.Minimize(cvx.norm(A-x*ones.T))
prob = cvx.Problem(obj)
# ECOS solver won't solve this kind of thing. SCS is the new default
prob.solve(verbose=False,eps=tol) # ~1e-7 duality gap, but CVXOPT gets a singular KKT system
print_status(prob, x)
prob.solve(verbose=False, solver='CVXOPT', kktsolver='robust')
print_status(prob, x)
Explanation: Problem 6
End of explanation
# X = cvx.Variable(5,10) # old syntax
X = cvx.Variable((5,10))
obj = cvx.Minimize(cvx.norm(X-A, 'fro'))
constraints = [ np.ones((5,)).T*X*np.ones((10,)) == 1. ]
prob = cvx.Problem(obj, constraints)
prob.solve()
print_status(prob, X)
Explanation: Problem 7
End of explanation
B = A[:,0:5]
X = cvx.Variable((5,5)) # could use Semidef or Symmetric here instead
obj = cvx.Minimize(cvx.norm(X-B, 'fro'))
constraints = [ X == X.T, X >> 0 ] # X is PSD
prob = cvx.Problem(obj, constraints)
prob.solve()
print_status(prob, X)
Explanation: Problem 8
End of explanation
<END_TASK>