From 89821af4d0e0ec0f81a2b23b20637b94beee8ae3 Mon Sep 17 00:00:00 2001 From: JAJHall Date: Sun, 5 Jan 2025 16:32:40 +0000 Subject: [PATCH] Added getReducedRow and getReducedColumn and sparse variants to highspy --- src/highs_bindings.cpp | 64 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/src/highs_bindings.cpp b/src/highs_bindings.cpp index c0c96e3acd..4e5ebaff79 100644 --- a/src/highs_bindings.cpp +++ b/src/highs_bindings.cpp @@ -317,6 +317,62 @@ highs_getBasisTransposeSolveSparse(Highs* h, dense_array_t rhs) { return std::make_tuple(status, py::cast(solution_vector), solution_num_nz, py::cast(solution_index)); } +std::tuple> +highs_getReducedRow(Highs* h, HighsInt row) { + HighsInt num_col = h->getNumCol(); + HighsInt num_row = h->getNumRow(); + + HighsStatus status = HighsStatus::kOk; + std::vector solution_vector(num_col); + double* solution_vector_ptr = static_cast(solution_vector.data()); + + if (num_row > 0) status = h->getReducedRow(row, solution_vector_ptr); + return std::make_tuple(status, py::cast(solution_vector)); +} + +std::tuple, HighsInt, dense_array_t> +highs_getReducedRowSparse(Highs* h, HighsInt row) { + HighsInt num_col = h->getNumCol(); + HighsInt num_row = h->getNumRow(); + + HighsStatus status = HighsStatus::kOk; + HighsInt solution_num_nz = 0; + std::vector solution_vector(num_row); + std::vector solution_index(num_row); + double* solution_vector_ptr = static_cast(solution_vector.data()); + HighsInt* solution_index_ptr = static_cast(solution_index.data()); + + if (num_row > 0) status = h->getReducedRow(row, solution_vector_ptr, &solution_num_nz, solution_index_ptr); + return std::make_tuple(status, py::cast(solution_vector), solution_num_nz, py::cast(solution_index)); +} + +std::tuple> +highs_getReducedColumn(Highs* h, HighsInt col) { + HighsInt num_row = h->getNumRow(); + + HighsStatus status = HighsStatus::kOk; + std::vector solution_vector(num_row); + double* solution_vector_ptr = static_cast(solution_vector.data()); + + if (num_row > 0) status = h->getReducedColumn(col, solution_vector_ptr); + return std::make_tuple(status, py::cast(solution_vector)); +} + +std::tuple, HighsInt, dense_array_t> +highs_getReducedColumnSparse(Highs* h, HighsInt col) { + HighsInt num_row = h->getNumRow(); + + HighsStatus status = HighsStatus::kOk; + HighsInt solution_num_nz = 0; + std::vector solution_vector(num_row); + std::vector solution_index(num_row); + double* solution_vector_ptr = static_cast(solution_vector.data()); + HighsInt* solution_index_ptr = static_cast(solution_index.data()); + + if (num_row > 0) status = h->getReducedColumn(col, solution_vector_ptr, &solution_num_nz, solution_index_ptr); + return std::make_tuple(status, py::cast(solution_vector), solution_num_nz, py::cast(solution_index)); +} + HighsStatus highs_addRow(Highs* h, double lower, double upper, HighsInt num_new_nz, dense_array_t indices, dense_array_t values) { @@ -1176,10 +1232,10 @@ PYBIND11_MODULE(_core, m, py::mod_gil_not_used()) { .def("getBasisSolveSparse", &highs_getBasisSolveSparse) .def("getBasisTransposeSolve", &highs_getBasisTransposeSolve) .def("getBasisTransposeSolveSparse", &highs_getBasisTransposeSolveSparse) - // .def("getReducedRow", &highs_getReducedRow) - // .def("getReducedRowSparse", &highs_getReducedRowSparse) - // .def("getReducedColumn", &highs_getReducedColumn) - // .def("getReducedColumnSparse", &highs_getReducedColumnSparse) + .def("getReducedRow", &highs_getReducedRow) + .def("getReducedRowSparse", &highs_getReducedRowSparse) + .def("getReducedColumn", &highs_getReducedColumn) + .def("getReducedColumnSparse", &highs_getReducedColumnSparse) .def("getNumCol", &Highs::getNumCol) .def("getNumRow", &Highs::getNumRow) .def("getNumNz", &Highs::getNumNz)