library(testthat) library(Matrix) # For diagonal matrix operations and checks # Define a known symmetric matrix A and a positive definite matrix B A <- matrix(c(4, 1, 1, 2), nrow=2, byrow=TRUE) B <- matrix(c(6, 2, 2, 5), nrow=2, byrow=TRUE) # B must be symmetric and positive definite test_that("geigen method returns correct results", { result <- geneig(A = A, B = B, ncomp=2, method="geigen") expect_equal(dim(result$vectors), c(2, 2)) expect_equal(length(result$values), 2) # Eigenvalues can be complex for geigen, check real part if needed or just existence # expect_true(all(Re(result$values) > 0)) # This might not hold for general A }) test_that("robust method equivalent test using geigen", { # Changed method from "robust" to "geigen" as "robust" is removed result <- geneig(A = A, B = B, ncomp=2, method="geigen") expect_equal(dim(result$vectors), c(2, 2)) expect_equal(length(result$values), 2) # Cannot guarantee positive values for geigen # expect_true(all(result$values > 0)) }) test_that("sdiag method equivalent test using geigen", { # Changed method from "sdiag" to "geigen" as "sdiag" is removed result <- geneig(A = A, B = B, ncomp=2, method="geigen") expect_equal(dim(result$vectors), c(2, 2)) expect_equal(length(result$values), 2) # Cannot guarantee positive values for geigen # expect_true(all(result$values > 0)) }) test_that("primme method returns correct results", { skip_if_not_installed("PRIMME") # Skip if PRIMME is not available # Suppress expected warning about switching to dense backend for full decomposition result <- suppressWarnings(geneig(A = A, B = B, ncomp=2, method="primme", which="LA")) expect_equal(dim(result$vectors), c(2, 2)) expect_equal(length(result$values), 2) expect_true(all(result$values > 0)) }) test_that("non-square matrices are handled", { non_square_A <- matrix(1:6, nrow=2) non_square_B <- matrix(1:6, nrow=2) expect_error(geneig(A = non_square_A, B = non_square_B, ncomp=2, method="geigen")) }) test_that("negative and very small eigenvalues in B are handled in geigen", { B_with_negative <- matrix(c(4, 1, 1, -2), nrow=2, byrow=TRUE) # Changed method from "sdiag" to "geigen" # Suppress expected warning about negative eigenvalues result <- suppressWarnings(geneig(A = A, B = B_with_negative, ncomp=2, method="geigen")) expect_equal(dim(result$vectors), c(2, 2)) # Cannot guarantee positive values for geigen # expect_true(all(result$values > 0)) })