tomstewart89 / BasicLinearAlgebra

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

Suggestion: Assignment 1-d array #4

Closed danlkv closed 7 years ago

danlkv commented 7 years ago

Assignment of 1-d array as Matrix<3> M = float array[3] would be be usefull. It only works if i use Matrix<3> = float[3][1], and thus the float array has to be converted. Maybe there's some another handy way to assign the array?

tomstewart89 commented 7 years ago

Ok, all done here too. For assigning matrices you now have a couple of options:

You can pass an initialiser list which will fill the matrix in row major order when it's constructed like so: Matrix<3,3> A = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};

Or after it's been constructed you can use FillRowMajor like so: A.FillRowMajor(1.0, 2.0, 3.0, 4.0, 5.0, 6.0);

Both of these functions will check that you've supplied the correct number of parameters to the matrix and will generate a compiler error if you've tried to pass too many arguments, for example you could try compiling this:

A.FillRowMajor(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0); // compiler error

I've also added comma initialisation (same as in the eigen library) so that you can fill up matrices like this:

A << 1.0, 2.0, 3.0, 4.0, 5.0, 6.0;

I quite like this syntax but unfortunately it doesn't allow for compile time checking of the number of arguments you pass in, so if you try to pass too many, like this:

A << 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0;

The 7.0 will just be silently ignored, so be careful there.

Hope that's helpful, let me know if I've missed anything!

danlkv commented 7 years ago

Thanks for your answer! Can i assign already defined float 1-d array in some handy ways like these?