From 587dbf35ddbc864e6c1e44f91829f40d70edcca7 Mon Sep 17 00:00:00 2001 From: Orestis <orestis.malaspinas@pm.me> Date: Sun, 13 Oct 2024 21:03:55 +0200 Subject: [PATCH] update --- slides/cours_5.md | 89 ++++++ slides/figs/Float_example.svg | 92 ++++++ slides/figs/Float_example_bare.svg | 430 +++++++++++++++++++++++++++++ 3 files changed, 611 insertions(+) create mode 100644 slides/figs/Float_example.svg create mode 100644 slides/figs/Float_example_bare.svg diff --git a/slides/cours_5.md b/slides/cours_5.md index 026299a..7e3681f 100644 --- a/slides/cours_5.md +++ b/slides/cours_5.md @@ -518,4 +518,93 @@ int fib_imp(int n) { } ``` +# Exponentiation rapide + +\Huge L'exponentiation rapide ou indienne + +# Exponentiation rapide ou indienne (1/4) + +## But: Calculer $x^n$ + +* Quel est l'algorithmie le plus simple que vous pouvez imaginer? + +. . . + +```C +double pow(double x, int n) { + if (0 == n) { + return 1; + } + double p = c; + for (int i = 1; i < n; ++i) { + p = p * x; // x *= x + } + return x; +} +``` + +* Combien de multiplication et d'assignations en fonction de `n`? + +. . . + +* `n` assignations et `n` multiplications. + +# Exponentiation rapide ou indienne (2/4) + +* Proposez un algorithme naïf et récursif + +. . . + +```C +double pow(double x, int n) { + if (n != 0) { + return x * pow(x, n-1); + } else { + return 1; + } +} +``` + +# Exponentiation rapide ou indienne (3/4) + +## Exponentiation rapide ou indienne de $x^n$ + +* Écrivons $n=\sum_{i=0}^{d-1}b_i 2^i,\ b_i=\{0,1\}$ (écriture binaire sur $d$ bits, avec +$d\sim\log_2(n)$). +* +$$ +x^n={x^{2^0}}^{b_0}\cdot {x^{2^1}}^{b_1}\cdots {x^{2^{d-1}}}^{b_{d-1}}. +$$ +* On a besoin de $d$ calculs pour les $x^{2^i}$. +* On a besoin de $d$ calculs pour évaluer les produits de tous les termes. + +## Combien de calculs en terme de $n$? + +. . . + +* $n$ est représenté en binaire avec $d$ bits $\Rightarrow d\sim\log_2(n)$. +* il y a $2\log_2(n)\sim \log_2(n)$ calculs. + +# Exponentiation rapide ou indienne (4/4) + +## Le vrai algorithme + +* Si n est pair: calculer $\left(x^{n/2}\cdot x^{n/2}\right)$, +* Si n est impair: calculer $x \cdot \left(x^{(n-1)/2}\right)^2=x\cdot x^{n-1}$. + +## Exercice: écrire l'algorithme récursif correspondant + +. . . + +```C +double pow(double x, int n) { + if (0 == n) { + return 1; + } else if (n % 2 == 0) { + return pow(x, n / 2) * pow(x, n/2); + } else { + return x * pow(x, (n-1)); + } +} +``` diff --git a/slides/figs/Float_example.svg b/slides/figs/Float_example.svg new file mode 100644 index 0000000..5b34a69 --- /dev/null +++ b/slides/figs/Float_example.svg @@ -0,0 +1,92 @@ +<?xml version="1.0" encoding="UTF-8"?> +<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="590" height="75"> + <style> + text { + font-size: 14px; + font-family: Arial, sans-serif; + text-anchor: middle; + } + </style> + <defs> + <g id="bit-0"> + <rect width="15" height="26" stroke="#000"/> + <text x="8" y="18.3" fill="#000">0</text> + </g> + <g id="bit-1"> + <rect width="15" height="26" stroke="#000"/> + <text x="8" y="18.3" fill="#000">1</text> + </g> + </defs> + + <g transform="translate(14.25,28.75)"> + <use xlink:href="#bit-0" fill="#c5fcff"/> + <g fill="#9fffad"> + <use xlink:href="#bit-0" x="15"/> + <use xlink:href="#bit-1" x="30"/> + <use xlink:href="#bit-1" x="45"/> + <use xlink:href="#bit-1" x="60"/> + <use xlink:href="#bit-1" x="75"/> + <use xlink:href="#bit-1" x="90"/> + <use xlink:href="#bit-0" x="105"/> + <use xlink:href="#bit-0" x="120"/> + </g> + <g fill="#ffaead"> + <use xlink:href="#bit-0" x="135"/> + <use xlink:href="#bit-1" x="150"/> + <use xlink:href="#bit-0" x="165"/> + <use xlink:href="#bit-0" x="180"/> + <use xlink:href="#bit-0" x="195"/> + <use xlink:href="#bit-0" x="210"/> + <use xlink:href="#bit-0" x="225"/> + <use xlink:href="#bit-0" x="240"/> + <use xlink:href="#bit-0" x="255"/> + <use xlink:href="#bit-0" x="270"/> + <use xlink:href="#bit-0" x="285"/> + <use xlink:href="#bit-0" x="300"/> + <use xlink:href="#bit-0" x="315"/> + <use xlink:href="#bit-0" x="330"/> + <use xlink:href="#bit-0" x="345"/> + <use xlink:href="#bit-0" x="360"/> + <use xlink:href="#bit-0" x="375"/> + <use xlink:href="#bit-0" x="390"/> + <use xlink:href="#bit-0" x="405"/> + <use xlink:href="#bit-0" x="420"/> + <use xlink:href="#bit-0" x="435"/> + <use xlink:href="#bit-0" x="450"/> + <use xlink:href="#bit-0" x="465"/> + </g> + <g fill="none" stroke="#000" stroke-width="2"> + <rect width="480" height="26"/> + <path d="m15,0 v26 M135,0 v26" fill="none" stroke="#000"/> + </g> + + <path d="m7.5,-3.5 v-9.5" stroke="#008080"/> + <path d="m16,-3.5 v-9.5 h118 v9.5" stroke="#008000" fill="none"/> + <path d="m136,-3.5 v-9.5 h343 v9.5" stroke="#800000" fill="none"/> + + <text y="-18"> + <tspan x="2">sign</tspan> + <tspan x="75">exponent (8 bits)</tspan> + <tspan x="307">fraction (23 bits)</tspan> + </text> + + <g transform="translate(0,30.5)" fill="#000"> + <circle r="2" cx="7.5"/> + <circle r="2" cx="22.5"/> + <circle r="2" cx="127.5"/> + <circle r="2" cx="142.5"/> + <circle r="2" cx="472.5"/> + </g> + + <text y="45.5"> + <tspan x="6">31</tspan> + <tspan x="27">30</tspan> + <tspan x="126">23</tspan> + <tspan x="146">22</tspan> + <tspan x="472.5">0</tspan> + </text> + <text x="243" y="44.5">(bit index)</text> + </g> + + <text x="504" y="48.4" style="text-anchor: start; font-size: 18.7px;">= 0.15625</text> +</svg> \ No newline at end of file diff --git a/slides/figs/Float_example_bare.svg b/slides/figs/Float_example_bare.svg new file mode 100644 index 0000000..c74df6c --- /dev/null +++ b/slides/figs/Float_example_bare.svg @@ -0,0 +1,430 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> +<svg + xmlns:dc="http://purl.org/dc/elements/1.1/" + xmlns:cc="http://creativecommons.org/ns#" + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" + xmlns:svg="http://www.w3.org/2000/svg" + xmlns="http://www.w3.org/2000/svg" + xmlns:xlink="http://www.w3.org/1999/xlink" + xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" + xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" + width="491.4209" + height="75.638672" + version="1.1" + id="svg134" + sodipodi:docname="Float_example_bare.svg" + inkscape:version="0.92.5 (2060ec1f9f, 2020-04-08)"> + <metadata + id="metadata138"> + <rdf:RDF> + <cc:Work + rdf:about=""> + <dc:format>image/svg+xml</dc:format> + <dc:type + rdf:resource="http://purl.org/dc/dcmitype/StillImage" /> + <dc:title></dc:title> + </cc:Work> + </rdf:RDF> + </metadata> + <sodipodi:namedview + pagecolor="#ffffff" + bordercolor="#666666" + borderopacity="1" + objecttolerance="10" + gridtolerance="10" + guidetolerance="10" + inkscape:pageopacity="0" + inkscape:pageshadow="2" + inkscape:window-width="1884" + inkscape:window-height="1052" + id="namedview136" + showgrid="false" + fit-margin-top="0" + fit-margin-left="0" + fit-margin-right="0" + fit-margin-bottom="0" + inkscape:zoom="1.2915254" + inkscape:cx="478.17825" + inkscape:cy="-102.24671" + inkscape:window-x="36" + inkscape:window-y="0" + inkscape:window-maximized="1" + inkscape:current-layer="svg134" /> + <style + id="style2"> + text { + font-size: 14px; + font-family: Arial, sans-serif; + text-anchor: middle; + } + </style> + <defs + id="defs14"> + <g + id="bit-0"> + <rect + width="15" + height="26" + id="rect4" + x="0" + y="0" + style="stroke:#000000" /> + <text + x="8" + y="18.299999" + id="text6" + style="font-size:14px;font-family:Arial, sans-serif;text-anchor:middle;fill:#000000">0</text> + </g> + <g + id="bit-1"> + <rect + width="15" + height="26" + id="rect9" + x="0" + y="0" + style="stroke:#000000" /> + <text + x="8" + y="18.299999" + id="text11" + style="font-size:14px;font-family:Arial, sans-serif;text-anchor:middle;fill:#000000">1</text> + </g> + </defs> + <g + transform="translate(10.420898,28.192383)" + id="g130"> + <use + xlink:href="#bit-0" + id="use16" + style="fill:#c5fcff" + x="0" + y="0" + width="100%" + height="100%" /> + <g + id="g34" + style="fill:#9fffad"> + <use + xlink:href="#bit-0" + x="15" + id="use18" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-1" + x="30" + id="use20" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-1" + x="45" + id="use22" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-1" + x="60" + id="use24" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-1" + x="75" + id="use26" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-1" + x="90" + id="use28" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-0" + x="105" + id="use30" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-0" + x="120" + id="use32" + y="0" + width="100%" + height="100%" /> + </g> + <g + id="g82" + style="fill:#ffaead"> + <use + xlink:href="#bit-0" + x="135" + id="use36" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-1" + x="150" + id="use38" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-0" + x="165" + id="use40" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-0" + x="180" + id="use42" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-0" + x="195" + id="use44" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-0" + x="210" + id="use46" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-0" + x="225" + id="use48" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-0" + x="240" + id="use50" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-0" + x="255" + id="use52" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-0" + x="270" + id="use54" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-0" + x="285" + id="use56" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-0" + x="300" + id="use58" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-0" + x="315" + id="use60" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-0" + x="330" + id="use62" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-0" + x="345" + id="use64" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-0" + x="360" + id="use66" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-0" + x="375" + id="use68" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-0" + x="390" + id="use70" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-0" + x="405" + id="use72" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-0" + x="420" + id="use74" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-0" + x="435" + id="use76" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-0" + x="450" + id="use78" + y="0" + width="100%" + height="100%" /> + <use + xlink:href="#bit-0" + x="465" + id="use80" + y="0" + width="100%" + height="100%" /> + </g> + <g + id="g88" + style="fill:none;stroke:#000000;stroke-width:2"> + <rect + width="480" + height="26" + id="rect84" + x="0" + y="0" /> + <path + d="M 15,0 V 26 M 135,0 v 26" + id="path86" + inkscape:connector-curvature="0" + style="fill:none;stroke:#000000" /> + </g> + <path + d="M 7.5,-3.5 V -13" + id="path90" + inkscape:connector-curvature="0" + style="stroke:#008080" /> + <path + d="M 16,-3.5 V -13 h 118 v 9.5" + id="path92" + inkscape:connector-curvature="0" + style="fill:none;stroke:#008000" /> + <path + d="M 136,-3.5 V -13 h 343 v 9.5" + id="path94" + inkscape:connector-curvature="0" + style="fill:none;stroke:#800000" /> + <text + y="-18" + id="text102" + style="font-size:14px;font-family:Arial, sans-serif;text-anchor:middle"> + <tspan + x="2" + id="tspan96">sign</tspan> + <tspan + x="75" + id="tspan98">exponent (8 bits)</tspan> + <tspan + x="307" + id="tspan100">fraction (23 bits)</tspan> + </text> + <g + transform="translate(0,30.5)" + id="g114" + style="fill:#000000"> + <circle + r="2" + cx="7.5" + id="circle104" + cy="0" /> + <circle + r="2" + cx="22.5" + id="circle106" + cy="0" /> + <circle + r="2" + cx="127.5" + id="circle108" + cy="0" /> + <circle + r="2" + cx="142.5" + id="circle110" + cy="0" /> + <circle + r="2" + cx="472.5" + id="circle112" + cy="0" /> + </g> + <text + y="45.5" + id="text126" + style="font-size:14px;font-family:Arial, sans-serif;text-anchor:middle"> + <tspan + x="6" + id="tspan116">31</tspan> + <tspan + x="27" + id="tspan118">30</tspan> + <tspan + x="126" + id="tspan120">23</tspan> + <tspan + x="146" + id="tspan122">22</tspan> + <tspan + x="472.5" + id="tspan124">0</tspan> + </text> + <text + x="243" + y="44.5" + id="text128" + style="font-size:14px;font-family:Arial, sans-serif;text-anchor:middle">(bit index)</text> + </g> +</svg> -- GitLab