This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Overload "/" for Matrix Division on Arduino Microcontroller

Tags: None
(comma "," separated)
randomvibe
Registered Member
Posts
5
Karma
0
Some background followed by my question....

Background:
I have been a Matlab power user for a long time and very accustomed to the vector programming style which makes coding linear algebra very natural, simple and minimalistic. I implemented a Kalman filter in Matlab from scratch (did not use toolbox) for a basic IMU (inertial measurement unit), and wanted to do the same in the Arduino Due 32-bit microcontroller. But I dreaded this because Arduino's core programming language is C++ and frankly I dislike C++, fortran, etc. Then I discovered Eigen, and wow, it's almost as simple as Matlab and Octave. I'm very impressed by the simplicity - to the Eigen developers, thank you very much. My Kalman filter is working on the Arduino Due using Eigen 3.1.3.

Question: I'm very happy and excited by the overloaded math operators for matrices. Addition, subtraction and multiplication are covered, but what about division? Is it possible to overload the "/" symbol? This would help streamline the Kalman filter matrix equations.

Thank you.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
One difficulty is that in C++ we can only overload operator/ . Therefore we can only express things like b/A <=> b * A.inverse() and not A\b <=> A.inverse() * b since in c++ \ is not an operator. Therefore I'm pretty sure such an operator would lead to a lot of confusion and errors. On the contrary, A.inverse() * b is not that verbose and very explicite. That said we still still have to "A.inverse() * b" fast such that it does not explicitly compute the inverse! That's a planed feature.
randomvibe
Registered Member
Posts
5
Karma
0
Doing something similar to A\b <=> A.inverse() * b would be very useful. Can you overload a different operator such as "~" (tilde) or "^" (hat)?

Or more specifically, "^" for division and "~" for transpose. Transpose is a very common matrix operation - it begs for its own operator.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
That's way too confusing! Anyway, if you wanna play you can easily add your own shortcut:

http://eigen.tuxfamily.org/dox-devel/To ... Eigen.html

If you want a fun but still readable shortcut for transpose (not tested, just to give the idea):

Code: Select all
struct T_t {};
static const T_t T; // very polluting global variable!!

// as a matrix base plugin:
Transpose<const Derived> operator^(const T_t&) const { return transpose(); }

// how to use it:
MatrixXf A;
MatrixXf ATA = A^T * A;


;)
randomvibe
Registered Member
Posts
5
Karma
0
ggael ~ thanks for the tips. I'll try them out.

I certainly don't want to make matrix operations confusing. Just want to emulate what Matlab and Octave can do with inversion and transpose. They use the apostrophe (') for transpose. Since that is not a standard operator in C++, I suggested tilde (~).

Question: why is the statement "static const T_t T;" a very polluting global variable?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
This is because you cannot name any other variables or types with the letter "T". Except for local variables, it is thus not recommended at all to have too simple and common variable names. Still much better than a macro that would be named "T"!
randomvibe
Registered Member
Posts
5
Karma
0
To those interested in using Eigen on the Arduino 32-bit board called "Due", instructions posted here:

http://forum.arduino.cc/index.php?PHPSE ... msg1089371


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot]