diff --git a/src/storage/matrix.cpp b/src/storage/matrix.cpp index 1d03384..b0e06da 100644 --- a/src/storage/matrix.cpp +++ b/src/storage/matrix.cpp @@ -2,47 +2,48 @@ #include "storm/storage/SparseMatrix.h" #include "src/helpers.h" -typedef storm::storage::SparseMatrix::index_type entry_index; -typedef unsigned int row_index; -typedef storm::storage::SparseMatrix::index_type parametric_entry_index; -typedef unsigned int parametric_row_index; +template using SparseMatrix = storm::storage::SparseMatrix; +template using entry_index = typename storm::storage::SparseMatrix::index_type; +template using MatrixEntry = storm::storage::MatrixEntry, ValueType>; +using RationalFunction = storm::RationalFunction; +using row_index = unsigned int; void define_sparse_matrix(py::module& m) { // MatrixEntry - py::class_>(m, "SparseMatrixEntry", "Entry of sparse matrix") - .def("__str__", &streamToString>) + py::class_>(m, "SparseMatrixEntry", "Entry of sparse matrix") + .def("__str__", &streamToString>) //def_property threw "pointer being freed not allocated" after exiting - .def("value", &storm::storage::MatrixEntry::getValue, "Value") - .def("set_value", &storm::storage::MatrixEntry::setValue, py::arg("value"), "Set value") - .def_property_readonly("column", &storm::storage::MatrixEntry::getColumn, "Column") + .def("value", &MatrixEntry::getValue, "Value") + .def("set_value", &MatrixEntry::setValue, py::arg("value"), "Set value") + .def_property_readonly("column", &MatrixEntry::getColumn, "Column") ; - py::class_>(m, "ParametricSparseMatrixEntry", "Entry of parametric sparse matrix") - .def("__str__", &streamToString>) + py::class_>(m, "ParametricSparseMatrixEntry", "Entry of parametric sparse matrix") + .def("__str__", &streamToString>) //def_property threw "pointer being freed not allocated" after exiting - .def("value", &storm::storage::MatrixEntry::getValue, "Value") - .def("set_value", &storm::storage::MatrixEntry::setValue, py::arg("value"), "Set value") - .def_property_readonly("column", &storm::storage::MatrixEntry::getColumn, "Column") + .def("value", &MatrixEntry::getValue, "Value") + .def("set_value", &MatrixEntry::setValue, py::arg("value"), "Set value") + .def_property_readonly("column", &MatrixEntry::getColumn, "Column") ; // SparseMatrix - py::class_>(m, "SparseMatrix", "Sparse matrix") - .def("__iter__", [](storm::storage::SparseMatrix& matrix) { + py::class_>(m, "SparseMatrix", "Sparse matrix") + .def("__iter__", [](SparseMatrix& matrix) { return py::make_iterator(matrix.begin(), matrix.end()); }, py::keep_alive<0, 1>() /* Essential: keep object alive while iterator exists */) - .def("__str__", &streamToString>) - .def_property_readonly("nr_rows", &storm::storage::SparseMatrix::getRowCount, "Number of rows") - .def_property_readonly("nr_columns", &storm::storage::SparseMatrix::getColumnCount, "Number of columns") - .def_property_readonly("nr_entries", &storm::storage::SparseMatrix::getEntryCount, "Number of non-zero entries") - .def_property_readonly("_row_group_indices", &storm::storage::SparseMatrix::getRowGroupIndices, "Starting rows of row groups") - .def("get_row", [](storm::storage::SparseMatrix& matrix, entry_index row) { + .def("__str__", &streamToString>) + .def_property_readonly("nr_rows", &SparseMatrix::getRowCount, "Number of rows") + .def_property_readonly("nr_columns", &SparseMatrix::getColumnCount, "Number of columns") + .def_property_readonly("nr_entries", &SparseMatrix::getEntryCount, "Number of non-zero entries") + .def_property_readonly("_row_group_indices", &SparseMatrix::getRowGroupIndices, "Starting rows of row groups") + .def("get_row", [](SparseMatrix& matrix, entry_index row) { return matrix.getRows(row, row+1); }, py::return_value_policy::reference, py::keep_alive<1, 0>(), py::arg("row"), "Get row") - .def("get_rows", [](storm::storage::SparseMatrix& matrix, entry_index start, entry_index end) { + .def("get_rows", [](SparseMatrix& matrix, entry_index start, entry_index end) { return matrix.getRows(start, end); }, py::return_value_policy::reference, py::keep_alive<1, 0>(), py::arg("row_start"), py::arg("row_end"), "Get rows from start to end") - .def("print_row", [](storm::storage::SparseMatrix const& matrix, entry_index row) { + .def("print_row", [](SparseMatrix const& matrix, entry_index row) { std::stringstream stream; auto rows = matrix.getRows(row, row+1); for (auto transition : rows) { @@ -51,18 +52,18 @@ void define_sparse_matrix(py::module& m) { return stream.str(); }, py::arg("row"), "Print row") // Entry_index lead to problems - .def("row_iter", [](storm::storage::SparseMatrix& matrix, row_index start, row_index end) { + .def("row_iter", [](SparseMatrix& matrix, row_index start, row_index end) { return py::make_iterator(matrix.begin(start), matrix.end(end)); }, py::keep_alive<0, 1>() /* keep object alive while iterator exists */, py::arg("row_start"), py::arg("row_end"), "Get iterator from start to end") // (partial) container interface to allow e.g. matrix[7:9] - .def("__len__", &storm::storage::SparseMatrix::getRowCount) - .def("__getitem__", [](storm::storage::SparseMatrix& matrix, entry_index i) { + .def("__len__", &SparseMatrix::getRowCount) + .def("__getitem__", [](SparseMatrix& matrix, entry_index i) { if (i >= matrix.getRowCount()) throw py::index_error(); return matrix.getRows(i, i+1); }, py::return_value_policy::reference, py::keep_alive<1, 0>()) - .def("__getitem__", [](storm::storage::SparseMatrix& matrix, py::slice slice) { + .def("__getitem__", [](SparseMatrix& matrix, py::slice slice) { size_t start, stop, step, slice_length; if (!slice.compute(matrix.getRowCount(), &start, &stop, &step, &slice_length)) throw py::error_already_set(); @@ -72,22 +73,22 @@ void define_sparse_matrix(py::module& m) { }, py::return_value_policy::reference, py::keep_alive<1, 0>()) ; - py::class_>(m, "ParametricSparseMatrix", "Parametric sparse matrix") - .def("__iter__", [](storm::storage::SparseMatrix& matrix) { + py::class_>(m, "ParametricSparseMatrix", "Parametric sparse matrix") + .def("__iter__", [](SparseMatrix& matrix) { return py::make_iterator(matrix.begin(), matrix.end()); }, py::keep_alive<0, 1>() /* Essential: keep object alive while iterator exists */) - .def("__str__", &streamToString>) - .def_property_readonly("nr_rows", &storm::storage::SparseMatrix::getRowCount, "Number of rows") - .def_property_readonly("nr_columns", &storm::storage::SparseMatrix::getColumnCount, "Number of columns") - .def_property_readonly("nr_entries", &storm::storage::SparseMatrix::getEntryCount, "Number of non-zero entries") - .def_property_readonly("_row_group_indices", &storm::storage::SparseMatrix::getRowGroupIndices, "Starting rows of row groups") - .def("get_row", [](storm::storage::SparseMatrix& matrix, parametric_entry_index row) { + .def("__str__", &streamToString>) + .def_property_readonly("nr_rows", &SparseMatrix::getRowCount, "Number of rows") + .def_property_readonly("nr_columns", &SparseMatrix::getColumnCount, "Number of columns") + .def_property_readonly("nr_entries", &SparseMatrix::getEntryCount, "Number of non-zero entries") + .def_property_readonly("_row_group_indices", &SparseMatrix::getRowGroupIndices, "Starting rows of row groups") + .def("get_row", [](SparseMatrix& matrix, entry_index row) { return matrix.getRows(row, row+1); }, py::return_value_policy::reference, py::keep_alive<1, 0>(), py::arg("row"), "Get row") - .def("get_rows", [](storm::storage::SparseMatrix& matrix, parametric_entry_index start, parametric_entry_index end) { + .def("get_rows", [](SparseMatrix& matrix, entry_index start, entry_index end) { return matrix.getRows(start, end); }, py::return_value_policy::reference, py::keep_alive<1, 0>(), py::arg("row_start"), py::arg("row_end"), "Get rows from start to end") - .def("print_row", [](storm::storage::SparseMatrix const& matrix, parametric_entry_index row) { + .def("print_row", [](SparseMatrix const& matrix, entry_index row) { std::stringstream stream; auto rows = matrix.getRows(row, row+1); for (auto transition : rows) { @@ -96,18 +97,18 @@ void define_sparse_matrix(py::module& m) { return stream.str(); }, py::arg("row"), "Print row") // Entry_index lead to problems - .def("row_iter", [](storm::storage::SparseMatrix& matrix, parametric_row_index start, parametric_row_index end) { + .def("row_iter", [](SparseMatrix& matrix, row_index start, row_index end) { return py::make_iterator(matrix.begin(start), matrix.end(end)); }, py::keep_alive<0, 1>() /* keep object alive while iterator exists */, py::arg("row_start"), py::arg("row_end"), "Get iterator from start to end") // (partial) container interface to allow e.g. matrix[7:9] - .def("__len__", &storm::storage::SparseMatrix::getRowCount) - .def("__getitem__", [](storm::storage::SparseMatrix& matrix, parametric_entry_index i) { + .def("__len__", &SparseMatrix::getRowCount) + .def("__getitem__", [](SparseMatrix& matrix, entry_index i) { if (i >= matrix.getRowCount()) throw py::index_error(); return matrix.getRows(i, i+1); }, py::return_value_policy::reference, py::keep_alive<1, 0>()) - .def("__getitem__", [](storm::storage::SparseMatrix& matrix, py::slice slice) { + .def("__getitem__", [](SparseMatrix& matrix, py::slice slice) { size_t start, stop, step, slice_length; if (!slice.compute(matrix.getRowCount(), &start, &stop, &step, &slice_length)) throw py::error_already_set(); @@ -118,17 +119,17 @@ void define_sparse_matrix(py::module& m) { ; // Rows - py::class_::rows>(m, "SparseMatrixRows", "Set of rows in a sparse matrix") - .def("__iter__", [](storm::storage::SparseMatrix::rows& rows) { + py::class_::rows>(m, "SparseMatrixRows", "Set of rows in a sparse matrix") + .def("__iter__", [](SparseMatrix::rows& rows) { return py::make_iterator(rows.begin(), rows.end()); }, py::keep_alive<0, 1>()) - .def("__str__", &containerToString::rows>) + .def("__str__", &containerToString::rows>) ; - py::class_::rows>(m, "ParametricSparseMatrixRows", "Set of rows in a parametric sparse matrix") - .def("__iter__", [](storm::storage::SparseMatrix::rows& rows) { + py::class_::rows>(m, "ParametricSparseMatrixRows", "Set of rows in a parametric sparse matrix") + .def("__iter__", [](SparseMatrix::rows& rows) { return py::make_iterator(rows.begin(), rows.end()); }, py::keep_alive<0, 1>()) - .def("__str__", &containerToString::rows>) + .def("__str__", &containerToString::rows>) ; }