{"id":122,"date":"2022-03-17T18:44:41","date_gmt":"2022-03-17T17:44:41","guid":{"rendered":"https:\/\/blog.univ-angers.fr\/mathsinfo\/?page_id=122"},"modified":"2023-06-01T12:29:10","modified_gmt":"2023-06-01T11:29:10","slug":"apl-et-rpl-scan","status":"publish","type":"page","link":"https:\/\/blog.univ-angers.fr\/mathsinfo\/apl-et-rpl-scan\/","title":{"rendered":"et RPL : Scan"},"content":{"rendered":"<p>En APL il existe l&rsquo;op\u00e9rateur&nbsp; \\ (appel\u00e9 <strong>scan<\/strong> ou <strong>balayage&nbsp;<\/strong>ou <strong>r\u00e9duction partielle<\/strong>) qui fait penser \u00e0 l&rsquo;op\u00e9rateur \/ de r\u00e9duction. Voici la diff\u00e9rence :<\/p>\n<pre>f\/ 1 2 3 \u2026 = 1 f 2 f 3 \u2026\nf\\ 1 2 3 \u2026 = (f\/1) (f\/1 2) (f\/1 2 3) \u2026<\/pre>\n<p>Le n-ieme r\u00e9sultat est \u00e9gal \u00e0 la r\u00e9duction des n premiers \u00e9l\u00e9ments. L&rsquo;exemple de base est la <strong>somme cumul\u00e9e<\/strong>&nbsp;des \u00e9l\u00e9ments d&rsquo;un vecteur :<\/p>\n<pre>+\\ 2 3 4 8 10\n2 5 9 17 27<\/pre>\n<p>On peut imaginer une version en RPL utilisant SEQ pour cr\u00e9er ces suites ou rester avec notre trio DOLIST, DOSUBS et STREAM. En effet, si on reprend la r\u00e9duction :<\/p>\n<pre>1: { 2 3 4 8 10 }\n\u00ab + \u00bb\n<strong>PRG<\/strong> LIST PROC STREAM\n1: 27<\/pre>\n<p>Mais si l&rsquo;on ajoute&nbsp;<strong>OVER<\/strong> pour dupliquer \u00e0 chaque fois le r\u00e9sultat pr\u00e9c\u00e9dent :<\/p>\n<pre>1: { 2 3 4 8 10 }\n\u00ab OVER + \u00bb\n<strong>PRG<\/strong> LIST PROC STREAM\n5: 2\n4: 5\n3: 9\n2: 17\n1: 27<\/pre>\n<p>On obtient les bonnes valeurs mais elles seront sur la pile. Pour comprendre le principe, tapez sur votre machine :<br>\n<strong>2 ENTER 3 OVER + 4 OVER + 8 OVER + 10 OVER +<\/strong><\/p>\n<p>Reste \u00e0 la transformer en liste { 2 5 9 17 27}. On peut&nbsp; imaginer cette version pour le cas d&rsquo;une somme cumul\u00e9e :<\/p>\n<pre>\u00ab \u2192 v\n \u00ab v \u00ab OVER + \u00bb STREAM v SIZE \u2192LIST \u00bb\n\u00bb\n'SCUM <strong>STO<\/strong>\n\n1: { 2 5 3 4}\n<strong>VAR<\/strong> SCUM\n1: {2 7 10 14}<\/pre>\n<p>Ou, de fa\u00e7on plus g\u00e9n\u00e9rale, d\u00e9finir <strong>SCA.<\/strong> (Scan), appel\u00e9 <strong>PRED<\/strong> dans le document d&rsquo;origine de Norman Brenner) :<\/p>\n<pre>\u00ab \u2192 v f\n \u00ab v \u00ab OVER f EVAL \u00bb STREAM v SIZE \u2192LIST \u00bb\n\u00bb\n'SCA. <strong>STO<\/strong><\/pre>\n<p>Exemples :<\/p>\n<pre>1: {1 2 3 4}\n2: \u00ab + \u00bb\n<strong>VAR<\/strong> SCA.\n1: {1 3 6 10}\n\n1: {2 5 4 1 8 3 3}\n2: \u00ab MAX \u00bb\n<strong>VAR<\/strong> SCA.\n1: {2 5 5 5 8 8 8}  # Maximum progressif\n\nEn APL :\n\n\u2308\\ 2 5 4 1 8 3 3\n2 5 5 5 8 8 8<\/pre>\n<p>En particulier, lorsque les vecteurs sont binaires, cela permet d&rsquo;avoir des r\u00e9sultats int\u00e9ressants :<\/p>\n<pre>\u2227\\ 1 1 1 0 1 0 0 1\n   1 1 1 0 0 0 0 0    \u235d = 0 \u00e0 partir du 1er \"0\"\n\u2228\\ 0 0 0 0 1 0 0 1\n   0 0 0 0 1 1 1 1    \u235d = 1 \u00e0 partir du 1er \"1\"\n&lt;\\ 0 0 1 0 1 1 0 0 1\n   0 0 1 0 0 0 0 0 0  \u235d Position du 1er \"1\"<\/pre>\n<p>En RPL :<\/p>\n<pre>2: {1 1 1 0 1 0 0 1}\n1: \u00ab AND \u00bb\n<strong>VAR<\/strong> SCA.\n1: {1 1 1 0 0 0 0 0}\n\n2: {0 0 0 0 1 0 0 1}\n1: \u00ab OR \u00bb\n<strong>VAR<\/strong> SCA.\n1: {0 0 0 0 1 1 1 1}<\/pre>\n<p>Lorsque l&rsquo;op\u00e9rateur f est commutatif (comme OR, AND, +, MAX&#8230;) il n&rsquo;y a pas de diff\u00e9rence entre le RPL et l&rsquo;APL. Par contre, pour des op\u00e9rateurs non commutatifs comme \u00ab\u00a0&lt;\u00ab\u00a0, le r\u00e9sultat sera faux.<\/p>\n<p><span style=\"text-decoration: underline\">APL<\/span> :&nbsp; &nbsp;<code>f\\ a b c = (a) (a f b) (a f (b f c))<\/code><\/p>\n<p><span style=\"text-decoration: underline\">RPL<\/span> :&nbsp; &nbsp;<code>f\\ a b c = (a) (a f b) ((a f b) f c)<\/code><\/p>\n<p>Pour se convaincre :<\/p>\n<pre>2: {0 0 1 0 1 1 0 0 1}\n1: \u00ab &lt; \u00bb\n<strong>VAR<\/strong> SCA.\n1: {0 0 0 0 0 0 0 0 0}\n\nR\u00e9sultat attendu : 0 0 1 0 0 0 0 0 0<\/pre>\n<p><strong>Exercice &#8211; Les immeubles<\/strong><\/p>\n<p>On vous donne une liste de hauteurs d&rsquo;immeubles adjacents et on vous demande combien seront visibles si vous les regardez \u00e0 partir de la gauche. Par exemple, si les hauteurs sont <strong>2 3 5 2 1 6 4<\/strong>, en vert ci-dessous les <strong>4<\/strong> seuls immeubles qui seront visibles (les autres sont cach\u00e9s par des b\u00e2timents plus hauts)<\/p>\n<p><a href=\"https:\/\/blog.univ-angers.fr\/mathsinfo\/files\/2022\/03\/imm1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-127\" alt=\"imm1\" src=\"https:\/\/blog.univ-angers.fr\/mathsinfo\/files\/2022\/03\/imm1.jpg\" width=\"618\" height=\"278\" srcset=\"https:\/\/blog.univ-angers.fr\/mathsinfo\/files\/2022\/03\/imm1.jpg 618w, https:\/\/blog.univ-angers.fr\/mathsinfo\/files\/2022\/03\/imm1-300x134.jpg 300w\" sizes=\"auto, (max-width: 618px) 100vw, 618px\" \/><\/a><\/p>\n<p>On supposera dans un premier temps qu&rsquo;il n&rsquo;y a pas de zones vides entre les immeubles, c&rsquo;est-\u00e0-dire que la liste ne contient pas de 0 (immeubles de hauteur nulle).<\/p>\n<pre>IMM 2 3 5 2 1 6 4\nOutput: 4<\/pre>\n<p>Dans un second temps, consid\u00e9rez le cas g\u00e9n\u00e9ral.<\/p>\n<p><span style=\"text-decoration: underline\">Corrig\u00e9 version 1<\/span> : Commen\u00e7ons par l&rsquo;APL. On va d\u00e9j\u00e0 scanner les immeubles pour r\u00e9cup\u00e9rer les hauteurs maximales atteintes :<\/p>\n<pre>       \u2308\\ 2 3 5 2 1 6 4\nOutput:   2 3 5 5 5 6 6<\/pre>\n<p>Il faut maintenant r\u00e9cup\u00e9rer les hauteurs distinctes :<\/p>\n<pre>       \u222a \u2308\\ 2 3 5 2 1 6 4\nOutput:     2 3 5 6<\/pre>\n<p>Et les compter :<\/p>\n<pre>\u2374 \u222a \u2308\\ 2 3 5 2 1 6 4\nOutput: 4<\/pre>\n<p>Le programme final en APL pour la version 1 :<\/p>\n<pre>IMM \u2190 {\u2374\u222a\u2308\\\u2375}<\/pre>\n<p>Nous pouvons imm\u00e9diatement traduire cela en RPL avec les programmes <strong>SCA.<\/strong> (scan), <strong>UN.<\/strong> (union) et la fonction SIZE :<\/p>\n<pre>\u00ab \u00ab MAX \u00bb SCA. UN. SIZE \u00bb\n'IMM STO<\/pre>\n<p>V\u00e9rifions :<\/p>\n<pre>1: { 2 3 5 2 1 6 4 }\n<strong>VAR<\/strong> IMM\n1: 4.<\/pre>\n<p><span style=\"text-decoration: underline\">Corrig\u00e9 de la version g\u00e9n\u00e9rale<\/span> : Si la liste commence par un ou plusieurs 0, le calcul sera faux :<\/p>\n<pre>1: { 0 2 1 }\n<strong>VAR<\/strong> IMM\n1: 2.<\/pre>\n<p>Ceci parce que les maximums progressifs sont { 0 2 2 } dont la r\u00e9union comporte 2 termes {0 2}. Il faut donc filtrer la liste des maximums pour enlever les 0. Une solution possible en APL :<\/p>\n<pre>IMM \u2190 {\u2374\u222a (0 \u2260 n) \/ n \u2190 \u2308\\\u2375}<\/pre>\n<p>Et nous avions cr\u00e9er en RPL la fonction <strong>CL.<\/strong>&nbsp;pour compresser une liste en utilisant un vecteur logique. D&rsquo;o\u00f9 cette version en RPL :<\/p>\n<pre>\u00ab \u00ab MAX \u00bb SCA. DUP 0 &gt; CL. UN. SIZE \u00bb\n'IMM STO<\/pre>\n<p>Qui signifie prendre les maximums progressifs, dupliquer cette liste, cr\u00e9er le vecteur logique des termes &gt; 0, \u00e9liminer les \u00e9l\u00e9ments qui sont \u00e0 FAUX, faire la r\u00e9union des \u00e9l\u00e9ments restants et les compter.<\/p>\n<pre>1: { 0 2 1 }\nVAR IMM\n1: 1.<\/pre>\n<p><strong>Exemple &#8211; Les fractions continues<\/strong><\/p>\n<p>Pour la&nbsp;<a title=\"Wikipedia\" href=\"https:\/\/fr.wikipedia.org\/wiki\/Fraction_continue\" target=\"_blank\" rel=\"noopener\">d\u00e9finition d&rsquo;une fraction continue<\/a>, voir par exemple Wikipedia.<\/p>\n<p>En APL, on obtient la valeur approch\u00e9e&nbsp;d&rsquo;une liste d&rsquo;\u00e9tages par :<\/p>\n<pre>+\u2218\u00f7\/ 1 2 2 2 2\n1.413793103<\/pre>\n<p>Qui correspond \u00e0 la valeur <strong>1<\/strong> + 1 \/ ( <strong>2<\/strong> + 1 \/ ( <strong>2<\/strong> + 1 \/ ( <strong>2<\/strong> + 1 \/ <strong>2<\/strong> ) ) )<\/p>\n<p>Et en utilisant un scan :<\/p>\n<pre>+\u2218\u00f7\\ 1 2 2 2 2\n1 1.5 1.4 1.416666667 1.413793103<\/pre>\n<p>On obtient les fractions 1, puis 1 + 1 \/ 2 puis<br>\n1 + 1 \/ (2 + 1 \/ 2)&#8230;<\/p>\n<p>En RPL, on peut obtenir la valeur finale par :<\/p>\n<pre>\u00ab REVLIST \u00ab OVER INV + \u00bb STREAM \u2192NUM \u00bb\n'FC <strong>STO<\/strong>\n\n1: { 1 2 2 2 2 }\n<strong>VAR<\/strong> FC\n1: 1.41379310345     # \u2243 racine de 2\n\n1: { 1 1 1 1 1 1 1 1 1 1 }\n<strong>VAR<\/strong> FC\n1: 1.61818181818     # \u2243 nombre d'or<\/pre>\n<p>L&rsquo;op\u00e9ration \u00ab\u00a0Prendre l&rsquo;inverse plus additionner\u00a0\u00bb n&rsquo;\u00e9tant pas commutative, on ne pourra pas utiliser notre fonction <strong>SCA.<\/strong><\/p>\n\n\n<p><a href=\"https:\/\/blog.univ-angers.fr\/mathsinfo\/apl-et-rpl-produit-externe\/\">Lire la suite&#8230;<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>En APL il existe l&rsquo;op\u00e9rateur&nbsp; \\ (appel\u00e9 scan ou balayage&nbsp;ou r\u00e9duction partielle) qui fait penser \u00e0 l&rsquo;op\u00e9rateur \/ de r\u00e9duction. Voici la diff\u00e9rence : f\/ 1 2 3 \u2026 = 1 f 2 f 3 \u2026 f\\ 1 2 3 &hellip; <a href=\"https:\/\/blog.univ-angers.fr\/mathsinfo\/apl-et-rpl-scan\/\">Continuer la lecture <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":4913,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-122","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/pages\/122","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/users\/4913"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/comments?post=122"}],"version-history":[{"count":1,"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/pages\/122\/revisions"}],"predecessor-version":[{"id":1612,"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/pages\/122\/revisions\/1612"}],"wp:attachment":[{"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/media?parent=122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}