tomstewart89 / BasicLinearAlgebra

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

Missing 2-Norm operator #15

Closed juanjqo closed 6 years ago

juanjqo commented 6 years ago

Hi Tom,

I want to compute the 2-norm operator of a vector. float n; BLA::Matrix<4,1> x = {1,2,3,4}; n = x.norm(); // n = 5.4772

Is it currently implemented?

Thank you for your time!

Att, Juan

tomstewart89 commented 6 years ago

Hey Juan, Norms aren't implemented but you can calculate one reasonably efficiently by doing an inner product like so:

BLA::Matrix<4,1> x = {1,2,3,4};
BLA::Matrix<1,1> inner = ~x * x;
n = sqrt(inner(0));

I'll admit that's a little convoluted but it should do the trick for now

juanjqo commented 6 years ago

Thanks tom!