Skip to content

Commit

Permalink
Remove tocsc method where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
gbanjac committed May 25, 2019
1 parent 6dacf67 commit b566609
Show file tree
Hide file tree
Showing 15 changed files with 41 additions and 40 deletions.
8 changes: 4 additions & 4 deletions docs/examples/huber.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ Python
+ np.multiply(10.*np.random.rand(m), 1. - ind95)
# OSQP data
Im = sparse.eye(m).tocsc()
Im = sparse.eye(m, format='csc')
P = sparse.block_diag((sparse.csc_matrix((n, n)), 2*Im,
sparse.csc_matrix((m, m)))).tocsc()
sparse.csc_matrix((m, m))),
format='csc')
q = np.append(np.zeros(m+n), 2*np.ones(m))
A = sparse.vstack([
sparse.hstack([Ad, -Im, Im]),
sparse.hstack([Ad, -Im, -Im])
]).tocsc()
sparse.hstack([Ad, -Im, -Im])], format='csc')
l = np.hstack([b, -np.inf*np.ones(m)])
u = np.hstack([np.inf*np.ones(m), b])
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/lasso.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Python
q = np.zeros(2*n + m)
A = sparse.vstack([sparse.hstack([Ad, -Im, Onm.T]),
sparse.hstack([In, Onm, -In]),
sparse.hstack([In, Onm, In])]).tocsc()
sparse.hstack([In, Onm, In])], format='csc')
l = np.hstack([b, -np.inf * np.ones(n), np.zeros(n)])
u = np.hstack([b, np.zeros(n), np.inf * np.ones(n)])
Expand Down
3 changes: 1 addition & 2 deletions docs/examples/least-squares.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ Python
q = np.zeros(n+m)
A = sparse.vstack([
sparse.hstack([Ad, -sparse.eye(m)]),
sparse.hstack((sparse.eye(n), sparse.csc_matrix((n, m))))
]).tocsc()
sparse.hstack([sparse.eye(n), sparse.csc_matrix((n, m))])], format='csc')
l = np.hstack([b, np.zeros(n)])
u = np.hstack([b, np.ones(n)])
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/mpc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Python
# Cast MPC problem to a QP: x = (x(0),x(1),...,x(N),u(0),...,u(N-1))
# - quadratic objective
P = sparse.block_diag([sparse.kron(sparse.eye(N), Q), QN,
sparse.kron(sparse.eye(N), R)]).tocsc()
sparse.kron(sparse.eye(N), R)], format='csc')
# - linear objective
q = np.hstack([np.kron(np.ones(N), -Q.dot(xr)), -QN.dot(xr),
np.zeros(N*nu)])
Expand All @@ -96,7 +96,7 @@ Python
lineq = np.hstack([np.kron(np.ones(N+1), xmin), np.kron(np.ones(N), umin)])
uineq = np.hstack([np.kron(np.ones(N+1), xmax), np.kron(np.ones(N), umax)])
# - OSQP constraints
A = sparse.vstack([Aeq, Aineq]).tocsc()
A = sparse.vstack([Aeq, Aineq], format='csc')
l = np.hstack([leq, lineq])
u = np.hstack([ueq, uineq])
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/portfolio.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Python
sparse.hstack([F.T, -sparse.eye(k)]),
sparse.hstack([sparse.csc_matrix(np.ones((1, n))), sparse.csc_matrix((1, k))]),
sparse.hstack((sparse.eye(n), sparse.csc_matrix((n, k))))
]).tocsc()
], format='csc')
l = np.hstack([np.zeros(k), 1., np.zeros(n)])
u = np.hstack([np.zeros(k), 1., np.ones(n)])
Expand Down
6 changes: 3 additions & 3 deletions docs/examples/svm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Python
Ad = sparse.vstack([
A_upp / np.sqrt(n) + (A_upp != 0.).astype(float) / n,
A_low / np.sqrt(n) - (A_low != 0.).astype(float) / n
]).tocsc()
], format='csc')
# OSQP data
Im = sparse.eye(m)
Expand All @@ -54,7 +54,7 @@ Python
A = sparse.vstack([
sparse.hstack([sparse.diags(b).dot(Ad), -Im]),
sparse.hstack([sparse.csc_matrix((m, n)), Im])
]).tocsc()
], format='csc')
l = np.hstack([-np.inf*np.ones(m), np.zeros(m)])
u = np.hstack([-np.ones(m), np.inf*np.ones(m)])
Expand Down Expand Up @@ -126,7 +126,7 @@ CVXPY
A = sparse.vstack([
A_upp / np.sqrt(n) + (A_upp != 0.).astype(float) / n,
A_low / np.sqrt(n) - (A_low != 0.).astype(float) / n
]).tocsc()
], format='csc')
# Define problem
x = Variable(n)
Expand Down
2 changes: 1 addition & 1 deletion tests/basic_qp/generate_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from scipy import sparse
import utils.codegen_utils as cu

P = sparse.triu([[4., 1.], [1., 2.]]).tocsc()
P = sparse.triu([[4., 1.], [1., 2.]], format='csc')
q = np.ones(2)

A = sparse.csc_matrix(np.array([[1., 1.], [1., 0.], [0., 1.], [0., 1.]]))
Expand Down
2 changes: 1 addition & 1 deletion tests/basic_qp2/generate_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from scipy import sparse
import utils.codegen_utils as cu

P = sparse.triu([[11., 0.], [0., 0.]]).tocsc()
P = sparse.triu([[11., 0.], [0., 0.]], format='csc')
q = np.array([3., 4.])

A = sparse.csc_matrix(np.array([[-1., 0.], [0., -1.], [-1., 3.],
Expand Down
18 changes: 9 additions & 9 deletions tests/lin_alg/generate_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@

# Test matrix operations
test_mat_ops_n = 2
test_mat_ops_A = sparse.random(test_mat_ops_n, test_mat_ops_n, density=0.8).tocsc()
test_mat_ops_A = sparse.random(test_mat_ops_n, test_mat_ops_n, density=0.8, format='csc')
test_mat_ops_d = np.random.randn(test_mat_ops_n)
D = sparse.diags(test_mat_ops_d).tocsc()
D = sparse.diags(test_mat_ops_d, format='csc')
test_mat_ops_prem_diag = D.dot(test_mat_ops_A).tocoo().tocsc() # Force matrix reordering
test_mat_ops_postm_diag = test_mat_ops_A.dot(D).tocoo().tocsc() # Force matrix reordering
test_mat_ops_inf_norm_cols = np.amax(np.abs(
Expand All @@ -45,10 +45,10 @@

test_mat_vec_n = n
test_mat_vec_m = m
test_mat_vec_A = sparse.random(m, n, density=1.0).tocsc()
test_mat_vec_P = sparse.random(n, n, density=0.8).tocsc()
test_mat_vec_A = sparse.random(m, n, density=1.0, format='csc')
test_mat_vec_P = sparse.random(n, n, density=0.8, format='csc')
test_mat_vec_P = test_mat_vec_P + test_mat_vec_P.T
test_mat_vec_Pu = sparse.triu(test_mat_vec_P).tocsc()
test_mat_vec_Pu = sparse.triu(test_mat_vec_P, format='csc')
test_mat_vec_x = np.random.randn(n)
test_mat_vec_y = np.random.randn(m)
test_mat_vec_Ax = test_mat_vec_A.dot(test_mat_vec_x)
Expand All @@ -61,18 +61,18 @@

# Test extract upper triangular
test_mat_extr_triu_n = 5
test_mat_extr_triu_P = sparse.random(test_mat_extr_triu_n, test_mat_extr_triu_n, density=0.8).tocsc()
test_mat_extr_triu_P = sparse.random(test_mat_extr_triu_n, test_mat_extr_triu_n, density=0.8, format='csc')
test_mat_extr_triu_P = test_mat_extr_triu_P + test_mat_extr_triu_P.T
test_mat_extr_triu_Pu = sparse.triu(test_mat_extr_triu_P).tocsc()
test_mat_extr_triu_Pu = sparse.triu(test_mat_extr_triu_P, format='csc')
test_mat_extr_triu_P_inf_norm_cols = np.amax(np.abs(
np.asarray(test_mat_extr_triu_P.todense())), axis=0)


# Test compute quad form
test_qpform_n = 4
test_qpform_P = sparse.random(test_qpform_n, test_qpform_n, density=0.8).tocsc()
test_qpform_P = sparse.random(test_qpform_n, test_qpform_n, density=0.8, format='csc')
test_qpform_P = test_qpform_P + test_qpform_P.T
test_qpform_Pu = sparse.triu(test_qpform_P).tocsc()
test_qpform_Pu = sparse.triu(test_qpform_P, format='csc')
test_qpform_x = np.random.randn(test_qpform_n)
test_qpform_value = .5 * test_qpform_x.T.dot(test_qpform_P.dot(test_qpform_x))

Expand Down
5 changes: 2 additions & 3 deletions tests/non_cvx/generate_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
from scipy import sparse
import utils.codegen_utils as cu

P = sparse.triu([[2., 5.], [5., 1.]]).tocsc()
P = sparse.triu([[2., 5.], [5., 1.]], format='csc')
q = np.array([3., 4.])

A = sparse.csc_matrix(np.array([[-1.0, 0.], [0., -1.], [-1., 3.],
[2., 5.], [3., 4]]))
A = sparse.csc_matrix([[-1., 0.], [0., -1.], [-1., 3.], [2., 5.], [3., 4]])
l = -np.inf * np.ones(A.shape[0])
u = np.array([0., 0., -15., 100., 80.])

Expand Down
2 changes: 1 addition & 1 deletion tests/primal_dual_infeasibility/generate_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from scipy import sparse
import utils.codegen_utils as cu

P = sparse.diags([1., 0.]).tocsc()
P = sparse.diags([1., 0.], format='csc')
q = np.array([1., -1.])

A12 = sparse.csc_matrix([[1., 1.], [1., 0.], [0., 1.]])
Expand Down
2 changes: 1 addition & 1 deletion tests/primal_infeasibility/generate_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# Generate random Matrices
Pt = sparse.random(n, n)
P = Pt.T.dot(Pt) + sparse.eye(n)
P = sparse.triu(P).tocsc()
P = sparse.triu(P, format='csc')
q = sp.randn(n)
A = sparse.random(m, n).tolil() # Lil for efficiency
u = 3 + sp.randn(m)
Expand Down
7 changes: 4 additions & 3 deletions tests/solve_linsys/generate_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@
test_solve_KKT_P = test_solve_KKT_P.dot(test_solve_KKT_P.T).tocsc()
test_solve_KKT_A = sparse.random(test_solve_KKT_m, test_solve_KKT_n,
density=0.4, format='csc')
test_solve_KKT_Pu = sparse.triu(test_solve_KKT_P).tocsc()
test_solve_KKT_Pu = sparse.triu(test_solve_KKT_P, format='csc')

test_solve_KKT_rho = 4.0
test_solve_KKT_sigma = 1.0
test_solve_KKT_KKT = sparse.vstack([
sparse.hstack([test_solve_KKT_P + test_solve_KKT_sigma *
sparse.eye(test_solve_KKT_n), test_solve_KKT_A.T]),
sparse.hstack([test_solve_KKT_A,
-1./test_solve_KKT_rho * sparse.eye(test_solve_KKT_m)])]).tocsc()
-1./test_solve_KKT_rho * sparse.eye(test_solve_KKT_m)])
], format='csc')
test_solve_KKT_rhs = np.random.randn(test_solve_KKT_m + test_solve_KKT_n)
test_solve_KKT_x = spla.splu(test_solve_KKT_KKT.tocsc()).solve(test_solve_KKT_rhs)
test_solve_KKT_x = spla.splu(test_solve_KKT_KKT).solve(test_solve_KKT_rhs)

test_solve_KKT_x[test_solve_KKT_n:] = test_solve_KKT_rhs[test_solve_KKT_n:] + \
test_solve_KKT_x[test_solve_KKT_n:] / test_solve_KKT_rho
Expand Down
2 changes: 1 addition & 1 deletion tests/unconstrained/generate_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from scipy import sparse
import utils.codegen_utils as cu

P = sparse.diags([0.617022, 0.92032449, 0.20011437, 0.50233257, 0.34675589]).tocsc()
P = sparse.diags([0.617022, 0.92032449, 0.20011437, 0.50233257, 0.34675589], format='csc')
q = np.array([-1.10593508, -1.65451545, -2.3634686, 1.13534535, -1.01701414])
A = sparse.csc_matrix((0,5))
l = np.array([])
Expand Down
16 changes: 9 additions & 7 deletions tests/update_matrices/generate_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@
test_form_KKT_m = m
p = 0.7

test_form_KKT_A = sparse.random(test_form_KKT_m, test_form_KKT_n, density=p).tocsc()
test_form_KKT_A = sparse.random(test_form_KKT_m, test_form_KKT_n, density=p, format='csc')
test_form_KKT_P = sparse.random(n, n, density=p)
test_form_KKT_P = test_form_KKT_P.dot(test_form_KKT_P.T).tocsc() + sparse.eye(n).tocsc()
test_form_KKT_Pu = sparse.triu(test_form_KKT_P).tocsc()
test_form_KKT_P = test_form_KKT_P.dot(test_form_KKT_P.T).tocsc() + sparse.eye(n, format='csc')
test_form_KKT_Pu = sparse.triu(test_form_KKT_P, format='csc')
test_form_KKT_rho = 1.6
test_form_KKT_sigma = 0.1
test_form_KKT_KKT = sparse.vstack([
sparse.hstack([test_form_KKT_P + test_form_KKT_sigma *
sparse.eye(test_form_KKT_n), test_form_KKT_A.T]),
sparse.hstack([test_form_KKT_A,
-1./test_form_KKT_rho * sparse.eye(test_form_KKT_m)])]).tocsc()
test_form_KKT_KKTu = sparse.triu(test_form_KKT_KKT).tocsc()
-1./test_form_KKT_rho * sparse.eye(test_form_KKT_m)])
], format='csc')
test_form_KKT_KKTu = sparse.triu(test_form_KKT_KKT, format='csc')


# Create new P, A and KKT
Expand All @@ -38,8 +39,9 @@
sparse.hstack([test_form_KKT_P_new + test_form_KKT_sigma *
sparse.eye(test_form_KKT_n), test_form_KKT_A_new.T]),
sparse.hstack([test_form_KKT_A_new,
-1./test_form_KKT_rho * sparse.eye(test_form_KKT_m)])]).tocsc()
test_form_KKT_KKTu_new = sparse.triu(test_form_KKT_KKT_new).tocsc()
-1./test_form_KKT_rho * sparse.eye(test_form_KKT_m)])
], format='csc')
test_form_KKT_KKTu_new = sparse.triu(test_form_KKT_KKT_new, format='csc')


# Test solve problem with initial P and A
Expand Down

0 comments on commit b566609

Please sign in to comment.