Skip to content

Commit 9137412

Browse files
committed
[ext] added eigen linear algebra submodule and example
1 parent 584f5e3 commit 9137412

File tree

6 files changed

+132
-0
lines changed

6 files changed

+132
-0
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,6 @@
5252
[submodule "ext/segger/rtt"]
5353
path = ext/segger/rtt
5454
url = https://github.com/modm-ext/segger-rtt.git
55+
[submodule "ext/eigen/eigen"]
56+
path = ext/eigen/eigen
57+
url = https://gitlab.com/libeigen/eigen
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2025, Henrik Hose
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#include <eigen_conf.h>
12+
13+
#include <Eigen/Dense>
14+
#include <modm/board.hpp>
15+
16+
using namespace Board;
17+
18+
int
19+
main()
20+
{
21+
Board::initialize();
22+
LedD13::setOutput();
23+
24+
while (1)
25+
{
26+
Eigen::Matrix2f mat{{1, 2}, {3, 4}};
27+
Eigen::Vector2f u{-1, 1}, v{2, 0};
28+
MODM_LOG_INFO << "Here is mat*mat:\n" << mat * mat << modm::endl;
29+
MODM_LOG_INFO << "Here is mat*u:\n" << mat * u << modm::endl;
30+
MODM_LOG_INFO << "Here is u^T*mat:\n" << u.transpose() * mat << modm::endl;
31+
MODM_LOG_INFO << "Here is u^T*v:\n" << u.transpose() * v << modm::endl;
32+
MODM_LOG_INFO << "Here is u*v^T:\n" << u * v.transpose() << modm::endl;
33+
MODM_LOG_INFO << "Let's multiply mat by itself" << modm::endl;
34+
mat = mat * mat;
35+
MODM_LOG_INFO << "Now mat is mat:\n" << mat << modm::endl;
36+
37+
modm::delay(std::chrono::seconds(1));
38+
}
39+
40+
return 0;
41+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<library>
2+
<extends>modm:nucleo-g474re</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/nucleo_g474re/eigen</option>
5+
</options>
6+
<modules>
7+
<module>modm:build:scons</module>
8+
<module>modm:eigen</module>
9+
</modules>
10+
</library>

ext/eigen/eigen

Submodule eigen added at bc3b398

ext/eigen/eigen.lb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2025, Henrik Hose
5+
#
6+
# This file is part of the modm project.
7+
#
8+
# This Source Code Form is subject to the terms of the Mozilla Public
9+
# License, v. 2.0. If a copy of the MPL was not distributed with this
10+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
11+
# -----------------------------------------------------------------------------
12+
13+
def init(module):
14+
module.name = ":eigen"
15+
module.description = """\
16+
# Eigen Linear Algebra Library
17+
"""
18+
19+
def prepare(module, options):
20+
if options[":target"].identifier.platform != "hosted":
21+
module.depends(":platform:heap")
22+
23+
module.add_option(
24+
BooleanOption(
25+
name="eigen_no_malloc",
26+
description="if defined, any request from inside the Eigen to allocate memory from the heap results in an assertion failure.",
27+
default=True))
28+
return True
29+
30+
def build(env):
31+
env.outbasepath = "modm/ext/eigen"
32+
env.copy("eigen/Eigen", "Eigen")
33+
env.copy("eigen/COPYING.APACHE", "COPYING.APACHE")
34+
env.copy("eigen/COPYING.BSD", "COPYING.BSD")
35+
env.copy("eigen/COPYING.MINPACK", "COPYING.MINPACK")
36+
env.copy("eigen/COPYING.MPL2", "COPYING.MPL2")
37+
env.copy("eigen/COPYING.README", "COPYING.README")
38+
env.copy("eigen/LICENSE", "LICENSE")
39+
env.collect(":build:path.include", "modm/ext/eigen")
40+
env.substitutions = {"eigen_no_malloc": env.get("eigen_no_malloc", True)}
41+
env.template("eigen_conf.h.in")

ext/eigen/eigen_conf.h.in

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef EIGEN_CONF_H
2+
#define EIGEN_CONF_H
3+
4+
#if __has_include(<eigen_conf_local.h>)
5+
# include <eigen_conf_local.h>
6+
#endif
7+
8+
#define EIGEN_DONT_VECTORIZE
9+
#define EIGEN_DISABLE_UNALIGNED_ARRAY_ASSERT
10+
11+
{% if eigen_no_malloc %}
12+
#define EIGEN_NO_MALLOC
13+
{% endif %}
14+
15+
16+
#include <Eigen/Dense>
17+
#include <modm/io/iostream.hpp>
18+
19+
namespace modm {
20+
21+
template<typename Derived>
22+
modm::IOStream& operator<<(modm::IOStream& s, const Eigen::MatrixBase<Derived>& m)
23+
{
24+
for (Eigen::Index r = 0; r < m.rows(); ++r) {
25+
for (Eigen::Index c = 0; c < m.cols(); ++c) {
26+
s << m(r, c);
27+
if (c + 1 < m.cols()) s << ", ";
28+
}
29+
if (r + 1 < m.rows()) s << "\n";
30+
}
31+
return s;
32+
}
33+
34+
} // namespace modm
35+
36+
#endif // EIGEN_CONF_H

0 commit comments

Comments
 (0)