| 251 | } |
| 252 | |
| 253 | void HighsSparseMatrix::addCols(const HighsSparseMatrix new_cols, |
| 254 | const int8_t* in_partition) { |
| 255 | assert(new_cols.isColwise()); |
| 256 | const HighsInt num_new_col = new_cols.num_col_; |
| 257 | const HighsInt num_new_nz = new_cols.numNz(); |
| 258 | const vector<HighsInt>& new_matrix_start = new_cols.start_; |
| 259 | const vector<HighsInt>& new_matrix_index = new_cols.index_; |
| 260 | const vector<double>& new_matrix_value = new_cols.value_; |
| 261 | |
| 262 | assert(this->formatOk()); |
| 263 | // Adding columns to a row-wise partitioned matrix needs the |
| 264 | // partition information |
| 265 | const bool partitioned = this->format_ == MatrixFormat::kRowwisePartitioned; |
| 266 | // Cannot handle the row-wise partitioned case |
| 267 | assert(!partitioned); |
| 268 | if (partitioned) { |
| 269 | // if (in_partition == NULL) { printf("in_partition == NULL\n"); } |
| 270 | assert(in_partition != NULL); |
| 271 | } |
| 272 | assert(num_new_col >= 0); |
| 273 | assert(num_new_nz >= 0); |
| 274 | if (num_new_col == 0) { |
| 275 | // No columns are being added, so check that no nonzeros are being |
| 276 | // added |
| 277 | assert(num_new_nz == 0); |
| 278 | return; |
| 279 | } |
| 280 | // Adding a positive number of columns to a matrix |
| 281 | if (num_new_nz) { |
| 282 | // Nonzeros are being added, so ensure that non-null data are |
| 283 | // being passed |
| 284 | assert(!new_matrix_start.empty()); |
| 285 | assert(!new_matrix_index.empty()); |
| 286 | assert(!new_matrix_value.empty()); |
| 287 | } |
| 288 | HighsInt num_col = this->num_col_; |
| 289 | HighsInt num_row = this->num_row_; |
| 290 | HighsInt num_nz = this->numNz(); |
| 291 | // Check that nonzeros aren't being appended to a matrix with no rows |
| 292 | assert(num_new_nz <= 0 || num_row > 0); |
| 293 | |
| 294 | // If matrix is currently a standard row-wise matrix and there are |
| 295 | // more new nonzeros than current nonzeros so flip column-wise |
| 296 | if (this->format_ == MatrixFormat::kRowwise && num_new_nz > num_nz) |
| 297 | this->ensureColwise(); |
| 298 | |
| 299 | // Determine the new number of columns and nonzeros in the matrix |
| 300 | HighsInt new_num_col = num_col + num_new_col; |
| 301 | HighsInt new_num_nz = num_nz + num_new_nz; |
| 302 | |
| 303 | if (this->isColwise()) { |
| 304 | // Matrix is column-wise |
| 305 | this->start_.resize(new_num_col + 1); |
| 306 | // Append the starts of the new columns |
| 307 | if (num_new_nz) { |
| 308 | // Nontrivial number of nonzeros being added, so use new_matrix_start |
| 309 | for (HighsInt iNewCol = 0; iNewCol < num_new_col; iNewCol++) |
| 310 | this->start_[num_col + iNewCol] = num_nz + new_matrix_start[iNewCol]; |