Re: [NTG-context] Code lua in a table
Dear Fabrice, You may split Binom(n,k) function into two functions as following: ***** \startluacode P={} combi = P function P.fact (n) if n <= 0 then return 1 else return n * P.fact(n-1) end end function P.ncr(n,r) return P.fact(n)/(P.fact(r)*P.fact(n-r)) end combi = { fact = fact, ncr = ncr, } \stopluacode ****** Your table is actually Pascal’d triangle. Using the above function, I was able to draw Pascal’s triangles. Hans helped me to complete it. I couldn’t wikify it at that time because I don’t know how to. I’ll do it soon. Here is the whole code for Pascal’s triangle in two different ways using Lua, Metafun and ConTeXt. You may enhance the code. \startbuffer[pt1] numeric n,r,s,u,dx,dy,tt; u := 1.8cm; path p, q; pair A,B,start,now; A := dir(210)*u; B := dir(-30)*u; dy := sind(30)*u; dx := 2*cosd(30)*u; for n=0 upto 4: start := n*dir(210)*u; for r=0 upto n: s := n-r; tt := lua("mp.print(P.ncr(" & decimal n & "," & decimal r & " ))"); now := start+r*right*dx; dotlabel.top(textext("$\displaystyle {" & decimal n & "\choose" & decimal r & "} = "& decimal tt & "$"),now); draw (now+A) -- now -- (now+B); endfor; endfor; \stopbuffer \startbuffer[pt2] numeric n,r,s,u,dx,dy,tt; u := 1cm; path p, q; pair A,B,start,now; A := dir(210)*u; B := dir(-30)*u; dy := sind(30)*u; dx := 2*cosd(30)*u; for n=0 upto 8: start := n*dir(210)*u; for r=0 upto n: s := n-r; tt := lua("mp.print(P.ncr(" & decimal n & "," & decimal r & " ))"); now := start+r*right*dx; label(textext("$" & decimal tt & "$"),now); endfor; endfor; \stopbuffer \startluacode P={} combi = P function P.fact (n) if n <= 0 then return 1 else return n * P.fact(n-1) end end function P.ncr(n,r) return P.fact(n)/(P.fact(r)*P.fact(n-r)) end combi = { fact = fact, ncr = ncr, } \stopluacode \starttext \processMPbuffer[pt1] \blank[big] \processMPbuffer[pt2] \stoptext I hope that it helps. Best regards, Dalyoung
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 <= 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 = { fact = fact, ncr = ncr, } function MP.pascal_ncr(n, r) mp.print(ncr(n,r)) end \stopluacode Watch the last definition. This permits % tt := lua("mp.print(userdata.P.ncr(" & decimal n & "," & decimal r & " ))"); replaced by tt := 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 := 1.8cm; A := dir(210)*u; B := dir(-30)*u; dy := sind(30)*u; dx := 2*cosd(30)*u; for n=0 upto 4: start := n*dir(210)*u; for r=0 upto n: s := n-r; % tt := lua("mp.print(userdata.P.ncr(" & decimal n & "," & decimal r & " ))"); tt := lua.MP.pascal_ncr(n,r); now := start+r*right*dx; dotlabel.top(textext("$\displaystyle {" & decimal n & "\choose" & decimal r & "} = "& 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 := runscript("MP.pascal_ncr_x()") (n,r) ; Of course one can decide to pick to two numerics instead, like tt := runscript("MP.pascal_ncr_x()") n r ; but i leave that as exercise. % tt := runscript mp_pascal_ncr (n,r) ; tt := 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 := scriptindex "pascal_ncr" ; def pascal_ncr = runscript mp_pascal_ncr enddef ; \stopMPextensions The internal permits this: tt := runscript mp_pascal_ncr (n,r) ; while the additional def permits tt := 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’t wikify it at that time because I don’t know how to. I’ll 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 -----------------------------------------------------------------
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 à 10:36, Hans Hagen
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 <= 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 = { fact = fact, ncr = ncr, }
function MP.pascal_ncr(n, r) mp.print(ncr(n,r)) end
\stopluacode
Watch the last definition. This permits
% tt := lua("mp.print(userdata.P.ncr(" & decimal n & "," & decimal r & " ))");
replaced by
tt := 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 := 1.8cm; A := dir(210)*u; B := dir(-30)*u; dy := sind(30)*u; dx := 2*cosd(30)*u; for n=0 upto 4: start := n*dir(210)*u; for r=0 upto n: s := n-r; % tt := lua("mp.print(userdata.P.ncr(" & decimal n & "," & decimal r & " ))"); tt := lua.MP.pascal_ncr(n,r); now := start+r*right*dx; dotlabel.top(textext("$\displaystyle {" & decimal n & "\choose" & decimal r & "} = "& 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 := runscript("MP.pascal_ncr_x()") (n,r) ;
Of course one can decide to pick to two numerics instead, like
tt := runscript("MP.pascal_ncr_x()") n r ;
but i leave that as exercise.
% tt := runscript mp_pascal_ncr (n,r) ; tt := 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 := scriptindex "pascal_ncr" ;
def pascal_ncr = runscript mp_pascal_ncr enddef ; \stopMPextensions
The internal permits this:
tt := runscript mp_pascal_ncr (n,r) ;
while the additional def permits
tt := 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’t wikify it at that time because I don’t know how to. I’ll 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
___________________________________________________________________________________
Dear Hans, Thank you for new methods! It would take some time for me to understand them fully. Learning Lua, metafun, lmtx, wiki is always a challenge for me. I also wonder where is the boundary of ConTeXt. I hope that you and all members in this list are well from COVID-19. Thank you again. Best regards, Dalyoung
2020. 7. 20. 오후 5:36, Hans Hagen
작성: 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 <= 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 = { fact = fact, ncr = ncr, }
function MP.pascal_ncr(n, r) mp.print(ncr(n,r)) end
\stopluacode
Watch the last definition. This permits
% tt := lua("mp.print(userdata.P.ncr(" & decimal n & "," & decimal r & " ))");
replaced by
tt := 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 := 1.8cm; A := dir(210)*u; B := dir(-30)*u; dy := sind(30)*u; dx := 2*cosd(30)*u; for n=0 upto 4: start := n*dir(210)*u; for r=0 upto n: s := n-r; % tt := lua("mp.print(userdata.P.ncr(" & decimal n & "," & decimal r & " ))"); tt := lua.MP.pascal_ncr(n,r); now := start+r*right*dx; dotlabel.top(textext("$\displaystyle {" & decimal n & "\choose" & decimal r & "} = "& 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 := runscript("MP.pascal_ncr_x()") (n,r) ;
Of course one can decide to pick to two numerics instead, like
tt := runscript("MP.pascal_ncr_x()") n r ;
but i leave that as exercise.
% tt := runscript mp_pascal_ncr (n,r) ; tt := 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 := scriptindex "pascal_ncr" ;
def pascal_ncr = runscript mp_pascal_ncr enddef ; \stopMPextensions
The internal permits this:
tt := runscript mp_pascal_ncr (n,r) ;
while the additional def permits
tt := 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’t wikify it at that time because I don’t know how to. I’ll 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 -----------------------------------------------------------------
On 7/20/2020 2:40 PM, Jeong Dal wrote:
I also wonder where is the boundary of ConTeXt. The boundaries are set and shift by users (the mailing list) and curiosity (personal).
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 -----------------------------------------------------------------
Hello,
I am making progress and I almost get what I want to achieve but I still
have two problems :
how to color cells not containing numbers with the same color as the others
?
how to color in salmon, for example, cells 1 2 ?
3
Thank you
Fabrice
\usecolors[X11]
\startuseMPgraphic{DiagonalRule}
rulethickness := \frameddimension{rulethickness};
drawoptions(
withpen pencircle scaled rulethickness
withcolor \MPcolor{\framedparameter{framecolor}});
pair leftcorner, rightcorner;
leftcorner := (rulethickness, \overlayheight-rulethickness);
rightcorner := (\overlaywidth-rulethickness, rulethickness);
draw leftcorner -- rightcorner;
\stopuseMPgraphic
\defineoverlay
[DiagonalRule]
[\useMPgraphic{DiagonalRule}]
\define[2]\DiagonalLabel{%
\setuptabulate [after={\blank[\frameddimension{offset}]}]
\starttabulate [|p|r|]
\NC \NC #2 \NC\NR
\NC #1 \NC \NC\NR
\stoptabulate
}
\starttext
\startluacode
function Binom(n,k)
if k > n then
return ""
elseif (n == 0 or k == 0) then
return 1
else
return math.round((n*Binom(n-1,k-1))/k)
end
end
context.startxtable({"align={middle,lohi},
width=1cm,offset=0.8ex,bodyfont=9pt,framecolor=cyan"})
context.startxrow()
context.startxcell({"background=DiagonalRule,background=color,backgroundcolor=thistle2"})
context("\\DiagonalLabel{\\m{n}}{\\m{k}}")
context.stopxcell()
for j = 0, 7 do
context.startxcell({"background=color,backgroundcolor=thistle2"})
context(j)
context.stopxcell()
end
context.startxcell({"background=color,backgroundcolor=thistle2"})
context("\\dots")
context.stopxcell()
context.stopxrow()
for i = 0, 7 do
context.startxrow()
context.startxcell({"background=color,backgroundcolor=thistle2"})
context(i)
context.stopxcell()
for j = 0, 8 do
context.startxcell()
context(Binom(i,j))
context.stopxcell()
end
context.stopxrow()
end
context.startxrow()
context.startxcell({"background=color,backgroundcolor=thistle2"})
context("\\dots")
context.stopxcell()
for i = 0, 8 do
context.startxcell({"background=color,backgroundcolor=thistle2"})
context("")
context.stopxcell()
end
context.stopxrow()
context.stopxtable()
\stopluacode
\stoptext
Le lun. 20 juil. 2020 à 14:55, Hans Hagen
On 7/20/2020 2:40 PM, Jeong Dal wrote:
I also wonder where is the boundary of ConTeXt. The boundaries are set and shift by users (the mailing list) and curiosity (personal).
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
___________________________________________________________________________________
participants (3)
-
Fabrice Couvreur
-
Hans Hagen
-
Jeong Dal