From 065331c18dc51e011e2d8c22dde05076ab679b87 Mon Sep 17 00:00:00 2001 From: mhjensen Date: Sat, 27 Jan 2018 09:55:46 -0500 Subject: [PATCH] Update some material --- doc/pub/Linalg/html/Linalg-bs.html | 1567 ++++++ doc/pub/Linalg/html/Linalg-reveal.html | 1716 +++++++ doc/pub/Linalg/html/Linalg-solarized.html | 1473 ++++++ doc/pub/Linalg/html/Linalg.html | 1478 ++++++ doc/pub/Linalg/html/reveal.js/.gitignore | 8 + doc/pub/Linalg/html/reveal.js/.travis.yml | 5 + doc/pub/Linalg/html/reveal.js/CONTRIBUTING.md | 23 + doc/pub/Linalg/html/reveal.js/Gruntfile.js | 140 + doc/pub/Linalg/html/reveal.js/LICENSE | 19 + doc/pub/Linalg/html/reveal.js/README.md | 1052 ++++ doc/pub/Linalg/html/reveal.js/bower.json | 27 + .../html/reveal.js/css/images/cbc_footer.png | Bin 0 -> 10008 bytes .../html/reveal.js/css/images/cbc_symbol.png | Bin 0 -> 2946 bytes .../reveal.js/css/images/simula_footer.png | Bin 0 -> 2513 bytes .../html/reveal.js/css/images/simula_logo.png | Bin 0 -> 2138 bytes .../reveal.js/css/images/simula_symbol.png | Bin 0 -> 2138 bytes .../html/reveal.js/css/images/uio_footer.png | Bin 0 -> 18189 bytes .../html/reveal.js/css/images/uio_symbol.png | Bin 0 -> 11352 bytes .../Linalg/html/reveal.js/css/print/paper.css | 202 + .../Linalg/html/reveal.js/css/print/pdf.css | 157 + doc/pub/Linalg/html/reveal.js/css/reveal.css | 1886 +++++++ doc/pub/Linalg/html/reveal.js/css/reveal.scss | 1319 +++++ .../Linalg/html/reveal.js/css/theme/README.md | 23 + .../Linalg/html/reveal.js/css/theme/beige.css | 154 + .../html/reveal.js/css/theme/beigesmall.css | 155 + .../Linalg/html/reveal.js/css/theme/black.css | 273 + .../Linalg/html/reveal.js/css/theme/blood.css | 180 + .../Linalg/html/reveal.js/css/theme/cbc.css | 144 + .../html/reveal.js/css/theme/darkgray.css | 153 + .../html/reveal.js/css/theme/default.css | 153 + .../html/reveal.js/css/theme/league.css | 279 + .../Linalg/html/reveal.js/css/theme/moon.css | 153 + .../Linalg/html/reveal.js/css/theme/night.css | 141 + .../Linalg/html/reveal.js/css/theme/serif.css | 143 + .../html/reveal.js/css/theme/simple.css | 144 + .../html/reveal.js/css/theme/simula.css | 144 + .../Linalg/html/reveal.js/css/theme/sky.css | 150 + .../html/reveal.js/css/theme/solarized.css | 153 + .../reveal.js/css/theme/source/beige.scss | 50 + .../css/theme/source/beigesmall.scss | 51 + .../reveal.js/css/theme/source/black.scss | 49 + .../reveal.js/css/theme/source/blood.scss | 91 + .../html/reveal.js/css/theme/source/cbc.scss | 39 + .../reveal.js/css/theme/source/darkgray.scss | 42 + .../reveal.js/css/theme/source/default.scss | 42 + .../reveal.js/css/theme/source/league.scss | 34 + .../html/reveal.js/css/theme/source/moon.scss | 68 + .../reveal.js/css/theme/source/night.scss | 35 + .../reveal.js/css/theme/source/serif.scss | 35 + .../reveal.js/css/theme/source/simple.scss | 38 + .../reveal.js/css/theme/source/simula.scss | 39 + .../html/reveal.js/css/theme/source/sky.scss | 46 + .../reveal.js/css/theme/source/solarized.scss | 74 + .../reveal.js/css/theme/source/white.scss | 49 + .../Linalg/html/reveal.js/css/theme/white.css | 273 + doc/pub/Linalg/html/reveal.js/index.html | 411 ++ doc/pub/Linalg/html/reveal.js/js/reveal.js | 4508 +++++++++++++++++ .../Linalg/html/reveal.js/lib/css/zenburn.css | 117 + .../reveal.js/lib/font/league-gothic/LICENSE | 2 + .../lib/font/league-gothic/league-gothic.css | 10 + .../lib/font/league-gothic/league-gothic.eot | Bin 0 -> 25696 bytes .../lib/font/league-gothic/league-gothic.ttf | Bin 0 -> 64256 bytes .../lib/font/league-gothic/league-gothic.woff | Bin 0 -> 30764 bytes .../lib/font/source-sans-pro/LICENSE | 45 + .../source-sans-pro-italic.eot | Bin 0 -> 75720 bytes .../source-sans-pro-italic.ttf | Bin 0 -> 238084 bytes .../source-sans-pro-italic.woff | Bin 0 -> 98556 bytes .../source-sans-pro-regular.eot | Bin 0 -> 88070 bytes .../source-sans-pro-regular.ttf | Bin 0 -> 288008 bytes .../source-sans-pro-regular.woff | Bin 0 -> 114324 bytes .../source-sans-pro-semibold.eot | Bin 0 -> 89897 bytes .../source-sans-pro-semibold.ttf | Bin 0 -> 284640 bytes .../source-sans-pro-semibold.woff | Bin 0 -> 115648 bytes .../source-sans-pro-semibolditalic.eot | Bin 0 -> 75706 bytes .../source-sans-pro-semibolditalic.ttf | Bin 0 -> 240944 bytes .../source-sans-pro-semibolditalic.woff | Bin 0 -> 98816 bytes .../font/source-sans-pro/source-sans-pro.css | 39 + .../Linalg/html/reveal.js/lib/js/classList.js | 2 + .../Linalg/html/reveal.js/lib/js/head.min.js | 8 + .../Linalg/html/reveal.js/lib/js/html5shiv.js | 7 + doc/pub/Linalg/html/reveal.js/package.json | 45 + .../reveal.js/plugin/highlight/highlight.js | 30 + .../Linalg/html/reveal.js/plugin/leap/leap.js | 159 + .../reveal.js/plugin/markdown/example.html | 129 + .../html/reveal.js/plugin/markdown/example.md | 31 + .../reveal.js/plugin/markdown/markdown.js | 393 ++ .../html/reveal.js/plugin/markdown/marked.js | 6 + .../Linalg/html/reveal.js/plugin/math/math.js | 64 + .../html/reveal.js/plugin/multiplex/client.js | 13 + .../html/reveal.js/plugin/multiplex/index.js | 56 + .../html/reveal.js/plugin/multiplex/master.js | 51 + .../reveal.js/plugin/notes-server/client.js | 60 + .../reveal.js/plugin/notes-server/index.js | 66 + .../reveal.js/plugin/notes-server/notes.html | 396 ++ .../html/reveal.js/plugin/notes/notes.html | 406 ++ .../html/reveal.js/plugin/notes/notes.js | 122 + .../reveal.js/plugin/print-pdf/print-pdf.js | 48 + .../html/reveal.js/plugin/remotes/remotes.js | 39 + .../html/reveal.js/plugin/search/search.js | 196 + .../html/reveal.js/plugin/zoom-js/zoom.js | 278 + .../reveal.js/test/examples/assets/image1.png | Bin 0 -> 21991 bytes .../reveal.js/test/examples/assets/image2.png | Bin 0 -> 10237 bytes .../reveal.js/test/examples/barebones.html | 41 + .../test/examples/embedded-media.html | 49 + .../html/reveal.js/test/examples/math.html | 185 + .../test/examples/slide-backgrounds.html | 144 + .../test/examples/slide-transitions.html | 101 + .../html/reveal.js/test/qunit-1.12.0.css | 244 + .../html/reveal.js/test/qunit-1.12.0.js | 2212 ++++++++ .../test-markdown-element-attributes.html | 134 + .../test/test-markdown-element-attributes.js | 46 + .../test/test-markdown-slide-attributes.html | 128 + .../test/test-markdown-slide-attributes.js | 47 + .../html/reveal.js/test/test-markdown.html | 52 + .../html/reveal.js/test/test-markdown.js | 15 + .../Linalg/html/reveal.js/test/test-pdf.html | 83 + .../Linalg/html/reveal.js/test/test-pdf.js | 15 + doc/pub/Linalg/html/reveal.js/test/test.html | 85 + doc/pub/Linalg/html/reveal.js/test/test.js | 589 +++ doc/pub/Linalg/ipynb/Linalg.ipynb | 2029 ++++++++ doc/pub/Linalg/ipynb/ipynb-Linalg-src.tar.gz | Bin 0 -> 209 bytes .../Linalg/pdf/Linalg-beamer-handouts2x3.pdf | Bin 0 -> 373989 bytes doc/pub/Linalg/pdf/Linalg-beamer.pdf | Bin 0 -> 358564 bytes doc/pub/Linalg/pdf/Linalg-minted.pdf | Bin 0 -> 384546 bytes doc/pub/Splines/html/Splines-bs.html | 868 ++++ doc/pub/Splines/html/Splines-reveal.html | 1017 ++++ doc/pub/Splines/html/Splines-solarized.html | 799 +++ doc/pub/Splines/html/Splines.html | 804 +++ doc/pub/Splines/html/reveal.js/.gitignore | 8 + doc/pub/Splines/html/reveal.js/.travis.yml | 5 + .../Splines/html/reveal.js/CONTRIBUTING.md | 23 + doc/pub/Splines/html/reveal.js/Gruntfile.js | 140 + doc/pub/Splines/html/reveal.js/LICENSE | 19 + doc/pub/Splines/html/reveal.js/README.md | 1052 ++++ doc/pub/Splines/html/reveal.js/bower.json | 27 + .../html/reveal.js/css/images/cbc_footer.png | Bin 0 -> 10008 bytes .../html/reveal.js/css/images/cbc_symbol.png | Bin 0 -> 2946 bytes .../reveal.js/css/images/simula_footer.png | Bin 0 -> 2513 bytes .../html/reveal.js/css/images/simula_logo.png | Bin 0 -> 2138 bytes .../reveal.js/css/images/simula_symbol.png | Bin 0 -> 2138 bytes .../html/reveal.js/css/images/uio_footer.png | Bin 0 -> 18189 bytes .../html/reveal.js/css/images/uio_symbol.png | Bin 0 -> 11352 bytes .../html/reveal.js/css/print/paper.css | 202 + .../Splines/html/reveal.js/css/print/pdf.css | 157 + doc/pub/Splines/html/reveal.js/css/reveal.css | 1886 +++++++ .../Splines/html/reveal.js/css/reveal.scss | 1319 +++++ .../html/reveal.js/css/theme/README.md | 23 + .../html/reveal.js/css/theme/beige.css | 154 + .../html/reveal.js/css/theme/beigesmall.css | 155 + .../html/reveal.js/css/theme/black.css | 273 + .../html/reveal.js/css/theme/blood.css | 180 + .../Splines/html/reveal.js/css/theme/cbc.css | 144 + .../html/reveal.js/css/theme/darkgray.css | 153 + .../html/reveal.js/css/theme/default.css | 153 + .../html/reveal.js/css/theme/league.css | 279 + .../Splines/html/reveal.js/css/theme/moon.css | 153 + .../html/reveal.js/css/theme/night.css | 141 + .../html/reveal.js/css/theme/serif.css | 143 + .../html/reveal.js/css/theme/simple.css | 144 + .../html/reveal.js/css/theme/simula.css | 144 + .../Splines/html/reveal.js/css/theme/sky.css | 150 + .../html/reveal.js/css/theme/solarized.css | 153 + .../reveal.js/css/theme/source/beige.scss | 50 + .../css/theme/source/beigesmall.scss | 51 + .../reveal.js/css/theme/source/black.scss | 49 + .../reveal.js/css/theme/source/blood.scss | 91 + .../html/reveal.js/css/theme/source/cbc.scss | 39 + .../reveal.js/css/theme/source/darkgray.scss | 42 + .../reveal.js/css/theme/source/default.scss | 42 + .../reveal.js/css/theme/source/league.scss | 34 + .../html/reveal.js/css/theme/source/moon.scss | 68 + .../reveal.js/css/theme/source/night.scss | 35 + .../reveal.js/css/theme/source/serif.scss | 35 + .../reveal.js/css/theme/source/simple.scss | 38 + .../reveal.js/css/theme/source/simula.scss | 39 + .../html/reveal.js/css/theme/source/sky.scss | 46 + .../reveal.js/css/theme/source/solarized.scss | 74 + .../reveal.js/css/theme/source/white.scss | 49 + .../html/reveal.js/css/theme/white.css | 273 + doc/pub/Splines/html/reveal.js/index.html | 411 ++ doc/pub/Splines/html/reveal.js/js/reveal.js | 4508 +++++++++++++++++ .../html/reveal.js/lib/css/zenburn.css | 117 + .../reveal.js/lib/font/league-gothic/LICENSE | 2 + .../lib/font/league-gothic/league-gothic.css | 10 + .../lib/font/league-gothic/league-gothic.eot | Bin 0 -> 25696 bytes .../lib/font/league-gothic/league-gothic.ttf | Bin 0 -> 64256 bytes .../lib/font/league-gothic/league-gothic.woff | Bin 0 -> 30764 bytes .../lib/font/source-sans-pro/LICENSE | 45 + .../source-sans-pro-italic.eot | Bin 0 -> 75720 bytes .../source-sans-pro-italic.ttf | Bin 0 -> 238084 bytes .../source-sans-pro-italic.woff | Bin 0 -> 98556 bytes .../source-sans-pro-regular.eot | Bin 0 -> 88070 bytes .../source-sans-pro-regular.ttf | Bin 0 -> 288008 bytes .../source-sans-pro-regular.woff | Bin 0 -> 114324 bytes .../source-sans-pro-semibold.eot | Bin 0 -> 89897 bytes .../source-sans-pro-semibold.ttf | Bin 0 -> 284640 bytes .../source-sans-pro-semibold.woff | Bin 0 -> 115648 bytes .../source-sans-pro-semibolditalic.eot | Bin 0 -> 75706 bytes .../source-sans-pro-semibolditalic.ttf | Bin 0 -> 240944 bytes .../source-sans-pro-semibolditalic.woff | Bin 0 -> 98816 bytes .../font/source-sans-pro/source-sans-pro.css | 39 + .../html/reveal.js/lib/js/classList.js | 2 + .../Splines/html/reveal.js/lib/js/head.min.js | 8 + .../html/reveal.js/lib/js/html5shiv.js | 7 + doc/pub/Splines/html/reveal.js/package.json | 45 + .../reveal.js/plugin/highlight/highlight.js | 30 + .../html/reveal.js/plugin/leap/leap.js | 159 + .../reveal.js/plugin/markdown/example.html | 129 + .../html/reveal.js/plugin/markdown/example.md | 31 + .../reveal.js/plugin/markdown/markdown.js | 393 ++ .../html/reveal.js/plugin/markdown/marked.js | 6 + .../html/reveal.js/plugin/math/math.js | 64 + .../html/reveal.js/plugin/multiplex/client.js | 13 + .../html/reveal.js/plugin/multiplex/index.js | 56 + .../html/reveal.js/plugin/multiplex/master.js | 51 + .../reveal.js/plugin/notes-server/client.js | 60 + .../reveal.js/plugin/notes-server/index.js | 66 + .../reveal.js/plugin/notes-server/notes.html | 396 ++ .../html/reveal.js/plugin/notes/notes.html | 406 ++ .../html/reveal.js/plugin/notes/notes.js | 122 + .../reveal.js/plugin/print-pdf/print-pdf.js | 48 + .../html/reveal.js/plugin/remotes/remotes.js | 39 + .../html/reveal.js/plugin/search/search.js | 196 + .../html/reveal.js/plugin/zoom-js/zoom.js | 278 + .../reveal.js/test/examples/assets/image1.png | Bin 0 -> 21991 bytes .../reveal.js/test/examples/assets/image2.png | Bin 0 -> 10237 bytes .../reveal.js/test/examples/barebones.html | 41 + .../test/examples/embedded-media.html | 49 + .../html/reveal.js/test/examples/math.html | 185 + .../test/examples/slide-backgrounds.html | 144 + .../test/examples/slide-transitions.html | 101 + .../html/reveal.js/test/qunit-1.12.0.css | 244 + .../html/reveal.js/test/qunit-1.12.0.js | 2212 ++++++++ .../test-markdown-element-attributes.html | 134 + .../test/test-markdown-element-attributes.js | 46 + .../test/test-markdown-slide-attributes.html | 128 + .../test/test-markdown-slide-attributes.js | 47 + .../html/reveal.js/test/test-markdown.html | 52 + .../html/reveal.js/test/test-markdown.js | 15 + .../Splines/html/reveal.js/test/test-pdf.html | 83 + .../Splines/html/reveal.js/test/test-pdf.js | 15 + doc/pub/Splines/html/reveal.js/test/test.html | 85 + doc/pub/Splines/html/reveal.js/test/test.js | 589 +++ doc/pub/Splines/ipynb/Splines.ipynb | 962 ++++ .../Splines/ipynb/ipynb-Splines-src.tar.gz | Bin 0 -> 211 bytes .../pdf/Splines-beamer-handouts2x3.pdf | Bin 0 -> 229494 bytes doc/pub/Splines/pdf/Splines-beamer.pdf | Bin 0 -> 213088 bytes doc/pub/Splines/pdf/Splines-minted.pdf | Bin 0 -> 298177 bytes doc/src/Linalg/Linalg.do.txt | 1100 ++++ doc/src/Linalg/beamerthemered_plain.sty | 12 + doc/src/Linalg/beamerthemered_shadow.sty | 15 + doc/src/Linalg/clean.sh | 3 + doc/src/Linalg/make.sh | 118 + doc/src/Splines/Splines.do.txt | 541 ++ doc/src/Splines/beamerthemered_plain.sty | 12 + doc/src/Splines/beamerthemered_shadow.sty | 15 + doc/src/Splines/clean.sh | 3 + doc/src/Splines/make.sh | 118 + doc/web/course.do.txt | 20 +- doc/web/course.html | 272 +- 260 files changed, 55552 insertions(+), 74 deletions(-) create mode 100644 doc/pub/Linalg/html/Linalg-bs.html create mode 100644 doc/pub/Linalg/html/Linalg-reveal.html create mode 100644 doc/pub/Linalg/html/Linalg-solarized.html create mode 100644 doc/pub/Linalg/html/Linalg.html create mode 100644 doc/pub/Linalg/html/reveal.js/.gitignore create mode 100644 doc/pub/Linalg/html/reveal.js/.travis.yml create mode 100644 doc/pub/Linalg/html/reveal.js/CONTRIBUTING.md create mode 100644 doc/pub/Linalg/html/reveal.js/Gruntfile.js create mode 100644 doc/pub/Linalg/html/reveal.js/LICENSE create mode 100644 doc/pub/Linalg/html/reveal.js/README.md create mode 100644 doc/pub/Linalg/html/reveal.js/bower.json create mode 100644 doc/pub/Linalg/html/reveal.js/css/images/cbc_footer.png create mode 100644 doc/pub/Linalg/html/reveal.js/css/images/cbc_symbol.png create mode 100644 doc/pub/Linalg/html/reveal.js/css/images/simula_footer.png create mode 100644 doc/pub/Linalg/html/reveal.js/css/images/simula_logo.png create mode 100644 doc/pub/Linalg/html/reveal.js/css/images/simula_symbol.png create mode 100644 doc/pub/Linalg/html/reveal.js/css/images/uio_footer.png create mode 100644 doc/pub/Linalg/html/reveal.js/css/images/uio_symbol.png create mode 100644 doc/pub/Linalg/html/reveal.js/css/print/paper.css create mode 100644 doc/pub/Linalg/html/reveal.js/css/print/pdf.css create mode 100644 doc/pub/Linalg/html/reveal.js/css/reveal.css create mode 100644 doc/pub/Linalg/html/reveal.js/css/reveal.scss create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/README.md create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/beige.css create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/beigesmall.css create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/black.css create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/blood.css create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/cbc.css create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/darkgray.css create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/default.css create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/league.css create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/moon.css create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/night.css create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/serif.css create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/simple.css create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/simula.css create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/sky.css create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/solarized.css create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/source/beige.scss create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/source/beigesmall.scss create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/source/black.scss create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/source/blood.scss create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/source/cbc.scss create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/source/darkgray.scss create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/source/default.scss create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/source/league.scss create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/source/moon.scss create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/source/night.scss create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/source/serif.scss create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/source/simple.scss create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/source/simula.scss create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/source/sky.scss create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/source/solarized.scss create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/source/white.scss create mode 100644 doc/pub/Linalg/html/reveal.js/css/theme/white.css create mode 100644 doc/pub/Linalg/html/reveal.js/index.html create mode 100644 doc/pub/Linalg/html/reveal.js/js/reveal.js create mode 100644 doc/pub/Linalg/html/reveal.js/lib/css/zenburn.css create mode 100644 doc/pub/Linalg/html/reveal.js/lib/font/league-gothic/LICENSE create mode 100644 doc/pub/Linalg/html/reveal.js/lib/font/league-gothic/league-gothic.css create mode 100644 doc/pub/Linalg/html/reveal.js/lib/font/league-gothic/league-gothic.eot create mode 100644 doc/pub/Linalg/html/reveal.js/lib/font/league-gothic/league-gothic.ttf create mode 100644 doc/pub/Linalg/html/reveal.js/lib/font/league-gothic/league-gothic.woff create mode 100644 doc/pub/Linalg/html/reveal.js/lib/font/source-sans-pro/LICENSE create mode 100644 doc/pub/Linalg/html/reveal.js/lib/font/source-sans-pro/source-sans-pro-italic.eot create mode 100644 doc/pub/Linalg/html/reveal.js/lib/font/source-sans-pro/source-sans-pro-italic.ttf create mode 100644 doc/pub/Linalg/html/reveal.js/lib/font/source-sans-pro/source-sans-pro-italic.woff create mode 100644 doc/pub/Linalg/html/reveal.js/lib/font/source-sans-pro/source-sans-pro-regular.eot create mode 100644 doc/pub/Linalg/html/reveal.js/lib/font/source-sans-pro/source-sans-pro-regular.ttf create mode 100644 doc/pub/Linalg/html/reveal.js/lib/font/source-sans-pro/source-sans-pro-regular.woff create mode 100644 doc/pub/Linalg/html/reveal.js/lib/font/source-sans-pro/source-sans-pro-semibold.eot create mode 100644 doc/pub/Linalg/html/reveal.js/lib/font/source-sans-pro/source-sans-pro-semibold.ttf create mode 100644 doc/pub/Linalg/html/reveal.js/lib/font/source-sans-pro/source-sans-pro-semibold.woff create mode 100644 doc/pub/Linalg/html/reveal.js/lib/font/source-sans-pro/source-sans-pro-semibolditalic.eot create mode 100644 doc/pub/Linalg/html/reveal.js/lib/font/source-sans-pro/source-sans-pro-semibolditalic.ttf create mode 100644 doc/pub/Linalg/html/reveal.js/lib/font/source-sans-pro/source-sans-pro-semibolditalic.woff create mode 100644 doc/pub/Linalg/html/reveal.js/lib/font/source-sans-pro/source-sans-pro.css create mode 100644 doc/pub/Linalg/html/reveal.js/lib/js/classList.js create mode 100644 doc/pub/Linalg/html/reveal.js/lib/js/head.min.js create mode 100644 doc/pub/Linalg/html/reveal.js/lib/js/html5shiv.js create mode 100644 doc/pub/Linalg/html/reveal.js/package.json create mode 100644 doc/pub/Linalg/html/reveal.js/plugin/highlight/highlight.js create mode 100644 doc/pub/Linalg/html/reveal.js/plugin/leap/leap.js create mode 100644 doc/pub/Linalg/html/reveal.js/plugin/markdown/example.html create mode 100644 doc/pub/Linalg/html/reveal.js/plugin/markdown/example.md create mode 100644 doc/pub/Linalg/html/reveal.js/plugin/markdown/markdown.js create mode 100644 doc/pub/Linalg/html/reveal.js/plugin/markdown/marked.js create mode 100644 doc/pub/Linalg/html/reveal.js/plugin/math/math.js create mode 100644 doc/pub/Linalg/html/reveal.js/plugin/multiplex/client.js create mode 100644 doc/pub/Linalg/html/reveal.js/plugin/multiplex/index.js create mode 100644 doc/pub/Linalg/html/reveal.js/plugin/multiplex/master.js create mode 100644 doc/pub/Linalg/html/reveal.js/plugin/notes-server/client.js create mode 100644 doc/pub/Linalg/html/reveal.js/plugin/notes-server/index.js create mode 100644 doc/pub/Linalg/html/reveal.js/plugin/notes-server/notes.html create mode 100644 doc/pub/Linalg/html/reveal.js/plugin/notes/notes.html create mode 100644 doc/pub/Linalg/html/reveal.js/plugin/notes/notes.js create mode 100644 doc/pub/Linalg/html/reveal.js/plugin/print-pdf/print-pdf.js create mode 100644 doc/pub/Linalg/html/reveal.js/plugin/remotes/remotes.js create mode 100644 doc/pub/Linalg/html/reveal.js/plugin/search/search.js create mode 100644 doc/pub/Linalg/html/reveal.js/plugin/zoom-js/zoom.js create mode 100644 doc/pub/Linalg/html/reveal.js/test/examples/assets/image1.png create mode 100644 doc/pub/Linalg/html/reveal.js/test/examples/assets/image2.png create mode 100644 doc/pub/Linalg/html/reveal.js/test/examples/barebones.html create mode 100644 doc/pub/Linalg/html/reveal.js/test/examples/embedded-media.html create mode 100644 doc/pub/Linalg/html/reveal.js/test/examples/math.html create mode 100644 doc/pub/Linalg/html/reveal.js/test/examples/slide-backgrounds.html create mode 100644 doc/pub/Linalg/html/reveal.js/test/examples/slide-transitions.html create mode 100644 doc/pub/Linalg/html/reveal.js/test/qunit-1.12.0.css create mode 100644 doc/pub/Linalg/html/reveal.js/test/qunit-1.12.0.js create mode 100644 doc/pub/Linalg/html/reveal.js/test/test-markdown-element-attributes.html create mode 100644 doc/pub/Linalg/html/reveal.js/test/test-markdown-element-attributes.js create mode 100644 doc/pub/Linalg/html/reveal.js/test/test-markdown-slide-attributes.html create mode 100644 doc/pub/Linalg/html/reveal.js/test/test-markdown-slide-attributes.js create mode 100644 doc/pub/Linalg/html/reveal.js/test/test-markdown.html create mode 100644 doc/pub/Linalg/html/reveal.js/test/test-markdown.js create mode 100644 doc/pub/Linalg/html/reveal.js/test/test-pdf.html create mode 100644 doc/pub/Linalg/html/reveal.js/test/test-pdf.js create mode 100644 doc/pub/Linalg/html/reveal.js/test/test.html create mode 100644 doc/pub/Linalg/html/reveal.js/test/test.js create mode 100644 doc/pub/Linalg/ipynb/Linalg.ipynb create mode 100644 doc/pub/Linalg/ipynb/ipynb-Linalg-src.tar.gz create mode 100644 doc/pub/Linalg/pdf/Linalg-beamer-handouts2x3.pdf create mode 100644 doc/pub/Linalg/pdf/Linalg-beamer.pdf create mode 100644 doc/pub/Linalg/pdf/Linalg-minted.pdf create mode 100644 doc/pub/Splines/html/Splines-bs.html create mode 100644 doc/pub/Splines/html/Splines-reveal.html create mode 100644 doc/pub/Splines/html/Splines-solarized.html create mode 100644 doc/pub/Splines/html/Splines.html create mode 100644 doc/pub/Splines/html/reveal.js/.gitignore create mode 100644 doc/pub/Splines/html/reveal.js/.travis.yml create mode 100644 doc/pub/Splines/html/reveal.js/CONTRIBUTING.md create mode 100644 doc/pub/Splines/html/reveal.js/Gruntfile.js create mode 100644 doc/pub/Splines/html/reveal.js/LICENSE create mode 100644 doc/pub/Splines/html/reveal.js/README.md create mode 100644 doc/pub/Splines/html/reveal.js/bower.json create mode 100644 doc/pub/Splines/html/reveal.js/css/images/cbc_footer.png create mode 100644 doc/pub/Splines/html/reveal.js/css/images/cbc_symbol.png create mode 100644 doc/pub/Splines/html/reveal.js/css/images/simula_footer.png create mode 100644 doc/pub/Splines/html/reveal.js/css/images/simula_logo.png create mode 100644 doc/pub/Splines/html/reveal.js/css/images/simula_symbol.png create mode 100644 doc/pub/Splines/html/reveal.js/css/images/uio_footer.png create mode 100644 doc/pub/Splines/html/reveal.js/css/images/uio_symbol.png create mode 100644 doc/pub/Splines/html/reveal.js/css/print/paper.css create mode 100644 doc/pub/Splines/html/reveal.js/css/print/pdf.css create mode 100644 doc/pub/Splines/html/reveal.js/css/reveal.css create mode 100644 doc/pub/Splines/html/reveal.js/css/reveal.scss create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/README.md create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/beige.css create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/beigesmall.css create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/black.css create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/blood.css create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/cbc.css create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/darkgray.css create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/default.css create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/league.css create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/moon.css create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/night.css create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/serif.css create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/simple.css create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/simula.css create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/sky.css create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/solarized.css create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/source/beige.scss create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/source/beigesmall.scss create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/source/black.scss create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/source/blood.scss create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/source/cbc.scss create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/source/darkgray.scss create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/source/default.scss create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/source/league.scss create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/source/moon.scss create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/source/night.scss create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/source/serif.scss create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/source/simple.scss create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/source/simula.scss create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/source/sky.scss create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/source/solarized.scss create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/source/white.scss create mode 100644 doc/pub/Splines/html/reveal.js/css/theme/white.css create mode 100644 doc/pub/Splines/html/reveal.js/index.html create mode 100644 doc/pub/Splines/html/reveal.js/js/reveal.js create mode 100644 doc/pub/Splines/html/reveal.js/lib/css/zenburn.css create mode 100644 doc/pub/Splines/html/reveal.js/lib/font/league-gothic/LICENSE create mode 100644 doc/pub/Splines/html/reveal.js/lib/font/league-gothic/league-gothic.css create mode 100644 doc/pub/Splines/html/reveal.js/lib/font/league-gothic/league-gothic.eot create mode 100644 doc/pub/Splines/html/reveal.js/lib/font/league-gothic/league-gothic.ttf create mode 100644 doc/pub/Splines/html/reveal.js/lib/font/league-gothic/league-gothic.woff create mode 100644 doc/pub/Splines/html/reveal.js/lib/font/source-sans-pro/LICENSE create mode 100644 doc/pub/Splines/html/reveal.js/lib/font/source-sans-pro/source-sans-pro-italic.eot create mode 100644 doc/pub/Splines/html/reveal.js/lib/font/source-sans-pro/source-sans-pro-italic.ttf create mode 100644 doc/pub/Splines/html/reveal.js/lib/font/source-sans-pro/source-sans-pro-italic.woff create mode 100644 doc/pub/Splines/html/reveal.js/lib/font/source-sans-pro/source-sans-pro-regular.eot create mode 100644 doc/pub/Splines/html/reveal.js/lib/font/source-sans-pro/source-sans-pro-regular.ttf create mode 100644 doc/pub/Splines/html/reveal.js/lib/font/source-sans-pro/source-sans-pro-regular.woff create mode 100644 doc/pub/Splines/html/reveal.js/lib/font/source-sans-pro/source-sans-pro-semibold.eot create mode 100644 doc/pub/Splines/html/reveal.js/lib/font/source-sans-pro/source-sans-pro-semibold.ttf create mode 100644 doc/pub/Splines/html/reveal.js/lib/font/source-sans-pro/source-sans-pro-semibold.woff create mode 100644 doc/pub/Splines/html/reveal.js/lib/font/source-sans-pro/source-sans-pro-semibolditalic.eot create mode 100644 doc/pub/Splines/html/reveal.js/lib/font/source-sans-pro/source-sans-pro-semibolditalic.ttf create mode 100644 doc/pub/Splines/html/reveal.js/lib/font/source-sans-pro/source-sans-pro-semibolditalic.woff create mode 100644 doc/pub/Splines/html/reveal.js/lib/font/source-sans-pro/source-sans-pro.css create mode 100644 doc/pub/Splines/html/reveal.js/lib/js/classList.js create mode 100644 doc/pub/Splines/html/reveal.js/lib/js/head.min.js create mode 100644 doc/pub/Splines/html/reveal.js/lib/js/html5shiv.js create mode 100644 doc/pub/Splines/html/reveal.js/package.json create mode 100644 doc/pub/Splines/html/reveal.js/plugin/highlight/highlight.js create mode 100644 doc/pub/Splines/html/reveal.js/plugin/leap/leap.js create mode 100644 doc/pub/Splines/html/reveal.js/plugin/markdown/example.html create mode 100644 doc/pub/Splines/html/reveal.js/plugin/markdown/example.md create mode 100644 doc/pub/Splines/html/reveal.js/plugin/markdown/markdown.js create mode 100644 doc/pub/Splines/html/reveal.js/plugin/markdown/marked.js create mode 100644 doc/pub/Splines/html/reveal.js/plugin/math/math.js create mode 100644 doc/pub/Splines/html/reveal.js/plugin/multiplex/client.js create mode 100644 doc/pub/Splines/html/reveal.js/plugin/multiplex/index.js create mode 100644 doc/pub/Splines/html/reveal.js/plugin/multiplex/master.js create mode 100644 doc/pub/Splines/html/reveal.js/plugin/notes-server/client.js create mode 100644 doc/pub/Splines/html/reveal.js/plugin/notes-server/index.js create mode 100644 doc/pub/Splines/html/reveal.js/plugin/notes-server/notes.html create mode 100644 doc/pub/Splines/html/reveal.js/plugin/notes/notes.html create mode 100644 doc/pub/Splines/html/reveal.js/plugin/notes/notes.js create mode 100644 doc/pub/Splines/html/reveal.js/plugin/print-pdf/print-pdf.js create mode 100644 doc/pub/Splines/html/reveal.js/plugin/remotes/remotes.js create mode 100644 doc/pub/Splines/html/reveal.js/plugin/search/search.js create mode 100644 doc/pub/Splines/html/reveal.js/plugin/zoom-js/zoom.js create mode 100644 doc/pub/Splines/html/reveal.js/test/examples/assets/image1.png create mode 100644 doc/pub/Splines/html/reveal.js/test/examples/assets/image2.png create mode 100644 doc/pub/Splines/html/reveal.js/test/examples/barebones.html create mode 100644 doc/pub/Splines/html/reveal.js/test/examples/embedded-media.html create mode 100644 doc/pub/Splines/html/reveal.js/test/examples/math.html create mode 100644 doc/pub/Splines/html/reveal.js/test/examples/slide-backgrounds.html create mode 100644 doc/pub/Splines/html/reveal.js/test/examples/slide-transitions.html create mode 100644 doc/pub/Splines/html/reveal.js/test/qunit-1.12.0.css create mode 100644 doc/pub/Splines/html/reveal.js/test/qunit-1.12.0.js create mode 100644 doc/pub/Splines/html/reveal.js/test/test-markdown-element-attributes.html create mode 100644 doc/pub/Splines/html/reveal.js/test/test-markdown-element-attributes.js create mode 100644 doc/pub/Splines/html/reveal.js/test/test-markdown-slide-attributes.html create mode 100644 doc/pub/Splines/html/reveal.js/test/test-markdown-slide-attributes.js create mode 100644 doc/pub/Splines/html/reveal.js/test/test-markdown.html create mode 100644 doc/pub/Splines/html/reveal.js/test/test-markdown.js create mode 100644 doc/pub/Splines/html/reveal.js/test/test-pdf.html create mode 100644 doc/pub/Splines/html/reveal.js/test/test-pdf.js create mode 100644 doc/pub/Splines/html/reveal.js/test/test.html create mode 100644 doc/pub/Splines/html/reveal.js/test/test.js create mode 100644 doc/pub/Splines/ipynb/Splines.ipynb create mode 100644 doc/pub/Splines/ipynb/ipynb-Splines-src.tar.gz create mode 100644 doc/pub/Splines/pdf/Splines-beamer-handouts2x3.pdf create mode 100644 doc/pub/Splines/pdf/Splines-beamer.pdf create mode 100644 doc/pub/Splines/pdf/Splines-minted.pdf create mode 100644 doc/src/Linalg/Linalg.do.txt create mode 100644 doc/src/Linalg/beamerthemered_plain.sty create mode 100644 doc/src/Linalg/beamerthemered_shadow.sty create mode 100644 doc/src/Linalg/clean.sh create mode 100755 doc/src/Linalg/make.sh create mode 100644 doc/src/Splines/Splines.do.txt create mode 100644 doc/src/Splines/beamerthemered_plain.sty create mode 100644 doc/src/Splines/beamerthemered_shadow.sty create mode 100644 doc/src/Splines/clean.sh create mode 100755 doc/src/Splines/make.sh diff --git a/doc/pub/Linalg/html/Linalg-bs.html b/doc/pub/Linalg/html/Linalg-bs.html new file mode 100644 index 000000000..5bd44a912 --- /dev/null +++ b/doc/pub/Linalg/html/Linalg-bs.html @@ -0,0 +1,1567 @@ + + + + + + + +Data analysis and Machine Learning Lectures: Linear Algebra methods + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +

 

 

 

+ + + + + +
+

Data analysis and Machine Learning Lectures: Linear Algebra methods

+ +

+ + +

+Morten Hjorth-Jensen [1, 2] +
+ +

+ + +

[1] Department of Physics, University of Oslo
+
[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University
+
+

+

Jan 27, 2018

+
+

+ +

+ + + +

Important Matrix and vector handling packages

+ +

+The Numerical Recipes codes have been rewritten in Fortran 90/95 and +C/C++ by us. The original source codes are taken from the widely used +software package LAPACK, which follows two other popular packages +developed in the 1970s, namely EISPACK and LINPACK. + +

+ +Add python material on linear algebra and array handling, text on numpy etc + +

+ + +

Basic Matrix Features

+ +

+

+
+

+$$ + \mathbf{A} = + \begin{bmatrix} a_{11} & a_{12} & a_{13} & a_{14} \\ + a_{21} & a_{22} & a_{23} & a_{24} \\ + a_{31} & a_{32} & a_{33} & a_{34} \\ + a_{41} & a_{42} & a_{43} & a_{44} + \end{bmatrix}\qquad +\mathbf{I} = + \begin{bmatrix} 1 & 0 & 0 & 0 \\ + 0 & 1 & 0 & 0 \\ + 0 & 0 & 1 & 0 \\ + 0 & 0 & 0 & 1 + \end{bmatrix} +$$ +

+
+ + + +

Basic Matrix Features

+
+
+

+The inverse of a matrix is defined by + +$$ +\mathbf{A}^{-1} \cdot \mathbf{A} = I +$$ +

+
+ + +

+ + +

Basic Matrix Features

+ +

+

+
+

+ +

+ +

+
+ + + + + + + + + + + +
Relations Name matrix elements
\( A = A^{T} \) symmetric \( a_{ij} = a_{ji} \)
\( A = \left (A^{T} \right )^{-1} \) real orthogonal \( \sum_k a_{ik} a_{jk} = \sum_k a_{ki} a_{kj} = \delta_{ij} \)
\( A = A^{ * } \) real matrix \( a_{ij} = a_{ij}^{ * } \)
\( A = A^{\dagger} \) hermitian \( a_{ij} = a_{ji}^{ * } \)
\( A = \left (A^{\dagger} \right )^{-1} \) unitary \( \sum_k a_{ik} a_{jk}^{ * } = \sum_k a_{ki}^{ * } a_{kj} = \delta_{ij} \)
+
+
+

+

+
+ + +

+ + +

Some famous Matrices

+ + + + + +

Basic Matrix Features

+ +

+

+
+

+For an \( N\times N \) matrix \( \mathbf{A} \) the following properties are all equivalent + +

    +
  • If the inverse of \( \mathbf{A} \) exists, \( \mathbf{A} \) is nonsingular.
  • +
  • The equation \( \mathbf{Ax}=0 \) implies \( \mathbf{x}=0 \).
  • +
  • The rows of \( \mathbf{A} \) form a basis of \( R^N \).
  • +
  • The columns of \( \mathbf{A} \) form a basis of \( R^N \).
  • +
  • \( \mathbf{A} \) is a product of elementary matrices.
  • +
  • \( 0 \) is not eigenvalue of \( \mathbf{A} \).
  • +
+
+
+ + +

+ + +

Matrix Handling in C/C++, Static and Dynamical allocation

+ +

+

+
+

+We have an \( N\times N \) matrix A with \( N=100 \) +In C/C++ this would be defined as + +

+ + +

   int N = 100;
+   double A[100][100];
+   //   initialize all elements to zero
+   for(i=0 ; i < N ; i++) {
+      for(j=0 ; j < N ; j++) {
+         A[i][j] = 0.0;
+
+

+Note the way the matrix is organized, row-major order. +

+
+ + +

+ + +

Matrix Handling in C/C++

+ +

+

+
+

+We have \( N\times N \) matrices A, B and C and we wish to +evaluate \( A=B+C \). + +$$ +\mathbf{A}= \mathbf{B}\pm\mathbf{C} \Longrightarrow a_{ij} = b_{ij}\pm c_{ij}, +$$ + +In C/C++ this would be coded like + +

+ + +

   for(i=0 ; i < N ; i++) {
+      for(j=0 ; j < N ; j++) {
+         a[i][j] = b[i][j]+c[i][j]
+
+

+

+
+ + +

+ + +

Matrix Handling in C/C++

+ +

+

+
+

+We have \( N\times N \) matrices A, B and C and we wish to +evaluate \( A=BC \). + +$$ +\mathbf{A}=\mathbf{BC} \Longrightarrow a_{ij} = \sum_{k=1}^{n} b_{ik}c_{kj}, +$$ + +In C/C++ this would be coded like + +

+ + +

   for(i=0 ; i < N ; i++) {
+      for(j=0 ; j < N ; j++) {
+         for(k=0 ; k < N ; k++) {
+            a[i][j]+=b[i][k]*c[k][j];
+
+

+

+
+ + +

+ + +

Dynamic memory allocation in C/C++

+ +

+At least three possibilities in this course + +

+ + + +

Matrix Handling in C/C++, Dynamic Allocation

+ +

+

+
+

+

+ + +

int N;
+double **  A;
+A = new double*[N]
+for ( i = 0; i < N; i++)
+    A[i] = new double[N];
+
+

+Always free space when you don't need an array anymore. + +

+ + +

for ( i = 0; i < N; i++)
+    delete[] A[i];
+delete[] A;
+
+

+

+
+ + +

+ + +

Armadillo, recommended!!

+ + + + + +

Armadillo, simple examples

+ +

+ + +

#include <iostream>
+#include <armadillo>
+
+using namespace std;
+using namespace arma;
+
+int main(int argc, char** argv)
+  {
+  mat A = randu<mat>(5,5);
+  mat B = randu<mat>(5,5);
+
+  cout << A*B << endl;
+
+  return 0;
+
+

+ + +

Armadillo, how to compile and install

+ +

+For people using Ubuntu, Debian, Linux Mint, simply go to the synaptic package manager and install +armadillo from there. +You may have to install Lapack as well. +For Mac and Windows users, follow the instructions from the webpage +http://arma.sourceforge.net. +To compile, use for example (linux/ubuntu) + +

+ + +

c++ -O2 -o program.x program.cpp  -larmadillo -llapack -lblas
+
+

+where the -l option indicates the library you wish to link to. + +

+For OS X users you may have to declare the paths to the include files and the libraries as +

+ + +

c++ -O2 -o program.x program.cpp  -L/usr/local/lib -I/usr/local/include -larmadillo -llapack -lblas
+
+

+ + +

Armadillo, simple examples

+ +

+ + +

#include <iostream>
+#include "armadillo"
+using namespace arma;
+using namespace std;
+
+int main(int argc, char** argv)
+  {
+  // directly specify the matrix size (elements are uninitialised)
+  mat A(2,3);
+  // .n_rows = number of rows    (read only)
+  // .n_cols = number of columns (read only)
+  cout << "A.n_rows = " << A.n_rows << endl;
+  cout << "A.n_cols = " << A.n_cols << endl;
+  // directly access an element (indexing starts at 0)
+  A(1,2) = 456.0;
+  A.print("A:");
+  // scalars are treated as a 1x1 matrix,
+  // hence the code below will set A to have a size of 1x1
+  A = 5.0;
+  A.print("A:");
+  // if you want a matrix with all elements set to a particular value
+  // the .fill() member function can be used
+  A.set_size(3,3);
+  A.fill(5.0);  A.print("A:");
+
+

+ + +

Armadillo, simple examples

+ +

+ + +

  mat B;
+
+  // endr indicates "end of row"
+  B << 0.555950 << 0.274690 << 0.540605 << 0.798938 << endr
+    << 0.108929 << 0.830123 << 0.891726 << 0.895283 << endr
+    << 0.948014 << 0.973234 << 0.216504 << 0.883152 << endr
+    << 0.023787 << 0.675382 << 0.231751 << 0.450332 << endr;
+
+  // print to the cout stream
+  // with an optional string before the contents of the matrix
+  B.print("B:");
+
+  // the << operator can also be used to print the matrix
+  // to an arbitrary stream (cout in this case)
+  cout << "B:" << endl << B << endl;
+  // save to disk
+  B.save("B.txt", raw_ascii);
+  // load from disk
+  mat C;
+  C.load("B.txt");
+  C += 2.0 * B;
+  C.print("C:");
+
+

+ + +

Armadillo, simple examples

+ +

+ + +

  // submatrix types:
+  //
+  // .submat(first_row, first_column, last_row, last_column)
+  // .row(row_number)
+  // .col(column_number)
+  // .cols(first_column, last_column)
+  // .rows(first_row, last_row)
+
+  cout << "C.submat(0,0,3,1) =" << endl;
+  cout << C.submat(0,0,3,1) << endl;
+
+  // generate the identity matrix
+  mat D = eye<mat>(4,4);
+
+  D.submat(0,0,3,1) = C.cols(1,2);
+  D.print("D:");
+
+  // transpose
+  cout << "trans(B) =" << endl;
+  cout << trans(B) << endl;
+
+  // maximum from each column (traverse along rows)
+  cout << "max(B) =" << endl;
+  cout << max(B) << endl;
+
+

+ + +

Armadillo, simple examples

+ +

+ + +

  // maximum from each row (traverse along columns)
+  cout << "max(B,1) =" << endl;
+  cout << max(B,1) << endl;
+  // maximum value in B
+  cout << "max(max(B)) = " << max(max(B)) << endl;
+  // sum of each column (traverse along rows)
+  cout << "sum(B) =" << endl;
+  cout << sum(B) << endl;
+  // sum of each row (traverse along columns)
+  cout << "sum(B,1) =" << endl;
+  cout << sum(B,1) << endl;
+  // sum of all elements
+  cout << "sum(sum(B)) = " << sum(sum(B)) << endl;
+  cout << "accu(B)     = " << accu(B) << endl;
+  // trace = sum along diagonal
+  cout << "trace(B)    = " << trace(B) << endl;
+  // random matrix -- values are uniformly distributed in the [0,1] interval
+  mat E = randu<mat>(4,4);
+  E.print("E:");
+
+

+ + +

Armadillo, simple examples

+ +

+ + +

  // row vectors are treated like a matrix with one row
+  rowvec r;
+  r << 0.59499 << 0.88807 << 0.88532 << 0.19968;
+  r.print("r:");
+
+  // column vectors are treated like a matrix with one column
+  colvec q;
+  q << 0.81114 << 0.06256 << 0.95989 << 0.73628;
+  q.print("q:");
+
+  // dot or inner product
+  cout << "as_scalar(r*q) = " << as_scalar(r*q) << endl;
+
+    // outer product
+  cout << "q*r =" << endl;
+  cout << q*r << endl;
+
+
+  // sum of three matrices (no temporary matrices are created)
+  mat F = B + C + D;
+  F.print("F:");
+
+    return 0;
+
+

+ + +

Armadillo, simple examples

+ +

+ + +

#include <iostream>
+#include "armadillo"
+using namespace arma;
+using namespace std;
+
+int main(int argc, char** argv)
+  {
+  cout << "Armadillo version: " << arma_version::as_string() << endl;
+
+  mat A;
+
+  A << 0.165300 << 0.454037 << 0.995795 << 0.124098 << 0.047084 << endr
+    << 0.688782 << 0.036549 << 0.552848 << 0.937664 << 0.866401 << endr
+    << 0.348740 << 0.479388 << 0.506228 << 0.145673 << 0.491547 << endr
+    << 0.148678 << 0.682258 << 0.571154 << 0.874724 << 0.444632 << endr
+    << 0.245726 << 0.595218 << 0.409327 << 0.367827 << 0.385736 << endr;
+
+  A.print("A =");
+
+  // determinant
+  cout << "det(A) = " << det(A) << endl;
+
+

+ + +

Armadillo, simple examples

+ +

+ + +

  // inverse
+  cout << "inv(A) = " << endl << inv(A) << endl;
+  double k = 1.23;
+
+  mat    B = randu<mat>(5,5);
+  mat    C = randu<mat>(5,5);
+
+  rowvec r = randu<rowvec>(5);
+  colvec q = randu<colvec>(5);
+
+
+  // examples of some expressions
+  // for which optimised implementations exist
+  // optimised implementation of a trinary expression
+  // that results in a scalar
+  cout << "as_scalar( r*inv(diagmat(B))*q ) = ";
+  cout << as_scalar( r*inv(diagmat(B))*q ) << endl;
+
+  // example of an expression which is optimised
+  // as a call to the dgemm() function in BLAS:
+  cout << "k*trans(B)*C = " << endl << k*trans(B)*C;
+
+    return 0;
+
+

+ + +

Gaussian Elimination

+ +

+We start with the linear set of equations + +$$ + \mathbf{A}\mathbf{x} = \mathbf{w}. +$$ + +We assume also that the matrix \( \mathbf{A} \) is non-singular and that the +matrix elements along the diagonal satisfy \( a_{ii} \ne 0 \). Simple \( 4\times 4 \) example + +$$ +\begin{bmatrix} + a_{11}& a_{12} &a_{13}& a_{14}\\ + a_{21}& a_{22} &a_{23}& a_{24}\\ + a_{31}& a_{32} &a_{33}& a_{34}\\ + a_{41}& a_{42} &a_{43}& a_{44}\\ + \end{bmatrix} \begin{bmatrix} + x_1\\ + x_2\\ + x_3 \\ + x_4 \\ + \end{bmatrix} + =\begin{bmatrix} + w_1\\ + w_2\\ + w_3 \\ + w_4\\ + \end{bmatrix}. +$$ + +

+ + +

Gaussian Elimination

+or + +$$ +\begin{align} + a_{11}x_1 +a_{12}x_2 +a_{13}x_3 + a_{14}x_4=&w_1 \nonumber \\ +a_{21}x_1 + a_{22}x_2 + a_{23}x_3 + a_{24}x_4=&w_2 \nonumber \\ +a_{31}x_1 + a_{32}x_2 + a_{33}x_3 + a_{34}x_4=&w_3 \nonumber \\ +a_{41}x_1 + a_{42}x_2 + a_{43}x_3 + a_{44}x_4=&w_4. \nonumber +\end{align} +$$ + +

+ + +

Gaussian Elimination

+ +

+The basic idea of Gaussian elimination is to use the first equation to eliminate the first unknown \( x_1 \) +from the remaining \( n-1 \) equations. Then we use the new second equation to eliminate the second unknown +\( x_2 \) from the remaining \( n-2 \) equations. With \( n-1 \) such eliminations +we obtain a so-called upper triangular set of equations of the form + +$$ +\begin{align} + b_{11}x_1 +b_{12}x_2 +b_{13}x_3 + b_{14}x_4=&y_1 \nonumber \\ + b_{22}x_2 + b_{23}x_3 + b_{24}x_4=&y_2 \nonumber \\ +b_{33}x_3 + b_{34}x_4=&y_3 \nonumber \\ +b_{44}x_4=&y_4. \nonumber +\label{eq:gaussbacksub} +\end{align} +$$ + +We can solve this system of equations recursively starting from \( x_n \) (in our case \( x_4 \)) and proceed with +what is called a backward substitution. + +

+ + +

Gaussian Elimination

+This process can be expressed mathematically as + +$$ +\begin{equation} + x_m = \frac{1}{b_{mm}}\left(y_m-\sum_{k=m+1}^nb_{mk}x_k\right)\quad m=n-1,n-2,\dots,1. +\label{_auto1} +\end{equation} +$$ + +To arrive at such an upper triangular system of equations, we start by eliminating +the unknown \( x_1 \) for \( j=2,n \). We achieve this by multiplying the first equation by \( a_{j1}/a_{11} \) and then subtract +the result from the $j$th equation. We assume obviously that \( a_{11}\ne 0 \) and that +\( \mathbf{A} \) is not singular. + +

+ + +

Gaussian Elimination

+ +

+Our actual \( 4\times 4 \) example reads after the first operation + +$$ +\begin{bmatrix} + a_{11}& a_{12} &a_{13}& a_{14}\\ + 0& (a_{22}-\frac{a_{21}a_{12}}{a_{11}}) &(a_{23}-\frac{a_{21}a_{13}}{a_{11}}) & (a_{24}-\frac{a_{21}a_{14}}{a_{11}})\\ +0& (a_{32}-\frac{a_{31}a_{12}}{a_{11}})& (a_{33}-\frac{a_{31}a_{13}}{a_{11}})& (a_{34}-\frac{a_{31}a_{14}}{a_{11}})\\ +0&(a_{42}-\frac{a_{41}a_{12}}{a_{11}}) &(a_{43}-\frac{a_{41}a_{13}}{a_{11}}) & (a_{44}-\frac{a_{41}a_{14}}{a_{11}}) \\ + \end{bmatrix} \begin{bmatrix} + x_1\\ + x_2\\ + x_3 \\ + x_4 \\ + \end{bmatrix} + =\begin{bmatrix} + y_1\\ + w_2^{(2)}\\ + w_3^{(2)} \\ + w_4^{(2)}\\ + \end{bmatrix}, +$$ + +or + +$$ +\begin{align} + b_{11}x_1 +b_{12}x_2 +b_{13}x_3 + b_{14}x_4=&y_1 \nonumber \\ + a^{(2)}_{22}x_2 + a^{(2)}_{23}x_3 + a^{(2)}_{24}x_4=&w^{(2)}_2 \nonumber \\ + a^{(2)}_{32}x_2 + a^{(2)}_{33}x_3 + a^{(2)}_{34}x_4=&w^{(2)}_3 \nonumber \\ + a^{(2)}_{42}x_2 + a^{(2)}_{43}x_3 + a^{(2)}_{44}x_4=&w^{(2)}_4, \nonumber \\ +\label{_auto2} +\end{align} +$$ + +

+ + +

Gaussian Elimination

+ +

+The new coefficients are + +$$ +\begin{equation} + b_{1k} = a_{1k}^{(1)} \quad k=1,\dots,n, +\label{_auto3} +\end{equation} +$$ + +where each \( a_{1k}^{(1)} \) is equal to the original \( a_{1k} \) element. The other coefficients are + +$$ +\begin{equation} +a_{jk}^{(2)} = a_{jk}^{(1)}-\frac{a_{j1}^{(1)}a_{1k}^{(1)}}{a_{11}^{(1)}} \quad j,k=2,\dots,n, +\label{_auto4} +\end{equation} +$$ + +with a new right-hand side given by + +$$ +\begin{equation} +y_{1}=w_1^{(1)}, \quad w_j^{(2)} =w_j^{(1)}-\frac{a_{j1}^{(1)}w_1^{(1)}}{a_{11}^{(1)}} \quad j=2,\dots,n. +\label{_auto5} +\end{equation} +$$ + +We have also set \( w_1^{(1)}=w_1 \), the original vector element. +We see that the system of unknowns \( x_1,\dots,x_n \) is transformed into an \( (n-1)\times (n-1) \) problem. + +

+ + +

Gaussian Elimination

+ +

+This step is called forward substitution. +Proceeding with these substitutions, we obtain the +general expressions for the new coefficients + +$$ +\begin{equation} + a_{jk}^{(m+1)} = a_{jk}^{(m)}-\frac{a_{jm}^{(m)}a_{mk}^{(m)}}{a_{mm}^{(m)}} \quad j,k=m+1,\dots,n, +\label{_auto6} +\end{equation} +$$ + +with \( m=1,\dots,n-1 \) and a +right-hand side given by + +$$ +\begin{equation} + w_j^{(m+1)} =w_j^{(m)}-\frac{a_{jm}^{(m)}w_m^{(m)}}{a_{mm}^{(m)}}\quad j=m+1,\dots,n. +\label{_auto7} +\end{equation} +$$ + +This set of \( n-1 \) elimations leads us to an equations which is solved by back substitution. +If the arithmetics is exact and the matrix \( \mathbf{A} \) is not singular, then the computed answer will be exact. + +

+Even though the matrix elements along the diagonal are not zero, +numerically small numbers may appear and subsequent divisions may lead to large numbers, which, if added +to a small number may yield losses of precision. Suppose for example that our first division in \( (a_{22}-a_{21}a_{12}/a_{11}) \) +results in \( -10^{-7} \) and that \( a_{22} \) is one. +one. We are then +adding \( 10^7+1 \). With single precision this results in \( 10^7 \). + +

+ + +

Linear Algebra Methods

+ + + + + +

LU Decomposition

+ +

+The LU decomposition method means that we can rewrite +this matrix as the product of two matrices \( \mathbf{L} \) and \( \mathbf{U} \) +where + +$$ + \begin{bmatrix} + a_{11} & a_{12} & a_{13} & a_{14} \\ + a_{21} & a_{22} & a_{23} & a_{24} \\ + a_{31} & a_{32} & a_{33} & a_{34} \\ + a_{41} & a_{42} & a_{43} & a_{44} + \end{bmatrix} + = \begin{bmatrix} + 1 & 0 & 0 & 0 \\ + l_{21} & 1 & 0 & 0 \\ + l_{31} & l_{32} & 1 & 0 \\ + l_{41} & l_{42} & l_{43} & 1 + \end{bmatrix} + \begin{bmatrix} + u_{11} & u_{12} & u_{13} & u_{14} \\ + 0 & u_{22} & u_{23} & u_{24} \\ + 0 & 0 & u_{33} & u_{34} \\ + 0 & 0 & 0 & u_{44} + \end{bmatrix}. +$$ + +

+ + +

LU Decomposition

+ +

+LU decomposition forms the backbone of other algorithms in linear algebra, such as the +solution of linear equations given by + +$$ +\begin{align} + a_{11}x_1 +a_{12}x_2 +a_{13}x_3 + a_{14}x_4=&w_1 \nonumber \\ +a_{21}x_1 + a_{22}x_2 + a_{23}x_3 + a_{24}x_4=&w_2 \nonumber \\ +a_{31}x_1 + a_{32}x_2 + a_{33}x_3 + a_{34}x_4=&w_3 \nonumber \\ +a_{41}x_1 + a_{42}x_2 + a_{43}x_3 + a_{44}x_4=&w_4. \nonumber +\end{align} +$$ + +The above set of equations is conveniently solved by using LU decomposition as an intermediate step. + +

+The matrix \( \mathbf{A}\in \mathbb{R}^{n\times n} \) has an LU factorization if the determinant +is different from zero. If the LU factorization exists and \( \mathbf{A} \) is non-singular, then the LU factorization +is unique and the determinant is given by + +$$ +det\{\mathbf{A}\}=det\{\mathbf{LU}\}= det\{\mathbf{L}\}det\{\mathbf{U}\}=u_{11}u_{22}\dots u_{nn}. +$$ + +

+ + +

LU Decomposition, why?

+ +

+There are at least three main advantages with LU decomposition compared with standard Gaussian elimination: + +

+ + + +

LU Decomposition, linear equations

+ +

+With the LU decomposition it is rather +simple to solve a system of linear equations + +$$ +\begin{align} + a_{11}x_1 +a_{12}x_2 +a_{13}x_3 + a_{14}x_4=&w_1 \nonumber \\ +a_{21}x_1 + a_{22}x_2 + a_{23}x_3 + a_{24}x_4=&w_2 \nonumber \\ +a_{31}x_1 + a_{32}x_2 + a_{33}x_3 + a_{34}x_4=&w_3 \nonumber \\ +a_{41}x_1 + a_{42}x_2 + a_{43}x_3 + a_{44}x_4=&w_4. \nonumber +\end{align} +$$ + +

+This can be written in matrix form as + +$$ \mathbf{Ax}=\mathbf{w}. $$ + +

+where \( \mathbf{A} \) and \( \mathbf{w} \) are known and we have to solve for +\( \mathbf{x} \). Using the LU dcomposition we write + +$$ \mathbf{A} \mathbf{x} \equiv \mathbf{L} \mathbf{U} \mathbf{x} =\mathbf{w}. $$ + +

+ + +

LU Decomposition, linear equations

+ +

+The previous equation can be calculated in two steps + +$$ \mathbf{L} \mathbf{y} = \mathbf{w};\qquad \mathbf{Ux}=\mathbf{y}. $$ + +

+To show that this is correct we use to the LU decomposition +to rewrite our system of linear equations as + +$$ \mathbf{LUx}=\mathbf{w}, $$ + +and since the determinat of \( \mathbf{L} \) is equal to 1 (by construction +since the diagonals of \( \mathbf{L} \) equal 1) we can use the inverse of +\( \mathbf{L} \) to obtain + +$$ + \mathbf{Ux}=\mathbf{L^{-1}w}=\mathbf{y}, +$$ + +which yields the intermediate step + +$$ + \mathbf{L^{-1}w}=\mathbf{y} +$$ + +and as soon as we have \( \mathbf{y} \) we can obtain \( \mathbf{x} \) +through \( \mathbf{Ux}=\mathbf{y} \). + +

+ + +

LU Decomposition, why?

+ +

+For our four-dimentional example this takes the form + +$$ +\begin{align} + y_1=&w_1 \nonumber\\ +l_{21}y_1 + y_2=&w_2\nonumber \\ +l_{31}y_1 + l_{32}y_2 + y_3 =&w_3\nonumber \\ +l_{41}y_1 + l_{42}y_2 + l_{43}y_3 + y_4=&w_4. \nonumber +\end{align} +$$ + +

+and + +$$ +\begin{align} + u_{11}x_1 +u_{12}x_2 +u_{13}x_3 + u_{14}x_4=&y_1 \nonumber\\ +u_{22}x_2 + u_{23}x_3 + u_{24}x_4=&y_2\nonumber \\ +u_{33}x_3 + u_{34}x_4=&y_3\nonumber \\ +u_{44}x_4=&y_4 \nonumber +\end{align} +$$ + +

+This example shows the basis for the algorithm +needed to solve the set of \( n \) linear equations. + +

+ + +

LU Decomposition, linear equations

+ +

+The algorithm goes as follows + +

+ + + +

LU Decomposition, the inverse of a matrix

+ +

+If the inverse exists then + +$$ + \mathbf{A}^{-1}\mathbf{A}=\mathbf{I}, +$$ + +the identity matrix. With an LU decomposed matrix we can rewrite the last equation as + +$$ + \mathbf{LU}\mathbf{A}^{-1}=\mathbf{I}. +$$ + +

+ + +

LU Decomposition, the inverse of a matrix

+ +

+If we assume that the first column (that is column 1) of the inverse matrix +can be written as a vector with unknown entries + +$$ + \mathbf{A}_1^{-1}= \begin{bmatrix} + a_{11}^{-1} \\ + a_{21}^{-1} \\ + \dots \\ + a_{n1}^{-1} \\ + \end{bmatrix}, +$$ + +then we have a linear set of equations + +$$ + \mathbf{LU}\begin{bmatrix} + a_{11}^{-1} \\ + a_{21}^{-1} \\ + \dots \\ + a_{n1}^{-1} \\ + \end{bmatrix} =\begin{bmatrix} + 1 \\ + 0 \\ + \dots \\ + 0 \\ + \end{bmatrix}. +$$ + +

+ + +

LU Decomposition, the inverse

+ +

+In a similar way we can compute the unknow entries of the second column, + +$$ + \mathbf{LU}\begin{bmatrix} + a_{12}^{-1} \\ + a_{22}^{-1} \\ + \dots \\ + a_{n2}^{-1} \\ + \end{bmatrix}=\begin{bmatrix} + 0 \\ + 1 \\ + \dots \\ + 0 \\ + \end{bmatrix}, +$$ + +and continue till we have solved all \( n \) sets of linear equations. + +

+ + +

Using Armadillo to perform an LU decomposition

+

+ + +

#include <iostream>
+#include "armadillo"
+using namespace arma;
+using namespace std;
+
+int main()
+  {
+   mat A = randu<mat>(5,5);
+   vec b = randu<vec>(5);
+
+  A.print("A =");
+  b.print("b=");
+  // solve Ax = b
+  vec x = solve(A,b);
+  // print x
+  x.print("x=");
+  // find LU decomp of A, if needed, P is the permutation matrix
+  mat L, U;
+  lu(L,U,A);
+  // print l
+  L.print(" L= ");
+  // print U
+  U.print(" U= ");
+  //Check that A = LU
+  (A-L*U).print("Test of LU decomposition");
+    return 0;
+  }
+
+

+ + +

Iterative methods, Chapter 6

+
+
+

+ +

    +
  • Direct solvers such as Gauss elimination and LU decomposition discussed in connection with project 1.
  • +
  • Iterative solvers such as Basic iterative solvers, Jacobi, Gauss-Seidel, Successive over-relaxation. These methods are easy to parallelize, as we will se later. Much used in solutions of partial differential equations.
  • +
  • Other iterative methods such as Krylov subspace methods with Generalized minimum residual (GMRES) and Conjugate gradient etc will not be discussed.
  • +
+
+
+ + +

+ + +

Iterative methods, Jacobi's method

+
+
+

+It is a simple method for solving +$$ +\mathbf{A}\mathbf{x}=\mathbf{b}, +$$ + +where \( \mathbf{A} \) is a matrix and \( \mathbf{x} \) and \( \mathbf{b} \) are vectors. The vector \( \mathbf{x} \) is +the unknown. + +

+It is an iterative scheme where we start with a guess for the unknown, and +after \( k+1 \) iterations we have +$$ +\mathbf{x}^{(k+1)}= \mathbf{D}^{-1}(\mathbf{b}-(\mathbf{L}+\mathbf{U})\mathbf{x}^{(k)}), +$$ + +with \( \mathbf{A}=\mathbf{D}+\mathbf{U}+\mathbf{L} \) and +\( \mathbf{D} \) being a diagonal matrix, \( \mathbf{U} \) an upper triangular matrix and \( \mathbf{L} \) a lower triangular +matrix. + +

+If the matrix \( \mathbf{A} \) is positive definite or diagonally dominant, one can show that this method will always converge to the exact solution. +

+
+ + +

+ + +

Iterative methods, Jacobi's method

+
+
+

+We can demonstrate Jacobi's method by this \( 4\times 4 \) matrix problem. We assume a guess +for the vector elements \( x_i^{(0)} \), a guess which represents our first iteration. The new +values are obtained by substitution +$$ +\begin{align} + x_1^{(1)} =&(b_1-a_{12}x_2^{(0)} -a_{13}x_3^{(0)} - a_{14}x_4^{(0)})/a_{11} \nonumber \\ + x_2^{(1)} =&(b_2-a_{21}x_1^{(0)} - a_{23}x_3^{(0)} - a_{24}x_4^{(0)})/a_{22} \nonumber \\ + x_3^{(1)} =&(b_3- a_{31}x_1^{(0)} -a_{32}x_2^{(0)} -a_{34}x_4^{(0)})/a_{33} \nonumber \\ + x_4^{(1)}=&(b_4-a_{41}x_1^{(0)} -a_{42}x_2^{(0)} - a_{43}x_3^{(0)})/a_{44}, \nonumber +\end{align} +$$ + +which after \( k+1 \) iterations reads +$$ +\begin{align} + x_1^{(k+1)} =&(b_1-a_{12}x_2^{(k)} -a_{13}x_3^{(k)} - a_{14}x_4^{(k)})/a_{11} \nonumber \\ + x_2^{(k+1)} =&(b_2-a_{21}x_1^{(k)} - a_{23}x_3^{(k)} - a_{24}x_4^{(k)})/a_{22} \nonumber \\ + x_3^{(k+1)} =&(b_3- a_{31}x_1^{(k)} -a_{32}x_2^{(k)} -a_{34}x_4^{(k)})/a_{33} \nonumber \\ + x_4^{(k+1)}=&(b_4-a_{41}x_1^{(k)} -a_{42}x_2^{(k)} - a_{43}x_3^{(k)})/a_{44}, \nonumber +\end{align} +$$ +

+
+ + +

+ + +

Iterative methods, Jacobi's method

+
+
+

+We can generalize the above equations to +$$ + x_i^{(k+1)}=(b_i-\sum_{j=1, j\ne i}^{n}a_{ij}x_j^{(k)})/a_{ii} +$$ + +or in an even more compact form as +$$ \mathbf{x}^{(k+1)}= \mathbf{D}^{-1}(\mathbf{b}-(\mathbf{L}+\mathbf{U})\mathbf{x}^{(k)}), +$$ + +with \( \mathbf{A}=\mathbf{D}+\mathbf{U}+\mathbf{L} \) and +\( \mathbf{D} \) being a diagonal matrix, \( \mathbf{U} \) an upper triangular matrix and \( \mathbf{L} \) a lower triangular +matrix. +

+
+ + +

+ + +

Iterative methods, Gauss-Seidel's method

+
+
+

+Our \( 4\times 4 \) matrix problem +$$ +\begin{align} + x_1^{(k+1)} =&(b_1-a_{12}x_2^{(k)} -a_{13}x_3^{(k)} - a_{14}x_4^{(k)})/a_{11} \nonumber \\ + x_2^{(k+1)} =&(b_2-a_{21}x_1^{(k)} - a_{23}x_3^{(k)} - a_{24}x_4^{(k)})/a_{22} \nonumber \\ + x_3^{(k+1)} =&(b_3- a_{31}x_1^{(k)} -a_{32}x_2^{(k)} -a_{34}x_4^{(k)})/a_{33} \nonumber \\ + x_4^{(k+1)}=&(b_4-a_{41}x_1^{(k)} -a_{42}x_2^{(k)} - a_{43}x_3^{(k)})/a_{44}, \nonumber +\end{align} +$$ + +can be rewritten as +$$ +\begin{align} + x_1^{(k+1)} =&(b_1-a_{12}x_2^{(k)} -a_{13}x_3^{(k)} - a_{14}x_4^{(k)})/a_{11} \nonumber \\ + x_2^{(k+1)} =&(b_2-a_{21}x_1^{(k+1)} - a_{23}x_3^{(k)} - a_{24}x_4^{(k)})/a_{22} \nonumber \\ + x_3^{(k+1)} =&(b_3- a_{31}x_1^{(k+1)} -a_{32}x_2^{(k+1)} -a_{34}x_4^{(k)})/a_{33} \nonumber \\ + x_4^{(k+1)}=&(b_4-a_{41}x_1^{(k+1)} -a_{42}x_2^{(k+1)} - a_{43}x_3^{(k+1)})/a_{44}, \nonumber +\end{align} +$$ + +which allows us to utilize the preceding solution (forward substitution). This improves normally the convergence +behavior and leads to the Gauss-Seidel method! +

+
+ + +

+ + +

Iterative methods, Gauss-Seidel's method

+
+
+

+We can generalize +$$ +\begin{align} + x_1^{(k+1)} =&(b_1-a_{12}x_2^{(k)} -a_{13}x_3^{(k)} - a_{14}x_4^{(k)})/a_{11} \nonumber \\ + x_2^{(k+1)} =&(b_2-a_{21}x_1^{(k+1)} - a_{23}x_3^{(k)} - a_{24}x_4^{(k)})/a_{22} \nonumber \\ + x_3^{(k+1)} =&(b_3- a_{31}x_1^{(k+1)} -a_{32}x_2^{(k+1)} -a_{34}x_4^{(k)})/a_{33} \nonumber \\ + x_4^{(k+1)}=&(b_4-a_{41}x_1^{(k+1)} -a_{42}x_2^{(k+1)} - a_{43}x_3^{(k+1)})/a_{44}, \nonumber +\end{align} +$$ + +to the following form +$$ + x^{(k+1)}_i = \frac{1}{a_{ii}} \left(b_i - \sum_{j > i}a_{ij}x^{(k)}_j - \sum_{j < i}a_{ij}x^{(k+1)}_j \right),\quad i=1,2,\ldots,n. +$$ + +The procedure is generally continued until the changes made by an iteration are below some tolerance. + +

+The convergence properties of the Jacobi method and the +Gauss-Seidel method are dependent on the matrix \( \mathbf{A} \). These methods converge when +the matrix is symmetric positive-definite, or is strictly or irreducibly diagonally dominant. +Both methods sometimes converge even if these conditions are not satisfied. +

+
+ + +

+ + +

Iterative methods, Successive over-relaxation

+
+
+

+Given a square system of n linear equations with unknown \( \mathbf x \): +$$ + \mathbf{A}\mathbf x = \mathbf b +$$ + +where +$$ + \mathbf{A}=\begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\a_{n1} & a_{n2} & \cdots & a_{nn} \end{bmatrix}, \qquad \mathbf{x} = \begin{bmatrix} x_{1} \\ x_2 \\ \vdots \\ x_n \end{bmatrix} , \qquad \mathbf{b} = \begin{bmatrix} b_{1} \\ b_2 \\ \vdots \\ b_n \end{bmatrix}. +$$ +

+
+ + +

+ + +

Iterative methods, Successive over-relaxation

+
+
+

+Then A can be decomposed into a diagonal component D, and strictly lower and upper triangular components L and U: +$$ + \mathbf{A} =\mathbf{D} + \mathbf{L} + \mathbf{U}, +$$ + +where +$$ + D = \begin{bmatrix} a_{11} & 0 & \cdots & 0 \\ 0 & a_{22} & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\0 & 0 & \cdots & a_{nn} \end{bmatrix}, \quad L = \begin{bmatrix} 0 & 0 & \cdots & 0 \\ a_{21} & 0 & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\a_{n1} & a_{n2} & \cdots & 0 \end{bmatrix}, \quad U = \begin{bmatrix} 0 & a_{12} & \cdots & a_{1n} \\ 0 & 0 & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\0 & 0 & \cdots & 0 \end{bmatrix}. +$$ + +The system of linear equations may be rewritten as: +$$ + (D+\omega L) \mathbf{x} = \omega \mathbf{b} - [\omega U + (\omega-1) D ] \mathbf{x} +$$ + +for a constant \( \omega > 1 \). +

+
+ + +

+ + +

Iterative methods, Successive over-relaxation

+
+
+

+The method of successive over-relaxation is an iterative technique that solves the left hand side of this expression for \( x \), using previous value for \( x \) on the right hand side. Analytically, this may be written as: +$$ + \mathbf{x}^{(k+1)} = (D+\omega L)^{-1} \big(\omega \mathbf{b} - [\omega U + (\omega-1) D ] \mathbf{x}^{(k)}\big). +$$ + +However, by taking advantage of the triangular form of \( (D+\omega L) \), the elements of \( x^{(k+1)} \) can be computed sequentially using forward substitution: +$$ + x^{(k+1)}_i = (1-\omega)x^{(k)}_i + \frac{\omega}{a_{ii}} \left(b_i - \sum_{j > i} a_{ij}x^{(k)}_j - \sum_{j < i} a_{ij}x^{(k+1)}_j \right),\quad i=1,2,\ldots,n. +$$ + +The choice of relaxation factor is not necessarily easy, and depends upon the properties of the coefficient matrix. For symmetric, positive-definite matrices it can be proven that \( 0 < \omega < 2 \) will lead to convergence, but we are generally interested in faster convergence rather than just convergence. +

+
+ + +

+ + + +

+ + + + + + + +
+ © 1999-2018, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license +
+ + + + + + diff --git a/doc/pub/Linalg/html/Linalg-reveal.html b/doc/pub/Linalg/html/Linalg-reveal.html new file mode 100644 index 000000000..6f8a46831 --- /dev/null +++ b/doc/pub/Linalg/html/Linalg-reveal.html @@ -0,0 +1,1716 @@ +\ + + + + + + +Data analysis and Machine Learning Lectures: Linear Algebra methods + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + +
+ + + + + + + + + + + + + + +
+ + + + +

Data analysis and Machine Learning Lectures: Linear Algebra methods

+ +

+ + +

+Morten Hjorth-Jensen [1, 2] +
+ +

 
+ + +

[1] Department of Physics, University of Oslo
+
[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University
+
+

 
+

Jan 27, 2018

+
+

+ +

+ © 1999-2018, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license +
+
+ + +
+

Important Matrix and vector handling packages

+ +

+The Numerical Recipes codes have been rewritten in Fortran 90/95 and +C/C++ by us. The original source codes are taken from the widely used +software package LAPACK, which follows two other popular packages +developed in the 1970s, namely EISPACK and LINPACK. + +

    + +

  • LINPACK: package for linear equations and least square problems.
  • + +

  • LAPACK:package for solving symmetric, unsymmetric and generalized eigenvalue problems. From LAPACK's website http://www.netlib.org it is possible to download for free all source codes from this library. Both C/C++ and Fortran versions are available.
  • + +

  • BLAS (I, II and III): (Basic Linear Algebra Subprograms) are routines that provide standard building blocks for performing basic vector and matrix operations. Blas I is vector operations, II vector-matrix operations and III matrix-matrix operations. Highly parallelized and efficient codes, all available for download from http://www.netlib.org.
  • +
+

+ +Add python material on linear algebra and array handling, text on numpy etc +

+ + +
+

Basic Matrix Features

+ +

+

+Matrix properties reminder. +

 
+$$ + \mathbf{A} = + \begin{bmatrix} a_{11} & a_{12} & a_{13} & a_{14} \\ + a_{21} & a_{22} & a_{23} & a_{24} \\ + a_{31} & a_{32} & a_{33} & a_{34} \\ + a_{41} & a_{42} & a_{43} & a_{44} + \end{bmatrix}\qquad +\mathbf{I} = + \begin{bmatrix} 1 & 0 & 0 & 0 \\ + 0 & 1 & 0 & 0 \\ + 0 & 0 & 1 & 0 \\ + 0 & 0 & 0 & 1 + \end{bmatrix} +$$ +

 
+

+
+ + +
+

Basic Matrix Features

+
+ +

+The inverse of a matrix is defined by + +

 
+$$ +\mathbf{A}^{-1} \cdot \mathbf{A} = I +$$ +

 
+

+
+ + +
+

Basic Matrix Features

+ +

+

+Matrix Properties Reminder. +

+ + + + + + + + + + + +
Relations Name matrix elements
\( A = A^{T} \) symmetric \( a_{ij} = a_{ji} \)
\( A = \left (A^{T} \right )^{-1} \) real orthogonal \( \sum_k a_{ik} a_{jk} = \sum_k a_{ki} a_{kj} = \delta_{ij} \)
\( A = A^{ * } \) real matrix \( a_{ij} = a_{ij}^{ * } \)
\( A = A^{\dagger} \) hermitian \( a_{ij} = a_{ji}^{ * } \)
\( A = \left (A^{\dagger} \right )^{-1} \) unitary \( \sum_k a_{ik} a_{jk}^{ * } = \sum_k a_{ki}^{ * } a_{kj} = \delta_{ij} \)
+ +

+
+ + +
+

Some famous Matrices

+ +
    + +

  • Diagonal if \( a_{ij}=0 \) for \( i\ne j \)
  • + +

  • Upper triangular if \( a_{ij}=0 \) for \( i > j \)
  • + +

  • Lower triangular if \( a_{ij}=0 \) for \( i < j \)
  • + +

  • Upper Hessenberg if \( a_{ij}=0 \) for \( i > j+1 \)
  • + +

  • Lower Hessenberg if \( a_{ij}=0 \) for \( i < j+1 \)
  • + +

  • Tridiagonal if \( a_{ij}=0 \) for \( |i -j| > 1 \)
  • + +

  • Lower banded with bandwidth \( p \): \( a_{ij}=0 \) for \( i > j+p \)
  • + +

  • Upper banded with bandwidth \( p \): \( a_{ij}=0 \) for \( i < j+p \)
  • + +

  • Banded, block upper triangular, block lower triangular....
  • +
+
+ + +
+

Basic Matrix Features

+ +

+

+Some Equivalent Statements. +

+For an \( N\times N \) matrix \( \mathbf{A} \) the following properties are all equivalent + +

    + +

  • If the inverse of \( \mathbf{A} \) exists, \( \mathbf{A} \) is nonsingular.
  • + +

  • The equation \( \mathbf{Ax}=0 \) implies \( \mathbf{x}=0 \).
  • + +

  • The rows of \( \mathbf{A} \) form a basis of \( R^N \).
  • + +

  • The columns of \( \mathbf{A} \) form a basis of \( R^N \).
  • + +

  • \( \mathbf{A} \) is a product of elementary matrices.
  • + +

  • \( 0 \) is not eigenvalue of \( \mathbf{A} \).
  • +
+
+
+ + +
+

Matrix Handling in C/C++, Static and Dynamical allocation

+ +

+

+Static. +

+We have an \( N\times N \) matrix A with \( N=100 \) +In C/C++ this would be defined as + +

+ + +

   int N = 100;
+   double A[100][100];
+   //   initialize all elements to zero
+   for(i=0 ; i < N ; i++) {
+      for(j=0 ; j < N ; j++) {
+         A[i][j] = 0.0;
+
+

+Note the way the matrix is organized, row-major order. +

+
+ + +
+

Matrix Handling in C/C++

+ +

+

+Row Major Order, Addition. +

+We have \( N\times N \) matrices A, B and C and we wish to +evaluate \( A=B+C \). + +

 
+$$ +\mathbf{A}= \mathbf{B}\pm\mathbf{C} \Longrightarrow a_{ij} = b_{ij}\pm c_{ij}, +$$ +

 
+ +In C/C++ this would be coded like + +

+ + +

   for(i=0 ; i < N ; i++) {
+      for(j=0 ; j < N ; j++) {
+         a[i][j] = b[i][j]+c[i][j]
+
+ +
+
+ + +
+

Matrix Handling in C/C++

+ +

+

+Row Major Order, Multiplication. +

+We have \( N\times N \) matrices A, B and C and we wish to +evaluate \( A=BC \). + +

 
+$$ +\mathbf{A}=\mathbf{BC} \Longrightarrow a_{ij} = \sum_{k=1}^{n} b_{ik}c_{kj}, +$$ +

 
+ +In C/C++ this would be coded like + +

+ + +

   for(i=0 ; i < N ; i++) {
+      for(j=0 ; j < N ; j++) {
+         for(k=0 ; k < N ; k++) {
+            a[i][j]+=b[i][k]*c[k][j];
+
+ +
+
+ + +
+

Dynamic memory allocation in C/C++

+ +

+At least three possibilities in this course + +

    + +

  • Do it yourself
  • + +

  • Use the functions provided in the library package lib.cpp
  • + +

  • Use Armadillo http://arma.sourceforgenet (a C++ linear algebra library, discussion both here and at lab).
  • +
+
+ + +
+

Matrix Handling in C/C++, Dynamic Allocation

+ +

+

+Do it yourself. +

+ + +

int N;
+double **  A;
+A = new double*[N]
+for ( i = 0; i < N; i++)
+    A[i] = new double[N];
+
+

+Always free space when you don't need an array anymore. + +

+ + +

for ( i = 0; i < N; i++)
+    delete[] A[i];
+delete[] A;
+
+ +
+
+ + +
+

Armadillo, recommended!!

+ +
    + +

  • Armadillo is a C++ linear algebra library (matrix maths) aiming towards a good balance between speed and ease of use. The syntax is deliberately similar to Matlab.
  • + +

  • Integer, floating point and complex numbers are supported, as well as a subset of trigonometric and statistics functions. Various matrix decompositions are provided through optional integration with LAPACK, or one of its high performance drop-in replacements (such as the multi-threaded MKL or ACML libraries).
  • + +

  • A delayed evaluation approach is employed (at compile-time) to combine several operations into one and reduce (or eliminate) the need for temporaries. This is accomplished through recursive templates and template meta-programming.
  • + +

  • Useful for conversion of research code into production environments, or if C++ has been decided as the language of choice, due to speed and/or integration capabilities.
  • + +

  • The library is open-source software, and is distributed under a license that is useful in both open-source and commercial/proprietary contexts.
  • +
+
+ + +
+

Armadillo, simple examples

+ +

+ + +

#include <iostream>
+#include <armadillo>
+
+using namespace std;
+using namespace arma;
+
+int main(int argc, char** argv)
+  {
+  mat A = randu<mat>(5,5);
+  mat B = randu<mat>(5,5);
+
+  cout << A*B << endl;
+
+  return 0;
+
+
+ + +
+

Armadillo, how to compile and install

+ +

+For people using Ubuntu, Debian, Linux Mint, simply go to the synaptic package manager and install +armadillo from there. +You may have to install Lapack as well. +For Mac and Windows users, follow the instructions from the webpage +http://arma.sourceforge.net. +To compile, use for example (linux/ubuntu) + +

+ + +

c++ -O2 -o program.x program.cpp  -larmadillo -llapack -lblas
+
+

+where the -l option indicates the library you wish to link to. + +

+For OS X users you may have to declare the paths to the include files and the libraries as +

+ + +

c++ -O2 -o program.x program.cpp  -L/usr/local/lib -I/usr/local/include -larmadillo -llapack -lblas
+
+
+ + +
+

Armadillo, simple examples

+ +

+ + +

#include <iostream>
+#include "armadillo"
+using namespace arma;
+using namespace std;
+
+int main(int argc, char** argv)
+  {
+  // directly specify the matrix size (elements are uninitialised)
+  mat A(2,3);
+  // .n_rows = number of rows    (read only)
+  // .n_cols = number of columns (read only)
+  cout << "A.n_rows = " << A.n_rows << endl;
+  cout << "A.n_cols = " << A.n_cols << endl;
+  // directly access an element (indexing starts at 0)
+  A(1,2) = 456.0;
+  A.print("A:");
+  // scalars are treated as a 1x1 matrix,
+  // hence the code below will set A to have a size of 1x1
+  A = 5.0;
+  A.print("A:");
+  // if you want a matrix with all elements set to a particular value
+  // the .fill() member function can be used
+  A.set_size(3,3);
+  A.fill(5.0);  A.print("A:");
+
+
+ + +
+

Armadillo, simple examples

+ +

+ + +

  mat B;
+
+  // endr indicates "end of row"
+  B << 0.555950 << 0.274690 << 0.540605 << 0.798938 << endr
+    << 0.108929 << 0.830123 << 0.891726 << 0.895283 << endr
+    << 0.948014 << 0.973234 << 0.216504 << 0.883152 << endr
+    << 0.023787 << 0.675382 << 0.231751 << 0.450332 << endr;
+
+  // print to the cout stream
+  // with an optional string before the contents of the matrix
+  B.print("B:");
+
+  // the << operator can also be used to print the matrix
+  // to an arbitrary stream (cout in this case)
+  cout << "B:" << endl << B << endl;
+  // save to disk
+  B.save("B.txt", raw_ascii);
+  // load from disk
+  mat C;
+  C.load("B.txt");
+  C += 2.0 * B;
+  C.print("C:");
+
+
+ + +
+

Armadillo, simple examples

+ +

+ + +

  // submatrix types:
+  //
+  // .submat(first_row, first_column, last_row, last_column)
+  // .row(row_number)
+  // .col(column_number)
+  // .cols(first_column, last_column)
+  // .rows(first_row, last_row)
+
+  cout << "C.submat(0,0,3,1) =" << endl;
+  cout << C.submat(0,0,3,1) << endl;
+
+  // generate the identity matrix
+  mat D = eye<mat>(4,4);
+
+  D.submat(0,0,3,1) = C.cols(1,2);
+  D.print("D:");
+
+  // transpose
+  cout << "trans(B) =" << endl;
+  cout << trans(B) << endl;
+
+  // maximum from each column (traverse along rows)
+  cout << "max(B) =" << endl;
+  cout << max(B) << endl;
+
+
+ + +
+

Armadillo, simple examples

+ +

+ + +

  // maximum from each row (traverse along columns)
+  cout << "max(B,1) =" << endl;
+  cout << max(B,1) << endl;
+  // maximum value in B
+  cout << "max(max(B)) = " << max(max(B)) << endl;
+  // sum of each column (traverse along rows)
+  cout << "sum(B) =" << endl;
+  cout << sum(B) << endl;
+  // sum of each row (traverse along columns)
+  cout << "sum(B,1) =" << endl;
+  cout << sum(B,1) << endl;
+  // sum of all elements
+  cout << "sum(sum(B)) = " << sum(sum(B)) << endl;
+  cout << "accu(B)     = " << accu(B) << endl;
+  // trace = sum along diagonal
+  cout << "trace(B)    = " << trace(B) << endl;
+  // random matrix -- values are uniformly distributed in the [0,1] interval
+  mat E = randu<mat>(4,4);
+  E.print("E:");
+
+
+ + +
+

Armadillo, simple examples

+ +

+ + +

  // row vectors are treated like a matrix with one row
+  rowvec r;
+  r << 0.59499 << 0.88807 << 0.88532 << 0.19968;
+  r.print("r:");
+
+  // column vectors are treated like a matrix with one column
+  colvec q;
+  q << 0.81114 << 0.06256 << 0.95989 << 0.73628;
+  q.print("q:");
+
+  // dot or inner product
+  cout << "as_scalar(r*q) = " << as_scalar(r*q) << endl;
+
+    // outer product
+  cout << "q*r =" << endl;
+  cout << q*r << endl;
+
+
+  // sum of three matrices (no temporary matrices are created)
+  mat F = B + C + D;
+  F.print("F:");
+
+    return 0;
+
+
+ + +
+

Armadillo, simple examples

+ +

+ + +

#include <iostream>
+#include "armadillo"
+using namespace arma;
+using namespace std;
+
+int main(int argc, char** argv)
+  {
+  cout << "Armadillo version: " << arma_version::as_string() << endl;
+
+  mat A;
+
+  A << 0.165300 << 0.454037 << 0.995795 << 0.124098 << 0.047084 << endr
+    << 0.688782 << 0.036549 << 0.552848 << 0.937664 << 0.866401 << endr
+    << 0.348740 << 0.479388 << 0.506228 << 0.145673 << 0.491547 << endr
+    << 0.148678 << 0.682258 << 0.571154 << 0.874724 << 0.444632 << endr
+    << 0.245726 << 0.595218 << 0.409327 << 0.367827 << 0.385736 << endr;
+
+  A.print("A =");
+
+  // determinant
+  cout << "det(A) = " << det(A) << endl;
+
+
+ + +
+

Armadillo, simple examples

+ +

+ + +

  // inverse
+  cout << "inv(A) = " << endl << inv(A) << endl;
+  double k = 1.23;
+
+  mat    B = randu<mat>(5,5);
+  mat    C = randu<mat>(5,5);
+
+  rowvec r = randu<rowvec>(5);
+  colvec q = randu<colvec>(5);
+
+
+  // examples of some expressions
+  // for which optimised implementations exist
+  // optimised implementation of a trinary expression
+  // that results in a scalar
+  cout << "as_scalar( r*inv(diagmat(B))*q ) = ";
+  cout << as_scalar( r*inv(diagmat(B))*q ) << endl;
+
+  // example of an expression which is optimised
+  // as a call to the dgemm() function in BLAS:
+  cout << "k*trans(B)*C = " << endl << k*trans(B)*C;
+
+    return 0;
+
+
+ + +
+

Gaussian Elimination

+ +

+We start with the linear set of equations + +

 
+$$ + \mathbf{A}\mathbf{x} = \mathbf{w}. +$$ +

 
+ +We assume also that the matrix \( \mathbf{A} \) is non-singular and that the +matrix elements along the diagonal satisfy \( a_{ii} \ne 0 \). Simple \( 4\times 4 \) example + +

 
+$$ +\begin{bmatrix} + a_{11}& a_{12} &a_{13}& a_{14}\\ + a_{21}& a_{22} &a_{23}& a_{24}\\ + a_{31}& a_{32} &a_{33}& a_{34}\\ + a_{41}& a_{42} &a_{43}& a_{44}\\ + \end{bmatrix} \begin{bmatrix} + x_1\\ + x_2\\ + x_3 \\ + x_4 \\ + \end{bmatrix} + =\begin{bmatrix} + w_1\\ + w_2\\ + w_3 \\ + w_4\\ + \end{bmatrix}. +$$ +

 
+

+ + +
+

Gaussian Elimination

+or + +

 
+$$ +\begin{align} + a_{11}x_1 +a_{12}x_2 +a_{13}x_3 + a_{14}x_4=&w_1 \nonumber \\ +a_{21}x_1 + a_{22}x_2 + a_{23}x_3 + a_{24}x_4=&w_2 \nonumber \\ +a_{31}x_1 + a_{32}x_2 + a_{33}x_3 + a_{34}x_4=&w_3 \nonumber \\ +a_{41}x_1 + a_{42}x_2 + a_{43}x_3 + a_{44}x_4=&w_4. \nonumber +\end{align} +$$ +

 
+

+ + +
+

Gaussian Elimination

+ +

+The basic idea of Gaussian elimination is to use the first equation to eliminate the first unknown \( x_1 \) +from the remaining \( n-1 \) equations. Then we use the new second equation to eliminate the second unknown +\( x_2 \) from the remaining \( n-2 \) equations. With \( n-1 \) such eliminations +we obtain a so-called upper triangular set of equations of the form + +

 
+$$ +\begin{align} + b_{11}x_1 +b_{12}x_2 +b_{13}x_3 + b_{14}x_4=&y_1 \nonumber \\ + b_{22}x_2 + b_{23}x_3 + b_{24}x_4=&y_2 \nonumber \\ +b_{33}x_3 + b_{34}x_4=&y_3 \nonumber \\ +b_{44}x_4=&y_4. \nonumber +\tag{1} +\end{align} +$$ +

 
+ +We can solve this system of equations recursively starting from \( x_n \) (in our case \( x_4 \)) and proceed with +what is called a backward substitution. +

+ + +
+

Gaussian Elimination

+This process can be expressed mathematically as + +

 
+$$ +\begin{equation} + x_m = \frac{1}{b_{mm}}\left(y_m-\sum_{k=m+1}^nb_{mk}x_k\right)\quad m=n-1,n-2,\dots,1. +\tag{2} +\end{equation} +$$ +

 
+ +To arrive at such an upper triangular system of equations, we start by eliminating +the unknown \( x_1 \) for \( j=2,n \). We achieve this by multiplying the first equation by \( a_{j1}/a_{11} \) and then subtract +the result from the $j$th equation. We assume obviously that \( a_{11}\ne 0 \) and that +\( \mathbf{A} \) is not singular. +

+ + +
+

Gaussian Elimination

+ +

+Our actual \( 4\times 4 \) example reads after the first operation + +

 
+$$ +\begin{bmatrix} + a_{11}& a_{12} &a_{13}& a_{14}\\ + 0& (a_{22}-\frac{a_{21}a_{12}}{a_{11}}) &(a_{23}-\frac{a_{21}a_{13}}{a_{11}}) & (a_{24}-\frac{a_{21}a_{14}}{a_{11}})\\ +0& (a_{32}-\frac{a_{31}a_{12}}{a_{11}})& (a_{33}-\frac{a_{31}a_{13}}{a_{11}})& (a_{34}-\frac{a_{31}a_{14}}{a_{11}})\\ +0&(a_{42}-\frac{a_{41}a_{12}}{a_{11}}) &(a_{43}-\frac{a_{41}a_{13}}{a_{11}}) & (a_{44}-\frac{a_{41}a_{14}}{a_{11}}) \\ + \end{bmatrix} \begin{bmatrix} + x_1\\ + x_2\\ + x_3 \\ + x_4 \\ + \end{bmatrix} + =\begin{bmatrix} + y_1\\ + w_2^{(2)}\\ + w_3^{(2)} \\ + w_4^{(2)}\\ + \end{bmatrix}, +$$ +

 
+ +or + +

 
+$$ +\begin{align} + b_{11}x_1 +b_{12}x_2 +b_{13}x_3 + b_{14}x_4=&y_1 \nonumber \\ + a^{(2)}_{22}x_2 + a^{(2)}_{23}x_3 + a^{(2)}_{24}x_4=&w^{(2)}_2 \nonumber \\ + a^{(2)}_{32}x_2 + a^{(2)}_{33}x_3 + a^{(2)}_{34}x_4=&w^{(2)}_3 \nonumber \\ + a^{(2)}_{42}x_2 + a^{(2)}_{43}x_3 + a^{(2)}_{44}x_4=&w^{(2)}_4, \nonumber \\ +\tag{3} +\end{align} +$$ +

 
+

+ + +
+

Gaussian Elimination

+ +

+The new coefficients are + +

 
+$$ +\begin{equation} + b_{1k} = a_{1k}^{(1)} \quad k=1,\dots,n, +\tag{4} +\end{equation} +$$ +

 
+ +where each \( a_{1k}^{(1)} \) is equal to the original \( a_{1k} \) element. The other coefficients are + +

 
+$$ +\begin{equation} +a_{jk}^{(2)} = a_{jk}^{(1)}-\frac{a_{j1}^{(1)}a_{1k}^{(1)}}{a_{11}^{(1)}} \quad j,k=2,\dots,n, +\tag{5} +\end{equation} +$$ +

 
+ +with a new right-hand side given by + +

 
+$$ +\begin{equation} +y_{1}=w_1^{(1)}, \quad w_j^{(2)} =w_j^{(1)}-\frac{a_{j1}^{(1)}w_1^{(1)}}{a_{11}^{(1)}} \quad j=2,\dots,n. +\tag{6} +\end{equation} +$$ +

 
+ +We have also set \( w_1^{(1)}=w_1 \), the original vector element. +We see that the system of unknowns \( x_1,\dots,x_n \) is transformed into an \( (n-1)\times (n-1) \) problem. +

+ + +
+

Gaussian Elimination

+ +

+This step is called forward substitution. +Proceeding with these substitutions, we obtain the +general expressions for the new coefficients + +

 
+$$ +\begin{equation} + a_{jk}^{(m+1)} = a_{jk}^{(m)}-\frac{a_{jm}^{(m)}a_{mk}^{(m)}}{a_{mm}^{(m)}} \quad j,k=m+1,\dots,n, +\tag{7} +\end{equation} +$$ +

 
+ +with \( m=1,\dots,n-1 \) and a +right-hand side given by + +

 
+$$ +\begin{equation} + w_j^{(m+1)} =w_j^{(m)}-\frac{a_{jm}^{(m)}w_m^{(m)}}{a_{mm}^{(m)}}\quad j=m+1,\dots,n. +\tag{8} +\end{equation} +$$ +

 
+ +This set of \( n-1 \) elimations leads us to an equations which is solved by back substitution. +If the arithmetics is exact and the matrix \( \mathbf{A} \) is not singular, then the computed answer will be exact. + +

+Even though the matrix elements along the diagonal are not zero, +numerically small numbers may appear and subsequent divisions may lead to large numbers, which, if added +to a small number may yield losses of precision. Suppose for example that our first division in \( (a_{22}-a_{21}a_{12}/a_{11}) \) +results in \( -10^{-7} \) and that \( a_{22} \) is one. +one. We are then +adding \( 10^7+1 \). With single precision this results in \( 10^7 \). +

+ + +
+

Linear Algebra Methods

+ +
    + +

  • Gaussian elimination, \( O(2/3n^3) \) flops, general matrix
  • + +

  • LU decomposition, upper triangular and lower tridiagonal matrices, \( O(2/3n^3) \) flops, general matrix. Get easily the inverse, determinant and can solve linear equations with back-substitution only, \( O(n^2) \) flops
  • + +

  • Cholesky decomposition. Real symmetric or hermitian positive definite matrix, \( O(1/3n^3) \) flops.
  • + +

  • Tridiagonal linear systems, important for differential equations. Normally positive definite and non-singular. \( O(8n) \) flops for symmetric. Special case of banded matrices.
  • + +

  • Singular value decomposition
  • + +

  • the QR method will be discussed in chapter 7 in connection with eigenvalue systems. \( O(4/3n^3) \) flops.
  • +
+
+ + +
+

LU Decomposition

+ +

+The LU decomposition method means that we can rewrite +this matrix as the product of two matrices \( \mathbf{L} \) and \( \mathbf{U} \) +where + +

 
+$$ + \begin{bmatrix} + a_{11} & a_{12} & a_{13} & a_{14} \\ + a_{21} & a_{22} & a_{23} & a_{24} \\ + a_{31} & a_{32} & a_{33} & a_{34} \\ + a_{41} & a_{42} & a_{43} & a_{44} + \end{bmatrix} + = \begin{bmatrix} + 1 & 0 & 0 & 0 \\ + l_{21} & 1 & 0 & 0 \\ + l_{31} & l_{32} & 1 & 0 \\ + l_{41} & l_{42} & l_{43} & 1 + \end{bmatrix} + \begin{bmatrix} + u_{11} & u_{12} & u_{13} & u_{14} \\ + 0 & u_{22} & u_{23} & u_{24} \\ + 0 & 0 & u_{33} & u_{34} \\ + 0 & 0 & 0 & u_{44} + \end{bmatrix}. +$$ +

 
+

+ + +
+

LU Decomposition

+ +

+LU decomposition forms the backbone of other algorithms in linear algebra, such as the +solution of linear equations given by + +

 
+$$ +\begin{align} + a_{11}x_1 +a_{12}x_2 +a_{13}x_3 + a_{14}x_4=&w_1 \nonumber \\ +a_{21}x_1 + a_{22}x_2 + a_{23}x_3 + a_{24}x_4=&w_2 \nonumber \\ +a_{31}x_1 + a_{32}x_2 + a_{33}x_3 + a_{34}x_4=&w_3 \nonumber \\ +a_{41}x_1 + a_{42}x_2 + a_{43}x_3 + a_{44}x_4=&w_4. \nonumber +\end{align} +$$ +

 
+ +The above set of equations is conveniently solved by using LU decomposition as an intermediate step. + +

+The matrix \( \mathbf{A}\in \mathbb{R}^{n\times n} \) has an LU factorization if the determinant +is different from zero. If the LU factorization exists and \( \mathbf{A} \) is non-singular, then the LU factorization +is unique and the determinant is given by + +

 
+$$ +det\{\mathbf{A}\}=det\{\mathbf{LU}\}= det\{\mathbf{L}\}det\{\mathbf{U}\}=u_{11}u_{22}\dots u_{nn}. +$$ +

 
+

+ + +
+

LU Decomposition, why?

+ +

+There are at least three main advantages with LU decomposition compared with standard Gaussian elimination: + +

    + +

  • It is straightforward to compute the determinant of a matrix
  • + +

  • If we have to solve sets of linear equations with the same matrix but with different vectors \( \mathbf{y} \), the number of FLOPS is of the order \( n^3 \).
  • + +

  • The inverse is such an operation
  • +
+
+ + +
+

LU Decomposition, linear equations

+ +

+With the LU decomposition it is rather +simple to solve a system of linear equations + +

 
+$$ +\begin{align} + a_{11}x_1 +a_{12}x_2 +a_{13}x_3 + a_{14}x_4=&w_1 \nonumber \\ +a_{21}x_1 + a_{22}x_2 + a_{23}x_3 + a_{24}x_4=&w_2 \nonumber \\ +a_{31}x_1 + a_{32}x_2 + a_{33}x_3 + a_{34}x_4=&w_3 \nonumber \\ +a_{41}x_1 + a_{42}x_2 + a_{43}x_3 + a_{44}x_4=&w_4. \nonumber +\end{align} +$$ +

 
+ +

+This can be written in matrix form as + +

 
+$$ \mathbf{Ax}=\mathbf{w}. $$ +

 
+ +

+where \( \mathbf{A} \) and \( \mathbf{w} \) are known and we have to solve for +\( \mathbf{x} \). Using the LU dcomposition we write + +

 
+$$ \mathbf{A} \mathbf{x} \equiv \mathbf{L} \mathbf{U} \mathbf{x} =\mathbf{w}. $$ +

 
+

+ + +
+

LU Decomposition, linear equations

+ +

+The previous equation can be calculated in two steps + +

 
+$$ \mathbf{L} \mathbf{y} = \mathbf{w};\qquad \mathbf{Ux}=\mathbf{y}. $$ +

 
+ +

+To show that this is correct we use to the LU decomposition +to rewrite our system of linear equations as + +

 
+$$ \mathbf{LUx}=\mathbf{w}, $$ +

 
+ +and since the determinat of \( \mathbf{L} \) is equal to 1 (by construction +since the diagonals of \( \mathbf{L} \) equal 1) we can use the inverse of +\( \mathbf{L} \) to obtain + +

 
+$$ + \mathbf{Ux}=\mathbf{L^{-1}w}=\mathbf{y}, +$$ +

 
+ +which yields the intermediate step + +

 
+$$ + \mathbf{L^{-1}w}=\mathbf{y} +$$ +

 
+ +and as soon as we have \( \mathbf{y} \) we can obtain \( \mathbf{x} \) +through \( \mathbf{Ux}=\mathbf{y} \). +

+ + +
+

LU Decomposition, why?

+ +

+For our four-dimentional example this takes the form + +

 
+$$ +\begin{align} + y_1=&w_1 \nonumber\\ +l_{21}y_1 + y_2=&w_2\nonumber \\ +l_{31}y_1 + l_{32}y_2 + y_3 =&w_3\nonumber \\ +l_{41}y_1 + l_{42}y_2 + l_{43}y_3 + y_4=&w_4. \nonumber +\end{align} +$$ +

 
+ +

+and + +

 
+$$ +\begin{align} + u_{11}x_1 +u_{12}x_2 +u_{13}x_3 + u_{14}x_4=&y_1 \nonumber\\ +u_{22}x_2 + u_{23}x_3 + u_{24}x_4=&y_2\nonumber \\ +u_{33}x_3 + u_{34}x_4=&y_3\nonumber \\ +u_{44}x_4=&y_4 \nonumber +\end{align} +$$ +

 
+ +

+This example shows the basis for the algorithm +needed to solve the set of \( n \) linear equations. +

+ + +
+

LU Decomposition, linear equations

+ +

+The algorithm goes as follows + +

    + +

  • Set up the matrix \( \bf A \) and the vector \( \bf w \) with their correct dimensions. This determines the dimensionality of the unknown vector \( \bf x \).
  • + +

  • Then LU decompose the matrix \( \bf A \) through a call to the function ludcmp(double a, int n, int indx, double &d). This functions returns the LU decomposed matrix \( \bf A \), its determinant and the vector indx which keeps track of the number of interchanges of rows. If the determinant is zero, the solution is malconditioned.
  • + +

  • Thereafter you call the function lubksb(double a, int n, int indx, double w) which uses the LU decomposed matrix \( \bf A \) and the vector \( \bf w \) and returns \( \bf x \) in the same place as \( \bf w \). Upon exit the original content in \( \bf w \) is destroyed. If you wish to keep this information, you should make a backup of it in your calling function.
  • +
+
+ + +
+

LU Decomposition, the inverse of a matrix

+ +

+If the inverse exists then + +

 
+$$ + \mathbf{A}^{-1}\mathbf{A}=\mathbf{I}, +$$ +

 
+ +the identity matrix. With an LU decomposed matrix we can rewrite the last equation as + +

 
+$$ + \mathbf{LU}\mathbf{A}^{-1}=\mathbf{I}. +$$ +

 
+

+ + +
+

LU Decomposition, the inverse of a matrix

+ +

+If we assume that the first column (that is column 1) of the inverse matrix +can be written as a vector with unknown entries + +

 
+$$ + \mathbf{A}_1^{-1}= \begin{bmatrix} + a_{11}^{-1} \\ + a_{21}^{-1} \\ + \dots \\ + a_{n1}^{-1} \\ + \end{bmatrix}, +$$ +

 
+ +then we have a linear set of equations + +

 
+$$ + \mathbf{LU}\begin{bmatrix} + a_{11}^{-1} \\ + a_{21}^{-1} \\ + \dots \\ + a_{n1}^{-1} \\ + \end{bmatrix} =\begin{bmatrix} + 1 \\ + 0 \\ + \dots \\ + 0 \\ + \end{bmatrix}. +$$ +

 
+

+ + +
+

LU Decomposition, the inverse

+ +

+In a similar way we can compute the unknow entries of the second column, + +

 
+$$ + \mathbf{LU}\begin{bmatrix} + a_{12}^{-1} \\ + a_{22}^{-1} \\ + \dots \\ + a_{n2}^{-1} \\ + \end{bmatrix}=\begin{bmatrix} + 0 \\ + 1 \\ + \dots \\ + 0 \\ + \end{bmatrix}, +$$ +

 
+ +and continue till we have solved all \( n \) sets of linear equations. +

+ + +
+

Using Armadillo to perform an LU decomposition

+

+ + +

#include <iostream>
+#include "armadillo"
+using namespace arma;
+using namespace std;
+
+int main()
+  {
+   mat A = randu<mat>(5,5);
+   vec b = randu<vec>(5);
+
+  A.print("A =");
+  b.print("b=");
+  // solve Ax = b
+  vec x = solve(A,b);
+  // print x
+  x.print("x=");
+  // find LU decomp of A, if needed, P is the permutation matrix
+  mat L, U;
+  lu(L,U,A);
+  // print l
+  L.print(" L= ");
+  // print U
+  U.print(" U= ");
+  //Check that A = LU
+  (A-L*U).print("Test of LU decomposition");
+    return 0;
+  }
+
+
+ + +
+

Iterative methods, Chapter 6

+
+ +
    +

  • Direct solvers such as Gauss elimination and LU decomposition discussed in connection with project 1.
  • +

  • Iterative solvers such as Basic iterative solvers, Jacobi, Gauss-Seidel, Successive over-relaxation. These methods are easy to parallelize, as we will se later. Much used in solutions of partial differential equations.
  • +

  • Other iterative methods such as Krylov subspace methods with Generalized minimum residual (GMRES) and Conjugate gradient etc will not be discussed.
  • +
+
+
+ + +
+

Iterative methods, Jacobi's method

+
+ +

+It is a simple method for solving +

 
+$$ +\mathbf{A}\mathbf{x}=\mathbf{b}, +$$ +

 
+ +where \( \mathbf{A} \) is a matrix and \( \mathbf{x} \) and \( \mathbf{b} \) are vectors. The vector \( \mathbf{x} \) is +the unknown. + +

+It is an iterative scheme where we start with a guess for the unknown, and +after \( k+1 \) iterations we have + +

 
+$$ +\mathbf{x}^{(k+1)}= \mathbf{D}^{-1}(\mathbf{b}-(\mathbf{L}+\mathbf{U})\mathbf{x}^{(k)}), +$$ +

 
+ +with \( \mathbf{A}=\mathbf{D}+\mathbf{U}+\mathbf{L} \) and +\( \mathbf{D} \) being a diagonal matrix, \( \mathbf{U} \) an upper triangular matrix and \( \mathbf{L} \) a lower triangular +matrix. + +

+If the matrix \( \mathbf{A} \) is positive definite or diagonally dominant, one can show that this method will always converge to the exact solution. +

+
+ + +
+

Iterative methods, Jacobi's method

+
+ +

+We can demonstrate Jacobi's method by this \( 4\times 4 \) matrix problem. We assume a guess +for the vector elements \( x_i^{(0)} \), a guess which represents our first iteration. The new +values are obtained by substitution +

 
+$$ +\begin{align} + x_1^{(1)} =&(b_1-a_{12}x_2^{(0)} -a_{13}x_3^{(0)} - a_{14}x_4^{(0)})/a_{11} \nonumber \\ + x_2^{(1)} =&(b_2-a_{21}x_1^{(0)} - a_{23}x_3^{(0)} - a_{24}x_4^{(0)})/a_{22} \nonumber \\ + x_3^{(1)} =&(b_3- a_{31}x_1^{(0)} -a_{32}x_2^{(0)} -a_{34}x_4^{(0)})/a_{33} \nonumber \\ + x_4^{(1)}=&(b_4-a_{41}x_1^{(0)} -a_{42}x_2^{(0)} - a_{43}x_3^{(0)})/a_{44}, \nonumber +\end{align} +$$ +

 
+ +which after \( k+1 \) iterations reads +

 
+$$ +\begin{align} + x_1^{(k+1)} =&(b_1-a_{12}x_2^{(k)} -a_{13}x_3^{(k)} - a_{14}x_4^{(k)})/a_{11} \nonumber \\ + x_2^{(k+1)} =&(b_2-a_{21}x_1^{(k)} - a_{23}x_3^{(k)} - a_{24}x_4^{(k)})/a_{22} \nonumber \\ + x_3^{(k+1)} =&(b_3- a_{31}x_1^{(k)} -a_{32}x_2^{(k)} -a_{34}x_4^{(k)})/a_{33} \nonumber \\ + x_4^{(k+1)}=&(b_4-a_{41}x_1^{(k)} -a_{42}x_2^{(k)} - a_{43}x_3^{(k)})/a_{44}, \nonumber +\end{align} +$$ +

 
+

+
+ + +
+

Iterative methods, Jacobi's method

+
+ +

+We can generalize the above equations to +

 
+$$ + x_i^{(k+1)}=(b_i-\sum_{j=1, j\ne i}^{n}a_{ij}x_j^{(k)})/a_{ii} +$$ +

 
+ +or in an even more compact form as +

 
+$$ \mathbf{x}^{(k+1)}= \mathbf{D}^{-1}(\mathbf{b}-(\mathbf{L}+\mathbf{U})\mathbf{x}^{(k)}), +$$ +

 
+ +with \( \mathbf{A}=\mathbf{D}+\mathbf{U}+\mathbf{L} \) and +\( \mathbf{D} \) being a diagonal matrix, \( \mathbf{U} \) an upper triangular matrix and \( \mathbf{L} \) a lower triangular +matrix. +

+
+ + +
+

Iterative methods, Gauss-Seidel's method

+
+ +

+Our \( 4\times 4 \) matrix problem +

 
+$$ +\begin{align} + x_1^{(k+1)} =&(b_1-a_{12}x_2^{(k)} -a_{13}x_3^{(k)} - a_{14}x_4^{(k)})/a_{11} \nonumber \\ + x_2^{(k+1)} =&(b_2-a_{21}x_1^{(k)} - a_{23}x_3^{(k)} - a_{24}x_4^{(k)})/a_{22} \nonumber \\ + x_3^{(k+1)} =&(b_3- a_{31}x_1^{(k)} -a_{32}x_2^{(k)} -a_{34}x_4^{(k)})/a_{33} \nonumber \\ + x_4^{(k+1)}=&(b_4-a_{41}x_1^{(k)} -a_{42}x_2^{(k)} - a_{43}x_3^{(k)})/a_{44}, \nonumber +\end{align} +$$ +

 
+ +can be rewritten as +

 
+$$ +\begin{align} + x_1^{(k+1)} =&(b_1-a_{12}x_2^{(k)} -a_{13}x_3^{(k)} - a_{14}x_4^{(k)})/a_{11} \nonumber \\ + x_2^{(k+1)} =&(b_2-a_{21}x_1^{(k+1)} - a_{23}x_3^{(k)} - a_{24}x_4^{(k)})/a_{22} \nonumber \\ + x_3^{(k+1)} =&(b_3- a_{31}x_1^{(k+1)} -a_{32}x_2^{(k+1)} -a_{34}x_4^{(k)})/a_{33} \nonumber \\ + x_4^{(k+1)}=&(b_4-a_{41}x_1^{(k+1)} -a_{42}x_2^{(k+1)} - a_{43}x_3^{(k+1)})/a_{44}, \nonumber +\end{align} +$$ +

 
+ +which allows us to utilize the preceding solution (forward substitution). This improves normally the convergence +behavior and leads to the Gauss-Seidel method! +

+
+ + +
+

Iterative methods, Gauss-Seidel's method

+
+ +

+We can generalize +

 
+$$ +\begin{align} + x_1^{(k+1)} =&(b_1-a_{12}x_2^{(k)} -a_{13}x_3^{(k)} - a_{14}x_4^{(k)})/a_{11} \nonumber \\ + x_2^{(k+1)} =&(b_2-a_{21}x_1^{(k+1)} - a_{23}x_3^{(k)} - a_{24}x_4^{(k)})/a_{22} \nonumber \\ + x_3^{(k+1)} =&(b_3- a_{31}x_1^{(k+1)} -a_{32}x_2^{(k+1)} -a_{34}x_4^{(k)})/a_{33} \nonumber \\ + x_4^{(k+1)}=&(b_4-a_{41}x_1^{(k+1)} -a_{42}x_2^{(k+1)} - a_{43}x_3^{(k+1)})/a_{44}, \nonumber +\end{align} +$$ +

 
+ +to the following form +

 
+$$ + x^{(k+1)}_i = \frac{1}{a_{ii}} \left(b_i - \sum_{j > i}a_{ij}x^{(k)}_j - \sum_{j < i}a_{ij}x^{(k+1)}_j \right),\quad i=1,2,\ldots,n. +$$ +

 
+ +The procedure is generally continued until the changes made by an iteration are below some tolerance. + +

+The convergence properties of the Jacobi method and the +Gauss-Seidel method are dependent on the matrix \( \mathbf{A} \). These methods converge when +the matrix is symmetric positive-definite, or is strictly or irreducibly diagonally dominant. +Both methods sometimes converge even if these conditions are not satisfied. +

+
+ + +
+

Iterative methods, Successive over-relaxation

+
+ +

+Given a square system of n linear equations with unknown \( \mathbf x \): +

 
+$$ + \mathbf{A}\mathbf x = \mathbf b +$$ +

 
+ +where +

 
+$$ + \mathbf{A}=\begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\a_{n1} & a_{n2} & \cdots & a_{nn} \end{bmatrix}, \qquad \mathbf{x} = \begin{bmatrix} x_{1} \\ x_2 \\ \vdots \\ x_n \end{bmatrix} , \qquad \mathbf{b} = \begin{bmatrix} b_{1} \\ b_2 \\ \vdots \\ b_n \end{bmatrix}. +$$ +

 
+

+
+ + +
+

Iterative methods, Successive over-relaxation

+
+ +

+Then A can be decomposed into a diagonal component D, and strictly lower and upper triangular components L and U: +

 
+$$ + \mathbf{A} =\mathbf{D} + \mathbf{L} + \mathbf{U}, +$$ +

 
+ +where +

 
+$$ + D = \begin{bmatrix} a_{11} & 0 & \cdots & 0 \\ 0 & a_{22} & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\0 & 0 & \cdots & a_{nn} \end{bmatrix}, \quad L = \begin{bmatrix} 0 & 0 & \cdots & 0 \\ a_{21} & 0 & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\a_{n1} & a_{n2} & \cdots & 0 \end{bmatrix}, \quad U = \begin{bmatrix} 0 & a_{12} & \cdots & a_{1n} \\ 0 & 0 & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\0 & 0 & \cdots & 0 \end{bmatrix}. +$$ +

 
+ +The system of linear equations may be rewritten as: +

 
+$$ + (D+\omega L) \mathbf{x} = \omega \mathbf{b} - [\omega U + (\omega-1) D ] \mathbf{x} +$$ +

 
+ +for a constant \( \omega > 1 \). +

+
+ + +
+

Iterative methods, Successive over-relaxation

+
+ +

+The method of successive over-relaxation is an iterative technique that solves the left hand side of this expression for \( x \), using previous value for \( x \) on the right hand side. Analytically, this may be written as: +

 
+$$ + \mathbf{x}^{(k+1)} = (D+\omega L)^{-1} \big(\omega \mathbf{b} - [\omega U + (\omega-1) D ] \mathbf{x}^{(k)}\big). +$$ +

 
+ +However, by taking advantage of the triangular form of \( (D+\omega L) \), the elements of \( x^{(k+1)} \) can be computed sequentially using forward substitution: +

 
+$$ + x^{(k+1)}_i = (1-\omega)x^{(k)}_i + \frac{\omega}{a_{ii}} \left(b_i - \sum_{j > i} a_{ij}x^{(k)}_j - \sum_{j < i} a_{ij}x^{(k+1)}_j \right),\quad i=1,2,\ldots,n. +$$ +

 
+ +The choice of relaxation factor is not necessarily easy, and depends upon the properties of the coefficient matrix. For symmetric, positive-definite matrices it can be proven that \( 0 < \omega < 2 \) will lead to convergence, but we are generally interested in faster convergence rather than just convergence. +

+
+ + + +
+
+ + + + + + + + + + + + diff --git a/doc/pub/Linalg/html/Linalg-solarized.html b/doc/pub/Linalg/html/Linalg-solarized.html new file mode 100644 index 000000000..33f9f90d9 --- /dev/null +++ b/doc/pub/Linalg/html/Linalg-solarized.html @@ -0,0 +1,1473 @@ + + + + + + + +Data analysis and Machine Learning Lectures: Linear Algebra methods + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Data analysis and Machine Learning Lectures: Linear Algebra methods

+ +

+ + +

+Morten Hjorth-Jensen [1, 2] +
+ +

+ + +

[1] Department of Physics, University of Oslo
+
[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University
+
+

+

Jan 27, 2018

+
+

+









+ +

Important Matrix and vector handling packages

+ +

+The Numerical Recipes codes have been rewritten in Fortran 90/95 and +C/C++ by us. The original source codes are taken from the widely used +software package LAPACK, which follows two other popular packages +developed in the 1970s, namely EISPACK and LINPACK. + +

+ +Add python material on linear algebra and array handling, text on numpy etc + +

+









+ +

Basic Matrix Features

+ +

+

+Matrix properties reminder. +

+$$ + \mathbf{A} = + \begin{bmatrix} a_{11} & a_{12} & a_{13} & a_{14} \\ + a_{21} & a_{22} & a_{23} & a_{24} \\ + a_{31} & a_{32} & a_{33} & a_{34} \\ + a_{41} & a_{42} & a_{43} & a_{44} + \end{bmatrix}\qquad +\mathbf{I} = + \begin{bmatrix} 1 & 0 & 0 & 0 \\ + 0 & 1 & 0 & 0 \\ + 0 & 0 & 1 & 0 \\ + 0 & 0 & 0 & 1 + \end{bmatrix} +$$ +

+ +









+ +

Basic Matrix Features

+
+ +

+The inverse of a matrix is defined by + +$$ +\mathbf{A}^{-1} \cdot \mathbf{A} = I +$$ +

+ + +

+









+ +

Basic Matrix Features

+ +

+

+Matrix Properties Reminder. +

+ +

+ + + + + + + + + + + +
Relations Name matrix elements
\( A = A^{T} \) symmetric \( a_{ij} = a_{ji} \)
\( A = \left (A^{T} \right )^{-1} \) real orthogonal \( \sum_k a_{ik} a_{jk} = \sum_k a_{ki} a_{kj} = \delta_{ij} \)
\( A = A^{ * } \) real matrix \( a_{ij} = a_{ij}^{ * } \)
\( A = A^{\dagger} \) hermitian \( a_{ij} = a_{ji}^{ * } \)
\( A = \left (A^{\dagger} \right )^{-1} \) unitary \( \sum_k a_{ik} a_{jk}^{ * } = \sum_k a_{ki}^{ * } a_{kj} = \delta_{ij} \)
+ +

+ + +

+









+ +

Some famous Matrices

+ + + +









+ +

Basic Matrix Features

+ +

+

+Some Equivalent Statements. +

+For an \( N\times N \) matrix \( \mathbf{A} \) the following properties are all equivalent + +

+
+ + +

+









+ +

Matrix Handling in C/C++, Static and Dynamical allocation

+ +

+

+Static. +

+We have an \( N\times N \) matrix A with \( N=100 \) +In C/C++ this would be defined as + +

+ + +

   int N = 100;
+   double A[100][100];
+   //   initialize all elements to zero
+   for(i=0 ; i < N ; i++) {
+      for(j=0 ; j < N ; j++) {
+         A[i][j] = 0.0;
+
+

+Note the way the matrix is organized, row-major order. +

+ + +

+









+ +

Matrix Handling in C/C++

+ +

+

+Row Major Order, Addition. +

+We have \( N\times N \) matrices A, B and C and we wish to +evaluate \( A=B+C \). + +$$ +\mathbf{A}= \mathbf{B}\pm\mathbf{C} \Longrightarrow a_{ij} = b_{ij}\pm c_{ij}, +$$ + +In C/C++ this would be coded like + +

+ + +

   for(i=0 ; i < N ; i++) {
+      for(j=0 ; j < N ; j++) {
+         a[i][j] = b[i][j]+c[i][j]
+
+ +
+ + +

+









+ +

Matrix Handling in C/C++

+ +

+

+Row Major Order, Multiplication. +

+We have \( N\times N \) matrices A, B and C and we wish to +evaluate \( A=BC \). + +$$ +\mathbf{A}=\mathbf{BC} \Longrightarrow a_{ij} = \sum_{k=1}^{n} b_{ik}c_{kj}, +$$ + +In C/C++ this would be coded like + +

+ + +

   for(i=0 ; i < N ; i++) {
+      for(j=0 ; j < N ; j++) {
+         for(k=0 ; k < N ; k++) {
+            a[i][j]+=b[i][k]*c[k][j];
+
+ +
+ + +

+









+ +

Dynamic memory allocation in C/C++

+ +

+At least three possibilities in this course + +

+ +









+ +

Matrix Handling in C/C++, Dynamic Allocation

+ +

+

+Do it yourself. +

+

+ + +

int N;
+double **  A;
+A = new double*[N]
+for ( i = 0; i < N; i++)
+    A[i] = new double[N];
+
+

+Always free space when you don't need an array anymore. + +

+ + +

for ( i = 0; i < N; i++)
+    delete[] A[i];
+delete[] A;
+
+ +
+ + +

+









+ +

Armadillo, recommended!!

+ + + +









+ +

Armadillo, simple examples

+ +

+ + +

#include <iostream>
+#include <armadillo>
+
+using namespace std;
+using namespace arma;
+
+int main(int argc, char** argv)
+  {
+  mat A = randu<mat>(5,5);
+  mat B = randu<mat>(5,5);
+
+  cout << A*B << endl;
+
+  return 0;
+
+

+









+ +

Armadillo, how to compile and install

+ +

+For people using Ubuntu, Debian, Linux Mint, simply go to the synaptic package manager and install +armadillo from there. +You may have to install Lapack as well. +For Mac and Windows users, follow the instructions from the webpage +http://arma.sourceforge.net. +To compile, use for example (linux/ubuntu) + +

+ + +

c++ -O2 -o program.x program.cpp  -larmadillo -llapack -lblas
+
+

+where the -l option indicates the library you wish to link to. + +

+For OS X users you may have to declare the paths to the include files and the libraries as +

+ + +

c++ -O2 -o program.x program.cpp  -L/usr/local/lib -I/usr/local/include -larmadillo -llapack -lblas
+
+

+









+ +

Armadillo, simple examples

+ +

+ + +

#include <iostream>
+#include "armadillo"
+using namespace arma;
+using namespace std;
+
+int main(int argc, char** argv)
+  {
+  // directly specify the matrix size (elements are uninitialised)
+  mat A(2,3);
+  // .n_rows = number of rows    (read only)
+  // .n_cols = number of columns (read only)
+  cout << "A.n_rows = " << A.n_rows << endl;
+  cout << "A.n_cols = " << A.n_cols << endl;
+  // directly access an element (indexing starts at 0)
+  A(1,2) = 456.0;
+  A.print("A:");
+  // scalars are treated as a 1x1 matrix,
+  // hence the code below will set A to have a size of 1x1
+  A = 5.0;
+  A.print("A:");
+  // if you want a matrix with all elements set to a particular value
+  // the .fill() member function can be used
+  A.set_size(3,3);
+  A.fill(5.0);  A.print("A:");
+
+

+









+ +

Armadillo, simple examples

+ +

+ + +

  mat B;
+
+  // endr indicates "end of row"
+  B << 0.555950 << 0.274690 << 0.540605 << 0.798938 << endr
+    << 0.108929 << 0.830123 << 0.891726 << 0.895283 << endr
+    << 0.948014 << 0.973234 << 0.216504 << 0.883152 << endr
+    << 0.023787 << 0.675382 << 0.231751 << 0.450332 << endr;
+
+  // print to the cout stream
+  // with an optional string before the contents of the matrix
+  B.print("B:");
+
+  // the << operator can also be used to print the matrix
+  // to an arbitrary stream (cout in this case)
+  cout << "B:" << endl << B << endl;
+  // save to disk
+  B.save("B.txt", raw_ascii);
+  // load from disk
+  mat C;
+  C.load("B.txt");
+  C += 2.0 * B;
+  C.print("C:");
+
+

+









+ +

Armadillo, simple examples

+ +

+ + +

  // submatrix types:
+  //
+  // .submat(first_row, first_column, last_row, last_column)
+  // .row(row_number)
+  // .col(column_number)
+  // .cols(first_column, last_column)
+  // .rows(first_row, last_row)
+
+  cout << "C.submat(0,0,3,1) =" << endl;
+  cout << C.submat(0,0,3,1) << endl;
+
+  // generate the identity matrix
+  mat D = eye<mat>(4,4);
+
+  D.submat(0,0,3,1) = C.cols(1,2);
+  D.print("D:");
+
+  // transpose
+  cout << "trans(B) =" << endl;
+  cout << trans(B) << endl;
+
+  // maximum from each column (traverse along rows)
+  cout << "max(B) =" << endl;
+  cout << max(B) << endl;
+
+

+









+ +

Armadillo, simple examples

+ +

+ + +

  // maximum from each row (traverse along columns)
+  cout << "max(B,1) =" << endl;
+  cout << max(B,1) << endl;
+  // maximum value in B
+  cout << "max(max(B)) = " << max(max(B)) << endl;
+  // sum of each column (traverse along rows)
+  cout << "sum(B) =" << endl;
+  cout << sum(B) << endl;
+  // sum of each row (traverse along columns)
+  cout << "sum(B,1) =" << endl;
+  cout << sum(B,1) << endl;
+  // sum of all elements
+  cout << "sum(sum(B)) = " << sum(sum(B)) << endl;
+  cout << "accu(B)     = " << accu(B) << endl;
+  // trace = sum along diagonal
+  cout << "trace(B)    = " << trace(B) << endl;
+  // random matrix -- values are uniformly distributed in the [0,1] interval
+  mat E = randu<mat>(4,4);
+  E.print("E:");
+
+

+









+ +

Armadillo, simple examples

+ +

+ + +

  // row vectors are treated like a matrix with one row
+  rowvec r;
+  r << 0.59499 << 0.88807 << 0.88532 << 0.19968;
+  r.print("r:");
+
+  // column vectors are treated like a matrix with one column
+  colvec q;
+  q << 0.81114 << 0.06256 << 0.95989 << 0.73628;
+  q.print("q:");
+
+  // dot or inner product
+  cout << "as_scalar(r*q) = " << as_scalar(r*q) << endl;
+
+    // outer product
+  cout << "q*r =" << endl;
+  cout << q*r << endl;
+
+
+  // sum of three matrices (no temporary matrices are created)
+  mat F = B + C + D;
+  F.print("F:");
+
+    return 0;
+
+

+









+ +

Armadillo, simple examples

+ +

+ + +

#include <iostream>
+#include "armadillo"
+using namespace arma;
+using namespace std;
+
+int main(int argc, char** argv)
+  {
+  cout << "Armadillo version: " << arma_version::as_string() << endl;
+
+  mat A;
+
+  A << 0.165300 << 0.454037 << 0.995795 << 0.124098 << 0.047084 << endr
+    << 0.688782 << 0.036549 << 0.552848 << 0.937664 << 0.866401 << endr
+    << 0.348740 << 0.479388 << 0.506228 << 0.145673 << 0.491547 << endr
+    << 0.148678 << 0.682258 << 0.571154 << 0.874724 << 0.444632 << endr
+    << 0.245726 << 0.595218 << 0.409327 << 0.367827 << 0.385736 << endr;
+
+  A.print("A =");
+
+  // determinant
+  cout << "det(A) = " << det(A) << endl;
+
+

+









+ +

Armadillo, simple examples

+ +

+ + +

  // inverse
+  cout << "inv(A) = " << endl << inv(A) << endl;
+  double k = 1.23;
+
+  mat    B = randu<mat>(5,5);
+  mat    C = randu<mat>(5,5);
+
+  rowvec r = randu<rowvec>(5);
+  colvec q = randu<colvec>(5);
+
+
+  // examples of some expressions
+  // for which optimised implementations exist
+  // optimised implementation of a trinary expression
+  // that results in a scalar
+  cout << "as_scalar( r*inv(diagmat(B))*q ) = ";
+  cout << as_scalar( r*inv(diagmat(B))*q ) << endl;
+
+  // example of an expression which is optimised
+  // as a call to the dgemm() function in BLAS:
+  cout << "k*trans(B)*C = " << endl << k*trans(B)*C;
+
+    return 0;
+
+

+









+ +

Gaussian Elimination

+ +

+We start with the linear set of equations + +$$ + \mathbf{A}\mathbf{x} = \mathbf{w}. +$$ + +We assume also that the matrix \( \mathbf{A} \) is non-singular and that the +matrix elements along the diagonal satisfy \( a_{ii} \ne 0 \). Simple \( 4\times 4 \) example + +$$ +\begin{bmatrix} + a_{11}& a_{12} &a_{13}& a_{14}\\ + a_{21}& a_{22} &a_{23}& a_{24}\\ + a_{31}& a_{32} &a_{33}& a_{34}\\ + a_{41}& a_{42} &a_{43}& a_{44}\\ + \end{bmatrix} \begin{bmatrix} + x_1\\ + x_2\\ + x_3 \\ + x_4 \\ + \end{bmatrix} + =\begin{bmatrix} + w_1\\ + w_2\\ + w_3 \\ + w_4\\ + \end{bmatrix}. +$$ + +

+









+ +

Gaussian Elimination

+or + +$$ +\begin{align} + a_{11}x_1 +a_{12}x_2 +a_{13}x_3 + a_{14}x_4=&w_1 \nonumber \\ +a_{21}x_1 + a_{22}x_2 + a_{23}x_3 + a_{24}x_4=&w_2 \nonumber \\ +a_{31}x_1 + a_{32}x_2 + a_{33}x_3 + a_{34}x_4=&w_3 \nonumber \\ +a_{41}x_1 + a_{42}x_2 + a_{43}x_3 + a_{44}x_4=&w_4. \nonumber +\end{align} +$$ + +

+









+ +

Gaussian Elimination

+ +

+The basic idea of Gaussian elimination is to use the first equation to eliminate the first unknown \( x_1 \) +from the remaining \( n-1 \) equations. Then we use the new second equation to eliminate the second unknown +\( x_2 \) from the remaining \( n-2 \) equations. With \( n-1 \) such eliminations +we obtain a so-called upper triangular set of equations of the form + +$$ +\begin{align} + b_{11}x_1 +b_{12}x_2 +b_{13}x_3 + b_{14}x_4=&y_1 \nonumber \\ + b_{22}x_2 + b_{23}x_3 + b_{24}x_4=&y_2 \nonumber \\ +b_{33}x_3 + b_{34}x_4=&y_3 \nonumber \\ +b_{44}x_4=&y_4. \nonumber +\label{eq:gaussbacksub} +\end{align} +$$ + +We can solve this system of equations recursively starting from \( x_n \) (in our case \( x_4 \)) and proceed with +what is called a backward substitution. + +

+









+ +

Gaussian Elimination

+This process can be expressed mathematically as + +$$ +\begin{equation} + x_m = \frac{1}{b_{mm}}\left(y_m-\sum_{k=m+1}^nb_{mk}x_k\right)\quad m=n-1,n-2,\dots,1. +\label{_auto1} +\end{equation} +$$ + +To arrive at such an upper triangular system of equations, we start by eliminating +the unknown \( x_1 \) for \( j=2,n \). We achieve this by multiplying the first equation by \( a_{j1}/a_{11} \) and then subtract +the result from the $j$th equation. We assume obviously that \( a_{11}\ne 0 \) and that +\( \mathbf{A} \) is not singular. + +

+









+ +

Gaussian Elimination

+ +

+Our actual \( 4\times 4 \) example reads after the first operation + +$$ +\begin{bmatrix} + a_{11}& a_{12} &a_{13}& a_{14}\\ + 0& (a_{22}-\frac{a_{21}a_{12}}{a_{11}}) &(a_{23}-\frac{a_{21}a_{13}}{a_{11}}) & (a_{24}-\frac{a_{21}a_{14}}{a_{11}})\\ +0& (a_{32}-\frac{a_{31}a_{12}}{a_{11}})& (a_{33}-\frac{a_{31}a_{13}}{a_{11}})& (a_{34}-\frac{a_{31}a_{14}}{a_{11}})\\ +0&(a_{42}-\frac{a_{41}a_{12}}{a_{11}}) &(a_{43}-\frac{a_{41}a_{13}}{a_{11}}) & (a_{44}-\frac{a_{41}a_{14}}{a_{11}}) \\ + \end{bmatrix} \begin{bmatrix} + x_1\\ + x_2\\ + x_3 \\ + x_4 \\ + \end{bmatrix} + =\begin{bmatrix} + y_1\\ + w_2^{(2)}\\ + w_3^{(2)} \\ + w_4^{(2)}\\ + \end{bmatrix}, +$$ + +or + +$$ +\begin{align} + b_{11}x_1 +b_{12}x_2 +b_{13}x_3 + b_{14}x_4=&y_1 \nonumber \\ + a^{(2)}_{22}x_2 + a^{(2)}_{23}x_3 + a^{(2)}_{24}x_4=&w^{(2)}_2 \nonumber \\ + a^{(2)}_{32}x_2 + a^{(2)}_{33}x_3 + a^{(2)}_{34}x_4=&w^{(2)}_3 \nonumber \\ + a^{(2)}_{42}x_2 + a^{(2)}_{43}x_3 + a^{(2)}_{44}x_4=&w^{(2)}_4, \nonumber \\ +\label{_auto2} +\end{align} +$$ + +

+









+ +

Gaussian Elimination

+ +

+The new coefficients are + +$$ +\begin{equation} + b_{1k} = a_{1k}^{(1)} \quad k=1,\dots,n, +\label{_auto3} +\end{equation} +$$ + +where each \( a_{1k}^{(1)} \) is equal to the original \( a_{1k} \) element. The other coefficients are + +$$ +\begin{equation} +a_{jk}^{(2)} = a_{jk}^{(1)}-\frac{a_{j1}^{(1)}a_{1k}^{(1)}}{a_{11}^{(1)}} \quad j,k=2,\dots,n, +\label{_auto4} +\end{equation} +$$ + +with a new right-hand side given by + +$$ +\begin{equation} +y_{1}=w_1^{(1)}, \quad w_j^{(2)} =w_j^{(1)}-\frac{a_{j1}^{(1)}w_1^{(1)}}{a_{11}^{(1)}} \quad j=2,\dots,n. +\label{_auto5} +\end{equation} +$$ + +We have also set \( w_1^{(1)}=w_1 \), the original vector element. +We see that the system of unknowns \( x_1,\dots,x_n \) is transformed into an \( (n-1)\times (n-1) \) problem. + +

+









+ +

Gaussian Elimination

+ +

+This step is called forward substitution. +Proceeding with these substitutions, we obtain the +general expressions for the new coefficients + +$$ +\begin{equation} + a_{jk}^{(m+1)} = a_{jk}^{(m)}-\frac{a_{jm}^{(m)}a_{mk}^{(m)}}{a_{mm}^{(m)}} \quad j,k=m+1,\dots,n, +\label{_auto6} +\end{equation} +$$ + +with \( m=1,\dots,n-1 \) and a +right-hand side given by + +$$ +\begin{equation} + w_j^{(m+1)} =w_j^{(m)}-\frac{a_{jm}^{(m)}w_m^{(m)}}{a_{mm}^{(m)}}\quad j=m+1,\dots,n. +\label{_auto7} +\end{equation} +$$ + +This set of \( n-1 \) elimations leads us to an equations which is solved by back substitution. +If the arithmetics is exact and the matrix \( \mathbf{A} \) is not singular, then the computed answer will be exact. + +

+Even though the matrix elements along the diagonal are not zero, +numerically small numbers may appear and subsequent divisions may lead to large numbers, which, if added +to a small number may yield losses of precision. Suppose for example that our first division in \( (a_{22}-a_{21}a_{12}/a_{11}) \) +results in \( -10^{-7} \) and that \( a_{22} \) is one. +one. We are then +adding \( 10^7+1 \). With single precision this results in \( 10^7 \). + +

+









+ +

Linear Algebra Methods

+ + + +









+ +

LU Decomposition

+ +

+The LU decomposition method means that we can rewrite +this matrix as the product of two matrices \( \mathbf{L} \) and \( \mathbf{U} \) +where + +$$ + \begin{bmatrix} + a_{11} & a_{12} & a_{13} & a_{14} \\ + a_{21} & a_{22} & a_{23} & a_{24} \\ + a_{31} & a_{32} & a_{33} & a_{34} \\ + a_{41} & a_{42} & a_{43} & a_{44} + \end{bmatrix} + = \begin{bmatrix} + 1 & 0 & 0 & 0 \\ + l_{21} & 1 & 0 & 0 \\ + l_{31} & l_{32} & 1 & 0 \\ + l_{41} & l_{42} & l_{43} & 1 + \end{bmatrix} + \begin{bmatrix} + u_{11} & u_{12} & u_{13} & u_{14} \\ + 0 & u_{22} & u_{23} & u_{24} \\ + 0 & 0 & u_{33} & u_{34} \\ + 0 & 0 & 0 & u_{44} + \end{bmatrix}. +$$ + +

+









+ +

LU Decomposition

+ +

+LU decomposition forms the backbone of other algorithms in linear algebra, such as the +solution of linear equations given by + +$$ +\begin{align} + a_{11}x_1 +a_{12}x_2 +a_{13}x_3 + a_{14}x_4=&w_1 \nonumber \\ +a_{21}x_1 + a_{22}x_2 + a_{23}x_3 + a_{24}x_4=&w_2 \nonumber \\ +a_{31}x_1 + a_{32}x_2 + a_{33}x_3 + a_{34}x_4=&w_3 \nonumber \\ +a_{41}x_1 + a_{42}x_2 + a_{43}x_3 + a_{44}x_4=&w_4. \nonumber +\end{align} +$$ + +The above set of equations is conveniently solved by using LU decomposition as an intermediate step. + +

+The matrix \( \mathbf{A}\in \mathbb{R}^{n\times n} \) has an LU factorization if the determinant +is different from zero. If the LU factorization exists and \( \mathbf{A} \) is non-singular, then the LU factorization +is unique and the determinant is given by + +$$ +det\{\mathbf{A}\}=det\{\mathbf{LU}\}= det\{\mathbf{L}\}det\{\mathbf{U}\}=u_{11}u_{22}\dots u_{nn}. +$$ + +

+









+ +

LU Decomposition, why?

+ +

+There are at least three main advantages with LU decomposition compared with standard Gaussian elimination: + +

+ +









+ +

LU Decomposition, linear equations

+ +

+With the LU decomposition it is rather +simple to solve a system of linear equations + +$$ +\begin{align} + a_{11}x_1 +a_{12}x_2 +a_{13}x_3 + a_{14}x_4=&w_1 \nonumber \\ +a_{21}x_1 + a_{22}x_2 + a_{23}x_3 + a_{24}x_4=&w_2 \nonumber \\ +a_{31}x_1 + a_{32}x_2 + a_{33}x_3 + a_{34}x_4=&w_3 \nonumber \\ +a_{41}x_1 + a_{42}x_2 + a_{43}x_3 + a_{44}x_4=&w_4. \nonumber +\end{align} +$$ + +

+This can be written in matrix form as + +$$ \mathbf{Ax}=\mathbf{w}. $$ + +

+where \( \mathbf{A} \) and \( \mathbf{w} \) are known and we have to solve for +\( \mathbf{x} \). Using the LU dcomposition we write + +$$ \mathbf{A} \mathbf{x} \equiv \mathbf{L} \mathbf{U} \mathbf{x} =\mathbf{w}. $$ + +

+









+ +

LU Decomposition, linear equations

+ +

+The previous equation can be calculated in two steps + +$$ \mathbf{L} \mathbf{y} = \mathbf{w};\qquad \mathbf{Ux}=\mathbf{y}. $$ + +

+To show that this is correct we use to the LU decomposition +to rewrite our system of linear equations as + +$$ \mathbf{LUx}=\mathbf{w}, $$ + +and since the determinat of \( \mathbf{L} \) is equal to 1 (by construction +since the diagonals of \( \mathbf{L} \) equal 1) we can use the inverse of +\( \mathbf{L} \) to obtain + +$$ + \mathbf{Ux}=\mathbf{L^{-1}w}=\mathbf{y}, +$$ + +which yields the intermediate step + +$$ + \mathbf{L^{-1}w}=\mathbf{y} +$$ + +and as soon as we have \( \mathbf{y} \) we can obtain \( \mathbf{x} \) +through \( \mathbf{Ux}=\mathbf{y} \). + +

+









+ +

LU Decomposition, why?

+ +

+For our four-dimentional example this takes the form + +$$ +\begin{align} + y_1=&w_1 \nonumber\\ +l_{21}y_1 + y_2=&w_2\nonumber \\ +l_{31}y_1 + l_{32}y_2 + y_3 =&w_3\nonumber \\ +l_{41}y_1 + l_{42}y_2 + l_{43}y_3 + y_4=&w_4. \nonumber +\end{align} +$$ + +

+and + +$$ +\begin{align} + u_{11}x_1 +u_{12}x_2 +u_{13}x_3 + u_{14}x_4=&y_1 \nonumber\\ +u_{22}x_2 + u_{23}x_3 + u_{24}x_4=&y_2\nonumber \\ +u_{33}x_3 + u_{34}x_4=&y_3\nonumber \\ +u_{44}x_4=&y_4 \nonumber +\end{align} +$$ + +

+This example shows the basis for the algorithm +needed to solve the set of \( n \) linear equations. + +

+









+ +

LU Decomposition, linear equations

+ +

+The algorithm goes as follows + +

+ +









+ +

LU Decomposition, the inverse of a matrix

+ +

+If the inverse exists then + +$$ + \mathbf{A}^{-1}\mathbf{A}=\mathbf{I}, +$$ + +the identity matrix. With an LU decomposed matrix we can rewrite the last equation as + +$$ + \mathbf{LU}\mathbf{A}^{-1}=\mathbf{I}. +$$ + +

+









+ +

LU Decomposition, the inverse of a matrix

+ +

+If we assume that the first column (that is column 1) of the inverse matrix +can be written as a vector with unknown entries + +$$ + \mathbf{A}_1^{-1}= \begin{bmatrix} + a_{11}^{-1} \\ + a_{21}^{-1} \\ + \dots \\ + a_{n1}^{-1} \\ + \end{bmatrix}, +$$ + +then we have a linear set of equations + +$$ + \mathbf{LU}\begin{bmatrix} + a_{11}^{-1} \\ + a_{21}^{-1} \\ + \dots \\ + a_{n1}^{-1} \\ + \end{bmatrix} =\begin{bmatrix} + 1 \\ + 0 \\ + \dots \\ + 0 \\ + \end{bmatrix}. +$$ + +

+









+ +

LU Decomposition, the inverse

+ +

+In a similar way we can compute the unknow entries of the second column, + +$$ + \mathbf{LU}\begin{bmatrix} + a_{12}^{-1} \\ + a_{22}^{-1} \\ + \dots \\ + a_{n2}^{-1} \\ + \end{bmatrix}=\begin{bmatrix} + 0 \\ + 1 \\ + \dots \\ + 0 \\ + \end{bmatrix}, +$$ + +and continue till we have solved all \( n \) sets of linear equations. + +

+









+ +

Using Armadillo to perform an LU decomposition

+

+ + +

#include <iostream>
+#include "armadillo"
+using namespace arma;
+using namespace std;
+
+int main()
+  {
+   mat A = randu<mat>(5,5);
+   vec b = randu<vec>(5);
+
+  A.print("A =");
+  b.print("b=");
+  // solve Ax = b
+  vec x = solve(A,b);
+  // print x
+  x.print("x=");
+  // find LU decomp of A, if needed, P is the permutation matrix
+  mat L, U;
+  lu(L,U,A);
+  // print l
+  L.print(" L= ");
+  // print U
+  U.print(" U= ");
+  //Check that A = LU
+  (A-L*U).print("Test of LU decomposition");
+    return 0;
+  }
+
+

+









+ +

Iterative methods, Chapter 6

+
+ +

+ +

+
+ + +

+









+ +

Iterative methods, Jacobi's method

+
+ +

+It is a simple method for solving +$$ +\mathbf{A}\mathbf{x}=\mathbf{b}, +$$ + +where \( \mathbf{A} \) is a matrix and \( \mathbf{x} \) and \( \mathbf{b} \) are vectors. The vector \( \mathbf{x} \) is +the unknown. + +

+It is an iterative scheme where we start with a guess for the unknown, and +after \( k+1 \) iterations we have +$$ +\mathbf{x}^{(k+1)}= \mathbf{D}^{-1}(\mathbf{b}-(\mathbf{L}+\mathbf{U})\mathbf{x}^{(k)}), +$$ + +with \( \mathbf{A}=\mathbf{D}+\mathbf{U}+\mathbf{L} \) and +\( \mathbf{D} \) being a diagonal matrix, \( \mathbf{U} \) an upper triangular matrix and \( \mathbf{L} \) a lower triangular +matrix. + +

+If the matrix \( \mathbf{A} \) is positive definite or diagonally dominant, one can show that this method will always converge to the exact solution. +

+ + +

+









+ +

Iterative methods, Jacobi's method

+
+ +

+We can demonstrate Jacobi's method by this \( 4\times 4 \) matrix problem. We assume a guess +for the vector elements \( x_i^{(0)} \), a guess which represents our first iteration. The new +values are obtained by substitution +$$ +\begin{align} + x_1^{(1)} =&(b_1-a_{12}x_2^{(0)} -a_{13}x_3^{(0)} - a_{14}x_4^{(0)})/a_{11} \nonumber \\ + x_2^{(1)} =&(b_2-a_{21}x_1^{(0)} - a_{23}x_3^{(0)} - a_{24}x_4^{(0)})/a_{22} \nonumber \\ + x_3^{(1)} =&(b_3- a_{31}x_1^{(0)} -a_{32}x_2^{(0)} -a_{34}x_4^{(0)})/a_{33} \nonumber \\ + x_4^{(1)}=&(b_4-a_{41}x_1^{(0)} -a_{42}x_2^{(0)} - a_{43}x_3^{(0)})/a_{44}, \nonumber +\end{align} +$$ + +which after \( k+1 \) iterations reads +$$ +\begin{align} + x_1^{(k+1)} =&(b_1-a_{12}x_2^{(k)} -a_{13}x_3^{(k)} - a_{14}x_4^{(k)})/a_{11} \nonumber \\ + x_2^{(k+1)} =&(b_2-a_{21}x_1^{(k)} - a_{23}x_3^{(k)} - a_{24}x_4^{(k)})/a_{22} \nonumber \\ + x_3^{(k+1)} =&(b_3- a_{31}x_1^{(k)} -a_{32}x_2^{(k)} -a_{34}x_4^{(k)})/a_{33} \nonumber \\ + x_4^{(k+1)}=&(b_4-a_{41}x_1^{(k)} -a_{42}x_2^{(k)} - a_{43}x_3^{(k)})/a_{44}, \nonumber +\end{align} +$$ +

+ + +

+









+ +

Iterative methods, Jacobi's method

+
+ +

+We can generalize the above equations to +$$ + x_i^{(k+1)}=(b_i-\sum_{j=1, j\ne i}^{n}a_{ij}x_j^{(k)})/a_{ii} +$$ + +or in an even more compact form as +$$ \mathbf{x}^{(k+1)}= \mathbf{D}^{-1}(\mathbf{b}-(\mathbf{L}+\mathbf{U})\mathbf{x}^{(k)}), +$$ + +with \( \mathbf{A}=\mathbf{D}+\mathbf{U}+\mathbf{L} \) and +\( \mathbf{D} \) being a diagonal matrix, \( \mathbf{U} \) an upper triangular matrix and \( \mathbf{L} \) a lower triangular +matrix. +

+ + +

+









+ +

Iterative methods, Gauss-Seidel's method

+
+ +

+Our \( 4\times 4 \) matrix problem +$$ +\begin{align} + x_1^{(k+1)} =&(b_1-a_{12}x_2^{(k)} -a_{13}x_3^{(k)} - a_{14}x_4^{(k)})/a_{11} \nonumber \\ + x_2^{(k+1)} =&(b_2-a_{21}x_1^{(k)} - a_{23}x_3^{(k)} - a_{24}x_4^{(k)})/a_{22} \nonumber \\ + x_3^{(k+1)} =&(b_3- a_{31}x_1^{(k)} -a_{32}x_2^{(k)} -a_{34}x_4^{(k)})/a_{33} \nonumber \\ + x_4^{(k+1)}=&(b_4-a_{41}x_1^{(k)} -a_{42}x_2^{(k)} - a_{43}x_3^{(k)})/a_{44}, \nonumber +\end{align} +$$ + +can be rewritten as +$$ +\begin{align} + x_1^{(k+1)} =&(b_1-a_{12}x_2^{(k)} -a_{13}x_3^{(k)} - a_{14}x_4^{(k)})/a_{11} \nonumber \\ + x_2^{(k+1)} =&(b_2-a_{21}x_1^{(k+1)} - a_{23}x_3^{(k)} - a_{24}x_4^{(k)})/a_{22} \nonumber \\ + x_3^{(k+1)} =&(b_3- a_{31}x_1^{(k+1)} -a_{32}x_2^{(k+1)} -a_{34}x_4^{(k)})/a_{33} \nonumber \\ + x_4^{(k+1)}=&(b_4-a_{41}x_1^{(k+1)} -a_{42}x_2^{(k+1)} - a_{43}x_3^{(k+1)})/a_{44}, \nonumber +\end{align} +$$ + +which allows us to utilize the preceding solution (forward substitution). This improves normally the convergence +behavior and leads to the Gauss-Seidel method! +

+ + +

+









+ +

Iterative methods, Gauss-Seidel's method

+
+ +

+We can generalize +$$ +\begin{align} + x_1^{(k+1)} =&(b_1-a_{12}x_2^{(k)} -a_{13}x_3^{(k)} - a_{14}x_4^{(k)})/a_{11} \nonumber \\ + x_2^{(k+1)} =&(b_2-a_{21}x_1^{(k+1)} - a_{23}x_3^{(k)} - a_{24}x_4^{(k)})/a_{22} \nonumber \\ + x_3^{(k+1)} =&(b_3- a_{31}x_1^{(k+1)} -a_{32}x_2^{(k+1)} -a_{34}x_4^{(k)})/a_{33} \nonumber \\ + x_4^{(k+1)}=&(b_4-a_{41}x_1^{(k+1)} -a_{42}x_2^{(k+1)} - a_{43}x_3^{(k+1)})/a_{44}, \nonumber +\end{align} +$$ + +to the following form +$$ + x^{(k+1)}_i = \frac{1}{a_{ii}} \left(b_i - \sum_{j > i}a_{ij}x^{(k)}_j - \sum_{j < i}a_{ij}x^{(k+1)}_j \right),\quad i=1,2,\ldots,n. +$$ + +The procedure is generally continued until the changes made by an iteration are below some tolerance. + +

+The convergence properties of the Jacobi method and the +Gauss-Seidel method are dependent on the matrix \( \mathbf{A} \). These methods converge when +the matrix is symmetric positive-definite, or is strictly or irreducibly diagonally dominant. +Both methods sometimes converge even if these conditions are not satisfied. +

+ + +

+









+ +

Iterative methods, Successive over-relaxation

+
+ +

+Given a square system of n linear equations with unknown \( \mathbf x \): +$$ + \mathbf{A}\mathbf x = \mathbf b +$$ + +where +$$ + \mathbf{A}=\begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\a_{n1} & a_{n2} & \cdots & a_{nn} \end{bmatrix}, \qquad \mathbf{x} = \begin{bmatrix} x_{1} \\ x_2 \\ \vdots \\ x_n \end{bmatrix} , \qquad \mathbf{b} = \begin{bmatrix} b_{1} \\ b_2 \\ \vdots \\ b_n \end{bmatrix}. +$$ +

+ + +

+









+ +

Iterative methods, Successive over-relaxation

+
+ +

+Then A can be decomposed into a diagonal component D, and strictly lower and upper triangular components L and U: +$$ + \mathbf{A} =\mathbf{D} + \mathbf{L} + \mathbf{U}, +$$ + +where +$$ + D = \begin{bmatrix} a_{11} & 0 & \cdots & 0 \\ 0 & a_{22} & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\0 & 0 & \cdots & a_{nn} \end{bmatrix}, \quad L = \begin{bmatrix} 0 & 0 & \cdots & 0 \\ a_{21} & 0 & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\a_{n1} & a_{n2} & \cdots & 0 \end{bmatrix}, \quad U = \begin{bmatrix} 0 & a_{12} & \cdots & a_{1n} \\ 0 & 0 & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\0 & 0 & \cdots & 0 \end{bmatrix}. +$$ + +The system of linear equations may be rewritten as: +$$ + (D+\omega L) \mathbf{x} = \omega \mathbf{b} - [\omega U + (\omega-1) D ] \mathbf{x} +$$ + +for a constant \( \omega > 1 \). +

+ + +

+









+ +

Iterative methods, Successive over-relaxation

+
+ +

+The method of successive over-relaxation is an iterative technique that solves the left hand side of this expression for \( x \), using previous value for \( x \) on the right hand side. Analytically, this may be written as: +$$ + \mathbf{x}^{(k+1)} = (D+\omega L)^{-1} \big(\omega \mathbf{b} - [\omega U + (\omega-1) D ] \mathbf{x}^{(k)}\big). +$$ + +However, by taking advantage of the triangular form of \( (D+\omega L) \), the elements of \( x^{(k+1)} \) can be computed sequentially using forward substitution: +$$ + x^{(k+1)}_i = (1-\omega)x^{(k)}_i + \frac{\omega}{a_{ii}} \left(b_i - \sum_{j > i} a_{ij}x^{(k)}_j - \sum_{j < i} a_{ij}x^{(k+1)}_j \right),\quad i=1,2,\ldots,n. +$$ + +The choice of relaxation factor is not necessarily easy, and depends upon the properties of the coefficient matrix. For symmetric, positive-definite matrices it can be proven that \( 0 < \omega < 2 \) will lead to convergence, but we are generally interested in faster convergence rather than just convergence. +

+ + +

+ + + + +

+ © 1999-2018, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license +
+ + + + + + diff --git a/doc/pub/Linalg/html/Linalg.html b/doc/pub/Linalg/html/Linalg.html new file mode 100644 index 000000000..d2d269200 --- /dev/null +++ b/doc/pub/Linalg/html/Linalg.html @@ -0,0 +1,1478 @@ + + + + + + + +Data analysis and Machine Learning Lectures: Linear Algebra methods + + + + + + + + + + + + + + + + + + + + + + + +

Data analysis and Machine Learning Lectures: Linear Algebra methods

+ +

+ + +

+Morten Hjorth-Jensen [1, 2] +
+ +

+ + +

[1] Department of Physics, University of Oslo
+
[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University
+
+

+

Jan 27, 2018

+
+

+









+ +

Important Matrix and vector handling packages

+ +

+The Numerical Recipes codes have been rewritten in Fortran 90/95 and +C/C++ by us. The original source codes are taken from the widely used +software package LAPACK, which follows two other popular packages +developed in the 1970s, namely EISPACK and LINPACK. + +

+ +Add python material on linear algebra and array handling, text on numpy etc + +

+









+ +

Basic Matrix Features

+ +

+

+Matrix properties reminder. +

+$$ + \mathbf{A} = + \begin{bmatrix} a_{11} & a_{12} & a_{13} & a_{14} \\ + a_{21} & a_{22} & a_{23} & a_{24} \\ + a_{31} & a_{32} & a_{33} & a_{34} \\ + a_{41} & a_{42} & a_{43} & a_{44} + \end{bmatrix}\qquad +\mathbf{I} = + \begin{bmatrix} 1 & 0 & 0 & 0 \\ + 0 & 1 & 0 & 0 \\ + 0 & 0 & 1 & 0 \\ + 0 & 0 & 0 & 1 + \end{bmatrix} +$$ +

+ +









+ +

Basic Matrix Features

+
+ +

+The inverse of a matrix is defined by + +$$ +\mathbf{A}^{-1} \cdot \mathbf{A} = I +$$ +

+ + +

+









+ +

Basic Matrix Features

+ +

+

+Matrix Properties Reminder. +

+ +

+ + + + + + + + + + + +
Relations Name matrix elements
\( A = A^{T} \) symmetric \( a_{ij} = a_{ji} \)
\( A = \left (A^{T} \right )^{-1} \) real orthogonal \( \sum_k a_{ik} a_{jk} = \sum_k a_{ki} a_{kj} = \delta_{ij} \)
\( A = A^{ * } \) real matrix \( a_{ij} = a_{ij}^{ * } \)
\( A = A^{\dagger} \) hermitian \( a_{ij} = a_{ji}^{ * } \)
\( A = \left (A^{\dagger} \right )^{-1} \) unitary \( \sum_k a_{ik} a_{jk}^{ * } = \sum_k a_{ki}^{ * } a_{kj} = \delta_{ij} \)
+ +

+ + +

+









+ +

Some famous Matrices

+ + + +









+ +

Basic Matrix Features

+ +

+

+Some Equivalent Statements. +

+For an \( N\times N \) matrix \( \mathbf{A} \) the following properties are all equivalent + +

+
+ + +

+









+ +

Matrix Handling in C/C++, Static and Dynamical allocation

+ +

+

+Static. +

+We have an \( N\times N \) matrix A with \( N=100 \) +In C/C++ this would be defined as + +

+ + +

   int N = 100;
+   double A[100][100];
+   //   initialize all elements to zero
+   for(i=0 ; i < N ; i++) {
+      for(j=0 ; j < N ; j++) {
+         A[i][j] = 0.0;
+
+

+Note the way the matrix is organized, row-major order. +

+ + +

+









+ +

Matrix Handling in C/C++

+ +

+

+Row Major Order, Addition. +

+We have \( N\times N \) matrices A, B and C and we wish to +evaluate \( A=B+C \). + +$$ +\mathbf{A}= \mathbf{B}\pm\mathbf{C} \Longrightarrow a_{ij} = b_{ij}\pm c_{ij}, +$$ + +In C/C++ this would be coded like + +

+ + +

   for(i=0 ; i < N ; i++) {
+      for(j=0 ; j < N ; j++) {
+         a[i][j] = b[i][j]+c[i][j]
+
+ +
+ + +

+









+ +

Matrix Handling in C/C++

+ +

+

+Row Major Order, Multiplication. +

+We have \( N\times N \) matrices A, B and C and we wish to +evaluate \( A=BC \). + +$$ +\mathbf{A}=\mathbf{BC} \Longrightarrow a_{ij} = \sum_{k=1}^{n} b_{ik}c_{kj}, +$$ + +In C/C++ this would be coded like + +

+ + +

   for(i=0 ; i < N ; i++) {
+      for(j=0 ; j < N ; j++) {
+         for(k=0 ; k < N ; k++) {
+            a[i][j]+=b[i][k]*c[k][j];
+
+ +
+ + +

+









+ +

Dynamic memory allocation in C/C++

+ +

+At least three possibilities in this course + +

+ +









+ +

Matrix Handling in C/C++, Dynamic Allocation

+ +

+

+Do it yourself. +

+

+ + +

int N;
+double **  A;
+A = new double*[N]
+for ( i = 0; i < N; i++)
+    A[i] = new double[N];
+
+

+Always free space when you don't need an array anymore. + +

+ + +

for ( i = 0; i < N; i++)
+    delete[] A[i];
+delete[] A;
+
+ +
+ + +

+









+ +

Armadillo, recommended!!

+ + + +









+ +

Armadillo, simple examples

+ +

+ + +

#include <iostream>
+#include <armadillo>
+
+using namespace std;
+using namespace arma;
+
+int main(int argc, char** argv)
+  {
+  mat A = randu<mat>(5,5);
+  mat B = randu<mat>(5,5);
+
+  cout << A*B << endl;
+
+  return 0;
+
+

+









+ +

Armadillo, how to compile and install

+ +

+For people using Ubuntu, Debian, Linux Mint, simply go to the synaptic package manager and install +armadillo from there. +You may have to install Lapack as well. +For Mac and Windows users, follow the instructions from the webpage +http://arma.sourceforge.net. +To compile, use for example (linux/ubuntu) + +

+ + +

c++ -O2 -o program.x program.cpp  -larmadillo -llapack -lblas
+
+

+where the -l option indicates the library you wish to link to. + +

+For OS X users you may have to declare the paths to the include files and the libraries as +

+ + +

c++ -O2 -o program.x program.cpp  -L/usr/local/lib -I/usr/local/include -larmadillo -llapack -lblas
+
+

+









+ +

Armadillo, simple examples

+ +

+ + +

#include <iostream>
+#include "armadillo"
+using namespace arma;
+using namespace std;
+
+int main(int argc, char** argv)
+  {
+  // directly specify the matrix size (elements are uninitialised)
+  mat A(2,3);
+  // .n_rows = number of rows    (read only)
+  // .n_cols = number of columns (read only)
+  cout << "A.n_rows = " << A.n_rows << endl;
+  cout << "A.n_cols = " << A.n_cols << endl;
+  // directly access an element (indexing starts at 0)
+  A(1,2) = 456.0;
+  A.print("A:");
+  // scalars are treated as a 1x1 matrix,
+  // hence the code below will set A to have a size of 1x1
+  A = 5.0;
+  A.print("A:");
+  // if you want a matrix with all elements set to a particular value
+  // the .fill() member function can be used
+  A.set_size(3,3);
+  A.fill(5.0);  A.print("A:");
+
+

+









+ +

Armadillo, simple examples

+ +

+ + +

  mat B;
+
+  // endr indicates "end of row"
+  B << 0.555950 << 0.274690 << 0.540605 << 0.798938 << endr
+    << 0.108929 << 0.830123 << 0.891726 << 0.895283 << endr
+    << 0.948014 << 0.973234 << 0.216504 << 0.883152 << endr
+    << 0.023787 << 0.675382 << 0.231751 << 0.450332 << endr;
+
+  // print to the cout stream
+  // with an optional string before the contents of the matrix
+  B.print("B:");
+
+  // the << operator can also be used to print the matrix
+  // to an arbitrary stream (cout in this case)
+  cout << "B:" << endl << B << endl;
+  // save to disk
+  B.save("B.txt", raw_ascii);
+  // load from disk
+  mat C;
+  C.load("B.txt");
+  C += 2.0 * B;
+  C.print("C:");
+
+

+









+ +

Armadillo, simple examples

+ +

+ + +

  // submatrix types:
+  //
+  // .submat(first_row, first_column, last_row, last_column)
+  // .row(row_number)
+  // .col(column_number)
+  // .cols(first_column, last_column)
+  // .rows(first_row, last_row)
+
+  cout << "C.submat(0,0,3,1) =" << endl;
+  cout << C.submat(0,0,3,1) << endl;
+
+  // generate the identity matrix
+  mat D = eye<mat>(4,4);
+
+  D.submat(0,0,3,1) = C.cols(1,2);
+  D.print("D:");
+
+  // transpose
+  cout << "trans(B) =" << endl;
+  cout << trans(B) << endl;
+
+  // maximum from each column (traverse along rows)
+  cout << "max(B) =" << endl;
+  cout << max(B) << endl;
+
+

+









+ +

Armadillo, simple examples

+ +

+ + +

  // maximum from each row (traverse along columns)
+  cout << "max(B,1) =" << endl;
+  cout << max(B,1) << endl;
+  // maximum value in B
+  cout << "max(max(B)) = " << max(max(B)) << endl;
+  // sum of each column (traverse along rows)
+  cout << "sum(B) =" << endl;
+  cout << sum(B) << endl;
+  // sum of each row (traverse along columns)
+  cout << "sum(B,1) =" << endl;
+  cout << sum(B,1) << endl;
+  // sum of all elements
+  cout << "sum(sum(B)) = " << sum(sum(B)) << endl;
+  cout << "accu(B)     = " << accu(B) << endl;
+  // trace = sum along diagonal
+  cout << "trace(B)    = " << trace(B) << endl;
+  // random matrix -- values are uniformly distributed in the [0,1] interval
+  mat E = randu<mat>(4,4);
+  E.print("E:");
+
+

+









+ +

Armadillo, simple examples

+ +

+ + +

  // row vectors are treated like a matrix with one row
+  rowvec r;
+  r << 0.59499 << 0.88807 << 0.88532 << 0.19968;
+  r.print("r:");
+
+  // column vectors are treated like a matrix with one column
+  colvec q;
+  q << 0.81114 << 0.06256 << 0.95989 << 0.73628;
+  q.print("q:");
+
+  // dot or inner product
+  cout << "as_scalar(r*q) = " << as_scalar(r*q) << endl;
+
+    // outer product
+  cout << "q*r =" << endl;
+  cout << q*r << endl;
+
+
+  // sum of three matrices (no temporary matrices are created)
+  mat F = B + C + D;
+  F.print("F:");
+
+    return 0;
+
+

+









+ +

Armadillo, simple examples

+ +

+ + +

#include <iostream>
+#include "armadillo"
+using namespace arma;
+using namespace std;
+
+int main(int argc, char** argv)
+  {
+  cout << "Armadillo version: " << arma_version::as_string() << endl;
+
+  mat A;
+
+  A << 0.165300 << 0.454037 << 0.995795 << 0.124098 << 0.047084 << endr
+    << 0.688782 << 0.036549 << 0.552848 << 0.937664 << 0.866401 << endr
+    << 0.348740 << 0.479388 << 0.506228 << 0.145673 << 0.491547 << endr
+    << 0.148678 << 0.682258 << 0.571154 << 0.874724 << 0.444632 << endr
+    << 0.245726 << 0.595218 << 0.409327 << 0.367827 << 0.385736 << endr;
+
+  A.print("A =");
+
+  // determinant
+  cout << "det(A) = " << det(A) << endl;
+
+

+









+ +

Armadillo, simple examples

+ +

+ + +

  // inverse
+  cout << "inv(A) = " << endl << inv(A) << endl;
+  double k = 1.23;
+
+  mat    B = randu<mat>(5,5);
+  mat    C = randu<mat>(5,5);
+
+  rowvec r = randu<rowvec>(5);
+  colvec q = randu<colvec>(5);
+
+
+  // examples of some expressions
+  // for which optimised implementations exist
+  // optimised implementation of a trinary expression
+  // that results in a scalar
+  cout << "as_scalar( r*inv(diagmat(B))*q ) = ";
+  cout << as_scalar( r*inv(diagmat(B))*q ) << endl;
+
+  // example of an expression which is optimised
+  // as a call to the dgemm() function in BLAS:
+  cout << "k*trans(B)*C = " << endl << k*trans(B)*C;
+
+    return 0;
+
+

+









+ +

Gaussian Elimination

+ +

+We start with the linear set of equations + +$$ + \mathbf{A}\mathbf{x} = \mathbf{w}. +$$ + +We assume also that the matrix \( \mathbf{A} \) is non-singular and that the +matrix elements along the diagonal satisfy \( a_{ii} \ne 0 \). Simple \( 4\times 4 \) example + +$$ +\begin{bmatrix} + a_{11}& a_{12} &a_{13}& a_{14}\\ + a_{21}& a_{22} &a_{23}& a_{24}\\ + a_{31}& a_{32} &a_{33}& a_{34}\\ + a_{41}& a_{42} &a_{43}& a_{44}\\ + \end{bmatrix} \begin{bmatrix} + x_1\\ + x_2\\ + x_3 \\ + x_4 \\ + \end{bmatrix} + =\begin{bmatrix} + w_1\\ + w_2\\ + w_3 \\ + w_4\\ + \end{bmatrix}. +$$ + +

+









+ +

Gaussian Elimination

+or + +$$ +\begin{align} + a_{11}x_1 +a_{12}x_2 +a_{13}x_3 + a_{14}x_4=&w_1 \nonumber \\ +a_{21}x_1 + a_{22}x_2 + a_{23}x_3 + a_{24}x_4=&w_2 \nonumber \\ +a_{31}x_1 + a_{32}x_2 + a_{33}x_3 + a_{34}x_4=&w_3 \nonumber \\ +a_{41}x_1 + a_{42}x_2 + a_{43}x_3 + a_{44}x_4=&w_4. \nonumber +\end{align} +$$ + +

+









+ +

Gaussian Elimination

+ +

+The basic idea of Gaussian elimination is to use the first equation to eliminate the first unknown \( x_1 \) +from the remaining \( n-1 \) equations. Then we use the new second equation to eliminate the second unknown +\( x_2 \) from the remaining \( n-2 \) equations. With \( n-1 \) such eliminations +we obtain a so-called upper triangular set of equations of the form + +$$ +\begin{align} + b_{11}x_1 +b_{12}x_2 +b_{13}x_3 + b_{14}x_4=&y_1 \nonumber \\ + b_{22}x_2 + b_{23}x_3 + b_{24}x_4=&y_2 \nonumber \\ +b_{33}x_3 + b_{34}x_4=&y_3 \nonumber \\ +b_{44}x_4=&y_4. \nonumber +\label{eq:gaussbacksub} +\end{align} +$$ + +We can solve this system of equations recursively starting from \( x_n \) (in our case \( x_4 \)) and proceed with +what is called a backward substitution. + +

+









+ +

Gaussian Elimination

+This process can be expressed mathematically as + +$$ +\begin{equation} + x_m = \frac{1}{b_{mm}}\left(y_m-\sum_{k=m+1}^nb_{mk}x_k\right)\quad m=n-1,n-2,\dots,1. +\label{_auto1} +\end{equation} +$$ + +To arrive at such an upper triangular system of equations, we start by eliminating +the unknown \( x_1 \) for \( j=2,n \). We achieve this by multiplying the first equation by \( a_{j1}/a_{11} \) and then subtract +the result from the $j$th equation. We assume obviously that \( a_{11}\ne 0 \) and that +\( \mathbf{A} \) is not singular. + +

+









+ +

Gaussian Elimination

+ +

+Our actual \( 4\times 4 \) example reads after the first operation + +$$ +\begin{bmatrix} + a_{11}& a_{12} &a_{13}& a_{14}\\ + 0& (a_{22}-\frac{a_{21}a_{12}}{a_{11}}) &(a_{23}-\frac{a_{21}a_{13}}{a_{11}}) & (a_{24}-\frac{a_{21}a_{14}}{a_{11}})\\ +0& (a_{32}-\frac{a_{31}a_{12}}{a_{11}})& (a_{33}-\frac{a_{31}a_{13}}{a_{11}})& (a_{34}-\frac{a_{31}a_{14}}{a_{11}})\\ +0&(a_{42}-\frac{a_{41}a_{12}}{a_{11}}) &(a_{43}-\frac{a_{41}a_{13}}{a_{11}}) & (a_{44}-\frac{a_{41}a_{14}}{a_{11}}) \\ + \end{bmatrix} \begin{bmatrix} + x_1\\ + x_2\\ + x_3 \\ + x_4 \\ + \end{bmatrix} + =\begin{bmatrix} + y_1\\ + w_2^{(2)}\\ + w_3^{(2)} \\ + w_4^{(2)}\\ + \end{bmatrix}, +$$ + +or + +$$ +\begin{align} + b_{11}x_1 +b_{12}x_2 +b_{13}x_3 + b_{14}x_4=&y_1 \nonumber \\ + a^{(2)}_{22}x_2 + a^{(2)}_{23}x_3 + a^{(2)}_{24}x_4=&w^{(2)}_2 \nonumber \\ + a^{(2)}_{32}x_2 + a^{(2)}_{33}x_3 + a^{(2)}_{34}x_4=&w^{(2)}_3 \nonumber \\ + a^{(2)}_{42}x_2 + a^{(2)}_{43}x_3 + a^{(2)}_{44}x_4=&w^{(2)}_4, \nonumber \\ +\label{_auto2} +\end{align} +$$ + +

+









+ +

Gaussian Elimination

+ +

+The new coefficients are + +$$ +\begin{equation} + b_{1k} = a_{1k}^{(1)} \quad k=1,\dots,n, +\label{_auto3} +\end{equation} +$$ + +where each \( a_{1k}^{(1)} \) is equal to the original \( a_{1k} \) element. The other coefficients are + +$$ +\begin{equation} +a_{jk}^{(2)} = a_{jk}^{(1)}-\frac{a_{j1}^{(1)}a_{1k}^{(1)}}{a_{11}^{(1)}} \quad j,k=2,\dots,n, +\label{_auto4} +\end{equation} +$$ + +with a new right-hand side given by + +$$ +\begin{equation} +y_{1}=w_1^{(1)}, \quad w_j^{(2)} =w_j^{(1)}-\frac{a_{j1}^{(1)}w_1^{(1)}}{a_{11}^{(1)}} \quad j=2,\dots,n. +\label{_auto5} +\end{equation} +$$ + +We have also set \( w_1^{(1)}=w_1 \), the original vector element. +We see that the system of unknowns \( x_1,\dots,x_n \) is transformed into an \( (n-1)\times (n-1) \) problem. + +

+









+ +

Gaussian Elimination

+ +

+This step is called forward substitution. +Proceeding with these substitutions, we obtain the +general expressions for the new coefficients + +$$ +\begin{equation} + a_{jk}^{(m+1)} = a_{jk}^{(m)}-\frac{a_{jm}^{(m)}a_{mk}^{(m)}}{a_{mm}^{(m)}} \quad j,k=m+1,\dots,n, +\label{_auto6} +\end{equation} +$$ + +with \( m=1,\dots,n-1 \) and a +right-hand side given by + +$$ +\begin{equation} + w_j^{(m+1)} =w_j^{(m)}-\frac{a_{jm}^{(m)}w_m^{(m)}}{a_{mm}^{(m)}}\quad j=m+1,\dots,n. +\label{_auto7} +\end{equation} +$$ + +This set of \( n-1 \) elimations leads us to an equations which is solved by back substitution. +If the arithmetics is exact and the matrix \( \mathbf{A} \) is not singular, then the computed answer will be exact. + +

+Even though the matrix elements along the diagonal are not zero, +numerically small numbers may appear and subsequent divisions may lead to large numbers, which, if added +to a small number may yield losses of precision. Suppose for example that our first division in \( (a_{22}-a_{21}a_{12}/a_{11}) \) +results in \( -10^{-7} \) and that \( a_{22} \) is one. +one. We are then +adding \( 10^7+1 \). With single precision this results in \( 10^7 \). + +

+









+ +

Linear Algebra Methods

+ + + +









+ +

LU Decomposition

+ +

+The LU decomposition method means that we can rewrite +this matrix as the product of two matrices \( \mathbf{L} \) and \( \mathbf{U} \) +where + +$$ + \begin{bmatrix} + a_{11} & a_{12} & a_{13} & a_{14} \\ + a_{21} & a_{22} & a_{23} & a_{24} \\ + a_{31} & a_{32} & a_{33} & a_{34} \\ + a_{41} & a_{42} & a_{43} & a_{44} + \end{bmatrix} + = \begin{bmatrix} + 1 & 0 & 0 & 0 \\ + l_{21} & 1 & 0 & 0 \\ + l_{31} & l_{32} & 1 & 0 \\ + l_{41} & l_{42} & l_{43} & 1 + \end{bmatrix} + \begin{bmatrix} + u_{11} & u_{12} & u_{13} & u_{14} \\ + 0 & u_{22} & u_{23} & u_{24} \\ + 0 & 0 & u_{33} & u_{34} \\ + 0 & 0 & 0 & u_{44} + \end{bmatrix}. +$$ + +

+









+ +

LU Decomposition

+ +

+LU decomposition forms the backbone of other algorithms in linear algebra, such as the +solution of linear equations given by + +$$ +\begin{align} + a_{11}x_1 +a_{12}x_2 +a_{13}x_3 + a_{14}x_4=&w_1 \nonumber \\ +a_{21}x_1 + a_{22}x_2 + a_{23}x_3 + a_{24}x_4=&w_2 \nonumber \\ +a_{31}x_1 + a_{32}x_2 + a_{33}x_3 + a_{34}x_4=&w_3 \nonumber \\ +a_{41}x_1 + a_{42}x_2 + a_{43}x_3 + a_{44}x_4=&w_4. \nonumber +\end{align} +$$ + +The above set of equations is conveniently solved by using LU decomposition as an intermediate step. + +

+The matrix \( \mathbf{A}\in \mathbb{R}^{n\times n} \) has an LU factorization if the determinant +is different from zero. If the LU factorization exists and \( \mathbf{A} \) is non-singular, then the LU factorization +is unique and the determinant is given by + +$$ +det\{\mathbf{A}\}=det\{\mathbf{LU}\}= det\{\mathbf{L}\}det\{\mathbf{U}\}=u_{11}u_{22}\dots u_{nn}. +$$ + +

+









+ +

LU Decomposition, why?

+ +

+There are at least three main advantages with LU decomposition compared with standard Gaussian elimination: + +

+ +









+ +

LU Decomposition, linear equations

+ +

+With the LU decomposition it is rather +simple to solve a system of linear equations + +$$ +\begin{align} + a_{11}x_1 +a_{12}x_2 +a_{13}x_3 + a_{14}x_4=&w_1 \nonumber \\ +a_{21}x_1 + a_{22}x_2 + a_{23}x_3 + a_{24}x_4=&w_2 \nonumber \\ +a_{31}x_1 + a_{32}x_2 + a_{33}x_3 + a_{34}x_4=&w_3 \nonumber \\ +a_{41}x_1 + a_{42}x_2 + a_{43}x_3 + a_{44}x_4=&w_4. \nonumber +\end{align} +$$ + +

+This can be written in matrix form as + +$$ \mathbf{Ax}=\mathbf{w}. $$ + +

+where \( \mathbf{A} \) and \( \mathbf{w} \) are known and we have to solve for +\( \mathbf{x} \). Using the LU dcomposition we write + +$$ \mathbf{A} \mathbf{x} \equiv \mathbf{L} \mathbf{U} \mathbf{x} =\mathbf{w}. $$ + +

+









+ +

LU Decomposition, linear equations

+ +

+The previous equation can be calculated in two steps + +$$ \mathbf{L} \mathbf{y} = \mathbf{w};\qquad \mathbf{Ux}=\mathbf{y}. $$ + +

+To show that this is correct we use to the LU decomposition +to rewrite our system of linear equations as + +$$ \mathbf{LUx}=\mathbf{w}, $$ + +and since the determinat of \( \mathbf{L} \) is equal to 1 (by construction +since the diagonals of \( \mathbf{L} \) equal 1) we can use the inverse of +\( \mathbf{L} \) to obtain + +$$ + \mathbf{Ux}=\mathbf{L^{-1}w}=\mathbf{y}, +$$ + +which yields the intermediate step + +$$ + \mathbf{L^{-1}w}=\mathbf{y} +$$ + +and as soon as we have \( \mathbf{y} \) we can obtain \( \mathbf{x} \) +through \( \mathbf{Ux}=\mathbf{y} \). + +

+









+ +

LU Decomposition, why?

+ +

+For our four-dimentional example this takes the form + +$$ +\begin{align} + y_1=&w_1 \nonumber\\ +l_{21}y_1 + y_2=&w_2\nonumber \\ +l_{31}y_1 + l_{32}y_2 + y_3 =&w_3\nonumber \\ +l_{41}y_1 + l_{42}y_2 + l_{43}y_3 + y_4=&w_4. \nonumber +\end{align} +$$ + +

+and + +$$ +\begin{align} + u_{11}x_1 +u_{12}x_2 +u_{13}x_3 + u_{14}x_4=&y_1 \nonumber\\ +u_{22}x_2 + u_{23}x_3 + u_{24}x_4=&y_2\nonumber \\ +u_{33}x_3 + u_{34}x_4=&y_3\nonumber \\ +u_{44}x_4=&y_4 \nonumber +\end{align} +$$ + +

+This example shows the basis for the algorithm +needed to solve the set of \( n \) linear equations. + +

+









+ +

LU Decomposition, linear equations

+ +

+The algorithm goes as follows + +

+ +









+ +

LU Decomposition, the inverse of a matrix

+ +

+If the inverse exists then + +$$ + \mathbf{A}^{-1}\mathbf{A}=\mathbf{I}, +$$ + +the identity matrix. With an LU decomposed matrix we can rewrite the last equation as + +$$ + \mathbf{LU}\mathbf{A}^{-1}=\mathbf{I}. +$$ + +

+









+ +

LU Decomposition, the inverse of a matrix

+ +

+If we assume that the first column (that is column 1) of the inverse matrix +can be written as a vector with unknown entries + +$$ + \mathbf{A}_1^{-1}= \begin{bmatrix} + a_{11}^{-1} \\ + a_{21}^{-1} \\ + \dots \\ + a_{n1}^{-1} \\ + \end{bmatrix}, +$$ + +then we have a linear set of equations + +$$ + \mathbf{LU}\begin{bmatrix} + a_{11}^{-1} \\ + a_{21}^{-1} \\ + \dots \\ + a_{n1}^{-1} \\ + \end{bmatrix} =\begin{bmatrix} + 1 \\ + 0 \\ + \dots \\ + 0 \\ + \end{bmatrix}. +$$ + +

+









+ +

LU Decomposition, the inverse

+ +

+In a similar way we can compute the unknow entries of the second column, + +$$ + \mathbf{LU}\begin{bmatrix} + a_{12}^{-1} \\ + a_{22}^{-1} \\ + \dots \\ + a_{n2}^{-1} \\ + \end{bmatrix}=\begin{bmatrix} + 0 \\ + 1 \\ + \dots \\ + 0 \\ + \end{bmatrix}, +$$ + +and continue till we have solved all \( n \) sets of linear equations. + +

+









+ +

Using Armadillo to perform an LU decomposition

+

+ + +

#include <iostream>
+#include "armadillo"
+using namespace arma;
+using namespace std;
+
+int main()
+  {
+   mat A = randu<mat>(5,5);
+   vec b = randu<vec>(5);
+
+  A.print("A =");
+  b.print("b=");
+  // solve Ax = b
+  vec x = solve(A,b);
+  // print x
+  x.print("x=");
+  // find LU decomp of A, if needed, P is the permutation matrix
+  mat L, U;
+  lu(L,U,A);
+  // print l
+  L.print(" L= ");
+  // print U
+  U.print(" U= ");
+  //Check that A = LU
+  (A-L*U).print("Test of LU decomposition");
+    return 0;
+  }
+
+

+









+ +

Iterative methods, Chapter 6

+
+ +

+ +

+
+ + +

+









+ +

Iterative methods, Jacobi's method

+
+ +

+It is a simple method for solving +$$ +\mathbf{A}\mathbf{x}=\mathbf{b}, +$$ + +where \( \mathbf{A} \) is a matrix and \( \mathbf{x} \) and \( \mathbf{b} \) are vectors. The vector \( \mathbf{x} \) is +the unknown. + +

+It is an iterative scheme where we start with a guess for the unknown, and +after \( k+1 \) iterations we have +$$ +\mathbf{x}^{(k+1)}= \mathbf{D}^{-1}(\mathbf{b}-(\mathbf{L}+\mathbf{U})\mathbf{x}^{(k)}), +$$ + +with \( \mathbf{A}=\mathbf{D}+\mathbf{U}+\mathbf{L} \) and +\( \mathbf{D} \) being a diagonal matrix, \( \mathbf{U} \) an upper triangular matrix and \( \mathbf{L} \) a lower triangular +matrix. + +

+If the matrix \( \mathbf{A} \) is positive definite or diagonally dominant, one can show that this method will always converge to the exact solution. +

+ + +

+









+ +

Iterative methods, Jacobi's method

+
+ +

+We can demonstrate Jacobi's method by this \( 4\times 4 \) matrix problem. We assume a guess +for the vector elements \( x_i^{(0)} \), a guess which represents our first iteration. The new +values are obtained by substitution +$$ +\begin{align} + x_1^{(1)} =&(b_1-a_{12}x_2^{(0)} -a_{13}x_3^{(0)} - a_{14}x_4^{(0)})/a_{11} \nonumber \\ + x_2^{(1)} =&(b_2-a_{21}x_1^{(0)} - a_{23}x_3^{(0)} - a_{24}x_4^{(0)})/a_{22} \nonumber \\ + x_3^{(1)} =&(b_3- a_{31}x_1^{(0)} -a_{32}x_2^{(0)} -a_{34}x_4^{(0)})/a_{33} \nonumber \\ + x_4^{(1)}=&(b_4-a_{41}x_1^{(0)} -a_{42}x_2^{(0)} - a_{43}x_3^{(0)})/a_{44}, \nonumber +\end{align} +$$ + +which after \( k+1 \) iterations reads +$$ +\begin{align} + x_1^{(k+1)} =&(b_1-a_{12}x_2^{(k)} -a_{13}x_3^{(k)} - a_{14}x_4^{(k)})/a_{11} \nonumber \\ + x_2^{(k+1)} =&(b_2-a_{21}x_1^{(k)} - a_{23}x_3^{(k)} - a_{24}x_4^{(k)})/a_{22} \nonumber \\ + x_3^{(k+1)} =&(b_3- a_{31}x_1^{(k)} -a_{32}x_2^{(k)} -a_{34}x_4^{(k)})/a_{33} \nonumber \\ + x_4^{(k+1)}=&(b_4-a_{41}x_1^{(k)} -a_{42}x_2^{(k)} - a_{43}x_3^{(k)})/a_{44}, \nonumber +\end{align} +$$ +

+ + +

+









+ +

Iterative methods, Jacobi's method

+
+ +

+We can generalize the above equations to +$$ + x_i^{(k+1)}=(b_i-\sum_{j=1, j\ne i}^{n}a_{ij}x_j^{(k)})/a_{ii} +$$ + +or in an even more compact form as +$$ \mathbf{x}^{(k+1)}= \mathbf{D}^{-1}(\mathbf{b}-(\mathbf{L}+\mathbf{U})\mathbf{x}^{(k)}), +$$ + +with \( \mathbf{A}=\mathbf{D}+\mathbf{U}+\mathbf{L} \) and +\( \mathbf{D} \) being a diagonal matrix, \( \mathbf{U} \) an upper triangular matrix and \( \mathbf{L} \) a lower triangular +matrix. +

+ + +

+









+ +

Iterative methods, Gauss-Seidel's method

+
+ +

+Our \( 4\times 4 \) matrix problem +$$ +\begin{align} + x_1^{(k+1)} =&(b_1-a_{12}x_2^{(k)} -a_{13}x_3^{(k)} - a_{14}x_4^{(k)})/a_{11} \nonumber \\ + x_2^{(k+1)} =&(b_2-a_{21}x_1^{(k)} - a_{23}x_3^{(k)} - a_{24}x_4^{(k)})/a_{22} \nonumber \\ + x_3^{(k+1)} =&(b_3- a_{31}x_1^{(k)} -a_{32}x_2^{(k)} -a_{34}x_4^{(k)})/a_{33} \nonumber \\ + x_4^{(k+1)}=&(b_4-a_{41}x_1^{(k)} -a_{42}x_2^{(k)} - a_{43}x_3^{(k)})/a_{44}, \nonumber +\end{align} +$$ + +can be rewritten as +$$ +\begin{align} + x_1^{(k+1)} =&(b_1-a_{12}x_2^{(k)} -a_{13}x_3^{(k)} - a_{14}x_4^{(k)})/a_{11} \nonumber \\ + x_2^{(k+1)} =&(b_2-a_{21}x_1^{(k+1)} - a_{23}x_3^{(k)} - a_{24}x_4^{(k)})/a_{22} \nonumber \\ + x_3^{(k+1)} =&(b_3- a_{31}x_1^{(k+1)} -a_{32}x_2^{(k+1)} -a_{34}x_4^{(k)})/a_{33} \nonumber \\ + x_4^{(k+1)}=&(b_4-a_{41}x_1^{(k+1)} -a_{42}x_2^{(k+1)} - a_{43}x_3^{(k+1)})/a_{44}, \nonumber +\end{align} +$$ + +which allows us to utilize the preceding solution (forward substitution). This improves normally the convergence +behavior and leads to the Gauss-Seidel method! +

+ + +

+









+ +

Iterative methods, Gauss-Seidel's method

+
+ +

+We can generalize +$$ +\begin{align} + x_1^{(k+1)} =&(b_1-a_{12}x_2^{(k)} -a_{13}x_3^{(k)} - a_{14}x_4^{(k)})/a_{11} \nonumber \\ + x_2^{(k+1)} =&(b_2-a_{21}x_1^{(k+1)} - a_{23}x_3^{(k)} - a_{24}x_4^{(k)})/a_{22} \nonumber \\ + x_3^{(k+1)} =&(b_3- a_{31}x_1^{(k+1)} -a_{32}x_2^{(k+1)} -a_{34}x_4^{(k)})/a_{33} \nonumber \\ + x_4^{(k+1)}=&(b_4-a_{41}x_1^{(k+1)} -a_{42}x_2^{(k+1)} - a_{43}x_3^{(k+1)})/a_{44}, \nonumber +\end{align} +$$ + +to the following form +$$ + x^{(k+1)}_i = \frac{1}{a_{ii}} \left(b_i - \sum_{j > i}a_{ij}x^{(k)}_j - \sum_{j < i}a_{ij}x^{(k+1)}_j \right),\quad i=1,2,\ldots,n. +$$ + +The procedure is generally continued until the changes made by an iteration are below some tolerance. + +

+The convergence properties of the Jacobi method and the +Gauss-Seidel method are dependent on the matrix \( \mathbf{A} \). These methods converge when +the matrix is symmetric positive-definite, or is strictly or irreducibly diagonally dominant. +Both methods sometimes converge even if these conditions are not satisfied. +

+ + +

+









+ +

Iterative methods, Successive over-relaxation

+
+ +

+Given a square system of n linear equations with unknown \( \mathbf x \): +$$ + \mathbf{A}\mathbf x = \mathbf b +$$ + +where +$$ + \mathbf{A}=\begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\a_{n1} & a_{n2} & \cdots & a_{nn} \end{bmatrix}, \qquad \mathbf{x} = \begin{bmatrix} x_{1} \\ x_2 \\ \vdots \\ x_n \end{bmatrix} , \qquad \mathbf{b} = \begin{bmatrix} b_{1} \\ b_2 \\ \vdots \\ b_n \end{bmatrix}. +$$ +

+ + +

+









+ +

Iterative methods, Successive over-relaxation

+
+ +

+Then A can be decomposed into a diagonal component D, and strictly lower and upper triangular components L and U: +$$ + \mathbf{A} =\mathbf{D} + \mathbf{L} + \mathbf{U}, +$$ + +where +$$ + D = \begin{bmatrix} a_{11} & 0 & \cdots & 0 \\ 0 & a_{22} & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\0 & 0 & \cdots & a_{nn} \end{bmatrix}, \quad L = \begin{bmatrix} 0 & 0 & \cdots & 0 \\ a_{21} & 0 & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\a_{n1} & a_{n2} & \cdots & 0 \end{bmatrix}, \quad U = \begin{bmatrix} 0 & a_{12} & \cdots & a_{1n} \\ 0 & 0 & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\0 & 0 & \cdots & 0 \end{bmatrix}. +$$ + +The system of linear equations may be rewritten as: +$$ + (D+\omega L) \mathbf{x} = \omega \mathbf{b} - [\omega U + (\omega-1) D ] \mathbf{x} +$$ + +for a constant \( \omega > 1 \). +

+ + +

+









+ +

Iterative methods, Successive over-relaxation

+
+ +

+The method of successive over-relaxation is an iterative technique that solves the left hand side of this expression for \( x \), using previous value for \( x \) on the right hand side. Analytically, this may be written as: +$$ + \mathbf{x}^{(k+1)} = (D+\omega L)^{-1} \big(\omega \mathbf{b} - [\omega U + (\omega-1) D ] \mathbf{x}^{(k)}\big). +$$ + +However, by taking advantage of the triangular form of \( (D+\omega L) \), the elements of \( x^{(k+1)} \) can be computed sequentially using forward substitution: +$$ + x^{(k+1)}_i = (1-\omega)x^{(k)}_i + \frac{\omega}{a_{ii}} \left(b_i - \sum_{j > i} a_{ij}x^{(k)}_j - \sum_{j < i} a_{ij}x^{(k+1)}_j \right),\quad i=1,2,\ldots,n. +$$ + +The choice of relaxation factor is not necessarily easy, and depends upon the properties of the coefficient matrix. For symmetric, positive-definite matrices it can be proven that \( 0 < \omega < 2 \) will lead to convergence, but we are generally interested in faster convergence rather than just convergence. +

+ + +

+ + + + +

+ © 1999-2018, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license +
+ + + + + + diff --git a/doc/pub/Linalg/html/reveal.js/.gitignore b/doc/pub/Linalg/html/reveal.js/.gitignore new file mode 100644 index 000000000..a5df3133d --- /dev/null +++ b/doc/pub/Linalg/html/reveal.js/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +.svn +log/*.log +tmp/** +node_modules/ +.sass-cache +css/reveal.min.css +js/reveal.min.js diff --git a/doc/pub/Linalg/html/reveal.js/.travis.yml b/doc/pub/Linalg/html/reveal.js/.travis.yml new file mode 100644 index 000000000..165d9ae9f --- /dev/null +++ b/doc/pub/Linalg/html/reveal.js/.travis.yml @@ -0,0 +1,5 @@ +language: node_js +node_js: + - 0.10 +before_script: + - npm install -g grunt-cli \ No newline at end of file diff --git a/doc/pub/Linalg/html/reveal.js/CONTRIBUTING.md b/doc/pub/Linalg/html/reveal.js/CONTRIBUTING.md new file mode 100644 index 000000000..c2091e88f --- /dev/null +++ b/doc/pub/Linalg/html/reveal.js/CONTRIBUTING.md @@ -0,0 +1,23 @@ +## Contributing + +Please keep the [issue tracker](http://github.com/hakimel/reveal.js/issues) limited to **bug reports**, **feature requests** and **pull requests**. + + +### Personal Support +If you have personal support or setup questions the best place to ask those are [StackOverflow](http://stackoverflow.com/questions/tagged/reveal.js). + + +### Bug Reports +When reporting a bug make sure to include information about which browser and operating system you are on as well as the necessary steps to reproduce the issue. If possible please include a link to a sample presentation where the bug can be tested. + + +### Pull Requests +- Should follow the coding style of the file you work in, most importantly: + - Tabs to indent + - Single-quoted strings +- Should be made towards the **dev branch** +- Should be submitted from a feature/topic branch (not your master) + + +### Plugins +Please do not submit plugins as pull requests. They should be maintained in their own separate repository. More information here: https://github.com/hakimel/reveal.js/wiki/Plugin-Guidelines diff --git a/doc/pub/Linalg/html/reveal.js/Gruntfile.js b/doc/pub/Linalg/html/reveal.js/Gruntfile.js new file mode 100644 index 000000000..b257e8f32 --- /dev/null +++ b/doc/pub/Linalg/html/reveal.js/Gruntfile.js @@ -0,0 +1,140 @@ +/* global module:false */ +module.exports = function(grunt) { + var port = grunt.option('port') || 8000; + // Project configuration + grunt.initConfig({ + pkg: grunt.file.readJSON('package.json'), + meta: { + banner: + '/*!\n' + + ' * reveal.js <%= pkg.version %> (<%= grunt.template.today("yyyy-mm-dd, HH:MM") %>)\n' + + ' * http://lab.hakim.se/reveal-js\n' + + ' * MIT licensed\n' + + ' *\n' + + ' * Copyright (C) 2014 Hakim El Hattab, http://hakim.se\n' + + ' */' + }, + + qunit: { + files: [ 'test/*.html' ] + }, + + uglify: { + options: { + banner: '<%= meta.banner %>\n' + }, + build: { + src: 'js/reveal.js', + dest: 'js/reveal.min.js' + } + }, + + cssmin: { + compress: { + files: { + 'css/reveal.min.css': [ 'css/reveal.css' ] + } + } + }, + + sass: { + main: { + files: { + 'css/theme/darkgray.css': 'css/theme/source/darkgray.scss', + 'css/theme/beigesmall.css': 'css/theme/source/beigesmall.scss', + 'css/theme/cbc.css': 'css/theme/source/cbc.scss', + 'css/theme/default.css': 'css/theme/source/default.scss', + 'css/theme/beige.css': 'css/theme/source/beige.scss', + 'css/theme/night.css': 'css/theme/source/night.scss', + 'css/theme/serif.css': 'css/theme/source/serif.scss', + 'css/theme/simple.css': 'css/theme/source/simple.scss', + 'css/theme/sky.css': 'css/theme/source/sky.scss', + 'css/theme/moon.css': 'css/theme/source/moon.scss', + 'css/theme/solarized.css': 'css/theme/source/solarized.scss', + 'css/theme/blood.css': 'css/theme/source/blood.scss' + } + } + }, + + jshint: { + options: { + curly: false, + eqeqeq: true, + immed: true, + latedef: true, + newcap: true, + noarg: true, + sub: true, + undef: true, + eqnull: true, + browser: true, + expr: true, + globals: { + head: false, + module: false, + console: false, + unescape: false + } + }, + files: [ 'Gruntfile.js', 'js/reveal.js' ] + }, + + connect: { + server: { + options: { + port: port, + base: '.' + } + } + }, + + zip: { + 'reveal-js-presentation.zip': [ + 'index.html', + 'css/**', + 'js/**', + 'lib/**', + 'images/**', + 'plugin/**' + ] + }, + + watch: { + main: { + files: [ 'Gruntfile.js', 'js/reveal.js', 'css/reveal.css' ], + tasks: 'default' + }, + theme: { + files: [ 'css/theme/source/*.scss', 'css/theme/template/*.scss' ], + tasks: 'themes' + } + } + + }); + + // Dependencies + grunt.loadNpmTasks( 'grunt-contrib-qunit' ); + grunt.loadNpmTasks( 'grunt-contrib-jshint' ); + grunt.loadNpmTasks( 'grunt-contrib-cssmin' ); + grunt.loadNpmTasks( 'grunt-contrib-uglify' ); + grunt.loadNpmTasks( 'grunt-contrib-watch' ); + grunt.loadNpmTasks( 'grunt-contrib-sass' ); + grunt.loadNpmTasks( 'grunt-contrib-connect' ); + grunt.loadNpmTasks( 'grunt-zip' ); + + // Default task + grunt.registerTask( 'default', [ 'jshint', 'cssmin', 'uglify', 'qunit' ] ); + + // Theme task + grunt.registerTask( 'themes', [ 'sass' ] ); + + // Package presentation to archive + grunt.registerTask( 'package', [ 'default', 'zip' ] ); + + // Serve presentation locally + grunt.registerTask( 'serve', [ 'connect', 'watch' ] ); + + // Run tests + grunt.registerTask( 'test', [ 'jshint', 'qunit' ] ); + +}; diff --git a/doc/pub/Linalg/html/reveal.js/LICENSE b/doc/pub/Linalg/html/reveal.js/LICENSE new file mode 100644 index 000000000..09623076f --- /dev/null +++ b/doc/pub/Linalg/html/reveal.js/LICENSE @@ -0,0 +1,19 @@ +Copyright (C) 2015 Hakim El Hattab, http://hakim.se + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/doc/pub/Linalg/html/reveal.js/README.md b/doc/pub/Linalg/html/reveal.js/README.md new file mode 100644 index 000000000..573b19597 --- /dev/null +++ b/doc/pub/Linalg/html/reveal.js/README.md @@ -0,0 +1,1052 @@ +# reveal.js [![Build Status](https://travis-ci.org/hakimel/reveal.js.svg?branch=master)](https://travis-ci.org/hakimel/reveal.js) + +A framework for easily creating beautiful presentations using HTML. [Check out the live demo](http://lab.hakim.se/reveal-js/). + +reveal.js comes with a broad range of features including [nested slides](https://github.com/hakimel/reveal.js#markup), [Markdown contents](https://github.com/hakimel/reveal.js#markdown), [PDF export](https://github.com/hakimel/reveal.js#pdf-export), [speaker notes](https://github.com/hakimel/reveal.js#speaker-notes) and a [JavaScript API](https://github.com/hakimel/reveal.js#api). It's best viewed in a modern browser but [fallbacks](https://github.com/hakimel/reveal.js/wiki/Browser-Support) are available to make sure your presentation can still be viewed elsewhere. + + +#### More reading: +- [Installation](#installation): Step-by-step instructions for getting reveal.js running on your computer. +- [Changelog](https://github.com/hakimel/reveal.js/releases): Up-to-date version history. +- [Examples](https://github.com/hakimel/reveal.js/wiki/Example-Presentations): Presentations created with reveal.js, add your own! +- [Browser Support](https://github.com/hakimel/reveal.js/wiki/Browser-Support): Explanation of browser support and fallbacks. +- [Plugins](https://github.com/hakimel/reveal.js/wiki/Plugins,-Tools-and-Hardware): A list of plugins that can be used to extend reveal.js. + +## Online Editor + +Presentations are written using HTML or Markdown but there's also an online editor for those of you who prefer a graphical interface. Give it a try at [http://slides.com](http://slides.com). + + +## Instructions + +### Markup + +Markup hierarchy needs to be ``
`` where the ``
`` represents one slide and can be repeated indefinitely. If you place multiple ``
``'s inside of another ``
`` they will be shown as vertical slides. The first of the vertical slides is the "root" of the others (at the top), and it will be included in the horizontal sequence. For example: + +```html +
+
+
Single Horizontal Slide
+
+
Vertical Slide 1
+
Vertical Slide 2
+
+
+
+``` + +### Markdown + +It's possible to write your slides using Markdown. To enable Markdown, add the ```data-markdown``` attribute to your ```
``` elements and wrap the contents in a ``` +
+``` + +#### External Markdown + +You can write your content as a separate file and have reveal.js load it at runtime. Note the separator arguments which determine how slides are delimited in the external file. The ```data-charset``` attribute is optional and specifies which charset to use when loading the external file. + +When used locally, this feature requires that reveal.js [runs from a local web server](#full-setup). + +```html +
+
+``` + +#### Element Attributes + +Special syntax (in html comment) is available for adding attributes to Markdown elements. This is useful for fragments, amongst other things. + +```html +
+ +
+``` + +#### Slide Attributes + +Special syntax (in html comment) is available for adding attributes to the slide `
` elements generated by your Markdown. + +```html +
+ +
+``` + + +### Configuration + +At the end of your page you need to initialize reveal by running the following code. Note that all config values are optional and will default as specified below. + +```javascript +Reveal.initialize({ + + // Display controls in the bottom right corner + controls: true, + + // Display a presentation progress bar + progress: true, + + // Display the page number of the current slide + slideNumber: false, + + // Push each slide change to the browser history + history: false, + + // Enable keyboard shortcuts for navigation + keyboard: true, + + // Enable the slide overview mode + overview: true, + + // Vertical centering of slides + center: true, + + // Enables touch navigation on devices with touch input + touch: true, + + // Loop the presentation + loop: false, + + // Change the presentation direction to be RTL + rtl: false, + + // Turns fragments on and off globally + fragments: true, + + // Flags if the presentation is running in an embedded mode, + // i.e. contained within a limited portion of the screen + embedded: false, + + // Flags if we should show a help overlay when the questionmark + // key is pressed + help: true, + + // Number of milliseconds between automatically proceeding to the + // next slide, disabled when set to 0, this value can be overwritten + // by using a data-autoslide attribute on your slides + autoSlide: 0, + + // Stop auto-sliding after user input + autoSlideStoppable: true, + + // Enable slide navigation via mouse wheel + mouseWheel: false, + + // Hides the address bar on mobile devices + hideAddressBar: true, + + // Opens links in an iframe preview overlay + previewLinks: false, + + // Transition style + transition: 'default', // none/fade/slide/convex/concave/zoom + + // Transition speed + transitionSpeed: 'default', // default/fast/slow + + // Transition style for full page slide backgrounds + backgroundTransition: 'default', // none/fade/slide/convex/concave/zoom + + // Number of slides away from the current that are visible + viewDistance: 3, + + // Parallax background image + parallaxBackgroundImage: '', // e.g. "'https://s3.amazonaws.com/hakim-static/reveal-js/reveal-parallax-1.jpg'" + + // Parallax background size + parallaxBackgroundSize: '', // CSS syntax, e.g. "2100px 900px" + + // Amount to move parallax background (horizontal and vertical) on slide change + // Number, e.g. 100 + parallaxBackgroundHorizontal: '', + parallaxBackgroundVertical: '' + +}); +``` + + +The configuration can be updated after initialization using the ```configure``` method: + +```javascript +// Turn autoSlide off +Reveal.configure({ autoSlide: 0 }); + +// Start auto-sliding every 5s +Reveal.configure({ autoSlide: 5000 }); +``` + + +### Dependencies + +Reveal.js doesn't _rely_ on any third party scripts to work but a few optional libraries are included by default. These libraries are loaded as dependencies in the order they appear, for example: + +```javascript +Reveal.initialize({ + dependencies: [ + // Cross-browser shim that fully implements classList - https://github.com/eligrey/classList.js/ + { src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } }, + + // Interpret Markdown in
elements + { src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } }, + { src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } }, + + // Syntax highlight for elements + { src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } }, + + // Zoom in and out with Alt+click + { src: 'plugin/zoom-js/zoom.js', async: true }, + + // Speaker notes + { src: 'plugin/notes/notes.js', async: true }, + + // Remote control your reveal.js presentation using a touch device + { src: 'plugin/remotes/remotes.js', async: true }, + + // MathJax + { src: 'plugin/math/math.js', async: true } + ] +}); +``` + +You can add your own extensions using the same syntax. The following properties are available for each dependency object: +- **src**: Path to the script to load +- **async**: [optional] Flags if the script should load after reveal.js has started, defaults to false +- **callback**: [optional] Function to execute when the script has loaded +- **condition**: [optional] Function which must return true for the script to be loaded + + +### Ready Event + +A 'ready' event is fired when reveal.js has loaded all non-async dependencies and is ready to start navigating. To check if reveal.js is already 'ready' you can call `Reveal.isReady()`. + +```javascript +Reveal.addEventListener( 'ready', function( event ) { + // event.currentSlide, event.indexh, event.indexv +} ); +``` + + +### Presentation Size + +All presentations have a normal size, that is the resolution at which they are authored. The framework will automatically scale presentations uniformly based on this size to ensure that everything fits on any given display or viewport. + +See below for a list of configuration options related to sizing, including default values: + +```javascript +Reveal.initialize({ + + ... + + // The "normal" size of the presentation, aspect ratio will be preserved + // when the presentation is scaled to fit different resolutions. Can be + // specified using percentage units. + width: 960, + height: 700, + + // Factor of the display size that should remain empty around the content + margin: 0.1, + + // Bounds for smallest/largest possible scale to apply to content + minScale: 0.2, + maxScale: 1.5 + +}); +``` + + +### Auto-sliding + +Presentations can be configured to progress through slides automatically, without any user input. To enable this you will need to tell the framework how many milliseconds it should wait between slides: + +```javascript +// Slide every five seconds +Reveal.configure({ + autoSlide: 5000 +}); +``` +When this is turned on a control element will appear that enables users to pause and resume auto-sliding. Alternatively, sliding can be paused or resumed by pressing »a« on the keyboard. Sliding is paused automatically as soon as the user starts navigating. You can disable these controls by specifying ```autoSlideStoppable: false``` in your reveal.js config. + +You can also override the slide duration for individual slides and fragments by using the ```data-autoslide``` attribute: + +```html +
+

After 2 seconds the first fragment will be shown.

+

After 10 seconds the next fragment will be shown.

+

Now, the fragment is displayed for 2 seconds before the next slide is shown.

+
+``` + +Whenever the auto-slide mode is resumed or paused the ```autoslideresumed``` and ```autoslidepaused``` events are fired. + + +### Keyboard Bindings + +If you're unhappy with any of the default keyboard bindings you can override them using the ```keyboard``` config option: + +```javascript +Reveal.configure({ + keyboard: { + 13: 'next', // go to the next slide when the ENTER key is pressed + 27: function() {}, // do something custom when ESC is pressed + 32: null // don't do anything when SPACE is pressed (i.e. disable a reveal.js default binding) + } +}); +``` + +### Lazy Loading + +When working on presentation with a lot of media or iframe content it's important to load lazily. Lazy loading means that reveal.js will only load content for the few slides nearest to the current slide. The number of slides that are preloaded is determined by the `viewDistance` configuration option. + +To enable lazy loading all you need to do is change your "src" attributes to "data-src" as shown below. This is supported for image, video, audio and iframe elements. Lazy loaded iframes will also unload when the containing slide is no longer visible. + +```html +
+ + + +
+``` + + +### API + +The ``Reveal`` object exposes a JavaScript API for controlling navigation and reading state: + +```javascript +// Navigation +Reveal.slide( indexh, indexv, indexf ); +Reveal.left(); +Reveal.right(); +Reveal.up(); +Reveal.down(); +Reveal.prev(); +Reveal.next(); +Reveal.prevFragment(); +Reveal.nextFragment(); + +// Toggle presentation states, optionally pass true/false to force on/off +Reveal.toggleOverview(); +Reveal.togglePause(); +Reveal.toggleAutoSlide(); + +// Change a config value at runtime +Reveal.configure({ controls: true }); + +// Returns the present configuration options +Reveal.getConfig(); + +// Fetch the current scale of the presentation +Reveal.getScale(); + +// Retrieves the previous and current slide elements +Reveal.getPreviousSlide(); +Reveal.getCurrentSlide(); + +Reveal.getIndices(); // { h: 0, v: 0 } } +Reveal.getProgress(); // 0-1 +Reveal.getTotalSlides(); + +// State checks +Reveal.isFirstSlide(); +Reveal.isLastSlide(); +Reveal.isOverview(); +Reveal.isPaused(); +Reveal.isAutoSliding(); +``` + +### Slide Changed Event + +A 'slidechanged' event is fired each time the slide is changed (regardless of state). The event object holds the index values of the current slide as well as a reference to the previous and current slide HTML nodes. + +Some libraries, like MathJax (see [#226](https://github.com/hakimel/reveal.js/issues/226#issuecomment-10261609)), get confused by the transforms and display states of slides. Often times, this can be fixed by calling their update or render function from this callback. + +```javascript +Reveal.addEventListener( 'slidechanged', function( event ) { + // event.previousSlide, event.currentSlide, event.indexh, event.indexv +} ); +``` + +### Presentation State + +The presentation's current state can be fetched by using the `getState` method. A state object contains all of the information required to put the presentation back as it was when `getState` was first called. Sort of like a snapshot. It's a simple object that can easily be stringified and persisted or sent over the wire. + +```javascript +Reveal.slide( 1 ); +// we're on slide 1 + +var state = Reveal.getState(); + +Reveal.slide( 3 ); +// we're on slide 3 + +Reveal.setState( state ); +// we're back on slide 1 +``` + +### Slide States + +If you set ``data-state="somestate"`` on a slide ``
``, "somestate" will be applied as a class on the document element when that slide is opened. This allows you to apply broad style changes to the page based on the active slide. + +Furthermore you can also listen to these changes in state via JavaScript: + +```javascript +Reveal.addEventListener( 'somestate', function() { + // TODO: Sprinkle magic +}, false ); +``` + +### Slide Backgrounds + +Slides are contained within a limited portion of the screen by default to allow them to fit any display and scale uniformly. You can apply full page backgrounds outside of the slide area by adding a ```data-background``` attribute to your ```
``` elements. Four different types of backgrounds are supported: color, image, video and iframe. Below are a few examples. + +```html +
+

All CSS color formats are supported, like rgba() or hsl().

+
+
+

This slide will have a full-size background image.

+
+
+

This background image will be sized to 100px and repeated.

+
+
+

Video. Multiple sources can be defined using a comma separated list. Video will loop when the data-background-video-loop attribute is provided.

+
+
+

Embeds a web page as a background. Note that the page won't be interactive.

+
+``` + +Backgrounds transition using a fade animation by default. This can be changed to a linear sliding transition by passing ```backgroundTransition: 'slide'``` to the ```Reveal.initialize()``` call. Alternatively you can set ```data-background-transition``` on any section with a background to override that specific transition. + + +### Parallax Background + +If you want to use a parallax scrolling background, set the first two config properties below when initializing reveal.js (the other two are optional). + +```javascript +Reveal.initialize({ + + // Parallax background image + parallaxBackgroundImage: '', // e.g. "https://s3.amazonaws.com/hakim-static/reveal-js/reveal-parallax-1.jpg" + + // Parallax background size + parallaxBackgroundSize: '', // CSS syntax, e.g. "2100px 900px" - currently only pixels are supported (don't use % or auto) + + // Amount of pixels to move the parallax background per slide step, + // a value of 0 disables movement along the given axis + // These are optional, if they aren't specified they'll be calculated automatically + parallaxBackgroundHorizontal: 200, + parallaxBackgroundVertical: 50 + +}); +``` + +Make sure that the background size is much bigger than screen size to allow for some scrolling. [View example](http://lab.hakim.se/reveal-js/?parallaxBackgroundImage=https%3A%2F%2Fs3.amazonaws.com%2Fhakim-static%2Freveal-js%2Freveal-parallax-1.jpg¶llaxBackgroundSize=2100px%20900px). + + + +### Slide Transitions +The global presentation transition is set using the ```transition``` config value. You can override the global transition for a specific slide by using the ```data-transition``` attribute: + +```html +
+

This slide will override the presentation transition and zoom!

+
+ +
+

Choose from three transition speeds: default, fast or slow!

+
+``` + +You can also use different in and out transitions for the same slide: + +```html +
+ The train goes on … +
+
+ and on … +
+
+ and stops. +
+
+ (Passengers entering and leaving) +
+
+ And it starts again. +
+``` + + +Note that this does not work with the page and cube transitions. + + +### Internal links + +It's easy to link between slides. The first example below targets the index of another slide whereas the second targets a slide with an ID attribute (```
```): + +```html +Link +Link +``` + +You can also add relative navigation links, similar to the built in reveal.js controls, by appending one of the following classes on any element. Note that each element is automatically given an ```enabled``` class when it's a valid navigation route based on the current slide. + +```html + + + + + + +``` + + +### Fragments +Fragments are used to highlight individual elements on a slide. Every element with the class ```fragment``` will be stepped through before moving on to the next slide. Here's an example: http://lab.hakim.se/reveal-js/#/fragments + +The default fragment style is to start out invisible and fade in. This style can be changed by appending a different class to the fragment: + +```html +
+

grow

+

shrink

+

fade-out

+

visible only once

+

blue only once

+

highlight-red

+

highlight-green

+

highlight-blue

+
+``` + +Multiple fragments can be applied to the same element sequentially by wrapping it, this will fade in the text on the first step and fade it back out on the second. + +```html +
+ + I'll fade in, then out + +
+``` + +The display order of fragments can be controlled using the ```data-fragment-index``` attribute. + +```html +
+

Appears last

+

Appears first

+

Appears second

+
+``` + +### Fragment events + +When a slide fragment is either shown or hidden reveal.js will dispatch an event. + +Some libraries, like MathJax (see #505), get confused by the initially hidden fragment elements. Often times this can be fixed by calling their update or render function from this callback. + +```javascript +Reveal.addEventListener( 'fragmentshown', function( event ) { + // event.fragment = the fragment DOM element +} ); +Reveal.addEventListener( 'fragmenthidden', function( event ) { + // event.fragment = the fragment DOM element +} ); +``` + +### Code syntax highlighting + +By default, Reveal is configured with [highlight.js](http://softwaremaniacs.org/soft/highlight/en/) for code syntax highlighting. Below is an example with clojure code that will be syntax highlighted. When the `data-trim` attribute is present surrounding whitespace is automatically removed. + +```html +
+

+(def lazy-fib
+  (concat
+   [0 1]
+   ((fn rfib [a b]
+        (lazy-cons (+ a b) (rfib b (+ a b)))) 0 1)))
+	
+
+``` + +### Slide number +If you would like to display the page number of the current slide you can do so using the ```slideNumber``` configuration value. + +```javascript +// Shows the slide number using default formatting +Reveal.configure({ slideNumber: true }); + +// Slide number formatting can be configured using these variables: +// h: current slide's horizontal index +// v: current slide's vertical index +// c: current slide index (flattened) +// t: total number of slides (flattened) +Reveal.configure({ slideNumber: 'c / t' }); + +``` + + +### Overview mode + +Press "Esc" or "o" keys to toggle the overview mode on and off. While you're in this mode, you can still navigate between slides, +as if you were at 1,000 feet above your presentation. The overview mode comes with a few API hooks: + +```javascript +Reveal.addEventListener( 'overviewshown', function( event ) { /* ... */ } ); +Reveal.addEventListener( 'overviewhidden', function( event ) { /* ... */ } ); + +// Toggle the overview mode programmatically +Reveal.toggleOverview(); +``` + +### Fullscreen mode +Just press »F« on your keyboard to show your presentation in fullscreen mode. Press the »ESC« key to exit fullscreen mode. + + +### Embedded media +Embedded HTML5 `
+ +
+ +

 

 

 

+ + + + + +
+

Data Analysis and Machine Learning Lectures: Cubic Splines and Gradient Methods

+ +

+ + +

+Morten Hjorth-Jensen [1, 2] +
+ +

+ + +

[1] Department of Physics, University of Oslo
+
[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University
+
+

+

Jan 27, 2018

+
+

+ +

+ + + +

Cubic Splines

+
+
+

+Cubic spline interpolation is among one of the most used +methods for interpolating between data points where the arguments +are organized as ascending series. In the library program we supply +such a function, based on the so-called cubic spline method to be +described below. + +

+A spline function consists of polynomial pieces defined on +subintervals. The different subintervals are connected via +various continuity relations. + +

+Assume we have at our disposal \( n+1 \) points \( x_0, x_1, \dots x_n \) +arranged so that \( x_0 < x_1 < x_2 < \dots x_{n-1} < x_n \) (such points are called +knots). A spline function \( s \) of degree \( k \) with \( n+1 \) knots is defined +as follows + +

    +
  • On every subinterval \( [x_{i-1},x_i) \) s is a polynomial of degree \( \le k \).
  • +
  • \( s \) has \( k-1 \) continuous derivatives in the whole interval \( [x_0,x_n] \).
  • +
+
+
+ + +

+ + +

Splines

+
+
+

+As an example, consider a spline function of degree \( k=1 \) defined as follows +$$ + s(x)=\begin{bmatrix} s_0(x)=a_0x+b_0 & x\in [x_0, x_1) \\ + s_1(x)=a_1x+b_1 & x\in [x_1, x_2) \\ + \dots & \dots \\ + s_{n-1}(x)=a_{n-1}x+b_{n-1} & x\in + [x_{n-1}, x_n] \end{bmatrix}. +$$ + +In this case the polynomial consists of series of straight lines +connected to each other at every endpoint. The number of continuous +derivatives is then \( k-1=0 \), as expected when we deal with straight lines. +Such a polynomial is quite easy to construct given +\( n+1 \) points \( x_0, x_1, \dots x_n \) and their corresponding +function values. +

+
+ + +

+ + +

Splines

+
+
+

+The most commonly used spline function is the one with \( k=3 \), the so-called +cubic spline function. +Assume that we have in adddition to the \( n+1 \) knots a series of +functions values \( y_0=f(x_0), y_1=f(x_1), \dots y_n=f(x_n) \). +By definition, the polynomials \( s_{i-1} \) and \( s_i \) +are thence supposed to interpolate the same point \( i \), that is +$$ + s_{i-1}(x_i)= y_i = s_i(x_i), +$$ + +with \( 1 \le i \le n-1 \). In total we have \( n \) polynomials of the +type +$$ + s_i(x)=a_{i0}+a_{i1}x+a_{i2}x^2+a_{i2}x^3, +$$ + +yielding \( 4n \) coefficients to determine. +

+
+ + +

+ + +

Splines

+
+
+

+Every subinterval provides in addition the \( 2n \) conditions +$$ + y_i = s(x_i), +$$ + +and +$$ + s(x_{i+1})= y_{i+1}, +$$ + +to be fulfilled. If we also assume that \( s' \) and \( s'' \) are continuous, +then +$$ + s'_{i-1}(x_i)= s'_i(x_i), +$$ + +yields \( n-1 \) conditions. Similarly, +$$ + s''_{i-1}(x_i)= s''_i(x_i), +$$ + +results in additional \( n-1 \) conditions. In total we have \( 4n \) coefficients +and \( 4n-2 \) equations to determine them, leaving us with \( 2 \) degrees of +freedom to be determined. +

+
+ + +

+ + +

Splines

+
+
+

+Using the last equation we define two values for the second derivative, namely +$$ + s''_{i}(x_i)= f_i, +$$ + +and +$$ + s''_{i}(x_{i+1})= f_{i+1}, +$$ + +and setting up a straight line between \( f_i \) and \( f_{i+1} \) we have +$$ + s_i''(x) = \frac{f_i}{x_{i+1}-x_i}(x_{i+1}-x)+ + \frac{f_{i+1}}{x_{i+1}-x_i}(x-x_i), +$$ + +and integrating twice one obtains +$$ + s_i(x) = \frac{f_i}{6(x_{i+1}-x_i)}(x_{i+1}-x)^3+ + \frac{f_{i+1}}{6(x_{i+1}-x_i)}(x-x_i)^3 + +c(x-x_i)+d(x_{i+1}-x). +$$ +

+
+ + +

+ + +

Splines

+
+
+

+Using the conditions \( s_i(x_i)=y_i \) and \( s_i(x_{i+1})=y_{i+1} \) +we can in turn determine the constants \( c \) and \( d \) resulting in +$$ +\begin{align} + s_i(x) =&\frac{f_i}{6(x_{i+1}-x_i)}(x_{i+1}-x)^3+ + \frac{f_{i+1}}{6(x_{i+1}-x_i)}(x-x_i)^3 \nonumber \\ + +&(\frac{y_{i+1}}{x_{i+1}-x_i}-\frac{f_{i+1}(x_{i+1}-x_i)}{6}) + (x-x_i)+ + (\frac{y_{i}}{x_{i+1}-x_i}-\frac{f_{i}(x_{i+1}-x_i)}{6}) + (x_{i+1}-x). +\label{_auto1} +\end{align} +$$ +

+
+ + +

+ + +

Splines

+
+
+

+How to determine the values of the second +derivatives \( f_{i} \) and \( f_{i+1} \)? We use the continuity assumption +of the first derivatives +$$ + s'_{i-1}(x_i)= s'_i(x_i), +$$ + +and set \( x=x_i \). Defining \( h_i=x_{i+1}-x_i \) we obtain finally +the following expression +$$ + h_{i-1}f_{i-1}+2(h_{i}+h_{i-1})f_i+h_if_{i+1}= + \frac{6}{h_i}(y_{i+1}-y_i)-\frac{6}{h_{i-1}}(y_{i}-y_{i-1}), +$$ + +and introducing the shorthands \( u_i=2(h_{i}+h_{i-1}) \), +\( v_i=\frac{6}{h_i}(y_{i+1}-y_i)-\frac{6}{h_{i-1}}(y_{i}-y_{i-1}) \), +we can reformulate the problem as a set of linear equations to be +solved through e.g., Gaussian elemination +

+
+ + +

+ + +

Splines

+
+
+

+Gaussian elimination +$$ + \begin{bmatrix} u_1 & h_1 &0 &\dots & & & & \\ + h_1 & u_2 & h_2 &0 &\dots & & & \\ + 0 & h_2 & u_3 & h_3 &0 &\dots & & \\ + \dots& & \dots &\dots &\dots &\dots &\dots & \\ + &\dots & & &0 &h_{n-3} &u_{n-2} &h_{n-2} \\ + & && & &0 &h_{n-2} &u_{n-1} \end{bmatrix} + \begin{bmatrix} f_1 \\ + f_2 \\ + f_3\\ + \dots \\ + f_{n-2} \\ + f_{n-1} \end{bmatrix} = + \begin{bmatrix} v_1 \\ + v_2 \\ + v_3\\ + \dots \\ + v_{n-2}\\ + v_{n-1} \end{bmatrix}. +$$ + +Note that this is a set of tridiagonal equations and can be solved +through only \( O(n) \) operations. +

+
+ + +

+ + +

Splines

+
+
+

+The functions supplied in the program library are spline and splint. +In order to use cubic spline interpolation you need first to call + +

+ + +

spline(double x[], double y[], int n, double yp1,  double yp2, double y2[])
+
+

+This function takes as +input \( x[0,..,n - 1] \) and \( y[0,..,n - 1] \) containing a tabulation +\( y_i = f(x_i) \) with \( x_0 < x_1 < .. < x_{n - 1} \) +together with the +first derivatives of \( f(x) \) at \( x_0 \) and \( x_{n-1} \), respectively. Then the +function returns \( y2[0,..,n-1] \) which contains the second derivatives of +\( f(x_i) \) at each point \( x_i \). \( n \) is the number of points. +This function provides the cubic spline interpolation for all subintervals +and is called only once. +

+
+ + +

+ + +

Splines

+
+
+

+Thereafter, if you wish to make various interpolations, you need to call the function +

+ + +

splint(double x[], double y[], double y2a[], int n, double x, double *y)
+
+

+which takes as input +the tabulated values \( x[0,..,n - 1] \) and \( y[0,..,n - 1] \) and the output +y2a[0,..,n - 1] from spline. It returns the value \( y \) corresponding +to the point \( x \). +

+
+ + +

+ + +

Conjugate gradient (CG) method

+
+
+

+The success of the CG method for finding solutions of non-linear problems is based +on the theory of conjugate gradients for linear systems of equations. It belongs +to the class of iterative methods for solving problems from linear algebra of the type +$$ +\begin{equation*} + \hat{A}\hat{x} = \hat{b}. +\end{equation*} +$$ + +In the iterative process we end up with a problem like + +$$ +\begin{equation*} + \hat{r}= \hat{b}-\hat{A}\hat{x}, +\end{equation*} +$$ + +where \( \hat{r} \) is the so-called residual or error in the iterative process. + +

+When we have found the exact solution, \( \hat{r}=0 \). +

+
+ + +

+ + +

Conjugate gradient method

+
+
+

+ +

+The residual is zero when we reach the minimum of the quadratic equation +$$ +\begin{equation*} + P(\hat{x})=\frac{1}{2}\hat{x}^T\hat{A}\hat{x} - \hat{x}^T\hat{b}, +\end{equation*} +$$ + +with the constraint that the matrix \( \hat{A} \) is positive definite and symmetric. +If we search for a minimum of the quantum mechanical variance, then the matrix +\( \hat{A} \), which is called the Hessian, is given by the second-derivative of the function we want to minimize. This quantity is always positive definite. In our case this corresponds normally to the second derivative of the energy. +

+
+ + +

+ + +

Conjugate gradient method, Newton's method first

+
+
+

+We seek the minimum of the energy or the variance as function of various variational parameters. +In our case we have thus a function \( f \) whose minimum we are seeking. +In Newton's method we set \( \nabla f = 0 \) and we can thus compute the next iteration point +$$ +\begin{equation*} +\hat{x}-\hat{x}_i=\hat{A}^{-1}\nabla f(\hat{x}_i). +\end{equation*} +$$ + +Subtracting this equation from that of \( \hat{x}_{i+1} \) we have +$$ +\begin{equation*} +\hat{x}_{i+1}-\hat{x}_i=\hat{A}^{-1}(\nabla f(\hat{x}_{i+1})-\nabla f(\hat{x}_i)). +\end{equation*} +$$ +

+
+ + +

+ + +

Simple example and demonstration

+
+
+

+The function \( f \) can be either the energy or the variance. If we choose the energy then we have +$$ +\begin{equation*} +\hat{\alpha}_{i+1}-\hat{\alpha}_i=\hat{A}^{-1}(\nabla E(\hat{\alpha}_{i+1})-\nabla E(\hat{\alpha}_i)). +\end{equation*} +$$ + +In the simple harmonic oscillator model, the gradient and the Hessian \( \hat{A} \) are +$$ +\begin{equation*} +\frac{d\langle E_L[\alpha]\rangle}{d\alpha} = \alpha-\frac{1}{4\alpha^3} +\end{equation*} +$$ + +and a second derivative which is always positive (meaning that we find a minimum) +$$ +\begin{equation*} +\hat{A}= \frac{d^2\langle E_L[\alpha]\rangle}{d\alpha^2} = 1+\frac{3}{4\alpha^4} +\end{equation*} +$$ +

+
+ + +

+ + +

Simple example and demonstration

+
+
+

+We get then +$$ +\begin{equation*} +\alpha_{i+1}=\frac{4}{3}\alpha_i-\frac{\alpha_i^4}{3\alpha_{i+1}^3}, +\end{equation*} +$$ + +which can be rewritten as +$$ +\begin{equation*} +\alpha_{i+1}^4-\frac{4}{3}\alpha_i\alpha_{i+1}^4+\frac{1}{3}\alpha_i^4. +\end{equation*} +$$ +

+
+ + +

+ + +

Conjugate gradient method

+
+
+

+In the CG method we define so-called conjugate directions and two vectors +\( \hat{s} \) and \( \hat{t} \) +are said to be +conjugate if +$$ +\begin{equation*} +\hat{s}^T\hat{A}\hat{t}= 0. +\end{equation*} +$$ + +The philosophy of the CG method is to perform searches in various conjugate directions +of our vectors \( \hat{x}_i \) obeying the above criterion, namely +$$ +\begin{equation*} +\hat{x}_i^T\hat{A}\hat{x}_j= 0. +\end{equation*} +$$ + +Two vectors are conjugate if they are orthogonal with respect to +this inner product. Being conjugate is a symmetric relation: if \( \hat{s} \) is conjugate to \( \hat{t} \), then \( \hat{t} \) is conjugate to \( \hat{s} \). +

+
+ + +

+ + +

Conjugate gradient method

+
+
+

+An example is given by the eigenvectors of the matrix +$$ +\begin{equation*} +\hat{v}_i^T\hat{A}\hat{v}_j= \lambda\hat{v}_i^T\hat{v}_j, +\end{equation*} +$$ + +which is zero unless \( i=j \). +

+
+ + +

+ + +

Conjugate gradient method

+
+
+

+Assume now that we have a symmetric positive-definite matrix \( \hat{A} \) of size +\( n\times n \). At each iteration \( i+1 \) we obtain the conjugate direction of a vector +$$ +\begin{equation*} +\hat{x}_{i+1}=\hat{x}_{i}+\alpha_i\hat{p}_{i}. +\end{equation*} +$$ + +We assume that \( \hat{p}_{i} \) is a sequence of \( n \) mutually conjugate directions. +Then the \( \hat{p}_{i} \) form a basis of \( R^n \) and we can expand the solution +$ \hat{A}\hat{x} = \hat{b}$ in this basis, namely + +$$ +\begin{equation*} + \hat{x} = \sum^{n}_{i=1} \alpha_i \hat{p}_i. +\end{equation*} +$$ +

+
+ + +

+ + +

Conjugate gradient method

+
+
+

+The coefficients are given by +$$ +\begin{equation*} + \mathbf{A}\mathbf{x} = \sum^{n}_{i=1} \alpha_i \mathbf{A} \mathbf{p}_i = \mathbf{b}. +\end{equation*} +$$ + +Multiplying with \( \hat{p}_k^T \) from the left gives + +$$ +\begin{equation*} + \hat{p}_k^T \hat{A}\hat{x} = \sum^{n}_{i=1} \alpha_i\hat{p}_k^T \hat{A}\hat{p}_i= \hat{p}_k^T \hat{b}, +\end{equation*} +$$ + +and we can define the coefficients \( \alpha_k \) as + +$$ +\begin{equation*} + \alpha_k = \frac{\hat{p}_k^T \hat{b}}{\hat{p}_k^T \hat{A} \hat{p}_k} +\end{equation*} +$$ +

+
+ + +

+ + +

Conjugate gradient method and iterations

+
+
+

+ +

+If we choose the conjugate vectors \( \hat{p}_k \) carefully, +then we may not need all of them to obtain a good approximation to the solution +\( \hat{x} \). +We want to regard the conjugate gradient method as an iterative method. +This will us to solve systems where \( n \) is so large that the direct +method would take too much time. + +

+We denote the initial guess for \( \hat{x} \) as \( \hat{x}_0 \). +We can assume without loss of generality that +$$ +\begin{equation*} +\hat{x}_0=0, +\end{equation*} +$$ + +or consider the system +$$ +\begin{equation*} +\hat{A}\hat{z} = \hat{b}-\hat{A}\hat{x}_0, +\end{equation*} +$$ + +instead. +

+
+ + +

+ + +

Conjugate gradient method

+
+
+

+One can show that the solution \( \hat{x} \) is also the unique minimizer of the quadratic form +$$ +\begin{equation*} + f(\hat{x}) = \frac{1}{2}\hat{x}^T\hat{A}\hat{x} - \hat{x}^T \hat{x} , \quad \hat{x}\in\mathbf{R}^n. +\end{equation*} +$$ + +This suggests taking the first basis vector \( \hat{p}_1 \) +to be the gradient of \( f \) at \( \hat{x}=\hat{x}_0 \), +which equals +$$ +\begin{equation*} +\hat{A}\hat{x}_0-\hat{b}, +\end{equation*} +$$ + +and +\( \hat{x}_0=0 \) it is equal \( -\hat{b} \). +The other vectors in the basis will be conjugate to the gradient, +hence the name conjugate gradient method. +

+
+ + +

+ + +

Conjugate gradient method

+
+
+

+Let \( \hat{r}_k \) be the residual at the \( k \)-th step: +$$ +\begin{equation*} +\hat{r}_k=\hat{b}-\hat{A}\hat{x}_k. +\end{equation*} +$$ + +Note that \( \hat{r}_k \) is the negative gradient of \( f \) at +\( \hat{x}=\hat{x}_k \), +so the gradient descent method would be to move in the direction \( \hat{r}_k \). +Here, we insist that the directions \( \hat{p}_k \) are conjugate to each other, +so we take the direction closest to the gradient \( \hat{r}_k \) +under the conjugacy constraint. +This gives the following expression +$$ +\begin{equation*} +\hat{p}_{k+1}=\hat{r}_k-\frac{\hat{p}_k^T \hat{A}\hat{r}_k}{\hat{p}_k^T\hat{A}\hat{p}_k} \hat{p}_k. +\end{equation*} +$$ +

+
+ + +

+ + +

Conjugate gradient method

+
+
+

+We can also compute the residual iteratively as +$$ +\begin{equation*} +\hat{r}_{k+1}=\hat{b}-\hat{A}\hat{x}_{k+1}, + \end{equation*} +$$ + +which equals +$$ +\begin{equation*} +\hat{b}-\hat{A}(\hat{x}_k+\alpha_k\hat{p}_k), + \end{equation*} +$$ + +or +$$ +\begin{equation*} +(\hat{b}-\hat{A}\hat{x}_k)-\alpha_k\hat{A}\hat{p}_k, + \end{equation*} +$$ + +which gives + +$$ +\begin{equation*} +\hat{r}_{k+1}=\hat{r}_k-\hat{A}\hat{p}_{k}, + \end{equation*} +$$ +

+
+ + +

+ + + +

+ + + + + + + +
+ © 1999-2018, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license +
+ + + + + + diff --git a/doc/pub/Splines/html/Splines-reveal.html b/doc/pub/Splines/html/Splines-reveal.html new file mode 100644 index 000000000..a0ce55155 --- /dev/null +++ b/doc/pub/Splines/html/Splines-reveal.html @@ -0,0 +1,1017 @@ +\ + + + + + + +Data Analysis and Machine Learning Lectures: Cubic Splines and Gradient Methods + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + +
+ + + + + + + + + + + + + + +
+ + + + +

Data Analysis and Machine Learning Lectures: Cubic Splines and Gradient Methods

+ +

+ + +

+Morten Hjorth-Jensen [1, 2] +
+ +

 
+ + +

[1] Department of Physics, University of Oslo
+
[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University
+
+

 
+

Jan 27, 2018

+
+

+ +

+ © 1999-2018, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license +
+
+ + +
+

Cubic Splines

+
+ +

+Cubic spline interpolation is among one of the most used +methods for interpolating between data points where the arguments +are organized as ascending series. In the library program we supply +such a function, based on the so-called cubic spline method to be +described below. + +

+A spline function consists of polynomial pieces defined on +subintervals. The different subintervals are connected via +various continuity relations. + +

+Assume we have at our disposal \( n+1 \) points \( x_0, x_1, \dots x_n \) +arranged so that \( x_0 < x_1 < x_2 < \dots x_{n-1} < x_n \) (such points are called +knots). A spline function \( s \) of degree \( k \) with \( n+1 \) knots is defined +as follows + +

    +

  • On every subinterval \( [x_{i-1},x_i) \) s is a polynomial of degree \( \le k \).
  • +

  • \( s \) has \( k-1 \) continuous derivatives in the whole interval \( [x_0,x_n] \).
  • +
+
+
+ + +
+

Splines

+
+ +

+As an example, consider a spline function of degree \( k=1 \) defined as follows +

 
+$$ + s(x)=\begin{bmatrix} s_0(x)=a_0x+b_0 & x\in [x_0, x_1) \\ + s_1(x)=a_1x+b_1 & x\in [x_1, x_2) \\ + \dots & \dots \\ + s_{n-1}(x)=a_{n-1}x+b_{n-1} & x\in + [x_{n-1}, x_n] \end{bmatrix}. +$$ +

 
+ +In this case the polynomial consists of series of straight lines +connected to each other at every endpoint. The number of continuous +derivatives is then \( k-1=0 \), as expected when we deal with straight lines. +Such a polynomial is quite easy to construct given +\( n+1 \) points \( x_0, x_1, \dots x_n \) and their corresponding +function values. +

+
+ + +
+

Splines

+
+ +

+The most commonly used spline function is the one with \( k=3 \), the so-called +cubic spline function. +Assume that we have in adddition to the \( n+1 \) knots a series of +functions values \( y_0=f(x_0), y_1=f(x_1), \dots y_n=f(x_n) \). +By definition, the polynomials \( s_{i-1} \) and \( s_i \) +are thence supposed to interpolate the same point \( i \), that is +

 
+$$ + s_{i-1}(x_i)= y_i = s_i(x_i), +$$ +

 
+ +with \( 1 \le i \le n-1 \). In total we have \( n \) polynomials of the +type +

 
+$$ + s_i(x)=a_{i0}+a_{i1}x+a_{i2}x^2+a_{i2}x^3, +$$ +

 
+ +yielding \( 4n \) coefficients to determine. +

+
+ + +
+

Splines

+
+ +

+Every subinterval provides in addition the \( 2n \) conditions +

 
+$$ + y_i = s(x_i), +$$ +

 
+ +and +

 
+$$ + s(x_{i+1})= y_{i+1}, +$$ +

 
+ +to be fulfilled. If we also assume that \( s' \) and \( s'' \) are continuous, +then +

 
+$$ + s'_{i-1}(x_i)= s'_i(x_i), +$$ +

 
+ +yields \( n-1 \) conditions. Similarly, +

 
+$$ + s''_{i-1}(x_i)= s''_i(x_i), +$$ +

 
+ +results in additional \( n-1 \) conditions. In total we have \( 4n \) coefficients +and \( 4n-2 \) equations to determine them, leaving us with \( 2 \) degrees of +freedom to be determined. +

+
+ + +
+

Splines

+
+ +

+Using the last equation we define two values for the second derivative, namely +

 
+$$ + s''_{i}(x_i)= f_i, +$$ +

 
+ +and +

 
+$$ + s''_{i}(x_{i+1})= f_{i+1}, +$$ +

 
+ +and setting up a straight line between \( f_i \) and \( f_{i+1} \) we have +

 
+$$ + s_i''(x) = \frac{f_i}{x_{i+1}-x_i}(x_{i+1}-x)+ + \frac{f_{i+1}}{x_{i+1}-x_i}(x-x_i), +$$ +

 
+ +and integrating twice one obtains +

 
+$$ + s_i(x) = \frac{f_i}{6(x_{i+1}-x_i)}(x_{i+1}-x)^3+ + \frac{f_{i+1}}{6(x_{i+1}-x_i)}(x-x_i)^3 + +c(x-x_i)+d(x_{i+1}-x). +$$ +

 
+

+
+ + +
+

Splines

+
+ +

+Using the conditions \( s_i(x_i)=y_i \) and \( s_i(x_{i+1})=y_{i+1} \) +we can in turn determine the constants \( c \) and \( d \) resulting in +

 
+$$ +\begin{align} + s_i(x) =&\frac{f_i}{6(x_{i+1}-x_i)}(x_{i+1}-x)^3+ + \frac{f_{i+1}}{6(x_{i+1}-x_i)}(x-x_i)^3 \nonumber \\ + +&(\frac{y_{i+1}}{x_{i+1}-x_i}-\frac{f_{i+1}(x_{i+1}-x_i)}{6}) + (x-x_i)+ + (\frac{y_{i}}{x_{i+1}-x_i}-\frac{f_{i}(x_{i+1}-x_i)}{6}) + (x_{i+1}-x). +\tag{1} +\end{align} +$$ +

 
+

+
+ + +
+

Splines

+
+ +

+How to determine the values of the second +derivatives \( f_{i} \) and \( f_{i+1} \)? We use the continuity assumption +of the first derivatives +

 
+$$ + s'_{i-1}(x_i)= s'_i(x_i), +$$ +

 
+ +and set \( x=x_i \). Defining \( h_i=x_{i+1}-x_i \) we obtain finally +the following expression +

 
+$$ + h_{i-1}f_{i-1}+2(h_{i}+h_{i-1})f_i+h_if_{i+1}= + \frac{6}{h_i}(y_{i+1}-y_i)-\frac{6}{h_{i-1}}(y_{i}-y_{i-1}), +$$ +

 
+ +and introducing the shorthands \( u_i=2(h_{i}+h_{i-1}) \), +\( v_i=\frac{6}{h_i}(y_{i+1}-y_i)-\frac{6}{h_{i-1}}(y_{i}-y_{i-1}) \), +we can reformulate the problem as a set of linear equations to be +solved through e.g., Gaussian elemination +

+
+ + +
+

Splines

+
+ +

+Gaussian elimination +

 
+$$ + \begin{bmatrix} u_1 & h_1 &0 &\dots & & & & \\ + h_1 & u_2 & h_2 &0 &\dots & & & \\ + 0 & h_2 & u_3 & h_3 &0 &\dots & & \\ + \dots& & \dots &\dots &\dots &\dots &\dots & \\ + &\dots & & &0 &h_{n-3} &u_{n-2} &h_{n-2} \\ + & && & &0 &h_{n-2} &u_{n-1} \end{bmatrix} + \begin{bmatrix} f_1 \\ + f_2 \\ + f_3\\ + \dots \\ + f_{n-2} \\ + f_{n-1} \end{bmatrix} = + \begin{bmatrix} v_1 \\ + v_2 \\ + v_3\\ + \dots \\ + v_{n-2}\\ + v_{n-1} \end{bmatrix}. +$$ +

 
+ +Note that this is a set of tridiagonal equations and can be solved +through only \( O(n) \) operations. +

+
+ + +
+

Splines

+
+ +

+The functions supplied in the program library are spline and splint. +In order to use cubic spline interpolation you need first to call + +

+ + +

spline(double x[], double y[], int n, double yp1,  double yp2, double y2[])
+
+

+This function takes as +input \( x[0,..,n - 1] \) and \( y[0,..,n - 1] \) containing a tabulation +\( y_i = f(x_i) \) with \( x_0 < x_1 < .. < x_{n - 1} \) +together with the +first derivatives of \( f(x) \) at \( x_0 \) and \( x_{n-1} \), respectively. Then the +function returns \( y2[0,..,n-1] \) which contains the second derivatives of +\( f(x_i) \) at each point \( x_i \). \( n \) is the number of points. +This function provides the cubic spline interpolation for all subintervals +and is called only once. +

+
+ + +
+

Splines

+
+ +

+Thereafter, if you wish to make various interpolations, you need to call the function +

+ + +

splint(double x[], double y[], double y2a[], int n, double x, double *y)
+
+

+which takes as input +the tabulated values \( x[0,..,n - 1] \) and \( y[0,..,n - 1] \) and the output +y2a[0,..,n - 1] from spline. It returns the value \( y \) corresponding +to the point \( x \). +

+
+ + +
+

Conjugate gradient (CG) method

+
+ +

+The success of the CG method for finding solutions of non-linear problems is based +on the theory of conjugate gradients for linear systems of equations. It belongs +to the class of iterative methods for solving problems from linear algebra of the type +

 
+$$ +\begin{equation*} + \hat{A}\hat{x} = \hat{b}. +\end{equation*} +$$ +

 
+ +In the iterative process we end up with a problem like + +

 
+$$ +\begin{equation*} + \hat{r}= \hat{b}-\hat{A}\hat{x}, +\end{equation*} +$$ +

 
+ +where \( \hat{r} \) is the so-called residual or error in the iterative process. + +

+When we have found the exact solution, \( \hat{r}=0 \). +

+
+ + +
+

Conjugate gradient method

+
+ +

+The residual is zero when we reach the minimum of the quadratic equation +

 
+$$ +\begin{equation*} + P(\hat{x})=\frac{1}{2}\hat{x}^T\hat{A}\hat{x} - \hat{x}^T\hat{b}, +\end{equation*} +$$ +

 
+ +with the constraint that the matrix \( \hat{A} \) is positive definite and symmetric. +If we search for a minimum of the quantum mechanical variance, then the matrix +\( \hat{A} \), which is called the Hessian, is given by the second-derivative of the function we want to minimize. This quantity is always positive definite. In our case this corresponds normally to the second derivative of the energy. +

+
+ + +
+

Conjugate gradient method, Newton's method first

+
+ +

+We seek the minimum of the energy or the variance as function of various variational parameters. +In our case we have thus a function \( f \) whose minimum we are seeking. +In Newton's method we set \( \nabla f = 0 \) and we can thus compute the next iteration point +

 
+$$ +\begin{equation*} +\hat{x}-\hat{x}_i=\hat{A}^{-1}\nabla f(\hat{x}_i). +\end{equation*} +$$ +

 
+ +Subtracting this equation from that of \( \hat{x}_{i+1} \) we have +

 
+$$ +\begin{equation*} +\hat{x}_{i+1}-\hat{x}_i=\hat{A}^{-1}(\nabla f(\hat{x}_{i+1})-\nabla f(\hat{x}_i)). +\end{equation*} +$$ +

 
+

+
+ + +
+

Simple example and demonstration

+
+ +

+The function \( f \) can be either the energy or the variance. If we choose the energy then we have +

 
+$$ +\begin{equation*} +\hat{\alpha}_{i+1}-\hat{\alpha}_i=\hat{A}^{-1}(\nabla E(\hat{\alpha}_{i+1})-\nabla E(\hat{\alpha}_i)). +\end{equation*} +$$ +

 
+ +In the simple harmonic oscillator model, the gradient and the Hessian \( \hat{A} \) are +

 
+$$ +\begin{equation*} +\frac{d\langle E_L[\alpha]\rangle}{d\alpha} = \alpha-\frac{1}{4\alpha^3} +\end{equation*} +$$ +

 
+ +and a second derivative which is always positive (meaning that we find a minimum) +

 
+$$ +\begin{equation*} +\hat{A}= \frac{d^2\langle E_L[\alpha]\rangle}{d\alpha^2} = 1+\frac{3}{4\alpha^4} +\end{equation*} +$$ +

 
+

+
+ + +
+

Simple example and demonstration

+
+ +

+We get then +

 
+$$ +\begin{equation*} +\alpha_{i+1}=\frac{4}{3}\alpha_i-\frac{\alpha_i^4}{3\alpha_{i+1}^3}, +\end{equation*} +$$ +

 
+ +which can be rewritten as +

 
+$$ +\begin{equation*} +\alpha_{i+1}^4-\frac{4}{3}\alpha_i\alpha_{i+1}^4+\frac{1}{3}\alpha_i^4. +\end{equation*} +$$ +

 
+

+
+ + +
+

Conjugate gradient method

+
+ +

+In the CG method we define so-called conjugate directions and two vectors +\( \hat{s} \) and \( \hat{t} \) +are said to be +conjugate if +

 
+$$ +\begin{equation*} +\hat{s}^T\hat{A}\hat{t}= 0. +\end{equation*} +$$ +

 
+ +The philosophy of the CG method is to perform searches in various conjugate directions +of our vectors \( \hat{x}_i \) obeying the above criterion, namely +

 
+$$ +\begin{equation*} +\hat{x}_i^T\hat{A}\hat{x}_j= 0. +\end{equation*} +$$ +

 
+ +Two vectors are conjugate if they are orthogonal with respect to +this inner product. Being conjugate is a symmetric relation: if \( \hat{s} \) is conjugate to \( \hat{t} \), then \( \hat{t} \) is conjugate to \( \hat{s} \). +

+
+ + +
+

Conjugate gradient method

+
+ +

+An example is given by the eigenvectors of the matrix +

 
+$$ +\begin{equation*} +\hat{v}_i^T\hat{A}\hat{v}_j= \lambda\hat{v}_i^T\hat{v}_j, +\end{equation*} +$$ +

 
+ +which is zero unless \( i=j \). +

+
+ + +
+

Conjugate gradient method

+
+ +

+Assume now that we have a symmetric positive-definite matrix \( \hat{A} \) of size +\( n\times n \). At each iteration \( i+1 \) we obtain the conjugate direction of a vector +

 
+$$ +\begin{equation*} +\hat{x}_{i+1}=\hat{x}_{i}+\alpha_i\hat{p}_{i}. +\end{equation*} +$$ +

 
+ +We assume that \( \hat{p}_{i} \) is a sequence of \( n \) mutually conjugate directions. +Then the \( \hat{p}_{i} \) form a basis of \( R^n \) and we can expand the solution +$ \hat{A}\hat{x} = \hat{b}$ in this basis, namely + +

 
+$$ +\begin{equation*} + \hat{x} = \sum^{n}_{i=1} \alpha_i \hat{p}_i. +\end{equation*} +$$ +

 
+

+
+ + +
+

Conjugate gradient method

+
+ +

+The coefficients are given by +

 
+$$ +\begin{equation*} + \mathbf{A}\mathbf{x} = \sum^{n}_{i=1} \alpha_i \mathbf{A} \mathbf{p}_i = \mathbf{b}. +\end{equation*} +$$ +

 
+ +Multiplying with \( \hat{p}_k^T \) from the left gives + +

 
+$$ +\begin{equation*} + \hat{p}_k^T \hat{A}\hat{x} = \sum^{n}_{i=1} \alpha_i\hat{p}_k^T \hat{A}\hat{p}_i= \hat{p}_k^T \hat{b}, +\end{equation*} +$$ +

 
+ +and we can define the coefficients \( \alpha_k \) as + +

 
+$$ +\begin{equation*} + \alpha_k = \frac{\hat{p}_k^T \hat{b}}{\hat{p}_k^T \hat{A} \hat{p}_k} +\end{equation*} +$$ +

 
+

+
+ + +
+

Conjugate gradient method and iterations

+
+ +

+If we choose the conjugate vectors \( \hat{p}_k \) carefully, +then we may not need all of them to obtain a good approximation to the solution +\( \hat{x} \). +We want to regard the conjugate gradient method as an iterative method. +This will us to solve systems where \( n \) is so large that the direct +method would take too much time. + +

+We denote the initial guess for \( \hat{x} \) as \( \hat{x}_0 \). +We can assume without loss of generality that +

 
+$$ +\begin{equation*} +\hat{x}_0=0, +\end{equation*} +$$ +

 
+ +or consider the system +

 
+$$ +\begin{equation*} +\hat{A}\hat{z} = \hat{b}-\hat{A}\hat{x}_0, +\end{equation*} +$$ +

 
+ +instead. +

+
+ + +
+

Conjugate gradient method

+
+ +

+One can show that the solution \( \hat{x} \) is also the unique minimizer of the quadratic form +

 
+$$ +\begin{equation*} + f(\hat{x}) = \frac{1}{2}\hat{x}^T\hat{A}\hat{x} - \hat{x}^T \hat{x} , \quad \hat{x}\in\mathbf{R}^n. +\end{equation*} +$$ +

 
+ +This suggests taking the first basis vector \( \hat{p}_1 \) +to be the gradient of \( f \) at \( \hat{x}=\hat{x}_0 \), +which equals +

 
+$$ +\begin{equation*} +\hat{A}\hat{x}_0-\hat{b}, +\end{equation*} +$$ +

 
+ +and +\( \hat{x}_0=0 \) it is equal \( -\hat{b} \). +The other vectors in the basis will be conjugate to the gradient, +hence the name conjugate gradient method. +

+
+ + +
+

Conjugate gradient method

+
+ +

+Let \( \hat{r}_k \) be the residual at the \( k \)-th step: +

 
+$$ +\begin{equation*} +\hat{r}_k=\hat{b}-\hat{A}\hat{x}_k. +\end{equation*} +$$ +

 
+ +Note that \( \hat{r}_k \) is the negative gradient of \( f \) at +\( \hat{x}=\hat{x}_k \), +so the gradient descent method would be to move in the direction \( \hat{r}_k \). +Here, we insist that the directions \( \hat{p}_k \) are conjugate to each other, +so we take the direction closest to the gradient \( \hat{r}_k \) +under the conjugacy constraint. +This gives the following expression +

 
+$$ +\begin{equation*} +\hat{p}_{k+1}=\hat{r}_k-\frac{\hat{p}_k^T \hat{A}\hat{r}_k}{\hat{p}_k^T\hat{A}\hat{p}_k} \hat{p}_k. +\end{equation*} +$$ +

 
+

+
+ + +
+

Conjugate gradient method

+
+ +

+We can also compute the residual iteratively as +

 
+$$ +\begin{equation*} +\hat{r}_{k+1}=\hat{b}-\hat{A}\hat{x}_{k+1}, + \end{equation*} +$$ +

 
+ +which equals +

 
+$$ +\begin{equation*} +\hat{b}-\hat{A}(\hat{x}_k+\alpha_k\hat{p}_k), + \end{equation*} +$$ +

 
+ +or +

 
+$$ +\begin{equation*} +(\hat{b}-\hat{A}\hat{x}_k)-\alpha_k\hat{A}\hat{p}_k, + \end{equation*} +$$ +

 
+ +which gives + +

 
+$$ +\begin{equation*} +\hat{r}_{k+1}=\hat{r}_k-\hat{A}\hat{p}_{k}, + \end{equation*} +$$ +

 
+

+
+ + + +
+
+ + + + + + + + + + + + diff --git a/doc/pub/Splines/html/Splines-solarized.html b/doc/pub/Splines/html/Splines-solarized.html new file mode 100644 index 000000000..bbfd2bfd3 --- /dev/null +++ b/doc/pub/Splines/html/Splines-solarized.html @@ -0,0 +1,799 @@ + + + + + + + +Data Analysis and Machine Learning Lectures: Cubic Splines and Gradient Methods + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Data Analysis and Machine Learning Lectures: Cubic Splines and Gradient Methods

+ +

+ + +

+Morten Hjorth-Jensen [1, 2] +
+ +

+ + +

[1] Department of Physics, University of Oslo
+
[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University
+
+

+

Jan 27, 2018

+
+

+









+ +

Cubic Splines

+
+ +

+Cubic spline interpolation is among one of the most used +methods for interpolating between data points where the arguments +are organized as ascending series. In the library program we supply +such a function, based on the so-called cubic spline method to be +described below. + +

+A spline function consists of polynomial pieces defined on +subintervals. The different subintervals are connected via +various continuity relations. + +

+Assume we have at our disposal \( n+1 \) points \( x_0, x_1, \dots x_n \) +arranged so that \( x_0 < x_1 < x_2 < \dots x_{n-1} < x_n \) (such points are called +knots). A spline function \( s \) of degree \( k \) with \( n+1 \) knots is defined +as follows + +

    +
  • On every subinterval \( [x_{i-1},x_i) \) s is a polynomial of degree \( \le k \).
  • +
  • \( s \) has \( k-1 \) continuous derivatives in the whole interval \( [x_0,x_n] \).
  • +
+
+ + +

+









+ +

Splines

+
+ +

+As an example, consider a spline function of degree \( k=1 \) defined as follows +$$ + s(x)=\begin{bmatrix} s_0(x)=a_0x+b_0 & x\in [x_0, x_1) \\ + s_1(x)=a_1x+b_1 & x\in [x_1, x_2) \\ + \dots & \dots \\ + s_{n-1}(x)=a_{n-1}x+b_{n-1} & x\in + [x_{n-1}, x_n] \end{bmatrix}. +$$ + +In this case the polynomial consists of series of straight lines +connected to each other at every endpoint. The number of continuous +derivatives is then \( k-1=0 \), as expected when we deal with straight lines. +Such a polynomial is quite easy to construct given +\( n+1 \) points \( x_0, x_1, \dots x_n \) and their corresponding +function values. +

+ + +

+









+ +

Splines

+
+ +

+The most commonly used spline function is the one with \( k=3 \), the so-called +cubic spline function. +Assume that we have in adddition to the \( n+1 \) knots a series of +functions values \( y_0=f(x_0), y_1=f(x_1), \dots y_n=f(x_n) \). +By definition, the polynomials \( s_{i-1} \) and \( s_i \) +are thence supposed to interpolate the same point \( i \), that is +$$ + s_{i-1}(x_i)= y_i = s_i(x_i), +$$ + +with \( 1 \le i \le n-1 \). In total we have \( n \) polynomials of the +type +$$ + s_i(x)=a_{i0}+a_{i1}x+a_{i2}x^2+a_{i2}x^3, +$$ + +yielding \( 4n \) coefficients to determine. +

+ + +

+









+ +

Splines

+
+ +

+Every subinterval provides in addition the \( 2n \) conditions +$$ + y_i = s(x_i), +$$ + +and +$$ + s(x_{i+1})= y_{i+1}, +$$ + +to be fulfilled. If we also assume that \( s' \) and \( s'' \) are continuous, +then +$$ + s'_{i-1}(x_i)= s'_i(x_i), +$$ + +yields \( n-1 \) conditions. Similarly, +$$ + s''_{i-1}(x_i)= s''_i(x_i), +$$ + +results in additional \( n-1 \) conditions. In total we have \( 4n \) coefficients +and \( 4n-2 \) equations to determine them, leaving us with \( 2 \) degrees of +freedom to be determined. +

+ + +

+









+ +

Splines

+
+ +

+Using the last equation we define two values for the second derivative, namely +$$ + s''_{i}(x_i)= f_i, +$$ + +and +$$ + s''_{i}(x_{i+1})= f_{i+1}, +$$ + +and setting up a straight line between \( f_i \) and \( f_{i+1} \) we have +$$ + s_i''(x) = \frac{f_i}{x_{i+1}-x_i}(x_{i+1}-x)+ + \frac{f_{i+1}}{x_{i+1}-x_i}(x-x_i), +$$ + +and integrating twice one obtains +$$ + s_i(x) = \frac{f_i}{6(x_{i+1}-x_i)}(x_{i+1}-x)^3+ + \frac{f_{i+1}}{6(x_{i+1}-x_i)}(x-x_i)^3 + +c(x-x_i)+d(x_{i+1}-x). +$$ +

+ + +

+









+ +

Splines

+
+ +

+Using the conditions \( s_i(x_i)=y_i \) and \( s_i(x_{i+1})=y_{i+1} \) +we can in turn determine the constants \( c \) and \( d \) resulting in +$$ +\begin{align} + s_i(x) =&\frac{f_i}{6(x_{i+1}-x_i)}(x_{i+1}-x)^3+ + \frac{f_{i+1}}{6(x_{i+1}-x_i)}(x-x_i)^3 \nonumber \\ + +&(\frac{y_{i+1}}{x_{i+1}-x_i}-\frac{f_{i+1}(x_{i+1}-x_i)}{6}) + (x-x_i)+ + (\frac{y_{i}}{x_{i+1}-x_i}-\frac{f_{i}(x_{i+1}-x_i)}{6}) + (x_{i+1}-x). +\label{_auto1} +\end{align} +$$ +

+ + +

+









+ +

Splines

+
+ +

+How to determine the values of the second +derivatives \( f_{i} \) and \( f_{i+1} \)? We use the continuity assumption +of the first derivatives +$$ + s'_{i-1}(x_i)= s'_i(x_i), +$$ + +and set \( x=x_i \). Defining \( h_i=x_{i+1}-x_i \) we obtain finally +the following expression +$$ + h_{i-1}f_{i-1}+2(h_{i}+h_{i-1})f_i+h_if_{i+1}= + \frac{6}{h_i}(y_{i+1}-y_i)-\frac{6}{h_{i-1}}(y_{i}-y_{i-1}), +$$ + +and introducing the shorthands \( u_i=2(h_{i}+h_{i-1}) \), +\( v_i=\frac{6}{h_i}(y_{i+1}-y_i)-\frac{6}{h_{i-1}}(y_{i}-y_{i-1}) \), +we can reformulate the problem as a set of linear equations to be +solved through e.g., Gaussian elemination +

+ + +

+









+ +

Splines

+
+ +

+Gaussian elimination +$$ + \begin{bmatrix} u_1 & h_1 &0 &\dots & & & & \\ + h_1 & u_2 & h_2 &0 &\dots & & & \\ + 0 & h_2 & u_3 & h_3 &0 &\dots & & \\ + \dots& & \dots &\dots &\dots &\dots &\dots & \\ + &\dots & & &0 &h_{n-3} &u_{n-2} &h_{n-2} \\ + & && & &0 &h_{n-2} &u_{n-1} \end{bmatrix} + \begin{bmatrix} f_1 \\ + f_2 \\ + f_3\\ + \dots \\ + f_{n-2} \\ + f_{n-1} \end{bmatrix} = + \begin{bmatrix} v_1 \\ + v_2 \\ + v_3\\ + \dots \\ + v_{n-2}\\ + v_{n-1} \end{bmatrix}. +$$ + +Note that this is a set of tridiagonal equations and can be solved +through only \( O(n) \) operations. +

+ + +

+









+ +

Splines

+
+ +

+The functions supplied in the program library are spline and splint. +In order to use cubic spline interpolation you need first to call + +

+ + +

spline(double x[], double y[], int n, double yp1,  double yp2, double y2[])
+
+

+This function takes as +input \( x[0,..,n - 1] \) and \( y[0,..,n - 1] \) containing a tabulation +\( y_i = f(x_i) \) with \( x_0 < x_1 < .. < x_{n - 1} \) +together with the +first derivatives of \( f(x) \) at \( x_0 \) and \( x_{n-1} \), respectively. Then the +function returns \( y2[0,..,n-1] \) which contains the second derivatives of +\( f(x_i) \) at each point \( x_i \). \( n \) is the number of points. +This function provides the cubic spline interpolation for all subintervals +and is called only once. +

+ + +

+









+ +

Splines

+
+ +

+Thereafter, if you wish to make various interpolations, you need to call the function +

+ + +

splint(double x[], double y[], double y2a[], int n, double x, double *y)
+
+

+which takes as input +the tabulated values \( x[0,..,n - 1] \) and \( y[0,..,n - 1] \) and the output +y2a[0,..,n - 1] from spline. It returns the value \( y \) corresponding +to the point \( x \). +

+ + +

+









+ +

Conjugate gradient (CG) method

+
+ +

+The success of the CG method for finding solutions of non-linear problems is based +on the theory of conjugate gradients for linear systems of equations. It belongs +to the class of iterative methods for solving problems from linear algebra of the type +$$ +\begin{equation*} + \hat{A}\hat{x} = \hat{b}. +\end{equation*} +$$ + +In the iterative process we end up with a problem like + +$$ +\begin{equation*} + \hat{r}= \hat{b}-\hat{A}\hat{x}, +\end{equation*} +$$ + +where \( \hat{r} \) is the so-called residual or error in the iterative process. + +

+When we have found the exact solution, \( \hat{r}=0 \). +

+ + +

+









+ +

Conjugate gradient method

+
+ +

+ +

+The residual is zero when we reach the minimum of the quadratic equation +$$ +\begin{equation*} + P(\hat{x})=\frac{1}{2}\hat{x}^T\hat{A}\hat{x} - \hat{x}^T\hat{b}, +\end{equation*} +$$ + +with the constraint that the matrix \( \hat{A} \) is positive definite and symmetric. +If we search for a minimum of the quantum mechanical variance, then the matrix +\( \hat{A} \), which is called the Hessian, is given by the second-derivative of the function we want to minimize. This quantity is always positive definite. In our case this corresponds normally to the second derivative of the energy. +

+ + +

+









+ +

Conjugate gradient method, Newton's method first

+
+ +

+We seek the minimum of the energy or the variance as function of various variational parameters. +In our case we have thus a function \( f \) whose minimum we are seeking. +In Newton's method we set \( \nabla f = 0 \) and we can thus compute the next iteration point +$$ +\begin{equation*} +\hat{x}-\hat{x}_i=\hat{A}^{-1}\nabla f(\hat{x}_i). +\end{equation*} +$$ + +Subtracting this equation from that of \( \hat{x}_{i+1} \) we have +$$ +\begin{equation*} +\hat{x}_{i+1}-\hat{x}_i=\hat{A}^{-1}(\nabla f(\hat{x}_{i+1})-\nabla f(\hat{x}_i)). +\end{equation*} +$$ +

+ + +

+









+ +

Simple example and demonstration

+
+ +

+The function \( f \) can be either the energy or the variance. If we choose the energy then we have +$$ +\begin{equation*} +\hat{\alpha}_{i+1}-\hat{\alpha}_i=\hat{A}^{-1}(\nabla E(\hat{\alpha}_{i+1})-\nabla E(\hat{\alpha}_i)). +\end{equation*} +$$ + +In the simple harmonic oscillator model, the gradient and the Hessian \( \hat{A} \) are +$$ +\begin{equation*} +\frac{d\langle E_L[\alpha]\rangle}{d\alpha} = \alpha-\frac{1}{4\alpha^3} +\end{equation*} +$$ + +and a second derivative which is always positive (meaning that we find a minimum) +$$ +\begin{equation*} +\hat{A}= \frac{d^2\langle E_L[\alpha]\rangle}{d\alpha^2} = 1+\frac{3}{4\alpha^4} +\end{equation*} +$$ +

+ + +

+









+ +

Simple example and demonstration

+
+ +

+We get then +$$ +\begin{equation*} +\alpha_{i+1}=\frac{4}{3}\alpha_i-\frac{\alpha_i^4}{3\alpha_{i+1}^3}, +\end{equation*} +$$ + +which can be rewritten as +$$ +\begin{equation*} +\alpha_{i+1}^4-\frac{4}{3}\alpha_i\alpha_{i+1}^4+\frac{1}{3}\alpha_i^4. +\end{equation*} +$$ +

+ + +

+









+ +

Conjugate gradient method

+
+ +

+In the CG method we define so-called conjugate directions and two vectors +\( \hat{s} \) and \( \hat{t} \) +are said to be +conjugate if +$$ +\begin{equation*} +\hat{s}^T\hat{A}\hat{t}= 0. +\end{equation*} +$$ + +The philosophy of the CG method is to perform searches in various conjugate directions +of our vectors \( \hat{x}_i \) obeying the above criterion, namely +$$ +\begin{equation*} +\hat{x}_i^T\hat{A}\hat{x}_j= 0. +\end{equation*} +$$ + +Two vectors are conjugate if they are orthogonal with respect to +this inner product. Being conjugate is a symmetric relation: if \( \hat{s} \) is conjugate to \( \hat{t} \), then \( \hat{t} \) is conjugate to \( \hat{s} \). +

+ + +

+









+ +

Conjugate gradient method

+
+ +

+An example is given by the eigenvectors of the matrix +$$ +\begin{equation*} +\hat{v}_i^T\hat{A}\hat{v}_j= \lambda\hat{v}_i^T\hat{v}_j, +\end{equation*} +$$ + +which is zero unless \( i=j \). +

+ + +

+









+ +

Conjugate gradient method

+
+ +

+Assume now that we have a symmetric positive-definite matrix \( \hat{A} \) of size +\( n\times n \). At each iteration \( i+1 \) we obtain the conjugate direction of a vector +$$ +\begin{equation*} +\hat{x}_{i+1}=\hat{x}_{i}+\alpha_i\hat{p}_{i}. +\end{equation*} +$$ + +We assume that \( \hat{p}_{i} \) is a sequence of \( n \) mutually conjugate directions. +Then the \( \hat{p}_{i} \) form a basis of \( R^n \) and we can expand the solution +$ \hat{A}\hat{x} = \hat{b}$ in this basis, namely + +$$ +\begin{equation*} + \hat{x} = \sum^{n}_{i=1} \alpha_i \hat{p}_i. +\end{equation*} +$$ +

+ + +

+









+ +

Conjugate gradient method

+
+ +

+The coefficients are given by +$$ +\begin{equation*} + \mathbf{A}\mathbf{x} = \sum^{n}_{i=1} \alpha_i \mathbf{A} \mathbf{p}_i = \mathbf{b}. +\end{equation*} +$$ + +Multiplying with \( \hat{p}_k^T \) from the left gives + +$$ +\begin{equation*} + \hat{p}_k^T \hat{A}\hat{x} = \sum^{n}_{i=1} \alpha_i\hat{p}_k^T \hat{A}\hat{p}_i= \hat{p}_k^T \hat{b}, +\end{equation*} +$$ + +and we can define the coefficients \( \alpha_k \) as + +$$ +\begin{equation*} + \alpha_k = \frac{\hat{p}_k^T \hat{b}}{\hat{p}_k^T \hat{A} \hat{p}_k} +\end{equation*} +$$ +

+ + +

+









+ +

Conjugate gradient method and iterations

+
+ +

+ +

+If we choose the conjugate vectors \( \hat{p}_k \) carefully, +then we may not need all of them to obtain a good approximation to the solution +\( \hat{x} \). +We want to regard the conjugate gradient method as an iterative method. +This will us to solve systems where \( n \) is so large that the direct +method would take too much time. + +

+We denote the initial guess for \( \hat{x} \) as \( \hat{x}_0 \). +We can assume without loss of generality that +$$ +\begin{equation*} +\hat{x}_0=0, +\end{equation*} +$$ + +or consider the system +$$ +\begin{equation*} +\hat{A}\hat{z} = \hat{b}-\hat{A}\hat{x}_0, +\end{equation*} +$$ + +instead. +

+ + +

+









+ +

Conjugate gradient method

+
+ +

+One can show that the solution \( \hat{x} \) is also the unique minimizer of the quadratic form +$$ +\begin{equation*} + f(\hat{x}) = \frac{1}{2}\hat{x}^T\hat{A}\hat{x} - \hat{x}^T \hat{x} , \quad \hat{x}\in\mathbf{R}^n. +\end{equation*} +$$ + +This suggests taking the first basis vector \( \hat{p}_1 \) +to be the gradient of \( f \) at \( \hat{x}=\hat{x}_0 \), +which equals +$$ +\begin{equation*} +\hat{A}\hat{x}_0-\hat{b}, +\end{equation*} +$$ + +and +\( \hat{x}_0=0 \) it is equal \( -\hat{b} \). +The other vectors in the basis will be conjugate to the gradient, +hence the name conjugate gradient method. +

+ + +

+









+ +

Conjugate gradient method

+
+ +

+Let \( \hat{r}_k \) be the residual at the \( k \)-th step: +$$ +\begin{equation*} +\hat{r}_k=\hat{b}-\hat{A}\hat{x}_k. +\end{equation*} +$$ + +Note that \( \hat{r}_k \) is the negative gradient of \( f \) at +\( \hat{x}=\hat{x}_k \), +so the gradient descent method would be to move in the direction \( \hat{r}_k \). +Here, we insist that the directions \( \hat{p}_k \) are conjugate to each other, +so we take the direction closest to the gradient \( \hat{r}_k \) +under the conjugacy constraint. +This gives the following expression +$$ +\begin{equation*} +\hat{p}_{k+1}=\hat{r}_k-\frac{\hat{p}_k^T \hat{A}\hat{r}_k}{\hat{p}_k^T\hat{A}\hat{p}_k} \hat{p}_k. +\end{equation*} +$$ +

+ + +

+









+ +

Conjugate gradient method

+
+ +

+We can also compute the residual iteratively as +$$ +\begin{equation*} +\hat{r}_{k+1}=\hat{b}-\hat{A}\hat{x}_{k+1}, + \end{equation*} +$$ + +which equals +$$ +\begin{equation*} +\hat{b}-\hat{A}(\hat{x}_k+\alpha_k\hat{p}_k), + \end{equation*} +$$ + +or +$$ +\begin{equation*} +(\hat{b}-\hat{A}\hat{x}_k)-\alpha_k\hat{A}\hat{p}_k, + \end{equation*} +$$ + +which gives + +$$ +\begin{equation*} +\hat{r}_{k+1}=\hat{r}_k-\hat{A}\hat{p}_{k}, + \end{equation*} +$$ +

+ + +

+ + + + +

+ © 1999-2018, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license +
+ + + + + + diff --git a/doc/pub/Splines/html/Splines.html b/doc/pub/Splines/html/Splines.html new file mode 100644 index 000000000..f7b3d6225 --- /dev/null +++ b/doc/pub/Splines/html/Splines.html @@ -0,0 +1,804 @@ + + + + + + + +Data Analysis and Machine Learning Lectures: Cubic Splines and Gradient Methods + + + + + + + + + + + + + + + + + + + + + + + +

Data Analysis and Machine Learning Lectures: Cubic Splines and Gradient Methods

+ +

+ + +

+Morten Hjorth-Jensen [1, 2] +
+ +

+ + +

[1] Department of Physics, University of Oslo
+
[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University
+
+

+

Jan 27, 2018

+
+

+









+ +

Cubic Splines

+
+ +

+Cubic spline interpolation is among one of the most used +methods for interpolating between data points where the arguments +are organized as ascending series. In the library program we supply +such a function, based on the so-called cubic spline method to be +described below. + +

+A spline function consists of polynomial pieces defined on +subintervals. The different subintervals are connected via +various continuity relations. + +

+Assume we have at our disposal \( n+1 \) points \( x_0, x_1, \dots x_n \) +arranged so that \( x_0 < x_1 < x_2 < \dots x_{n-1} < x_n \) (such points are called +knots). A spline function \( s \) of degree \( k \) with \( n+1 \) knots is defined +as follows + +

    +
  • On every subinterval \( [x_{i-1},x_i) \) s is a polynomial of degree \( \le k \).
  • +
  • \( s \) has \( k-1 \) continuous derivatives in the whole interval \( [x_0,x_n] \).
  • +
+
+ + +

+









+ +

Splines

+
+ +

+As an example, consider a spline function of degree \( k=1 \) defined as follows +$$ + s(x)=\begin{bmatrix} s_0(x)=a_0x+b_0 & x\in [x_0, x_1) \\ + s_1(x)=a_1x+b_1 & x\in [x_1, x_2) \\ + \dots & \dots \\ + s_{n-1}(x)=a_{n-1}x+b_{n-1} & x\in + [x_{n-1}, x_n] \end{bmatrix}. +$$ + +In this case the polynomial consists of series of straight lines +connected to each other at every endpoint. The number of continuous +derivatives is then \( k-1=0 \), as expected when we deal with straight lines. +Such a polynomial is quite easy to construct given +\( n+1 \) points \( x_0, x_1, \dots x_n \) and their corresponding +function values. +

+ + +

+









+ +

Splines

+
+ +

+The most commonly used spline function is the one with \( k=3 \), the so-called +cubic spline function. +Assume that we have in adddition to the \( n+1 \) knots a series of +functions values \( y_0=f(x_0), y_1=f(x_1), \dots y_n=f(x_n) \). +By definition, the polynomials \( s_{i-1} \) and \( s_i \) +are thence supposed to interpolate the same point \( i \), that is +$$ + s_{i-1}(x_i)= y_i = s_i(x_i), +$$ + +with \( 1 \le i \le n-1 \). In total we have \( n \) polynomials of the +type +$$ + s_i(x)=a_{i0}+a_{i1}x+a_{i2}x^2+a_{i2}x^3, +$$ + +yielding \( 4n \) coefficients to determine. +

+ + +

+









+ +

Splines

+
+ +

+Every subinterval provides in addition the \( 2n \) conditions +$$ + y_i = s(x_i), +$$ + +and +$$ + s(x_{i+1})= y_{i+1}, +$$ + +to be fulfilled. If we also assume that \( s' \) and \( s'' \) are continuous, +then +$$ + s'_{i-1}(x_i)= s'_i(x_i), +$$ + +yields \( n-1 \) conditions. Similarly, +$$ + s''_{i-1}(x_i)= s''_i(x_i), +$$ + +results in additional \( n-1 \) conditions. In total we have \( 4n \) coefficients +and \( 4n-2 \) equations to determine them, leaving us with \( 2 \) degrees of +freedom to be determined. +

+ + +

+









+ +

Splines

+
+ +

+Using the last equation we define two values for the second derivative, namely +$$ + s''_{i}(x_i)= f_i, +$$ + +and +$$ + s''_{i}(x_{i+1})= f_{i+1}, +$$ + +and setting up a straight line between \( f_i \) and \( f_{i+1} \) we have +$$ + s_i''(x) = \frac{f_i}{x_{i+1}-x_i}(x_{i+1}-x)+ + \frac{f_{i+1}}{x_{i+1}-x_i}(x-x_i), +$$ + +and integrating twice one obtains +$$ + s_i(x) = \frac{f_i}{6(x_{i+1}-x_i)}(x_{i+1}-x)^3+ + \frac{f_{i+1}}{6(x_{i+1}-x_i)}(x-x_i)^3 + +c(x-x_i)+d(x_{i+1}-x). +$$ +

+ + +

+









+ +

Splines

+
+ +

+Using the conditions \( s_i(x_i)=y_i \) and \( s_i(x_{i+1})=y_{i+1} \) +we can in turn determine the constants \( c \) and \( d \) resulting in +$$ +\begin{align} + s_i(x) =&\frac{f_i}{6(x_{i+1}-x_i)}(x_{i+1}-x)^3+ + \frac{f_{i+1}}{6(x_{i+1}-x_i)}(x-x_i)^3 \nonumber \\ + +&(\frac{y_{i+1}}{x_{i+1}-x_i}-\frac{f_{i+1}(x_{i+1}-x_i)}{6}) + (x-x_i)+ + (\frac{y_{i}}{x_{i+1}-x_i}-\frac{f_{i}(x_{i+1}-x_i)}{6}) + (x_{i+1}-x). +\label{_auto1} +\end{align} +$$ +

+ + +

+









+ +

Splines

+
+ +

+How to determine the values of the second +derivatives \( f_{i} \) and \( f_{i+1} \)? We use the continuity assumption +of the first derivatives +$$ + s'_{i-1}(x_i)= s'_i(x_i), +$$ + +and set \( x=x_i \). Defining \( h_i=x_{i+1}-x_i \) we obtain finally +the following expression +$$ + h_{i-1}f_{i-1}+2(h_{i}+h_{i-1})f_i+h_if_{i+1}= + \frac{6}{h_i}(y_{i+1}-y_i)-\frac{6}{h_{i-1}}(y_{i}-y_{i-1}), +$$ + +and introducing the shorthands \( u_i=2(h_{i}+h_{i-1}) \), +\( v_i=\frac{6}{h_i}(y_{i+1}-y_i)-\frac{6}{h_{i-1}}(y_{i}-y_{i-1}) \), +we can reformulate the problem as a set of linear equations to be +solved through e.g., Gaussian elemination +

+ + +

+









+ +

Splines

+
+ +

+Gaussian elimination +$$ + \begin{bmatrix} u_1 & h_1 &0 &\dots & & & & \\ + h_1 & u_2 & h_2 &0 &\dots & & & \\ + 0 & h_2 & u_3 & h_3 &0 &\dots & & \\ + \dots& & \dots &\dots &\dots &\dots &\dots & \\ + &\dots & & &0 &h_{n-3} &u_{n-2} &h_{n-2} \\ + & && & &0 &h_{n-2} &u_{n-1} \end{bmatrix} + \begin{bmatrix} f_1 \\ + f_2 \\ + f_3\\ + \dots \\ + f_{n-2} \\ + f_{n-1} \end{bmatrix} = + \begin{bmatrix} v_1 \\ + v_2 \\ + v_3\\ + \dots \\ + v_{n-2}\\ + v_{n-1} \end{bmatrix}. +$$ + +Note that this is a set of tridiagonal equations and can be solved +through only \( O(n) \) operations. +

+ + +

+









+ +

Splines

+
+ +

+The functions supplied in the program library are spline and splint. +In order to use cubic spline interpolation you need first to call + +

+ + +

spline(double x[], double y[], int n, double yp1,  double yp2, double y2[])
+
+

+This function takes as +input \( x[0,..,n - 1] \) and \( y[0,..,n - 1] \) containing a tabulation +\( y_i = f(x_i) \) with \( x_0 < x_1 < .. < x_{n - 1} \) +together with the +first derivatives of \( f(x) \) at \( x_0 \) and \( x_{n-1} \), respectively. Then the +function returns \( y2[0,..,n-1] \) which contains the second derivatives of +\( f(x_i) \) at each point \( x_i \). \( n \) is the number of points. +This function provides the cubic spline interpolation for all subintervals +and is called only once. +

+ + +

+









+ +

Splines

+
+ +

+Thereafter, if you wish to make various interpolations, you need to call the function +

+ + +

splint(double x[], double y[], double y2a[], int n, double x, double *y)
+
+

+which takes as input +the tabulated values \( x[0,..,n - 1] \) and \( y[0,..,n - 1] \) and the output +y2a[0,..,n - 1] from spline. It returns the value \( y \) corresponding +to the point \( x \). +

+ + +

+









+ +

Conjugate gradient (CG) method

+
+ +

+The success of the CG method for finding solutions of non-linear problems is based +on the theory of conjugate gradients for linear systems of equations. It belongs +to the class of iterative methods for solving problems from linear algebra of the type +$$ +\begin{equation*} + \hat{A}\hat{x} = \hat{b}. +\end{equation*} +$$ + +In the iterative process we end up with a problem like + +$$ +\begin{equation*} + \hat{r}= \hat{b}-\hat{A}\hat{x}, +\end{equation*} +$$ + +where \( \hat{r} \) is the so-called residual or error in the iterative process. + +

+When we have found the exact solution, \( \hat{r}=0 \). +

+ + +

+









+ +

Conjugate gradient method

+
+ +

+ +

+The residual is zero when we reach the minimum of the quadratic equation +$$ +\begin{equation*} + P(\hat{x})=\frac{1}{2}\hat{x}^T\hat{A}\hat{x} - \hat{x}^T\hat{b}, +\end{equation*} +$$ + +with the constraint that the matrix \( \hat{A} \) is positive definite and symmetric. +If we search for a minimum of the quantum mechanical variance, then the matrix +\( \hat{A} \), which is called the Hessian, is given by the second-derivative of the function we want to minimize. This quantity is always positive definite. In our case this corresponds normally to the second derivative of the energy. +

+ + +

+









+ +

Conjugate gradient method, Newton's method first

+
+ +

+We seek the minimum of the energy or the variance as function of various variational parameters. +In our case we have thus a function \( f \) whose minimum we are seeking. +In Newton's method we set \( \nabla f = 0 \) and we can thus compute the next iteration point +$$ +\begin{equation*} +\hat{x}-\hat{x}_i=\hat{A}^{-1}\nabla f(\hat{x}_i). +\end{equation*} +$$ + +Subtracting this equation from that of \( \hat{x}_{i+1} \) we have +$$ +\begin{equation*} +\hat{x}_{i+1}-\hat{x}_i=\hat{A}^{-1}(\nabla f(\hat{x}_{i+1})-\nabla f(\hat{x}_i)). +\end{equation*} +$$ +

+ + +

+









+ +

Simple example and demonstration

+
+ +

+The function \( f \) can be either the energy or the variance. If we choose the energy then we have +$$ +\begin{equation*} +\hat{\alpha}_{i+1}-\hat{\alpha}_i=\hat{A}^{-1}(\nabla E(\hat{\alpha}_{i+1})-\nabla E(\hat{\alpha}_i)). +\end{equation*} +$$ + +In the simple harmonic oscillator model, the gradient and the Hessian \( \hat{A} \) are +$$ +\begin{equation*} +\frac{d\langle E_L[\alpha]\rangle}{d\alpha} = \alpha-\frac{1}{4\alpha^3} +\end{equation*} +$$ + +and a second derivative which is always positive (meaning that we find a minimum) +$$ +\begin{equation*} +\hat{A}= \frac{d^2\langle E_L[\alpha]\rangle}{d\alpha^2} = 1+\frac{3}{4\alpha^4} +\end{equation*} +$$ +

+ + +

+









+ +

Simple example and demonstration

+
+ +

+We get then +$$ +\begin{equation*} +\alpha_{i+1}=\frac{4}{3}\alpha_i-\frac{\alpha_i^4}{3\alpha_{i+1}^3}, +\end{equation*} +$$ + +which can be rewritten as +$$ +\begin{equation*} +\alpha_{i+1}^4-\frac{4}{3}\alpha_i\alpha_{i+1}^4+\frac{1}{3}\alpha_i^4. +\end{equation*} +$$ +

+ + +

+









+ +

Conjugate gradient method

+
+ +

+In the CG method we define so-called conjugate directions and two vectors +\( \hat{s} \) and \( \hat{t} \) +are said to be +conjugate if +$$ +\begin{equation*} +\hat{s}^T\hat{A}\hat{t}= 0. +\end{equation*} +$$ + +The philosophy of the CG method is to perform searches in various conjugate directions +of our vectors \( \hat{x}_i \) obeying the above criterion, namely +$$ +\begin{equation*} +\hat{x}_i^T\hat{A}\hat{x}_j= 0. +\end{equation*} +$$ + +Two vectors are conjugate if they are orthogonal with respect to +this inner product. Being conjugate is a symmetric relation: if \( \hat{s} \) is conjugate to \( \hat{t} \), then \( \hat{t} \) is conjugate to \( \hat{s} \). +

+ + +

+









+ +

Conjugate gradient method

+
+ +

+An example is given by the eigenvectors of the matrix +$$ +\begin{equation*} +\hat{v}_i^T\hat{A}\hat{v}_j= \lambda\hat{v}_i^T\hat{v}_j, +\end{equation*} +$$ + +which is zero unless \( i=j \). +

+ + +

+









+ +

Conjugate gradient method

+
+ +

+Assume now that we have a symmetric positive-definite matrix \( \hat{A} \) of size +\( n\times n \). At each iteration \( i+1 \) we obtain the conjugate direction of a vector +$$ +\begin{equation*} +\hat{x}_{i+1}=\hat{x}_{i}+\alpha_i\hat{p}_{i}. +\end{equation*} +$$ + +We assume that \( \hat{p}_{i} \) is a sequence of \( n \) mutually conjugate directions. +Then the \( \hat{p}_{i} \) form a basis of \( R^n \) and we can expand the solution +$ \hat{A}\hat{x} = \hat{b}$ in this basis, namely + +$$ +\begin{equation*} + \hat{x} = \sum^{n}_{i=1} \alpha_i \hat{p}_i. +\end{equation*} +$$ +

+ + +

+









+ +

Conjugate gradient method

+
+ +

+The coefficients are given by +$$ +\begin{equation*} + \mathbf{A}\mathbf{x} = \sum^{n}_{i=1} \alpha_i \mathbf{A} \mathbf{p}_i = \mathbf{b}. +\end{equation*} +$$ + +Multiplying with \( \hat{p}_k^T \) from the left gives + +$$ +\begin{equation*} + \hat{p}_k^T \hat{A}\hat{x} = \sum^{n}_{i=1} \alpha_i\hat{p}_k^T \hat{A}\hat{p}_i= \hat{p}_k^T \hat{b}, +\end{equation*} +$$ + +and we can define the coefficients \( \alpha_k \) as + +$$ +\begin{equation*} + \alpha_k = \frac{\hat{p}_k^T \hat{b}}{\hat{p}_k^T \hat{A} \hat{p}_k} +\end{equation*} +$$ +

+ + +

+









+ +

Conjugate gradient method and iterations

+
+ +

+ +

+If we choose the conjugate vectors \( \hat{p}_k \) carefully, +then we may not need all of them to obtain a good approximation to the solution +\( \hat{x} \). +We want to regard the conjugate gradient method as an iterative method. +This will us to solve systems where \( n \) is so large that the direct +method would take too much time. + +

+We denote the initial guess for \( \hat{x} \) as \( \hat{x}_0 \). +We can assume without loss of generality that +$$ +\begin{equation*} +\hat{x}_0=0, +\end{equation*} +$$ + +or consider the system +$$ +\begin{equation*} +\hat{A}\hat{z} = \hat{b}-\hat{A}\hat{x}_0, +\end{equation*} +$$ + +instead. +

+ + +

+









+ +

Conjugate gradient method

+
+ +

+One can show that the solution \( \hat{x} \) is also the unique minimizer of the quadratic form +$$ +\begin{equation*} + f(\hat{x}) = \frac{1}{2}\hat{x}^T\hat{A}\hat{x} - \hat{x}^T \hat{x} , \quad \hat{x}\in\mathbf{R}^n. +\end{equation*} +$$ + +This suggests taking the first basis vector \( \hat{p}_1 \) +to be the gradient of \( f \) at \( \hat{x}=\hat{x}_0 \), +which equals +$$ +\begin{equation*} +\hat{A}\hat{x}_0-\hat{b}, +\end{equation*} +$$ + +and +\( \hat{x}_0=0 \) it is equal \( -\hat{b} \). +The other vectors in the basis will be conjugate to the gradient, +hence the name conjugate gradient method. +

+ + +

+









+ +

Conjugate gradient method

+
+ +

+Let \( \hat{r}_k \) be the residual at the \( k \)-th step: +$$ +\begin{equation*} +\hat{r}_k=\hat{b}-\hat{A}\hat{x}_k. +\end{equation*} +$$ + +Note that \( \hat{r}_k \) is the negative gradient of \( f \) at +\( \hat{x}=\hat{x}_k \), +so the gradient descent method would be to move in the direction \( \hat{r}_k \). +Here, we insist that the directions \( \hat{p}_k \) are conjugate to each other, +so we take the direction closest to the gradient \( \hat{r}_k \) +under the conjugacy constraint. +This gives the following expression +$$ +\begin{equation*} +\hat{p}_{k+1}=\hat{r}_k-\frac{\hat{p}_k^T \hat{A}\hat{r}_k}{\hat{p}_k^T\hat{A}\hat{p}_k} \hat{p}_k. +\end{equation*} +$$ +

+ + +

+









+ +

Conjugate gradient method

+
+ +

+We can also compute the residual iteratively as +$$ +\begin{equation*} +\hat{r}_{k+1}=\hat{b}-\hat{A}\hat{x}_{k+1}, + \end{equation*} +$$ + +which equals +$$ +\begin{equation*} +\hat{b}-\hat{A}(\hat{x}_k+\alpha_k\hat{p}_k), + \end{equation*} +$$ + +or +$$ +\begin{equation*} +(\hat{b}-\hat{A}\hat{x}_k)-\alpha_k\hat{A}\hat{p}_k, + \end{equation*} +$$ + +which gives + +$$ +\begin{equation*} +\hat{r}_{k+1}=\hat{r}_k-\hat{A}\hat{p}_{k}, + \end{equation*} +$$ +

+ + +

+ + + + +

+ © 1999-2018, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license +
+ + + + + + diff --git a/doc/pub/Splines/html/reveal.js/.gitignore b/doc/pub/Splines/html/reveal.js/.gitignore new file mode 100644 index 000000000..a5df3133d --- /dev/null +++ b/doc/pub/Splines/html/reveal.js/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +.svn +log/*.log +tmp/** +node_modules/ +.sass-cache +css/reveal.min.css +js/reveal.min.js diff --git a/doc/pub/Splines/html/reveal.js/.travis.yml b/doc/pub/Splines/html/reveal.js/.travis.yml new file mode 100644 index 000000000..165d9ae9f --- /dev/null +++ b/doc/pub/Splines/html/reveal.js/.travis.yml @@ -0,0 +1,5 @@ +language: node_js +node_js: + - 0.10 +before_script: + - npm install -g grunt-cli \ No newline at end of file diff --git a/doc/pub/Splines/html/reveal.js/CONTRIBUTING.md b/doc/pub/Splines/html/reveal.js/CONTRIBUTING.md new file mode 100644 index 000000000..c2091e88f --- /dev/null +++ b/doc/pub/Splines/html/reveal.js/CONTRIBUTING.md @@ -0,0 +1,23 @@ +## Contributing + +Please keep the [issue tracker](http://github.com/hakimel/reveal.js/issues) limited to **bug reports**, **feature requests** and **pull requests**. + + +### Personal Support +If you have personal support or setup questions the best place to ask those are [StackOverflow](http://stackoverflow.com/questions/tagged/reveal.js). + + +### Bug Reports +When reporting a bug make sure to include information about which browser and operating system you are on as well as the necessary steps to reproduce the issue. If possible please include a link to a sample presentation where the bug can be tested. + + +### Pull Requests +- Should follow the coding style of the file you work in, most importantly: + - Tabs to indent + - Single-quoted strings +- Should be made towards the **dev branch** +- Should be submitted from a feature/topic branch (not your master) + + +### Plugins +Please do not submit plugins as pull requests. They should be maintained in their own separate repository. More information here: https://github.com/hakimel/reveal.js/wiki/Plugin-Guidelines diff --git a/doc/pub/Splines/html/reveal.js/Gruntfile.js b/doc/pub/Splines/html/reveal.js/Gruntfile.js new file mode 100644 index 000000000..b257e8f32 --- /dev/null +++ b/doc/pub/Splines/html/reveal.js/Gruntfile.js @@ -0,0 +1,140 @@ +/* global module:false */ +module.exports = function(grunt) { + var port = grunt.option('port') || 8000; + // Project configuration + grunt.initConfig({ + pkg: grunt.file.readJSON('package.json'), + meta: { + banner: + '/*!\n' + + ' * reveal.js <%= pkg.version %> (<%= grunt.template.today("yyyy-mm-dd, HH:MM") %>)\n' + + ' * http://lab.hakim.se/reveal-js\n' + + ' * MIT licensed\n' + + ' *\n' + + ' * Copyright (C) 2014 Hakim El Hattab, http://hakim.se\n' + + ' */' + }, + + qunit: { + files: [ 'test/*.html' ] + }, + + uglify: { + options: { + banner: '<%= meta.banner %>\n' + }, + build: { + src: 'js/reveal.js', + dest: 'js/reveal.min.js' + } + }, + + cssmin: { + compress: { + files: { + 'css/reveal.min.css': [ 'css/reveal.css' ] + } + } + }, + + sass: { + main: { + files: { + 'css/theme/darkgray.css': 'css/theme/source/darkgray.scss', + 'css/theme/beigesmall.css': 'css/theme/source/beigesmall.scss', + 'css/theme/cbc.css': 'css/theme/source/cbc.scss', + 'css/theme/default.css': 'css/theme/source/default.scss', + 'css/theme/beige.css': 'css/theme/source/beige.scss', + 'css/theme/night.css': 'css/theme/source/night.scss', + 'css/theme/serif.css': 'css/theme/source/serif.scss', + 'css/theme/simple.css': 'css/theme/source/simple.scss', + 'css/theme/sky.css': 'css/theme/source/sky.scss', + 'css/theme/moon.css': 'css/theme/source/moon.scss', + 'css/theme/solarized.css': 'css/theme/source/solarized.scss', + 'css/theme/blood.css': 'css/theme/source/blood.scss' + } + } + }, + + jshint: { + options: { + curly: false, + eqeqeq: true, + immed: true, + latedef: true, + newcap: true, + noarg: true, + sub: true, + undef: true, + eqnull: true, + browser: true, + expr: true, + globals: { + head: false, + module: false, + console: false, + unescape: false + } + }, + files: [ 'Gruntfile.js', 'js/reveal.js' ] + }, + + connect: { + server: { + options: { + port: port, + base: '.' + } + } + }, + + zip: { + 'reveal-js-presentation.zip': [ + 'index.html', + 'css/**', + 'js/**', + 'lib/**', + 'images/**', + 'plugin/**' + ] + }, + + watch: { + main: { + files: [ 'Gruntfile.js', 'js/reveal.js', 'css/reveal.css' ], + tasks: 'default' + }, + theme: { + files: [ 'css/theme/source/*.scss', 'css/theme/template/*.scss' ], + tasks: 'themes' + } + } + + }); + + // Dependencies + grunt.loadNpmTasks( 'grunt-contrib-qunit' ); + grunt.loadNpmTasks( 'grunt-contrib-jshint' ); + grunt.loadNpmTasks( 'grunt-contrib-cssmin' ); + grunt.loadNpmTasks( 'grunt-contrib-uglify' ); + grunt.loadNpmTasks( 'grunt-contrib-watch' ); + grunt.loadNpmTasks( 'grunt-contrib-sass' ); + grunt.loadNpmTasks( 'grunt-contrib-connect' ); + grunt.loadNpmTasks( 'grunt-zip' ); + + // Default task + grunt.registerTask( 'default', [ 'jshint', 'cssmin', 'uglify', 'qunit' ] ); + + // Theme task + grunt.registerTask( 'themes', [ 'sass' ] ); + + // Package presentation to archive + grunt.registerTask( 'package', [ 'default', 'zip' ] ); + + // Serve presentation locally + grunt.registerTask( 'serve', [ 'connect', 'watch' ] ); + + // Run tests + grunt.registerTask( 'test', [ 'jshint', 'qunit' ] ); + +}; diff --git a/doc/pub/Splines/html/reveal.js/LICENSE b/doc/pub/Splines/html/reveal.js/LICENSE new file mode 100644 index 000000000..09623076f --- /dev/null +++ b/doc/pub/Splines/html/reveal.js/LICENSE @@ -0,0 +1,19 @@ +Copyright (C) 2015 Hakim El Hattab, http://hakim.se + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/doc/pub/Splines/html/reveal.js/README.md b/doc/pub/Splines/html/reveal.js/README.md new file mode 100644 index 000000000..573b19597 --- /dev/null +++ b/doc/pub/Splines/html/reveal.js/README.md @@ -0,0 +1,1052 @@ +# reveal.js [![Build Status](https://travis-ci.org/hakimel/reveal.js.svg?branch=master)](https://travis-ci.org/hakimel/reveal.js) + +A framework for easily creating beautiful presentations using HTML. [Check out the live demo](http://lab.hakim.se/reveal-js/). + +reveal.js comes with a broad range of features including [nested slides](https://github.com/hakimel/reveal.js#markup), [Markdown contents](https://github.com/hakimel/reveal.js#markdown), [PDF export](https://github.com/hakimel/reveal.js#pdf-export), [speaker notes](https://github.com/hakimel/reveal.js#speaker-notes) and a [JavaScript API](https://github.com/hakimel/reveal.js#api). It's best viewed in a modern browser but [fallbacks](https://github.com/hakimel/reveal.js/wiki/Browser-Support) are available to make sure your presentation can still be viewed elsewhere. + + +#### More reading: +- [Installation](#installation): Step-by-step instructions for getting reveal.js running on your computer. +- [Changelog](https://github.com/hakimel/reveal.js/releases): Up-to-date version history. +- [Examples](https://github.com/hakimel/reveal.js/wiki/Example-Presentations): Presentations created with reveal.js, add your own! +- [Browser Support](https://github.com/hakimel/reveal.js/wiki/Browser-Support): Explanation of browser support and fallbacks. +- [Plugins](https://github.com/hakimel/reveal.js/wiki/Plugins,-Tools-and-Hardware): A list of plugins that can be used to extend reveal.js. + +## Online Editor + +Presentations are written using HTML or Markdown but there's also an online editor for those of you who prefer a graphical interface. Give it a try at [http://slides.com](http://slides.com). + + +## Instructions + +### Markup + +Markup hierarchy needs to be ``
`` where the ``
`` represents one slide and can be repeated indefinitely. If you place multiple ``
``'s inside of another ``
`` they will be shown as vertical slides. The first of the vertical slides is the "root" of the others (at the top), and it will be included in the horizontal sequence. For example: + +```html +
+
+
Single Horizontal Slide
+
+
Vertical Slide 1
+
Vertical Slide 2
+
+
+
+``` + +### Markdown + +It's possible to write your slides using Markdown. To enable Markdown, add the ```data-markdown``` attribute to your ```
``` elements and wrap the contents in a ``` +
+``` + +#### External Markdown + +You can write your content as a separate file and have reveal.js load it at runtime. Note the separator arguments which determine how slides are delimited in the external file. The ```data-charset``` attribute is optional and specifies which charset to use when loading the external file. + +When used locally, this feature requires that reveal.js [runs from a local web server](#full-setup). + +```html +
+
+``` + +#### Element Attributes + +Special syntax (in html comment) is available for adding attributes to Markdown elements. This is useful for fragments, amongst other things. + +```html +
+ +
+``` + +#### Slide Attributes + +Special syntax (in html comment) is available for adding attributes to the slide `
` elements generated by your Markdown. + +```html +
+ +
+``` + + +### Configuration + +At the end of your page you need to initialize reveal by running the following code. Note that all config values are optional and will default as specified below. + +```javascript +Reveal.initialize({ + + // Display controls in the bottom right corner + controls: true, + + // Display a presentation progress bar + progress: true, + + // Display the page number of the current slide + slideNumber: false, + + // Push each slide change to the browser history + history: false, + + // Enable keyboard shortcuts for navigation + keyboard: true, + + // Enable the slide overview mode + overview: true, + + // Vertical centering of slides + center: true, + + // Enables touch navigation on devices with touch input + touch: true, + + // Loop the presentation + loop: false, + + // Change the presentation direction to be RTL + rtl: false, + + // Turns fragments on and off globally + fragments: true, + + // Flags if the presentation is running in an embedded mode, + // i.e. contained within a limited portion of the screen + embedded: false, + + // Flags if we should show a help overlay when the questionmark + // key is pressed + help: true, + + // Number of milliseconds between automatically proceeding to the + // next slide, disabled when set to 0, this value can be overwritten + // by using a data-autoslide attribute on your slides + autoSlide: 0, + + // Stop auto-sliding after user input + autoSlideStoppable: true, + + // Enable slide navigation via mouse wheel + mouseWheel: false, + + // Hides the address bar on mobile devices + hideAddressBar: true, + + // Opens links in an iframe preview overlay + previewLinks: false, + + // Transition style + transition: 'default', // none/fade/slide/convex/concave/zoom + + // Transition speed + transitionSpeed: 'default', // default/fast/slow + + // Transition style for full page slide backgrounds + backgroundTransition: 'default', // none/fade/slide/convex/concave/zoom + + // Number of slides away from the current that are visible + viewDistance: 3, + + // Parallax background image + parallaxBackgroundImage: '', // e.g. "'https://s3.amazonaws.com/hakim-static/reveal-js/reveal-parallax-1.jpg'" + + // Parallax background size + parallaxBackgroundSize: '', // CSS syntax, e.g. "2100px 900px" + + // Amount to move parallax background (horizontal and vertical) on slide change + // Number, e.g. 100 + parallaxBackgroundHorizontal: '', + parallaxBackgroundVertical: '' + +}); +``` + + +The configuration can be updated after initialization using the ```configure``` method: + +```javascript +// Turn autoSlide off +Reveal.configure({ autoSlide: 0 }); + +// Start auto-sliding every 5s +Reveal.configure({ autoSlide: 5000 }); +``` + + +### Dependencies + +Reveal.js doesn't _rely_ on any third party scripts to work but a few optional libraries are included by default. These libraries are loaded as dependencies in the order they appear, for example: + +```javascript +Reveal.initialize({ + dependencies: [ + // Cross-browser shim that fully implements classList - https://github.com/eligrey/classList.js/ + { src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } }, + + // Interpret Markdown in
elements + { src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } }, + { src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } }, + + // Syntax highlight for elements + { src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } }, + + // Zoom in and out with Alt+click + { src: 'plugin/zoom-js/zoom.js', async: true }, + + // Speaker notes + { src: 'plugin/notes/notes.js', async: true }, + + // Remote control your reveal.js presentation using a touch device + { src: 'plugin/remotes/remotes.js', async: true }, + + // MathJax + { src: 'plugin/math/math.js', async: true } + ] +}); +``` + +You can add your own extensions using the same syntax. The following properties are available for each dependency object: +- **src**: Path to the script to load +- **async**: [optional] Flags if the script should load after reveal.js has started, defaults to false +- **callback**: [optional] Function to execute when the script has loaded +- **condition**: [optional] Function which must return true for the script to be loaded + + +### Ready Event + +A 'ready' event is fired when reveal.js has loaded all non-async dependencies and is ready to start navigating. To check if reveal.js is already 'ready' you can call `Reveal.isReady()`. + +```javascript +Reveal.addEventListener( 'ready', function( event ) { + // event.currentSlide, event.indexh, event.indexv +} ); +``` + + +### Presentation Size + +All presentations have a normal size, that is the resolution at which they are authored. The framework will automatically scale presentations uniformly based on this size to ensure that everything fits on any given display or viewport. + +See below for a list of configuration options related to sizing, including default values: + +```javascript +Reveal.initialize({ + + ... + + // The "normal" size of the presentation, aspect ratio will be preserved + // when the presentation is scaled to fit different resolutions. Can be + // specified using percentage units. + width: 960, + height: 700, + + // Factor of the display size that should remain empty around the content + margin: 0.1, + + // Bounds for smallest/largest possible scale to apply to content + minScale: 0.2, + maxScale: 1.5 + +}); +``` + + +### Auto-sliding + +Presentations can be configured to progress through slides automatically, without any user input. To enable this you will need to tell the framework how many milliseconds it should wait between slides: + +```javascript +// Slide every five seconds +Reveal.configure({ + autoSlide: 5000 +}); +``` +When this is turned on a control element will appear that enables users to pause and resume auto-sliding. Alternatively, sliding can be paused or resumed by pressing »a« on the keyboard. Sliding is paused automatically as soon as the user starts navigating. You can disable these controls by specifying ```autoSlideStoppable: false``` in your reveal.js config. + +You can also override the slide duration for individual slides and fragments by using the ```data-autoslide``` attribute: + +```html +
+

After 2 seconds the first fragment will be shown.

+

After 10 seconds the next fragment will be shown.

+

Now, the fragment is displayed for 2 seconds before the next slide is shown.

+
+``` + +Whenever the auto-slide mode is resumed or paused the ```autoslideresumed``` and ```autoslidepaused``` events are fired. + + +### Keyboard Bindings + +If you're unhappy with any of the default keyboard bindings you can override them using the ```keyboard``` config option: + +```javascript +Reveal.configure({ + keyboard: { + 13: 'next', // go to the next slide when the ENTER key is pressed + 27: function() {}, // do something custom when ESC is pressed + 32: null // don't do anything when SPACE is pressed (i.e. disable a reveal.js default binding) + } +}); +``` + +### Lazy Loading + +When working on presentation with a lot of media or iframe content it's important to load lazily. Lazy loading means that reveal.js will only load content for the few slides nearest to the current slide. The number of slides that are preloaded is determined by the `viewDistance` configuration option. + +To enable lazy loading all you need to do is change your "src" attributes to "data-src" as shown below. This is supported for image, video, audio and iframe elements. Lazy loaded iframes will also unload when the containing slide is no longer visible. + +```html +
+ + + +
+``` + + +### API + +The ``Reveal`` object exposes a JavaScript API for controlling navigation and reading state: + +```javascript +// Navigation +Reveal.slide( indexh, indexv, indexf ); +Reveal.left(); +Reveal.right(); +Reveal.up(); +Reveal.down(); +Reveal.prev(); +Reveal.next(); +Reveal.prevFragment(); +Reveal.nextFragment(); + +// Toggle presentation states, optionally pass true/false to force on/off +Reveal.toggleOverview(); +Reveal.togglePause(); +Reveal.toggleAutoSlide(); + +// Change a config value at runtime +Reveal.configure({ controls: true }); + +// Returns the present configuration options +Reveal.getConfig(); + +// Fetch the current scale of the presentation +Reveal.getScale(); + +// Retrieves the previous and current slide elements +Reveal.getPreviousSlide(); +Reveal.getCurrentSlide(); + +Reveal.getIndices(); // { h: 0, v: 0 } } +Reveal.getProgress(); // 0-1 +Reveal.getTotalSlides(); + +// State checks +Reveal.isFirstSlide(); +Reveal.isLastSlide(); +Reveal.isOverview(); +Reveal.isPaused(); +Reveal.isAutoSliding(); +``` + +### Slide Changed Event + +A 'slidechanged' event is fired each time the slide is changed (regardless of state). The event object holds the index values of the current slide as well as a reference to the previous and current slide HTML nodes. + +Some libraries, like MathJax (see [#226](https://github.com/hakimel/reveal.js/issues/226#issuecomment-10261609)), get confused by the transforms and display states of slides. Often times, this can be fixed by calling their update or render function from this callback. + +```javascript +Reveal.addEventListener( 'slidechanged', function( event ) { + // event.previousSlide, event.currentSlide, event.indexh, event.indexv +} ); +``` + +### Presentation State + +The presentation's current state can be fetched by using the `getState` method. A state object contains all of the information required to put the presentation back as it was when `getState` was first called. Sort of like a snapshot. It's a simple object that can easily be stringified and persisted or sent over the wire. + +```javascript +Reveal.slide( 1 ); +// we're on slide 1 + +var state = Reveal.getState(); + +Reveal.slide( 3 ); +// we're on slide 3 + +Reveal.setState( state ); +// we're back on slide 1 +``` + +### Slide States + +If you set ``data-state="somestate"`` on a slide ``
``, "somestate" will be applied as a class on the document element when that slide is opened. This allows you to apply broad style changes to the page based on the active slide. + +Furthermore you can also listen to these changes in state via JavaScript: + +```javascript +Reveal.addEventListener( 'somestate', function() { + // TODO: Sprinkle magic +}, false ); +``` + +### Slide Backgrounds + +Slides are contained within a limited portion of the screen by default to allow them to fit any display and scale uniformly. You can apply full page backgrounds outside of the slide area by adding a ```data-background``` attribute to your ```
``` elements. Four different types of backgrounds are supported: color, image, video and iframe. Below are a few examples. + +```html +
+

All CSS color formats are supported, like rgba() or hsl().

+
+
+

This slide will have a full-size background image.

+
+
+

This background image will be sized to 100px and repeated.

+
+
+

Video. Multiple sources can be defined using a comma separated list. Video will loop when the data-background-video-loop attribute is provided.

+
+
+

Embeds a web page as a background. Note that the page won't be interactive.

+
+``` + +Backgrounds transition using a fade animation by default. This can be changed to a linear sliding transition by passing ```backgroundTransition: 'slide'``` to the ```Reveal.initialize()``` call. Alternatively you can set ```data-background-transition``` on any section with a background to override that specific transition. + + +### Parallax Background + +If you want to use a parallax scrolling background, set the first two config properties below when initializing reveal.js (the other two are optional). + +```javascript +Reveal.initialize({ + + // Parallax background image + parallaxBackgroundImage: '', // e.g. "https://s3.amazonaws.com/hakim-static/reveal-js/reveal-parallax-1.jpg" + + // Parallax background size + parallaxBackgroundSize: '', // CSS syntax, e.g. "2100px 900px" - currently only pixels are supported (don't use % or auto) + + // Amount of pixels to move the parallax background per slide step, + // a value of 0 disables movement along the given axis + // These are optional, if they aren't specified they'll be calculated automatically + parallaxBackgroundHorizontal: 200, + parallaxBackgroundVertical: 50 + +}); +``` + +Make sure that the background size is much bigger than screen size to allow for some scrolling. [View example](http://lab.hakim.se/reveal-js/?parallaxBackgroundImage=https%3A%2F%2Fs3.amazonaws.com%2Fhakim-static%2Freveal-js%2Freveal-parallax-1.jpg¶llaxBackgroundSize=2100px%20900px). + + + +### Slide Transitions +The global presentation transition is set using the ```transition``` config value. You can override the global transition for a specific slide by using the ```data-transition``` attribute: + +```html +
+

This slide will override the presentation transition and zoom!

+
+ +
+

Choose from three transition speeds: default, fast or slow!

+
+``` + +You can also use different in and out transitions for the same slide: + +```html +
+ The train goes on … +
+
+ and on … +
+
+ and stops. +
+
+ (Passengers entering and leaving) +
+
+ And it starts again. +
+``` + + +Note that this does not work with the page and cube transitions. + + +### Internal links + +It's easy to link between slides. The first example below targets the index of another slide whereas the second targets a slide with an ID attribute (```
```): + +```html +Link +Link +``` + +You can also add relative navigation links, similar to the built in reveal.js controls, by appending one of the following classes on any element. Note that each element is automatically given an ```enabled``` class when it's a valid navigation route based on the current slide. + +```html + + + + + + +``` + + +### Fragments +Fragments are used to highlight individual elements on a slide. Every element with the class ```fragment``` will be stepped through before moving on to the next slide. Here's an example: http://lab.hakim.se/reveal-js/#/fragments + +The default fragment style is to start out invisible and fade in. This style can be changed by appending a different class to the fragment: + +```html +
+

grow

+

shrink

+

fade-out

+

visible only once

+

blue only once

+

highlight-red

+

highlight-green

+

highlight-blue

+
+``` + +Multiple fragments can be applied to the same element sequentially by wrapping it, this will fade in the text on the first step and fade it back out on the second. + +```html +
+ + I'll fade in, then out + +
+``` + +The display order of fragments can be controlled using the ```data-fragment-index``` attribute. + +```html +
+

Appears last

+

Appears first

+

Appears second

+
+``` + +### Fragment events + +When a slide fragment is either shown or hidden reveal.js will dispatch an event. + +Some libraries, like MathJax (see #505), get confused by the initially hidden fragment elements. Often times this can be fixed by calling their update or render function from this callback. + +```javascript +Reveal.addEventListener( 'fragmentshown', function( event ) { + // event.fragment = the fragment DOM element +} ); +Reveal.addEventListener( 'fragmenthidden', function( event ) { + // event.fragment = the fragment DOM element +} ); +``` + +### Code syntax highlighting + +By default, Reveal is configured with [highlight.js](http://softwaremaniacs.org/soft/highlight/en/) for code syntax highlighting. Below is an example with clojure code that will be syntax highlighted. When the `data-trim` attribute is present surrounding whitespace is automatically removed. + +```html +
+

+(def lazy-fib
+  (concat
+   [0 1]
+   ((fn rfib [a b]
+        (lazy-cons (+ a b) (rfib b (+ a b)))) 0 1)))
+	
+
+``` + +### Slide number +If you would like to display the page number of the current slide you can do so using the ```slideNumber``` configuration value. + +```javascript +// Shows the slide number using default formatting +Reveal.configure({ slideNumber: true }); + +// Slide number formatting can be configured using these variables: +// h: current slide's horizontal index +// v: current slide's vertical index +// c: current slide index (flattened) +// t: total number of slides (flattened) +Reveal.configure({ slideNumber: 'c / t' }); + +``` + + +### Overview mode + +Press "Esc" or "o" keys to toggle the overview mode on and off. While you're in this mode, you can still navigate between slides, +as if you were at 1,000 feet above your presentation. The overview mode comes with a few API hooks: + +```javascript +Reveal.addEventListener( 'overviewshown', function( event ) { /* ... */ } ); +Reveal.addEventListener( 'overviewhidden', function( event ) { /* ... */ } ); + +// Toggle the overview mode programmatically +Reveal.toggleOverview(); +``` + +### Fullscreen mode +Just press »F« on your keyboard to show your presentation in fullscreen mode. Press the »ESC« key to exit fullscreen mode. + + +### Embedded media +Embedded HTML5 `