|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "cb515e2a-7f48-454a-8962-3f604d3eb00b", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# APA - Octave/Matlab arbitrary precision arithmetic." |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "id": "3d298073-4ecd-4582-8f11-e9ddd8a8f193", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "## Installation\n", |
| 17 | + "\n", |
| 18 | + "From the Octave command-line run:" |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "code", |
| 23 | + "execution_count": null, |
| 24 | + "id": "f9668ec5-51e5-4bb7-bca7-baffc620e717", |
| 25 | + "metadata": {}, |
| 26 | + "outputs": [], |
| 27 | + "source": [ |
| 28 | + "pkg install 'https://github.com/gnu-octave/apa/releases/download/v0.1.5/apa-0.1.5.zip'\n", |
| 29 | + "pkg load apa\n", |
| 30 | + "pkg test apa" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "markdown", |
| 35 | + "id": "6dbc0ecb-238e-4496-86c6-4b5fefb96467", |
| 36 | + "metadata": {}, |
| 37 | + "source": [ |
| 38 | + "From the Matlab command-line run (also works for Octave):" |
| 39 | + ] |
| 40 | + }, |
| 41 | + { |
| 42 | + "cell_type": "code", |
| 43 | + "execution_count": null, |
| 44 | + "id": "66b7afda-6ee7-4241-8c85-482c03d34b1e", |
| 45 | + "metadata": {}, |
| 46 | + "outputs": [], |
| 47 | + "source": [ |
| 48 | + "urlwrite ('https://github.com/gnu-octave/apa/releases/download/v0.1.5/apa-0.1.5.zip', ...\n", |
| 49 | + " 'apa-0.1.5.zip');\n", |
| 50 | + "unzip ('apa-0.1.5.zip');\n", |
| 51 | + "cd (fullfile ('apa-0.1.5', 'inst'))\n", |
| 52 | + "install_apa\n", |
| 53 | + "test_apa" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "markdown", |
| 58 | + "id": "24925e83-23be-48d1-b1e1-ed14141d574f", |
| 59 | + "metadata": {}, |
| 60 | + "source": [ |
| 61 | + "## High-level MPFR Interface\n", |
| 62 | + "\n", |
| 63 | + "The high-level MPFR interface is given through the `@mpfr_t` class.\n", |
| 64 | + "A variable of that type \"behaves\" like a \"normal\" built-in Octave/Matlab\n", |
| 65 | + "data type." |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "code", |
| 70 | + "execution_count": 3, |
| 71 | + "id": "08296fd1-44fb-4719-8aea-62823fcedfc7", |
| 72 | + "metadata": {}, |
| 73 | + "outputs": [ |
| 74 | + { |
| 75 | + "name": "stdout", |
| 76 | + "output_type": "stream", |
| 77 | + "text": [ |
| 78 | + "rop =\n", |
| 79 | + "\n", |
| 80 | + " MPFR 3x3 matrix (precision 53 binary digits)\n", |
| 81 | + "\n", |
| 82 | + " Double approximation:\n", |
| 83 | + "\n", |
| 84 | + " 5 1 1\n", |
| 85 | + " 1 5 1\n", |
| 86 | + " 1 1 5\n", |
| 87 | + "\n" |
| 88 | + ] |
| 89 | + } |
| 90 | + ], |
| 91 | + "source": [ |
| 92 | + "op1 = mpfr_t (eye (3) * 4);\n", |
| 93 | + "\n", |
| 94 | + "rop = op1 + 1" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "markdown", |
| 99 | + "id": "0823de62-cf4a-4799-b066-2791bc862f41", |
| 100 | + "metadata": {}, |
| 101 | + "source": [ |
| 102 | + "The high-level MPFR interface is the preferred choice for quick numerical\n", |
| 103 | + "experiments.\n", |
| 104 | + "\n", |
| 105 | + "However, if performance is more critical, please use the low-level MPFR\n", |
| 106 | + "interface (explained below) and vectorization wherever possible.\n", |
| 107 | + "\n", |
| 108 | + "> Please note that an interface from an interpreted high-level programming\n", |
| 109 | + "> language like Octave/Matlab is most likely slower than a pre-compiled C\n", |
| 110 | + "> program.\n", |
| 111 | + ">\n", |
| 112 | + "> If performance is highly-critical, use this tool for initial experiments\n", |
| 113 | + "> and translate the developed algorithm to native MPFR C-code." |
| 114 | + ] |
| 115 | + }, |
| 116 | + { |
| 117 | + "cell_type": "markdown", |
| 118 | + "id": "39dcbdfa-4634-40f9-8a45-1f01ae1611bb", |
| 119 | + "metadata": {}, |
| 120 | + "source": [ |
| 121 | + "## Low-level MPFR Interface\n", |
| 122 | + "\n", |
| 123 | + "> For information how to compile/develop the interface, see\n", |
| 124 | + "> [`doc/MEX_INTERFACE.md`](https://github.com/gnu-octave/apa/blob/main/doc/MEX_INTERFACE.md).\n", |
| 125 | + "\n", |
| 126 | + "The low-level MPFR interface permits efficient access to almost all functions\n", |
| 127 | + "specified by MPFR 4.1.0 <https://www.mpfr.org/mpfr-4.1.0/mpfr.html>.\n", |
| 128 | + "\n", |
| 129 | + "All supported functions are [listed in the `inst` folder](inst)\n", |
| 130 | + "and can be called from Octave/Matlab like in the C programming language.\n", |
| 131 | + "\n", |
| 132 | + "For example, the C function:\n", |
| 133 | + "\n", |
| 134 | + "```c\n", |
| 135 | + "int mpfr_add (mpfr_t rop, mpfr_t op1, mpfr_t op2, mpfr_rnd_t rnd)\n", |
| 136 | + "```\n", |
| 137 | + "\n", |
| 138 | + "can be called from Octave/Matlab with scalar, vector, or matrix quantities:" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "code", |
| 143 | + "execution_count": 4, |
| 144 | + "id": "c0dfe5e1-4eda-4b3f-b779-785ed66ad09d", |
| 145 | + "metadata": {}, |
| 146 | + "outputs": [ |
| 147 | + { |
| 148 | + "name": "stdout", |
| 149 | + "output_type": "stream", |
| 150 | + "text": [ |
| 151 | + "rop =\n", |
| 152 | + "\n", |
| 153 | + " MPFR 3x3 matrix (precision 53 binary digits)\n", |
| 154 | + "\n", |
| 155 | + " Double approximation:\n", |
| 156 | + "\n", |
| 157 | + " 6 2 2\n", |
| 158 | + " 2 6 2\n", |
| 159 | + " 2 2 6\n", |
| 160 | + "\n" |
| 161 | + ] |
| 162 | + } |
| 163 | + ], |
| 164 | + "source": [ |
| 165 | + "% Prepare input and output variables.\n", |
| 166 | + "rop = mpfr_t (zeros (3));\n", |
| 167 | + "op1 = mpfr_t (eye (3) * 4);\n", |
| 168 | + "op2 = mpfr_t (2);\n", |
| 169 | + "rnd = mpfr_get_default_rounding_mode ();\n", |
| 170 | + "\n", |
| 171 | + "% Call mpfr_add. Note unlike Octave/Matlab the\n", |
| 172 | + "% left-hand side does NOT contain the result.\n", |
| 173 | + "ret = mpfr_add (rop, op1, op2, rnd);\n", |
| 174 | + "\n", |
| 175 | + "rop % Note rop vs. ret!" |
| 176 | + ] |
| 177 | + }, |
| 178 | + { |
| 179 | + "cell_type": "markdown", |
| 180 | + "id": "f3002580-752c-499c-b31e-f8fa8b17fb22", |
| 181 | + "metadata": {}, |
| 182 | + "source": [ |
| 183 | + "In the low-level interface the type checks are stricter,\n", |
| 184 | + "but scalar and matrix quantities can still be mixed.\n", |
| 185 | + "\n", |
| 186 | + "Another benefit of using the low-level MPFR interface is that **in-place**\n", |
| 187 | + "operations are permitted, which do not create new (temporary) variables:" |
| 188 | + ] |
| 189 | + }, |
| 190 | + { |
| 191 | + "cell_type": "code", |
| 192 | + "execution_count": 5, |
| 193 | + "id": "92886baf-f06e-43ae-8108-cc3e9cce2c15", |
| 194 | + "metadata": {}, |
| 195 | + "outputs": [], |
| 196 | + "source": [ |
| 197 | + "ret = mpfr_add (op1, op1, op1, rnd); % op1 += op1" |
| 198 | + ] |
| 199 | + } |
| 200 | + ], |
| 201 | + "metadata": { |
| 202 | + "kernelspec": { |
| 203 | + "display_name": "Octave", |
| 204 | + "language": "octave", |
| 205 | + "name": "octave" |
| 206 | + }, |
| 207 | + "language_info": { |
| 208 | + "file_extension": ".m", |
| 209 | + "help_links": [ |
| 210 | + { |
| 211 | + "text": "GNU Octave", |
| 212 | + "url": "https://www.gnu.org/software/octave/support.html" |
| 213 | + }, |
| 214 | + { |
| 215 | + "text": "Octave Kernel", |
| 216 | + "url": "https://github.com/Calysto/octave_kernel" |
| 217 | + }, |
| 218 | + { |
| 219 | + "text": "MetaKernel Magics", |
| 220 | + "url": "https://metakernel.readthedocs.io/en/latest/source/README.html" |
| 221 | + } |
| 222 | + ], |
| 223 | + "mimetype": "text/x-octave", |
| 224 | + "name": "octave", |
| 225 | + "version": "6.4.0" |
| 226 | + } |
| 227 | + }, |
| 228 | + "nbformat": 4, |
| 229 | + "nbformat_minor": 5 |
| 230 | +} |
0 commit comments