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
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="generator" content="litedown 0.8">
<title>Functional programming: Sectioning function to a smaller domain</title>
<style type="text/css">
body {
font-family: sans-serif;
max-width: 800px;
margin: auto;
padding: 1em;
line-height: 1.5;
print-color-adjust: exact;
-webkit-print-color-adjust: exact;
}
body, .abstract, code, .footnotes, footer, #refs, .caption { font-size: .9em; }
li li { font-size: .95em; }
ul:has(li > input[type="checkbox"]) { list-style: none; padding-left: 1em; }
*, :before, :after { box-sizing: border-box; }
a { color: steelblue; }
pre, img { max-width: 100%; }
pre { white-space: pre-wrap; word-break: break-word; }
pre code { display: block; padding: 1em; overflow-x: auto; }
code { font-family: 'DejaVu Sans Mono', 'Droid Sans Mono', 'Lucida Console', Consolas, Monaco, monospace; }
:not(pre, th) > code, code[class], div > .caption { background: #f8f8f8; }
pre > code:is(:not([class]), .language-plain, .language-none, .plain), .box, .figure, .table { background: inherit; border: 1px solid #eee; }
pre > code {
&.message { border-color: #9eeaf9; }
&.warning { background: #fff3cd; border-color: #fff3cd; }
&.error { background: #f8d7da; border-color: #f8d7da; }
}
.fenced-chunk { border-left: 1px solid #666; }
.code-fence {
opacity: .4;
border: 1px dashed #666;
border-left: 2px solid;
&:hover { opacity: inherit; }
}
.box, .figure, .table, table { margin: 1em auto; }
div > .caption { padding: 1px 1em; }
.figure { p:has(img, svg), pre:has(svg) { text-align: center; } }
.flex-col { display: flex; justify-content: space-between; }
table {
&:only-child:not(.table > *) { margin: auto; }
th, td { padding: 5px; font-variant-numeric: tabular-nums; }
thead, tfoot, tr:nth-child(even) { background: whitesmoke; }
thead th { border-bottom: 1px solid #ddd; }
&:not(.datatable-table) {
border-top: 1px solid #666;
border-bottom: 1px solid #666;
}
}
blockquote {
color: #666;
margin: 0;
padding: 1px 1em;
border-left: .5em solid #eee;
}
hr, .footnotes::before { border: 1px dashed #ddd; }
.frontmatter { text-align: center; }
#TOC {
a { text-decoration: none; }
ul { list-style: none; padding-left: 1em; }
& > ul { padding: 0; }
ul ul { border-left: 1px solid lightsteelblue; }
}
.body h2 { border-bottom: 1px solid #666; }
.body .appendix, .appendix ~ h2 { border-bottom-style: dashed; }
.main-number::after { content: "."; }
span[class^="ref-number-"] { font-weight: bold; }
.ref-number-fig::after, .ref-number-tab::after { content: ":"; }
.cross-ref-chp::before { content: "Chapter "; }
.cross-ref-sec::before { content: "Section "; }
.cross-ref-fig::before, .ref-number-fig::before { content: "Figure "; }
.cross-ref-tab::before, .ref-number-tab::before { content: "Table "; }
.cross-ref-eqn::before, .MathJax_ref:has(mjx-mtext > mjx-c + mjx-c)::before { content: "Equation "; }
.abstract, #refs {
&::before { display: block; margin: 1em auto; font-weight: bold; }
}
.abstract::before { content: "Abstract"; text-align: center; }
#refs::before { content: "Bibliography"; font-size: 1.5em; }
.ref-paren-open::before { content: "("; }
.ref-paren-close::after { content: ")"; }
.ref-semicolon::after { content: "; "; }
.ref-and::after { content: " and "; }
.ref-et-al::after { content: " et al."; font-style: italic; }
.footnote-ref a {
&::before { content: "["; }
&::after { content: "]"; }
}
section.footnotes {
margin-top: 2em;
&::before { content: ""; display: block; max-width: 20em; }
}
.fade {
background: repeating-linear-gradient(135deg, white, white 30px, #ddd 32px, #ddd 32px);
opacity: 0.6;
}
@media print {
body { max-width: 100%; }
tr, img { break-inside: avoid; }
}
@media only screen and (min-width: 992px) {
body:not(.pagesjs) pre:has(.line-numbers):not(:hover) { white-space: pre; }
}
</style>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.25/dist/katex.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@xiee/utils@1.14.17/css/prism-xcode.min.css">
<script src="https://cdn.jsdelivr.net/combine/npm/katex@0.16.25/dist/katex.min.js,npm/katex@0.16.25/dist/contrib/auto-render.min.js" defer></script>
<script src="https://cdn.jsdelivr.net/npm/@xiee/utils@1.14.17/js/render-katex.min.js" defer></script>
<script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/components/prism-core.min.js" defer></script>
<script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/autoloader/prism-autoloader.min.js" defer></script>
</head>
<body>
<div class="frontmatter">
<div class="title"><h1>Functional programming: Sectioning function to a smaller domain</h1></div>
<div class="author"><h2>Søren Højsgaard</h2></div>
</div>
<div class="body">
<h2 id="sec:sectioning-a-function-domain-with-section-fun">Sectioning a function domain with <code>section_fun()</code></h2>
<p>The <code>section_fun</code> utility in <strong>doBy</strong> creates a new function by <strong>fixing some arguments</strong> of an existing function. The result is a <em>section</em> of the original function, defined only on the remaining arguments.</p>
<p>For example, if you have:</p>
<p>$$
f(x,y) = x + y
$$</p>
<p>then fixing \(x=10\) yields:</p>
<p>$$
f_x(y) = 10 + y
$$</p>
<p>In <code>R</code> terms, <code>section_fun</code> lets you programmatically create such specialized versions.</p>
<hr />
<h2 id="sec:how-section-fun-works">How <code>section_fun</code> works</h2>
<p><code>section_fun()</code> offers three ways to fix arguments:</p>
<ol>
<li><strong>Defaults (method = “def”)</strong> – Inserts the fixed values as defaults in the argument list.</li>
<li><strong>Substitution (method = “sub”)</strong> – Rewrites the function body with the fixed values.</li>
<li><strong>Environment (method = “env”)</strong> – Stores fixed values in an auxiliary environment.</li>
</ol>
<p>Example:</p>
<pre><code class="language-r">fun <- function(a, b, c=4, d=9) {
a + b + c + d
}
</code></pre>
<pre><code class="language-r">fun_def <- section_fun(fun, list(b=7, d=10))
fun_def
#> function (a, c = 4, b = 7, d = 10)
#> {
#> a + b + c + d
#> }
fun_body <- section_fun(fun, list(b=7, d=10), method="sub")
fun_body
#> function (a, c = 4)
#> {
#> b = 7
#> d = 10
#> a + b + c + d
#> }
fun_env <- section_fun(fun, list(b=7, d=10), method = "env")
fun_env
#> function (a, c = 4)
#> {
#> . <- "use get_section(function_name) to see section"
#> . <- "use get_fun(function_name) to see original function"
#> args <- arg_getter()
#> do.call(fun, args)
#> }
#> <environment: 0x5d58c7af6838>
</code></pre>
<p>You can inspect the environment-based section:</p>
<pre><code class="language-r">get_section(fun_env)
#> $b
#> [1] 7
#>
#> $d
#> [1] 10
## same as: attr(fun_env, "arg_env")$args
get_fun(fun_env)
#> <srcref: file "" chars 1:9 to 3:1>
## same as: environment(fun_env)$fun
</code></pre>
<p>Example evaluations:</p>
<pre><code class="language-r">fun(a=10, b=7, c=5, d=10)
#> [1] 32
fun_def(a=10, c=5)
#> [1] 32
fun_body(a=10, c=5)
#> [1] 32
fun_env(a=10, c=5)
#> [1] 32
</code></pre>
<h2 id="sec:benchmarking-example">Benchmarking example</h2>
<p>Suppose you want to benchmark a function for different input values without writing repetitive code:</p>
<pre><code class="language-r">inv_toep <- function(n) {
solve(toeplitz(1:n))
}
</code></pre>
<p>Instead of typing the following</p>
<pre><code class="language-r">microbenchmark(
inv_toep(4), inv_toep(8), inv_toep(16),
times=3
)
</code></pre>
<p>you can create specialized versions programmatically:</p>
<pre><code class="language-r">n.vec <- c(4, 8, 16)
fun_list <- lapply(n.vec,
function(ni) {
section_fun(inv_toep, list(n=ni))
})
fun_list
#> [[1]]
#> function (n = 4)
#> {
#> solve(toeplitz(1:n))
#> }
#>
#> [[2]]
#> function (n = 8)
#> {
#> solve(toeplitz(1:n))
#> }
#>
#> [[3]]
#> function (n = 16)
#> {
#> solve(toeplitz(1:n))
#> }
</code></pre>
<p>Inspect and evaluate:</p>
<pre><code class="language-r">fun_list[[1]]
#> function (n = 4)
#> {
#> solve(toeplitz(1:n))
#> }
fun_list[[1]]()
#> [,1] [,2] [,3] [,4]
#> [1,] -0.4 0.5 0.0 0.1
#> [2,] 0.5 -1.0 0.5 0.0
#> [3,] 0.0 0.5 -1.0 0.5
#> [4,] 0.1 0.0 0.5 -0.4
</code></pre>
<p>To use with microbenchmark, we need expressions:</p>
<pre><code class="language-r">bquote_list <- function(fun_list) {
lapply(fun_list, function(g){
bquote(.(g)())
})
}
</code></pre>
<p>We get:</p>
<pre><code class="language-r">bq_fun_list <- bquote_list(fun_list)
bq_fun_list
#> [[1]]
#> (function (n = 4)
#> {
#> solve(toeplitz(1:n))
#> })()
#>
#> [[2]]
#> (function (n = 8)
#> {
#> solve(toeplitz(1:n))
#> })()
#>
#> [[3]]
#> (function (n = 16)
#> {
#> solve(toeplitz(1:n))
#> })()
bq_fun_list[[1]]
#> (function (n = 4)
#> {
#> solve(toeplitz(1:n))
#> })()
eval(bq_fun_list[[1]])
#> [,1] [,2] [,3] [,4]
#> [1,] -0.4 0.5 0.0 0.1
#> [2,] 0.5 -1.0 0.5 0.0
#> [3,] 0.0 0.5 -1.0 0.5
#> [4,] 0.1 0.0 0.5 -0.4
</code></pre>
<p>Now run:</p>
<pre><code class="language-r">microbenchmark(
list = bq_fun_list,
times = 5
)
#> Unit: microseconds
#> expr min lq mean median
#> (function (n = 4) { solve(toeplitz(1:n)) })() 8.93 9.04 23.3 10.1
#> (function (n = 8) { solve(toeplitz(1:n)) })() 10.04 10.60 12.0 12.4
#> (function (n = 16) { solve(toeplitz(1:n)) })() 15.96 16.35 18.7 17.1
#> uq max neval cld
#> 12.9 75.4 5 a
#> 13.2 13.7 5 a
#> 19.1 25.1 5 a
</code></pre>
<p>Running the code below provides a benchmark of the different ways of sectioning in terms of speed.</p>
<pre><code class="language-r">n.vec <- seq(20, 80, by=20)
fun_def <- lapply(n.vec,
function(n){
section_fun(inv_toep, list(n=n), method="def")
})
fun_body <- lapply(n.vec,
function(n){
section_fun(inv_toep, list(n=n), method="sub")
})
fun_env <- lapply(n.vec,
function(n){
section_fun(inv_toep, list(n=n), method="env")
})
names(fun_def) <- paste0("def", n.vec)
names(fun_body) <- paste0("body", n.vec)
names(fun_env) <- paste0("env", n.vec)
bq_fun_list <- bquote_list(c(fun_def, fun_body, fun_env))
bq_fun_list |> head()
#> $def20
#> (function (n = 20)
#> {
#> solve(toeplitz(1:n))
#> })()
#>
#> $def40
#> (function (n = 40)
#> {
#> solve(toeplitz(1:n))
#> })()
#>
#> $def60
#> (function (n = 60)
#> {
#> solve(toeplitz(1:n))
#> })()
#>
#> $def80
#> (function (n = 80)
#> {
#> solve(toeplitz(1:n))
#> })()
#>
#> $body20
#> (function ()
#> {
#> n = 20
#> solve(toeplitz(1:n))
#> })()
#>
#> $body40
#> (function ()
#> {
#> n = 40
#> solve(toeplitz(1:n))
#> })()
mb <- microbenchmark(
list = bq_fun_list,
times = 2
)
mb
#> Unit: microseconds
#> expr min lq mean median uq max neval cld
#> def20 29.3 29.3 38.7 38.7 48.1 48.1 2 a
#> def40 76.4 76.4 80.7 80.7 85.1 85.1 2 a
#> def60 168.9 168.9 181.9 181.9 195.0 195.0 2 a
#> def80 343.8 343.8 344.6 344.6 345.5 345.5 2 a
#> body20 42.9 42.9 427.8 427.8 812.6 812.6 2 a
#> body40 104.6 104.6 535.5 535.5 966.4 966.4 2 a
#> body60 267.4 267.4 691.1 691.1 1114.9 1114.9 2 a
#> body80 518.9 518.9 961.4 961.4 1404.0 1404.0 2 a
#> env20 28.3 28.3 41.0 41.0 53.6 53.6 2 a
#> env40 84.4 84.4 620.9 620.9 1157.5 1157.5 2 a
#> env60 184.5 184.5 232.9 232.9 281.4 281.4 2 a
#> env80 345.0 345.0 353.5 353.5 362.1 362.1 2 a
</code></pre>
</div>
</body>
</html>
|