8 pbqp_matrix *pbqp_matrix_alloc(pbqp *pbqp, unsigned rows, unsigned cols)
13 unsigned length = rows * cols;
15 pbqp_matrix *mat = obstack_alloc(&pbqp->obstack, sizeof(*mat) + sizeof(*mat->entries) * length);
20 memset(mat->entries, 0, sizeof(*mat->entries) * length);
25 pbqp_matrix *pbqp_matrix_copy(pbqp *pbqp, pbqp_matrix *m)
27 unsigned len = m->rows * m->cols;
28 pbqp_matrix *copy = obstack_copy(&pbqp->obstack, m, sizeof(*copy) + sizeof(*copy->entries) * len);
34 pbqp_matrix *pbqp_matrix_copy_and_transpose(pbqp *pbqp, pbqp_matrix *m)
38 unsigned cols = m->cols;
39 unsigned rows = m->rows;
40 unsigned len = rows * cols;
41 pbqp_matrix *copy = obstack_alloc(&pbqp->obstack, sizeof(*copy) + sizeof(*copy->entries) * len);
44 for (i = 0; i < rows; ++i) {
45 for (j = 0; j < cols; ++j) {
46 copy->entries[j*rows+i] = m->entries[i*cols+j];
56 void pbqp_matrix_transpose(pbqp *pbqp, pbqp_matrix *mat)
61 len = mat->rows * mat->cols;
63 pbqp_matrix *tmp = pbqp_matrix_copy_and_transpose(pbqp, mat);
65 memcpy(mat, tmp, sizeof(*mat) + sizeof(*mat->entries) * len);
67 obstack_free(&pbqp->obstack, tmp);
70 void pbqp_matrix_add(pbqp_matrix *sum, pbqp_matrix *summand)
77 assert(sum->cols == summand->cols);
78 assert(sum->rows == summand->rows);
80 len = sum->rows * sum->cols;
82 for (i = 0; i < len; ++i) {
83 sum->entries[i] = pbqp_add(sum->entries[i], summand->entries[i]);
87 void pbqp_matrix_set_col_value(pbqp_matrix *mat, unsigned col, num value)
93 assert(col < mat->cols);
97 for (row_index = 0; row_index < row_len; ++row_index) {
98 mat->entries[row_index * mat->cols + col] = value;
102 void pbqp_matrix_set_row_value(pbqp_matrix *mat, unsigned row, num value)
108 assert(row < mat->rows);
112 for (col_index = 0; col_index < col_len; ++col_index) {
113 mat->entries[row * mat->cols + col_index] = value;
117 void pbqp_matrix_set(pbqp_matrix *mat, unsigned row, unsigned col, num value)
120 assert(col < mat->cols);
121 assert(row < mat->rows);
123 mat->entries[row * mat->cols + col] = value;
126 num pbqp_matrix_get_col_min(pbqp_matrix *matrix, unsigned col_index, vector *flags)
133 assert(matrix->rows == flags->len);
135 unsigned col_len = matrix->cols;
136 unsigned row_len = matrix->rows;
138 for (row_index = 0; row_index < row_len; ++row_index) {
139 /* Ignore virtual deleted columns. */
140 if (flags->entries[row_index].data == INF_COSTS) continue;
142 num elem = matrix->entries[row_index * col_len + col_index];
152 void pbqp_matrix_sub_col_value(pbqp_matrix *matrix, unsigned col_index,
153 vector *flags, num value)
161 assert(matrix->rows == flags->len);
163 col_len = matrix->cols;
164 row_len = matrix->rows;
166 for (row_index = 0; row_index < row_len; ++row_index) {
167 if (flags->entries[row_index].data == INF_COSTS) {
168 matrix->entries[row_index * col_len + col_index] = 0;
171 /* inf - x = inf if x < inf */
172 if (matrix->entries[row_index * col_len + col_index] == INF_COSTS && value
175 matrix->entries[row_index * col_len + col_index] -= value;
179 num pbqp_matrix_get_row_min(pbqp_matrix *matrix, unsigned row_index, vector *flags)
186 assert(matrix->cols == flags->len);
188 unsigned len = flags->len;
190 for (col_index = 0; col_index < len; ++col_index) {
191 /* Ignore virtual deleted columns. */
192 if (flags->entries[col_index].data == INF_COSTS) continue;
194 num elem = matrix->entries[row_index * len + col_index];
204 void pbqp_matrix_sub_row_value(pbqp_matrix *matrix, unsigned row_index,
205 vector *flags, num value)
212 assert(matrix->cols == flags->len);
214 col_len = matrix->cols;
216 for (col_index = 0; col_index < col_len; ++col_index) {
217 if (flags->entries[col_index].data == INF_COSTS) {
218 matrix->entries[row_index * col_len + col_index] = 0;
221 /* inf - x = inf if x < inf */
222 if (matrix->entries[row_index * col_len + col_index] == INF_COSTS && value
225 matrix->entries[row_index * col_len + col_index] -= value;
229 int pbqp_matrix_is_zero(pbqp_matrix *mat, vector *src_vec, vector *tgt_vec)
239 assert(mat->cols = tgt_vec->len);
240 assert(mat->rows = src_vec->len);
245 for (row_index = 0; row_index < row_len; ++row_index) {
246 if (src_vec->entries[row_index].data == INF_COSTS) continue;
247 for (col_index = 0; col_index < col_len; ++col_index) {
248 if (tgt_vec->entries[col_index].data == INF_COSTS) continue;
250 if (mat->entries[row_index * col_len + col_index] != 0) {
259 void pbqp_matrix_add_to_all_cols(pbqp_matrix *mat, vector *vec)
268 assert(mat->rows == vec->len);
273 for (row_index = 0; row_index < row_len; ++row_index) {
274 num value = vec->entries[row_index].data;
275 for (col_index = 0; col_index < col_len; ++col_index) {
276 mat->entries[row_index * col_len + col_index] = pbqp_add(
277 mat->entries[row_index * col_len + col_index], value);
282 void pbqp_matrix_add_to_all_rows(pbqp_matrix *mat, vector *vec)
291 assert(mat->cols == vec->len);
296 for (row_index = 0; row_index < row_len; ++row_index) {
297 for (col_index = 0; col_index < col_len; ++col_index) {
298 num value = vec->entries[col_index].data;
300 mat->entries[row_index * col_len + col_index] = pbqp_add(mat->entries[row_index * col_len + col_index], value);