forked from cvxgrp/diffcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello_world.py
More file actions
38 lines (30 loc) · 735 Bytes
/
hello_world.py
File metadata and controls
38 lines (30 loc) · 735 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import diffcp
import numpy as np
from scipy import sparse
import utils
np.set_printoptions(precision=5, suppress=True)
cone_dict = {
'z': 3,
'l': 3,
'q': [5]
}
m = 3 + 3 + 5
n = 5
np.random.seed(0)
A, b, c = utils.random_cone_prog(m, n, cone_dict)
m, n = A.shape
x, y, s, D, DT = diffcp.solve_and_derivative(A, b, c, cone_dict, solve_method="Clarabel")
# evaluate the derivative
nonzeros = A.nonzero()
data = 1e-4 * np.random.randn(A.size)
dA = sparse.csc_matrix((data, nonzeros), shape=A.shape)
db = 1e-4 * np.random.randn(m)
dc = 1e-4 * np.random.randn(n)
dx, dy, ds = D(dA, db, dc)
print(dx)
# evaluate the adjoint of the derivative
dx = c
dy = np.zeros(m)
ds = np.zeros(m)
dA, db, dc = DT(dx, dy, ds)
print(dc)