Skip to content

Commit b47721d

Browse files
authored
SmallMatrix.__repr__ like NumPy (#421)
Ensure that printing the `SmallMatrix` looks & feels like the equivalent in `numpy.ndarray`: ``` SmallMatrix([[1, 7, 13, 19, 25, 31], [2, 8, 14, 20, 26, 32], [3, 9, 15, 21, 27, 33], [4, 10, 16, 22, 28, 34], [5, 11, 17, 23, 29, 35], [6, 12, 18, 24, 30, 36]]) ```
1 parent f8746e7 commit b47721d

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/Base/SmallMatrix.H

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,22 @@ namespace
130130
py::class_< SM > py_sm(m, sm_name.c_str());
131131
py_sm
132132
.def("__repr__",
133-
[sm_name](SM const &) {
134-
return "<amrex." + sm_name + ">";
135-
}
136-
)
137-
.def("__str__",
138-
[sm_name](SM const & sm) {
133+
[](SM const & sm) {
134+
// similar look & feel like numpy.ndarray.__repr__
139135
std::stringstream ss;
140-
ss << sm;
141-
return ss.str();
136+
for (int i = sm.starting_index; i < sm.row_size + sm.starting_index; ++i) {
137+
if (i > sm.starting_index) { ss << ",\n "; }
138+
ss << "[";
139+
ss << sm(i, sm.starting_index);
140+
for (int j = 1 + sm.starting_index; j < sm.column_size + sm.starting_index; ++j) {
141+
ss << ", " << sm(i, j);
142+
}
143+
ss << "]";
144+
}
145+
using namespace std::string_literals;
146+
return "SmallMatrix(["s +
147+
ss.str() +
148+
"])"s;
142149
}
143150
)
144151

0 commit comments

Comments
 (0)