-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathnorm.js
More file actions
executable file
·36 lines (33 loc) · 1001 Bytes
/
norm.js
File metadata and controls
executable file
·36 lines (33 loc) · 1001 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
27
28
29
30
31
32
33
34
35
36
import { euclidean } from "../metrics/index";
import { Matrix } from "../matrix/index";
//import { neumair_sum } from "../numerical/index";
export default function(v, metric = euclidean) {
//export default function(vector, p=2, 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 "matrix must be 1d!"
} else {
vector = v;
}
let n = vector.length;
let z = new Array(n)
z.fill(0);
return metric(vector, z);
/*let v;
if (vector instanceof Matrix) {
let [ rows, cols ] = v.shape;
if (rows === 1) {
v = vector.row(0);
} else if (cols === 1) {
v = vector.col(0);
} else {
throw "matrix must be 1d"
}
} else {
v = vector;
}
return Math.pow(neumair_sum(v.map(e => Math.pow(e, p))), 1 / p)*/
}