ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Hans Hagen <j.hagen@xs4all.nl>
To: Jeong Dal <haksan@me.com>
Cc: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: running lua in metafun and in ConTeXt
Date: Sun, 12 Apr 2020 11:44:36 +0200	[thread overview]
Message-ID: <549b5134-e8e7-f583-521a-a1b99b9f3852@xs4all.nl> (raw)
In-Reply-To: <BAE69044-E5FD-4BE5-8481-B10DD5DDC755@me.com>

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
___________________________________________________________________________________

  reply	other threads:[~2020-04-12  9:44 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [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
2020-04-11 17:08   ` Hans Hagen
2020-04-11 23:19     ` Jeong Dal
2020-04-12  9:44       ` Hans Hagen [this message]
2020-04-12 13:02         ` Jeong Dal

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=549b5134-e8e7-f583-521a-a1b99b9f3852@xs4all.nl \
    --to=j.hagen@xs4all.nl \
    --cc=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).