-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathnorm.js
More file actions
executable file
·26 lines (25 loc) · 913 Bytes
/
norm.js
File metadata and controls
executable file
·26 lines (25 loc) · 913 Bytes
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
import { euclidean } from "../metrics/index.js";
import { Matrix } from "../matrix/index.js";
//import { neumair_sum } from "../numerical/index";
/**
* Computes the norm of a vector, by computing its distance to **0**.
* @memberof module:matrix
* @alias norm
* @param {Matrix|Array<Number>|Float64Array} v - Vector.
* @param {Function} [metric = euclidean] - Which metric should be used to compute the norm.
* @returns {Number} - The norm of {@link v}.
*/
export default function (v, metric = euclidean) {
let vector = null;
if (v instanceof Matrix) {
let [rows, cols] = v.shape;
if (rows === 1) vector = v.row(0);
else if (cols === 1) vector = v.col(0);
else throw new Error("Matrix must be 1d!");
} else {
vector = v;
}
const n = vector.length;
const zeros = Float64Array.from({ length: n }, () => 0);
return metric(vector, zeros);
}