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 via email to