Browse Source

use type alias to improve readability of matrix.cpp

refactoring
Tom Janson 8 years ago
parent
commit
0be192a0ae
  1. 97
      src/storage/matrix.cpp

97
src/storage/matrix.cpp

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