2 Nov 2007 11:21

Re: Function defined by a sum

```On 10/31/07, Nicolas FRANCOIS wrote:
> Hi.
>
> I'd like to show the Gibbs phenomenon on an example. For this I'd like to
> draw the partial sums of the Fourier serie :
>
>   sum(sin(n*x)/n,n=1..infty)

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;
```

2 Nov 2007 12:46

Re: Function defined by a sum

```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)=
```

2 Nov 2007 13:16

Re: Function defined by a sum

```On 11/2/07, Nicolas FRANCOIS wrote:
> 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" :
>
```

2 Nov 2007 13:59

Re: Function defined by a sum

```Nicolas FRANCOIS wrote:

> 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 ?

well, definitely not texish because tex has no 'functions' like mp

the for loop is fully expandable and it makes up the final value which
is returned because it's a vardef

Hans

-----------------------------------------------------------------
Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com
| www.pragma-pod.nl
-----------------------------------------------------------------
--
http://tug.org/mailman/listinfo/metapost
```

2 Nov 2007 14:33

Re: Function defined by a sum

```Le Fri, 2 Nov 2007 13:16:26 +0100 "Mojca Miklavec"
<mojca.miklavec.lists <at> gmail.com> a écrit :

> On 11/2/07, Nicolas FRANCOIS wrote:
> > Le Fri, 2 Nov 2007 11:21:19 +0100 "Mojca Miklavec"
> > <mojca.miklavec.lists <at> gmail.com> a écrit :
> >
> > 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;

Well, except it is "    + sin(k*x)/k"

> Sure, that's a more elegant solution. It gets expanded directly into
>     return 0+sin(1*x)/1+sin(2*x)/2+...+sin(n*x)/n (whatever n is)
> while my example was doing
>     sum:=0;
>     sum:=sum+sin(1*x)/1;
>     sum:=sum+sin(2*x)/2;
>     ...
>     return sum
>
> > 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 ?
```

4 Nov 2007 21:39

Re: Function defined by a sum

```At 05:46 AM 11/2/2007, you wrote:
>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.

OR:
beginfig(1);
```

4 Nov 2007 21:40

pens and number of points

```Hi,

please excuse my ignorance. Why does MetaPost (1.000) need 18(!) points
for drawing a square with a squared pen, while it only needs (the
expected) four in case of a circled pen? Is this PS related?

path p;

p:= unitsquare xyscaled(90,90);
draw p withpen
pensquare scaled 10; % 18 points (see ps output)
%    pencircle scaled 10; % 4 points + pen defs(?)

And is there a better way than using fill in this case (8 points)?

p:= unitsquare xyscaled(100,100);
fill p;
p:= unitsquare xyscaled(80,80) shifted(10,10);
unfill p;

Any help welcome.

Peter
--
http://tug.org/mailman/listinfo/metapost

```
5 Nov 2007 09:19

Re: pens and number of points

```Peter Rolf wrote:
> Hi,
>
> please excuse my ignorance. Why does MetaPost (1.000) need 18(!) points
> for drawing a square with a squared pen, while it only needs (the
> expected) four in case of a circled pen? Is this PS related?

The suprising thing is that it needs only 4 for the pencircle.

There is special optimization code in metapost because manipulating
elliptical pens is supported by the PS interpreter itself. That is
not possible for other types of pens.

As seen in your second example, it is possible in PS to draw this
particular case with only 8 points, but that is only true because
all the pen sides are modulo 90 to the path sides. From a programming
viewpoint, that is pretty unlikely (and in general, hard to test).

Best wishes,
Taco
--
http://tug.org/mailman/listinfo/metapost

```
5 Nov 2007 14:36

Re: pens and number of points

```Taco Hoekwater schrieb:
> Peter Rolf wrote:
>> Hi,
>>
>> please excuse my ignorance. Why does MetaPost (1.000) need 18(!) points
>> for drawing a square with a squared pen, while it only needs (the
>> expected) four in case of a circled pen? Is this PS related?
>
> The suprising thing is that it needs only 4 for the pencircle.
>
not from my (naive) point of view ;)

> There is special optimization code in metapost because manipulating
> elliptical pens is supported by the PS interpreter itself. That is
> not possible for other types of pens.
>
ok

> As seen in your second example, it is possible in PS to draw this
> particular case with only 8 points, but that is only true because
> all the pen sides are modulo 90 to the path sides. From a programming
> viewpoint, that is pretty unlikely (and in general, hard to test).
>
I see that there is no general solution for this problem (pen can be any
closed path). In this special test case using *pencircle* in combination
with

interim linejoin:= mitered;
interim lincut:= squared;

```

5 Nov 2007 15:20

Re: pens and number of points

```Peter Rolf schrieb:
> Taco Hoekwater schrieb:
>> Peter Rolf wrote:
>>> Hi,
>>>
>>> please excuse my ignorance. Why does MetaPost (1.000) need 18(!) points
>>> for drawing a square with a squared pen, while it only needs (the
>>> expected) four in case of a circled pen? Is this PS related?
>> The suprising thing is that it needs only 4 for the pencircle.
>>
> not from my (naive) point of view ;)
>
>> There is special optimization code in metapost because manipulating
>> elliptical pens is supported by the PS interpreter itself. That is
>> not possible for other types of pens.
>>
> ok
>
>> As seen in your second example, it is possible in PS to draw this
>> particular case with only 8 points, but that is only true because
>> all the pen sides are modulo 90 to the path sides. From a programming
>> viewpoint, that is pretty unlikely (and in general, hard to test).
>>
> I see that there is no general solution for this problem (pen can be any
> closed path). In this special test case using *pencircle* in combination
> with
>
> interim linejoin:= mitered;
> interim lincut:= squared;
linecap
```