norm method
Returns the norm (or length) of this vector.
The norm of a vector is the square root of the sum of the squares of its elements. It gives a measure of the magnitude (or length) of the vector.
Example:
var v = Vector.fromList([3, 4]);
print(v.norm());
Output:
5.0
Explanation:
The vector [3, 4] has a norm of 5 because sqrt(33 + 44) = sqrt(9 + 16) = sqrt(25) = 5.
Implementation
Complex norm([Norm normType = Norm.frobenius]) {
switch (normType) {
case Norm.frobenius:
return math
.sqrt(_data.map((value) => value * value).reduce((a, b) => a + b));
case Norm.manhattan:
return _data.map((value) => value.abs()).reduce((a, b) => a + b);
case Norm.chebyshev:
return _data
.map((value) => value.abs())
.reduce((a, b) => math.max(a, b));
case Norm.hamming:
return Complex(_data.where((value) => value != 0).length);
case Norm.cosine:
final magnitude = norm(Norm.frobenius);
final vDot = dot(this);
return vDot / magnitude;
// The below norms need more context to implement.
case Norm.mahalanobis:
throw UnimplementedError('Mahalanobis norm is not implemented');
case Norm.spectral:
case Norm.trace:
default:
throw Exception('Invalid norm type');
}
}