tomstewart89 / BasicLinearAlgebra

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

Initialize Matrix to 0 #45

Closed PJansky closed 2 years ago

PJansky commented 2 years ago

I would expect you could initialize a Matrix with zeros like this; BLA::Matrix<3, 3> A = {0}; Unfortunatly this doesn't seem to be the case. All subsequent values stay uninitialized . I'm aware the "HowToUse" doesn't explicitly say this should possible, but since you can do this BLA::Matrix<3, 3> B = {6.54, 3.66, 2.95, 3.22, 7.54, 5.12, 8.98, 9.99, 1.56}; I thought the afor mentioned way would also work.

Otherwise the Library works as advertised :) Keep up the good work!

tomstewart89 commented 2 years ago

Hey @PJansky thanks for the info, glad you're getting some use out of this library!

Yeah I guess that behavior is a bit unexpected. I guess I can add some code such that when an intiialiser list is passed that shorter than the length of the matrix, then consider the trailing elements as zero. This seems to be the rule with regular C-arrays (for example: https://godbolt.org/z/jKq84f5P7) so it makes sense to follow that convention here too.

In the meantime, you can zero-initialise a matrix using the Zeros type like so:

BLA::Matrix<3, 3> A = BLA::Zeros<3,3>();

Hope that helps!

tomstewart89 commented 2 years ago

This should be fixed in version 3.6, let me know if you have any trouble with it!