tomstewart89 / BasicLinearAlgebra

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

Unable to perform scalar operations #64

Closed aloysiousBenoy closed 1 year ago

aloysiousBenoy commented 1 year ago

For some reason scalar multiplication is giving me error When I add in the following line of code the code fails to compile

 k_process_noise = 10 * k_intermediate;

I get the following error

 no match for 'operator*' (operand types are 'int' and 'BLA::Matrix<1, 1>')
     k_process_noise = 10 * k_intermediate;
                       ~~~^~~~~~~~~~~~~~~~
In file included from Arduino/libraries/BasicLinearAlgebra/BasicLinearAlgebra.h:142:0,
               Arduino/libraries/BasicLinearAlgebra/impl/BasicLinearAlgebra.h:12:35: note: candidate: template<class MatAType, class MatBType, int MatARows, int MatACols, int MatBCols, class DType> BLA::Matrix<MatARows, MatBCols, DType> BLA::operator*(const BLA::MatrixBase<MatAType, MatARows, MatACols, DType>&, const BLA::MatrixBase<MatBType, MatACols, MatBCols, DType>&)
 Matrix<MatARows, MatBCols, DType> operator*(const MatrixBase<MatAType, MatARows, MatACols, DType> &matA,
                                   ^~~~~~~~
Arduino/libraries/BasicLinearAlgebra/impl/BasicLinearAlgebra.h:12:35: note:   template argument deduction/substitution failed:
/xiao_test/xiao_test.ino:68:28: note:   mismatched types 'const BLA::MatrixBase<MatAType, MatARows, MatACols, DType>' and 'int'
     k_process_noise = 10 * k_intermediate;
                            ^~~~~~~~~~~~~~
Arduino/libraries/BasicLinearAlgebra/BasicLinearAlgebra.h:142:0,
                xiao_test/xiao_test.ino:5:
Arduino/libraries/BasicLinearAlgebra/impl/BasicLinearAlgebra.h:158:27: note: candidate: template<class MatType, int Rows, int Cols, class DType> BLA::Matrix<Rows, Cols, DType> BLA::operator*(const BLA::MatrixBase<MatType, Rows, Cols, DType>&, DType)
 Matrix<Rows, Cols, DType> operator*(const MatrixBase<MatType, Rows, Cols, DType> &mat, const DType k)
                           ^~~~~~~~
Arduino/libraries/BasicLinearAlgebra/impl/BasicLinearAlgebra.h:158:27: note:   template argument deduction/substitution failed:
xiao_test/xiao_test.ino:68:28: note:   mismatched types 'const BLA::MatrixBase<MatType, Rows, Cols, DType>' and 'int'
     k_process_noise = 10 * k_intermediate;
tomstewart89 commented 1 year ago

Hey @aloysiousBenoy , I'm pretty sure that line will compile if you modify it like so:

 k_process_noise = k_intermediate * 10.0f;

The problem is that you're using an integer datatype for the multiplier whereas BLA::Matrixs expect a multiplier of the same type as their elements, which by default is float.

Hope that helps! Let me know if you have any more trouble

aloysiousBenoy commented 1 year ago

That solved the issue !! Thanks a lot ! Is this documented/ mentioned somewhere or is this a very obvious thing?

tomstewart89 commented 1 year ago

No problem! I'm glad that did the trick.

It's not super obvious and it's also not explicitly documented anywhere. That said, pretty much every function in this library has a unit test that demonstrates how it works so they might be a useful reference if you run into any more compiler errors. In this case the corresponding test is here: https://github.com/tomstewart89/BasicLinearAlgebra/blob/782afcee029544fe5b1dc25265ab8169ab2d0db4/test/test_arithmetic.cpp#L106

Hope that helps! Good luck with your project!

bobthefairycat commented 1 year ago

I guess my recent opened issue has to do with this as well. Somehow, v3.7.0 may have been doing implicit type casting which I guess is dangerous? Wouldn't it default to the type of the left-hand-side being assigned to (i.e. casting to higher resolution should be allowed)?

In our case, it seems that casting int -> float should be permissible (and indeed it is in v 3.7.0).