<jupyter_start><jupyter_text>CVXPY Tutorial> https://www.cvxpy.org/tutorial/intro/index.html<jupyter_code>import cvxpy as cp
from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns<jupyter_output><empty_output><jupyter_text>Lesson one> https://www.cvxpy.org/tutorial/intro/index.html  First example<jupyter_code># create two scalar optimization variables
x = cp.Variable()
y = cp.Variable()

# objective
cost = (x-y)**2
obj = cp.Minimize(cost)

# create two constraints
constr = [x + y == 1, x - y >= 1]

# form problem
prob = cp.Problem(obj, constr)
opt_val = prob.solve()
print("status:", prob.status)
print(f"optimal value", prob.value)
print(f"x = {x.value}, y = {y.value}")
# https://jakevdp.github.io/PythonDataScienceHandbook/04.12-three-dimensional-plotting.html
def f(x, y):
    return (x-y)**2

x = np.linspace(-10, 10, 30)
y = np.linspace(-10, 10, 30)

X, Y = np.meshgrid(x, y)
Z = f(X, Y)

fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, Z, 50, cmap='binary')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z');<jupyter_output><empty_output><jupyter_text>Second example<jupyter_code>import cvxpy as cp
import numpy

# Problem data.
m = 10
n = 2
numpy.random.seed(1)
A = numpy.random.randn(m, n)
b = numpy.random.randn(m)

# Construct the problem.
x = cp.Variable(n)
objective = cp.Minimize(cp.sum_squares(A @ x - b))
constraints = [0 <= x, x <= 1]
prob = cp.Problem(objective, constraints)

print("Optimal value", prob.solve())
print("Optimal var")
print(x.value) # A numpy ndarray.
A @ np.arange(2) - b
# https://jakevdp.github.io/PythonDataScienceHandbook/04.12-three-dimensional-plotting.html
def f(x, y):
    return (x-y)**2

x = np.linspace(-10, 10, 30)
y = np.linspace(-10, 10, 30)

X, Y = np.meshgrid(x, y)
Z = f(X, Y)

fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, Z, 50, cmap='binary')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z');<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17502507522567703, 'fraction_numerical': 0.02657973921765296, 'mean_word_length': 4.884955752212389, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 16, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '174060', 'license': 'Apache-2.0', 'n_tokens_mistral': 880, 'n_tokens_neox': 838, 'n_words': 179, 'path': 'notebooks/cvxpy-tutorial.ipynb', 'repo_name': 'skipperkongen/cvxpy-tutorial', 'size': '1994'}