Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compare "optimization" of internal sgccak code #25

Open
llrs opened this issue Feb 13, 2019 · 0 comments
Open

Compare "optimization" of internal sgccak code #25

llrs opened this issue Feb 13, 2019 · 0 comments
Assignees

Comments

@llrs
Copy link
Owner

llrs commented Feb 13, 2019

Optimization in the sgccak function

Original:

for (q in seq_len(J)) {
  if (mode(scheme) == "function") {
    dgx <- dg(cov2(Y, Y[, q], bias = bias))
    CbyCovq <- C[q, ] * dgx
  } else {
    if (scheme == "horst") {
      CbyCovq <- C[q, ]
    } else if (scheme == "factorial") {
      CbyCovq <- C[q, ] * 2 * cov2(Y, Y[, q], bias = bias)
    } else if (scheme == "centroid") {
      CbyCovq <- C[q, ] * sign(cov2(Y, Y[, q], bias = bias))
    }
  }

  Z[, q] <- rowSums(mapply("*", CbyCovq, as.data.frame(Y)))
  a[[q]] <- drop(crossprod(A[[q]], Z[, q, drop = FALSE]))
  a[[q]] <- soft.threshold(a[[q]], const[q])
  a[[q]] <- as.vector(a[[q]]) / norm2(a[[q]])
  Y[, q] <- drop(A[[q]] %*% a[[q]])
}

Proposed:

  if (mode(scheme) == "function") {
    dgx <- dg(cov2(Y, Y, bias = bias))
    CbyCovq <- C * dgx
  } else {
    if (scheme == "horst") {
      CbyCovq <- C
    } else if (scheme == "factorial") {
      CbyCovq <- C * 2 * cov2(Y, Y, bias = bias)
    } else if (scheme == "centroid") {
      CbyCovq <- C * sign(cov2(Y, Y, bias = bias))
    }
  }

Z <- t(tcrossprod(CbyCovq, Y))
for (q in seq_len(J)) {
  a[[q]] <- drop(crossprod(A[[q]], Z[, q, drop = FALSE]))
  a[[q]] <- soft.threshold(a[[q]], const[q])
  a[[q]] <- as.vector(a[[q]]) / norm2(a[[q]])
  Y[, q] <- drop(A[[q]] %*% a[[q]])
}

see related issues #16, #19, and the comment

@llrs llrs self-assigned this Feb 13, 2019
llrs pushed a commit that referenced this issue Sep 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant