| 1 |
<!DOCTYPE html> |
| 2 |
|
| 3 |
<html> |
| 4 |
|
| 5 |
<head> |
| 6 |
|
| 7 |
<meta charset="utf-8" /> |
| 8 |
<meta name="generator" content="pandoc" /> |
| 9 |
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" /> |
| 10 |
|
| 11 |
<meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
<title>Design behind sparsevctrs</title> |
| 16 |
|
| 17 |
<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to |
| 18 |
// be compatible with the behavior of Pandoc < 2.8). |
| 19 |
document.addEventListener('DOMContentLoaded', function(e) { |
| 20 |
var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); |
| 21 |
var i, h, a; |
| 22 |
for (i = 0; i < hs.length; i++) { |
| 23 |
h = hs[i]; |
| 24 |
if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 |
| 25 |
a = h.attributes; |
| 26 |
while (a.length > 0) h.removeAttribute(a[0].name); |
| 27 |
} |
| 28 |
}); |
| 29 |
</script> |
| 30 |
|
| 31 |
<style type="text/css"> |
| 32 |
code{white-space: pre-wrap;} |
| 33 |
span.smallcaps{font-variant: small-caps;} |
| 34 |
span.underline{text-decoration: underline;} |
| 35 |
div.column{display: inline-block; vertical-align: top; width: 50%;} |
| 36 |
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} |
| 37 |
ul.task-list{list-style: none;} |
| 38 |
</style> |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
<style type="text/css"> |
| 43 |
code { |
| 44 |
white-space: pre; |
| 45 |
} |
| 46 |
.sourceCode { |
| 47 |
overflow: visible; |
| 48 |
} |
| 49 |
</style> |
| 50 |
<style type="text/css" data-origin="pandoc"> |
| 51 |
html { -webkit-text-size-adjust: 100%; } |
| 52 |
pre > code.sourceCode { white-space: pre; position: relative; } |
| 53 |
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; } |
| 54 |
pre > code.sourceCode > span:empty { height: 1.2em; } |
| 55 |
.sourceCode { overflow: visible; } |
| 56 |
code.sourceCode > span { color: inherit; text-decoration: inherit; } |
| 57 |
div.sourceCode { margin: 1em 0; } |
| 58 |
pre.sourceCode { margin: 0; } |
| 59 |
@media screen { |
| 60 |
div.sourceCode { overflow: auto; } |
| 61 |
} |
| 62 |
@media print { |
| 63 |
pre > code.sourceCode { white-space: pre-wrap; } |
| 64 |
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; } |
| 65 |
} |
| 66 |
pre.numberSource code |
| 67 |
{ counter-reset: source-line 0; } |
| 68 |
pre.numberSource code > span |
| 69 |
{ position: relative; left: -4em; counter-increment: source-line; } |
| 70 |
pre.numberSource code > span > a:first-child::before |
| 71 |
{ content: counter(source-line); |
| 72 |
position: relative; left: -1em; text-align: right; vertical-align: baseline; |
| 73 |
border: none; display: inline-block; |
| 74 |
-webkit-touch-callout: none; -webkit-user-select: none; |
| 75 |
-khtml-user-select: none; -moz-user-select: none; |
| 76 |
-ms-user-select: none; user-select: none; |
| 77 |
padding: 0 4px; width: 4em; |
| 78 |
color: #aaaaaa; |
| 79 |
} |
| 80 |
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; } |
| 81 |
div.sourceCode |
| 82 |
{ } |
| 83 |
@media screen { |
| 84 |
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; } |
| 85 |
} |
| 86 |
code span.al { color: #ff0000; font-weight: bold; } |
| 87 |
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 88 |
code span.at { color: #7d9029; } |
| 89 |
code span.bn { color: #40a070; } |
| 90 |
code span.bu { color: #008000; } |
| 91 |
code span.cf { color: #007020; font-weight: bold; } |
| 92 |
code span.ch { color: #4070a0; } |
| 93 |
code span.cn { color: #880000; } |
| 94 |
code span.co { color: #60a0b0; font-style: italic; } |
| 95 |
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 96 |
code span.do { color: #ba2121; font-style: italic; } |
| 97 |
code span.dt { color: #902000; } |
| 98 |
code span.dv { color: #40a070; } |
| 99 |
code span.er { color: #ff0000; font-weight: bold; } |
| 100 |
code span.ex { } |
| 101 |
code span.fl { color: #40a070; } |
| 102 |
code span.fu { color: #06287e; } |
| 103 |
code span.im { color: #008000; font-weight: bold; } |
| 104 |
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 105 |
code span.kw { color: #007020; font-weight: bold; } |
| 106 |
code span.op { color: #666666; } |
| 107 |
code span.ot { color: #007020; } |
| 108 |
code span.pp { color: #bc7a00; } |
| 109 |
code span.sc { color: #4070a0; } |
| 110 |
code span.ss { color: #bb6688; } |
| 111 |
code span.st { color: #4070a0; } |
| 112 |
code span.va { color: #19177c; } |
| 113 |
code span.vs { color: #4070a0; } |
| 114 |
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 115 |
</style> |
| 116 |
<script> |
| 117 |
// apply pandoc div.sourceCode style to pre.sourceCode instead |
| 118 |
(function() { |
| 119 |
var sheets = document.styleSheets; |
| 120 |
for (var i = 0; i < sheets.length; i++) { |
| 121 |
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue; |
| 122 |
try { var rules = sheets[i].cssRules; } catch (e) { continue; } |
| 123 |
var j = 0; |
| 124 |
while (j < rules.length) { |
| 125 |
var rule = rules[j]; |
| 126 |
// check if there is a div.sourceCode rule |
| 127 |
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") { |
| 128 |
j++; |
| 129 |
continue; |
| 130 |
} |
| 131 |
var style = rule.style.cssText; |
| 132 |
// check if color or background-color is set |
| 133 |
if (rule.style.color === '' && rule.style.backgroundColor === '') { |
| 134 |
j++; |
| 135 |
continue; |
| 136 |
} |
| 137 |
// replace div.sourceCode by a pre.sourceCode rule |
| 138 |
sheets[i].deleteRule(j); |
| 139 |
sheets[i].insertRule('pre.sourceCode{' + style + '}', j); |
| 140 |
} |
| 141 |
} |
| 142 |
})(); |
| 143 |
</script> |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
<style type="text/css">body { |
| 149 |
background-color: #fff; |
| 150 |
margin: 1em auto; |
| 151 |
max-width: 700px; |
| 152 |
overflow: visible; |
| 153 |
padding-left: 2em; |
| 154 |
padding-right: 2em; |
| 155 |
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; |
| 156 |
font-size: 14px; |
| 157 |
line-height: 1.35; |
| 158 |
} |
| 159 |
#TOC { |
| 160 |
clear: both; |
| 161 |
margin: 0 0 10px 10px; |
| 162 |
padding: 4px; |
| 163 |
width: 400px; |
| 164 |
border: 1px solid #CCCCCC; |
| 165 |
border-radius: 5px; |
| 166 |
background-color: #f6f6f6; |
| 167 |
font-size: 13px; |
| 168 |
line-height: 1.3; |
| 169 |
} |
| 170 |
#TOC .toctitle { |
| 171 |
font-weight: bold; |
| 172 |
font-size: 15px; |
| 173 |
margin-left: 5px; |
| 174 |
} |
| 175 |
#TOC ul { |
| 176 |
padding-left: 40px; |
| 177 |
margin-left: -1.5em; |
| 178 |
margin-top: 5px; |
| 179 |
margin-bottom: 5px; |
| 180 |
} |
| 181 |
#TOC ul ul { |
| 182 |
margin-left: -2em; |
| 183 |
} |
| 184 |
#TOC li { |
| 185 |
line-height: 16px; |
| 186 |
} |
| 187 |
table { |
| 188 |
margin: 1em auto; |
| 189 |
border-width: 1px; |
| 190 |
border-color: #DDDDDD; |
| 191 |
border-style: outset; |
| 192 |
border-collapse: collapse; |
| 193 |
} |
| 194 |
table th { |
| 195 |
border-width: 2px; |
| 196 |
padding: 5px; |
| 197 |
border-style: inset; |
| 198 |
} |
| 199 |
table td { |
| 200 |
border-width: 1px; |
| 201 |
border-style: inset; |
| 202 |
line-height: 18px; |
| 203 |
padding: 5px 5px; |
| 204 |
} |
| 205 |
table, table th, table td { |
| 206 |
border-left-style: none; |
| 207 |
border-right-style: none; |
| 208 |
} |
| 209 |
table thead, table tr.even { |
| 210 |
background-color: #f7f7f7; |
| 211 |
} |
| 212 |
p { |
| 213 |
margin: 0.5em 0; |
| 214 |
} |
| 215 |
blockquote { |
| 216 |
background-color: #f6f6f6; |
| 217 |
padding: 0.25em 0.75em; |
| 218 |
} |
| 219 |
hr { |
| 220 |
border-style: solid; |
| 221 |
border: none; |
| 222 |
border-top: 1px solid #777; |
| 223 |
margin: 28px 0; |
| 224 |
} |
| 225 |
dl { |
| 226 |
margin-left: 0; |
| 227 |
} |
| 228 |
dl dd { |
| 229 |
margin-bottom: 13px; |
| 230 |
margin-left: 13px; |
| 231 |
} |
| 232 |
dl dt { |
| 233 |
font-weight: bold; |
| 234 |
} |
| 235 |
ul { |
| 236 |
margin-top: 0; |
| 237 |
} |
| 238 |
ul li { |
| 239 |
list-style: circle outside; |
| 240 |
} |
| 241 |
ul ul { |
| 242 |
margin-bottom: 0; |
| 243 |
} |
| 244 |
pre, code { |
| 245 |
background-color: #f7f7f7; |
| 246 |
border-radius: 3px; |
| 247 |
color: #333; |
| 248 |
white-space: pre-wrap; |
| 249 |
} |
| 250 |
pre { |
| 251 |
border-radius: 3px; |
| 252 |
margin: 5px 0px 10px 0px; |
| 253 |
padding: 10px; |
| 254 |
} |
| 255 |
pre:not([class]) { |
| 256 |
background-color: #f7f7f7; |
| 257 |
} |
| 258 |
code { |
| 259 |
font-family: Consolas, Monaco, 'Courier New', monospace; |
| 260 |
font-size: 85%; |
| 261 |
} |
| 262 |
p > code, li > code { |
| 263 |
padding: 2px 0px; |
| 264 |
} |
| 265 |
div.figure { |
| 266 |
text-align: center; |
| 267 |
} |
| 268 |
img { |
| 269 |
background-color: #FFFFFF; |
| 270 |
padding: 2px; |
| 271 |
border: 1px solid #DDDDDD; |
| 272 |
border-radius: 3px; |
| 273 |
border: 1px solid #CCCCCC; |
| 274 |
margin: 0 5px; |
| 275 |
} |
| 276 |
h1 { |
| 277 |
margin-top: 0; |
| 278 |
font-size: 35px; |
| 279 |
line-height: 40px; |
| 280 |
} |
| 281 |
h2 { |
| 282 |
border-bottom: 4px solid #f7f7f7; |
| 283 |
padding-top: 10px; |
| 284 |
padding-bottom: 2px; |
| 285 |
font-size: 145%; |
| 286 |
} |
| 287 |
h3 { |
| 288 |
border-bottom: 2px solid #f7f7f7; |
| 289 |
padding-top: 10px; |
| 290 |
font-size: 120%; |
| 291 |
} |
| 292 |
h4 { |
| 293 |
border-bottom: 1px solid #f7f7f7; |
| 294 |
margin-left: 8px; |
| 295 |
font-size: 105%; |
| 296 |
} |
| 297 |
h5, h6 { |
| 298 |
border-bottom: 1px solid #ccc; |
| 299 |
font-size: 105%; |
| 300 |
} |
| 301 |
a { |
| 302 |
color: #0033dd; |
| 303 |
text-decoration: none; |
| 304 |
} |
| 305 |
a:hover { |
| 306 |
color: #6666ff; } |
| 307 |
a:visited { |
| 308 |
color: #800080; } |
| 309 |
a:visited:hover { |
| 310 |
color: #BB00BB; } |
| 311 |
a[href^="http:"] { |
| 312 |
text-decoration: underline; } |
| 313 |
a[href^="https:"] { |
| 314 |
text-decoration: underline; } |
| 315 |
|
| 316 |
code > span.kw { color: #555; font-weight: bold; } |
| 317 |
code > span.dt { color: #902000; } |
| 318 |
code > span.dv { color: #40a070; } |
| 319 |
code > span.bn { color: #d14; } |
| 320 |
code > span.fl { color: #d14; } |
| 321 |
code > span.ch { color: #d14; } |
| 322 |
code > span.st { color: #d14; } |
| 323 |
code > span.co { color: #888888; font-style: italic; } |
| 324 |
code > span.ot { color: #007020; } |
| 325 |
code > span.al { color: #ff0000; font-weight: bold; } |
| 326 |
code > span.fu { color: #900; font-weight: bold; } |
| 327 |
code > span.er { color: #a61717; background-color: #e3d2d2; } |
| 328 |
</style> |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
</head> |
| 334 |
|
| 335 |
<body> |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
<h1 class="title toc-ignore">Design behind sparsevctrs</h1> |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="fu">library</span>(sparsevctrs)</span></code></pre></div> |
| 345 |
<p>The sparsevctrs package produces 3 things; ALTREP classes, |
| 346 |
matrix/data.frame converting functions, helper functions. This document |
| 347 |
outlines the rationale behind each of these and the decisions behind |
| 348 |
them.</p> |
| 349 |
<p>The primary objective of this package is to provide tools to work |
| 350 |
with sparse data in data.frames/tibbles. The next highest priority is |
| 351 |
execution speed. This means that algorithms and methods in this package |
| 352 |
are written to minimize memory allocations whenever possible, once that |
| 353 |
is done, running the code as fast as we can. These choices are made |
| 354 |
because this package was written to deal with tasks that were otherwise |
| 355 |
not possible due to memory constraints.</p> |
| 356 |
<div id="altrep-functions" class="section level2"> |
| 357 |
<h2>Altrep Functions</h2> |
| 358 |
<p>The functions <code>sparse_double()</code> and its relatives are used |
| 359 |
to construct sparse vectors of the noted type. To work they all need 4 |
| 360 |
pieces of information:</p> |
| 361 |
<ul> |
| 362 |
<li><code>values</code></li> |
| 363 |
<li><code>positions</code></li> |
| 364 |
<li><code>length</code></li> |
| 365 |
<li><code>default</code> (defaults to 0)</li> |
| 366 |
</ul> |
| 367 |
<p>The values need to match the type of the function name or be easily |
| 368 |
coerced into the type (double -> integer). The positions should be |
| 369 |
integers or doubles that can losslessly be turned into integers. The |
| 370 |
length should be a single non-negative integer-like value.</p> |
| 371 |
<p>Values and positions are paired, and will thus be expected to be the |
| 372 |
same length, furthermore, positions are expected to be sorted in |
| 373 |
increasing order with no duplicates. The ordering is done to let the |
| 374 |
various extraction methods work as efficiently as possible.</p> |
| 375 |
<p>These functions have quite strict input checking by choice, to allow |
| 376 |
the inner workings to be as efficient as possible.</p> |
| 377 |
<p>The input of these functions mirrors the values stored in the ALTREP |
| 378 |
class that they produce.</p> |
| 379 |
</div> |
| 380 |
<div id="converting-functions" class="section level2"> |
| 381 |
<h2>Converting Functions</h2> |
| 382 |
<p>3 functions fall into this category:</p> |
| 383 |
<ul> |
| 384 |
<li><code>coerce_to_sparse_data_frame()</code></li> |
| 385 |
<li><code>coerce_to_sparse_tibble()</code></li> |
| 386 |
<li><code>coerce_to_sparse_matrix()</code></li> |
| 387 |
</ul> |
| 388 |
<p>the first two take a sparse matrix from the Matrix package and |
| 389 |
produce a data.frame/tibble with sparse columns. The last one takes a |
| 390 |
data.frame/tibble with sparse columns and produces a sparse matrix using |
| 391 |
the Matrix package.</p> |
| 392 |
<p>These functions are expected to be inverse of each other, such that |
| 393 |
<code>coerce_to_sparse_matrix(coerce_to_sparse_data_frame(x))</code> |
| 394 |
returns <code>x</code> back. They are made to be highly performant both |
| 395 |
in terms of speed and memory consumption, Meaning that sparsity is |
| 396 |
applied when appropriate.</p> |
| 397 |
<p>These functions have quite strict input checking by choice, to allow |
| 398 |
the inner workings to be as efficient as possible. It is in part why |
| 399 |
data.frames with sparse vectors with different can’t be used with |
| 400 |
<code>coerce_to_sparse_matrix()</code> yet.</p> |
| 401 |
</div> |
| 402 |
<div id="helper-functions" class="section level2"> |
| 403 |
<h2>Helper Functions</h2> |
| 404 |
<p>There are 3 types of helper functions. First, we have the |
| 405 |
<code>is_*</code> family of functions. The specific |
| 406 |
<code>is_sparse_double()</code> and more general |
| 407 |
<code>is_sparse_vector()</code> can be used as a way to determine |
| 408 |
whether a vector is an ALTREP sparse vector. This is otherwise hard to |
| 409 |
tell as <code>as.numeric()</code> can’t tell the difference.</p> |
| 410 |
<p>Secondly, we have the extraction functions. They are |
| 411 |
<code>sparse_values()</code> and <code>sparse_positions()</code>. These |
| 412 |
extract the values and positions respectively, without materializing the |
| 413 |
whole dense vector. These functions are made to work with non-sparse |
| 414 |
vectors as well to make them more ergonomic for the user. Internally |
| 415 |
they call <code>is_sparse_vector()</code>, so the choice to return |
| 416 |
something useful as the alternative wasn’t hard. There is no |
| 417 |
<code>sparse_length()</code> function as <code>length()</code> works |
| 418 |
with these types of</p> |
| 419 |
<p>The last type of helper function is less clearly defined and is |
| 420 |
expanded as needed. The functions provide alternatives to functions that |
| 421 |
don’t have ALTREP support. Such as <code>mean()</code>. Calling |
| 422 |
<code>mean()</code> on a sparse vector will force materialization, and |
| 423 |
then calculate the mean. This is memory inefficient as it could have |
| 424 |
been calculated like so.</p> |
| 425 |
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a><span class="fu">sum</span>(<span class="fu">sparse_values</span>(x)) <span class="sc">/</span> <span class="fu">length</span>(x)</span></code></pre></div> |
| 426 |
<p>These functions, all starting with the name prefix |
| 427 |
<code>sparse_*</code>, are made to work with non-sparse vectors for the |
| 428 |
same reasons listed above regarding ergonomic use.</p> |
| 429 |
</div> |
| 430 |
<div id="faq" class="section level2"> |
| 431 |
<h2>FAQ</h2> |
| 432 |
<blockquote> |
| 433 |
<p>Why aren’t the results returned as {vctrs} classes?</p> |
| 434 |
</blockquote> |
| 435 |
<p>As it stands right now, it is viewed to be beneficial to have the |
| 436 |
users not be alerted to these vectors as they are expected to be used |
| 437 |
internally in packages and rarely by the end user. Furthermore having |
| 438 |
these sparse vectors produce the same result as dense vectors with |
| 439 |
<code>class()</code> is a big plus.</p> |
| 440 |
<blockquote> |
| 441 |
<p>Will this package try to replace the {Matrix} package?</p> |
| 442 |
</blockquote> |
| 443 |
<p>Not at all. The sparse vector types provided in this package mimic |
| 444 |
those created with <code>Matrix::sparseVector()</code>. They work with |
| 445 |
different types and allow for different defaults. None of the matrix |
| 446 |
operations will be reimplemented here.</p> |
| 447 |
</div> |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
<script> |
| 456 |
(function () { |
| 457 |
var script = document.createElement("script"); |
| 458 |
script.type = "text/javascript"; |
| 459 |
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"; |
| 460 |
document.getElementsByTagName("head")[0].appendChild(script); |
| 461 |
})(); |
| 462 |
</script> |
| 463 |
|
| 464 |
</body> |
| 465 |
</html> |