ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* running lua in metafun and in ConTeXt
       [not found] <mailman.2082.1586557063.1332.ntg-context@ntg.nl>
@ 2020-04-11 12:32 ` Jeong Dal
  2020-04-11 12:48   ` Otared Kavian
  2020-04-11 14:15 ` Jeong Dal
  1 sibling, 1 reply; 7+ messages in thread
From: Jeong Dal @ 2020-04-11 12:32 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: 2574 bytes --]

Dear all,

I draw Pascal’s triangle with the label of each point created by using lua functions.
As you see in the code below, I defined fact(n) and combi(n,r) using lua.
For the calculation tt, I used two different methods.
Both are working well when I run the given code.

But I got an error, when I added this code to a ConTeXt file composed product and components,
and using the second method, i.e. combi(n,r). The first method is still working well.
The error message is as following:

metapost log    > loading metafun, including plain.mp version 1.004 for metafun iv and xl
metapost log    > 
luatex warning  > mplib: run script: [string "return mp.print(combi(0,0 ))"]:1: attempt to call a nil value (global 'combi')

metapost log    > >> tt
metapost log    > >> vacuous
metapost log    > ! Equation cannot be performed (numeric=vacuous).
metapost log    > <to be read again> 
metapost log    >                    ;
metapost log    > <for(0)> ...&decimal(0)&","&decimal(EXPR0)&" ))");
metapost log    >                                                   now:=start+(EXPR0)*right*d...
metapost log    > <for(0)> ....now--(now+A);draw.now--(now+B);endfor
metapost log    >                                                   ; ENDFOR
metapost log    > <*> ...now+A); draw now -- (now+B); endfor; endfor
metapost log    >                                                   ;;

I don’t know why the same code treated differently.
I think that I have to do something more in luacode which I don’t know.
Comments are wellcome!

Best regards,

Dalyoung

%%%%%%%

\startluacode
function fact (n)
  if n <= 0 then
    return 1
  else
    return n * fact(n-1)
  end
end
function combi(n,r)
   return fact(n)/(fact(r)*fact(n-r))
end
\stopluacode
\startbuffer[fig121]
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(fact(" & decimal n & ")/(fact(" & decimal r & ")*fact(" & decimal s &" )))");
      tt := lua("mp.print(combi(" & decimal n & "," &  decimal r & " ))");
      
      now := start+r*right*dx;
      dotlabel.top(textext("$\displaystyle {" & decimal n & "\choose" & decimal r & "} = "& decimal tt & "$"),now);
      draw now -- (now+A);
      draw now -- (now+B);
   endfor;
endfor;
\stopbuffer
\starttext
\switchtobodyfont[11pt]
\processMPbuffer[fig121]
\switchtobodyfont[12pt]
\stoptext


[-- Attachment #1.2: Type: text/html, Size: 11827 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] 7+ messages in thread

* Re: running lua in metafun and in ConTeXt
  2020-04-11 12:32 ` running lua in metafun and in ConTeXt Jeong Dal
@ 2020-04-11 12:48   ` Otared Kavian
  0 siblings, 0 replies; 7+ messages in thread
From: Otared Kavian @ 2020-04-11 12:48 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hi Dalyoung,

I tested your code: it works fine with both definitions of the variable tt in your code. I am using the current LMTX version 2020.04.08 14:38.

Best regards: Otared

> On 11 Apr 2020, at 14:32, Jeong Dal <haksan@me.com> wrote:
> 
> Dear all,
> 
> I draw Pascal’s triangle with the label of each point created by using lua functions.
> As you see in the code below, I defined fact(n) and combi(n,r) using lua.
> For the calculation tt, I used two different methods.
> Both are working well when I run the given code.
> 
> But I got an error, when I added this code to a ConTeXt file composed product and components,
> and using the second method, i.e. combi(n,r). The first method is still working well.
> The error message is as following:
> 
> metapost log    > loading metafun, including plain.mp version 1.004 for metafun iv and xl
> metapost log    > 
> luatex warning  > mplib: run script: [string "return mp.print(combi(0,0 ))"]:1: attempt to call a nil value (global 'combi')
> 
> metapost log    > >> tt
> metapost log    > >> vacuous
> metapost log    > ! Equation cannot be performed (numeric=vacuous).
> metapost log    > <to be read again> 
> metapost log    >                    ;
> metapost log    > <for(0)> ...&decimal(0)&","&decimal(EXPR0)&" ))");
> metapost log    >                                                   now:=start+(EXPR0)*right*d...
> metapost log    > <for(0)> ....now--(now+A);draw.now--(now+B);endfor
> metapost log    >                                                   ; ENDFOR
> metapost log    > <*> ...now+A); draw now -- (now+B); endfor; endfor
> metapost log    >                                                   ;;
> 
> I don’t know why the same code treated differently.
> I think that I have to do something more in luacode which I don’t know.
> Comments are wellcome!
> 
> Best regards,
> 
> Dalyoung
> 
> %%%%%%%
> 
> \startluacode
> function fact (n)
>   if n <= 0 then
>     return 1
>   else
>     return n * fact(n-1)
>   end
> end
> function combi(n,r)
>    return fact(n)/(fact(r)*fact(n-r))
> end
> \stopluacode
> \startbuffer[fig121]
> 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(fact(" & decimal n & ")/(fact(" & decimal r & ")*fact(" & decimal s &" )))");
>       tt := lua("mp.print(combi(" & decimal n & "," &  decimal r & " ))");
>       
>       now := start+r*right*dx;
>       dotlabel.top(textext("$\displaystyle {" & decimal n & "\choose" & decimal r & "} = "& decimal tt & "$"),now);
>       draw now -- (now+A);
>       draw now -- (now+B);
>    endfor;
> endfor;
> \stopbuffer
> \starttext
> \switchtobodyfont[11pt]
> \processMPbuffer[fig121]
> \switchtobodyfont[12pt]
> \stoptext
> 
> ___________________________________________________________________________________
> 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
> ___________________________________________________________________________________

___________________________________________________________________________________
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] 7+ messages in thread

* running lua in metafun and in ConTeXt
       [not found] <mailman.2082.1586557063.1332.ntg-context@ntg.nl>
  2020-04-11 12:32 ` running lua in metafun and in ConTeXt Jeong Dal
@ 2020-04-11 14:15 ` Jeong Dal
  2020-04-11 17:08   ` Hans Hagen
  1 sibling, 1 reply; 7+ messages in thread
From: Jeong Dal @ 2020-04-11 14:15 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: 1176 bytes --]

Dear all,

The problem is solved by using the namespace of lua as below:
I am not sure what I did is the right way.
If it is not the right way, please let me know.

Thanks for reading.

Best regards,

Dalyoung



\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

\startbuffer[fig121]
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.fact(" & decimal n & ")/(P.fact(" & decimal r & ")*P.fact(" & decimal s &" )))");
      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 -- (now+A);
      draw now -- (now+B);
   endfor;
endfor;
\stopbuffer


[-- Attachment #1.2: Type: text/html, Size: 9606 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] 7+ messages in thread

* Re: running lua in metafun and in ConTeXt
  2020-04-11 14:15 ` Jeong Dal
@ 2020-04-11 17:08   ` Hans Hagen
  2020-04-11 23:19     ` Jeong Dal
  0 siblings, 1 reply; 7+ messages in thread
From: Hans Hagen @ 2020-04-11 17:08 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Jeong Dal

On 4/11/2020 4:15 PM, Jeong Dal wrote:
> Dear all,
> 
> The problem is solved by using the namespace of lua as below:
> I am not sure what I did is the right way.
> If it is not the right way, please let me know.
> 
> Thanks for reading.
> 
> Best regards,
> 
> Dalyoung
> 
> 
> 
> \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
> 
> \startbuffer[fig121]
> 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.fact(" & decimal n & ")/(P.fact(" & decimal r & 
> ")*P.fact(" & decimal s &" )))");
> 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 -- (now+A);
> draw now -- (now+B);
> endfor;
> endfor;
> \stopbuffer
Sunday afternoon educational moment (that you can wikify), four variants:

1 : A more metafunish alternative of your example.
2 : The same but avoiding a temporary variable.
3 : Less code and clutter, the real deal.
4 : Idem, but permits more tuning at the TeX end.

\unexpanded\def\MyWhatever#1#2#3%
   {$\displaystyle{#1\choose #2} = #3$}

\startluacode
local function fact(n)
     if n <= 0 then
         return 1
     else
         return n * fact(n - 1)
     end
end

local function whatever(n,r)
    return fact(n) / (fact(r) * fact(n-r))
end

MP.WhateverA = whatever

function MP.WhateverB(n,r)
    mp.quoted("%.0f",whatever(n,r))
end

function MP.WhateverC(n,r)
    mp.quoted([[$\displaystyle{%.0f\choose %.0f} = 
%.0f$]],n,r,whatever(n,r))
end

function MP.WhateverD(n,r)
    mp.quoted([[\MyWhatever{%.0f}{%.0f}{%.0f}]],n,r,whatever(n,r))
end
\stopluacode

\startbuffer[fig121]
     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;
             now := start + r*right*dx;
             draw (now + A) -- now -- (now + B);

             tt := lua.MP.WhateverA(n,r) ;

             dotlabel.top(textext("$\displaystyle {" & decimal n & 
"\choose" & decimal r & "} = "& decimal tt & "$"),now);

             dotlabel.top(textext("$\displaystyle {" & decimal n & 
"\choose" & decimal r & "} = "& lua.MP.WhateverB(n,r) & "$"),now);

             dotlabel.top(textext(lua.MP.WhateverC(n,r)),now);

             dotlabel.top(textext(lua.MP.WhateverD(n,r)),now);

         endfor;
     endfor;
\stopbuffer

\starttext

     {\switchtobodyfont[11pt]\processMPbuffer[fig121]}

\stoptext



-----------------------------------------------------------------
                                           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] 7+ messages in thread

* Re: running lua in metafun and in ConTeXt
  2020-04-11 17:08   ` Hans Hagen
@ 2020-04-11 23:19     ` Jeong Dal
  2020-04-12  9:44       ` Hans Hagen
  0 siblings, 1 reply; 7+ messages in thread
From: Jeong Dal @ 2020-04-11 23:19 UTC (permalink / raw)
  To: Hans Hagen; +Cc: mailing list for ConTeXt users

Dear Hans,

Thank you for your nice code.
lua.MP.Whatever is something!

Have a nice weekend.

Best regards,

Dalyoung

> 2020. 4. 12. 오전 2:08, Hans Hagen <j.hagen@xs4all.nl> 작성:
> 
> On 4/11/2020 4:15 PM, Jeong Dal wrote:
>> Dear all,
>> The problem is solved by using the namespace of lua as below:
>> I am not sure what I did is the right way.
>> If it is not the right way, please let me know.
>> Thanks for reading.
>> Best regards,
>> Dalyoung
>> \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
>> \startbuffer[fig121]
>> 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.fact(" & decimal n & ")/(P.fact(" & decimal r & ")*P.fact(" & decimal s &" )))");
>> 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 -- (now+A);
>> draw now -- (now+B);
>> endfor;
>> endfor;
>> \stopbuffer
> Sunday afternoon educational moment (that you can wikify), four variants:
> 
> 1 : A more metafunish alternative of your example.
> 2 : The same but avoiding a temporary variable.
> 3 : Less code and clutter, the real deal.
> 4 : Idem, but permits more tuning at the TeX end.
> 
> \unexpanded\def\MyWhatever#1#2#3%
>  {$\displaystyle{#1\choose #2} = #3$}
> 
> \startluacode
> local function fact(n)
>    if n <= 0 then
>        return 1
>    else
>        return n * fact(n - 1)
>    end
> end
> 
> local function whatever(n,r)
>   return fact(n) / (fact(r) * fact(n-r))
> end
> 
> MP.WhateverA = whatever
> 
> function MP.WhateverB(n,r)
>   mp.quoted("%.0f",whatever(n,r))
> end
> 
> function MP.WhateverC(n,r)
>   mp.quoted([[$\displaystyle{%.0f\choose %.0f} = %.0f$]],n,r,whatever(n,r))
> end
> 
> function MP.WhateverD(n,r)
>   mp.quoted([[\MyWhatever{%.0f}{%.0f}{%.0f}]],n,r,whatever(n,r))
> end
> \stopluacode
> 
> \startbuffer[fig121]
>    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;
>            now := start + r*right*dx;
>            draw (now + A) -- now -- (now + B);
> 
>            tt := lua.MP.WhateverA(n,r) ;
> 
>            dotlabel.top(textext("$\displaystyle {" & decimal n & "\choose" & decimal r & "} = "& decimal tt & "$"),now);
> 
>            dotlabel.top(textext("$\displaystyle {" & decimal n & "\choose" & decimal r & "} = "& lua.MP.WhateverB(n,r) & "$"),now);
> 
>            dotlabel.top(textext(lua.MP.WhateverC(n,r)),now);
> 
>            dotlabel.top(textext(lua.MP.WhateverD(n,r)),now);
> 
>        endfor;
>    endfor;
> \stopbuffer
> 
> \starttext
> 
>    {\switchtobodyfont[11pt]\processMPbuffer[fig121]}
> 
> \stoptext
> 
> 
> 
> -----------------------------------------------------------------
>                                          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] 7+ messages in thread

* Re: running lua in metafun and in ConTeXt
  2020-04-11 23:19     ` Jeong Dal
@ 2020-04-12  9:44       ` Hans Hagen
  2020-04-12 13:02         ` Jeong Dal
  0 siblings, 1 reply; 7+ messages in thread
From: Hans Hagen @ 2020-04-12  9:44 UTC (permalink / raw)
  To: Jeong Dal; +Cc: mailing list for ConTeXt users

Hi Dal,

> Thank you for your nice code.
> lua.MP.Whatever is something!

just keep in mind that lua.MP is the user namespace and lua.mp 
(lowercase) the system one

btw, as you're doing math in mp, in lmtx (which has some extra metafun 
features) we have some more math functions available:

m_acos m_acosh m_asin m_asinh m_atan m_atantwo m_atanh m_cbrt m_ceil 
m_copysign
m_cos m_cosh m_deg m_erf m_erfc m_exp m_exptwo m_expm m_fabs m_fdim 
m_floor m_fma
m_fmax m_fmin m_fmod m_frexp m_gamma m_hypot m_isfinite m_isinf m_isnan
m_isnormal m_jz m_j m_jn m_ldexp m_lgamma m_log m_logten m_logp m_logtwo 
m_logb
m_modf m_nearbyint m_nextafter m_pow m_rad m_remainder m_remquo m_round 
m_scalbn
m_sin m_sinh m_sqrt m_tan m_tanh m_tgamma m_trunc m_yz m_y m_yn

c_sin c_cos c_tan c_sinh c_cosh c_tanh c_asin c_acos c_atan c_asinh c_acosh
c_atanh c_sqrt c_abs c_arg c_conj c_exp c_log c_proj c_erf c_erfc 
c_erfcx c_erfi
c_imag c_real c_neg c_pow c_add c_sub c_mul c_div c_voigt c_voigt_hwhm 
c_imag
c_real c_neg

these are using the functions in the xmath and xcomplex namespaces that 
are provided in the lmtx lua interfaces

there's also an xdecimal namespace for using decimal calculations with 
high precisions but not yet interfaced to the mp end which actually 
already has decimal anyway

(if some matrix features are needed at the mp end, as extension to the 
matrix module, then we can explore that as we have ways to pick up data 
from mp, process it by lua, and pipe it back, which is also pretty 
efficient by now - not that mp graphics are much of a bottleneck)

Here is an example of a crude interfacve if you need indeed hundreds 
such exercises

\unexpanded\def\WhateverShape#1#2#3%
   {$\displaystyle{#1\choose #2} = #3$}

\startluacode
local function fact(n)
     if n <= 0 then
         return 1
     else
         return n * fact(n - 1)
     end
end

local function whatever(n,r)
     return fact(n) / (fact(r) * fact(n-r))
end

function MP.lmt_WhateverShape(n,r)
     mp.quoted([[\WhateverShape{%.0f}{%.0f}{%.0f}]],n,r,whatever(n,r))
end

-- or in latest lmtx:
--
-- function MP.lmt_WhateverShape(n,r)
--     mp.inject(
--         string.format(
--             [[\WhateverShape{%.0f}{%.0f}{%.0f}]],
--             n,
--             r,
--             whatever(n,r)
--         )
--     )
-- end
\stopluacode

\startMPextensions

presetparameters "WhateverShape" [
     options = "labels",
     angle   = 30,
     unit    = 2cm,
     color   = "black",
] ;

def WhateverShape = applyparameters "WhateverShape" "lmt_WhateverShape" 
enddef ;

vardef lmt_WhateverShape =
     image (

         pushparameters "WhateverShape" ;

             save u, a, b, dx, n, r ;
             save A, B, start, now ; pair A, B, start, now ;

             u  := getparameter "unit" ;
             a  := getparameter "angle" ;
             b  := a + 180 ;

             A  := u * dir(a) ;
             B  := u * dir(-b) ;
             dx := u * 2 * cosd(b) ;

             draw image (
                 for n = 0 upto 4 :
                     start := n * A ;
                     for r = 0 upto n :
                         now := start + r * right * dx ;
                         draw (now + A) -- now -- (now + B) ;
                     endfor ;
                 endfor ;
             ) withcolor getparameter "color" ;

             for n = 0 upto 4 :
                 start := n * A ;
                 for r = 0 upto n :
                     now := start + r * right * dx ;
                     if hasoption "options" "labels" :
 
dotlabel.top(textext(lua.MP.lmt_WhateverShape(n,r)),now) ;
                     fi ;
                 endfor;
             endfor;

         popparameters ;
     )
enddef ;

\stopMPextensions

\starttext

\startMPcode
     draw WhateverShape [
         angle = 30,
         unit  = 1.8cm,
         color = "darkgreen",
     ] ;
\stopMPcode

\startMPcode
     draw WhateverShape [
         options = "",
         angle   = 40,
         color   = "darkred",
         unit    = 4*BodyFontSize,
     ] ;
\stopMPcode

\stopMPcode

(parameters can be strings, numerics, booleans, pairs, paths, tables, etc)

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] 7+ messages in thread

* Re: running lua in metafun and in ConTeXt
  2020-04-12  9:44       ` Hans Hagen
@ 2020-04-12 13:02         ` Jeong Dal
  0 siblings, 0 replies; 7+ messages in thread
From: Jeong Dal @ 2020-04-12 13:02 UTC (permalink / raw)
  To: Hans Hagen; +Cc: mailing list for ConTeXt users

Dear Hans,

Thank you for your new code.

Frankly speaking, it is hard to follow your code right now because of my little knowledge about these matter.
I need some time  to understand your code fully, especially lmtx.
I’ll do  more exercises using your code.

Thank you for your concern.

Best regards,

Dalyoung


> 2020. 4. 12. 오후 6:44, Hans Hagen <j.hagen@xs4all.nl> 작성:
> 
> Hi Dal,
> 
>> Thank you for your nice code.
>> lua.MP.Whatever is something!
> 
> just keep in mind that lua.MP is the user namespace and lua.mp (lowercase) the system one
> 
> btw, as you're doing math in mp, in lmtx (which has some extra metafun features) we have some more math functions available:
> 
> m_acos m_acosh m_asin m_asinh m_atan m_atantwo m_atanh m_cbrt m_ceil m_copysign
> m_cos m_cosh m_deg m_erf m_erfc m_exp m_exptwo m_expm m_fabs m_fdim m_floor m_fma
> m_fmax m_fmin m_fmod m_frexp m_gamma m_hypot m_isfinite m_isinf m_isnan
> m_isnormal m_jz m_j m_jn m_ldexp m_lgamma m_log m_logten m_logp m_logtwo m_logb
> m_modf m_nearbyint m_nextafter m_pow m_rad m_remainder m_remquo m_round m_scalbn
> m_sin m_sinh m_sqrt m_tan m_tanh m_tgamma m_trunc m_yz m_y m_yn
> 
> c_sin c_cos c_tan c_sinh c_cosh c_tanh c_asin c_acos c_atan c_asinh c_acosh
> c_atanh c_sqrt c_abs c_arg c_conj c_exp c_log c_proj c_erf c_erfc c_erfcx c_erfi
> c_imag c_real c_neg c_pow c_add c_sub c_mul c_div c_voigt c_voigt_hwhm c_imag
> c_real c_neg
> 
> these are using the functions in the xmath and xcomplex namespaces that are provided in the lmtx lua interfaces
> 
> there's also an xdecimal namespace for using decimal calculations with high precisions but not yet interfaced to the mp end which actually already has decimal anyway
> 
> (if some matrix features are needed at the mp end, as extension to the matrix module, then we can explore that as we have ways to pick up data from mp, process it by lua, and pipe it back, which is also pretty efficient by now - not that mp graphics are much of a bottleneck)
> 
> Here is an example of a crude interfacve if you need indeed hundreds such exercises
> 
> \unexpanded\def\WhateverShape#1#2#3%
>  {$\displaystyle{#1\choose #2} = #3$}
> 
> \startluacode
> local function fact(n)
>    if n <= 0 then
>        return 1
>    else
>        return n * fact(n - 1)
>    end
> end
> 
> local function whatever(n,r)
>    return fact(n) / (fact(r) * fact(n-r))
> end
> 
> function MP.lmt_WhateverShape(n,r)
>    mp.quoted([[\WhateverShape{%.0f}{%.0f}{%.0f}]],n,r,whatever(n,r))
> end
> 
> -- or in latest lmtx:
> --
> -- function MP.lmt_WhateverShape(n,r)
> --     mp.inject(
> --         string.format(
> --             [[\WhateverShape{%.0f}{%.0f}{%.0f}]],
> --             n,
> --             r,
> --             whatever(n,r)
> --         )
> --     )
> -- end
> \stopluacode
> 
> \startMPextensions
> 
> presetparameters "WhateverShape" [
>    options = "labels",
>    angle   = 30,
>    unit    = 2cm,
>    color   = "black",
> ] ;
> 
> def WhateverShape = applyparameters "WhateverShape" "lmt_WhateverShape" enddef ;
> 
> vardef lmt_WhateverShape =
>    image (
> 
>        pushparameters "WhateverShape" ;
> 
>            save u, a, b, dx, n, r ;
>            save A, B, start, now ; pair A, B, start, now ;
> 
>            u  := getparameter "unit" ;
>            a  := getparameter "angle" ;
>            b  := a + 180 ;
> 
>            A  := u * dir(a) ;
>            B  := u * dir(-b) ;
>            dx := u * 2 * cosd(b) ;
> 
>            draw image (
>                for n = 0 upto 4 :
>                    start := n * A ;
>                    for r = 0 upto n :
>                        now := start + r * right * dx ;
>                        draw (now + A) -- now -- (now + B) ;
>                    endfor ;
>                endfor ;
>            ) withcolor getparameter "color" ;
> 
>            for n = 0 upto 4 :
>                start := n * A ;
>                for r = 0 upto n :
>                    now := start + r * right * dx ;
>                    if hasoption "options" "labels" :
> dotlabel.top(textext(lua.MP.lmt_WhateverShape(n,r)),now) ;
>                    fi ;
>                endfor;
>            endfor;
> 
>        popparameters ;
>    )
> enddef ;
> 
> \stopMPextensions
> 
> \starttext
> 
> \startMPcode
>    draw WhateverShape [
>        angle = 30,
>        unit  = 1.8cm,
>        color = "darkgreen",
>    ] ;
> \stopMPcode
> 
> \startMPcode
>    draw WhateverShape [
>        options = "",
>        angle   = 40,
>        color   = "darkred",
>        unit    = 4*BodyFontSize,
>    ] ;
> \stopMPcode
> 
> \stopMPcode
> 
> (parameters can be strings, numerics, booleans, pairs, paths, tables, etc)
> 
> 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] 7+ messages in thread

end of thread, other threads:[~2020-04-12 13:02 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <mailman.2082.1586557063.1332.ntg-context@ntg.nl>
2020-04-11 12:32 ` running lua in metafun and in ConTeXt Jeong Dal
2020-04-11 12:48   ` Otared Kavian
2020-04-11 14:15 ` Jeong Dal
2020-04-11 17:08   ` Hans Hagen
2020-04-11 23:19     ` Jeong Dal
2020-04-12  9:44       ` Hans Hagen
2020-04-12 13:02         ` Jeong Dal

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).