Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
n = norm2( data )
data may be a multidimensional array (e.g. data(:,:)). n is the Frobenius norm if data is a 2-dimensional array (matrix).
func Euclidean(data []float64) float64 {
n := 0.0
for _, val := range data {
n += val * val
}
return math.Sqrt(n)
const n = Math.hypot(...data)
Spread syntax requires ES6 or newer
var n = Math.hypot.apply(null, data)
If support for older browsers is necessary.
double n = 0d;
for(double value : data) {
n += value * value;
}
n = Math.sqrt(n);
my $data = [5.0, 4.0, 3.0, 2.0, 1.0];
my $n = gsl_blas_dnrm2(Math::GSL::Vector->new($data)->raw);
data := #( 5 4 3 2 1 ).
n := data squared sum sqrt.
No builtin but this is pretty straight forward.