'Test examples from cvxpy.org introduction.'

def test_intro(self):

    import numpy
    m = 30
    n = 20
    numpy.random.seed(1)
    A = numpy.random.randn(m, n)
    b = numpy.random.randn(m, 1)
    x = Variable(n)
    objective = Minimize(sum_squares(((A * x) - b)))
    constraints = [(0 <= x), (x <= 1)]
    prob = Problem(objective, constraints)
    result = prob.solve()
    print(x.value)
    print(constraints[0].dual_value)
    x = Variable()
    y = Variable()
    constraints = [((x + y) == 1), ((x - y) >= 1)]
    obj = Minimize(square((x - y)))
    prob = Problem(obj, constraints)
    prob.solve()
    print('status:', prob.status)
    print('optimal    value', prob.value)
    print('optimal    var', x.value, y.value)
    import cvxpy as cvx
    x = cvx.Variable()
    y = cvx.Variable()
    constraints = [((x + y) == 1), ((x - y) >= 1)]
    obj = cvx.Minimize(cvx.square((x - y)))
    prob = cvx.Problem(obj, constraints)
    prob.solve()
    print('status:', prob.status)
    print('optimal    value', prob.value)
    print('optimal    var', x.value, y.value)
    self.assertEqual(prob.status, OPTIMAL)
    self.assertAlmostEqual(prob.value, 1.0)
    self.assertAlmostEqual(x.value, 1.0)
    self.assertAlmostEqual(y.value, 0)
    prob.objective = Maximize((x + y))
    print('optimal    value', prob.solve())
    self.assertAlmostEqual(prob.value, 1.0)
    prob.constraints[0] = ((x + y) <= 3)
    print('optimal    value', prob.solve())
    self.assertAlmostEqual(prob.value, 3.0)
    x = Variable()
    prob = Problem(Minimize(x), [(x >= 1), (x <= 0)])
    prob.solve()
    print('status:', prob.status)
    print('optimal    value', prob.value)
    self.assertEqual(prob.status, INFEASIBLE)
    self.assertAlmostEqual(prob.value, np.inf)
    prob = Problem(Minimize(x))
    prob.solve()
    print('status:', prob.status)
    print('optimal    value', prob.value)
    self.assertEqual(prob.status, UNBOUNDED)
    self.assertAlmostEqual(prob.value, (- np.inf))
    a = Variable()
    x = Variable(5)
    A = Variable(4, 7)
    import numpy
    m = 10
    n = 5
    numpy.random.seed(1)
    A = numpy.random.randn(m, n)
    b = numpy.random.randn(m, 1)
    x = Variable(n)
    objective = Minimize(sum_entries(square(((A * x) - b))))
    constraints = [(0 <= x), (x <= 1)]
    prob = Problem(objective, constraints)
    print('Optimal    value', prob.solve())
    print('Optimal    var')
    print(x.value)
    self.assertAlmostEqual(prob.value, 4.14133859146)
    m = Parameter(sign='positive')
    c = Parameter(5)
    G = Parameter(4, 7, sign='negative')
    G.value = (- numpy.ones((4, 7)))
    rho = Parameter(sign='positive')
    rho.value = 2
    rho = Parameter(sign='positive', value=2)
    import numpy
    n = 15
    m = 10
    numpy.random.seed(1)
    A = numpy.random.randn(n, m)
    b = numpy.random.randn(n, 1)
    gamma = Parameter(sign='positive')
    x = Variable(m)
    error = sum_squares(((A * x) - b))
    obj = Minimize((error + (gamma * norm(x, 1))))
    prob = Problem(obj)
    sq_penalty = []
    l1_penalty = []
    x_values = []
    gamma_vals = numpy.logspace((-4), 6)
    for val in gamma_vals:
        gamma.value = val
        prob.solve()
        sq_penalty.append(error.value)
        l1_penalty.append(norm(x, 1).value)
        x_values.append(x.value)
    import numpy
    X = Variable(5, 4)
    A = numpy.ones((3, 5))
    print('dimensions    of    X:', X.size)
    print('dimensions    of    sum_entries(X):', sum_entries(X).size)
    print('dimensions    of    A*X:', (A * X).size)
    try:
        (A + X)
    except ValueError as e:
        print(e)
