-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
218 lines (195 loc) · 7.99 KB
/
app.R
File metadata and controls
218 lines (195 loc) · 7.99 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
library(Matrix)
library(shiny)
library(gtools)
library(igraph)
source("./R/Faces.R")
source("./R/Betti.R")
source("./R/Boundary.R")
source("./R/EulerCharacteristic.R")
source("./R/AbstractSimplicialComplex.R")
source("./R/VRComplex.R")
parse_points <- function(txt) {
lines <- strsplit(txt, "\n")[[1]]
lines <- trimws(lines)
lines <- lines[nchar(lines) > 0]
mat <- do.call(rbind, lapply(lines, function(ln) as.numeric(strsplit(ln, "[,\\s]+")[[1]][1:2])))
matrix(mat, ncol = 2, byrow = FALSE)
}
default_simplices <- list(c(1, 2), c(3, 4), c(2, 1, 3), c(4, 2))
default_input <- paste0(
"list(\n",
paste(sapply(default_simplices, function(x) paste0(" c(", paste(x, collapse = ", "), ")")), collapse = ",\n"),
"\n)"
)
default_points_txt <- "0,0
1,0
1,1
0,1"
ui <- fluidPage(
titlePanel("Simplicial Complex Explorer"),
sidebarLayout(
sidebarPanel(
radioButtons(
"mode", "Input Mode",
choices = c("Simplices (manual)" = "simp", "Data points → VR simplices" = "pts"),
selected = "simp"
),
conditionalPanel(
condition = "input.mode == 'simp'",
textAreaInput("simplices_input", "Input Simplices", value = default_input, rows = 10, width = "100%")
),
conditionalPanel(
condition = "input.mode == 'pts'",
textAreaInput("points_input", "Points (each line: x,y)", value = default_points_txt, rows = 6, width = "100%"),
numericInput("epsilon_pts", "ε (VR edge threshold)", value = 1.5, step = 0.1, min = 0)
),
tags$hr(),
numericInput("dim", "Target Dimension k", value = 0, min = 0, max = 10),
numericInput("tol", "tolerance (for Betti & Euler)", value = 0.1, step = 0.05),
actionButton("compute", "Compute", class = "btn-primary", width = "100%"),
width = 3
),
mainPanel(
conditionalPanel(
condition = "input.mode == 'pts'",
h4("Plot"),
plotOutput("vr_plot"),
tags$hr()
),
tabsetPanel(
tabPanel("Faces",
h4("Faces Interpretation"),
withMathJax(
HTML("simplicial complex outputs the faces of dimension \\( k \\)(points, edge,...)")
),
verbatimTextOutput("faces_out")
),
tabPanel("Boundary Matrix",
h4("Boundary Matrix \\( \\partial_k \\)"),
withMathJax(
HTML("$$\\partial_k \\sigma = \\sum_i (-1)^i [v_0\\ v_1\\ \\ldots\\ \\hat{v}_i\\ \\ldots\\ v_k]$$ <br/>
Boundary function returns a matrix, the shape is the dimension of the faces and it's lower one.
Given example, if you compute 1-dimensional boundary with the data above, you'll get a 4 (lower dimensional face)
x 5 (dimensiional face) matrix. <br/>
Now you get [1,2,3,4] and [(1,2), (3,4), (1,3), (2,3), (2,4)], which you can then get whether there's a
pair for 1 or -1 by using the formula above, or just empty.
In detail, the formula will be $${(-1)^1[1, (1,2)], (-1)^2[2, (1,2)], (-1)^3[3, (3,4)], (-1)^4[4, (3,4)],
(-1)^5[1, (1,3)], (-1)^6[3, (1,3)], (-1)^7[2, (2,3)], (-1)^8[3, (2,3)], (-1)^9[2, (2,4)], (-1)^10[4, (2,4)]}$$")
),
verbatimTextOutput("boundary_out"),
withMathJax(
HTML("The reason of using the formula is that it gives us the orientation of the face.<br/>
So the main reason of 1 and -1 is to let boundaries of boundaries equal zero :
$$\\partial_{k-1}\\partial_k=0$$<br/>
")
),
),
tabPanel("Betti Numbers",
h4("Betti Number \\( \\beta_k \\)"),
withMathJax(
HTML("Betti number represent the number of holes in k dimension.
\\( \\beta_0 \\) is the number of connected components,
\\( \\beta_1 \\) is the number of 1-dimensional loops, and so on.<br/>
The formula of betti number is : <br/>
\\(\\beta_k = \\text{rank} (\\text{ker} \\partial_k) - \\text{rank} (\\text{Im} \\partial_{k+1})\\),
then by replacing \\(\\text{ker} \\partial_k\\) with rank-nullity formula:<br/>
\\(\\text{rank} (\\text{ker} \\partial_k) +\\text{rank} (\\text{Im} \\partial_k) = \\text{dim} (\\partial_k)\\), you get:<br/>
$$\\beta_k = \\text{dim} (\\partial_k) - \\text{rank}(\\text{Im} \\partial_k)) - \\text{rank} (\\text{Im} \\partial_{k+1})$$<br/>
ker: How many closed structure, all k-cycles (no obundary k-chains)<br/>
Im: Which closed structures are the boundaries of high-dimensional objects. (k+1 dimensional boundaries)<br/>
Betti: The number of no boundaries k-chain, minus those that are actually some higher-dimensional boundary (quotient space)")
),
verbatimTextOutput("betti_out")
),
tabPanel("Euler Characteristic",
h4("Euler Characteristic \\( \\chi \\)"),
withMathJax(
HTML("Euler formula :\\[ \\chi = \\sum_{k=0}^{n} (-1)^k \\beta_k \\] <br/>
It returns the sum of the Betti numbers with alternating signs.
Different shapes could have the same Euler characteristic,
but it gives a global property of the simplicial complex.")
),
verbatimTextOutput("euler_out")
),
tabPanel("Abstract Simplicial Complex",
h4("Abstract Simplicial Complex"),
withMathJax(
HTML("Give a summary of the global structure of simplicial complex")
),
verbatimTextOutput("abstract_out")
)
),
width = 9
)
)
)
server <- function(
input, output, session
) {
simplices_reactive <- eventReactive(input$compute, {
if (input$mode == "simp") {
eval(parse(text = input$simplices_input))
} else {
pts <- parse_points(input$points_input)
vr <- VietorisRipsComplex(pts, input$epsilon_pts)
vr$simplices
}
}, ignoreInit = TRUE)
observeEvent(input$compute, {
tryCatch({
simplices <- simplices_reactive()
k <- input$dim
tol <- input$tol
if (input$mode == "pts") {
output$simplices_generated <- renderPrint({
print(simplices)
})
}
output$faces_out <- renderPrint({
cat("Faces in dim", k, ":\n")
print(faces(simplices, target_dim = k))
})
output$boundary_out <- renderPrint({
cat("Boundary matrix for ∂", k, ":\n")
print(boundary(simplices, k))
})
output$betti_out <- renderPrint({
res <- sapply(0:(k + 1), function(d) {
paste0("β_", d, " = ", betti_number(simplices, d, tol))
})
cat(paste(res, collapse = "\n"))
})
output$euler_out <- renderPrint({
chi <- euler_characteristic(simplices, tol)
cat("Euler Characteristic (χ):", chi)
})
output$abstract_out <- renderPrint({
cat("Abstract Simplicial Complex (dim", k, "):\n")
print(abstract_simplicial_complex(simplices, k, tol))
})
}, error = function(e) {
showNotification(paste("Error:", e$message), type = "error")
})
})
output$vr_plot <- renderPlot({
req(input$mode == "pts")
pts <- parse_points(input$points_input)
vr <- VietorisRipsComplex(pts, input$epsilon_pts)
g <- vr$network
req(!is.null(g))
xr <- range(pts[,1]); yr <- range(pts[,2])
par(xpd = NA)
plot(
g,
layout = as.matrix(pts),
vertex.label = 1:nrow(pts),
vertex.size = 12,
edge.arrow.mode = 0,
rescale = FALSE,
xlim = xr,
ylim = c(yr[1] - pad_y, yr[2] + pad_y),
asp = 0,
)
})
}
shinyApp(ui, server)