(
id_to_col: dict,
param_to_size: dict,
param_to_col: dict,
var_length: int,
constr_length: int,
linOps: List[lo.LinOp],
)
| 32 | |
| 33 | |
| 34 | def build_matrix( |
| 35 | id_to_col: dict, |
| 36 | param_to_size: dict, |
| 37 | param_to_col: dict, |
| 38 | var_length: int, |
| 39 | constr_length: int, |
| 40 | linOps: List[lo.LinOp], |
| 41 | ) -> sp.csc_array: |
| 42 | lin_vec = cvxcore.ConstLinOpVector() |
| 43 | |
| 44 | id_to_col_C = cvxcore.IntIntMap() |
| 45 | for id, col in id_to_col.items(): |
| 46 | id_to_col_C[int(id)] = int(col) |
| 47 | |
| 48 | param_to_size_C = cvxcore.IntIntMap() |
| 49 | for id, size in param_to_size.items(): |
| 50 | param_to_size_C[int(id)] = int(size) |
| 51 | |
| 52 | # dict to memoize construction of C++ linOps, and to keep Python references |
| 53 | # to them to prevent their deletion |
| 54 | linPy_to_linC = {} |
| 55 | for lin in linOps: |
| 56 | build_lin_op_tree(lin, linPy_to_linC) |
| 57 | tree = linPy_to_linC[lin] |
| 58 | lin_vec.push_back(tree) |
| 59 | |
| 60 | problemData = cvxcore.build_matrix( |
| 61 | lin_vec, int(var_length), id_to_col_C, param_to_size_C, s.get_num_threads() |
| 62 | ) |
| 63 | |
| 64 | # Populate tensors with info from problemData. |
| 65 | tensor_V = {} |
| 66 | tensor_I = {} |
| 67 | tensor_J = {} |
| 68 | for param_id, size in param_to_size.items(): |
| 69 | tensor_V[param_id] = [] |
| 70 | tensor_I[param_id] = [] |
| 71 | tensor_J[param_id] = [] |
| 72 | problemData.param_id = param_id |
| 73 | for i in range(size): |
| 74 | problemData.vec_idx = i |
| 75 | prob_len = problemData.getLen() |
| 76 | tensor_V[param_id].append(problemData.getV(prob_len)) |
| 77 | tensor_I[param_id].append(problemData.getI(prob_len)) |
| 78 | tensor_J[param_id].append(problemData.getJ(prob_len)) |
| 79 | |
| 80 | # Reduce tensors to a single sparse CSR matrix. |
| 81 | V = [] |
| 82 | I = [] |
| 83 | J = [] |
| 84 | # one of the 'parameters' in param_to_col is a constant scalar offset, |
| 85 | # hence 'plus_one' |
| 86 | param_size_plus_one = 0 |
| 87 | for param_id, col in param_to_col.items(): |
| 88 | size = param_to_size[param_id] |
| 89 | param_size_plus_one += size |
| 90 | for i in range(size): |
| 91 | V.append(tensor_V[param_id][i]) |
no test coverage detected