Browse Source

fix & extract matrix row __str__

refactoring
Tom Janson 8 years ago
parent
commit
8d93227c72
  1. 13
      src/helpers.h
  2. 16
      src/storage/matrix.cpp

13
src/helpers.h

@ -16,12 +16,23 @@
* Used for __str__ functions.
*/
template<typename T>
std::string streamToString(T const & t) {
std::string streamToString(T const& t) {
std::stringstream ss;
ss << t;
return ss.str();
}
template<typename T>
std::string containerToString(T& t) {
// is there a way to make ^this const&?
// I guess not all containers have const iterators
std::stringstream ss;
for (auto const& e : t) {
ss << e << ", ";
}
return ss.str();
}
// Be warned: Enabling something like this will break everything about Monomial,
// as to Python the shared_ptr (Arg) IS the Monomial
// //PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>);

16
src/storage/matrix.cpp

@ -122,25 +122,13 @@ void define_sparse_matrix(py::module& m) {
.def("__iter__", [](storm::storage::SparseMatrix<double>::rows& rows) {
return py::make_iterator(rows.begin(), rows.end());
}, py::keep_alive<0, 1>())
.def("__str__", [](storm::storage::SparseMatrix<double>::const_rows& rows) {
std::stringstream stream;
for (auto transition : rows) {
stream << transition << ", ";
}
return stream.str();
})
.def("__str__", &containerToString<storm::storage::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) {
return py::make_iterator(rows.begin(), rows.end());
}, py::keep_alive<0, 1>())
.def("__str__", [](storm::storage::SparseMatrix<storm::RationalFunction>::const_rows& rows) {
std::stringstream stream;
for (auto transition : rows) {
stream << transition << ", ";
}
return stream.str();
})
.def("__str__", &containerToString<storm::storage::SparseMatrix<storm::RationalFunction>::rows>)
;
}
Loading…
Cancel
Save