* Code lua in a table @ 2020-07-18 18:05 Fabrice Couvreur 2020-07-18 18:32 ` Wolfgang Schuster 0 siblings, 1 reply; 15+ messages in thread From: Fabrice Couvreur @ 2020-07-18 18:05 UTC (permalink / raw) To: mailing list for ConTeXt users [-- Attachment #1.1: Type: text/plain, Size: 773 bytes --] Hello, I want to display the values of Pascal's triangle in a table. Unfortunately my knowledge of Lua is not sufficient to run this code. Thank you. Fabrice \starttext \startluacode function Binom(n,k) if k > n then return 0 elseif (n == 0 or k == 0) then return 1 else return (n*Binom(n-1,k-1))/k end context(Binom(n,k)) end context.startxtable({"align={middle,lohi}, width=1.2cm,offset=0.8ex,bodyfont=9pt"}) context.startxrow({"background=color,backgroundcolor=green"}) for i = 0, 10 do for j = 0, i do context.startxcell() context(Binom(i,j)) context.stopxcell() end end context.stopxrow() \stopluacode \stoptext [-- Attachment #1.2: Type: text/html, Size: 1623 bytes --] [-- Attachment #2: Type: text/plain, Size: 493 bytes --] ___________________________________________________________________________________ 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: Code lua in a table 2020-07-18 18:05 Code lua in a table Fabrice Couvreur @ 2020-07-18 18:32 ` Wolfgang Schuster 2020-07-18 20:09 ` Fabrice Couvreur 0 siblings, 1 reply; 15+ messages in thread From: Wolfgang Schuster @ 2020-07-18 18:32 UTC (permalink / raw) To: mailing list for ConTeXt users [-- Attachment #1.1: Type: text/plain, Size: 960 bytes --] Fabrice Couvreur schrieb am 18.07.2020 um 20:05: > Hello, > I want to display the values of Pascal's triangle in a table. > Unfortunately my knowledge of Lua is not sufficient to run this code. > Thank you. > Fabrice > > \starttext > > \startluacode > function Binom(n,k) > if k > n then > return 0 > elseif (n == 0 or k == 0) then > return 1 > else > return (n*Binom(n-1,k-1))/k > end > context(Binom(n,k)) Are you sure you want to use context(...) here? > end > > context.startxtable({"align={middle,lohi}, > width=1.2cm,offset=0.8ex,bodyfont=9pt"}) > context.startxrow({"background=color,backgroundcolor=green"}) > for i = 0, 10 do > for j = 0, i do > context.startxcell() > context(Binom(i,j)) > context.stopxcell() > end > end > context.stopxrow() context.stopxtable() Wolfgang [-- Attachment #1.2: Type: text/html, Size: 2387 bytes --] [-- Attachment #2: Type: text/plain, Size: 493 bytes --] ___________________________________________________________________________________ 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: Code lua in a table 2020-07-18 18:32 ` Wolfgang Schuster @ 2020-07-18 20:09 ` Fabrice Couvreur 2020-07-18 20:22 ` Wolfgang Schuster 0 siblings, 1 reply; 15+ messages in thread From: Fabrice Couvreur @ 2020-07-18 20:09 UTC (permalink / raw) To: mailing list for ConTeXt users [-- Attachment #1.1: Type: text/plain, Size: 2447 bytes --] Hi, I improved the code to be able to display the grid but I cannot display the coefficients. Thank you Fabrice \starttext \startluacode function Binom(n,k) if k > n then return 0 elseif (n == 0 or k == 0) then return 1 else return (n*Binom(n-1,k-1))/k end end context.startxtable({"align={middle,lohi}, width=1.2cm,offset=0.8ex,bodyfont=9pt"}) for i = 1, 10 do context.startxrow({"background=color,backgroundcolor=cyan"}) for j = 1, i do context.startxcell() \ctxlua{context(Binom(i,j))} context.stopxcell() end context.stopxrow() end context.stopxtable() \stopluacode \stoptext Le sam. 18 juil. 2020 à 20:32, Wolfgang Schuster < wolfgang.schuster.lists@gmail.com> a écrit : > Fabrice Couvreur schrieb am 18.07.2020 um 20:05: > > Hello, > I want to display the values of Pascal's triangle in a table. > Unfortunately my knowledge of Lua is not sufficient to run this code. > Thank you. > Fabrice > > \starttext > > \startluacode > function Binom(n,k) > if k > n then > return 0 > elseif (n == 0 or k == 0) then > return 1 > else > return (n*Binom(n-1,k-1))/k > end > context(Binom(n,k)) > > Are you sure you want to use context(...) here? > > end > > context.startxtable({"align={middle,lohi}, > width=1.2cm,offset=0.8ex,bodyfont=9pt"}) > context.startxrow({"background=color,backgroundcolor=green"}) > for i = 0, 10 do > for j = 0, i do > context.startxcell() > context(Binom(i,j)) > context.stopxcell() > end > end > context.stopxrow() > > context.stopxtable() > > Wolfgang > > > ___________________________________________________________________________________ > 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 > > ___________________________________________________________________________________ > [-- Attachment #1.2: Type: text/html, Size: 5263 bytes --] [-- Attachment #2: Type: text/plain, Size: 493 bytes --] ___________________________________________________________________________________ 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: Code lua in a table 2020-07-18 20:09 ` Fabrice Couvreur @ 2020-07-18 20:22 ` Wolfgang Schuster 2020-07-18 20:25 ` Fabrice Couvreur 0 siblings, 1 reply; 15+ messages in thread From: Wolfgang Schuster @ 2020-07-18 20:22 UTC (permalink / raw) To: mailing list for ConTeXt users Fabrice Couvreur schrieb am 18.07.2020 um 22:09: > Hi, > I improved the code to be able to display the grid but I cannot display > the coefficients. > Thank you > Fabrice > > \starttext > > \startluacode > function Binom(n,k) > if k > n then > return 0 > elseif (n == 0 or k == 0) then > return 1 > else > return (n*Binom(n-1,k-1))/k > end > > end > > context.startxtable({"align={middle,lohi}, > width=1.2cm,offset=0.8ex,bodyfont=9pt"}) > for i = 1, 10 do > context.startxrow({"background=color,backgroundcolor=cyan"}) > for j = 1, i do > context.startxcell() > \ctxlua{context(Binom(i,j))} Remove \ctxlua: context(Binom(i,j)) Wolfgang ___________________________________________________________________________________ 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: Code lua in a table 2020-07-18 20:22 ` Wolfgang Schuster @ 2020-07-18 20:25 ` Fabrice Couvreur 2020-07-18 20:34 ` Fabrice Couvreur ` (2 more replies) 0 siblings, 3 replies; 15+ messages in thread From: Fabrice Couvreur @ 2020-07-18 20:25 UTC (permalink / raw) To: mailing list for ConTeXt users [-- Attachment #1.1: Type: text/plain, Size: 1718 bytes --] Hi Wolfgang, It works but why did you ask this question since I finally use context ? > Are you sure you want to use context(...) here? > How not to display the .0 ? Fabrice Le sam. 18 juil. 2020 à 22:22, Wolfgang Schuster < wolfgang.schuster.lists@gmail.com> a écrit : > Fabrice Couvreur schrieb am 18.07.2020 um 22:09: > > Hi, > > I improved the code to be able to display the grid but I cannot display > > the coefficients. > > Thank you > > Fabrice > > > > \starttext > > > > \startluacode > > function Binom(n,k) > > if k > n then > > return 0 > > elseif (n == 0 or k == 0) then > > return 1 > > else > > return (n*Binom(n-1,k-1))/k > > end > > > > end > > > > context.startxtable({"align={middle,lohi}, > > width=1.2cm,offset=0.8ex,bodyfont=9pt"}) > > for i = 1, 10 do > > context.startxrow({"background=color,backgroundcolor=cyan"}) > > for j = 1, i do > > context.startxcell() > > \ctxlua{context(Binom(i,j))} > > Remove \ctxlua: > > context(Binom(i,j)) > > Wolfgang > > ___________________________________________________________________________________ > 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 > > ___________________________________________________________________________________ > [-- Attachment #1.2: Type: text/html, Size: 3719 bytes --] [-- Attachment #2: Type: text/plain, Size: 493 bytes --] ___________________________________________________________________________________ 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: Code lua in a table 2020-07-18 20:25 ` Fabrice Couvreur @ 2020-07-18 20:34 ` Fabrice Couvreur 2020-07-18 20:36 ` Wolfgang Schuster 2020-07-18 21:35 ` Otared Kavian 2 siblings, 0 replies; 15+ messages in thread From: Fabrice Couvreur @ 2020-07-18 20:34 UTC (permalink / raw) To: mailing list for ConTeXt users [-- Attachment #1.1: Type: text/plain, Size: 2588 bytes --] Small correction : \starttext \startluacode function Binom(n,k) if k > n then return 0 elseif (n == 0 or k == 0) then return 1 else return (n*Binom(n-1,k-1))/k end end context.startxtable({"align={middle,lohi}, width=1cm,offset=0.8ex,bodyfont=9pt"}) for i = 0, 9 do context.startxrow({"background=color,backgroundcolor=cyan"}) for j = 0, i do context.startxcell() context(Binom(i,j)) context.stopxcell() end context.stopxrow() end context.stopxtable() \stopluacode \stoptext Le sam. 18 juil. 2020 à 22:25, Fabrice Couvreur <fabrice1.couvreur@gmail.com> a écrit : > Hi Wolfgang, > > It works but why did you ask this question since I finally use context ? > >> Are you sure you want to use context(...) here? >> > How not to display the .0 ? > Fabrice > > Le sam. 18 juil. 2020 à 22:22, Wolfgang Schuster < > wolfgang.schuster.lists@gmail.com> a écrit : > >> Fabrice Couvreur schrieb am 18.07.2020 um 22:09: >> > Hi, >> > I improved the code to be able to display the grid but I cannot display >> > the coefficients. >> > Thank you >> > Fabrice >> > >> > \starttext >> > >> > \startluacode >> > function Binom(n,k) >> > if k > n then >> > return 0 >> > elseif (n == 0 or k == 0) then >> > return 1 >> > else >> > return (n*Binom(n-1,k-1))/k >> > end >> > >> > end >> > >> > context.startxtable({"align={middle,lohi}, >> > width=1.2cm,offset=0.8ex,bodyfont=9pt"}) >> > for i = 1, 10 do >> > context.startxrow({"background=color,backgroundcolor=cyan"}) >> > for j = 1, i do >> > context.startxcell() >> > \ctxlua{context(Binom(i,j))} >> >> Remove \ctxlua: >> >> context(Binom(i,j)) >> >> Wolfgang >> >> ___________________________________________________________________________________ >> 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 >> >> ___________________________________________________________________________________ >> > [-- Attachment #1.2: Type: text/html, Size: 4972 bytes --] [-- Attachment #2: Type: text/plain, Size: 493 bytes --] ___________________________________________________________________________________ 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: Code lua in a table 2020-07-18 20:25 ` Fabrice Couvreur 2020-07-18 20:34 ` Fabrice Couvreur @ 2020-07-18 20:36 ` Wolfgang Schuster 2020-07-18 21:35 ` Otared Kavian 2 siblings, 0 replies; 15+ messages in thread From: Wolfgang Schuster @ 2020-07-18 20:36 UTC (permalink / raw) To: mailing list for ConTeXt users Fabrice Couvreur schrieb am 18.07.2020 um 22:25: > Hi Wolfgang, > > It works but why did you ask this question since I finally use context ? > > Are you sure you want to use context(...) here? In your first version you used the context function in the loop to print the output of the Binom but the Binom function used context as well, this could result in context(context(...)). > How not to display the .0 ? Format the content of the context function to show only integer values, e.g. context("%d",Binom(...)). For more information search for formatter in cld-mkiv.pdf. Wolfgang ___________________________________________________________________________________ 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: Code lua in a table 2020-07-18 20:25 ` Fabrice Couvreur 2020-07-18 20:34 ` Fabrice Couvreur 2020-07-18 20:36 ` Wolfgang Schuster @ 2020-07-18 21:35 ` Otared Kavian 2020-07-18 22:05 ` Otared Kavian 2 siblings, 1 reply; 15+ messages in thread From: Otared Kavian @ 2020-07-18 21:35 UTC (permalink / raw) To: mailing list for ConTeXt users > On 18 Jul 2020, at 22:25, Fabrice Couvreur <fabrice1.couvreur@gmail.com> wrote: > > Hi Wolfgang, > > It works but why did you ask this question since I finally use context ? > Are you sure you want to use context(...) here? > How not to display the .0 ? Besides what Wolfgang suggested, ou can also use context(math.round(Binom(i,j))) to have integers instead of real numbers. Best regards: Otared ___________________________________________________________________________________ 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: Code lua in a table 2020-07-18 21:35 ` Otared Kavian @ 2020-07-18 22:05 ` Otared Kavian 0 siblings, 0 replies; 15+ messages in thread From: Otared Kavian @ 2020-07-18 22:05 UTC (permalink / raw) To: mailing list for ConTeXt users > On 18 Jul 2020, at 23:35, Otared Kavian <otared@gmail.com> wrote: > > > >> On 18 Jul 2020, at 22:25, Fabrice Couvreur <fabrice1.couvreur@gmail.com> wrote: >> >> Hi Wolfgang, >> >> It works but why did you ask this question since I finally use context ? >> Are you sure you want to use context(...) here? >> How not to display the .0 ? Or even better, you can return an integer in your Binom function: return math.round((n*Binom(n-1,k-1))/k) Best regards: Otared ___________________________________________________________________________________ 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 15+ messages in thread
[parent not found: <mailman.1.1595152801.23549.ntg-context@ntg.nl>]
* Re: Code lua in a table [not found] <mailman.1.1595152801.23549.ntg-context@ntg.nl> @ 2020-07-20 5:56 ` Jeong Dal 2020-07-20 8:36 ` Hans Hagen 0 siblings, 1 reply; 15+ messages in thread From: Jeong Dal @ 2020-07-20 5:56 UTC (permalink / raw) To: list ntg-context@ntg.nl ntg-context@ntg.nl ntg-context@ntg.nl ntg-context@ntg.nl [-- Attachment #1.1: Type: text/plain, Size: 2166 bytes --] 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 [-- Attachment #1.2: Type: text/html, Size: 13029 bytes --] [-- Attachment #2: Type: text/plain, Size: 493 bytes --] ___________________________________________________________________________________ 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: Code lua in a table 2020-07-20 5:56 ` Jeong Dal @ 2020-07-20 8:36 ` Hans Hagen 2020-07-20 10:13 ` Fabrice Couvreur 2020-07-20 12:40 ` Jeong Dal 0 siblings, 2 replies; 15+ messages in thread From: Hans Hagen @ 2020-07-20 8:36 UTC (permalink / raw) To: mailing list for ConTeXt users, Jeong Dal 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: Code lua in a table 2020-07-20 8:36 ` Hans Hagen @ 2020-07-20 10:13 ` Fabrice Couvreur 2020-07-20 12:40 ` Jeong Dal 1 sibling, 0 replies; 15+ messages in thread From: Fabrice Couvreur @ 2020-07-20 10:13 UTC (permalink / raw) To: mailing list for ConTeXt users [-- Attachment #1.1: Type: text/plain, Size: 4452 bytes --] 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 <j.hagen@xs4all.nl> a é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 <= 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 > > ___________________________________________________________________________________ > [-- Attachment #1.2: Type: text/html, Size: 7300 bytes --] [-- Attachment #2: Type: text/plain, Size: 493 bytes --] ___________________________________________________________________________________ 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: Code lua in a table 2020-07-20 8:36 ` Hans Hagen 2020-07-20 10:13 ` Fabrice Couvreur @ 2020-07-20 12:40 ` Jeong Dal 2020-07-20 12:54 ` Hans Hagen 1 sibling, 1 reply; 15+ messages in thread From: Jeong Dal @ 2020-07-20 12:40 UTC (permalink / raw) To: Hans Hagen; +Cc: mailing list for ConTeXt users 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 <j.hagen@xs4all.nl> 작성: > > 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: Code lua in a table 2020-07-20 12:40 ` Jeong Dal @ 2020-07-20 12:54 ` Hans Hagen 2020-07-21 13:08 ` Fabrice Couvreur 0 siblings, 1 reply; 15+ messages in thread From: Hans Hagen @ 2020-07-20 12:54 UTC (permalink / raw) To: Jeong Dal; +Cc: mailing list for ConTeXt users 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: Code lua in a table 2020-07-20 12:54 ` Hans Hagen @ 2020-07-21 13:08 ` Fabrice Couvreur 0 siblings, 0 replies; 15+ messages in thread From: Fabrice Couvreur @ 2020-07-21 13:08 UTC (permalink / raw) To: mailing list for ConTeXt users [-- Attachment #1.1: Type: text/plain, Size: 3617 bytes --] 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 <j.hagen@xs4all.nl> a écrit : > 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 > > ___________________________________________________________________________________ > [-- Attachment #1.2: Type: text/html, Size: 5836 bytes --] [-- Attachment #2: Type: text/plain, Size: 493 bytes --] ___________________________________________________________________________________ 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 15+ messages in thread
end of thread, other threads:[~2020-07-21 13:08 UTC | newest] Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2020-07-18 18:05 Code lua in a table Fabrice Couvreur 2020-07-18 18:32 ` Wolfgang Schuster 2020-07-18 20:09 ` Fabrice Couvreur 2020-07-18 20:22 ` Wolfgang Schuster 2020-07-18 20:25 ` Fabrice Couvreur 2020-07-18 20:34 ` Fabrice Couvreur 2020-07-18 20:36 ` Wolfgang Schuster 2020-07-18 21:35 ` Otared Kavian 2020-07-18 22:05 ` Otared Kavian [not found] <mailman.1.1595152801.23549.ntg-context@ntg.nl> 2020-07-20 5:56 ` Jeong Dal 2020-07-20 8:36 ` Hans Hagen 2020-07-20 10:13 ` Fabrice Couvreur 2020-07-20 12:40 ` Jeong Dal 2020-07-20 12:54 ` Hans Hagen 2020-07-21 13:08 ` Fabrice Couvreur
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).