<div>Salut!<br>J&#39;ai besoin d&#39;un peu d&#39;aide...<br>Je débute en haskell. J&#39;essai d&#39;écrire un petit programme qui implémente les &quot;polynômes interpolateurs de Lagrange&quot;.<br><br>Mais ma question est plus générale, c&#39;est que j&#39;ai souvent du mal avec le moteur d&#39;inférence de Haskell.
<br>En effet, un coup mon code compile. Ensuite je fait une petite modif sur une ligne et ça ne compile plus en me levant une erreur sur une partie (apparemment) complètement différente du code!!<br>&nbsp;</div>
<div>Dans mon exemple j&#39;ai une variable <em>nombre_points</em>. C&#39;est clairement un entier. Comment le préciser?</div>
<div>Je voudrais aussi l&#39;utiliser dans des divisions. Comment faire?</div>
<div>Je pense que c&#39;est le problème dans mon exemple ci-dessous.</div>
<div>&nbsp;</div>
<div>
<div>Merci pour votre aide!</div>
<div>Corentin</div>
<div>&nbsp;</div>
<div>PS: Y as t-il un forum en Français d&#39;entraide?</div></div>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div>Il me sort une erreur du style: </div>
<div>&nbsp;</div>
<div><em>Lagrange.hs:60:13:<br>&nbsp;&nbsp;&nbsp; No instance for (Fractional Int)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arising from use of `us&#39; at Lagrange.hs:60:13-17<br>&nbsp;&nbsp;&nbsp; Possible fix: add an instance declaration for (Fractional Int)<br>&nbsp;&nbsp;&nbsp; In the second argument of `map&#39;, namely `(us (m))&#39;
<br>&nbsp;&nbsp;&nbsp; In the expression: map by (us (m))<br>&nbsp;&nbsp;&nbsp; In the definition of `ys&#39;: ys = map by (us (m))</em></div>
<div><br>Voici l&#39;exemple:<br><br><em>module Lagrange where<br><br><br>nombre_points = 7<br><br>-- creation d&#39;une liste exluant i<br>list i = [x | x &lt;- [0..nombre_points-1], x /= i]<br><br>-- un terme du polynôme de Lagrange
<br>un_terme t j i = (t - i)/(j - i)<br><br>--produit des termes pour obtenir le polynôme d&#39;un point<br>les_termes t j = map (un_terme t j) (list j)<br>poly t j = product (les_termes t j)<br><br><br><font color="#ff0000">
--Si je décommente les 2 lignes suivantes et que je commente l&#39;autre définition de blend ci-dessous, ça marche:<br></font>--blend (a,t) = a(0) * (poly t 0) + a(1) * (poly t 1) + a(2) * (poly t 2) + a(3) * (poly t 3) +
<br>--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a(4) * (poly t 4) + a(5) * (poly t 5) + a(6) * (poly t 6)<br><br>--t est le paramètre du polynôme, a sera la coordonnée (x ou y). <br>blend_un_point t a numero_point = a(numero_point) * (poly t numero_point)
<br>blend_les_points t a = map (blend_un_point t a) [0..6]<br>blend (a,t) = sum (blend_les_points t a)<br><br>-- Sample points<br>xy = [(-4.0,0.0), (-1.0,1.0), (-3.0,3.0), (0.0,4.0), (3.0,3.0),(1.0,1.0),(4.0,0.0)]<br><br>
<br>--creation des fonctions x et y<br>x a = fst (xy !! a)<br>y a = snd (xy !! a)<br><br><br>-- Blend the sample points for some given u:<br>bx(u) = blend(x,u)<br>by(u) = blend(y,u)<br><br>-- Take m+1 values for u, from 0 to nombre_points, equally spaced:
<br>us(m) = map (/m) [0.0..(7-1)*m]<br><br>-- For<br><br>m = 50.0<br><br>-- we get us(m)=[0.0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0].<br><br>-- Now get a list of points for the above values of the parameter:<br>
<br>xs = map bx (us(m))</em></div><em>
<div><br><font color="#ff0000">ys = map by (us(m))</font><br><br><br>-- Running this, we get, where I&#39;ve rounded the results to 2 digits:<br>--<br>--&nbsp;&nbsp;xs = [0.00, 0.38, 0.75, 1.1, 1.5, 1.9, 2.3, 2.6, 3.0]<br>--&nbsp;&nbsp;ys = [
0.00, 0.46, 1.00, 1.7, 2.3, 2.8, 3.1, 3.2, 3.0]<br><br>-- Finally, get a list of pairs (x,y), i.e. a list of points:<br><br>ps = zip xs ys<br><br>-- In this example, running &quot;ps&quot; we get, after rounding, the points:
<br>--<br>-- [(0, 0), (0.38, 0.46), (0.75, 1), (1.1, 1.7),<br>--&nbsp;&nbsp;(1.5, 2.3), (1.9, 2.8), (2.3, 3.1), (2.6, 3.2), (3, 3)]<br>--<br>-- Now plot lines joining these points to get an approximation of the curve<br></div></em>

<div>&nbsp;</div>
<div><br><br>&nbsp;</div>