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(pbqp_matrix *mat, unsigned row, unsigned col, num value)
89 assert(col < mat->cols);
90 assert(row < mat->rows);
92 mat->entries[row * mat->cols + col] = value;
95 num pbqp_matrix_get_col_min(pbqp_matrix *matrix, unsigned col_index, vector *flags)
102 assert(matrix->rows == flags->len);
104 unsigned col_len = matrix->cols;
105 unsigned row_len = matrix->rows;
107 for (row_index = 0; row_index < row_len; ++row_index) {
108 /* Ignore virtual deleted columns. */
109 if (flags->entries[row_index].data == INF_COSTS) continue;
111 num elem = matrix->entries[row_index * col_len + col_index];
121 void pbqp_matrix_sub_col_value(pbqp_matrix *matrix, unsigned col_index,
122 vector *flags, num value)
128 assert(matrix->rows == flags->len);
130 unsigned col_len = matrix->cols;
131 unsigned row_len = matrix->rows;
133 for (row_index = 0; row_index < row_len; ++row_index) {
134 /* inf - x = inf if x < inf */
135 if (matrix->entries[row_index * col_len + col_index] == INF_COSTS && value
138 matrix->entries[row_index * col_len + col_index] -= value;
142 num pbqp_matrix_get_row_min(pbqp_matrix *matrix, unsigned row_index, vector *flags)
149 assert(matrix->cols == flags->len);
151 unsigned len = flags->len;
153 for (col_index = 0; col_index < len; ++col_index) {
154 /* Ignore virtual deleted columns. */
155 if (flags->entries[col_index].data == INF_COSTS) continue;
157 num elem = matrix->entries[row_index * len + col_index];
167 void pbqp_matrix_sub_row_value(pbqp_matrix *matrix, unsigned row_index,
168 vector *flags, num value)
174 assert(matrix->cols == flags->len);
176 unsigned len = flags->len;
178 for (col_index = 0; col_index < len; ++col_index) {
179 /* inf - x = inf if x < inf */
180 if (matrix->entries[row_index * len + col_index] == INF_COSTS && value
183 matrix->entries[row_index * len + col_index] -= value;
187 int pbqp_matrix_is_zero(pbqp_matrix *mat, vector *src_vec, vector *tgt_vec)
197 assert(mat->cols = tgt_vec->len);
198 assert(mat->rows = src_vec->len);
203 for (row_index = 0; row_index < row_len; ++row_index) {
204 if (src_vec->entries[row_index].data == INF_COSTS) continue;
205 for (col_index = 0; col_index < col_len; ++col_index) {
206 if (tgt_vec->entries[col_index].data == INF_COSTS) continue;
208 if (mat->entries[row_index * col_len + col_index] != 0) {
217 void pbqp_matrix_add_to_all_cols(pbqp_matrix *mat, vector *vec)
226 assert(mat->rows == vec->len);
231 for (row_index = 0; row_index < row_len; ++row_index) {
232 num value = vec->entries[row_index].data;
233 for (col_index = 0; col_index < col_len; ++col_index) {
234 mat->entries[row_index * col_len + col_index] = pbqp_add(
235 mat->entries[row_index * col_len + col_index], value);
240 void pbqp_matrix_add_to_all_rows(pbqp_matrix *mat, vector *vec)
249 assert(mat->cols == vec->len);
254 for (row_index = 0; row_index < row_len; ++row_index) {
255 for (col_index = 0; col_index < col_len; ++col_index) {
256 num value = vec->entries[col_index].data;
258 mat->entries[row_index * col_len + col_index] = pbqp_add(mat->entries[row_index * col_len + col_index], value);