| 1 | <?xml version="1.0" encoding="UTF-8"?> |
| 2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" |
| 3 | "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> |
| 4 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> |
| 5 | <head> |
| 6 | <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" /> |
| 7 | <meta name="generator" content="AsciiDoc 10.1.2" /> |
| 8 | <title>LA_pack primer</title> |
| 9 | <style type="text/css"> |
| 10 | /* Shared CSS for AsciiDoc xhtml11 and html5 backends */ |
| 11 | |
| 12 | /* Default font. */ |
| 13 | body { |
| 14 | font-family: Georgia,serif; |
| 15 | } |
| 16 | |
| 17 | /* Title font. */ |
| 18 | h1, h2, h3, h4, h5, h6, |
| 19 | div.title, caption.title, |
| 20 | thead, p.table.header, |
| 21 | #toctitle, |
| 22 | #author, #revnumber, #revdate, #revremark, |
| 23 | #footer { |
| 24 | font-family: Arial,Helvetica,sans-serif; |
| 25 | } |
| 26 | |
| 27 | body { |
| 28 | margin: 1em 5% 1em 5%; |
| 29 | } |
| 30 | |
| 31 | a { |
| 32 | color: blue; |
| 33 | text-decoration: underline; |
| 34 | } |
| 35 | a:visited { |
| 36 | color: fuchsia; |
| 37 | } |
| 38 | |
| 39 | em { |
| 40 | font-style: italic; |
| 41 | color: navy; |
| 42 | } |
| 43 | |
| 44 | strong { |
| 45 | font-weight: bold; |
| 46 | color: #083194; |
| 47 | } |
| 48 | |
| 49 | h1, h2, h3, h4, h5, h6 { |
| 50 | color: #527bbd; |
| 51 | margin-top: 1.2em; |
| 52 | margin-bottom: 0.5em; |
| 53 | line-height: 1.3; |
| 54 | } |
| 55 | |
| 56 | h1, h2, h3 { |
| 57 | border-bottom: 2px solid silver; |
| 58 | } |
| 59 | h2 { |
| 60 | padding-top: 0.5em; |
| 61 | } |
| 62 | h3 { |
| 63 | float: left; |
| 64 | } |
| 65 | h3 + * { |
| 66 | clear: left; |
| 67 | } |
| 68 | h5 { |
| 69 | font-size: 1.0em; |
| 70 | } |
| 71 | |
| 72 | div.sectionbody { |
| 73 | margin-left: 0; |
| 74 | } |
| 75 | |
| 76 | hr { |
| 77 | border: 1px solid silver; |
| 78 | } |
| 79 | |
| 80 | p { |
| 81 | margin-top: 0.5em; |
| 82 | margin-bottom: 0.5em; |
| 83 | } |
| 84 | |
| 85 | ul, ol, li > p { |
| 86 | margin-top: 0; |
| 87 | } |
| 88 | ul > li { color: #aaa; } |
| 89 | ul > li > * { color: black; } |
| 90 | |
| 91 | .monospaced, code, pre { |
| 92 | font-family: "Courier New", Courier, monospace; |
| 93 | font-size: inherit; |
| 94 | color: navy; |
| 95 | padding: 0; |
| 96 | margin: 0; |
| 97 | } |
| 98 | pre { |
| 99 | white-space: pre-wrap; |
| 100 | } |
| 101 | |
| 102 | #author { |
| 103 | color: #527bbd; |
| 104 | font-weight: bold; |
| 105 | font-size: 1.1em; |
| 106 | } |
| 107 | #email { |
| 108 | } |
| 109 | #revnumber, #revdate, #revremark { |
| 110 | } |
| 111 | |
| 112 | #footer { |
| 113 | font-size: small; |
| 114 | border-top: 2px solid silver; |
| 115 | padding-top: 0.5em; |
| 116 | margin-top: 4.0em; |
| 117 | } |
| 118 | #footer-text { |
| 119 | float: left; |
| 120 | padding-bottom: 0.5em; |
| 121 | } |
| 122 | #footer-badges { |
| 123 | float: right; |
| 124 | padding-bottom: 0.5em; |
| 125 | } |
| 126 | |
| 127 | #preamble { |
| 128 | margin-top: 1.5em; |
| 129 | margin-bottom: 1.5em; |
| 130 | } |
| 131 | div.imageblock, div.exampleblock, div.verseblock, |
| 132 | div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock, |
| 133 | div.admonitionblock { |
| 134 | margin-top: 1.0em; |
| 135 | margin-bottom: 1.5em; |
| 136 | } |
| 137 | div.admonitionblock { |
| 138 | margin-top: 2.0em; |
| 139 | margin-bottom: 2.0em; |
| 140 | margin-right: 10%; |
| 141 | color: #606060; |
| 142 | } |
| 143 | |
| 144 | div.content { /* Block element content. */ |
| 145 | padding: 0; |
| 146 | } |
| 147 | |
| 148 | /* Block element titles. */ |
| 149 | div.title, caption.title { |
| 150 | color: #527bbd; |
| 151 | font-weight: bold; |
| 152 | text-align: left; |
| 153 | margin-top: 1.0em; |
| 154 | margin-bottom: 0.5em; |
| 155 | } |
| 156 | div.title + * { |
| 157 | margin-top: 0; |
| 158 | } |
| 159 | |
| 160 | td div.title:first-child { |
| 161 | margin-top: 0.0em; |
| 162 | } |
| 163 | div.content div.title:first-child { |
| 164 | margin-top: 0.0em; |
| 165 | } |
| 166 | div.content + div.title { |
| 167 | margin-top: 0.0em; |
| 168 | } |
| 169 | |
| 170 | div.sidebarblock > div.content { |
| 171 | background: #ffffee; |
| 172 | border: 1px solid #dddddd; |
| 173 | border-left: 4px solid #f0f0f0; |
| 174 | padding: 0.5em; |
| 175 | } |
| 176 | |
| 177 | div.listingblock > div.content { |
| 178 | border: 1px solid #dddddd; |
| 179 | border-left: 5px solid #f0f0f0; |
| 180 | background: #f8f8f8; |
| 181 | padding: 0.5em; |
| 182 | } |
| 183 | |
| 184 | div.quoteblock, div.verseblock { |
| 185 | padding-left: 1.0em; |
| 186 | margin-left: 1.0em; |
| 187 | margin-right: 10%; |
| 188 | border-left: 5px solid #f0f0f0; |
| 189 | color: #888; |
| 190 | } |
| 191 | |
| 192 | div.quoteblock > div.attribution { |
| 193 | padding-top: 0.5em; |
| 194 | text-align: right; |
| 195 | } |
| 196 | |
| 197 | div.verseblock > pre.content { |
| 198 | font-family: inherit; |
| 199 | font-size: inherit; |
| 200 | } |
| 201 | div.verseblock > div.attribution { |
| 202 | padding-top: 0.75em; |
| 203 | text-align: left; |
| 204 | } |
| 205 | /* DEPRECATED: Pre version 8.2.7 verse style literal block. */ |
| 206 | div.verseblock + div.attribution { |
| 207 | text-align: left; |
| 208 | } |
| 209 | |
| 210 | div.admonitionblock .icon { |
| 211 | vertical-align: top; |
| 212 | font-size: 1.1em; |
| 213 | font-weight: bold; |
| 214 | text-decoration: underline; |
| 215 | color: #527bbd; |
| 216 | padding-right: 0.5em; |
| 217 | } |
| 218 | div.admonitionblock td.content { |
| 219 | padding-left: 0.5em; |
| 220 | border-left: 3px solid #dddddd; |
| 221 | } |
| 222 | |
| 223 | div.exampleblock > div.content { |
| 224 | border-left: 3px solid #dddddd; |
| 225 | padding-left: 0.5em; |
| 226 | } |
| 227 | |
| 228 | div.imageblock div.content { padding-left: 0; } |
| 229 | span.image img { border-style: none; vertical-align: text-bottom; } |
| 230 | a.image:visited { color: white; } |
| 231 | |
| 232 | dl { |
| 233 | margin-top: 0.8em; |
| 234 | margin-bottom: 0.8em; |
| 235 | } |
| 236 | dt { |
| 237 | margin-top: 0.5em; |
| 238 | margin-bottom: 0; |
| 239 | font-style: normal; |
| 240 | color: navy; |
| 241 | } |
| 242 | dd > *:first-child { |
| 243 | margin-top: 0.1em; |
| 244 | } |
| 245 | |
| 246 | ul, ol { |
| 247 | list-style-position: outside; |
| 248 | } |
| 249 | ol.arabic { |
| 250 | list-style-type: decimal; |
| 251 | } |
| 252 | ol.loweralpha { |
| 253 | list-style-type: lower-alpha; |
| 254 | } |
| 255 | ol.upperalpha { |
| 256 | list-style-type: upper-alpha; |
| 257 | } |
| 258 | ol.lowerroman { |
| 259 | list-style-type: lower-roman; |
| 260 | } |
| 261 | ol.upperroman { |
| 262 | list-style-type: upper-roman; |
| 263 | } |
| 264 | |
| 265 | div.compact ul, div.compact ol, |
| 266 | div.compact p, div.compact p, |
| 267 | div.compact div, div.compact div { |
| 268 | margin-top: 0.1em; |
| 269 | margin-bottom: 0.1em; |
| 270 | } |
| 271 | |
| 272 | tfoot { |
| 273 | font-weight: bold; |
| 274 | } |
| 275 | td > div.verse { |
| 276 | white-space: pre; |
| 277 | } |
| 278 | |
| 279 | div.hdlist { |
| 280 | margin-top: 0.8em; |
| 281 | margin-bottom: 0.8em; |
| 282 | } |
| 283 | div.hdlist tr { |
| 284 | padding-bottom: 15px; |
| 285 | } |
| 286 | dt.hdlist1.strong, td.hdlist1.strong { |
| 287 | font-weight: bold; |
| 288 | } |
| 289 | td.hdlist1 { |
| 290 | vertical-align: top; |
| 291 | font-style: normal; |
| 292 | padding-right: 0.8em; |
| 293 | color: navy; |
| 294 | } |
| 295 | td.hdlist2 { |
| 296 | vertical-align: top; |
| 297 | } |
| 298 | div.hdlist.compact tr { |
| 299 | margin: 0; |
| 300 | padding-bottom: 0; |
| 301 | } |
| 302 | |
| 303 | .comment { |
| 304 | background: yellow; |
| 305 | } |
| 306 | |
| 307 | .footnote, .footnoteref { |
| 308 | font-size: 0.8em; |
| 309 | } |
| 310 | |
| 311 | span.footnote, span.footnoteref { |
| 312 | vertical-align: super; |
| 313 | } |
| 314 | |
| 315 | #footnotes { |
| 316 | margin: 20px 0 20px 0; |
| 317 | padding: 7px 0 0 0; |
| 318 | } |
| 319 | |
| 320 | #footnotes div.footnote { |
| 321 | margin: 0 0 5px 0; |
| 322 | } |
| 323 | |
| 324 | #footnotes hr { |
| 325 | border: none; |
| 326 | border-top: 1px solid silver; |
| 327 | height: 1px; |
| 328 | text-align: left; |
| 329 | margin-left: 0; |
| 330 | width: 20%; |
| 331 | min-width: 100px; |
| 332 | } |
| 333 | |
| 334 | div.colist td { |
| 335 | padding-right: 0.5em; |
| 336 | padding-bottom: 0.3em; |
| 337 | vertical-align: top; |
| 338 | } |
| 339 | div.colist td img { |
| 340 | margin-top: 0.3em; |
| 341 | } |
| 342 | |
| 343 | @media print { |
| 344 | #footer-badges { display: none; } |
| 345 | } |
| 346 | |
| 347 | #toc { |
| 348 | margin-bottom: 2.5em; |
| 349 | } |
| 350 | |
| 351 | #toctitle { |
| 352 | color: #527bbd; |
| 353 | font-size: 1.1em; |
| 354 | font-weight: bold; |
| 355 | margin-top: 1.0em; |
| 356 | margin-bottom: 0.1em; |
| 357 | } |
| 358 | |
| 359 | div.toclevel0, div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 { |
| 360 | margin-top: 0; |
| 361 | margin-bottom: 0; |
| 362 | } |
| 363 | div.toclevel2 { |
| 364 | margin-left: 2em; |
| 365 | font-size: 0.9em; |
| 366 | } |
| 367 | div.toclevel3 { |
| 368 | margin-left: 4em; |
| 369 | font-size: 0.9em; |
| 370 | } |
| 371 | div.toclevel4 { |
| 372 | margin-left: 6em; |
| 373 | font-size: 0.9em; |
| 374 | } |
| 375 | |
| 376 | span.aqua { color: aqua; } |
| 377 | span.black { color: black; } |
| 378 | span.blue { color: blue; } |
| 379 | span.fuchsia { color: fuchsia; } |
| 380 | span.gray { color: gray; } |
| 381 | span.green { color: green; } |
| 382 | span.lime { color: lime; } |
| 383 | span.maroon { color: maroon; } |
| 384 | span.navy { color: navy; } |
| 385 | span.olive { color: olive; } |
| 386 | span.purple { color: purple; } |
| 387 | span.red { color: red; } |
| 388 | span.silver { color: silver; } |
| 389 | span.teal { color: teal; } |
| 390 | span.white { color: white; } |
| 391 | span.yellow { color: yellow; } |
| 392 | |
| 393 | span.aqua-background { background: aqua; } |
| 394 | span.black-background { background: black; } |
| 395 | span.blue-background { background: blue; } |
| 396 | span.fuchsia-background { background: fuchsia; } |
| 397 | span.gray-background { background: gray; } |
| 398 | span.green-background { background: green; } |
| 399 | span.lime-background { background: lime; } |
| 400 | span.maroon-background { background: maroon; } |
| 401 | span.navy-background { background: navy; } |
| 402 | span.olive-background { background: olive; } |
| 403 | span.purple-background { background: purple; } |
| 404 | span.red-background { background: red; } |
| 405 | span.silver-background { background: silver; } |
| 406 | span.teal-background { background: teal; } |
| 407 | span.white-background { background: white; } |
| 408 | span.yellow-background { background: yellow; } |
| 409 | |
| 410 | span.big { font-size: 2em; } |
| 411 | span.small { font-size: 0.6em; } |
| 412 | |
| 413 | span.underline { text-decoration: underline; } |
| 414 | span.overline { text-decoration: overline; } |
| 415 | span.line-through { text-decoration: line-through; } |
| 416 | |
| 417 | div.unbreakable { page-break-inside: avoid; } |
| 418 | |
| 419 | |
| 420 | /* |
| 421 | * xhtml11 specific |
| 422 | * |
| 423 | * */ |
| 424 | |
| 425 | div.tableblock { |
| 426 | margin-top: 1.0em; |
| 427 | margin-bottom: 1.5em; |
| 428 | } |
| 429 | div.tableblock > table { |
| 430 | border: 3px solid #527bbd; |
| 431 | } |
| 432 | thead, p.table.header { |
| 433 | font-weight: bold; |
| 434 | color: #527bbd; |
| 435 | } |
| 436 | p.table { |
| 437 | margin-top: 0; |
| 438 | } |
| 439 | /* Because the table frame attribute is overridden by CSS in most browsers. */ |
| 440 | div.tableblock > table[frame="void"] { |
| 441 | border-style: none; |
| 442 | } |
| 443 | div.tableblock > table[frame="hsides"] { |
| 444 | border-left-style: none; |
| 445 | border-right-style: none; |
| 446 | } |
| 447 | div.tableblock > table[frame="vsides"] { |
| 448 | border-top-style: none; |
| 449 | border-bottom-style: none; |
| 450 | } |
| 451 | |
| 452 | |
| 453 | /* |
| 454 | * html5 specific |
| 455 | * |
| 456 | * */ |
| 457 | |
| 458 | table.tableblock { |
| 459 | margin-top: 1.0em; |
| 460 | margin-bottom: 1.5em; |
| 461 | } |
| 462 | thead, p.tableblock.header { |
| 463 | font-weight: bold; |
| 464 | color: #527bbd; |
| 465 | } |
| 466 | p.tableblock { |
| 467 | margin-top: 0; |
| 468 | } |
| 469 | table.tableblock { |
| 470 | border-width: 3px; |
| 471 | border-spacing: 0px; |
| 472 | border-style: solid; |
| 473 | border-color: #527bbd; |
| 474 | border-collapse: collapse; |
| 475 | } |
| 476 | th.tableblock, td.tableblock { |
| 477 | border-width: 1px; |
| 478 | padding: 4px; |
| 479 | border-style: solid; |
| 480 | border-color: #527bbd; |
| 481 | } |
| 482 | |
| 483 | table.tableblock.frame-topbot { |
| 484 | border-left-style: hidden; |
| 485 | border-right-style: hidden; |
| 486 | } |
| 487 | table.tableblock.frame-sides { |
| 488 | border-top-style: hidden; |
| 489 | border-bottom-style: hidden; |
| 490 | } |
| 491 | table.tableblock.frame-none { |
| 492 | border-style: hidden; |
| 493 | } |
| 494 | |
| 495 | th.tableblock.halign-left, td.tableblock.halign-left { |
| 496 | text-align: left; |
| 497 | } |
| 498 | th.tableblock.halign-center, td.tableblock.halign-center { |
| 499 | text-align: center; |
| 500 | } |
| 501 | th.tableblock.halign-right, td.tableblock.halign-right { |
| 502 | text-align: right; |
| 503 | } |
| 504 | |
| 505 | th.tableblock.valign-top, td.tableblock.valign-top { |
| 506 | vertical-align: top; |
| 507 | } |
| 508 | th.tableblock.valign-middle, td.tableblock.valign-middle { |
| 509 | vertical-align: middle; |
| 510 | } |
| 511 | th.tableblock.valign-bottom, td.tableblock.valign-bottom { |
| 512 | vertical-align: bottom; |
| 513 | } |
| 514 | |
| 515 | |
| 516 | /* |
| 517 | * manpage specific |
| 518 | * |
| 519 | * */ |
| 520 | |
| 521 | body.manpage h1 { |
| 522 | padding-top: 0.5em; |
| 523 | padding-bottom: 0.5em; |
| 524 | border-top: 2px solid silver; |
| 525 | border-bottom: 2px solid silver; |
| 526 | } |
| 527 | body.manpage h2 { |
| 528 | border-style: none; |
| 529 | } |
| 530 | body.manpage div.sectionbody { |
| 531 | margin-left: 3em; |
| 532 | } |
| 533 | |
| 534 | @media print { |
| 535 | body.manpage div#toc { display: none; } |
| 536 | } |
| 537 | |
| 538 | |
| 539 | </style> |
| 540 | <script type="text/javascript"> |
| 541 | /*<+'])'); |
| 584 | // Function that scans the DOM tree for header elements (the DOM2 |
| 585 | // nodeIterator API would be a better technique but not supported by all |
| 586 | // browsers). |
| 587 | var iterate = function (el) { |
| 588 | for (var i = el.firstChild; i != null; i = i.nextSibling) { |
| 589 | if (i.nodeType == 1 /* Node.ELEMENT_NODE */) { |
| 590 | var mo = re.exec(i.tagName); |
| 591 | if (mo && (i.getAttribute("class") || i.getAttribute("className")) != "float") { |
| 592 | result[result.length] = new TocEntry(i, getText(i), mo[1]-1); |
| 593 | } |
| 594 | iterate(i); |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | iterate(el); |
| 599 | return result; |
| 600 | } |
| 601 | |
| 602 | var toc = document.getElementById("toc"); |
| 603 | if (!toc) { |
| 604 | return; |
| 605 | } |
| 606 | |
| 607 | // Delete existing TOC entries in case we're reloading the TOC. |
| 608 | var tocEntriesToRemove = []; |
| 609 | var i; |
| 610 | for (i = 0; i < toc.childNodes.length; i++) { |
| 611 | var entry = toc.childNodes[i]; |
| 612 | if (entry.nodeName.toLowerCase() == 'div' |
| 613 | && entry.getAttribute("class") |
| 614 | && entry.getAttribute("class").match(/^toclevel/)) |
| 615 | tocEntriesToRemove.push(entry); |
| 616 | } |
| 617 | for (i = 0; i < tocEntriesToRemove.length; i++) { |
| 618 | toc.removeChild(tocEntriesToRemove[i]); |
| 619 | } |
| 620 | |
| 621 | // Rebuild TOC entries. |
| 622 | var entries = tocEntries(document.getElementById("content"), toclevels); |
| 623 | for (var i = 0; i < entries.length; ++i) { |
| 624 | var entry = entries[i]; |
| 625 | if (entry.element.id == "") |
| 626 | entry.element.id = "_toc_" + i; |
| 627 | var a = document.createElement("a"); |
| 628 | a.href = "#" + entry.element.id; |
| 629 | a.appendChild(document.createTextNode(entry.text)); |
| 630 | var div = document.createElement("div"); |
| 631 | div.appendChild(a); |
| 632 | div.className = "toclevel" + entry.toclevel; |
| 633 | toc.appendChild(div); |
| 634 | } |
| 635 | if (entries.length == 0) |
| 636 | toc.parentNode.removeChild(toc); |
| 637 | }, |
| 638 | |
| 639 | |
| 640 | ///////////////////////////////////////////////////////////////////// |
| 641 | // Footnotes generator |
| 642 | ///////////////////////////////////////////////////////////////////// |
| 643 | |
| 644 | /* Based on footnote generation code from: |
| 645 | * http://www.brandspankingnew.net/archive/2005/07/format_footnote.html |
| 646 | */ |
| 647 | |
| 648 | footnotes: function () { |
| 649 | // Delete existing footnote entries in case we're reloading the footnodes. |
| 650 | var i; |
| 651 | var noteholder = document.getElementById("footnotes"); |
| 652 | if (!noteholder) { |
| 653 | return; |
| 654 | } |
| 655 | var entriesToRemove = []; |
| 656 | for (i = 0; i < noteholder.childNodes.length; i++) { |
| 657 | var entry = noteholder.childNodes[i]; |
| 658 | if (entry.nodeName.toLowerCase() == 'div' && entry.getAttribute("class") == "footnote") |
| 659 | entriesToRemove.push(entry); |
| 660 | } |
| 661 | for (i = 0; i < entriesToRemove.length; i++) { |
| 662 | noteholder.removeChild(entriesToRemove[i]); |
| 663 | } |
| 664 | |
| 665 | // Rebuild footnote entries. |
| 666 | var cont = document.getElementById("content"); |
| 667 | var spans = cont.getElementsByTagName("span"); |
| 668 | var refs = {}; |
| 669 | var n = 0; |
| 670 | for (i=0; i<spans.length; i++) { |
| 671 | if (spans[i].className == "footnote") { |
| 672 | n++; |
| 673 | var note = spans[i].getAttribute("data-note"); |
| 674 | if (!note) { |
| 675 | // Use [\s\S] in place of . so multi-line matches work. |
| 676 | // Because JavaScript has no s (dotall) regex flag. |
| 677 | note = spans[i].innerHTML.match(/\s*\[([\s\S]*)]\s*/)[1]; |
| 678 | spans[i].innerHTML = |
| 679 | "[<a id='_footnoteref_" + n + "' href='#_footnote_" + n + |
| 680 | "' title='View footnote' class='footnote'>" + n + "</a>]"; |
| 681 | spans[i].setAttribute("data-note", note); |
| 682 | } |
| 683 | noteholder.innerHTML += |
| 684 | "<div class='footnote' id='_footnote_" + n + "'>" + |
| 685 | "<a href='#_footnoteref_" + n + "' title='Return to text'>" + |
| 686 | n + "</a>. " + note + "</div>"; |
| 687 | var id =spans[i].getAttribute("id"); |
| 688 | if (id != null) refs["#"+id] = n; |
| 689 | } |
| 690 | } |
| 691 | if (n == 0) |
| 692 | noteholder.parentNode.removeChild(noteholder); |
| 693 | else { |
| 694 | // Process footnoterefs. |
| 695 | for (i=0; i<spans.length; i++) { |
| 696 | if (spans[i].className == "footnoteref") { |
| 697 | var href = spans[i].getElementsByTagName("a")[0].getAttribute("href"); |
| 698 | href = href.match(/#.*/)[0]; // Because IE return full URL. |
| 699 | n = refs[href]; |
| 700 | spans[i].innerHTML = |
| 701 | "[<a href='#_footnote_" + n + |
| 702 | "' title='View footnote' class='footnote'>" + n + "</a>]"; |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | }, |
| 707 | |
| 708 | install: function(toclevels) { |
| 709 | var timerId; |
| 710 | |
| 711 | function reinstall() { |
| 712 | asciidoc.footnotes(); |
| 713 | if (toclevels) { |
| 714 | asciidoc.toc(toclevels); |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | function reinstallAndRemoveTimer() { |
| 719 | clearInterval(timerId); |
| 720 | reinstall(); |
| 721 | } |
| 722 | |
| 723 | timerId = setInterval(reinstall, 500); |
| 724 | if (document.addEventListener) |
| 725 | document.addEventListener("DOMContentLoaded", reinstallAndRemoveTimer, false); |
| 726 | else |
| 727 | window.onload = reinstallAndRemoveTimer; |
| 728 | } |
| 729 | |
| 730 | } |
| 731 | asciidoc.install(4); |
| 732 | /*]]>*/ |
| 733 | </script> |
| 734 | </head> |
| 735 | <body class="book"> |
| 736 | <div id="header"> |
| 737 | <h1>LA_pack primer</h1> |
| 738 | <div id="toc"> |
| 739 | <div id="toctitle">Table of Contents</div> |
| 740 | </div> |
| 741 | </div> |
| 742 | <div id="content"> |
| 743 | <div class="sect1"> |
| 744 | <h2 id="_abstract">Abstract</h2> |
| 745 | <div class="sectionbody"> |
| 746 | <div class="paragraph"><p>GNU APL implements the APL functions monadic ⌹ (aka. <strong>Matrix Inverse</strong>) and |
| 747 | dyadic ⌹ (aka. <strong>Matrix Divide</strong>) by means of <em>LApack functions</em>. These LApack |
| 748 | functions were originally written in FORTRAN, but then manually translated |
| 749 | from FORTRAN to C++ by the author. In this translation, features of the |
| 750 | LApack functions that were not needed for ⌹ were removed to simplify the C++ |
| 751 | code as much as possible.</p></div> |
| 752 | </div> |
| 753 | </div> |
| 754 | <div class="sect1"> |
| 755 | <h2 id="_monadic_matrix_inverse">Monadic ⌹: Matrix Inverse</h2> |
| 756 | <div class="sectionbody"> |
| 757 | <div class="paragraph"><p>Monadic <strong>⌹</strong> is the special case of dyadic <strong>A⌹B</strong> where A is the unit matrix:</p></div> |
| 758 | <div class="listingblock"> |
| 759 | <div class="content"> |
| 760 | <pre> |
| 761 | |
| 762 | <code> Let A←(↑⍴B) ∘.= (↑⍴B). Then ⌹B ←→ A⍴B.</code> |
| 763 | |
| 764 | </pre> |
| 765 | </div></div> |
| 766 | <div class="paragraph"><p><strong>Bif_F12_DOMINO::eval_B(Value_P B) const</strong>, implements |
| 767 | monadic <strong>⌹</strong>. It simply constructs a unit matrix <strong>I</strong> of proper |
| 768 | size and then returns <strong>Bif_F12_DOMINO::eval_AB(I, B)</strong>, i.e. the result of |
| 769 | the implementation of dyadic <strong>A⌹B</strong>. See below.</p></div> |
| 770 | </div> |
| 771 | </div> |
| 772 | <div class="sect1"> |
| 773 | <h2 id="_dyadic_a_b_matrix_divide">Dyadic A⌹B: Matrix Divide</h2> |
| 774 | <div class="sectionbody"> |
| 775 | <div class="paragraph"><p>In APL, <strong>A⌹B</strong> for an N-column matrix <strong>A</strong> is defined in terms of each column |
| 776 | of <strong>A</strong>:</p></div> |
| 777 | <div class="listingblock"> |
| 778 | <div class="content"> |
| 779 | <pre> |
| 780 | |
| 781 | <code> Let X←A⌹B. Then X[;J] ←→ A[;J]⌹B for all (scalar) columns J∈⍳¯1↑⍴A.</code> |
| 782 | |
| 783 | </pre> |
| 784 | </div></div> |
| 785 | <div class="paragraph"><p>In other words, computing <strong>A⌹B</strong> for a matrix <strong>A</strong> boils down to computing |
| 786 | <strong>A⌹B</strong> for (column-) vectors (which are the columns of <strong>A</strong>). For every such |
| 787 | column vector a of <strong>A</strong>, the result is a column vector <strong>x</strong> of <strong>X</strong>:</p></div> |
| 788 | <div class="literalblock"> |
| 789 | <div class="content"> |
| 790 | <pre> |
| 791 | |
| 792 | <code>B ∘ x ≡ a ←→ x ≡ a ⌹ B ←→ x ≡ a ∘ B⁻¹</code> |
| 793 | |
| 794 | </pre> |
| 795 | </div></div> |
| 796 | <div class="admonitionblock"> |
| 797 | <table><tr> |
| 798 | <td class="icon"> |
| 799 | <img src="/usr/share/asciidoc/icons/note.png" alt="Note" /> |
| 800 | </td> |
| 801 | <td class="content">GNU APL has a dyadic function <strong>A∘B</strong> ←→ <strong>A +.× B</strong>. Usage statistics of |
| 802 | APL show that the vast majority of inner products is the matrix multiplication |
| 803 | aka. <strong>A +.× B</strong>. The GNU APL function <strong>A∘B</strong> is an optimized version of this |
| 804 | important special case <strong>A +.× B</strong> of the more general inner product <strong>A f.g B</strong>.</td> |
| 805 | </tr></table> |
| 806 | </div> |
| 807 | <div class="paragraph"><p>Because <strong>B</strong> is the same for all columns a of <strong>A</strong> (and their corresponding |
| 808 | result columns <strong>x</strong> of <strong>X</strong>), the result <strong>X</strong> of <strong>X←A⌹B</strong> is computed in two steps:</p></div> |
| 809 | <div class="olist arabic"><ol class="arabic"> |
| 810 | <li> |
| 811 | <p> |
| 812 | compute <strong>B⁻¹</strong> |
| 813 | </p> |
| 814 | </li> |
| 815 | <li> |
| 816 | <p> |
| 817 | compute <strong>X←A∘B⁻¹</strong> |
| 818 | </p> |
| 819 | </li> |
| 820 | </ol></div> |
| 821 | <div class="paragraph"><p>The second step <strong>X←A∘B⁻¹</strong> is simple, therefore only the first step, |
| 822 | the computation of <strong>B⁻¹</strong> is of concern here.</p></div> |
| 823 | <div class="paragraph"><p>BTW, the iteration over the columns of <strong>A</strong> in the second step above occurs |
| 824 | near the end of function <strong>LA_pack::gelsy()</strong> in function <strong>trsm()</strong>.</p></div> |
| 825 | <div class="paragraph"><p>To compute <strong>B⁻¹</strong>, matrix <strong>B</strong> is factorized into two matrices <strong>Q</strong> and <strong>R</strong> |
| 826 | which have the following properties:</p></div> |
| 827 | <div class="ulist"><ul> |
| 828 | <li> |
| 829 | <p> |
| 830 | <strong>Q</strong> is orthogonal (Hermitian for complex <strong>B</strong>). That is: <strong>Q⁻¹ ≡ ⍉+Q</strong>, |
| 831 | </p> |
| 832 | </li> |
| 833 | <li> |
| 834 | <p> |
| 835 | <strong>R</strong> is upper triangular, i.e. <strong>Rᵢⱼ = 0</strong> for all <strong>i > j</strong>, |
| 836 | </p> |
| 837 | </li> |
| 838 | <li> |
| 839 | <p> |
| 840 | <strong>B = Q∘R</strong>, |
| 841 | </p> |
| 842 | </li> |
| 843 | </ul></div> |
| 844 | <div class="paragraph"><p>Once <strong>Q</strong> and <strong>R</strong> are found, <strong>B⁻¹</strong> can be computed easily with:</p></div> |
| 845 | <div class="listingblock"> |
| 846 | <div class="content"> |
| 847 | <pre> |
| 848 | |
| 849 | <code> B⁻¹ = (Q∘R)⁻¹ = R⁻¹∘Q⁻¹ = R⁻¹∘⍉+Q.</code> |
| 850 | |
| 851 | </pre> |
| 852 | </div></div> |
| 853 | <div class="paragraph"><p>The inverse of an upper triangular matrix (such as <strong>R⁻¹</strong>) is far easier |
| 854 | to compute than the inverse of a general matrix (such as <strong>B⁻¹</strong>). See below. |
| 855 | What remains is therefore the computation of a QR factorization of an |
| 856 | arbitrary matrix <strong>B</strong>.</p></div> |
| 857 | <div class="sect2"> |
| 858 | <h3 id="_inversion_of_an_orthogonal_q">Inversion of an orthogonal Q</h3> |
| 859 | <div class="paragraph"><p>The inversion of an orthogonal matrix <strong>Q</strong> means transposition of <strong>Q</strong> (and, |
| 860 | in the complex case, conjugation of the complex items). |
| 861 | However, unlike in APL where <strong>⍉Q</strong> is a new value computed from <strong>Q</strong>, this |
| 862 | transposition is never computed in C++ or in FORTRAN. Instead, where <strong>⍉Q</strong> is |
| 863 | needed, the items of matrix <strong>Q</strong> are simply accessed with the row and column |
| 864 | interchanged. IOW, with a little bit of care regarding what are rows and what |
| 865 | are columns <strong>⍉Q</strong> is no-op. In GNU APL three typedefs are used to distinguish |
| 866 | rows and columns throughout the code:</p></div> |
| 867 | <div class="listingblock"> |
| 868 | <div class="content"> |
| 869 | <pre> |
| 870 | |
| 871 | <code> In LApack.hh: |
| 872 | |
| 873 | typedef int Crow; // row number |
| 874 | typedef int Ccol; // column number |
| 875 | typedef int Cdia; // diagonal item (row == column)</code> |
| 876 | |
| 877 | </pre> |
| 878 | </div></div> |
| 879 | <div class="paragraph"><p>The translation from FORTRAN to C++ is somewhat tricky for two reasons:</p></div> |
| 880 | <div class="olist arabic"><ol class="arabic"> |
| 881 | <li> |
| 882 | <p> |
| 883 | FORTRAN indices run from 1..N while C++ indices run from 0..N-1 |
| 884 | </p> |
| 885 | </li> |
| 886 | <li> |
| 887 | <p> |
| 888 | FORTRAN matrices are stored in column-major order (i.e. adjacent items |
| 889 | in memory belong to the same column) while C++ matrices are stored in |
| 890 | row-major order (i.e. adjacent items in memory belong to the same row). |
| 891 | </p> |
| 892 | </li> |
| 893 | </ol></div> |
| 894 | </div> |
| 895 | <div class="sect2"> |
| 896 | <h3 id="_inverting_an_upper_triangular_matrix_r">Inverting an upper triangular matrix R</h3> |
| 897 | <div class="paragraph"><p>Suppose <strong>R = op₁ ∘ op₂ ∘ … ∘ opₙ ∘ ID</strong> where every <strong>opₖ</strong> adds a multiple |
| 898 | of some row of R to some other row of R. Then, as we learn in school, |
| 899 | <strong>R⁻¹ = ID ∘ op₁ ∘ op₂ ∘ … ∘ opₙ</strong>. In other words, if the operations |
| 900 | <strong>op₁</strong>, <strong>op₂</strong>, …, <strong>opₙ</strong> turn a matrix <strong>R</strong> into the identity matrix, then |
| 901 | the same operations (in the same order) turn the identity matrix into |
| 902 | the inverse <strong>R⁻¹</strong> of <strong>R</strong>:</p></div> |
| 903 | <div class="listingblock"> |
| 904 | <div class="content"> |
| 905 | <pre> |
| 906 | |
| 907 | <code> ┬ ╔═════R═════╤════AUG════╗ ╔════ID═════╤═══R⁻¹═════╗ ┬ |
| 908 | │ ║ │ 1 ║ ║ 1 │ ║ │ |
| 909 | │ ║ │ 1 0 ║ ║ 1 0 │ ║ │ |
| 910 | N ║ │ 1 ║ → ║ 1 │ ║ N |
| 911 | │ ║ │ 0 1 ║ ║ 0 1 │ ║ │ |
| 912 | │ ║ │ 1 ║ ║ 1 │ ║ │ |
| 913 | ┴ ╚═══════════╧═══════════╝ ╚═══════════╧═══════════╝ ┴ |
| 914 | ├ ─────N─────┼─────N─────┤ ├─────N─────┼─────N─────┤</code> |
| 915 | |
| 916 | </pre> |
| 917 | </div></div> |
| 918 | <div class="paragraph"><p>The operations that turn an upper triangular matrix <strong>R</strong> into the identity |
| 919 | matrix ID are fairly simple:</p></div> |
| 920 | <div class="olist arabic"><ol class="arabic"> |
| 921 | <li> |
| 922 | <p> |
| 923 | <strong>R</strong> is upper triangular. Therefore the last row of <strong>R</strong>, say row <strong>n</strong>, |
| 924 | has only one nonzero element <strong>rₙₙ</strong> on its diagonal. For every row above |
| 925 | that last row, say row <strong>k</strong> (i.e. with k < n), do: |
| 926 | </p> |
| 927 | <div class="olist loweralpha"><ol class="loweralpha"> |
| 928 | <li> |
| 929 | <p> |
| 930 | Let <strong>rₖₙ</strong> be the last element in row <strong>k</strong>. Subtract a multiple of the last |
| 931 | row from row <strong>k</strong> so that rₖₙ becomes <strong>0</strong>. |
| 932 | </p> |
| 933 | </li> |
| 934 | <li> |
| 935 | <p> |
| 936 | That multiple is <strong>rₖₙ÷rₙₙ</strong>. |
| 937 | </p> |
| 938 | </li> |
| 939 | <li> |
| 940 | <p> |
| 941 | Since all other elements of the last row are <strong>0</strong>, this only sets <strong>rₖₙ</strong> to |
| 942 | <strong>0</strong> but does not change any other element of row <strong>k</strong>. |
| 943 | </p> |
| 944 | </li> |
| 945 | <li> |
| 946 | <p> |
| 947 | After repeating this step for every k < n the entire last column of the |
| 948 | matrix <strong>R</strong> is <strong>0</strong>, except for the element <strong>rₙₙ</strong> on the diagonal. |
| 949 | </p> |
| 950 | </li> |
| 951 | </ol></div> |
| 952 | </li> |
| 953 | <li> |
| 954 | <p> |
| 955 | Repeat the same step for the second-last, third-last, … first column of |
| 956 | the matrix. In every iteration one column of <strong>R</strong> is set to 0 above the |
| 957 | diagonal. After the last <strong>k=1</strong> the matrix is diagonal (i.e. all elements |
| 958 | above <strong>and</strong> below the diagonal are <strong>0</strong>). |
| 959 | </p> |
| 960 | </li> |
| 961 | <li> |
| 962 | <p> |
| 963 | Finally, subtract a multiple of every row from itself so that the |
| 964 | diagonal item becomes <strong>1.0</strong>. |
| 965 | </p> |
| 966 | <div class="olist loweralpha"><ol class="loweralpha"> |
| 967 | <li> |
| 968 | <p> |
| 969 | That multiple for diagonal element rₖₖ of row k is (1-rₖₖ)÷rₖₖ. |
| 970 | </p> |
| 971 | </li> |
| 972 | <li> |
| 973 | <p> |
| 974 | After repeating that step for every <strong>k ≤ n</strong> <strong>R</strong> is the unit matrix. |
| 975 | </p> |
| 976 | </li> |
| 977 | </ol></div> |
| 978 | </li> |
| 979 | <li> |
| 980 | <p> |
| 981 | Notice that every operation above subtracts a multiple of some row from some |
| 982 | other (or the same) row. |
| 983 | </p> |
| 984 | </li> |
| 985 | </ol></div> |
| 986 | <div class="paragraph"><p>The algorithm above defines the sequence <strong>op₁ ∘ op₂ ∘ … ∘ opₙ</strong> above, |
| 987 | where <strong>n</strong> is the number of columns in <strong>R</strong>. In practice (in LApack and |
| 988 | consequently also in GNU APL) these operations are not performed on <strong>R</strong> first |
| 989 | and then on the identity matrix afterwords but rather in parallel:</p></div> |
| 990 | <div class="olist arabic"><ol class="arabic"> |
| 991 | <li> |
| 992 | <p> |
| 993 | Start with a matrix <strong>R, AUG</strong> where <strong>AUG←(⍴R)↑(⍳↑⍴R)∘.=⍳↑⍴R)</strong>. That is, <strong>AUG</strong> |
| 994 | is initially the unit matrix with the same shape as <strong>R</strong>. The matrix <strong>AUG</strong> |
| 995 | (or sometimes the matrix <strong>R,AUG</strong>) is commonly known as the <em>augmented matrix</em>. |
| 996 | </p> |
| 997 | </li> |
| 998 | <li> |
| 999 | <p> |
| 1000 | The operations described above are then simultaneously applied to the |
| 1001 | left half (i.e. to <strong>R</strong>) and to the right half (i.e. to <strong>AUG</strong>) until the left |
| 1002 | half <strong>R</strong> of <strong>R,AUG</strong> has become the unit matrix. At that point, the right |
| 1003 | half <strong>AUG</strong> has become the desired inverse <strong>R⁻¹</strong> of <strong>R</strong>. |
| 1004 | </p> |
| 1005 | </li> |
| 1006 | <li> |
| 1007 | <p> |
| 1008 | All this happens in functions <strong>LA_pack::invert_UTM()</strong> and |
| 1009 | <strong>LA_pack::invert_QUTM()</strong>. The term <strong>QUTM</strong> stands for <strong>Q</strong>uadratic <strong>U</strong>pper |
| 1010 | <strong>T</strong>riangular <strong>M</strong>atrix and takes advantage of the fact that the inversion |
| 1011 | of a non-quadratic upper triangular M×N matrix is essentially the inversion |
| 1012 | of its upper N×N sub-matrix. The rows N+1..M are all zero and become zero |
| 1013 | columns in the inverse. |
| 1014 | </p> |
| 1015 | </li> |
| 1016 | </ol></div> |
| 1017 | </div> |
| 1018 | <div class="sect2"> |
| 1019 | <h3 id="_qr_factorization">QR factorization</h3> |
| 1020 | <div class="sect3"> |
| 1021 | <h4 id="_householder_matrix">Householder matrix</h4> |
| 1022 | <div class="paragraph"><p>For the moment we consider only real matrices; the complex case is almost |
| 1023 | identical, except that <strong>⍉X</strong> becomes <strong>⍉+X</strong> in the complex case. <strong>⍉+X</strong> is |
| 1024 | called the <em>conjugate transpose</em> of <strong>X</strong> which is the same as the "normal" |
| 1025 | transpose if <strong>X</strong> is real.</p></div> |
| 1026 | <div class="paragraph"><p>Let <strong>B</strong> be an arbitrary M×N matrix.</p></div> |
| 1027 | <div class="paragraph"><p>Let <strong>V←1↓B[;1]</strong>, i.e. V is the first column of <strong>B</strong> except the |
| 1028 | diagonal item <strong>B[1;1]</strong>.</p></div> |
| 1029 | <div class="paragraph"><p>Let <strong>L←+/V×V</strong>, i.e. <strong>L</strong> is (the square of) the Euclidean length of <strong>V</strong>.</p></div> |
| 1030 | <div class="paragraph"><p>The Householder matrix <strong>H=H(B)</strong> for matrix <strong>B</strong> is then defined as:</p></div> |
| 1031 | <div class="listingblock"> |
| 1032 | <div class="content"> |
| 1033 | <pre> |
| 1034 | |
| 1035 | <code> 2 |
| 1036 | H = I - ─ × V ∘.× V. |
| 1037 | L</code> |
| 1038 | |
| 1039 | </pre> |
| 1040 | </div></div> |
| 1041 | <div class="paragraph"><p>That is:</p></div> |
| 1042 | <div class="listingblock"> |
| 1043 | <div class="content"> |
| 1044 | <pre> |
| 1045 | |
| 1046 | <code> ⎧ 1.0 - 2×V[i]×V[i]÷L if i=j, and |
| 1047 | H = (H)ᵢⱼ = ⎨ |
| 1048 | ⎩ - 2×V[i]×V[j]÷L otherwise.</code> |
| 1049 | |
| 1050 | </pre> |
| 1051 | </div></div> |
| 1052 | <div class="paragraph"><p>It follows immediately that:</p></div> |
| 1053 | <div class="olist arabic"><ol class="arabic"> |
| 1054 | <li> |
| 1055 | <p> |
| 1056 | <strong>H</strong> is <em>Hermitian</em> (aka. <em>symmetric</em> if <strong>H</strong> is real). I.e. <strong>H = ⍉+H</strong>, |
| 1057 | </p> |
| 1058 | </li> |
| 1059 | <li> |
| 1060 | <p> |
| 1061 | <strong>H</strong> is <em>unitary</em> (aka. <em>orthogonal</em> if <strong>H</strong> is real). I.e. <strong>H⁻¹ = ⍉+H</strong>, and |
| 1062 | </p> |
| 1063 | </li> |
| 1064 | <li> |
| 1065 | <p> |
| 1066 | <strong>H</strong> is <em>involutary</em>. I.e. <strong>H</strong> = <strong>H⁻¹</strong> |
| 1067 | </p> |
| 1068 | </li> |
| 1069 | </ol></div> |
| 1070 | <div class="paragraph"><p>These really nice properties of <strong>H</strong>:</p></div> |
| 1071 | <div class="olist arabic"><ol class="arabic"> |
| 1072 | <li> |
| 1073 | <p> |
| 1074 | make the computation of the transposition <strong>⍉H</strong> essentially a no-op, and |
| 1075 | </p> |
| 1076 | </li> |
| 1077 | <li> |
| 1078 | <p> |
| 1079 | ensures that the composition <strong>H = H₁ ∘ H₂ ∘ … ∘ Hₙ</strong> of several Householder |
| 1080 | matrices is also Hermitian (complex case) or orthogonal (real case). |
| 1081 | </p> |
| 1082 | </li> |
| 1083 | </ol></div> |
| 1084 | <div class="paragraph"><p>In the following let:</p></div> |
| 1085 | <div class="ulist"><ul> |
| 1086 | <li> |
| 1087 | <p> |
| 1088 | <strong>B</strong> be an arbitrary real or complex matrix, |
| 1089 | </p> |
| 1090 | </li> |
| 1091 | <li> |
| 1092 | <p> |
| 1093 | vector <strong>B1</strong> be the first column of <strong>B</strong>, and |
| 1094 | </p> |
| 1095 | </li> |
| 1096 | <li> |
| 1097 | <p> |
| 1098 | scalar <strong>B11</strong> be the first item of column vector <strong>B1</strong> (= the first item |
| 1099 | on the diagonal of B). |
| 1100 | </p> |
| 1101 | </li> |
| 1102 | </ul></div> |
| 1103 | <div class="paragraph"><p>In the LApack code <strong>B1</strong> is usually a variable named (lowercase) <strong>x</strong> and <strong>B11</strong> |
| 1104 | a scalar variable named (uppercase) <strong>ALPHA</strong> (or <strong>⍺</strong> in comments). |
| 1105 | That is, matrix B looks like this:</p></div> |
| 1106 | <div class="listingblock"> |
| 1107 | <div class="content"> |
| 1108 | <pre> |
| 1109 | |
| 1110 | <code> LApack matrix B APL matrix B |
| 1111 | ╔═══╤═════════╗ ╔═════╤═════════╗ |
| 1112 | ║ ⍺ │ ║ ║ B11 │ ║ |
| 1113 | ╟───┤ ║ ╟─────┤ ║ |
| 1114 | ║ │ ║ ║ │ ║ |
| 1115 | ║ │ ║ ←→ ║ │ ║ |
| 1116 | ║ x │ ║ ║ B1 │ ║ |
| 1117 | ║ │ ║ ║ │ ║ |
| 1118 | ║ │ ║ ║ │ ║ |
| 1119 | ╚═══╧═════════╝ ╚═════╧═════════╝</code> |
| 1120 | |
| 1121 | </pre> |
| 1122 | </div></div> |
| 1123 | <div class="paragraph"><p>There are subtle but noteworthy differences between the APL code discussed |
| 1124 | below and the LApack FORTRAN or C++ code:</p></div> |
| 1125 | <div class="ulist"><ul> |
| 1126 | <li> |
| 1127 | <p> |
| 1128 | In the original FORTRAN code of LApack, the vector <strong>x</strong> <em>excludes</em> the |
| 1129 | diagonal element <strong>⍺</strong> and <strong>⍺</strong> is passed as a separate function argument or |
| 1130 | result. |
| 1131 | </p> |
| 1132 | </li> |
| 1133 | <li> |
| 1134 | <p> |
| 1135 | In the GNU APL C++ translation of that code, the vector <strong>x</strong> also |
| 1136 | <em>excludes</em> the diagonal element <strong>⍺</strong>. However, <strong>⍺</strong> is not passed as a separate |
| 1137 | function argument like in FORTRAN. Instead C++ takes advantage of the fact |
| 1138 | that our matrices are stored in FORTRAN order which makes <strong>⍺ ←→ x[-1]</strong> and |
| 1139 | therefore a function that already knows <strong>x</strong> can easily recover <strong>⍺</strong> from <strong>x</strong>. |
| 1140 | </p> |
| 1141 | </li> |
| 1142 | <li> |
| 1143 | <p> |
| 1144 | In the APL code below, the vector <strong>B1</strong> (which corresponds to <strong>x</strong>) |
| 1145 | <em>includes</em> the scalar <strong>B11</strong> (which corresponds to <strong>⍺</strong>). |
| 1146 | </p> |
| 1147 | </li> |
| 1148 | </ul></div> |
| 1149 | <div class="paragraph"><p>Instead of a strict mathematical proof we use APL as a tool of thought.</p></div> |
| 1150 | <div class="paragraph"><p>Let <strong>V</strong> be a real or complex vector. The following APL function named |
| 1151 | <strong>Householder</strong> simply implements the definition of a Householder matrix |
| 1152 | given above:</p></div> |
| 1153 | <div class="listingblock"> |
| 1154 | <div class="content"> |
| 1155 | <pre> |
| 1156 | |
| 1157 | <code>∇H←Householder V;I;L |
| 1158 | ⍝ |
| 1159 | ⍝⍝ return the Householder matrix (aka. elementary reflector) for vector V |
| 1160 | ⍝ |
| 1161 | I←I∘.=I←⍳⍴V ⍝ I: the (⍴V)×(⍴V) identity matrix, |
| 1162 | L←+/V×+V ⍝ L: ║V║² i.e. the square of the norm of vector V |
| 1163 | H←I-(2÷L)×V∘.×+V ⍝ H: the Householder matrix for vector V |
| 1164 | ∇</code> |
| 1165 | |
| 1166 | </pre> |
| 1167 | </div></div> |
| 1168 | <div class="paragraph"><p>Next we define a somewhat random matrix <strong>B</strong> that shall prove our claims |
| 1169 | by example (aka. <em>incomplete induction</em>):</p></div> |
| 1170 | <div class="listingblock"> |
| 1171 | <div class="content"> |
| 1172 | <pre> |
| 1173 | |
| 1174 | <code> MN←(M N)←4 3 ⍝ choose a matrix size (M≥N) |
| 1175 | ⎕RL←+/⎕TS ◊ B←?(2, MN)⍴18 ⍝ prepare for random B |
| 1176 | B←+⌿9 0J9×[1]B ⍝ some complex M×N random matrix B |
| 1177 | B11←↑B1←B[;1] ⍝ vector B1 is column 1 of matrix B, and scalar B11 its first item</code> |
| 1178 | |
| 1179 | </pre> |
| 1180 | </div></div> |
| 1181 | <div class="paragraph"><p>With these prerequisites we can "prove" the following:</p></div> |
| 1182 | <div class="paragraph"><p><strong>Proposition:</strong> Let</p></div> |
| 1183 | <div class="listingblock"> |
| 1184 | <div class="content"> |
| 1185 | <pre> |
| 1186 | |
| 1187 | <code> L11←×B11 ⍝ B11 normalized to length 1 (= ±1 for real B11) |
| 1188 | LB1←(+/B1×+B1)⋆0.5 ⍝ scalar LB1 is ║B1║ i.e. the length of B1 |
| 1189 | |
| 1190 | H1←Householder V1←1↓B1 + L11×M↑LB1 ⍝ a Householder matrix (from V1) |
| 1191 | H2←Householder V2←1↓B1 - L11×M↑LB1 ⍝ another Householder matrix (from V2)</code> |
| 1192 | |
| 1193 | </pre> |
| 1194 | </div></div> |
| 1195 | <div class="paragraph"><p>Then the first columns of matrices <strong>B∘H1</strong> and of <strong>B∘H2</strong> are 0 below the |
| 1196 | top-left item <strong>B11</strong> of <strong>B</strong>.</p></div> |
| 1197 | <div class="paragraph"><p><strong>Proofs</strong> (by APL examples <strong>B</strong>):</p></div> |
| 1198 | <div class="listingblock"> |
| 1199 | <div class="content"> |
| 1200 | <pre> |
| 1201 | |
| 1202 | <code> ∇ |
| 1203 | [0] H←Householder V;I;L |
| 1204 | [1] ⍝ |
| 1205 | [2] ⍝⍝ return the Householder matrix (aka. elementary reflector) for vector V |
| 1206 | [3] ⍝ |
| 1207 | [4] I←I∘.=I←⍳⍴V ⍝ I: the (⍴V)×(⍴V) identity matrix, |
| 1208 | [5] L←+/V×+V ⍝ L: ║V║² i.e. the square of the norm of vector V |
| 1209 | [6] H←I-(2÷L)×V∘.×+V ⍝ H: the Householder matrix for vector V |
| 1210 | ∇ |
| 1211 | |
| 1212 | LB1←(+/B1×+B1)⋆0.5 ⍝ scalar LB1 is ║B1║ i.e. the length of B1 |
| 1213 | L11←×B11 ⍝ B11 normalized to length 1 (= ±1 for real B11) |
| 1214 | H1←Householder 1↓B1+L11×M↑LB1 |
| 1215 | H2←Householder 1↓B1-L11×M↑LB1 |
| 1216 | |
| 1217 | H1 = ⍉+H1 ⍝ H1 is Hermitian |
| 1218 | 1 1 1 1 |
| 1219 | 1 1 1 1 |
| 1220 | 1 1 1 1 |
| 1221 | 1 1 1 1 |
| 1222 | |
| 1223 | (⌹H1) = ⍉+H1 ⍝ H1 is Unitary |
| 1224 | 1 1 1 1 |
| 1225 | 1 1 1 1 |
| 1226 | 1 1 1 1 |
| 1227 | 1 1 1 1 |
| 1228 | |
| 1229 | H1 = ⌹H1 ⍝ H1 is Involutary |
| 1230 | 1 1 1 1 |
| 1231 | 1 1 1 1 |
| 1232 | 1 1 1 1 |
| 1233 | 1 1 1 1 |
| 1234 | |
| 1235 | B |
| 1236 | 27J117 54J72 18J27 |
| 1237 | 81J117 18J117 126J144 |
| 1238 | 117J81 117J108 9J54 |
| 1239 | 135J27 117J18 117J90 |
| 1240 | |
| 1241 | 2⍕H1 ∘ B ⍝ notice the zeros in first column |
| 1242 | ¯61.12J¯264.83 ¯33.28J¯233.03 ¯8.67J¯203.23 |
| 1243 | .00J.00 ¯52.41J25.80 86.09J69.90 |
| 1244 | .00J.00 19.00J47.42 ¯54.44J¯1.31 |
| 1245 | .00J.00 6.71J1.84 39.43J65.25 |
| 1246 | |
| 1247 | 2⍕H2 ∘ B ⍝ notice zeros in the first column |
| 1248 | 61.12J264.83 33.28J233.03 8.67J203.23 |
| 1249 | .00J.00 ¯19.79J¯30.51 73.02J¯12.81 |
| 1250 | .00J.00 29.70J¯16.77 ¯95.79J¯74.13 |
| 1251 | .00J.00 ¯8.43J¯59.27 ¯25.25J16.46</code> |
| 1252 | |
| 1253 | </pre> |
| 1254 | </div></div> |
| 1255 | </div> |
| 1256 | </div> |
| 1257 | <div class="sect2"> |
| 1258 | <h3 id="_repeated_application_of_reflectors">Repeated Application of Reflectors</h3> |
| 1259 | <div class="paragraph"><p>Once we know how to set the first column of some arbitrary matrix <strong>B</strong> |
| 1260 | to <strong>0</strong> (below the diagonal) we can now turn the entire matrix into an |
| 1261 | upper triangular form:</p></div> |
| 1262 | <div class="olist arabic"><ol class="arabic"> |
| 1263 | <li> |
| 1264 | <p> |
| 1265 | Set <strong>B₁ = B</strong> and find a reflector <strong>H₁</strong> that sets the items below the first |
| 1266 | column of <strong>B₁</strong> to <strong>0</strong>. |
| 1267 | </p> |
| 1268 | </li> |
| 1269 | <li> |
| 1270 | <p> |
| 1271 | Repeat that step for <strong>B₂ = 1 1↓B₁, B₃ = 1 1↓B₂, … Bₙ = 1 1↓Bₙ₋₁</strong> which, |
| 1272 | in the end, computes a sequence of reflectors <strong>H₁, H₂, … Hₙ</strong>. |
| 1273 | In this process: |
| 1274 | </p> |
| 1275 | <div class="olist lowerroman"><ol class="lowerroman"> |
| 1276 | <li> |
| 1277 | <p> |
| 1278 | the matrices Bₖ become smaller and smaller, |
| 1279 | </p> |
| 1280 | </li> |
| 1281 | <li> |
| 1282 | <p> |
| 1283 | the reflectors Hₖ become shorter and shorter, and |
| 1284 | </p> |
| 1285 | </li> |
| 1286 | <li> |
| 1287 | <p> |
| 1288 | the upper triangular result matrix result becomes more and more complete. |
| 1289 | </p> |
| 1290 | </li> |
| 1291 | </ol></div> |
| 1292 | </li> |
| 1293 | </ol></div> |
| 1294 | <div class="paragraph"><p>After all, say <strong>n</strong>, steps matrix <strong>B</strong> looks like this:</p></div> |
| 1295 | <div class="listingblock"> |
| 1296 | <div class="content"> |
| 1297 | <pre> |
| 1298 | |
| 1299 | <code> Initial B B after 1 step B after 2 steps B after n steps |
| 1300 | ╔═════════════╗ ╔══╤═══════════╗ ╔══╤═══════════╗ ╔══╤═══════════╗ |
| 1301 | ║ ║ ║⍺₁│ R1 ║ ║⍺₁│ R1 ║ ║⍺₁│ R1 ║ |
| 1302 | ║ ║ ╟──┤ ║ ╟──┼──┐ R2 ║ ╟──┼──┐ R2 ║ |
| 1303 | ║ ║ ║ │ ║ ║ │⍺₂│ ║ ║ │⍺₂│ ... ║ |
| 1304 | ║ B=B₁ ║ → ║ │ ║ → ║ ├──┤ ║ ... ║ ├──┤ ║ |
| 1305 | ║ ║ ║H₁│ ║ ║H₁│ │ ║ ║H₁│ │ ... ║ |
| 1306 | ║ ║ ║ │ ║ ║ │H₂│ ║ ║ │H₂│ ┌──╢ |
| 1307 | ║ ║ ║ │ ║ ║ │ │ ║ ║ │ │ │⍺ₙ║ |
| 1308 | ╚═════════════╝ ╚══╧═══════════╝ ╚══╧══╧════════╝ ╚══╧══╧═════╧══╝</code> |
| 1309 | |
| 1310 | </pre> |
| 1311 | </div></div> |
| 1312 | <div class="admonitionblock"> |
| 1313 | <table><tr> |
| 1314 | <td class="icon"> |
| 1315 | <img src="/usr/share/asciidoc/icons/note.png" alt="Note" /> |
| 1316 | </td> |
| 1317 | <td class="content">For lack of space we show <strong>⍺₁ … ⍺ₙ</strong> on the diagonal above, but in |
| 1318 | reality the diagonal is <strong>R₁₁, R₂₂, … Rₙₙ</strong>. The sequence <strong>⍺₁ … ⍺ₙ</strong> is |
| 1319 | stored in <strong>tau</strong> as will be explained below.</td> |
| 1320 | </tr></table> |
| 1321 | </div> |
| 1322 | <div class="paragraph"><p>Now:</p></div> |
| 1323 | <div class="olist arabic"><ol class="arabic"> |
| 1324 | <li> |
| 1325 | <p> |
| 1326 | <strong>R</strong> is upper triangular (where the items below the diagonal are <strong>0</strong> by |
| 1327 | definition and therefore need not be stored in <strong>B</strong>) |
| 1328 | </p> |
| 1329 | </li> |
| 1330 | <li> |
| 1331 | <p> |
| 1332 | <strong>H ← H₁ ∘ H₂ ∘ … ∘ Hₙ</strong> is unitary or orthogonal since it is the |
| 1333 | product of <strong>n</strong> unitary or orthogonal matrices <strong>Hₖ</strong> respectively. |
| 1334 | </p> |
| 1335 | </li> |
| 1336 | </ol></div> |
| 1337 | <div class="paragraph"><p>The iteration above takes place in function <strong>LA_pack::laqp2()</strong> which starts |
| 1338 | with matrix <strong>B</strong> and ends with <strong>R</strong> in the upper half (including the diagonal), |
| 1339 | <strong>Q</strong> in the lower half, and the diagonal factors <strong>⍺₁ … ⍺ₙ</strong> stored in |
| 1340 | variable <strong>tau</strong>.</p></div> |
| 1341 | <div class="paragraph"><p>At this point the lower half and <strong>tau</strong> are not the matrix <strong>Q</strong> itself but the |
| 1342 | reflectors that turn the identity matrix into <strong>Q</strong>. For this reason we usually |
| 1343 | refer to lower half as <strong>HR</strong> and not as <strong>QR</strong> in the GNU APL C++ code.</p></div> |
| 1344 | </div> |
| 1345 | <div class="sect2"> |
| 1346 | <h3 id="_numerical_considerations">Numerical considerations</h3> |
| 1347 | <div class="paragraph"><p>In mathematics we compute with exact numbers and would be finished at this |
| 1348 | point. On real computers we work with approximations of exact numbers which |
| 1349 | may cause problems with the accuracy of the result.</p></div> |
| 1350 | <div class="paragraph"><p>Let <strong>⍺</strong> and <strong>β</strong> be numbers. For the sake of explanation assume <strong>⍺>0</strong> and <strong>β>0</strong>. |
| 1351 | Due to rounding, <strong>⍺</strong> and <strong>β</strong> only determine two ranges in which the exact |
| 1352 | ⍺ and <strong>β</strong> lie:</p></div> |
| 1353 | <div class="listingblock"> |
| 1354 | <div class="content"> |
| 1355 | <pre> |
| 1356 | |
| 1357 | <code> ⍺ - e₁ ≤ exact ⍺ ≤ ⍺ + e₁ and |
| 1358 | β - e₂ ≤ exact β ≤ β + e₂</code> |
| 1359 | |
| 1360 | </pre> |
| 1361 | </div></div> |
| 1362 | <div class="paragraph"><p>The (usually small) numbers <strong>e₁</strong> and <strong>e₂</strong> define the <em>absolute error</em> of <strong>⍺</strong> |
| 1363 | and <strong>β</strong> while the quotients <strong>e₁÷⍺</strong> and <strong>e₂÷β</strong> define the <em>relative_error</em> |
| 1364 | of <strong>⍺</strong> and <strong>β</strong> respectively.</p></div> |
| 1365 | <div class="paragraph"><p>Now, adding or subtracting the inequalities above gives:</p></div> |
| 1366 | <div class="listingblock"> |
| 1367 | <div class="content"> |
| 1368 | <pre> |
| 1369 | |
| 1370 | <code> ⍺ - e₁ ≤ exact ⍺ ≤ ⍺ + e₁ |
| 1371 | ± β - e₂ ≤ exact β ≤ β + e₂ |
| 1372 | ═════════════════════════════════════════════ |
| 1373 | (⍺±β) - (e₁+e₂) ≤ exact ⍺±β ≤ (⍺±β) + (e₁+e₂)</code> |
| 1374 | |
| 1375 | </pre> |
| 1376 | </div></div> |
| 1377 | <div class="paragraph"><p>This means that the absolute error of the sum or difference <strong>(⍺±β)</strong> is the |
| 1378 | sum of the absolute errors of <strong>⍺</strong> and <strong>β</strong>. This is no problem as long as |
| 1379 | <strong>(⍺±β)</strong> does not come too close to <strong>0</strong>. If it does, however, then the |
| 1380 | absolute error remains small, but the relative error is now:</p></div> |
| 1381 | <div class="listingblock"> |
| 1382 | <div class="content"> |
| 1383 | <pre> |
| 1384 | |
| 1385 | <code> e₁ + e₂ |
| 1386 | ─────── → ∞ as (⍺ ± β) → 0 |
| 1387 | ⍺ ± β</code> |
| 1388 | |
| 1389 | </pre> |
| 1390 | </div></div> |
| 1391 | <div class="paragraph"><p>That is, for <strong>(⍺-β)</strong> the relative error can become huge if <strong>⍺</strong>≈<strong>β</strong>. |
| 1392 | In LApack these issues are addressed in 3 ways:</p></div> |
| 1393 | <div class="olist arabic"><ol class="arabic"> |
| 1394 | <li> |
| 1395 | <p> |
| 1396 | If the input matrices are very large or very small (in terms of their norm) |
| 1397 | then they (and their result) are scaled up or down so that their |
| 1398 | coefficients land in a range that is not subject to major rounding error. |
| 1399 | See function <strong>LA_pack::gelsy()</strong> vs. <strong>LA_pack::scaled_gelsy()</strong>. |
| 1400 | </p> |
| 1401 | </li> |
| 1402 | <li> |
| 1403 | <p> |
| 1404 | As we have seen above, for any matrix <strong>B</strong> there are actually two reflectors |
| 1405 | <strong>H1</strong> and <strong>H2</strong> each of which bring the first column off <strong>B</strong> below the |
| 1406 | diagonal to <strong>0). LA_pack then chooses the one that produces the largest |
| 1407 | difference *V1</strong> or <strong>V2</strong> above. |
| 1408 | </p> |
| 1409 | </li> |
| 1410 | <li> |
| 1411 | <p> |
| 1412 | Some functions, in particular <strong>LA_pack::larfg()</strong> take additional measures |
| 1413 | to minimize the errors caused by too small arguments. |
| 1414 | </p> |
| 1415 | </li> |
| 1416 | <li> |
| 1417 | <p> |
| 1418 | Column pivoting, instead of processing the columns in normal order (different |
| 1419 | from B[;1], B[;2], … B[N]) to avoid small coefficients in the subsequent |
| 1420 | matrices. |
| 1421 | </p> |
| 1422 | </li> |
| 1423 | </ol></div> |
| 1424 | <div class="paragraph"><p>Column pivoting works like this:</p></div> |
| 1425 | <div class="olist arabic"><ol class="arabic"> |
| 1426 | <li> |
| 1427 | <p> |
| 1428 | In every iteration above: |
| 1429 | </p> |
| 1430 | <div class="olist loweralpha"><ol class="loweralpha"> |
| 1431 | <li> |
| 1432 | <p> |
| 1433 | Compute the norms of the columns, |
| 1434 | </p> |
| 1435 | </li> |
| 1436 | <li> |
| 1437 | <p> |
| 1438 | Find the column <strong>k</strong> with the largest norm (called "the pivot") |
| 1439 | </p> |
| 1440 | </li> |
| 1441 | <li> |
| 1442 | <p> |
| 1443 | If <strong>k≠1</strong> (i.e. the first column does not have the largest norm already) |
| 1444 | then exchange columns <strong>1</strong> and <strong>k</strong> before proceeding. |
| 1445 | </p> |
| 1446 | </li> |
| 1447 | <li> |
| 1448 | <p> |
| 1449 | In the context of solving equations, the pivoting is essentially a renaming |
| 1450 | of the variables <strong>x₁, x₂, …, xₙ</strong>. This renaming is later undone (see the |
| 1451 | end of <strong>scaled_gelsy()</strong>) before the result is returned to APL. |
| 1452 | </p> |
| 1453 | </li> |
| 1454 | </ol></div> |
| 1455 | </li> |
| 1456 | </ol></div> |
| 1457 | </div> |
| 1458 | <div class="sect2"> |
| 1459 | <h3 id="_memory_layout">Memory Layout</h3> |
| 1460 | <div class="paragraph"><p>At the time when FORTRAN was popular, memories were small. The LApack FORTRAN |
| 1461 | code addressed this in two ways:</p></div> |
| 1462 | </div> |
| 1463 | <div class="sect2"> |
| 1464 | <h3 id="_blocked_execution">Blocked execution</h3> |
| 1465 | <div class="paragraph"><p>Blocked operation means that the processing of matrices that may not fit |
| 1466 | into the core memory of a computer were processed in chunks. Some FORTRAN |
| 1467 | functions therefore had a "blocked" version (for large matrices) and an |
| 1468 | "unblocked" version (for small matrices and for the chunks of large matrices).</p></div> |
| 1469 | <div class="paragraph"><p>These days memory is large and therefore blocking was removed entirely. Only |
| 1470 | the unblocked FORTRAN functions survived in the GNU APL code.</p></div> |
| 1471 | </div> |
| 1472 | <div class="sect2"> |
| 1473 | <h3 id="_in_place_computation">In-place computation</h3> |
| 1474 | <div class="paragraph"><p>Another heavily used trick to reduce memory consumption is in-place |
| 1475 | computation. Instead of passing the argument of a function in one variable and |
| 1476 | storing the result in a another variable, the same variable was used for the |
| 1477 | largest input variable and the result. For example, function <strong>laqp2()</strong> |
| 1478 | computes the QR factorization of an arbitrary matrix <strong>B</strong> and the result is |
| 1479 | stored at the same memory location. However, the result <strong>QR</strong> is split in two |
| 1480 | pieces along the diagonal, taking advantage of the fact that <strong>Q</strong> is symmetric |
| 1481 | (so its upper half can be derived directly from its lower half, and that <strong>R</strong> |
| 1482 | is upper triangular (so its lower half is implicitly <strong>0</strong> and therefore |
| 1483 | not of interest):</p></div> |
| 1484 | <div class="listingblock"> |
| 1485 | <div class="content"> |
| 1486 | <pre> |
| 1487 | |
| 1488 | <code> Input matrix B Results Q and R |
| 1489 | ╔══════════════╗ ╔════╤═══════════╗ |
| 1490 | ║ ║ ║R₁₁ │ ║ |
| 1491 | ║ ║ ╟────┼───┐ ║ |
| 1492 | ║ ║ ║ │R₂₂│ Rᵢⱼ ║ |
| 1493 | ║ Bᵢⱼ ║ → ║ └───┘ ║ |
| 1494 | ║ ║ ║ ... ║ |
| 1495 | ║ ║ ║ Q ┌───╢ |
| 1496 | ║ ║ ║ │Rₙₙ║ |
| 1497 | ╚══════════════╝ ╚════════════╧═══╝</code> |
| 1498 | |
| 1499 | </pre> |
| 1500 | </div></div> |
| 1501 | <div class="paragraph"><p>Unfortunately in this scheme the matrices of <strong>Q</strong> and <strong>R</strong> now compete for the |
| 1502 | former diagonal of input <strong>B</strong>. This is solved by storing in the diagonal of <strong>Q</strong> |
| 1503 | in a separate vector (named <strong>tau</strong> in the LApack code):</p></div> |
| 1504 | <div class="listingblock"> |
| 1505 | <div class="content"> |
| 1506 | <pre> |
| 1507 | |
| 1508 | <code> ╔══════════════╗ ╔══════════════╗ |
| 1509 | ║ ║ ║ ║ |
| 1510 | ╙───┐ Rᵢⱼ ║ ╟───┐ Rᵢⱼ ║ |
| 1511 | │ ║ ║ │ ║ |
| 1512 | └───┐ ║ ║ └───┐ ║ |
| 1513 | ╔═══╕ │ ║ → ║ │ ║ |
| 1514 | ║⍺₁ │ └───┐ ║ ║ Hᵢⱼ └───┐ ║ |
| 1515 | ╟───┼───┐ │ ║ ║ │ ║ |
| 1516 | ║ │⍺₂ │ ╘══╝ ╚═══════════╧══╝ |
| 1517 | ║ └───┼───┐ |
| 1518 | ║ │...│ |
| 1519 | ║ Hᵢⱼ └───┼──╖ ╔═══╤═══╤═══╤═══╗ |
| 1520 | ║ │⍺ₙ║ tau: ║⍺₁ │⍺₂ │...│⍺ₙ ║ |
| 1521 | ╚═══════════╧══╝ ╚═══╧═══╧═══╧═══╝</code> |
| 1522 | |
| 1523 | </pre> |
| 1524 | </div></div> |
| 1525 | <div class="paragraph"><p>Another issue to understand LApack is that functions <strong>laqp2()</strong> and friends |
| 1526 | do <strong>not</strong> return the matrices <strong>Q</strong> and <strong>R</strong> of the factorization <strong>B = Q∘R</strong> |
| 1527 | directly. Instead of <strong>Q</strong> it returns a matrix <strong>H</strong> with the Householder |
| 1528 | reflectors for the different columns. The matrix <strong>Q</strong> can then be obtained |
| 1529 | by applying these reflectors to the <strong>n×n</strong> unit matrix <strong>ID(n)</strong>:</p></div> |
| 1530 | <div class="listingblock"> |
| 1531 | <div class="content"> |
| 1532 | <pre> |
| 1533 | |
| 1534 | <code> Q ←→ H∘ID(n)</code> |
| 1535 | |
| 1536 | </pre> |
| 1537 | </div></div> |
| 1538 | <div class="paragraph"><p>This avoids one intermediate matrix multiplication. Instead of <strong>Q←H∘I</strong> |
| 1539 | followed by some other <strong>Z←Q∘R</strong>, LApack leaves <strong>H</strong> for the moment and later |
| 1540 | computes <strong>Z←H∘R</strong> directly. In addition LApack avoids the computation of |
| 1541 | the Householder matrices used in the APL examples above for demonstration |
| 1542 | purposes. Instead of full k×k reflector matrices it uses the reflector |
| 1543 | vectors, typically called <strong>v</strong>, and computes the elements <strong>Hᵢⱼ</strong> of each |
| 1544 | reflector as needed. The lifetime of each <strong>Hᵢⱼ</strong> is usually short, |
| 1545 | therefore it makes little sense to store it in a large matrix.</p></div> |
| 1546 | </div> |
| 1547 | <div class="sect2"> |
| 1548 | <h3 id="_lapack_function_names">LApack function names</h3> |
| 1549 | <div class="paragraph"><p>The names of LApack functions look somewhat scary at first glance. For example |
| 1550 | the name of function <strong>zungqr()</strong> may not be too obvious. However these names |
| 1551 | follow a naming scheme that roughly looks as follows:</p></div> |
| 1552 | <div class="listingblock"> |
| 1553 | <div class="content"> |
| 1554 | <pre> |
| 1555 | |
| 1556 | <code> ┌───────── data type (i←→integer, d←→double, z←→complex) |
| 1557 | │┌┬─────── matrix type (un ←→ unitary, or ←→ orthogonal, ge ←→ general, etc.) |
| 1558 | │││┌┬┬──── function type (gqr ←→ generate QR factorization) |
| 1559 | ││││││ function variant (blocked/unblocked, etc. |
| 1560 | ││││││ |
| 1561 | zungqr()</code> |
| 1562 | |
| 1563 | </pre> |
| 1564 | </div></div> |
| 1565 | <div class="paragraph"><p>With this naming scheme function <strong>zungqr()</strong> for complex matrices becomes |
| 1566 | <strong>dorqr2()</strong> for real matrices.</p></div> |
| 1567 | <div class="paragraph"><p>In FORTRAN every complex function (like <strong>zungqr()</strong> has a real counterpart |
| 1568 | <strong>dorqr2()</strong> for real matrices. In fact there are even more variants because |
| 1569 | there are different precisions (like 32-bit <strong>float</strong> and 64-bit <strong>double</strong> |
| 1570 | in C++) and each has its own FORTRAN function. That made a lot of sense |
| 1571 | in the FORTRAN days where it made a considerable difference in performance.</p></div> |
| 1572 | <div class="paragraph"><p>A closer look at these functions revealed, however, that the difference |
| 1573 | between data types are actually rather small. The GNU APL version of these |
| 1574 | functions make the data type a template parameter so that every group of |
| 1575 | FORTRAN functions becomes a single template function in C++. In our example:</p></div> |
| 1576 | <div class="listingblock"> |
| 1577 | <div class="content"> |
| 1578 | <pre> |
| 1579 | |
| 1580 | <code> FORTRAN: |
| 1581 | |
| 1582 | dorgqr QR-factorize real matrix |
| 1583 | zungqr QR-factorize complex matrix |
| 1584 | |
| 1585 | becomes C++ |
| 1586 | |
| 1587 | template<T> ungqr()</code> |
| 1588 | |
| 1589 | </pre> |
| 1590 | </div></div> |
| 1591 | <div class="paragraph"><p>where the template argument <strong><T></strong> is either <strong><DD></strong> for real or <strong><ZZ></strong> for |
| 1592 | complex numbers. The use of C++ templates cuts the number of functions |
| 1593 | needed roughly in half,</p></div> |
| 1594 | </div> |
| 1595 | <div class="sect2"> |
| 1596 | <h3 id="_call_tree">Call Tree</h3> |
| 1597 | <div class="paragraph"><p>With all this in mind, <strong>A⌹B</strong> aka. <strong>Bif_F12_DOMINO::eval_AB</strong> becomes simple. |
| 1598 | We look at its call tree, which is roughly this:</p></div> |
| 1599 | <div class="listingblock"> |
| 1600 | <div class="content"> |
| 1601 | <pre> |
| 1602 | |
| 1603 | <code> Bif_F12_DOMINO::eval_AB() |
| 1604 | │ |
| 1605 | └─── LA_pack::divide_matrix() |
| 1606 | │ |
| 1607 | └─── LA_pack::gelsy<T>() |
| 1608 | │ |
| 1609 | └─── LA_pack::scaled_gelsy() |
| 1610 | │ |
| 1611 | ├─── LA_pack::laqp2<T>() |
| 1612 | │ │ |
| 1613 | │ ├─── LA_pack::larfg<T>() |
| 1614 | │ └─── LA_pack::larf<T>() |
| 1615 | │ |
| 1616 | ├─── LA_pack::estimate_rank() |
| 1617 | │ |
| 1618 | ├─── LA_pack::trsm<T>() |
| 1619 | │ |
| 1620 | └─── LA_pack::unm2r<T>() |
| 1621 | │ |
| 1622 | ├─── LA_pack::larf<T>() |
| 1623 | ├─── ... |
| 1624 | └─── LA_pack::larf<T>()</code> |
| 1625 | |
| 1626 | </pre> |
| 1627 | </div></div> |
| 1628 | <div class="paragraph"><p>In this call tree:</p></div> |
| 1629 | <div class="ulist"><ul> |
| 1630 | <li> |
| 1631 | <p> |
| 1632 | <strong>eval_AB()</strong> performs the APL argument checking (matching shapes of <strong>A</strong> |
| 1633 | and <strong>B</strong>) and determines if <strong>A</strong> or <strong>B</strong> contain complex items. It also converts |
| 1634 | scalars and vectors <strong>A</strong> and <strong>B</strong> into 1×1 and 1×N matrices respectively; |
| 1635 | </p> |
| 1636 | </li> |
| 1637 | <li> |
| 1638 | <p> |
| 1639 | <strong>divide_matrix()</strong> translates the ravels of APL matrices <strong>A</strong> and <strong>B</strong> into |
| 1640 | (transposed) FORTRAN matrices; |
| 1641 | </p> |
| 1642 | </li> |
| 1643 | <li> |
| 1644 | <p> |
| 1645 | <strong>gelsy<T>()</strong> scales (and later unscales) FORTRAN matrices <strong>A</strong> and <strong>B</strong> with |
| 1646 | very small or very large items so that subsequent operations are |
| 1647 | safe in regard to rounding errors. In almost all real life cases |
| 1648 | <strong>A</strong> and <strong>B</strong> will not need any scaling. |
| 1649 | </p> |
| 1650 | </li> |
| 1651 | <li> |
| 1652 | <p> |
| 1653 | <strong>scaled_gelsy()</strong> computes <strong>Z←A⌹B</strong> for the now properly scaled <strong>A</strong> and <strong>B</strong> |
| 1654 | </p> |
| 1655 | </li> |
| 1656 | <li> |
| 1657 | <p> |
| 1658 | <strong>laqp2()</strong> computes a QR factorization <strong>B=Q∘R</strong> of <strong>B</strong>. In the result: |
| 1659 | </p> |
| 1660 | <div class="ulist"><ul> |
| 1661 | <li> |
| 1662 | <p> |
| 1663 | <strong>Q</strong> is orthogonal (for real <strong>A</strong> and <strong>B</strong>) or Hermitian (for complex |
| 1664 | <strong>A</strong> or <strong>B</strong>). <strong>Q</strong> is easy to invert since <strong>Q⁻¹ ←→ ⍉+Q</strong>, and |
| 1665 | </p> |
| 1666 | </li> |
| 1667 | <li> |
| 1668 | <p> |
| 1669 | <strong>R</strong> is upper triangular (so that <strong>R∘X=C</strong> is simple to solve). |
| 1670 | </p> |
| 1671 | </li> |
| 1672 | </ul></div> |
| 1673 | </li> |
| 1674 | <li> |
| 1675 | <p> |
| 1676 | In the QR factorization: for every column of <strong>B</strong>: |
| 1677 | </p> |
| 1678 | <div class="ulist"><ul> |
| 1679 | <li> |
| 1680 | <p> |
| 1681 | <strong>larfg<T>()</strong> generates one reflector (i.e. for one column of <strong>B</strong>) |
| 1682 | </p> |
| 1683 | </li> |
| 1684 | <li> |
| 1685 | <p> |
| 1686 | <strong>larf<T>()</strong> applies that reflector to a sub-matrix of <strong>B</strong>; |
| 1687 | </p> |
| 1688 | </li> |
| 1689 | </ul></div> |
| 1690 | </li> |
| 1691 | <li> |
| 1692 | <p> |
| 1693 | <strong>estimate_rank()</strong> checks that the system <strong>A=B∘X</strong> of linear equations is |
| 1694 | not under-determined and raises a DOMAIN ERROR if it is. In contrast, |
| 1695 | over-determination (more equations than variables) is accepted and |
| 1696 | produces a least-square solution for <strong>A=X=B∘X</strong> |
| 1697 | </p> |
| 1698 | </li> |
| 1699 | <li> |
| 1700 | <p> |
| 1701 | <strong>trsm<T>()</strong> computes <strong>R⁻¹</strong> for the upper triangular factor <strong>R</strong> |
| 1702 | of <strong>B=Q∘R</strong> that was computed above |
| 1703 | </p> |
| 1704 | </li> |
| 1705 | <li> |
| 1706 | <p> |
| 1707 | <strong>unm2r<T>()</strong> applies <strong>R⁻¹</strong> to <strong>A</strong> |
| 1708 | </p> |
| 1709 | </li> |
| 1710 | </ul></div> |
| 1711 | </div> |
| 1712 | </div> |
| 1713 | </div> |
| 1714 | <div class="sect1"> |
| 1715 | <h2 id="_with_axis_qr_factorization">⌹ with axis: QR factorization</h2> |
| 1716 | <div class="sectionbody"> |
| 1717 | <div class="paragraph"><p>The first non-trivial computation in monadic <strong>⌹B</strong> and in dyadic <strong>A⌹B</strong> |
| 1718 | is a <em>QR-factorization</em> of matrix <strong>B</strong>. A QR factorization of a matrix B is |
| 1719 | a pair <strong>(Q R)</strong> of matrices with the following properties:</p></div> |
| 1720 | <div class="olist arabic"><ol class="arabic"> |
| 1721 | <li> |
| 1722 | <p> |
| 1723 | <strong>Q</strong> is orthogonal (Hermitian if B is complex), |
| 1724 | </p> |
| 1725 | </li> |
| 1726 | <li> |
| 1727 | <p> |
| 1728 | <strong>R</strong> is upper triangular, and |
| 1729 | </p> |
| 1730 | </li> |
| 1731 | <li> |
| 1732 | <p> |
| 1733 | <strong>B ≡ Q ∘ R</strong> within the limits of ⎕CT. |
| 1734 | </p> |
| 1735 | </li> |
| 1736 | </ol></div> |
| 1737 | <div class="paragraph"><p>The same matrix <strong>B</strong> may have different QR factorizations with the above |
| 1738 | properties. And different factorization algorithms may produce different |
| 1739 | pairs <strong>(Q R)</strong> for the same matrix <strong>B</strong>.</p></div> |
| 1740 | <div class="paragraph"><p>Often the orthogonal <strong>Q</strong> and/or the upper triangular <strong>R</strong> of a factorization |
| 1741 | <strong>B=Q∘R</strong> is more valuable than the final result <strong>B⁻¹=R⁻¹∘Q⁻¹</strong> of <strong>⌹B</strong> or |
| 1742 | <strong>A⌹B</strong>. For this reason GNU APL provides <strong>⌹[X]</strong> which returns the matrices |
| 1743 | <strong>Q</strong> and <strong>R</strong> before applying them (i.e. to solve <strong>A∘X=B</strong>). As a matter of |
| 1744 | convenience for the programmer, GNU APL not only computes the pair <strong>(Q R)</strong>, |
| 1745 | but a triple <strong>(Q R Rinv)</strong> where <strong>Rinv ≡ ⌹R</strong>.</p></div> |
| 1746 | <div class="paragraph"><p>One could, of course, compute <strong>Rinv</strong> from <strong>R</strong> in APL with <strong>Rinv←⌹R</strong>. |
| 1747 | However, <strong>⌹R</strong> in APL cannot easily take advantage of the fact that <strong>⌹R</strong> is |
| 1748 | upper triangular, which makes it more efficient to use an inversion algorithm |
| 1749 | that is tailored to the upper triangular case of <strong>⌹R</strong> as opposed to the |
| 1750 | general <strong>B</strong> case. Also, computing <strong>Rinv←⌹R</strong> after returning <strong>(Q R)</strong> would |
| 1751 | factorize R a second time for no reason.</p></div> |
| 1752 | <div class="paragraph"><p>GNU APL has implemented two different algorithms for the QR-factorization |
| 1753 | of a matrix <strong>B</strong>, which produce slightly different results.</p></div> |
| 1754 | <div class="olist arabic"><ol class="arabic"> |
| 1755 | <li> |
| 1756 | <p> |
| 1757 | The first algorithm is based on a factorization algorithm published by |
| 1758 | <strong>Garry Helzer</strong> in <strong>APL Quote Quad, Volume 9, Issue 3, 1979</strong>, and |
| 1759 | </p> |
| 1760 | </li> |
| 1761 | <li> |
| 1762 | <p> |
| 1763 | the second algorithm is, like monadic and dyadic <strong>⌹</strong>, based on LApack |
| 1764 | function <strong>laqp2()</strong> but with column pivoting disabled. |
| 1765 | </p> |
| 1766 | </li> |
| 1767 | </ol></div> |
| 1768 | <div class="paragraph"><p>The algorithm to be used is selected with axis argument <strong>X</strong> as follows:</p></div> |
| 1769 | <div class="olist arabic"><ol class="arabic"> |
| 1770 | <li> |
| 1771 | <p> |
| 1772 | Integer scalar 1 selects Garry Helzer’s algorithm, |
| 1773 | </p> |
| 1774 | </li> |
| 1775 | <li> |
| 1776 | <p> |
| 1777 | Integer scalar 2 selects the <strong>laqp2()</strong> algorithm, and |
| 1778 | </p> |
| 1779 | </li> |
| 1780 | <li> |
| 1781 | <p> |
| 1782 | (obsolete) a small real scalar like ⎕CT also selects Garry Helzer’s |
| 1783 | algorithm. This is for backward compatibility with older GNU APL |
| 1784 | versions (that always used Garry Helzer’s algorithm). |
| 1785 | </p> |
| 1786 | </li> |
| 1787 | </ol></div> |
| 1788 | <div class="sect2"> |
| 1789 | <h3 id="_quadratic_b">Quadratic B</h3> |
| 1790 | <div class="paragraph"><p>The simplest cases are those where <strong>B</strong> is quadratic, i.e. <strong>⍴B ←→ N N</strong>. In |
| 1791 | those cases <strong>Q</strong>, <strong>R</strong>, and <strong>Ri</strong> have the same shape as <strong>B</strong>. The same <strong>B</strong> can |
| 1792 | have different QR factorizations, therefore the ravels of the results |
| 1793 | usually differ between different algorithms. For example,</p></div> |
| 1794 | <div class="listingblock"> |
| 1795 | <div class="content"> |
| 1796 | <pre> |
| 1797 | |
| 1798 | <code> MN←4 4 ◊ B←?MN⍴9 ⍝ B quadratic (M=N) |
| 1799 | Q_R_Ri←(Q R Ri)← ⌹[1]B ⍝ Helzer algorithm |
| 1800 | ⍝ 8 ⎕CR¨(⊂B), 2⍕¨Q R Ri ⍝ Argument B and results Q R Ri |
| 1801 | ⍝ or alternatively from the nested variable |
| 1802 | 8 ⎕CR¨(⊂B), 2⍕¨Q_R_Ri ⍝ Argument B and results Q R Ri |
| 1803 | ┌→──────┐ ┌→──────────────────┐ ┌→──────────────────────┐ ┌→──────────────────┐ |
| 1804 | ↓4 4 9 5│ ↓ .34 .19 ¯.68 .62│ ↓ 11.70 9.23 11.36 13.50│ ↓ .09 ¯.17 .17 .59│ |
| 1805 | │6 1 6 7│ │ .51 ¯.82 .20 .18│ │ .00 4.57 ¯2.16 ¯1.00│ │ .00 .22 ¯.06 ¯.05│ |
| 1806 | │7 8 1 6│ │ .60 .54 .57 .14│ │ .00 .00 ¯8.08 ¯2.29│ │ .00 .00 ¯.12 .18│ |
| 1807 | │6 5 9 9│ │ .51 .06 ¯.41 ¯.75│ │ .00 .00 .00 ¯1.60│ │ .00 .00 .00 ¯.62│ |
| 1808 | └───────┘ └───────────────────┘ └───────────────────────┘ └───────────────────┘ |
| 1809 | |
| 1810 | 8 ⎕CR¨ ⍴¨B Q R Ri ⍝ Shapes of argument B and results |
| 1811 | ┌→──┐ ┌→──┐ ┌→──┐ ┌→──┐ |
| 1812 | │4 4│ │4 4│ │4 4│ │4 4│ |
| 1813 | └───┘ └───┘ └───┘ └───┘ |
| 1814 | |
| 1815 | 8 ⎕CR B=Q∘R ⍝ Q R is a factorization of B |
| 1816 | ┌→──────┐ |
| 1817 | ↓1 1 1 1│ |
| 1818 | │1 1 1 1│ |
| 1819 | │1 1 1 1│ |
| 1820 | │1 1 1 1│ |
| 1821 | └───────┘ |
| 1822 | |
| 1823 | 8 ⎕CR¨ (Q∘⍉Q) ((⍉Q)∘Q) ⍝ Q is orthogonal |
| 1824 | ┌→──────┐ ┌→──────┐ |
| 1825 | ↓1 0 0 0│ ↓1 0 0 0│ |
| 1826 | │0 1 0 0│ │0 1 0 0│ |
| 1827 | │0 0 1 0│ │0 0 1 0│ |
| 1828 | │0 0 0 1│ │0 0 0 1│ |
| 1829 | └───────┘ └───────┘ |
| 1830 | |
| 1831 | 8 ⎕CR¨ (R∘Ri) (Ri∘R) ⍝ Ri is the inverse of R |
| 1832 | ┌→──────┐ ┌→──────┐ |
| 1833 | ↓1 0 0 0│ ↓1 0 0 0│ |
| 1834 | │0 1 0 0│ │0 1 0 0│ |
| 1835 | │0 0 1 0│ │0 0 1 0│ |
| 1836 | │0 0 0 1│ │0 0 0 1│ |
| 1837 | └───────┘ └───────┘ |
| 1838 | |
| 1839 | |
| 1840 | MN←4 4 ◊ B←?MN⍴9 ⍝ B quadratic (M=N) |
| 1841 | Q_R_Ri←(Q R Ri) ← ⌹[2]B ⍝ LApack algorithm |
| 1842 | 8 ⎕CR¨(⊂B), 2⍕¨Q_R_Ri ⍝ Argument B and results Q R Ri |
| 1843 | ┌→──────┐ ┌→───────────────────┐ ┌→─────────────────────────┐ ┌→───────────────────┐ |
| 1844 | ↓2 5 5 7│ ↓ ¯.22 .41 .64 .61│ ↓ ¯9.22 ¯12.91 ¯5.10 ¯11.50│ ↓ ¯.11 ¯.26 .16 .22│ |
| 1845 | │8 9 2 8│ │ ¯.87 ¯.41 ¯.21 .18│ │ .00 5.33 4.17 7.24│ │ .00 .19 ¯.23 ¯.43│ |
| 1846 | │4 8 5 5│ │ ¯.43 .45 .27 ¯.73│ │ .00 .00 3.41 ¯1.40│ │ .00 .00 .29 .10│ |
| 1847 | │1 5 1 8│ │ ¯.11 .68 ¯.69 .23│ │ .00 .00 .00 3.92│ │ .00 .00 .00 .25│ |
| 1848 | └───────┘ └────────────────────┘ └──────────────────────────┘ └────────────────────┘ |
| 1849 | |
| 1850 | 8 ⎕CR¨ ⍴¨B Q R Ri ⍝ Shapes of argument B and results |
| 1851 | ┌→──┐ ┌→──┐ ┌→──┐ ┌→──┐ |
| 1852 | │4 4│ │4 4│ │4 4│ │4 4│ |
| 1853 | └───┘ └───┘ └───┘ └───┘ |
| 1854 | |
| 1855 | 8 ⎕CR B=Q∘R ⍝ Q R is a factorization of B |
| 1856 | ┌→──────┐ |
| 1857 | ↓1 1 1 1│ |
| 1858 | │1 1 1 1│ |
| 1859 | │1 1 1 1│ |
| 1860 | │1 1 1 1│ |
| 1861 | └───────┘ |
| 1862 | |
| 1863 | 8 ⎕CR¨ (Q∘⍉Q) ((⍉Q)∘Q) ⍝ Q is orthogonal |
| 1864 | ┌→──────┐ ┌→──────┐ |
| 1865 | ↓1 0 0 0│ ↓1 0 0 0│ |
| 1866 | │0 1 0 0│ │0 1 0 0│ |
| 1867 | │0 0 1 0│ │0 0 1 0│ |
| 1868 | │0 0 0 1│ │0 0 0 1│ |
| 1869 | └───────┘ └───────┘ |
| 1870 | |
| 1871 | 8 ⎕CR¨ (R∘Ri) (Ri∘R) ⍝ Ri is the inverse of R |
| 1872 | ┌→──────┐ ┌→──────┐ |
| 1873 | ↓1 0 0 0│ ↓1 0 0 0│ |
| 1874 | │0 1 0 0│ │0 1 0 0│ |
| 1875 | │0 0 1 0│ │0 0 1 0│ |
| 1876 | │0 0 0 1│ │0 0 0 1│ |
| 1877 | └───────┘ └───────┘</code> |
| 1878 | |
| 1879 | </pre> |
| 1880 | </div></div> |
| 1881 | <div class="paragraph"><p>Notice the differences between the Helzer and the LAack algorithm |
| 1882 | in the <strong>8 ⎕CR¨2⍕¨Q R Ri</strong> lines above.</p></div> |
| 1883 | </div> |
| 1884 | <div class="sect2"> |
| 1885 | <h3 id="_under_determined_b">Under-determined B</h3> |
| 1886 | <div class="paragraph"><p>If <strong>B</strong> is under-determined, i.e. <strong>⍴B ←→ M N</strong> with <strong>M < N</strong>, |
| 1887 | then matters become a little more complicated. Due to conformity |
| 1888 | constraints (LENGTH ERROR), only <strong>(MM←⍴Ri)↑R</strong> can be multiplied |
| 1889 | from the left or from the right, but not <strong>R</strong> itself:</p></div> |
| 1890 | <div class="listingblock"> |
| 1891 | <div class="content"> |
| 1892 | <pre> |
| 1893 | |
| 1894 | <code> ⎕LC |
| 1895 | MM←2/↑(M N)←MN←2 4 ◊ B←?MN⍴9 ⍝ Helzer, B under-determined (M<N) |
| 1896 | Q_R_Ri←(Q R Ri) ← ⌹[1]B ⍝ Helzer algorithm |
| 1897 | |
| 1898 | 8 ⎕CR¨(⊂B), 2⍕¨Q_R_Ri ⍝ Argument B and results Q R Ri |
| 1899 | ┌→──────┐ ┌→────────┐ ┌→────────────────────┐ ┌→─────────┐ |
| 1900 | ↓2 5 3 5│ ↓ .89 .45│ ↓ 2.24 5.81 4.02 4.92│ ↓ .45 5.81│ |
| 1901 | │1 3 3 1│ │ .45 ¯.89│ │ .00 ¯.45 ¯1.34 1.34│ │ .00 ¯2.24│ |
| 1902 | └───────┘ └─────────┘ └─────────────────────┘ └──────────┘ |
| 1903 | |
| 1904 | 8 ⎕CR¨ ⍴¨B Q R Ri ⍝ Shapes of argument B and results |
| 1905 | ┌→──┐ ┌→──┐ ┌→──┐ ┌→──┐ |
| 1906 | │2 4│ │2 2│ │2 4│ │2 2│ |
| 1907 | └───┘ └───┘ └───┘ └───┘ |
| 1908 | |
| 1909 | 8 ⎕CR B=Q∘R ⍝ Q R is a factorization of B |
| 1910 | ┌→──────┐ |
| 1911 | ↓1 1 1 1│ |
| 1912 | │1 1 1 1│ |
| 1913 | └───────┘ |
| 1914 | |
| 1915 | 8 ⎕CR¨ (Q∘⍉Q) ((⍉Q)∘Q) ⍝ Q is orthogonal |
| 1916 | ┌→──┐ ┌→──┐ |
| 1917 | ↓1 0│ ↓1 0│ |
| 1918 | │0 1│ │0 1│ |
| 1919 | └───┘ └───┘ |
| 1920 | |
| 1921 | 8 ⎕CR¨ ((MM↑R)∘Ri) (Ri∘(MM↑R)) ⍝ Ri is the inverse of R |
| 1922 | ┌→──┐ ┌→──┐ |
| 1923 | ↓1 0│ ↓1 0│ |
| 1924 | │0 1│ │0 1│ |
| 1925 | └───┘ └───┘</code> |
| 1926 | |
| 1927 | </pre> |
| 1928 | </div></div> |
| 1929 | </div> |
| 1930 | <div class="sect2"> |
| 1931 | <h3 id="_over_determined_b">Over-determined B</h3> |
| 1932 | <div class="paragraph"><p>The most complicated cases are those where <strong>B</strong> is over-determined, |
| 1933 | i.e. <strong>⍴B ←→ M N</strong> with <strong>M > N</strong>. The conformity constraints are similar |
| 1934 | to those of the under-determined cases.</p></div> |
| 1935 | <div class="listingblock"> |
| 1936 | <div class="content"> |
| 1937 | <pre> |
| 1938 | |
| 1939 | <code> NN←2/1↓(M N)←MN←4 2 ◊ B←?MN⍴9 ⍝ B over-determined (M>N) |
| 1940 | Q_R_Ri←(Q R Ri) ← ⌹[1]B ⍝ Helzer algorithm |
| 1941 | |
| 1942 | 8 ⎕CR¨(⊂B), 2⍕¨Q R Ri ⍝ Argument B and results Q R Ri |
| 1943 | ┌→──┐ ┌→──────────────────┐ ┌→───────────┐ ┌→────────┐ |
| 1944 | ↓9 3│ ↓ .65 .31 ¯.54 .44│ ↓ 13.82 6.51│ ↓ .07 .12│ |
| 1945 | │7 3│ │ .51 .08 ¯.04 ¯.86│ │ .00 ¯3.95│ │ .00 ¯.25│ |
| 1946 | │6 2│ │ .43 .21 .84 .24│ │ .00 .00│ └─────────┘ |
| 1947 | │5 6│ │ .36 ¯.92 .01 .13│ │ .00 .00│ |
| 1948 | └───┘ └───────────────────┘ └────────────┘ |
| 1949 | |
| 1950 | 8 ⎕CR¨ ⍴¨B Q R Ri ⍝ Shapes of argument B and results |
| 1951 | ┌→──┐ ┌→──┐ ┌→──┐ ┌→──┐ |
| 1952 | │4 2│ │4 4│ │4 2│ │2 2│ |
| 1953 | └───┘ └───┘ └───┘ └───┘ |
| 1954 | |
| 1955 | 8 ⎕CR B=Q∘R ⍝ Q R is a factorization of B |
| 1956 | ┌→──┐ |
| 1957 | ↓1 1│ |
| 1958 | │1 1│ |
| 1959 | │1 1│ |
| 1960 | │1 1│ |
| 1961 | └───┘ |
| 1962 | |
| 1963 | 8 ⎕CR¨ (Q∘⍉Q) ((⍉Q)∘Q) ⍝ Q is orthogonal |
| 1964 | ┌→──────┐ ┌→──────┐ |
| 1965 | ↓1 0 0 0│ ↓1 0 0 0│ |
| 1966 | │0 1 0 0│ │0 1 0 0│ |
| 1967 | │0 0 1 0│ │0 0 1 0│ |
| 1968 | │0 0 0 1│ │0 0 0 1│ |
| 1969 | └───────┘ └───────┘ |
| 1970 | |
| 1971 | 8 ⎕CR¨ ((NN↑R)∘Ri) (Ri∘(NN↑R)) ⍝ Ri is the inverse of R |
| 1972 | ┌→──┐ ┌→──┐ |
| 1973 | ↓1 0│ ↓1 0│ |
| 1974 | │0 1│ │0 1│ |
| 1975 | └───┘ └───┘ |
| 1976 | |
| 1977 | |
| 1978 | NN←2/1↓(M N)←MN←4 2 ◊ B←?MN⍴9 ⍝ B over-determined (M>N) |
| 1979 | Q_R_Ri←(Q R Ri) ← ⌹[2]B ⍝ LApack algorithm |
| 1980 | |
| 1981 | 8 ⎕CR¨(⊂B), 2⍕¨Q_R_Ri ⍝ Argument B and results Q R Ri |
| 1982 | ┌→──┐ ┌→─────────┐ ┌→────────────┐ ┌→─────────┐ |
| 1983 | ↓5 3│ ↓ ¯.48 .09│ ↓ ¯10.34 ¯5.51│ ↓ ¯.10 ¯.14│ |
| 1984 | │8 3│ │ ¯.77 ¯.34│ │ .00 3.69│ │ .00 .27│ |
| 1985 | │3 1│ │ ¯.29 ¯.16│ └─────────────┘ └──────────┘ |
| 1986 | │3 5│ │ ¯.29 .92│ |
| 1987 | └───┘ └──────────┘ |
| 1988 | |
| 1989 | 8 ⎕CR¨ ⍴¨B Q R Ri ⍝ Shapes of argument B and results |
| 1990 | ┌→──┐ ┌→──┐ ┌→──┐ ┌→──┐ |
| 1991 | │4 2│ │4 2│ │2 2│ │2 2│ |
| 1992 | └───┘ └───┘ └───┘ └───┘ |
| 1993 | 8 ⎕CR B=Q∘R ⍝ Q R is a factorization of B |
| 1994 | ┌→──┐ |
| 1995 | ↓1 1│ |
| 1996 | │1 1│ |
| 1997 | │1 1│ |
| 1998 | │1 1│ |
| 1999 | └───┘ |
| 2000 | |
| 2001 | 8 ⎕CR¨ (2⍕Q∘⍉Q) ((⍉Q)∘Q) ⍝ Q is orthogonal ? |
| 2002 | ┌→──────────────────┐ ┌→──┐ |
| 2003 | ↓ .24 .34 .13 .22│ ↓1 0│ |
| 2004 | │ .34 .71 .28 ¯.09│ │0 1│ |
| 2005 | │ .13 .28 .11 ¯.07│ └───┘ |
| 2006 | │ .22 ¯.09 ¯.07 .93│ |
| 2007 | └───────────────────┘ |
| 2008 | |
| 2009 | 8 ⎕CR¨ ((NN↑R)∘Ri) (Ri∘(NN↑R)) ⍝ Ri is the inverse of NN↑R |
| 2010 | ┌→──┐ ┌→──┐ |
| 2011 | ↓1 0│ ↓1 0│ |
| 2012 | │0 1│ │0 1│ |
| 2013 | └───┘ └───┘</code> |
| 2014 | |
| 2015 | </pre> |
| 2016 | </div></div> |
| 2017 | <div class="paragraph"><p>Note that above <strong>Q</strong> is orthogonal in the Helzer case but not in the LApack |
| 2018 | case. This is most likely caused by the fact that in an over-determined <strong>B</strong> |
| 2019 | the LApack functions compute a least-square solution instead of an exact one. |
| 2020 | Note also the different <strong>⍴Q</strong> in both cases.</p></div> |
| 2021 | </div> |
| 2022 | </div> |
| 2023 | </div> |
| 2024 | <div class="sect1"> |
| 2025 | <h2 id="_impact_of_ct">Impact of ⎕CT</h2> |
| 2026 | <div class="sectionbody"> |
| 2027 | <div class="paragraph"><p>All ⌹-functions use <strong>⎕CT</strong> although in different ways.</p></div> |
| 2028 | <div class="sect2"> |
| 2029 | <h3 id="_ct_in_the_helzer_algorithm">⎕CT in the Helzer Algorithm</h3> |
| 2030 | <div class="paragraph"><p>To quote Garry Helzer:</p></div> |
| 2031 | <div class="paragraph"><p><em>For this reason, whenever it is necessary to check an array for zeros below, |
| 2032 | the check is always made relative to another array and with a specified |
| 2033 | tolerance.</em></p></div> |
| 2034 | <div class="paragraph"><p>In GNU APL this comparison of small items with exact 0.0 can be |
| 2035 | controlled with ⎕CT. The original Helzer algorithm used two APL functions |
| 2036 | <strong>CPR</strong> (for compare) and <strong>TOL</strong> (tolerance) to check for zeros:</p></div> |
| 2037 | <div class="listingblock"> |
| 2038 | <div class="content"> |
| 2039 | <pre> |
| 2040 | |
| 2041 | <code> O ^.= (I+V) CPR L TOL DCT</code> |
| 2042 | |
| 2043 | </pre> |
| 2044 | </div></div> |
| 2045 | <div class="paragraph"><p>which have been inlined in GNU APL, see <strong>Bif_F12_DOMINO::significant()</strong>.</p></div> |
| 2046 | <div class="admonitionblock"> |
| 2047 | <table><tr> |
| 2048 | <td class="icon"> |
| 2049 | <img src="/usr/share/asciidoc/icons/note.png" alt="Note" /> |
| 2050 | </td> |
| 2051 | <td class="content">the Helzer Algorithm in GNU APL, i.e. <strong>⌹[1]B</strong>, does not use any |
| 2052 | of the LApack functions.</td> |
| 2053 | </tr></table> |
| 2054 | </div> |
| 2055 | </div> |
| 2056 | <div class="sect2"> |
| 2057 | <h3 id="_ct_in_the_lapack_functions">⎕CT in the LApack functions</h3> |
| 2058 | <div class="paragraph"><p>The LApack based functions <strong>A⌹B</strong> and <strong>⌹B</strong> in GNU APL use the C++ function |
| 2059 | <strong>LApack::estimate_rank()</strong> to compute the effective rank of the right |
| 2060 | argument <strong>B</strong>. Matrices with fewer rows than columns are <em>under-determined</em> |
| 2061 | and always yield a RANK ERROR. However a matrix can be under-determined even |
| 2062 | if the number of rows is suffiently large. This is the case if the rows are |
| 2063 | not linearly independent. Put differently, a large number of rows alone |
| 2064 | does not guarantee a sufficient matrix rank because a linear dependent row |
| 2065 | is as good as no row.</p></div> |
| 2066 | <div class="paragraph"><p>Function <strong>estimate_rank()</strong> has no corresponding FORTRAN function in LApack, |
| 2067 | but used to be a somewhat lengthy section of <strong>LApack::gelsy()</strong>. That section |
| 2068 | was modularized into <strong>estimate_rank()</strong> in order to improve the readability |
| 2069 | of the GNU APL C++ code.</p></div> |
| 2070 | <div class="paragraph"><p>Function <strong>estimate_rank()</strong> iteratively computes two values <strong>smin</strong> (an upper |
| 2071 | bound for the smallest singular value of <strong>B</strong>) and <strong>smax</strong> (a lower bound for |
| 2072 | the largest singular value of <strong>B</strong>), and considers matrix <strong>B</strong> as rank-deficient |
| 2073 | if:</p></div> |
| 2074 | <div class="listingblock"> |
| 2075 | <div class="content"> |
| 2076 | <pre> |
| 2077 | |
| 2078 | <code> smax * ⎕CT > smin</code> |
| 2079 | |
| 2080 | </pre> |
| 2081 | </div></div> |
| 2082 | <div class="paragraph"><p>If that happens then GNU APL raises a <strong>DOMAIN ERROR</strong>. It means that the |
| 2083 | matrix <strong>B</strong> (i.e. of <strong>A⌹B</strong>, <strong>⌹B</strong>) is either truly rank-deficient, |
| 2084 | or at least close to a rank-deficient matrix. Decreasing <strong>⎕CT</strong> may |
| 2085 | or may not solve that problem.</p></div> |
| 2086 | <div class="paragraph"><p>In such cases the precision of the result is rather questionable and |
| 2087 | the DOMAIN ERROR indicates that problem rather than continuing with |
| 2088 | the computation and producing an unreliable result.</p></div> |
| 2089 | </div> |
| 2090 | </div> |
| 2091 | </div> |
| 2092 | </div> |
| 2093 | <div id="footnotes"><hr /></div> |
| 2094 | <div id="footer"> |
| 2095 | <div id="footer-text"> |
| 2096 | Last updated |
| 2097 | 2025-01-23 17:08:55 CET |
| 2098 | </div> |
| 2099 | </div> |
| 2100 | </body> |
| 2101 | </html> |