Skip to content

Instantly share code, notes, and snippets.

@alexpghayes
Created March 18, 2021 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexpghayes/e71fc4cf8110c72af012dc795228732f to your computer and use it in GitHub Desktop.
Save alexpghayes/e71fc4cf8110c72af012dc795228732f to your computer and use it in GitHub Desktop.
``` r
set.seed(27)
n <- 100
k <- 5
A <- matrix(rnorm(n * n), nrow = n, ncol = n)
s <- svd(A, k, k)
# generate two n x k orthonormal bases
u <- s$u
v <- s$v
# following vu and lei (2013) -- inefficient in space
frob_norm_sq1 <- norm(tcrossprod(u) - tcrossprod(v), type = "F")^2 / 2
# following rohe, chatterjee and yu (2011) -- efficient in space
s2 <- svd(crossprod(u, v))
frob_norm_sq2 <- k - sum(s2$d^2)
frob_norm_sq1
#> [1] 4.845742
frob_norm_sq2
#> [1] 4.845742
```
<sup>Created on 2021-03-18 by the [reprex package](https://reprex.tidyverse.org) (v1.0.0.9000)</sup>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment