7 pbqp_matrix *pbqp_matrix_alloc(pbqp *pbqp, unsigned rows, unsigned cols)
12 unsigned length = rows * cols;
14 pbqp_matrix *mat = obstack_alloc(&pbqp->obstack, sizeof(*mat) + sizeof(*mat->entries) * length);
19 memset(mat->entries, 0, sizeof(*mat->entries) * length);
24 pbqp_matrix *pbqp_matrix_copy(pbqp *pbqp, pbqp_matrix *m)
26 unsigned len = m->rows * m->cols;
27 pbqp_matrix *copy = obstack_copy(&pbqp->obstack, m, sizeof(*copy) + sizeof(*copy->entries) * len);
33 pbqp_matrix *pbqp_matrix_copy_and_transpose(pbqp *pbqp, pbqp_matrix *m)
37 unsigned cols = m->cols;
38 unsigned rows = m->rows;
39 unsigned len = rows * cols;
40 pbqp_matrix *copy = obstack_alloc(&pbqp->obstack, sizeof(*copy) + sizeof(*copy->entries) * len);
43 for (i = 0; i < rows; ++i) {
44 for (j = 0; j < cols; ++j) {
45 copy->entries[j*rows+i] = m->entries[i*cols+j];
55 void pbqp_matrix_add(pbqp_matrix *sum, pbqp_matrix *summand)
62 assert(sum->cols == summand->cols);
63 assert(sum->rows == summand->rows);
65 len = sum->rows * sum->cols;
67 for (i = 0; i < len; ++i) {
68 sum->entries[i] += summand->entries[i];
72 void pbqp_matrix_set(pbqp_matrix *mat, unsigned row, unsigned col, num value)
75 assert(col < mat->cols);
76 assert(row < mat->rows);
78 mat->entries[row * mat->cols + col] = value;
81 num pbqp_matrix_get_col_min(pbqp_matrix *matrix, unsigned col_index, vector *flags)
88 assert(matrix->rows == flags->len);
90 unsigned col_len = matrix->cols;
91 unsigned row_len = matrix->rows;
93 for (row_index = 0; row_index < row_len; ++row_index) {
94 /* Ignore virtual deleted columns. */
95 if (flags->entries[row_index].data == INF_COSTS) continue;
97 num elem = matrix->entries[row_index * col_len + col_index];
107 void pbqp_matrix_sub_col_value(pbqp_matrix *matrix, unsigned col_index,
108 vector *flags, num value)
114 assert(matrix->rows == flags->len);
116 unsigned col_len = matrix->cols;
117 unsigned row_len = matrix->rows;
119 for (row_index = 0; row_index < row_len; ++row_index) {
120 /* inf - x = inf if x < inf */
121 if (matrix->entries[row_index * col_len + col_index] == INF_COSTS && value
124 matrix->entries[row_index * col_len + col_index] -= value;
128 num pbqp_matrix_get_row_min(pbqp_matrix *matrix, unsigned row_index, vector *flags)
135 assert(matrix->cols == flags->len);
137 unsigned len = flags->len;
139 for (col_index = 0; col_index < len; ++col_index) {
140 /* Ignore virtual deleted columns. */
141 if (flags->entries[col_index].data == INF_COSTS) continue;
143 num elem = matrix->entries[row_index * len + col_index];
153 void pbqp_matrix_sub_row_value(pbqp_matrix *matrix, unsigned row_index,
154 vector *flags, num value)
160 assert(matrix->cols == flags->len);
162 unsigned len = flags->len;
164 for (col_index = 0; col_index < len; ++col_index) {
165 /* inf - x = inf if x < inf */
166 if (matrix->entries[row_index * len + col_index] == INF_COSTS && value
169 matrix->entries[row_index * len + col_index] -= value;
173 int pbqp_matrix_is_zero(pbqp_matrix *mat, vector *src_vec, vector *tgt_vec)
183 assert(mat->cols = tgt_vec->len);
184 assert(mat->rows = src_vec->len);
189 for (row_index = 0; row_index < row_len; ++row_index) {
190 if (src_vec->entries[row_index].data == INF_COSTS) continue;
191 for (col_index = 0; col_index < col_len; ++col_index) {
192 if (tgt_vec->entries[col_index].data == INF_COSTS) continue;
194 if (mat->entries[row_index * col_len + col_index] != 0) {