Skip to content

Commit dea0fd7

Browse files
maxschallervineetbansal
authored andcommitted
Not writing empty arrays for the case of LPS. (#70)
1 parent d4fcca3 commit dea0fd7

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

src/osqp/codegen/utils.py

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,47 @@ def write_vec(f, vec, name, vec_type):
1919
"""
2020
Write vector to file
2121
"""
22-
f.write('%s %s[%d] = {\n' % (vec_type, name, len(vec)))
22+
if len(vec) > 0:
2323

24-
# Write vector elements
25-
for i in range(len(vec)):
26-
if vec_type == 'c_float':
27-
f.write('(c_float)%.20f,\n' % vec[i])
28-
else:
29-
f.write('%i,\n' % vec[i])
24+
f.write('%s %s[%d] = {\n' % (vec_type, name, len(vec)))
25+
26+
# Write vector elements
27+
for i in range(len(vec)):
28+
if vec_type == 'c_float':
29+
f.write('(c_float)%.20f,\n' % vec[i])
30+
else:
31+
f.write('%i,\n' % vec[i])
3032

31-
f.write('};\n')
33+
f.write('};\n')
3234

3335

3436
def write_vec_extern(f, vec, name, vec_type):
3537
"""
3638
Write vector prototype to file
3739
"""
38-
f.write("extern %s %s[%d];\n" % (vec_type, name, len(vec)))
40+
if len(vec) > 0:
41+
f.write("extern %s %s[%d];\n" % (vec_type, name, len(vec)))
3942

4043

4144
def write_mat(f, mat, name):
4245
"""
4346
Write scipy sparse matrix in CSC form to file
4447
"""
45-
write_vec(f, mat['i'], name + '_i', 'c_int')
4648
write_vec(f, mat['p'], name + '_p', 'c_int')
47-
write_vec(f, mat['x'], name + '_x', 'c_float')
49+
if len(mat['x']) > 0:
50+
write_vec(f, mat['i'], name + '_i', 'c_int')
51+
write_vec(f, mat['x'], name + '_x', 'c_float')
4852

4953
f.write("csc %s = {" % name)
5054
f.write("%d, " % mat['nzmax'])
5155
f.write("%d, " % mat['m'])
5256
f.write("%d, " % mat['n'])
5357
f.write("%s_p, " % name)
54-
f.write("%s_i, " % name)
55-
f.write("%s_x, " % name)
58+
if len(mat['x']) > 0:
59+
f.write("%s_i, " % name)
60+
f.write("%s_x, " % name)
61+
else:
62+
f.write("0, 0, ")
5663
f.write("%d};\n" % mat['nz'])
5764

5865

@@ -219,9 +226,20 @@ def write_linsys_solver_src(f, linsys_solver, embedded_flag):
219226
f.write("%d, " % linsys_solver['m'])
220227

221228
if embedded_flag != 1:
222-
f.write("linsys_solver_Pdiag_idx, ")
229+
if len(linsys_solver['Pdiag_idx']) > 0:
230+
linsys_solver_Pdiag_idx_string = 'linsys_solver_Pdiag_idx'
231+
linsys_solver_PtoKKT_string = 'linsys_solver_PtoKKT'
232+
else:
233+
linsys_solver_Pdiag_idx_string = '0'
234+
linsys_solver_PtoKKT_string = '0'
235+
if len(linsys_solver['AtoKKT']) > 0:
236+
linsys_solver_AtoKKT_string = 'linsys_solver_AtoKKT'
237+
else:
238+
linsys_solver_AtoKKT_string = '0'
239+
f.write("%s, " % linsys_solver_Pdiag_idx_string)
223240
f.write("%d, " % linsys_solver['Pdiag_n'])
224-
f.write("&linsys_solver_KKT, linsys_solver_PtoKKT, linsys_solver_AtoKKT, linsys_solver_rhotoKKT, " +
241+
f.write("&linsys_solver_KKT, %s, %s, linsys_solver_rhotoKKT, "
242+
% (linsys_solver_PtoKKT_string, linsys_solver_AtoKKT_string) +
225243
"linsys_solver_D, linsys_solver_etree, linsys_solver_Lnz, " +
226244
"linsys_solver_iwork, linsys_solver_bwork, linsys_solver_fwork, ")
227245

0 commit comments

Comments
 (0)