ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Jeong Dal <haksan@me.com>
To: "list ntg-context@ntg.nl ntg-context@ntg.nl ntg-context@ntg.nl
	ntg-context@ntg.nl" <ntg-context@ntg.nl>
Subject: Re: Code lua in a table
Date: Mon, 20 Jul 2020 14:56:58 +0900	[thread overview]
Message-ID: <D512FBE4-D72C-4DE6-BE4E-19D2E8266604@me.com> (raw)
In-Reply-To: <mailman.1.1595152801.23549.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
___________________________________________________________________________________

       reply	other threads:[~2020-07-20  5:56 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.1.1595152801.23549.ntg-context@ntg.nl>
2020-07-20  5:56 ` Jeong Dal [this message]
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
2020-07-18 18:05 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=D512FBE4-D72C-4DE6-BE4E-19D2E8266604@me.com \
    --to=haksan@me.com \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).