From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.comp.tex.context/108105 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Fabrice Couvreur Newsgroups: gmane.comp.tex.context Subject: Re: Code lua in a table Date: Mon, 20 Jul 2020 12:13:18 +0200 Message-ID: References: <31495829-66d7-c670-75e2-d1c538f38e9c@xs4all.nl> Reply-To: mailing list for ConTeXt users Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============3532493619657829189==" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="7460"; mail-complaints-to="usenet@ciao.gmane.io" To: mailing list for ConTeXt users Original-X-From: ntg-context-bounces@ntg.nl Mon Jul 20 12:15:30 2020 Return-path: Envelope-to: gctc-ntg-context-518@m.gmane-mx.org Original-Received: from zapf.boekplan.nl ([5.39.185.232] helo=zapf.ntg.nl) by ciao.gmane.io with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jxSpW-0001pq-6B for gctc-ntg-context-518@m.gmane-mx.org; Mon, 20 Jul 2020 12:15:30 +0200 Original-Received: from localhost (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id 85023185D1A; Mon, 20 Jul 2020 12:15:09 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at zapf.boekplan.nl Original-Received: from zapf.ntg.nl ([127.0.0.1]) by localhost (zapf.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id qIhpTMbskU5z; Mon, 20 Jul 2020 12:15:06 +0200 (CEST) Original-Received: from zapf.ntg.nl (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id C2EFB185D0E; Mon, 20 Jul 2020 12:15:06 +0200 (CEST) Original-Received: from localhost (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id 2D79A185D07 for ; Mon, 20 Jul 2020 12:15:05 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at zapf.boekplan.nl Original-Received: from zapf.ntg.nl ([127.0.0.1]) by localhost (zapf.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ZuQyRGmKT1j7 for ; Mon, 20 Jul 2020 12:15:04 +0200 (CEST) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=209.85.128.51; helo=mail-wm1-f51.google.com; envelope-from=fabrice1.couvreur@gmail.com; receiver= Original-Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by zapf.ntg.nl (Postfix) with ESMTPS id 2618E185CAE for ; Mon, 20 Jul 2020 12:15:04 +0200 (CEST) Original-Received: by mail-wm1-f51.google.com with SMTP id f139so24600189wmf.5 for ; Mon, 20 Jul 2020 03:15:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=sYnGnkACIyWsrkpYV3Dxg0wxlNHz/O6bxwdz5Hoc5AM=; b=M3INNCd9sd8WPwrvhJfRM3fDwuTyj88NXIZSFH/QZdexanjRBUeRO1AjanPqJ4MU2y UpivZxdNgNZXHvq3i/smumz7CQi+zJIdCsTqD2PbG3ASvn19GOzmJpwCDuOtzHcAap0Y mjSHeQEzI3b+2nhIvofd9m4dssOFqJjQc2cYHS13CpsLouO7xt1noGMSpkTfBZ5zcgM1 lNIOhZ8e5P9YXUrmQLL1uOwrkeDWbA9dXVp8KUa+tLrkULSUxTt6qBeYUhI0SaAZKToO aycPz6cNfUu5pI02AlIiycb5M1VYbAvPUn89VggU8hsJN/516hJe1L5jsEyYY53fhPDA WQhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=sYnGnkACIyWsrkpYV3Dxg0wxlNHz/O6bxwdz5Hoc5AM=; b=MiMZkw4MJh2+HfS6MSgYMr9Z4iQF/MOwXlF2ujqFxbj74T2y8sOSokkREmpwvArMYS +sk9E7iJ2QKeZspuqu8DvNQlzfJACqUEl3JnmcnPaYHj/nh8yUxs5Kn4Ye97HFqTjXUZ dWZnk/y2KhzXCd4XPI6atzJFjqSe09pSL8s0F7ZOWx/Tc1YWs5mr9F7URYGuTBngLcZj OsWgYnFO6NLERfl6BRj/5/tWEYNknN42tEkFHFMo7wH1eXMPD+adRKgCy7DtZkDyHMa2 vI3s1kC2fFhz5KwpqilEfwY/yAtSy00FAgWxvwD2g88yiR1G4ZsPWiHZNucUO2sTMF8m B/Vw== X-Gm-Message-State: AOAM531xqZBc/JIG8yU72aZqmml5YF0Pq6EYdDlfnyeLVTP/eSLBhAKi vXjje+gatQE7FbjYYUb6Mi/Iy3tDhwMNuB0K9aibtKOM X-Google-Smtp-Source: ABdhPJzefc94c2sZ1IuZHyhKXS0kfPty2/rIvmqWbY1VDak5Z3AFzv9gihk3YRn4ffRXjcLerqqdkgj5bmKfcVlHLbE= X-Received: by 2002:a7b:ca52:: with SMTP id m18mr20707596wml.92.1595240103013; Mon, 20 Jul 2020 03:15:03 -0700 (PDT) In-Reply-To: <31495829-66d7-c670-75e2-d1c538f38e9c@xs4all.nl> X-BeenThere: ntg-context@ntg.nl X-Mailman-Version: 2.1.26 Precedence: list List-Id: mailing list for ConTeXt users List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ntg-context-bounces@ntg.nl Original-Sender: "ntg-context" Xref: news.gmane.io gmane.comp.tex.context:108105 Archived-At: --===============3532493619657829189== Content-Type: multipart/alternative; boundary="000000000000abef4d05aadcc763" --000000000000abef4d05aadcc763 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi all, Thank you for your contributions, it allows me to progress. @Hans and Deal The code is impressive but does not correspond to the shape of the triangle that I have to make (by convention). Fabrice Le lun. 20 juil. 2020 =C3=A0 10:36, Hans Hagen a =C3=A9= crit : > On 7/20/2020 7:56 AM, Jeong Dal wrote: > > Dear Fabrice, > > > > You may split Binom(n,k) function into two functions as following: > > > > see original mail > > > > Dalyoung > Best stay in a protected namespace ... > > \startluacode > > local function fact (n) > if n <=3D 0 then > return 1 > else > return n * fact(n-1) > end > end > > local function ncr(n,r) > return fact(n)/(fact(r)*fact(n-r)) > end > > userdata.P =3D { > fact =3D fact, > ncr =3D ncr, > } > > function MP.pascal_ncr(n, r) > mp.print(ncr(n,r)) > end > > \stopluacode > > Watch the last definition. This permits > > % tt :=3D lua("mp.print(userdata.P.ncr(" & decimal n & "," & > decimal r & " ))"); > > replaced by > > tt :=3D lua.MP.pascal_ncr(n,r); > > which looks nicer. > > \startbuffer[pt1] > numeric n, r, s, u, dx, dy, tt; > path p, q; > pair A, B, start, now; > u :=3D 1.8cm; > A :=3D dir(210)*u; > B :=3D dir(-30)*u; > dy :=3D sind(30)*u; > dx :=3D 2*cosd(30)*u; > for n=3D0 upto 4: > start :=3D n*dir(210)*u; > for r=3D0 upto n: > s :=3D n-r; > % tt :=3D lua("mp.print(userdata.P.ncr(" & decimal n & "," & > decimal r & " ))"); > tt :=3D lua.MP.pascal_ncr(n,r); > now :=3D start+r*right*dx; > dotlabel.top(textext("$\displaystyle {" & decimal n & > "\choose" & decimal r & "} =3D "& decimal tt & "$"),now); > draw (now+A) -- now -- (now+B); > endfor; > endfor; > \stopbuffer > > Now, in context lmtx we can have a different kind of abstraction. We can > do this: > > function MP.pascal_ncr_x() > mp.print(ncr(mp.scan.pair())) > end > > and then use: > > tt :=3D runscript("MP.pascal_ncr_x()") (n,r) ; > > Of course one can decide to pick to two numerics instead, like > > tt :=3D runscript("MP.pascal_ncr_x()") n r ; > > but i leave that as exercise. > > % tt :=3D runscript mp_pascal_ncr (n,r) ; > tt :=3D pascal_ncr (n,r) ; > > However, we still have the rather verbose runscript here, so we go > further, we register pascal as script: > > \startluacode > metapost.registerscript("pascal_ncr",MP.pascal_ncr_x) > \stopluacode > > And then define an alias at the metafun end: > > \startMPextensions > newinternal mp_pascal_ncr ; mp_pascal_ncr :=3D scriptindex "pascal_n= cr" > ; > > def pascal_ncr =3D > runscript mp_pascal_ncr > enddef ; > \stopMPextensions > > The internal permits this: > > tt :=3D runscript mp_pascal_ncr (n,r) ; > > while the additional def permits > > tt :=3D pascal_ncr (n,r) ; > > Now watch out, because we define pascal_ncr here, something > lua.MP.pascal_ncr(n,r) won't work because the last part gets expanded > because that is what mp does (i'll probably cook something for that some > day). > > Now, to come back to > > "I couldn=E2=80=99t wikify it at that time because I don=E2=80=99t kn= ow > how to. I=E2=80=99ll do it soon." > > looks like you suddenly have an additional challenge, > > Hans > > > > ----------------------------------------------------------------- > Hans Hagen | PRAGMA ADE > Ridderstraat 27 | 8061 GH Hasselt | The Netherlands > tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl > ----------------------------------------------------------------- > > _________________________________________________________________________= __________ > If your question is of interest to others as well, please add an entry to > the Wiki! > > maillist : ntg-context@ntg.nl / > http://www.ntg.nl/mailman/listinfo/ntg-context > webpage : http://www.pragma-ade.nl / http://context.aanhet.net > archive : https://bitbucket.org/phg/context-mirror/commits/ > wiki : http://contextgarden.net > > _________________________________________________________________________= __________ > --000000000000abef4d05aadcc763 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi all,
Thank you for your contributions, it allows me to progress.@Hans and Deal
The code is im= pressive but does not correspond to the shape of the triangle

that I have to make (by convention).
Fabrice

Le=C2=A0lun. 20 juil. 2020 =C3=A0=C2=A010:36, Hans Hagen &l= t;j.hagen@xs4all.nl> a =C3=A9cr= it=C2=A0:
On 7/2= 0/2020 7:56 AM, Jeong Dal wrote:
> Dear Fabrice,
>
> You may split Binom(n,k) function into two functions as following:
>
=C2=A0> see original mail
=C2=A0>
> Dalyoung
Best stay in a protected namespace ...

\startluacode

=C2=A0 =C2=A0 =C2=A0local function fact (n)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if n <=3D 0 then
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return 1
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0else
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return n * fact(n-1)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0end
=C2=A0 =C2=A0 =C2=A0end

=C2=A0 =C2=A0 =C2=A0local function ncr(n,r)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return fact(n)/(fact(r)*fact(n-r))
=C2=A0 =C2=A0 =C2=A0end

=C2=A0 =C2=A0 =C2=A0userdata.P =3D {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0fact =3D fact,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ncr=C2=A0 =3D ncr,
=C2=A0 =C2=A0 =C2=A0}

=C2=A0 =C2=A0 =C2=A0function MP.pascal_ncr(n, r)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0mp.print(ncr(n,r))
=C2=A0 =C2=A0 =C2=A0end

\stopluacode

Watch the last definition. This permits

=C2=A0 =C2=A0 =C2=A0 =C2=A0% tt :=3D lua("mp.print(userdata.P.ncr(&quo= t; & decimal n & "," &
decimal r & " ))");

replaced by

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0tt :=3D lua.MP.pascal_ncr(n,r);

which looks nicer.

\startbuffer[pt1]
=C2=A0 =C2=A0 =C2=A0numeric n, r, s, u, dx, dy, tt;
=C2=A0 =C2=A0 =C2=A0path p, q;
=C2=A0 =C2=A0 =C2=A0pair A, B, start, now;
=C2=A0 =C2=A0 =C2=A0u :=3D 1.8cm;
=C2=A0 =C2=A0 =C2=A0A :=3D dir(210)*u;
=C2=A0 =C2=A0 =C2=A0B :=3D dir(-30)*u;
=C2=A0 =C2=A0 =C2=A0dy :=3D sind(30)*u;
=C2=A0 =C2=A0 =C2=A0dx :=3D 2*cosd(30)*u;
=C2=A0 =C2=A0 =C2=A0for n=3D0 upto 4:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0start :=3D n*dir(210)*u;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0for r=3D0 upto n:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0s :=3D n-r;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0% tt :=3D lua("mp.print(userd= ata.P.ncr(" & decimal n & "," &
decimal r & " ))");
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0tt :=3D lua.MP.pascal_ncr(n,r); =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0now :=3D start+r*right*dx;<= br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0dotlabel.top(textext("= $\displaystyle {" & decimal n &
"\choose" & decimal r & "} =3D "& decimal t= t & "$"),now);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0draw (now+A) -- now -- (now= +B);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0endfor;
=C2=A0 =C2=A0 =C2=A0endfor;
\stopbuffer

Now, in context lmtx we can have a different kind of abstraction. We can do this:

=C2=A0 =C2=A0 =C2=A0function MP.pascal_ncr_x()
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0mp.print(ncr(mp.scan.pair()))
=C2=A0 =C2=A0 =C2=A0end

and then use:

=C2=A0 =C2=A0 =C2=A0tt :=3D runscript("MP.pascal_ncr_x()") (n,r) = ;

Of course one can decide to pick to two numerics instead, like

=C2=A0 =C2=A0 =C2=A0tt :=3D runscript("MP.pascal_ncr_x()") n r ;<= br>
but i leave that as exercise.

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0% tt :=3D runscript mp_pascal_ncr = (n,r) ;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0tt :=3D pascal_ncr (n,r) ;<= br>
However, we still have the rather verbose runscript here, so we go
further, we register pascal as script:

\startluacode
=C2=A0 =C2=A0 =C2=A0metapost.registerscript("pascal_ncr",MP.pasca= l_ncr_x)
\stopluacode

And then define an alias at the metafun end:

\startMPextensions
=C2=A0 =C2=A0 =C2=A0newinternal mp_pascal_ncr ; mp_pascal_ncr :=3D scriptin= dex "pascal_ncr" ;

=C2=A0 =C2=A0 =C2=A0def pascal_ncr =3D
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0runscript mp_pascal_ncr
=C2=A0 =C2=A0 =C2=A0enddef ;
\stopMPextensions

The internal permits this:

=C2=A0 =C2=A0 tt :=3D runscript mp_pascal_ncr (n,r) ;

while the additional def permits

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0tt :=3D pascal_ncr (n,r) ;<= br>
Now watch out, because we define pascal_ncr here, something
lua.MP.pascal_ncr(n,r) won't work because the last part gets expanded <= br> because that is what mp does (i'll probably cook something for that som= e
day).

Now, to come back to

=C2=A0 =C2=A0 "I couldn=E2=80=99t wikify it at that time because I don= =E2=80=99t know
=C2=A0 =C2=A0 =C2=A0how to.=C2=A0 I=E2=80=99ll do it soon."

looks like you suddenly have an additional challenge,

Hans



-----------------------------------------------------------------
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0Hans Hagen | PRAGMA ADE
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Ridderstraat 27 | 80= 61 GH Hasselt | The Netherlands
=C2=A0 =C2=A0 =C2=A0 =C2=A0 tel: 038 477 53 69 | www.pragma-ade.nl | www.= pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________= ________
If your question is of interest to others as well, please add an entry to t= he Wiki!

maillist : ntg-cont= ext@ntg.nl / http://www.ntg.nl/mailman/listinfo/nt= g-context
webpage=C2=A0 : http://www.pragma-ade.nl / http://context.aanhet.net=
archive=C2=A0 : https://bitbucket.org/phg/context-m= irror/commits/
wiki=C2=A0 =C2=A0 =C2=A0: http://contextgarden.net
___________________________________________________________________________= ________
--000000000000abef4d05aadcc763-- --===============3532493619657829189== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f X19fX19fX19fX19fX19fX19fX19fX19fX18KSWYgeW91ciBxdWVzdGlvbiBpcyBvZiBpbnRlcmVz dCB0byBvdGhlcnMgYXMgd2VsbCwgcGxlYXNlIGFkZCBhbiBlbnRyeSB0byB0aGUgV2lraSEKCm1h aWxsaXN0IDogbnRnLWNvbnRleHRAbnRnLm5sIC8gaHR0cDovL3d3dy5udGcubmwvbWFpbG1hbi9s aXN0aW5mby9udGctY29udGV4dAp3ZWJwYWdlICA6IGh0dHA6Ly93d3cucHJhZ21hLWFkZS5ubCAv IGh0dHA6Ly9jb250ZXh0LmFhbmhldC5uZXQKYXJjaGl2ZSAgOiBodHRwczovL2JpdGJ1Y2tldC5v cmcvcGhnL2NvbnRleHQtbWlycm9yL2NvbW1pdHMvCndpa2kgICAgIDogaHR0cDovL2NvbnRleHRn YXJkZW4ubmV0Cl9fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fCg== --===============3532493619657829189==--