Derniers messages sur Zeste de Savoirhttps://zestedesavoir.com/forums/2020-09-06T16:23:45+02:00Les derniers messages parus sur le forum de Zeste de Savoir.Python définir une fonction logarithme, message #2256752020-09-06T16:23:45+02:00adri1/@adri1https://zestedesavoir.com/forums/sujet/14511/python-definir-une-fonction-logarithme/?page=2#p225675<p>D’ailleurs, puisque ça peut intéresser d’autres lecteurs, si je devais écrire une fonction responsable de plotter d’autres fonctions, je passerais les fonctions en paramètre.</p>
<div class="hljs-code-div hljs-code-python"><div class="hljs-line-numbers"><span data-count="1"></span><span data-count="2"></span><span data-count="3"></span><span data-count="4"></span><span data-count="5"></span><span data-count="6"></span><span data-count="7"></span><span data-count="8"></span><span data-count="9"></span><span data-count="10"></span><span data-count="11"></span><span data-count="12"></span></div><pre><code class="hljs language-python"><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">representation</span>(<span class="hljs-params">*funcs, domain</span>):</span>
<span class="hljs-keyword">for</span> f <span class="hljs-keyword">in</span> funcs:
plt.plot(domain, f(domain))
representation(
<span class="hljs-keyword">lambda</span> x: np.log(<span class="hljs-number">1</span>+x),
<span class="hljs-keyword">lambda</span> x: x - x**<span class="hljs-number">2</span> / <span class="hljs-number">2</span>,
<span class="hljs-keyword">lambda</span> x: x,
domain=np.linspace(<span class="hljs-number">0</span>, <span class="hljs-number">10</span>, <span class="hljs-number">100</span>))
</code></pre></div>
<p>La syntaxe <code>def fonction(*args)</code> permet de dire que fonction prend un nombre quelconque d’arguments et qu’ils doivent être groupés dans la liste <code>args</code>. La syntaxe <code>lambda x: np.log(1+x)</code> permet de définir une fonction dite <em>anonyme</em> parce qu’on n’a pas besoin de la nommer. Le code que j’ai écrit fonctionne de la même façon que</p>
<div class="hljs-code-div hljs-code-python"><div class="hljs-line-numbers"><span data-count="1"></span><span data-count="2"></span><span data-count="3"></span><span data-count="4"></span><span data-count="5"></span><span data-count="6"></span><span data-count="7"></span><span data-count="8"></span><span data-count="9"></span><span data-count="10"></span><span data-count="11"></span><span data-count="12"></span><span data-count="13"></span><span data-count="14"></span><span data-count="15"></span><span data-count="16"></span><span data-count="17"></span><span data-count="18"></span></div><pre><code class="hljs language-python"><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">representation</span>(<span class="hljs-params">funcs, domain</span>):</span>
<span class="hljs-keyword">for</span> f <span class="hljs-keyword">in</span> funcs:
plt.plot(domain, f(domain))
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f1</span>(<span class="hljs-params">x</span>):</span>
<span class="hljs-keyword">return</span> np.log(<span class="hljs-number">1</span> + x)
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f2</span>(<span class="hljs-params">x</span>):</span>
<span class="hljs-keyword">return</span> x - x**<span class="hljs-number">2</span> / <span class="hljs-number">2</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f3</span>(<span class="hljs-params">x</span>):</span>
<span class="hljs-keyword">return</span> x
representation([f1, f2, f3],
domain=np.linspace(<span class="hljs-number">0</span>, <span class="hljs-number">10</span>, <span class="hljs-number">100</span>))
</code></pre></div>
<p>sauf qu’on n’a pas besoin de nommer les fonctions ni des les grouper à la main dans une liste.</p>Python définir une fonction logarithme, message #2256732020-09-06T15:37:05+02:00CODAlex32/@CODAlex32https://zestedesavoir.com/forums/sujet/14511/python-definir-une-fonction-logarithme/?page=1#p225673<p>Merci beaucoup adri1</p>Python définir une fonction logarithme, message #2256712020-09-06T15:05:09+02:00adri1/@adri1https://zestedesavoir.com/forums/sujet/14511/python-definir-une-fonction-logarithme/?page=1#p225671<p>Quelque chose comme ça pour se débarrasser des imports implicites :</p>
<div class="hljs-code-div hljs-code-python"><div class="hljs-line-numbers"><span data-count="1"></span><span data-count="2"></span><span data-count="3"></span><span data-count="4"></span><span data-count="5"></span><span data-count="6"></span><span data-count="7"></span><span data-count="8"></span><span data-count="9"></span></div><pre><code class="hljs language-python"><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">representation</span>():</span>
x = np.arange(<span class="hljs-number">0</span>, <span class="hljs-number">10</span>, <span class="hljs-number">0.01</span>)
plt.plot(x, np.log(<span class="hljs-number">1</span> + x))
plt.plot(x, x - x**<span class="hljs-number">2</span> / <span class="hljs-number">2</span>)
plt.plot(x, x)
plt.show()
</code></pre></div>Python définir une fonction logarithme, message #2256702020-09-06T14:55:25+02:00CODAlex32/@CODAlex32https://zestedesavoir.com/forums/sujet/14511/python-definir-une-fonction-logarithme/?page=1#p225670<p>D’accord, comment aurait tu fais donc ? Pour connaître la bonne méthode</p>Python définir une fonction logarithme, message #2256692020-09-06T14:48:31+02:00adri1/@adri1https://zestedesavoir.com/forums/sujet/14511/python-definir-une-fonction-logarithme/?page=1#p225669<p>En fait tu as fait exactement pareil sans le savoir. <code>pylab</code> importe plein de chose, notamment <code>from numpy import *</code> et <code>from matplotlib.pyplot import *</code>. Note que le <code>log</code> que tu utilises est donc celui de <code>numpy</code>, et donc que ton <code>from math import *</code> est inutile. Cette collision des noms est la raison pour laquelle utiliser des <code>from <package> import *</code> est une mauvaise pratique (mais malheureusement sûrement celle qui est enseignée en lycée…) et que <code>pylab</code> est une horreur pour gérer proprement ses dépendances…</p>Python définir une fonction logarithme, message #2256672020-09-06T14:38:12+02:00CODAlex32/@CODAlex32https://zestedesavoir.com/forums/sujet/14511/python-definir-une-fonction-logarithme/?page=1#p225667<p>Au final j’ai fait autrement mais ça fonctionne :</p>
<div class="hljs-code-div hljs-code-py"><div class="hljs-line-numbers"><span data-count="1"></span><span data-count="2"></span><span data-count="3"></span><span data-count="4"></span><span data-count="5"></span><span data-count="6"></span><span data-count="7"></span><span data-count="8"></span><span data-count="9"></span><span data-count="10"></span><span data-count="11"></span></div><pre><code class="hljs language-py"><span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span>*
<span class="hljs-keyword">from</span> pylab <span class="hljs-keyword">import</span>*
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">representation</span>():</span>
x=arange(<span class="hljs-number">0</span>,<span class="hljs-number">10</span>,<span class="hljs-number">0.01</span>)
y_1=log(<span class="hljs-number">1</span>+x)
y_2=x-(x**<span class="hljs-number">2</span>/<span class="hljs-number">2</span>)
y_3=x
plot(x,y_1)
plot(x,y_2)
plot(x,y_3)
show()
</code></pre></div>Python définir une fonction logarithme, message #2256622020-09-06T13:47:14+02:00adri1/@adri1https://zestedesavoir.com/forums/sujet/14511/python-definir-une-fonction-logarithme/?page=1#p225662<p>Attention, <code>import numpy as np</code> n’importe pas le contenu du module dans le contexte courant, donc <code>arange</code> n’est pas accessible directement. Il faut taper <code>np.arange</code> pour y accéder.</p>
<p>Par ailleurs, il te faudra utiliser la fonction <code>np.log</code> pour pouvoir calculer le log élément par élément de ton tableau (et tu n’as alors plus besoin de <code>from math import *</code>).</p>
<blockquote>
<p>Mais je n’ai pas de fonction représentation comme mon professeur me le demande</p>
</blockquote>
<p>Ben il te suffit de mettre le code que tu as écrit dans un bloc <code>def representation():</code>.</p>Python définir une fonction logarithme, message #2256612020-09-06T13:43:41+02:00CODAlex32/@CODAlex32https://zestedesavoir.com/forums/sujet/14511/python-definir-une-fonction-logarithme/?page=1#p225661<p>J’obtiens donc :</p>
<div class="hljs-code-div hljs-code-py"><div class="hljs-line-numbers"><span data-count="1"></span><span data-count="2"></span><span data-count="3"></span><span data-count="4"></span><span data-count="5"></span><span data-count="6"></span><span data-count="7"></span><span data-count="8"></span><span data-count="9"></span><span data-count="10"></span><span data-count="11"></span></div><pre><code class="hljs language-py"><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span>*
<span class="hljs-keyword">from</span> matplotlib.pyplot <span class="hljs-keyword">import</span>*
x=arange(<span class="hljs-number">0</span>,<span class="hljs-number">10</span>,<span class="hljs-number">0.01</span>)
y_1=log(<span class="hljs-number">1</span>+x)
y_2=x-x²/<span class="hljs-number">2</span>
y_3=x
plot(x,y_1)
plot(x,y_2)
plot(x,y_3)
show()
</code></pre></div>
<p>Mais je n’ai pas de fonction <em>représentation</em> comme mon professeur me le demande</p>Python définir une fonction logarithme, message #2256602020-09-06T13:26:58+02:00adri1/@adri1https://zestedesavoir.com/forums/sujet/14511/python-definir-une-fonction-logarithme/?page=1#p225660<blockquote>
<p>’étais en train de modifier mon message</p>
</blockquote>
<p>Prends le temps d’écrire tes messages, il n’y a pas le feu. <img src="/static/smileys/svg/smile.svg" alt=":)" class="smiley"> C’est plus simple si tu postes directement un message "fini" plutôt que si on se mélange entre les réponses et les éditions.</p>
<p>Ton code est pas mal, mais <code>arange</code> est défini dans <code>numpy</code> qu’il te faut donc importer.</p>
<p>Par ailleurs, tu définis <code>x</code> comme <span class="math math-inline"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">[</mo><mo>−</mo><mn>10</mn><mo separator="true">,</mo><mn>10</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[-10,10]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">−</span><span class="mord">1</span><span class="mord">0</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">1</span><span class="mord">0</span><span class="mclose">]</span></span></span></span></span> alors qu’on te donne un ensemble de définition de <span class="math math-inline"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">[</mo><mn>0</mn><mo separator="true">,</mo><mo>+</mo><mi mathvariant="normal">∞</mi><mo stretchy="false">[</mo></mrow><annotation encoding="application/x-tex">[0,+\infty[</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">0</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">+</span><span class="mord">∞</span><span class="mopen">[</span></span></span></span></span>. Bien évidemment, tu ne peux pas faire un graphique qui va jusqu’à l’infini, donc il va falloir une borne supérieure plus raisonable (et tu n’as pas besoin d’aller très haut de toute façon, 10 par exemple convient vu que le comportement intéressant de ces fonctions se passe relativement près de <span class="math math-inline"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0</mn></mrow><annotation encoding="application/x-tex">0</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">0</span></span></span></span></span>).</p>Python définir une fonction logarithme, message #2256592020-09-06T13:20:33+02:00CODAlex32/@CODAlex32https://zestedesavoir.com/forums/sujet/14511/python-definir-une-fonction-logarithme/?page=1#p225659<p>J’étais en train de modifier mon message, j’ai fait ça :</p>
<div class="hljs-code-div hljs-code-py"><div class="hljs-line-numbers"><span data-count="1"></span><span data-count="2"></span><span data-count="3"></span><span data-count="4"></span><span data-count="5"></span><span data-count="6"></span><span data-count="7"></span><span data-count="8"></span><span data-count="9"></span><span data-count="10"></span></div><pre><code class="hljs language-py"><span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span>*
<span class="hljs-keyword">from</span> matplotlib.pyplot <span class="hljs-keyword">import</span>*
x=arange(<span class="hljs-number">-10</span>,<span class="hljs-number">10</span>,<span class="hljs-number">0.01</span>)
y_1=log(<span class="hljs-number">1</span>+x)
y_2=x-x²/<span class="hljs-number">2</span>
y_3=x
plot(x,y_1)
plot(x,y_2)
plot(x,y_3)
show()
</code></pre></div>Python définir une fonction logarithme, message #2256572020-09-06T13:15:07+02:00adri1/@adri1https://zestedesavoir.com/forums/sujet/14511/python-definir-une-fonction-logarithme/?page=1#p225657<blockquote>
<p>Notre professeur nous demande de construire une fonction représentation qui ne prend pas d’argument (pas de 'x’), mais qui affiche les courbes associées aux fonction f(x)=ln(1+x), g(x)=x-x²/2 et h(x)=x sur l’ensemble de définition qui est [0;+infini[.</p>
</blockquote>
<p>Même questions que dans la première réponse. Qu’est-ce que tu as essayé et qu’est-ce qui te bloque ?</p>Python définir une fonction logarithme, message #2256562020-09-06T13:08:44+02:00CODAlex32/@CODAlex32https://zestedesavoir.com/forums/sujet/14511/python-definir-une-fonction-logarithme/?page=1#p225656<p>Oui c’est ce qu’on nous demande, enfin c’est le début.</p>
<p>J’avais une autre question à poser d’ailleurs. Notre professeur nous demande de construire une fonction <em>représentation</em> qui ne prend pas d’argument (pas de 'x’), mais qui affiche les courbes associées aux fonction f(x)=ln(1+x), g(x)=x-x²/2 et h(x)=x sur l’ensemble de définition qui est [0;+infini[.</p>
<p>J’ai commencé à faire ça:</p>
<div class="hljs-code-div hljs-code-py"><div class="hljs-line-numbers"><span data-count="1"></span><span data-count="2"></span><span data-count="3"></span><span data-count="4"></span><span data-count="5"></span><span data-count="6"></span><span data-count="7"></span><span data-count="8"></span><span data-count="9"></span></div><pre><code class="hljs language-py"><span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span>*
<span class="hljs-keyword">from</span> matplotlib.pyplot <span class="hljs-keyword">import</span>*
x=arange(<span class="hljs-number">-10</span>,<span class="hljs-number">10</span>,<span class="hljs-number">0.01</span>)
y_1=log(<span class="hljs-number">1</span>+x)
y_2=x-x²/<span class="hljs-number">2</span>
y_3=x
plot(x,y_1)
plot(x,y_2)
plot(x,y_3)
</code></pre></div>Python définir une fonction logarithme, message #2256552020-09-06T13:02:49+02:00adri1/@adri1https://zestedesavoir.com/forums/sujet/14511/python-definir-une-fonction-logarithme/?page=1#p225655<p>Dans mon commentaire, je parlais du <code>print(log(x+1))</code> après le return, qui ne pouvait jamais être atteint puisque <code>return</code> te fait sortir de la fonction.</p>
<p>Par ailleurs, je suppose que tu exécutes ton script dans un IDE qui te permet d’appeler <code>f</code> par la suite, mais le programme que tu as posté ici ne fait rien à part <em>définir</em> une fonction. Si c’est ce qu’on vous demande, c’est très bien, mais c’est un peu étrange…</p>Python définir une fonction logarithme, message #2256542020-09-06T12:58:31+02:00CODAlex32/@CODAlex32https://zestedesavoir.com/forums/sujet/14511/python-definir-une-fonction-logarithme/?page=1#p225654<p>Bonjour adr1,</p>
<p>Merci j’avais oublié qu’il fallait mettre # pour les commentaires</p>
<p>Mais je viens de tester le programme suivant :</p>
<div class="hljs-code-div hljs-code-py"><div class="hljs-line-numbers"><span data-count="1"></span><span data-count="2"></span><span data-count="3"></span><span data-count="4"></span><span data-count="5"></span><span data-count="6"></span></div><pre><code class="hljs language-py"><span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span>*
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span>(<span class="hljs-params">x</span>):</span>
<span class="hljs-keyword">if</span> x><span class="hljs-number">-1</span>:
<span class="hljs-keyword">return</span> log(<span class="hljs-number">1</span>+x)
<span class="hljs-keyword">else</span>:
print(<span class="hljs-string">"x n'appartient pas à I"</span>)
</code></pre></div>
<p>Et ce programme fonctionne très bien</p>Python définir une fonction logarithme, message #2256532020-09-06T12:39:11+02:00adri1/@adri1https://zestedesavoir.com/forums/sujet/14511/python-definir-une-fonction-logarithme/?page=1#p225653<p>Salut,</p>
<p>La condition <code>x > -1</code> est bonne. En revanche, ton code ne vas pas s’exécuter en l’état. Les commentaires se marquent avec <code>#</code>. Du texte entre guillemets est une chaîne, donc la ligne <code>x=50 "par exemple"</code> n’est pas valide en Python. EDIT : tu l’as enlevé entre temps.</p>
<p>Ensuite, le <code>print</code> après le <code>return</code> ne sera jamais exécuté, vois-tu pourquoi ?</p>
<p>Enfin, le code que tu as écrit se contente de définir une fonction, mais ne l’appelle pas. Donc même après avoir corrigé les points que je soulève, il ne se passera rien lorsque tu vas exécuter ton script.</p>Python définir une fonction logarithme, message #2256512020-09-06T12:32:49+02:00CODAlex32/@CODAlex32https://zestedesavoir.com/forums/sujet/14511/python-definir-une-fonction-logarithme/?page=1#p225651<p>Cela donnerai donc :</p>
<div class="hljs-code-div hljs-code-py"><div class="hljs-line-numbers"><span data-count="1"></span><span data-count="2"></span><span data-count="3"></span><span data-count="4"></span><span data-count="5"></span><span data-count="6"></span><span data-count="7"></span></div><pre><code class="hljs language-py"><span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span>*
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span>(<span class="hljs-params">x</span>):</span>
<span class="hljs-keyword">if</span> x><span class="hljs-number">-1</span>:
<span class="hljs-keyword">return</span> log(<span class="hljs-number">1</span>+x)
print(log(<span class="hljs-number">1</span>+x))
<span class="hljs-keyword">else</span>:
print(<span class="hljs-string">"x n'appartient pas à I"</span>)
</code></pre></div>
<p>Et ça fonctionne!!</p>
<p>Merci beaucoup Aabu</p>Python définir une fonction logarithme, message #2256502020-09-06T12:17:08+02:00Aabu/@Aabuhttps://zestedesavoir.com/forums/sujet/14511/python-definir-une-fonction-logarithme/?page=1#p225650<p>Justement, <strong>tu ne peux pas</strong> écrire une liste qui décrit un intervalle. Par contre, tu peux traduire la définition de l’intervalle en terme de comparaisons.</p>
<p>Par exemple mathématiquement :</p>
<p><span class="math math-inline math-display"><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>e</mi><mo>∈</mo><mo stretchy="false">]</mo><mi>a</mi><mo separator="true">,</mo><mi>b</mi><mo stretchy="false">[</mo><mo>⇔</mo><mi>a</mi><mo><</mo><mi>e</mi><mtext> </mtext><mrow><mi mathvariant="normal">e</mi><mi mathvariant="normal">t</mi></mrow><mtext> </mtext><mi>e</mi><mo><</mo><mi>b</mi></mrow><annotation encoding="application/x-tex">e \in ]a, b[ \Leftrightarrow a < e~\mathrm{et}~e < b</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.5782em;vertical-align:-0.0391em;"></span><span class="mord mathdefault">e</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">∈</span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mclose">]</span><span class="mord mathdefault">a</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord mathdefault">b</span><span class="mopen">[</span><span class="mrel">⇔</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.5782em;vertical-align:-0.0391em;"></span><span class="mord mathdefault">a</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"><</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.65418em;vertical-align:-0.0391em;"></span><span class="mord mathdefault">e</span><span class="mspace nobreak"> </span><span class="mord"><span class="mord mathrm">e</span><span class="mord mathrm">t</span></span><span class="mspace nobreak"> </span><span class="mord mathdefault">e</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"><</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathdefault">b</span></span></span></span></span></span></p>
<p>La partie droite se traduit très facilement en Python ; je te laisse quand même adapter à ton cas à toi.</p>Python définir une fonction logarithme, message #2256492020-09-06T11:58:14+02:00CODAlex32/@CODAlex32https://zestedesavoir.com/forums/sujet/14511/python-definir-une-fonction-logarithme/?page=1#p225649<p>Merci de ta réponse Aabu, mais comment je dois créer une liste qui décrit l’intervalle I=]-1;+infini[ où -1 est exclu ? Car si je tape : <code>I=[-1,10^9[</code> (je prends 10^9 car je ne peux pas écrire inf) -1 n’est pas exclu de mon intervalle.</p>Python définir une fonction logarithme, message #2256472020-09-06T11:05:16+02:00Aabu/@Aabuhttps://zestedesavoir.com/forums/sujet/14511/python-definir-une-fonction-logarithme/?page=1#p225647<p>D’accord, je comprends ton problème. Tu es confus sur la notation des listes en Python et la notation mathématiques des intervalles.</p>
<p>En Python, la notation <code>[a, b, ...]</code> sert à créer une <em>liste</em>. Une liste n’est rien d’autre qu’une séquence d’éléments. Un élément appartient à la liste s’il y apparaît au moins une fois, ce que teste l’opérateur <code>in</code>. Tu ne peux pas représenter un intervalle de réels avec une liste, parce qu’un intervalle contient une infinité (indénombrable qui plus est) de valeurs.</p>
<p>En mathématiques, on note <span class="math math-inline"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">[</mo><mi>a</mi><mo separator="true">,</mo><mi>b</mi><mo stretchy="false">[</mo></mrow><annotation encoding="application/x-tex">[a, b[</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord mathdefault">a</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord mathdefault">b</span><span class="mopen">[</span></span></span></span></span> tous les réels compris entre a (inclus) et b (exclus). Il n’y a pas de notation spéciale en Python pour noter un intervalle, et il n’y a pas non plus d’opérateur pour l’appartenance. </p>
<p>Cela veut dire que pour savoir si un nombre est dans un intervalle en Python (dont on connaît les bornes a et b), il faut tester qu’il est plus grand ou égal à a et plus petit strictement que b. On peut le faire à l’aide des opérateurs de comparaison.</p>
<p>À noter que <code>inf</code> existe en Python, mais il n’est pas prévu de s’en servir dans ces cas normaux (et donc tu ne peux pas juste écrire <code>inf</code>).</p>Python définir une fonction logarithme, message #2256442020-09-06T10:30:55+02:00CODAlex32/@CODAlex32https://zestedesavoir.com/forums/sujet/14511/python-definir-une-fonction-logarithme/?page=1#p225644<p>Bonjour Aabu,</p>
<p>Ce qui me bloque le plus c’est la liste, sinon j’ai déjà avancé comme je pouvais :</p>
<div class="hljs-code-div hljs-code-py"><div class="hljs-line-numbers"><span data-count="1"></span><span data-count="2"></span><span data-count="3"></span><span data-count="4"></span><span data-count="5"></span><span data-count="6"></span><span data-count="7"></span><span data-count="8"></span><span data-count="9"></span></div><pre><code class="hljs language-py"><span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span>*
x=<span class="hljs-number">50</span> <span class="hljs-string">"par exemple"</span>
I=[<span class="hljs-number">-1</span>,inf] <span class="hljs-string">"sauf que -1 doit être exclu et je ne crois pas que inf existe sur Python"</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span>(<span class="hljs-params">x</span>):</span>
<span class="hljs-keyword">if</span> x <span class="hljs-keyword">in</span> I:
<span class="hljs-keyword">return</span> log(<span class="hljs-number">1</span>+x)
print(log(<span class="hljs-number">1</span>+x))
<span class="hljs-keyword">else</span>:
print(<span class="hljs-string">"x n'appartient pas à I"</span>)
</code></pre></div>Python définir une fonction logarithme, message #2256392020-09-06T09:58:41+02:00Aabu/@Aabuhttps://zestedesavoir.com/forums/sujet/14511/python-definir-une-fonction-logarithme/?page=1#p225639<p>Bonjour,</p>
<p>Qu’as-tu déjà tenté ? Qu’est-ce qui te bloque exactement ? </p>
<p>Connais-tu la fonction logarithme fournit de base par Python ? Est-ce que tu sais définir une fonction en Python ?</p>