Description Usage Arguments Author(s) Examples
Computes the projective decomposition of a real-valued matrix.
1 |
A |
|
D.start |
|
method |
|
precision |
|
max.iter |
|
verbose |
|
digits |
Max Robinson
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | ##---- Should be DIRECTLY executable !! ----
##-- ==> Define data, use random,
##-- or do help(data=index) for the standard data sets.
## The function is currently defined as
function (A, D.start = NULL, method = "Sinkhorn", precision = 1e-06,
max.iter = 1000, verbose = FALSE, digits = 3)
{
m <- dim(A)[1]
n <- dim(A)[2]
sqrt.m <- sqrt(m)
sqrt.n <- sqrt(n)
sqrt.mn <- sqrt.m * sqrt.n
D <- list(scalar = Frobenius(A)/sqrt.mn, row.factor = vector(mode = "numeric",
length = m), col.factor = vector(mode = "numeric", length = n),
precision = precision, iterations = 0)
tol <- sqrt(2) * precision
W2 <- as(A, "dgTMatrix")
W2@x <- W2@x/D$scalar
W2@x <- W2@x * W2@x
a2 <- as.vector(W2@x)
if (is.null(D.start) || ((m != length(D.start$row.factor)) &&
(n != length(D.start$col.factor)))) {
D$row.factor <- sqrt(rowSums(W2)/n)
D$col.factor <- sqrt(colSums(W2)/m)
}
else {
if (m == length(D.start$row.factor)) {
if (n == length(D.start$col.factor)) {
if (verbose) {
print("Reusing row and column factors")
}
D$row.factor <- D.start$row.factor * D.start$row.factor
D$col.factor <- D.start$col.factor * D.start$col.factor
}
else {
if (verbose) {
print("Reusing row factors")
}
D$row.factor <- D.start$row.factor * D.start$row.factor
W2@x <- a2/D$row.factor[1 + W2@i]
D$col.factor <- colSums(W2)/m
W2@x <- a2/(D$row.factor[1 + W2@i] * D$col.factor[1 +
W2@j])
D$row.factor <- D$row.factor * (rowSums(W2)/n)
}
}
else {
if (verbose) {
print("Reusing column factors")
}
D$col.factor <- D.start$col.factor * D.start$col.factor
W2@x <- a2/D$col.factor[1 + W2@j]
D$row.factor <- rowSums(W2)/n
W2@x <- a2/(D$row.factor[1 + W2@i] * D$col.factor[1 +
W2@j])
D$col.factor <- D$col.factor * (colSums(W2)/m)
}
}
W2@x <- a2/(D$row.factor[1 + W2@i] * D$col.factor[1 + W2@j])
style <- toupper(substr(method, 1, 1))
for (iter in c(1:max.iter)) {
rL <- rowSums(W2)/n
cL <- colSums(W2)/m
coeff.v <- max(cv(cL[cL > 0]), cv(rL[rL > 0]))
if (verbose) {
print(paste(iter, signif(coeff.v, digits), date()))
}
if (coeff.v < tol) {
break
}
if ("S" == style) {
D$row.factor <- D$row.factor * rL
W2@x <- a2/(D$row.factor[1 + W2@i] * D$col.factor[1 +
W2@j])
D$col.factor <- D$col.factor * cL
}
else if ("R" == style) {
D$col.factor <- D$col.factor * sqrt(cL)
D$row.factor <- D$row.factor * sqrt(rL)
}
else {
if (1 == (iter%%2)) {
D$col.factor <- D$col.factor * sqrt(cL)
D$row.factor <- D$row.factor * sqrt(rL)
}
else {
D$row.factor <- D$row.factor * rL
W2@x <- a2/(D$row.factor[1 + W2@i] * D$col.factor[1 +
W2@j])
D$col.factor <- D$col.factor * cL
}
}
W2@x <- a2/(D$row.factor[1 + W2@i] * D$col.factor[1 +
W2@j])
}
D$row.factor <- sqrt(D$row.factor)
D$col.factor <- sqrt(D$col.factor)
D$iterations <- iter
class(D) <- "ProjectiveDecomposition"
D
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.