-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMatrixDist.R
More file actions
44 lines (38 loc) · 1.21 KB
/
MatrixDist.R
File metadata and controls
44 lines (38 loc) · 1.21 KB
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
MatrixDist <-
function (A, B, method = c('Frobenius', 'Riemann'), ...)
UseMethod ('MatrixDist')
FrobeniusDist <-
function (A, B)
### Computes Frobenius distance between two matrices
{
difference <- A - B
return (sqrt (sum (diag (t (difference) %*% difference))))
}
RiemannDist <-
function (A, B)
### Computes Riemannian distance between two matrices, following Moakher, 2005.
sqrt (sum (log (eigen (solve (A) %*% B) $ values) ^ 2))
MatrixDist.matrix <-
function (A, B, method = c('Frobenius', 'Riemann'))
{
DistFunc <- eval (parse (text = paste (method, 'Dist', sep = '')))
DistFunc (A, B)
}
MatrixDist.array <-
function (A, B = NULL, method = c('Frobenius', 'Riemann'), parallel = F)
{
DistFunc <- eval (parse (text = paste (method, 'Dist', sep = '')))
if (!is.null (B))
aaply (A, 3, DistFunc, B = B)
else
Pairwise (A, DistFunc, parallel = parallel)
}
MatrixDist.list <-
function (A, B = NULL, method = c('Frobenius', 'Riemann'), parallel = F)
{
DistFunc <- eval (parse (text = paste (method, 'Dist', sep = '')))
if (!is.null (B))
laply (A, DistFunc, B = B)
else
Pairwise (A, DistFunc, parallel = parallel)
}