-
Notifications
You must be signed in to change notification settings - Fork 2
/
_vector_norm.gml
44 lines (37 loc) · 995 Bytes
/
_vector_norm.gml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/// @func _vector_norm(v[, p])
/// @desc Calculates a vector Lp-norm.
/// @param {real[]} v - Vector to evaluate the norm of.
/// @param {real} [p=2] - Parameter to define Lp-norm (must be between 1 and infinity, inclusive).
/// @return {real} Lp-norm of v (or -1 if an error occurs).
function _vector_norm(v)
{
// Check for optional p argument
var p = (argument_count > 1 ? argument[1] : 2);
// Verify that p >= 1
if (p < 1)
return -1;
// Use separate procedures for finite and infinite p-norms
if (p == infinity)
{
// Infinity-norm
// Calculate the maximum value of |vi|
var maxval = -infinity;
for (var i = 0; i < array_length(v); i++)
{
if (abs(v[i]) > maxval)
maxval = abs(v[i]);
}
// Return the maximum
return maxval;
}
else
{
// Finite p-norm
// Calculate the sum of |vi|^p
var tot = 0;
for (var i = 0; i < array_length(v); i++)
tot += power(abs(v[i]), p);
// Return the 1/p power of the sum
return power(tot, 1/p);
}
}