Skip to content

Conversation

@hshose
Copy link
Contributor

@hshose hshose commented Nov 22, 2025

Simple lbuild include for Eigen and modm::IOStream for printing of Eigen::MatrixBase

Example tested on Nucleo G474 board.

Niklas modified some things in the integration:

  • Eigen doesn't seem to support a config file, so I inject the config into the Eigen/Version file, which is included by every file indirectly.
  • I moved the iostream support to it's own file and inject it into the :io module.
  • I made the no malloc macro dependent on whether the :platform:heap module is included.
  • Redirect eigen_assert to modm_assert otherwise you won't get any output.
  • Add more documentation about eigen
  • Somehow use the built-in Eigen formatting functions instead of rolling our own.
  • Fix the compilation regarding conflicting declaration of C function 'int __cxa_guard_acquire(guard_type*)'. Was an issue with circular imports due to assert.hpp relying on register.hpp.

@hshose
Copy link
Contributor Author

hshose commented Nov 22, 2025

#1005

@salkinium
Copy link
Member

salkinium commented Nov 23, 2025

I added GitLab support to partial and then added the eigen-partial repository.

@salkinium salkinium force-pushed the feature/eigen branch 2 times, most recently from 17e7ace to dab4e2a Compare November 23, 2025 18:25
salkinium and others added 3 commits November 23, 2025 19:37
This caused issues with circular imports and it is not necessary to have.
@hshose
Copy link
Contributor Author

hshose commented Nov 23, 2025

Wow! Pretty cool! Thank you @salkinium

Regarding the printing and reuse of the Eigen::print_matrix function:
I think we could string-replace the std::ostream with modm::IOStream (sed -i 's/std::ostream/modm::IOStream/g' Eigen/src/Core/IO.h in the github actions repo update)
and then patch with the following patch to fix the use of std::stringstream to determine the print width:

diff --git a/Eigen/src/Core/IO.h b/Eigen/src/Core/IO.h
index 0a1b583..5dc6d34 100644
--- a/Eigen/src/Core/IO.h
+++ b/Eigen/src/Core/IO.h
@@ -13,7 +13,7 @@
 
 // IWYU pragma: private
 #include "./InternalHeaderCheck.h"
-
+#include <modm/io.hpp>
 namespace Eigen {
 
 enum { DontAlignCols = 1 };
@@ -167,10 +167,19 @@ std::ostream& print_matrix(std::ostream& s, const Derived& _m, const IOFormat& f
     // compute the largest width
     for (Index j = 0; j < m.cols(); ++j)
       for (Index i = 0; i < m.rows(); ++i) {
-        std::stringstream sstr;
-        sstr.copyfmt(s);
-        sstr << static_cast<PrintType>(m.coeff(i, j));
-        width = std::max<Index>(width, Index(sstr.str().length()));
+        char tmpbuf[64];
+        std::streamsize prec  = s.precision();
+        int len = 0;
+        if constexpr (std::is_floating_point_v<PrintType>) {
+            if (prec > 0)
+                len = snprintf(tmpbuf, sizeof(tmpbuf), "%.*g", (int)prec, (double)static_cast<PrintType>(m.coeff(i, j)));
+            else
+                len = snprintf(tmpbuf, sizeof(tmpbuf), "%g", (double)static_cast<PrintType>(m.coeff(i, j)));
+        } else {
+            len = snprintf(tmpbuf, sizeof(tmpbuf), "%lld", (long long)static_cast<PrintType>(m.coeff(i, j)));
+        }
+        if (len > width)
+            width = len;
       }
   }
   std::streamsize old_width = s.width();

I added a commit with the required changes to the modm::IOStream:

  • dummy setter/getter for fill and width as this is currently not implemented
  • setter/getter for precision in float/double printing

@salkinium
Copy link
Member

Yes, go for it. I added you as maintainer for the eigen-partial repo, remember to use the partial cli --patch option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

[math] math::Matrix class size limitation

2 participants