-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtest_cur.R
88 lines (69 loc) · 2.69 KB
/
test_cur.R
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
#devtools::use_package("testthat")
#library('testthat')
context("Deterministic CUR decomposition")
#Load rsvd library
library(rsvd)
#Set seed
set.seed(1234)
#Accuray
atol_float64 <- 1e-8
#*************************************************************************************
# Test: real input matrix
#*************************************************************************************
#Create real random test matrix of dimension m x n with target rank k
m = 50
n = 30
k = 10
testMat <- matrix(runif(m*k), m, k)
testMat <- testMat %*% t(testMat)
testMat <- testMat[,1:n]
#CUR decomposition
cur_out <- rcur(testMat, rand=FALSE)
testMat.re = cur_out$C %*% cur_out$U %*% cur_out$R
testthat::test_that("Test 1: CUR decomposition c=r=NULL", {
testthat::expect_equal(testMat, testMat.re)
})
testMat.re = testMat[,cur_out$C.idx] %*% cur_out$U %*% testMat[cur_out$R.idx,]
testthat::test_that("Test 2: CUR decomposition (column idx) c=r=NULL", {
testthat::expect_equal(testMat, testMat.re)
})
#CUR decomposition, k
cur_out <- rcur(testMat, k=k, rand=FALSE)
testMat.re = cur_out$C %*% cur_out$U %*% cur_out$R
testthat::test_that("Test 3: CUR decomposition k=k", {
testthat::expect_equal(testMat, testMat.re)
})
#CUR decomposition, k
cur_out <- rcur(H(testMat), k=k, rand=FALSE)
testMat.re = cur_out$C %*% cur_out$U %*% cur_out$R
testthat::test_that("Test 4: CUR decomposition k=k", {
testthat::expect_equal(H(testMat), testMat.re)
})
#*************************************************************************************
# Test: complex input matrix
#*************************************************************************************
testMat <- matrix(runif(m*k), m, k) + 1i* matrix(runif(m*k), m, k)
testMat <- testMat %*% H(testMat)
testMat <- testMat[,1:n]
#CUR decomposition
cur_out <- rcur(testMat, rand=FALSE)
testMat.re = cur_out$C %*% cur_out$U %*% cur_out$R
testthat::test_that("Test 5: CUR decomposition c=r=NULL", {
testthat::expect_equal(testMat, testMat.re)
})
testMat.re = testMat[,cur_out$C.idx] %*% cur_out$U %*% testMat[cur_out$R.idx,]
testthat::test_that("Test 6: CUR decomposition (column idx) c=r=NULL", {
testthat::expect_equal(testMat, testMat.re)
})
#CUR decomposition, k
cur_out <- rcur(testMat, k=k, rand=FALSE)
testMat.re = cur_out$C %*% cur_out$U %*% cur_out$R
testthat::test_that("Test 7: CUR decomposition k=k", {
testthat::expect_equal(testMat, testMat.re)
})
#CUR decomposition, k
cur_out <- rcur(H(testMat), k=k, rand=FALSE)
testMat.re = cur_out$C %*% cur_out$U %*% cur_out$R
testthat::test_that("Test 8: CUR decomposition k=k", {
testthat::expect_equal(H(testMat), testMat.re)
})