Simple matrix for the rest of us


The Introduction

“…There are issues such as dynamic memory allocation in C/C++, which is forbidden, under the DO-178B standard, in safety-critical embedded avionics code…”
The source

Before knowing the above, the other day I have developed a handy little template to hold and provide, a plain 2d array, completely on the stack (aka the “matrix”).

It is also fully “compile time capable” with the full static internal presentation.

It is very handy to gently enforce company-wide C++ policies and to give some useful functionality in return. I am calling this approach “almost a pod”.

All of the dbj::stack_matrix is in this one header file.

Tests are in this another, single header file.

Probably the main reason for having matrices in some app is matrix multiplication. With this API it is rather silly simple, one could say:

After multiplication, A,B and R relationship is: A[n][m] x B[m][p] = R[n][p] . Columns of A must be the same as rows of B, and R matrix size must be A rows x B cols.

Nice, simple, functional and easy. Since this is all stack solution, the maximum matrix size matter here a lot and is approx 64KB.

This API is more than enough for the 99% of real-life use cases.  No huge and complex matrix libraries required.

In case you need matrix addition, difference and such I am sure you will find it very easy to add them by observing how is multiplication implemented, in there.

This is, after all, an open source. Just please respect the copyright.

In case you just want to use ‘dbj::static_matrix’ as matrix storage, you are most welcome.  You will be also more than capable to do so. By looking into the two headers, whose github links are provided.

Example. I personally use its storage in a C++ standard way

That way I can reach to the storage inside and use it elsewhere, to change it or to read it.

If I am using some external API written in a proper standard C++

I can use this in a “super silly” way:

Really, a celebration of modern C++. But.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.