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_transpose(pbqp *pbqp, pbqp_matrix *mat)
60 len = mat->rows * mat->cols;
62 pbqp_matrix *tmp = pbqp_matrix_copy_and_transpose(pbqp, mat);
64 memcpy(mat, tmp, sizeof(*mat) + sizeof(*mat->entries) * len);
66 obstack_free(&pbqp->obstack, tmp);
69 void pbqp_matrix_add(pbqp_matrix *sum, pbqp_matrix *summand)
76 assert(sum->cols == summand->cols);
77 assert(sum->rows == summand->rows);
79 len = sum->rows * sum->cols;
81 for (i = 0; i < len; ++i) {
82 sum->entries[i] = pbqp_add(sum->entries[i], summand->entries[i]);
86 void pbqp_matrix_set_col_value(pbqp_matrix *mat, unsigned col, num value)
92 assert(col < mat->cols);
96 for (row_index = 0; row_index < row_len; ++row_index) {
97 mat->entries[row_index * mat->cols + col] = value;
101 void pbqp_matrix_set_row_value(pbqp_matrix *mat, unsigned row, num value)
107 assert(row < mat->rows);
111 for (col_index = 0; col_index < col_len; ++col_index) {
112 mat->entries[row * mat->cols + col_index] = value;
116 void pbqp_matrix_set(pbqp_matrix *mat, unsigned row, unsigned col, num value)
119 assert(col < mat->cols);
120 assert(row < mat->rows);
122 mat->entries[row * mat->cols + col] = value;
125 num pbqp_matrix_get_col_min(pbqp_matrix *matrix, unsigned col_index, vector *flags)
132 assert(matrix->rows == flags->len);
134 unsigned col_len = matrix->cols;
135 unsigned row_len = matrix->rows;
137 for (row_index = 0; row_index < row_len; ++row_index) {
138 /* Ignore virtual deleted columns. */
139 if (flags->entries[row_index].data == INF_COSTS) continue;
141 num elem = matrix->entries[row_index * col_len + col_index];
151 void pbqp_matrix_sub_col_value(pbqp_matrix *matrix, unsigned col_index,
152 vector *flags, num value)
160 assert(matrix->rows == flags->len);
162 col_len = matrix->cols;
163 row_len = matrix->rows;
165 for (row_index = 0; row_index < row_len; ++row_index) {
166 if (flags->entries[row_index].data == INF_COSTS) {
167 matrix->entries[row_index * col_len + col_index] = 0;
170 /* inf - x = inf if x < inf */
171 if (matrix->entries[row_index * col_len + col_index] == INF_COSTS && value
174 matrix->entries[row_index * col_len + col_index] -= value;
178 num pbqp_matrix_get_row_min(pbqp_matrix *matrix, unsigned row_index, vector *flags)
185 assert(matrix->cols == flags->len);
187 unsigned len = flags->len;
189 for (col_index = 0; col_index < len; ++col_index) {
190 /* Ignore virtual deleted columns. */
191 if (flags->entries[col_index].data == INF_COSTS) continue;
193 num elem = matrix->entries[row_index * len + col_index];
203 void pbqp_matrix_sub_row_value(pbqp_matrix *matrix, unsigned row_index,
204 vector *flags, num value)
211 assert(matrix->cols == flags->len);
213 col_len = matrix->cols;
215 for (col_index = 0; col_index < col_len; ++col_index) {
216 if (flags->entries[col_index].data == INF_COSTS) {
217 matrix->entries[row_index * col_len + col_index] = 0;
220 /* inf - x = inf if x < inf */
221 if (matrix->entries[row_index * col_len + col_index] == INF_COSTS && value
224 matrix->entries[row_index * col_len + col_index] -= value;
228 int pbqp_matrix_is_zero(pbqp_matrix *mat, vector *src_vec, vector *tgt_vec)
238 assert(mat->cols = tgt_vec->len);
239 assert(mat->rows = src_vec->len);
244 for (row_index = 0; row_index < row_len; ++row_index) {
245 if (src_vec->entries[row_index].data == INF_COSTS) continue;
246 for (col_index = 0; col_index < col_len; ++col_index) {
247 if (tgt_vec->entries[col_index].data == INF_COSTS) continue;
249 if (mat->entries[row_index * col_len + col_index] != 0) {
258 void pbqp_matrix_add_to_all_cols(pbqp_matrix *mat, vector *vec)
267 assert(mat->rows == vec->len);
272 for (row_index = 0; row_index < row_len; ++row_index) {
273 num value = vec->entries[row_index].data;
274 for (col_index = 0; col_index < col_len; ++col_index) {
275 mat->entries[row_index * col_len + col_index] = pbqp_add(
276 mat->entries[row_index * col_len + col_index], value);
281 void pbqp_matrix_add_to_all_rows(pbqp_matrix *mat, vector *vec)
290 assert(mat->cols == vec->len);
295 for (row_index = 0; row_index < row_len; ++row_index) {
296 for (col_index = 0; col_index < col_len; ++col_index) {
297 num value = vec->entries[col_index].data;
299 mat->entries[row_index * col_len + col_index] = pbqp_add(mat->entries[row_index * col_len + col_index], value);