* How to use in Metafun, a function defined in Lua
@ 2019-12-28 15:22 Otared Kavian
2019-12-28 15:46 ` Hans Hagen
0 siblings, 1 reply; 2+ messages in thread
From: Otared Kavian @ 2019-12-28 15:22 UTC (permalink / raw)
To: mailing list for ConTeXt users
Hi all,
I would like to use a math function defined in lua in Metafun in order to draw its graph, together with the graphs of some other functions.
As a simple example consider the following:
%%% begin lua-metafun-example.tex
\startluacode
function document.myfunction(n,x)
local S
S = math.sin(n*x)
return S
end
\stopluacode
\starttext
Here \type{document.myfunction(n,x)} is $\sin(nx)$. For instance when $n = 2$ and $x = 355/113$ we have:
\type{document.myfunction(2,355/113)} $=\cldcontext{document.myfunction(2,355/113)} \sim 0$.
\startplacefigure[title={Nothing works\dots}]
\startMPcode{GridAxis}
numeric gu ; gu = 5mm ; % gu = graph unit
draw image (
draw function (1,"x","document.myfunction(2,x)", % <-- this does not work
% draw function (1,"x","sin(2*x)", % <-- this works
-355/113,355/113,.01)
scaled gu
withpen pencircle scaled .075pt
withcolor darkred ;
) xsized .7TextWidth ;
\stopMPcode
\stopplacefigure
\stoptext
%%% end lua-metafun-example.tex
Here one sees that the function « document.myfunction » is well defined in Lua and is known in ConTeXt, but I cannot use it in a Metapost code: how can one do that?
Thanks in advance for any help.
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] 2+ messages in thread
* Re: How to use in Metafun, a function defined in Lua
2019-12-28 15:22 How to use in Metafun, a function defined in Lua Otared Kavian
@ 2019-12-28 15:46 ` Hans Hagen
0 siblings, 0 replies; 2+ messages in thread
From: Hans Hagen @ 2019-12-28 15:46 UTC (permalink / raw)
To: mailing list for ConTeXt users, Otared Kavian
On 12/28/2019 4:22 PM, Otared Kavian wrote:
> Hi all,
>
> I would like to use a math function defined in lua in Metafun in order to draw its graph, together with the graphs of some other functions.
> As a simple example consider the following:
>
> %%% begin lua-metafun-example.tex
> \startluacode
> function document.myfunction(n,x)
> local S
> S = math.sin(n*x)
> return S
> end
> \stopluacode
>
> \starttext
>
> Here \type{document.myfunction(n,x)} is $\sin(nx)$. For instance when $n = 2$ and $x = 355/113$ we have:
>
> \type{document.myfunction(2,355/113)} $=\cldcontext{document.myfunction(2,355/113)} \sim 0$.
>
> \startplacefigure[title={Nothing works\dots}]
> \startMPcode{GridAxis}
> numeric gu ; gu = 5mm ; % gu = graph unit
> draw image (
> draw function (1,"x","document.myfunction(2,x)", % <-- this does not work
> % draw function (1,"x","sin(2*x)", % <-- this works
> -355/113,355/113,.01)
> scaled gu
> withpen pencircle scaled .075pt
> withcolor darkred ;
> ) xsized .7TextWidth ;
> \stopMPcode
> \stopplacefigure
>
> \stoptext
> %%% end lua-metafun-example.tex
>
> Here one sees that the function « document.myfunction » is well defined in Lua and is known in ConTeXt, but I cannot use it in a Metapost code: how can one do that?
you confuse lua and mp functions
\startluacode
function MP.myfunction(n,x)
return math.sin(n*x)
end
\stopluacode
\starttext
\startMPcode
numeric gu ; gu = 5mm ;
vardef myfunction(expr n, x) = MP.myfunction(n,x) enddef ;
draw image (
draw function (1,"x","myfunction(2,x)",-355/113,355/113,.01)
scaled gu
withpen pencircle scaled .075pt
withcolor darkred ;
) xsized .7TextWidth ;
\stopMPcode
\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] 2+ messages in thread
end of thread, other threads:[~2019-12-28 15:46 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-28 15:22 How to use in Metafun, a function defined in Lua Otared Kavian
2019-12-28 15:46 ` Hans Hagen
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).