From 1918a44fd1f01da3fb3e6d16ef68ae7ac6d847d4 Mon Sep 17 00:00:00 2001 From: Brehel1 Date: Fri, 4 Jul 2025 09:30:25 +0200 Subject: [PATCH] Added test to check if sample satisfies all constraints and fixed some signs on the vignette --- tests/testthat/test-rlim.R | 14 +++++++ ...ional_polytope_sampling_with_samplelim.Rmd | 40 +++++++++---------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/tests/testthat/test-rlim.R b/tests/testthat/test-rlim.R index d277b92..976c787 100644 --- a/tests/testthat/test-rlim.R +++ b/tests/testthat/test-rlim.R @@ -25,6 +25,20 @@ test_that("rlim() works as expected for LIM object", { # Check type is correct expect_type(samp, "double") + # Check if every point lies within the polytope i.e satistifies both equalities and inequalities + + ## Check if every point satisfies all the equalities + eq_lin_combs <- BOWF$A %*% t(samp) + apply(eq_lin_combs, 2, function(col) { + expect_equal(col, BOWF$B) + }) + + ## Check if every point satisfies all the inequalities + ineq_lin_combs <- BOWF$G %*% t(samp) + apply(ineq_lin_combs, 2, function(col) { + expect_true(all(col >= BOWF$H)) + }) + # Check if every points are within the ranges ranges <- lim.ranges(BOWF) result <- apply(samp, 1, is_within_range, ranges=ranges) diff --git a/vignettes/high_dimensional_polytope_sampling_with_samplelim.Rmd b/vignettes/high_dimensional_polytope_sampling_with_samplelim.Rmd index 48be855..fa9540c 100644 --- a/vignettes/high_dimensional_polytope_sampling_with_samplelim.Rmd +++ b/vignettes/high_dimensional_polytope_sampling_with_samplelim.Rmd @@ -71,12 +71,12 @@ For the mere aim of illustration, we will consider in the following the minimal $$\begin{cases} 1x_1 + 1x_2 + 1x_3 = 1 \\ - 0x_1 + 0x_2 + 1x_3 \geq 0.7 \\ - 0x_1 + 0x_2 - 1x_3 \geq 0 \\ - 0x_1 + 1x_2 + 0x_3 \geq 0.8 \\ - 0x_1 - 1x_2 + 0x_3 \geq 0 \\ - 1x_1 + 0x_2 + 0x_3 \geq 0.8 \\ - -1x_1 + 0x_2 + 0x_3 \geq 0 \\ + 0x_1 + 0x_2 - 1x_3 \geq -0.7 \\ + 0x_1 + 0x_2 + 1x_3 \geq 0 \\ + 0x_1 - 1x_2 + 0x_3 \geq -0.8 \\ + 0x_1 + 1x_2 + 0x_3 \geq 0 \\ + - 1x_1 + 0x_2 + 0x_3 \geq -0.8 \\ + 1x_1 + 0x_2 + 0x_3 \geq 0 \\ \end{cases}$$ with associated matrices $$ @@ -87,19 +87,19 @@ with associated matrices $$ 1 \end{pmatrix}, \quad \mathbf{G} = \begin{pmatrix} - 0 & 0 & 1 \\ 0 & 0 & -1 \\ - 0 & 1 & 0 \\ + 0 & 0 & 1 \\ 0 & -1 & 0 \\ - 1 & 0 & 0 \\ - -1 & 0 &0 + 0 & 1 & 0 \\ + -1 & 0 & 0 \\ + 1 & 0 &0 \end{pmatrix},\quad \mathbf{H} = \begin{pmatrix} -0.7 \\ +-0.7 \\ 0 \\ -0.8 \\ +-0.8 \\ 0 \\ -0.8 \\ +-0.8 \\ 0 \end{pmatrix}$$. @@ -110,15 +110,15 @@ library("samplelim") # Define equality and inequality constraints through matrices A, B, G, H A <- matrix(c(1, 1, 1), nrow = 1, ncol = 3) B <- 1 -G <- -matrix(c(0, 0, 1, - 0, 0, -1, - 0, 1, 0, - 0, -1, 0, - 1, 0, 0, - -1, 0, 0), +G <- matrix(c(0, 0, -1, + 0, 0, 1, + 0, -1, 0, + 0, 1, 0, + -1, 0, 0, + 1, 0, 0), byrow = TRUE, nrow = 6, ncol = 3) -H <- -matrix(c(0.7, 0, 0.8, 0, 0.8, 0), nrow = 6) +H <- matrix(c(-0.7, 0, -0.8, 0, -0.8, 0), nrow = 6) # Store into a list lim_exm <- list(A = A, B = B,G = G,H = H) # Sampling into the polytope defined by these constraints