blMatrix3d — A simple 3×3 matrix in c++

Introduction

As I have mentioned in most of my articles, over the years, I have developed many mathematical tools that I have used time over time. In this article, I present a simple 3×3 matrix class, which I use in many applications, from machine vision to physics simulation, to 3d graphics. As with the rest of my work, simplicity and code readability are my top priorities, because the key to code reuse, is to make the code understandable.

Here’s a little snippet showing how easy this 3×3 matrix class is to use:

int main(int argc, char *argv[])
{
    // Let's show different ways to
    // create a 3x3 matrix

    // First:   Create a 3x3 matrix
    //          by passing its values
    blMatrix3d<double> M1(1,0,3,4,5,-1,10,20,2);
    cout << "M1 = " << M1 << "\n\n";

    // Second:  Create a 3x3 matrix by
    //          giving it an initial value
    blMatrix3d<double> M2(10);
    cout << "M2 = " << M2 << "\n\n";

    // Third:   Create an identity matrix
    blMatrix3d<double> M3 = eye3d<double>(3);
    cout << "M3 = " << M3 << "\n\n";

    // Fourth:  Create a 3x3 matrix by
    //          passing it a static 3x3 array
    double Arr[3][3] = {-1.2,2.3,10,-5,6,7.6,1,1,-1};
    blMatrix3d<double> M4(Arr);
    cout << "M4 = " << M4 << "\n\n";

    // Fifth:   Create a 3x3 matrix by
    //          passing it a static nxm array
    double Arr2[4][2] = {9.2,2.42,5,5.2,1.2,8,6.7,7};
    blMatrix3d<double> M5(Arr2);
    cout << "M5 = " << M5 << "\n\n";

    // Calculate determinant of M1
    cout << "det(M1) = " << det(M1) << "\n\n";

    // Transpose M1
    cout << "Transpose(M1) = " << Transpose(M1) << "\n\n";

    // Calculate the inverse of M1
    cout << "inv(M1) = " << inv(M1) << "\n\n";

    // Calculate M1 * inv(M1) (equals identity)
    cout << "M1 * inv(M1) = " << M1*inv(M1) << "\n\n";

    int waitforkey;
    cin >> waitforkey;
}

The resulting console output is:

Dependencies

  1. blVector3d from the blMathAPI
    1. Used to multiply a vector by a matrix and viceversa

List of available functions

As I use this 3×3 matrix class for different applications, new needs pop out and I keep adding functionality to it. The following is a list of the current operators/functions available

  1. Arithmetic operators
    1. operator+ and operator-
      1. Add/Subtract two matrices
    2. operator*
      1. Multiply a matrix by a scalar and viceversa
      2. Multiply two matrices
    3. operator/
      1. Divide a matrix by a scalar
      2. Multiply a matrix by the inverse of a second matrix
      3. Multiple a scalar by the inverse of a second matrix
  2. det
    1. Calculate the determinant of a matrix
  3. Trace
    1. Sum the diagonal components of a matrix
  4. Transpose
    1. Take the transpose of a matrix
  5. inv
    1. Calculate the inverse of a matrix
  6. stream operators
    1. operator<<
      1. Output a matrix to the console

The code

blMatrix3d structure is simple, but provides overloaded operators to make the code very readable. The structure is saved in a file called blMatrix3d.hpp, which uses inline documentation.

blMatrix3d.hpp (Click to see code…)

Usage

This 3×3 matrix structure can be used for anything from graphics to physics simulations, and the code is very readable. Here’s a little snippet showing just how easy it is to use:

int main(int argc, char *argv[])
{
    // Let's show different ways to
    // create a 3x3 matrix

    // First:   Create a 3x3 matrix
    //          by passing its values
    blMatrix3d<double> M1(1,0,3,4,5,-1,10,20,2);
    cout << "M1 = " << M1 << "\n\n";

    // Second:  Create a 3x3 matrix by
    //          giving it an initial value
    blMatrix3d<double> M2(10);
    cout << "M2 = " << M2 << "\n\n";

    // Third:   Create an identity matrix
    blMatrix3d<double> M3 = eye3d<double>(3);
    cout << "M3 = " << M3 << "\n\n";

    // Fourth:  Create a 3x3 matrix by
    //          passing it a static 3x3 array
    double Arr[3][3] = {-1.2,2.3,10,-5,6,7.6,1,1,-1};
    blMatrix3d<double> M4(Arr);
    cout << "M4 = " << M4 << "\n\n";

    // Fifth:   Create a 3x3 matrix by
    //          passing it a static nxm array
    double Arr2[4][2] = {9.2,2.42,5,5.2,1.2,8,6.7,7};
    blMatrix3d<double> M5(Arr2);
    cout << "M5 = " << M5 << "\n\n";

    // Calculate determinant of M1
    cout << "det(M1) = " << det(M1) << "\n\n";

    // Transpose M1
    cout << "Transpose(M1) = " << Transpose(M1) << "\n\n";

    // Calculate the inverse of M1
    cout << "inv(M1) = " << inv(M1) << "\n\n";

    // Calculate M1 * inv(M1) (equals identity)
    cout << "M1 * inv(M1) = " << M1*inv(M1) << "\n\n";

    int waitforkey;
    cin >> waitforkey;
}

The resulting console output is:

Downloads

I have put all the files into a zip file which can be downloaded here.  All you have to do is extract it somewhere, let’s say in a directory called blMathAPI, and then include the blMathAPI.hpp file as follows:

#include "blMathAPI.hpp"
using namespace blMathAPI;

Note: Everything is declared in the namespace blMathAPI, and such you would use it as: blMathAPI::blMatrix3d

[download#2]

Updates

Check back as I will often update this file whenever I add more 3×3 matrix functions to it

About Vincenzo Barbato

Known to his friends as Enzo, he's an outside-the-box engineer/researcher whose interests and expertise span many fields, including controls systems, multi-physics simulations, mechatronics, oil technologies, data analysis and machine vision just to name a few.

Refusing to grow up, he's on a continuous journey to develop simple and creative solutions that have the power of disrupting industries by optimizing systems and processes.

Married to a beautiful wife, with two beautiful daughters and two identical twin boys, his home is a never ending chaotic fountain of inspiration.

His outlook on life:

"Never blindly accept what you're told, listen, but then question, with curiosity, creativity and collaboration we can change the world"

About Enzo

Known to his friends as Enzo, he's an outside-the-box engineer/researcher whose interests and expertise span many fields, including controls systems, multi-physics simulations, mechatronics, oil technologies, data analysis and machine vision just to name a few. Refusing to grow up, he's on a continuous journey to develop simple and creative solutions that have the power of disrupting industries by optimizing systems and processes. Married to a beautiful wife, with two beautiful daughters and two identical twin boys, his home is a never ending chaotic fountain of inspiration. His outlook on life: "Never blindly accept what you're told, listen, but then question, with curiosity, creativity and collaboration we can change the world"

4 Comments

  1. No problem Ron, just glad I could help someone.
    Let me know if you have ideas or suggestions on posts

  2. It’s not that I want to duplicate your internet site, but I really like the design. Could you tell me which theme are you using? Or was it tailor made?

  3. Pingback: blRigidBodyAPI — A simple rigid body API in c++ | BarbatoLabs

Leave a Reply

Your email address will not be published. Required fields are marked *