tomstewart89 / BasicLinearAlgebra

A library for using matrices and linear algebra on Arduino
MIT License
187 stars 38 forks source link

Cannot Multiply Two Matrices with nondefault type of double #26

Closed BoxWizard000000 closed 5 years ago

BoxWizard000000 commented 5 years ago

When I try to multiply two matrices like so...

    Serial.begin(9600);
    Matrix<2, 2, double> B = {1, 0,
                              0, 1};

    Matrix<2, 2, double> A = {1, 0,
                              0, 2.3};

    Serial << (A * B) << '\n';

I get a compiler error telling me that "* is an invalid binary expression" for the types; however, even stranger, if the left matrix has double elements and the right matrix doesn't, the compiler error goes away.

    Serial.begin(9600);
    Matrix<2, 2, double> B = {1, 0,
                              0, 1};

    Matrix<2, 2> A = {1, 0,
                      0, 2.3};

    Serial << (A * B) << '\n';

The multiplication of normal matrices works as desired.

tomstewart89 commented 5 years ago

Hey @BoxWizard000000

Thanks for your interest in this library!

The problem here is that Matrix expects its third template parameter to be a class of a MemoryDelegate (i.e something that allocates some memory and returns elements of it when asked for them). The default memory delegate is an Array so to declare a Matrix of doubles you just need to use:

Matrix<2, 2, Array<2,2,double>> B = {1, 0, 0, 1};
Matrix<2, 2, Array<2,2,double>> A = {1, 0, 0, 2.3};

Then you should be able to multiply them as usual:

Serial << (A * B) << '\n';

Admittedly this MemoryDelegate thing is probably a bit confusing, especially if you're used to using Eigen so if you prefer you can also just use the template alias ArrayMatrix like so:

ArrayMatrix<2, 2, double> C = {1, 0, 0, 1};

That'll expand to the exact same type as above.

Hope that helps!

BoxWizard000000 commented 5 years ago

Ahh now I get what those are. Thank you very much.