[metapost] Function defined by a sum

Nicolas FRANCOIS nicolas.francois at free.fr
Fri Nov 2 12:46:11 CET 2007


Le Fri, 2 Nov 2007 11:21:19 +0100 "Mojca Miklavec"
<mojca.miklavec.lists at gmail.com> a écrit :

> There is no "sum" (unless you define one), but oyu can use "for" loop:
> 
> vardef sin(expr xx) = sind(xx*180/3.14) enddef;
> 
> vardef f(expr xx,n) =
>    save sum;
>    sum:=0;
>    for i=1 upto n:
>       sum := sum+sin(i*(xx))/i;
>    endfor;
> 
>    sum
> enddef;
> 
> beginfig(1);
>    path p;
>    p := (0,0)
>    for j=1 upto 700:
>       --(j/100,f(j/100,3))
>    endfor;
>    draw p scaled 1cm;
> endfig;
> end.

Thanks. Someone on another list gave me something more "TeXish" :

vardef sum_sin(expr x,n)=
  0
  for k:=1 upto n:
    + sin(k*x)/x
    endfor
enddef;

I thought something like this could work. But I don't know exactly why.
Is this something like "token replacement", like in TeX ? Where can this
be explained in the Metapost documentation ?

Thanks anyway.

\bye

-- 

                   Nicolas FRANCOIS
            http://nicolas.francois.free.fr
 A TRUE Klingon programmer does NOT comment his code


More information about the metapost mailing list