-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL2.js
36 lines (24 loc) · 1 KB
/
L2.js
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
// Function: L2()
// Authors: Corey Devin Anderson and Kirankumar Batchu
//------------------------------------------------------------------------------
// Description:
// Calculates the L2 norms of a 1D Array. Ignores value in calculation if null.
//------------------------------------------------------------------------------
// Parameters:
// a : 1D Array for which the L2 norm will be calculated
// Returns:
// A scalar representing the L2 norm
// Dependencies:
// none
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Function: L2()
// START
function L2(a) {
let squares = a.map(function(x){if (x != null) {return (x) ** 2} else {return null}});
let sum = 0;
squares.forEach(element => sum = sum + element); // Sum the squares
return(sum ** 0.5); // return the square root of the sums of squares
}
// END
//------------------------------------------------------------------------------