-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions_on_graph.Rmd
439 lines (401 loc) · 19.6 KB
/
functions_on_graph.Rmd
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
---
title: "Functions on metric graphs"
date: "Created: 05-07-2024. Last modified: `r format(Sys.time(), '%d-%m-%Y.')`"
output:
html_document:
mathjax: "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"
highlight: pygments
theme: flatly
code_folding: show # class.source = "fold-hide" to hide code and add a button to show it
# df_print: paged
toc: true
toc_float:
collapsed: true
smooth_scroll: true
number_sections: false
fig_caption: true
code_download: true
always_allow_html: true
bibliography:
- references.bib
- grateful-refs.bib
header-includes:
- \newcommand{\ar}{\mathbb{R}}
- \newcommand{\llav}[1]{\left\{#1\right\}}
- \newcommand{\pare}[1]{\left(#1\right)}
- \newcommand{\Ncal}{\mathcal{N}}
- \newcommand{\Vcal}{\mathcal{V}}
- \newcommand{\Ecal}{\mathcal{E}}
---
```{r xaringanExtra-clipboard, echo = FALSE}
htmltools::tagList(
xaringanExtra::use_clipboard(
button_text = "<i class=\"fa-solid fa-clipboard\" style=\"color: #00008B\"></i>",
success_text = "<i class=\"fa fa-check\" style=\"color: #90BE6D\"></i>",
error_text = "<i class=\"fa fa-times-circle\" style=\"color: #F94144\"></i>"
),
rmarkdown::html_dependency_font_awesome()
)
```
```{css, echo = FALSE}
body .main-container {
max-width: 100% !important;
width: 100% !important;
}
body {
max-width: 100% !important;
}
body, td {
font-size: 16px;
}
code.r{
font-size: 14px;
}
pre {
font-size: 14px
}
.caption {
margin: auto;
text-align: center;
margin-bottom: 20px; /* Spacing below the box */
}
```
Go back to the [About page](about.html).
Go back to the [Basis Functions](basis_functions.html) page.
Let us set some global options for all code chunks in this document.
```{r}
# Set seed for reproducibility
set.seed(1982)
# Set global options for all code chunks
knitr::opts_chunk$set(
# Disable messages printed by R code chunks
message = FALSE,
# Disable warnings printed by R code chunks
warning = FALSE,
# Show R code within code chunks in output
echo = TRUE,
# Include both R code and its results in output
include = TRUE,
# Evaluate R code chunks
eval = TRUE,
# Enable caching of R code chunks for faster rendering
cache = FALSE,
# Align figures in the center of the output
fig.align = "center",
# Enable retina display for high-resolution figures
retina = 2,
# Show errors in the output instead of stopping rendering
error = TRUE,
# Do not collapse code and output into a single block
collapse = FALSE
)
# Start the figure counter
fig_count <- 0
# Define the captioner function
captioner <- function(caption) {
fig_count <<- fig_count + 1
paste0("Figure ", fig_count, ": ", caption)
}
```
# Import libraries
```{r}
library(MetricGraph)
library(dplyr)
library(plotly)
library(here)
library(rmarkdown)
library(grateful) # to cite
```
# Building the graph object
```{r}
V <- rbind(c(0, 0), c(1, 0), c(1, 1), c(0, 1), c(-1, 1), c(-1, 0), c(0, -1))
E <- rbind(c(1, 2), c(2, 3), c(3, 4), c(4, 5),
c(5, 6), c(6, 1), c(4, 1),c(1, 7))
graph <- metric_graph$new(V = V, E = E)
```
# Geodesic distance on a metric graph {#geodesic}
Figure 1 below shows an illustration of a compact and connected graph $\Gamma = (\mathcal{V},\mathcal{E})$, where $\mathcal{E}= \{e_1,\dots,e_8\}$ (black lines) and $\mathcal{V} = \{v_1,\dots,v_7\}$ (blue points).
<div style="color: blue;">
********
**Press the Show button below to reveal the code.**
********
</div>
```{r, fig.height = 8, out.width = "100%", class.source = "fold-hide", fig.cap = captioner("Illustration of a compact and connected graph $\\Gamma = \\pare{\\Vcal,\\Ecal}$, where $\\Ecal= \\llav{e_1,\\dots,e_8}$ (black) and $\\Vcal = \\llav{v_1,\\dots,v_7}$ (blue). Notice that $\\ell_{e_i} = 1$ for $i = 1,\\dots,8$. Note also that, for example, $\\Ecal_{v_2} = \\llav{e_1,e_2,e_5,e_8}$ and therefore $d_{v_2} = 4.$ Given two points $s_1 = (e_3, 0.5)$ and $s_2 = (e_1, 0.5)$, we have that $d(s_1,s_2) = 2$ (red). Following the convention mentioned in the text, we may write, for example, $e_8 = (v_2,v_7) \\cong [0,\\ell_{e_8}]$.")}
df = data.frame(x = c(0.5,0), y = c(-1,-1), z = c(0,0))
df2 = data.frame(x = c(0,0), y = c(-1,0), z = c(0,0))
df3 = data.frame(x = c(0,-0.5), y = c(0,0), z = c(0,0))
graph$build_mesh(h = 1/40)
x <- graph$mesh$V[, 1]
y <- graph$mesh$V[, 2]
f <- x^2 - y^2
graph$plot(plotly = TRUE, vertex_size = 5, vertex_color = "blue",
edge_color = "black", edge_width = 2) %>%
config(mathjax = 'cdn') %>%
layout(title = TeX("\\text{Geodesic distance on a metric graph }\\Gamma = (\\mathcal{V},\\mathcal{E})"),
showlegend = FALSE,
font = list(family = "Palatino"),
scene = list(
aspectratio = list(x = 1.6, y = 1.6, z = 1),
annotations = list(
list(
x = -1, y = 0, z = 0,
text = TeX("v_1"),
textangle = 0, ax = 0, ay = -25,
font = list(color = "black", size = 20),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 0, y = 0, z = 0,
text = TeX("v_2"),
textangle = 0, ax = 0, ay = -25,
font = list(color = "black", size = 20),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 0, y = -1, z = 0,
text = TeX("v_3"),
textangle = 0, ax = 0, ay = -25,
font = list(color = "black", size = 20),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 1, y = -1, z = 0,
text = TeX("v_4"),
textangle = 0, ax = 0, ay = -25,
font = list(color = "black", size = 20),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 1, y = 0, z = 0,
text = TeX("v_5"),
textangle = 0, ax = 0, ay = -25,
font = list(color = "black", size = 20),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 1, y = 1, z = 0,
text = TeX("v_6"),
textangle = 0, ax = 0, ay = -25,
font = list(color = "black", size = 20),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 0, y = 1, z = 0,
text = TeX("v_7"),
textangle = 0, ax = 0, ay = -25,
font = list(color = "black", size = 20),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 0.5, y = -1, z = 0,
text = TeX("s_1"),
textangle = 0, ax = 0, ay = -25,
font = list(color = "black", size = 20),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = -0.5, y = 0, z = 0,
text = TeX("s_2"),
textangle = 0, ax = 0, ay = -25,
font = list(color = "black", size = 20),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = -0.5, y = 0, z = 0,
text = TeX("e_1"),
textangle = 0, ax = 0, ay = 10,
font = list(color = "black", size = 20),
arrowcolor = "white", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 0, y = -0.5, z = 0,
text = TeX("e_2"),
textangle = 0, ax = 0, ay = 10,
font = list(color = "black", size = 20),
arrowcolor = "white", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 0.5, y = -1, z = 0,
text = TeX("e_3"),
textangle = 0, ax = 0, ay = 10,
font = list(color = "black", size = 20),
arrowcolor = "white", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 1, y = -0.5, z = 0,
text = TeX("e_4"),
textangle = 0, ax = 0, ay = 10,
font = list(color = "black", size = 20),
arrowcolor = "white", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 0.5, y = 0, z = 0,
text = TeX("e_5"),
textangle = 0, ax = 0, ay = 10,
font = list(color = "black", size = 20),
arrowcolor = "white", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 1, y = 0.5, z = 0,
text = TeX("e_6"),
textangle = 0, ax = 0, ay = 10,
font = list(color = "black", size = 20),
arrowcolor = "white", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 0.5, y = 1, z = 0,
text = TeX("e_7"),
textangle = 0, ax = 0, ay = 10,
font = list(color = "black", size = 20),
arrowcolor = "white", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 0, y = 0.5, z = 0,
text = TeX("e_8"),
textangle = 0, ax = 0, ay = 10,
font = list(color = "black", size = 20),
arrowcolor = "white", arrowsize = 1, arrowwidth = 1,arrowhead = 1)
))) %>%
add_trace(x = 0.5, y = -1, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 5, color = "red", symbol = 104)) %>%
add_trace(x = -0.5, y = 0, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 5, color = "red", symbol = 104)) %>%
add_trace(data = df, x = ~x, y = ~y, z = ~z, mode = "lines", type = "scatter3d",
line = list(width = 2, color = "red", symbol = 104)) %>%
add_trace(data = df2, x = ~x, y = ~y, z = ~z, mode = "lines", type = "scatter3d",
line = list(width = 2, color = "red", symbol = 104)) %>%
add_trace(data = df3, x = ~x, y = ~y, z = ~z, mode = "lines", type = "scatter3d",
line = list(width = 2, color = "red", symbol = 104))
```
# Function on a metric graph {#functions-def-on-graph}
Figure 2 below provides an illustration of a function defined on a graph.
<div style="color: blue;">
********
**Press the Show button below to reveal the code.**
********
</div>
```{r, fig.height = 8, out.width = "100%", class.source = "fold-hide", fig.cap = captioner("Illustration of a function $f$ (in blue) defined on the graph $\\Gamma= (\\Vcal,\\Ecal)$, where $\\Ecal= \\llav{e_1,\\dots,e_8}$ and $\\Vcal = \\llav{v_1,\\dots,v_7}$. Function $f:\\Gamma\\longrightarrow\\ar$ is the restriction of the surface $\\gamma:\\ar^2\\longrightarrow\\ar$ to $\\Gamma$, where $\\gamma(x,y) = x^2-y^2$. That is, $f= \\gamma\\big|_\\Gamma$.")}
graph$build_mesh(h = 1/40)
x <- graph$mesh$V[, 1]
y <- graph$mesh$V[, 2]
f <- x^2 - y^2
df = data.frame(x = c(0.5,0), y = c(-1,-1), z = c(0,0))
df2 = data.frame(x = c(0,0), y = c(-1,0), z = c(0,0))
df3 = data.frame(x = c(0,-0.5), y = c(0,0), z = c(0,0))
graph$plot_function(f,plotly = TRUE, vertex_size = 5, vertex_color = "blue", line_color = "blue",
edge_color = "black", edge_width = 2) %>%
config(mathjax = 'cdn') %>%
layout(title = TeX("\\text{Function on a metric graph }\\Gamma = (\\mathcal{V},\\mathcal{E})"),
showlegend = FALSE,
font = list(family = "Palatino"),
scene = list(
aspectratio = list(x = 1.6, y = 1.6, z = 1),
annotations = list(
list(
x = -1, y = 0, z = 0,
text = TeX("v_1"),
textangle = 0, ax = 0, ay = -25,
font = list(color = "black", size = 16),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 0, y = 0, z = 0,
text = TeX("v_2"),
textangle = 0, ax = 0, ay = -25,
font = list(color = "black", size = 16),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 0, y = -1, z = 0,
text = TeX("v_3"),
textangle = 0, ax = 0, ay = -25,
font = list(color = "black", size = 16),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 1, y = -1, z = 0,
text = TeX("v_4"),
textangle = 0, ax = 0, ay = -25,
font = list(color = "black", size = 16),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 1, y = 0, z = 0,
text = TeX("v_5"),
textangle = 0, ax = 0, ay = -25,
font = list(color = "black", size = 16),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 1, y = 1, z = 0,
text = TeX("v_6"),
textangle = 0, ax = 0, ay = -25,
font = list(color = "black", size = 16),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 0, y = 1, z = 0,
text = TeX("v_7"),
textangle = 0, ax = 0, ay = -25,
font = list(color = "black", size = 16),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 0.5, y = -1, z = 0,
text = TeX("s_1"),
textangle = 0, ax = 0, ay = -25,
font = list(color = "black", size = 16),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = -0.5, y = 0, z = 0,
text = TeX("s_2"),
textangle = 0, ax = 0, ay = -25,
font = list(color = "black", size = 16),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = -0.5, y = 0, z = 0,
text = TeX("e_1"),
textangle = 0, ax = 0, ay = 10,
font = list(color = "black", size = 16),
arrowcolor = "white", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 0, y = -0.5, z = 0,
text = TeX("e_2"),
textangle = 0, ax = 0, ay = 10,
font = list(color = "black", size = 16),
arrowcolor = "white", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 0.5, y = -1, z = 0,
text = TeX("e_3"),
textangle = 0, ax = 0, ay = 10,
font = list(color = "black", size = 16),
arrowcolor = "white", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 1, y = -0.5, z = 0,
text = TeX("e_4"),
textangle = 0, ax = 0, ay = 10,
font = list(color = "black", size = 16),
arrowcolor = "white", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 0.5, y = 0, z = 0,
text = TeX("e_5"),
textangle = 0, ax = 0, ay = 10,
font = list(color = "black", size = 16),
arrowcolor = "white", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 1, y = 0.5, z = 0,
text = TeX("e_6"),
textangle = 0, ax = 0, ay = 10,
font = list(color = "black", size = 16),
arrowcolor = "white", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 0.5, y = 1, z = 0,
text = TeX("e_7"),
textangle = 0, ax = 0, ay = 10,
font = list(color = "black", size = 16),
arrowcolor = "white", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 0, y = 0.5, z = 0,
text = TeX("e_8"),
textangle = 0, ax = 0, ay = 10,
font = list(color = "black", size = 16),
arrowcolor = "white", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 0, y = -1, z = 1,
text = TeX("f=\\gamma\\big|_\\Gamma"),
textangle = 0, ax = 60, ay = 0,
font = list(color = "blue", size = 16),
arrowcolor = "white", arrowsize = 1, arrowwidth = 1,arrowhead = 1)
))) %>%
add_trace(x = 0.5, y = -1, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 5, color = "red", symbol = 104)) %>%
add_trace(x = -0.5, y = 0, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 5, color = "red", symbol = 104)) %>%
add_trace(data = df, x = ~x, y = ~y, z = ~z, mode = "lines", type = "scatter3d",
line = list(width = 2, color = "red", symbol = 104)) %>%
add_trace(data = df2, x = ~x, y = ~y, z = ~z, mode = "lines", type = "scatter3d",
line = list(width = 2, color = "red", symbol = 104)) %>%
add_trace(data = df3, x = ~x, y = ~y, z = ~z, mode = "lines", type = "scatter3d",
line = list(width = 2, color = "red", symbol = 104))
```
# References
```{r}
cite_packages(output = "paragraph", out.dir = ".")
```