From d7bee2a4b305d34e0a1020aaef3eeb37b1cc3662 Mon Sep 17 00:00:00 2001 From: Evan Porter <115374841+evanwporter@users.noreply.github.com> Date: Tue, 2 Sep 2025 23:43:10 -0700 Subject: [PATCH 01/12] added fixed widths when printing a sparse matrix --- Eigen/src/SparseCore/SparseMatrixBase.h | 62 ++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/Eigen/src/SparseCore/SparseMatrixBase.h b/Eigen/src/SparseCore/SparseMatrixBase.h index 7ac16f834..31ea07ba4 100644 --- a/Eigen/src/SparseCore/SparseMatrixBase.h +++ b/Eigen/src/SparseCore/SparseMatrixBase.h @@ -225,35 +225,85 @@ class SparseMatrixBase : public EigenBase { #ifndef EIGEN_NO_IO friend std::ostream& operator<<(std::ostream& s, const SparseMatrixBase& m) { typedef typename Derived::Nested Nested; - typedef internal::remove_all_t NestedCleaned; + typedef typename internal::remove_all::type NestedCleaned; + + /// For converting `0's` to the matrices numerical type + typedef typename Derived::Scalar Scalar; if (Flags & RowMajorBit) { Nested nm(m.derived()); internal::evaluator thisEval(nm); + + // compute global width + Index width = 0; + { + std::ostringstream ss0; + ss0.copyfmt(s); + ss0 << Scalar(0); + width = Index(ss0.str().size()); + for (Index row = 0; row < nm.outerSize(); ++row) { + for (typename internal::evaluator::InnerIterator it(thisEval, row); it; ++it) { + std::ostringstream ss; + ss.copyfmt(s); + ss << it.value(); + width = std::max(width, Index(ss.str().size())); + } + } + } + for (Index row = 0; row < nm.outerSize(); ++row) { Index col = 0; for (typename internal::evaluator::InnerIterator it(thisEval, row); it; ++it) { - for (; col < it.index(); ++col) s << "0 "; + for (; col < it.index(); ++col) { + s.width(width); + s << Scalar(0) << " "; + } + s.width(width); s << it.value() << " "; ++col; } - for (; col < m.cols(); ++col) s << "0 "; + for (; col < m.cols(); ++col) { + s.width(width); + s << Scalar(0) << " "; + } s << std::endl; } } else { Nested nm(m.derived()); internal::evaluator thisEval(nm); if (m.cols() == 1) { + // compute local width (single col) + Index width = 0; + { + std::ostringstream ss0; + ss0.copyfmt(s); + ss0 << Scalar(0); + width = Index(ss0.str().size()); + for (typename internal::evaluator::InnerIterator it(thisEval, 0); it; ++it) { + std::ostringstream ss; + ss.copyfmt(s); + ss << it.value(); + width = std::max(width, Index(ss.str().size())); + } + } + Index row = 0; for (typename internal::evaluator::InnerIterator it(thisEval, 0); it; ++it) { - for (; row < it.index(); ++row) s << "0" << std::endl; + for (; row < it.index(); ++row) { + s.width(width); + s << Scalar(0) << std::endl; + } + s.width(width); s << it.value() << std::endl; ++row; } - for (; row < m.rows(); ++row) s << "0" << std::endl; + for (; row < m.rows(); ++row) { + s.width(width); + s << Scalar(0) << std::endl; + } } else { SparseMatrix trans = m; - s << static_cast >&>(trans); + s << static_cast>&>(trans); } } return s; -- GitLab From c569f2a268e7d66497fb05c914d42589ab6e3aa8 Mon Sep 17 00:00:00 2001 From: Evan Porter Date: Wed, 3 Sep 2025 06:55:44 +0000 Subject: [PATCH 02/12] Undo little bit at the end --- Eigen/src/SparseCore/SparseMatrixBase.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Eigen/src/SparseCore/SparseMatrixBase.h b/Eigen/src/SparseCore/SparseMatrixBase.h index 31ea07ba4..50e1e9b35 100644 --- a/Eigen/src/SparseCore/SparseMatrixBase.h +++ b/Eigen/src/SparseCore/SparseMatrixBase.h @@ -303,7 +303,7 @@ class SparseMatrixBase : public EigenBase { } } else { SparseMatrix trans = m; - s << static_cast>&>(trans); + s << static_cast >&>(trans); } } return s; -- GitLab From 7a118a43aabfa75e261d1184e472b095b54b9510 Mon Sep 17 00:00:00 2001 From: Evan Porter Date: Sat, 6 Sep 2025 22:47:06 +0000 Subject: [PATCH 03/12] `using` instead of `typedef` --- Eigen/src/SparseCore/SparseMatrixBase.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Eigen/src/SparseCore/SparseMatrixBase.h b/Eigen/src/SparseCore/SparseMatrixBase.h index 50e1e9b35..45001ade5 100644 --- a/Eigen/src/SparseCore/SparseMatrixBase.h +++ b/Eigen/src/SparseCore/SparseMatrixBase.h @@ -224,11 +224,11 @@ class SparseMatrixBase : public EigenBase { public: #ifndef EIGEN_NO_IO friend std::ostream& operator<<(std::ostream& s, const SparseMatrixBase& m) { - typedef typename Derived::Nested Nested; - typedef typename internal::remove_all::type NestedCleaned; + using Nested = typename Derived::Nested; + using NestedCleaned = internal::remove_all::type; /// For converting `0's` to the matrices numerical type - typedef typename Derived::Scalar Scalar; + using Scalar = typename Derived::Scalar; if (Flags & RowMajorBit) { Nested nm(m.derived()); -- GitLab From 9ccfedad32be229790567caafdd9611b834e2adc Mon Sep 17 00:00:00 2001 From: Evan Porter Date: Sun, 7 Sep 2025 18:24:44 +0000 Subject: [PATCH 04/12] remove usage of `std::max` --- Eigen/src/SparseCore/SparseMatrixBase.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Eigen/src/SparseCore/SparseMatrixBase.h b/Eigen/src/SparseCore/SparseMatrixBase.h index 45001ade5..b7331eb5d 100644 --- a/Eigen/src/SparseCore/SparseMatrixBase.h +++ b/Eigen/src/SparseCore/SparseMatrixBase.h @@ -246,7 +246,10 @@ class SparseMatrixBase : public EigenBase { std::ostringstream ss; ss.copyfmt(s); ss << it.value(); - width = std::max(width, Index(ss.str().size())); + + const auto potential_width = Index(ss.str().size()); + if (potential_width > width) + width = potential_width; } } } @@ -283,7 +286,10 @@ class SparseMatrixBase : public EigenBase { std::ostringstream ss; ss.copyfmt(s); ss << it.value(); - width = std::max(width, Index(ss.str().size())); + + const auto potential_width = Index(ss.str().size()); + if (potential_width > width) + width = potential_width; } } -- GitLab From abf6c5b497747546e248f56bc5a4268f114b9608 Mon Sep 17 00:00:00 2001 From: Evan Porter Date: Mon, 8 Sep 2025 07:17:30 +0000 Subject: [PATCH 05/12] formatting --- Eigen/src/SparseCore/SparseMatrixBase.h | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/Eigen/src/SparseCore/SparseMatrixBase.h b/Eigen/src/SparseCore/SparseMatrixBase.h index b7331eb5d..3e0134f4c 100644 --- a/Eigen/src/SparseCore/SparseMatrixBase.h +++ b/Eigen/src/SparseCore/SparseMatrixBase.h @@ -203,8 +203,7 @@ class SparseMatrixBase : public EigenBase { return derived(); } - SparseMatrixBase() : m_isRValue(false) { /* TODO check flags */ - } + SparseMatrixBase() : m_isRValue(false) { /* TODO check flags */ } template Derived& operator=(const ReturnByValue& other); @@ -248,8 +247,7 @@ class SparseMatrixBase : public EigenBase { ss << it.value(); const auto potential_width = Index(ss.str().size()); - if (potential_width > width) - width = potential_width; + if (potential_width > width) width = potential_width; } } } @@ -288,8 +286,7 @@ class SparseMatrixBase : public EigenBase { ss << it.value(); const auto potential_width = Index(ss.str().size()); - if (potential_width > width) - width = potential_width; + if (potential_width > width) width = potential_width; } } -- GitLab From ccb3292b4c7ae9051b0678424c68c56bedadcf75 Mon Sep 17 00:00:00 2001 From: Evan Porter Date: Mon, 8 Sep 2025 18:05:15 +0000 Subject: [PATCH 06/12] undo overformmatted so git pipelines doesn't scream at me --- Eigen/src/SparseCore/SparseMatrixBase.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Eigen/src/SparseCore/SparseMatrixBase.h b/Eigen/src/SparseCore/SparseMatrixBase.h index 3e0134f4c..d530e2bce 100644 --- a/Eigen/src/SparseCore/SparseMatrixBase.h +++ b/Eigen/src/SparseCore/SparseMatrixBase.h @@ -203,7 +203,8 @@ class SparseMatrixBase : public EigenBase { return derived(); } - SparseMatrixBase() : m_isRValue(false) { /* TODO check flags */ } + SparseMatrixBase() : m_isRValue(false) { /* TODO check flags */ + } template Derived& operator=(const ReturnByValue& other); -- GitLab From 6cbab3f4651e689672dd3e9154b75b18740d99bc Mon Sep 17 00:00:00 2001 From: Evan Porter Date: Mon, 8 Sep 2025 18:05:55 +0000 Subject: [PATCH 07/12] remove trailing space --- Eigen/src/SparseCore/SparseMatrixBase.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Eigen/src/SparseCore/SparseMatrixBase.h b/Eigen/src/SparseCore/SparseMatrixBase.h index d530e2bce..0725f68a6 100644 --- a/Eigen/src/SparseCore/SparseMatrixBase.h +++ b/Eigen/src/SparseCore/SparseMatrixBase.h @@ -203,7 +203,7 @@ class SparseMatrixBase : public EigenBase { return derived(); } - SparseMatrixBase() : m_isRValue(false) { /* TODO check flags */ + SparseMatrixBase() : m_isRValue(false) { /* TODO check flags */ } template -- GitLab From 8370a3789afe8c6c3ff79d4b4f748f1c65d3c0fd Mon Sep 17 00:00:00 2001 From: Evan Porter Date: Mon, 8 Sep 2025 18:25:33 +0000 Subject: [PATCH 08/12] width is now `std::size_t` --- Eigen/src/SparseCore/SparseMatrixBase.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Eigen/src/SparseCore/SparseMatrixBase.h b/Eigen/src/SparseCore/SparseMatrixBase.h index 0725f68a6..03175ff9c 100644 --- a/Eigen/src/SparseCore/SparseMatrixBase.h +++ b/Eigen/src/SparseCore/SparseMatrixBase.h @@ -235,12 +235,12 @@ class SparseMatrixBase : public EigenBase { internal::evaluator thisEval(nm); // compute global width - Index width = 0; + std::size_t width = 0; { std::ostringstream ss0; ss0.copyfmt(s); ss0 << Scalar(0); - width = Index(ss0.str().size()); + width = ss0.str().size(); for (Index row = 0; row < nm.outerSize(); ++row) { for (typename internal::evaluator::InnerIterator it(thisEval, row); it; ++it) { std::ostringstream ss; @@ -275,18 +275,18 @@ class SparseMatrixBase : public EigenBase { internal::evaluator thisEval(nm); if (m.cols() == 1) { // compute local width (single col) - Index width = 0; + std::size_t width = 0; { std::ostringstream ss0; ss0.copyfmt(s); ss0 << Scalar(0); - width = Index(ss0.str().size()); + width = ss0.str().size(); for (typename internal::evaluator::InnerIterator it(thisEval, 0); it; ++it) { std::ostringstream ss; ss.copyfmt(s); ss << it.value(); - const auto potential_width = Index(ss.str().size()); + const auto potential_width = ss.str().size(); if (potential_width > width) width = potential_width; } } -- GitLab From 6df37b0eb6e1dde31983d082f8c4e06b7769c519 Mon Sep 17 00:00:00 2001 From: Evan Porter Date: Mon, 8 Sep 2025 18:26:26 +0000 Subject: [PATCH 09/12] Removed cast --- Eigen/src/SparseCore/SparseMatrixBase.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Eigen/src/SparseCore/SparseMatrixBase.h b/Eigen/src/SparseCore/SparseMatrixBase.h index 03175ff9c..a85cd2473 100644 --- a/Eigen/src/SparseCore/SparseMatrixBase.h +++ b/Eigen/src/SparseCore/SparseMatrixBase.h @@ -247,7 +247,7 @@ class SparseMatrixBase : public EigenBase { ss.copyfmt(s); ss << it.value(); - const auto potential_width = Index(ss.str().size()); + const auto potential_width = ss.str().size(); if (potential_width > width) width = potential_width; } } -- GitLab From 0dab191da4a92ddd4f543d4b2cce75cda30e3399 Mon Sep 17 00:00:00 2001 From: Evan Porter Date: Mon, 8 Sep 2025 19:04:04 +0000 Subject: [PATCH 10/12] change auto --- Eigen/src/SparseCore/SparseMatrixBase.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Eigen/src/SparseCore/SparseMatrixBase.h b/Eigen/src/SparseCore/SparseMatrixBase.h index a85cd2473..4d9b9c122 100644 --- a/Eigen/src/SparseCore/SparseMatrixBase.h +++ b/Eigen/src/SparseCore/SparseMatrixBase.h @@ -247,7 +247,7 @@ class SparseMatrixBase : public EigenBase { ss.copyfmt(s); ss << it.value(); - const auto potential_width = ss.str().size(); + const std::size_t potential_width = ss.str().size(); if (potential_width > width) width = potential_width; } } -- GitLab From 3f4ea5387b057746c26b63d11edba8b6112167e9 Mon Sep 17 00:00:00 2001 From: Evan Porter Date: Mon, 8 Sep 2025 19:11:21 +0000 Subject: [PATCH 11/12] Edit SparseMatrixBase.h --- Eigen/src/SparseCore/SparseMatrixBase.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Eigen/src/SparseCore/SparseMatrixBase.h b/Eigen/src/SparseCore/SparseMatrixBase.h index 4d9b9c122..4ce41879e 100644 --- a/Eigen/src/SparseCore/SparseMatrixBase.h +++ b/Eigen/src/SparseCore/SparseMatrixBase.h @@ -286,7 +286,7 @@ class SparseMatrixBase : public EigenBase { ss.copyfmt(s); ss << it.value(); - const auto potential_width = ss.str().size(); + const std::size_t potential_width = ss.str().size(); if (potential_width > width) width = potential_width; } } -- GitLab From aa4bf8b4e8aee34d5ff4d62faa000f12c54f3b98 Mon Sep 17 00:00:00 2001 From: Evan Porter Date: Mon, 8 Sep 2025 19:46:55 +0000 Subject: [PATCH 12/12] Edit SparseMatrixBase.h --- Eigen/src/SparseCore/SparseMatrixBase.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Eigen/src/SparseCore/SparseMatrixBase.h b/Eigen/src/SparseCore/SparseMatrixBase.h index 4ce41879e..fbf1313d5 100644 --- a/Eigen/src/SparseCore/SparseMatrixBase.h +++ b/Eigen/src/SparseCore/SparseMatrixBase.h @@ -225,7 +225,7 @@ class SparseMatrixBase : public EigenBase { #ifndef EIGEN_NO_IO friend std::ostream& operator<<(std::ostream& s, const SparseMatrixBase& m) { using Nested = typename Derived::Nested; - using NestedCleaned = internal::remove_all::type; + using NestedCleaned = typename internal::remove_all::type; /// For converting `0's` to the matrices numerical type using Scalar = typename Derived::Scalar; -- GitLab