Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit a92d566

Browse files
authored
Improve the stream operator for matrices (#156)
Adds line breaks to the matrix output and aligns columns for better readability. Also sets the output of all vector and matrix elements to a fixed precision and scientific notation
1 parent 01cf491 commit a92d566

1 file changed

Lines changed: 7 additions & 10 deletions

File tree

utils/include/algebra/utils/print.hpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "algebra/qualifiers.hpp"
1313

1414
// System include(s).
15+
#include <iomanip>
1516
#include <iostream>
1617

1718
namespace algebra {
@@ -49,10 +50,8 @@ ALGEBRA_HOST std::ostream& operator<<(std::ostream& out, const vector_t& v) {
4950

5051
out << "[";
5152
for (index_t i = 0; i < rows; ++i) {
52-
out << element_getter_t{}(v, i, 0);
53-
if (i != rows - 1) {
54-
out << ", ";
55-
}
53+
// Account for the sign of negative elements
54+
out << std::setw(i == 0 ? 15 : 16) << element_getter_t{}(v, i, 0);
5655
}
5756
out << "]";
5857

@@ -71,16 +70,14 @@ ALGEBRA_HOST std::ostream& operator<<(std::ostream& out, const matrix_t& m) {
7170

7271
out << "[";
7372
for (index_t i = 0; i < rows; ++i) {
74-
out << "[";
73+
out << (i == 0 ? "[" : " [");
7574
for (index_t j = 0; j < columns; ++j) {
76-
out << element_getter_t{}(m, i, j);
77-
if (j != columns - 1) {
78-
out << ", ";
79-
}
75+
// Account for the sign of negative elements
76+
out << std::setw(j == 0 ? 15 : 16) << element_getter_t{}(m, i, j);
8077
}
8178
out << "]";
8279
if (i != rows - 1) {
83-
out << ", ";
80+
out << "\n";
8481
}
8582
}
8683
out << "]";

0 commit comments

Comments
 (0)