@@ -3,38 +3,6 @@ library(testthat)
33library(patrick )
44library(spectralGraphTopology )
55
6- # lambda update step using CVX for the sake of unit testing
7- lambda_update_cvx <- function (lb , ub , beta , U , Lw , k ) {
8- n <- ncol(Lw )
9- d <- diag(t(U ) %*% Lw %*% U )
10- q <- n - k
11- lambda <- CVXR :: Variable(q )
12- objective <- CVXR :: Minimize(sum(.5 * beta * (lambda - d )^ 2 - log(lambda )))
13- constraints <- list (lambda [q ] < = ub , lambda [1 ] > = lb , lambda [2 : q ] > = lambda [1 : (q - 1 )])
14- prob <- CVXR :: Problem(objective , constraints )
15- result <- solve(prob )
16- return (as.vector(result $ getValue(lambda )))
17- }
18-
19-
20- psi_update_cvx <- function (V , Aw ) {
21- c <- diag(t(V ) %*% Aw %*% V )
22- q <- length(c )
23- psi <- CVXR :: Variable(q )
24- objective <- CVXR :: Minimize(sum((psi - c ) ^ 2 ))
25- constraints <- list (psi [1 : (q - 1 )] < psi [2 : q ])
26- j <- length(constraints ) + 1
27- i <- 1
28- while (i < q + 1 - i ) {
29- constraints [[j ]] <- psi [i ] == - psi [q + 1 - i ]
30- j <- j + 1
31- i <- i + 1
32- }
33- prob <- CVXR :: Problem(objective , constraints )
34- result <- solve(prob )
35- return (as.vector(result $ getValue(psi )))
36- }
37-
386
397test_that(" test that U remains orthonormal after being updated" , {
408 w <- runif(1000 )
@@ -62,43 +30,73 @@ test_that("test that V remains orthonormal after being updated", {
6230})
6331
6432
65- test_that(" test that the eigenvalues of the adjacency matrix meet the criterion" , {
66- skip_if_not_installed(" CVXR" )
67- w <- runif(4 * 9 )
68- n <- as.integer(.5 * (1 + sqrt(1 + 8 * length(w ))))
69- z <- 3
70- Aw <- A(w )
71- V <- bipartite.V_update(Aw , z )
72- psi <- bipartite.psi_update(V , Aw )
73- psi_cvx <- psi_update_cvx(V , Aw )
74- expect_equal(psi , psi_cvx , tolerance = 1e-4 )
75- })
76-
77-
78- with_parameters_test_that(" test that the eigenvalues of the Laplacian matrix
79- meet the criterion after being updated" , {
80- skip_if_not_installed(" CVXR" )
81- n <- as.integer(.5 * (1 + sqrt(1 + 8 * length(w ))))
82- k <- 1
83- Lw <- L(w )
84- U <- laplacian.U_update(Lw , k )
85- beta <- .5
86- q <- n - k
33+ # test_that("test that the eigenvalues of the adjacency matrix meet the criterion", {
34+ # skip_if_not_installed("CVXR")
35+ # psi_update_cvx <- function(V, Aw) {
36+ # c <- diag(t(V) %*% Aw %*% V)
37+ # q <- length(c)
38+ # psi <- CVXR::Variable(q)
39+ # objective <- CVXR::Minimize(sum((psi - c) ^ 2))
40+ # constraints <- list(psi[1:(q - 1)] < psi[2:q])
41+ # j <- length(constraints) + 1
42+ # i <- 1
43+ # while (i < q + 1 - i) {
44+ # constraints[[j]] <- psi[i] == -psi[q + 1 - i]
45+ # j <- j + 1
46+ # i <- i + 1
47+ # }
48+ # prob <- CVXR::Problem(objective, constraints)
49+ # result <- solve(prob)
50+ # return(as.vector(result$getValue(psi)))
51+ # }
52+ # w <- runif(4*9)
53+ # n <- as.integer(.5 * (1 + sqrt(1 + 8 * length(w))))
54+ # z <- 3
55+ # Aw <- A(w)
56+ # V <- bipartite.V_update(Aw, z)
57+ # psi <- bipartite.psi_update(V, Aw)
58+ # psi_cvx <- psi_update_cvx(V, Aw)
59+ # expect_equal(psi, psi_cvx, tolerance = 1e-4)
60+ # })
8761
88- lambda <- laplacian.lambda_update(lb , ub , beta , U , Lw , k )
89- expect_true(length(lambda ) == q )
90- expect_true(all(lambda [1 ] > = lb , lambda [q ] < = ub ,
91- lambda [2 : q ] > = lambda [1 : (q - 1 )]))
9262
93- # compare against results from CVXR
94- lambda_cvx <- lambda_update_cvx(lb , ub , beta , U , Lw , k )
95- expect_true(length(lambda_cvx ) == q )
96- expect_true(all(abs(lambda_cvx - lambda ) < 1e-3 ))
97- },
98- cases(
99- list (lb = 1e-2 , ub = 10 , w = runif(1000 )),
100- list (lb = 1e-2 , ub = 1.5 , w = runif(6 )),
101- list (lb = 3 , ub = 100 , w = runif(20 )),
102- list (lb = 3.3 , ub = 4 , w = runif(20 ))
103- )
104- )
63+ # with_parameters_test_that("test that the eigenvalues of the Laplacian matrix
64+ # meet the criterion after being updated", {
65+ # skip_if_not_installed("CVXR")
66+ #
67+ # lambda_update_cvx <- function(lb, ub, beta, U, Lw, k) {
68+ # n <- ncol(Lw)
69+ # d <- diag(t(U) %*% Lw %*% U)
70+ # q <- n - k
71+ # lambda <- CVXR::Variable(q)
72+ # objective <- CVXR::Minimize(sum(.5 * beta * (lambda - d)^2 - log(lambda)))
73+ # constraints <- list(lambda[q] <= ub, lambda[1] >= lb, lambda[2:q] >= lambda[1:(q-1)])
74+ # prob <- CVXR::Problem(objective, constraints)
75+ # result <- solve(prob)
76+ # return(as.vector(result$getValue(lambda)))
77+ # }
78+ #
79+ # n <- as.integer(.5 * (1 + sqrt(1 + 8 * length(w))))
80+ # k <- 1
81+ # Lw <- L(w)
82+ # U <- laplacian.U_update(Lw, k)
83+ # beta <- .5
84+ # q <- n - k
85+ #
86+ # lambda <- laplacian.lambda_update(lb, ub, beta, U, Lw, k)
87+ # expect_true(length(lambda) == q)
88+ # expect_true(all(lambda[1] >= lb, lambda[q] <= ub,
89+ # lambda[2:q] >= lambda[1:(q-1)]))
90+ #
91+ # # compare against results from CVXR
92+ # lambda_cvx <- lambda_update_cvx(lb, ub, beta, U, Lw, k)
93+ # expect_true(length(lambda_cvx) == q)
94+ # expect_true(all(abs(lambda_cvx - lambda) < 1e-3))
95+ # },
96+ # cases(
97+ # list(lb = 1e-2, ub = 10, w = runif(1000)),
98+ # list(lb = 1e-2, ub = 1.5, w = runif(6)),
99+ # list(lb = 3, ub = 100, w = runif(20)),
100+ # list(lb = 3.3, ub = 4, w = runif(20))
101+ # )
102+ # )
0 commit comments