tomstewart89 / BasicLinearAlgebra

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

Code-breaking updates in 4.0.0 #65

Closed bobthefairycat closed 1 year ago

bobthefairycat commented 1 year ago

Hi there, my Arduino code compiles and runs, working as expected, using v3.7.0 of BLA, but updating to v4.0.0 breaks it. Here is a truncated example of my code that compiles fine on v3.7.0 but fails on v4.0.0

#include <BasicLinearAlgebra.h>
using namespace BLA;

Matrix<2, 2> A = {0, 1, 0, -17.13};
Matrix<2, 1> B = {0, 2.46};
Matrix<1, 2> C = {1, 0};
Matrix<1, 2> K = {136.8767, 0.7485};
Matrix<2, 1> L = {62.87, 523.0369};

Matrix<2, 1> z_hat;
Matrix<2, 1> z_hat_dot;
Matrix<1, 1> u = {0};

void setup(){
  z_hat.Fill(0);
}

void loop(){
  //double y = encoderPos*K_encoder-x_sg;
  double y = 1.0;
  z_hat_dot = (A * z_hat) + (B * u) - (L * (C * z_hat - y));
  u = -K * z_hat;
}

The following I would expect to work as negation:

  candidate expects 2 arguments, 1 provided
   u = -K * z_hat;
        ^
exit status 1
no match for 'operator-' (operand types are 'BLA::Matrix<1, 1>' and 'double')

[EDIT] The following error makes sense as this would cast y down to a lower resolution. Strange however that it throws an error in 4.0.0 and not in 3.7.0 And the following, which I can "fix" by changing the declaration of y to be a float:

note:   deduced conflicting types for parameter 'DType' ('float' and 'double')
   z_hat_dot = (A * z_hat) + (B * u) - (L * (C * z_hat - y));
tomstewart89 commented 1 year ago

Hey @bobthefairycat , thanks for the information! I guess in my update to 4.0.0 I missed the unary operator-, I'll push a change for that in a moment.

As for the implicit casting from float to double that you pointed out, the cause is as you discovered on #64. It wasn't explicitly intended to disable these implicit casts in 4.0.0 but at the same time they're kind of notorious for being bug prone so I think it might be for the best that it's flagged as an error.

FWIW you can also use the Cast<T> to convert a Matrix it into a different datatype for example:

double y = 1.0;
Matrix<2,1, double> tmp = z_hat.Cast<double>() - y;
tomstewart89 commented 1 year ago

Ok, I reinstated the operator- with version 4.1. Let me know if you have any more trouble!

bobthefairycat commented 9 months ago

I appreciate the effort to fix the bug! To be "safe", I packaged out the project with instructions to use 3.7.0 (use case is educational in a very limited setting, so unlikely that the oversights allowed will be problematic).

Thanks again!