Re: [NTG-context] Implicit plots/level curves possible?
On Tue, 9 Oct 2018, Alan Braslau wrote: On Mon, 8 Oct 2018 17:56:24 -0400 (EDT) Aditya Mahajan wrote: Here is a proof of concept implementation in Lua + MP so that you can use: \ContourPlot [ function=2*x^5 + x*y + y^5, x={0, 2}, y={-2, 0.5}, n=1000, % Number of discretization points ] The code is fairly fast. But be careful. As with all ConTeXt key-value assignment, `x = { ...}` is different from `x={...}`. I am being a bit lazy here, and haven't adapted the metapost code to draw the axes to adapt to the function. Hans and I played with Aditya's demonstration, to complete the example. It demonstrates some fun lua+MP+ConTeXt tricks: Looking at lua-mplib.mpvi, isn't it better to use MP namespace (instead of mp) for user defined functions? The metafun manual says that one can omit the prefix `lua` and simply use `mp` or `MP`. That does not seem to work. Aditya ___ 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 ___
Re: [NTG-context] Implicit plots/level curves possible?
On Mon, 8 Oct 2018 17:56:24 -0400 (EDT) Aditya Mahajan wrote: > Here is a proof of concept implementation in Lua + MP so that you can use: > > \ContourPlot >[ > function=2*x^5 + x*y + y^5, > x={0, 2}, > y={-2, 0.5}, > n=1000, % Number of discretization points >] > > The code is fairly fast. But be careful. As with all ConTeXt key-value > assignment, `x = { ...}` is different from `x={...}`. I am being a bit > lazy here, and haven't adapted the metapost code to draw the axes to adapt > to the function. Hans and I played with Aditya's demonstration, to complete the example. It demonstrates some fun lua+MP+ConTeXt tricks: Alan \startluacode userdata = userdata or { } userdata.contour = { } userdata.xlim= { 0, 0 } userdata.ylim= { 0, 0 } function userdata.contourplot(f, xlim, ylim, length, ef) local xmin, xmax = xlim[1], xlim[2] local ymin, ymax = ylim[1], ylim[2] local t = { } local n = 0 for x = xmin, xmax, (xmax - xmin)/length do for y = ymin, ymax, (ymax - ymin)/length do local e = ef(x,y) local z = f(x,y) if z < e and z > -e then n = n + 1 t[n] = { x, y } end end end userdata.xlim= xlim userdata.ylim= ylim userdata.contour = t end function mp.ContourPath() mp.path(userdata.contour) end function mp.ContourX() mp.pair(userdata.xlim) end function mp.ContourY() mp.pair(userdata.ylim) end \stopluacode \startuseMPgraphic{doublefun::ContourPlot}{width} save xmin, xmax, ymin, ymax ; (xmin, xmax) = lua.mp.ContourX() ; (ymin, ymax) = lua.mp.ContourY() ; draw lua.mp.ContourPath() withpen pencircle scaled ((xmax-xmin)/200) ; setbounds currentpicture to boundingbox ((xmin,ymin)--(xmax,ymax)); currentpicture := currentpicture xsized \MPvar{width} ; save pic ; picture pic ; pic := currentpicture ; drawarrow llcorner pic--lrcorner pic ; drawarrow llcorner pic--ulcorner pic ; label.rt ("$x$", lrcorner pic) ; label.top("$y$", ulcorner pic) ; label.bot(decimal xmin,llcorner pic) ; label.bot(decimal xmax,lrcorner pic) ; label.lft(decimal ymin,llcorner pic) ; label.lft(decimal ymax,ulcorner pic) ; \stopuseMPgraphic \unexpanded\def\ContourPlot {\dosingleempty\doContourPlot} \def\doContourPlot[#1]% {\setvariables [ContourPlot] [x={0,0}, y={0,0}, w=10cm, n=1000, e=1e-2, #1]% \ctxlua{userdata.contourplot( function(x,y) return \getvariable{ContourPlot}{function} end, {\getvariable{ContourPlot}{x}},{\getvariable{ContourPlot}{y}}, \getvariable{ContourPlot}{n}, function(x,y) return \getvariable{ContourPlot}{e} end )}% \useMPgraphic{doublefun::ContourPlot}{width=\getvariable{ContourPlot}{w}}} \starttext \ContourPlot [function={2*x^5 + x*y + y^5}, x={0,2},y={-2,0.5}, n=1000,e={x/1000},w=5cm] \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 ___
Re: [NTG-context] Implicit plots/level curves possible?
Wow! This is cool! I must admit I do not get how the connections between MetaPost and Lua works, but it seem to work fine. Challenge: To do this for a more complicated closed and self-intersecting curve like the Bernoulli lemniscate, say (x^2+y^2)^2=x^2-y^2 (a good domain could be -1.5 wrote: > On 10/8/2018 11:25 PM, Aditya Mahajan wrote: > > On Mon, 8 Oct 2018, Alan Braslau wrote: > > > >> On Mon, 8 Oct 2018 16:00:10 -0400 (EDT) > >> Aditya Mahajan wrote: > >> > >>> On Sun, 7 Oct 2018, Hans Hagen wrote: > >>> > On 10/7/2018 7:14 PM, Alan Braslau wrote: > > On Sun, 7 Oct 2018 17:25:35 +0200 > > "Mikael P. Sundqvist" wrote: > > > >> ContourPlot[2 x^5 + x y + y^5 == 0, {x, 0, 2}, {y, -2, 1/2}] > > > > Brut force: > > [...] > > > as this takes some time here's a cheat: > > \starttext > > \startbuffer[demo] > [...] > \stopbuffer > > \startTEXpage > \typesetbuffer[demo] > \stopTEXpage > \stoptext > > a next run the already prepared buffer will be taken unles it has been > changed. > >>> > >>> I thought that this will also be a good usecase of showing Lua+MP > >>> interaction. I wrote the code below following the metafun manual, but > >>> I cannot > >>> get it to compile. What am I missing? > >>> > >>> > >> You need to put it into the mp namespace: > > > > Thanks. The metafun manual is confusing in this regard and I got the > > impression that any lua namespace could be used. > > > >> (then eps should be made a linear function of xi) > > > > Oh, I missed that. Thanks. > > > >> (and, indeed this is much faster than calculating in MP) > > > > Yes. Iterating over 10^6 values on a 1GHz computer should roughly take > > hm, my 3.4 gig laptop cpu needs 0.18 sec for the slightly more that i > million steps ... it depends on the function too > > > 1ms in any reasonable programming language. Metapost for loops work with > > macro expansion, which can be very expensive for large loops. > > generalized: > > \startluacode > - abs = math.abs > > local contour = { } > local data= { } > local origin = { 0, 0 } > local length = 0 > > local function generate(f, x_min, x_max, y_min, y_max) > local points = { } > local length = 1000 > local eps= 1e-3 > local spe= -eps > local n = 0 > local code = "return function(x,y) return " .. f .. " end" > local action = load(code) > if action then > action = action() > end > for xi = x_min, x_max, (x_max - x_min)/length do > for yi = y_min, y_max, (y_max - y_min)/length do > -- if abs(action(xi,yi)) < eps then -- 10% gain with: > local v = action(xi,yi) > if v < eps and v > spe then > n = n + 1 > points[n] = { xi, yi } > end > end > end > return points, n > end > > function mp.Countour(...) > data, length = generate(...) > end > > function mp.ContourN() > mp.print(length) > end > > function mp.ContourPoint(i) > mp.pair(data[i] or origin) > end > > function mp.ContourPath(f,...) > if f then > data, length = generate(f,...) > end > mp.path(length > 0 and data or origin) > end > > \stopluacode > > \starttext > \startMPpage[instance=doublefun] >% lua.mp.Countour("2*x^5 + x*y + y^5", 0, 2, -1, 0.5) ; >% draw lua.mp.ContourPath() withpen pencircle scaled .01 ; > draw lua.mp.ContourPath("2*x^5 + x*y + y^5", 0, 2, -1, 0.5) > withpen pencircle scaled .01 ; > setbounds currentpicture to (0,-2) -- (2,-2) -- (2,.5) -- > (0,.5) -- cycle ; > currentpicture := currentpicture xsized 5cm ; > picture pic ; pic := currentpicture ; > drawarrow llcorner pic -- lrcorner pic ; > drawarrow llcorner pic -- ulcorner pic ; > label.rt ("$x$", lrcorner pic) ; > label.top("$y$", ulcorner pic) ; > for x=0 step .5 until 2 : > label.bot(decimal x,(x/2)[llcorner pic,lrcorner pic]) ; > endfor ; > for y=0 step .5 until 2.5 : > label.lft(decimal (y-2),(y/2.5)[llcorner pic,ulcorner pic]) ; > endfor ; > \stopMPpage > \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-mirr
Re: [NTG-context] Implicit plots/level curves possible?
On 10/8/2018 11:25 PM, Aditya Mahajan wrote: On Mon, 8 Oct 2018, Alan Braslau wrote: On Mon, 8 Oct 2018 16:00:10 -0400 (EDT) Aditya Mahajan wrote: On Sun, 7 Oct 2018, Hans Hagen wrote: On 10/7/2018 7:14 PM, Alan Braslau wrote: On Sun, 7 Oct 2018 17:25:35 +0200 "Mikael P. Sundqvist" wrote: ContourPlot[2 x^5 + x y + y^5 == 0, {x, 0, 2}, {y, -2, 1/2}] Brut force: [...] as this takes some time here's a cheat: \starttext \startbuffer[demo] [...] \stopbuffer \startTEXpage \typesetbuffer[demo] \stopTEXpage \stoptext a next run the already prepared buffer will be taken unles it has been changed. I thought that this will also be a good usecase of showing Lua+MP interaction. I wrote the code below following the metafun manual, but I cannot get it to compile. What am I missing? You need to put it into the mp namespace: Thanks. The metafun manual is confusing in this regard and I got the impression that any lua namespace could be used. (then eps should be made a linear function of xi) Oh, I missed that. Thanks. (and, indeed this is much faster than calculating in MP) Yes. Iterating over 10^6 values on a 1GHz computer should roughly take hm, my 3.4 gig laptop cpu needs 0.18 sec for the slightly more that i million steps ... it depends on the function too 1ms in any reasonable programming language. Metapost for loops work with macro expansion, which can be very expensive for large loops. generalized: \startluacode - abs = math.abs local contour = { } local data= { } local origin = { 0, 0 } local length = 0 local function generate(f, x_min, x_max, y_min, y_max) local points = { } local length = 1000 local eps= 1e-3 local spe= -eps local n = 0 local code = "return function(x,y) return " .. f .. " end" local action = load(code) if action then action = action() end for xi = x_min, x_max, (x_max - x_min)/length do for yi = y_min, y_max, (y_max - y_min)/length do -- if abs(action(xi,yi)) < eps then -- 10% gain with: local v = action(xi,yi) if v < eps and v > spe then n = n + 1 points[n] = { xi, yi } end end end return points, n end function mp.Countour(...) data, length = generate(...) end function mp.ContourN() mp.print(length) end function mp.ContourPoint(i) mp.pair(data[i] or origin) end function mp.ContourPath(f,...) if f then data, length = generate(f,...) end mp.path(length > 0 and data or origin) end \stopluacode \starttext \startMPpage[instance=doublefun] % lua.mp.Countour("2*x^5 + x*y + y^5", 0, 2, -1, 0.5) ; % draw lua.mp.ContourPath() withpen pencircle scaled .01 ; draw lua.mp.ContourPath("2*x^5 + x*y + y^5", 0, 2, -1, 0.5) withpen pencircle scaled .01 ; setbounds currentpicture to (0,-2) -- (2,-2) -- (2,.5) -- (0,.5) -- cycle ; currentpicture := currentpicture xsized 5cm ; picture pic ; pic := currentpicture ; drawarrow llcorner pic -- lrcorner pic ; drawarrow llcorner pic -- ulcorner pic ; label.rt ("$x$", lrcorner pic) ; label.top("$y$", ulcorner pic) ; for x=0 step .5 until 2 : label.bot(decimal x,(x/2)[llcorner pic,lrcorner pic]) ; endfor ; for y=0 step .5 until 2.5 : label.lft(decimal (y-2),(y/2.5)[llcorner pic,ulcorner pic]) ; endfor ; \stopMPpage \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 ___
Re: [NTG-context] Implicit plots/level curves possible?
On Mon, 8 Oct 2018, Aditya Mahajan wrote: On Mon, 8 Oct 2018, Alan Braslau wrote: On Mon, 8 Oct 2018 16:00:10 -0400 (EDT) Aditya Mahajan wrote: On Sun, 7 Oct 2018, Hans Hagen wrote: On 10/7/2018 7:14 PM, Alan Braslau wrote: On Sun, 7 Oct 2018 17:25:35 +0200 "Mikael P. Sundqvist" wrote: ContourPlot[2 x^5 + x y + y^5 == 0, {x, 0, 2}, {y, -2, 1/2}] Here is a proof of concept implementation in Lua + MP so that you can use: \ContourPlot [ function=2*x^5 + x*y + y^5, x={0, 2}, y={-2, 0.5}, n=1000, % Number of discretization points ] The code is fairly fast. But be careful. As with all ConTeXt key-value assignment, `x = { ...}` is different from `x={...}`. I am being a bit lazy here, and haven't adapted the metapost code to draw the axes to adapt to the function. \define\ContourPlot {\dosingleargument\doContourPlot} \def\doContourPlot[#1]% {\setvariables[ContourPlot][#1]% \ctxlua{userdata.contourplot( function(x,y) return \getvariable{ContourPlot}{function} end, {\getvariable{ContourPlot}{x}}, {\getvariable{ContourPlot}{y}}, \getvariable{ContourPlot}{n})}% \useMPgraphic{doublefun::ContourPlot}} \startluacode userdata = userdata or { } local abs = math.abs local data = { } local eps = 1e-3 function userdata.contourplot(f, xlim, ylim, length) local n = 0 data= { } for x = xlim[1], xlim[2], (xlim[2] - xlim[1])/length do for y = ylim[1], ylim[2], (ylim[2] - ylim[1])/length do if abs(f(x,y)) < eps*x then n = n + 1 data[n] = {x, y} end end end end function mp.ContourPath() mp.path(data) end \stopluacode \startuseMPgraphic{doublefun::ContourPlot} draw lua.mp.ContourPath() withpen pencircle scaled .01 ; % This needs to be fixed to adapt to the function. setbounds currentpicture to (0,-2)--(2,-2)--(2,.5)--(0,.5)--cycle ; currentpicture := currentpicture xsized 5cm ; picture pic ; pic := currentpicture ; drawarrow llcorner pic--lrcorner pic ; drawarrow llcorner pic--ulcorner pic ; label.rt ("$x$", lrcorner pic) ; label.top("$y$", ulcorner pic) ; for x=0 step .5 until 2 : label.bot(decimal x,(x/2)[llcorner pic,lrcorner pic]) ; endfor ; for y=0 step .5 until 2.5 : label.lft(decimal (y-2),(y/2.5)[llcorner pic,ulcorner pic]) ; endfor ; \stopuseMPgraphic \starttext \ContourPlot [ function=2*x^5 + x*y + y^5, x={0, 2}, y={-2, 0.5}, n=1000, ] \stoptext \endinput \starttext \startMPpage[instance=doublefun] pen savedpen ; savedpen := currentpen ; pickup pencircle scaled .01 ; p := for i = 1 upto lua.contour.n() : lua.contour.point(i) .. endfor cycle; draw subpath (0,length p - 1) of p ; setbounds currentpicture to (0,-2)--(2,-2)--(2,.5)--(0,.5)--cycle ; currentpicture := currentpicture xsized 5cm ; pickup savedpen ; picture pic ; pic := currentpicture ; drawarrow llcorner pic--lrcorner pic ; drawarrow llcorner pic--ulcorner pic ; label.rt ("$x$", lrcorner pic) ; label.top("$y$", ulcorner pic) ; for x=0 step .5 until 2 : label.bot(decimal x,(x/2)[llcorner pic,lrcorner pic]) ; endfor ; for y=0 step .5 until 2.5 : label.lft(decimal (y-2),(y/2.5)[llcorner pic,ulcorner pic]) ; endfor ; \stopMPpage \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 ___
Re: [NTG-context] Implicit plots/level curves possible?
On Mon, 8 Oct 2018 17:25:24 -0400 (EDT) Aditya Mahajan wrote: > > (then eps should be made a linear function of xi) > > Oh, I missed that. Thanks. This is because the function is O(xy) for small x so one will get oscillations in the curve (multiple hits in the grid search) otherwise. Alan ___ 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 ___
Re: [NTG-context] Implicit plots/level curves possible?
On Mon, 8 Oct 2018, Alan Braslau wrote: On Mon, 8 Oct 2018 16:00:10 -0400 (EDT) Aditya Mahajan wrote: On Sun, 7 Oct 2018, Hans Hagen wrote: On 10/7/2018 7:14 PM, Alan Braslau wrote: On Sun, 7 Oct 2018 17:25:35 +0200 "Mikael P. Sundqvist" wrote: ContourPlot[2 x^5 + x y + y^5 == 0, {x, 0, 2}, {y, -2, 1/2}] Brut force: [...] as this takes some time here's a cheat: \starttext \startbuffer[demo] [...] \stopbuffer \startTEXpage \typesetbuffer[demo] \stopTEXpage \stoptext a next run the already prepared buffer will be taken unles it has been changed. I thought that this will also be a good usecase of showing Lua+MP interaction. I wrote the code below following the metafun manual, but I cannot get it to compile. What am I missing? You need to put it into the mp namespace: Thanks. The metafun manual is confusing in this regard and I got the impression that any lua namespace could be used. (then eps should be made a linear function of xi) Oh, I missed that. Thanks. (and, indeed this is much faster than calculating in MP) Yes. Iterating over 10^6 values on a 1GHz computer should roughly take 1ms in any reasonable programming language. Metapost for loops work with macro expansion, which can be very expensive for large loops. Aditya ___ 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 ___
Re: [NTG-context] Implicit plots/level curves possible?
On Mon, 8 Oct 2018 16:00:10 -0400 (EDT) Aditya Mahajan wrote: > On Sun, 7 Oct 2018, Hans Hagen wrote: > > > On 10/7/2018 7:14 PM, Alan Braslau wrote: > >> On Sun, 7 Oct 2018 17:25:35 +0200 > >> "Mikael P. Sundqvist" wrote: > >> > >>> ContourPlot[2 x^5 + x y + y^5 == 0, {x, 0, 2}, {y, -2, 1/2}] > >> > >> Brut force: > >> [...] > >> > > as this takes some time here's a cheat: > > > > \starttext > > > > \startbuffer[demo] > > [...] > > \stopbuffer > > > > \startTEXpage > > \typesetbuffer[demo] > > \stopTEXpage > > \stoptext > > > > a next run the already prepared buffer will be taken unles it has been > > changed. > > I thought that this will also be a good usecase of showing Lua+MP > interaction. I wrote the code below following the metafun manual, but I cannot > get it to compile. What am I missing? > > > \startluacode > local f = function(x, y) return 2*x^5 + x*y + y^5 end > local abs = math.abs > > contour = { } > > function contour.generate(x_min, x_max, y_min, y_max) >local pts = { } >local length = 1000 >local eps = 1e-3 > >for xi = x_min, x_max, (x_max - x_min)/length do >for yi = y_min, y_max, (y_max - y_min)/length do >if abs(f(xi,yi)) < eps then > pts[#pts + 1] = {xi, yi} >end >end >end > >return pts > end > > contour.data = contour.generate(0, 2, -1, 0.5) > > function contour.n() > mp.print(#contour.data) > end > > function contour.point(i) > mp.pair(contour.data[i]) > end > > \stopluacode > > \starttext > \startMPpage[instance=doublefun] > pen savedpen ; savedpen := currentpen ; > pickup pencircle scaled .01 ; > > p := for i = 1 upto lua.contour.n() : >lua.contour.point(i) .. > endfor cycle; > > draw subpath (0,length p - 1) of p ; > setbounds currentpicture to (0,-2)--(2,-2)--(2,.5)--(0,.5)--cycle ; > currentpicture := currentpicture xsized 5cm ; > pickup savedpen ; > picture pic ; pic := currentpicture ; > drawarrow llcorner pic--lrcorner pic ; > drawarrow llcorner pic--ulcorner pic ; > label.rt ("$x$", lrcorner pic) ; > label.top("$y$", ulcorner pic) ; > for x=0 step .5 until 2 : > label.bot(decimal x,(x/2)[llcorner pic,lrcorner pic]) ; > endfor ; > for y=0 step .5 until 2.5 : > label.lft(decimal (y-2),(y/2.5)[llcorner pic,ulcorner pic]) ; > endfor ; > \stopMPpage > \stoptext > > > I get an error: > > > ! Missing argument to lua. > > contour > <*> ...caled .01 ; p := for i = 1 upto lua.contour > .n() : lua.contour.point(i... > That macro has more parameters than you thought. > I'll continue by pretending that each missing argument > is either zero or null. > > > Thanks, > Aditya You need to put it into the mp namespace: (then eps should be made a linear function of xi) (and, indeed this is much faster than calculating in MP) Alan \startluacode local f = function(x, y) return 2*x^5 + x*y + y^5 end local abs = math.abs local contour = { } function contour.generate(x_min, x_max, y_min, y_max) local pts = { } local length = 1000 local eps = 1e-3 local n = 0 for xi = x_min, x_max, (x_max - x_min)/length do for yi = y_min, y_max, (y_max - y_min)/length do if abs(f(xi,yi)) < eps then n = n + 1 pts[n] = {xi, yi} end end end return pts end contour.data = contour.generate(0, 2, -1, 0.5) function mp.ContourN() mp.print(#contour.data) end function mp.ContourPoint(i) mp.pair(contour.data[i]) end function mp.ContourPath() mp.path(contour.data) end \stopluacode \starttext \startMPpage[instance=doublefun] draw lua.mp.ContourPath() withpen pencircle scaled .01 ; setbounds currentpicture to (0,-2)--(2,-2)--(2,.5)--(0,.5)--cycle ; currentpicture := currentpicture xsized 5cm ; picture pic ; pic := currentpicture ; drawarrow llcorner pic--lrcorner pic ; drawarrow llcorner pic--ulcorner pic ; label.rt ("$x$", lrcorner pic) ; label.top("$y$", ulcorner pic) ; for x=0 step .5 until 2 : label.bot(decimal x,(x/2)[llcorner pic,lrcorner pic]) ; endfor ; for y=0 step .5 until 2.5 : label.lft(decimal (y-2),(y/2.5)[llcorner pic,ulcorner pic]) ; endfor ; \stopMPpage \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 ___
Re: [NTG-context] Implicit plots/level curves possible?
On Sun, 7 Oct 2018, Hans Hagen wrote: On 10/7/2018 7:14 PM, Alan Braslau wrote: On Sun, 7 Oct 2018 17:25:35 +0200 "Mikael P. Sundqvist" wrote: ContourPlot[2 x^5 + x y + y^5 == 0, {x, 0, 2}, {y, -2, 1/2}] Brut force: [...] as this takes some time here's a cheat: \starttext \startbuffer[demo] [...] \stopbuffer \startTEXpage \typesetbuffer[demo] \stopTEXpage \stoptext a next run the already prepared buffer will be taken unles it has been changed. I thought that this will also be a good usecase of showing Lua+MP interaction. I wrote the code below following the metafun manual, but I cannot get it to compile. What am I missing? \startluacode local f = function(x, y) return 2*x^5 + x*y + y^5 end local abs = math.abs contour = { } function contour.generate(x_min, x_max, y_min, y_max) local pts = { } local length = 1000 local eps = 1e-3 for xi = x_min, x_max, (x_max - x_min)/length do for yi = y_min, y_max, (y_max - y_min)/length do if abs(f(xi,yi)) < eps then pts[#pts + 1] = {xi, yi} end end end return pts end contour.data = contour.generate(0, 2, -1, 0.5) function contour.n() mp.print(#contour.data) end function contour.point(i) mp.pair(contour.data[i]) end \stopluacode \starttext \startMPpage[instance=doublefun] pen savedpen ; savedpen := currentpen ; pickup pencircle scaled .01 ; p := for i = 1 upto lua.contour.n() : lua.contour.point(i) .. endfor cycle; draw subpath (0,length p - 1) of p ; setbounds currentpicture to (0,-2)--(2,-2)--(2,.5)--(0,.5)--cycle ; currentpicture := currentpicture xsized 5cm ; pickup savedpen ; picture pic ; pic := currentpicture ; drawarrow llcorner pic--lrcorner pic ; drawarrow llcorner pic--ulcorner pic ; label.rt ("$x$", lrcorner pic) ; label.top("$y$", ulcorner pic) ; for x=0 step .5 until 2 : label.bot(decimal x,(x/2)[llcorner pic,lrcorner pic]) ; endfor ; for y=0 step .5 until 2.5 : label.lft(decimal (y-2),(y/2.5)[llcorner pic,ulcorner pic]) ; endfor ; \stopMPpage \stoptext I get an error: ! Missing argument to lua. contour <*> ...caled .01 ; p := for i = 1 upto lua.contour .n() : lua.contour.point(i... That macro has more parameters than you thought. I'll continue by pretending that each missing argument is either zero or null. Thanks, Aditya ___ 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 ___
Re: [NTG-context] Implicit plots/level curves possible?
On 10/7/2018 7:14 PM, Alan Braslau wrote: On Sun, 7 Oct 2018 17:25:35 +0200 "Mikael P. Sundqvist" wrote: ContourPlot[2 x^5 + x y + y^5 == 0, {x, 0, 2}, {y, -2, 1/2}] Brut force: \startTEXpage \startMPcode {doublefun} pen savedpen ; savedpen := currentpen ; pickup pencircle scaled .01 ; path p ; p := for i=0 upto 1000 : for j=0 upto 1000 : hide(x := 2i/1000 ; y := 2.5j/1000 - 2 ;) if abs(2*(x**5)+x*y+y**5) < .002i/1000 : (x,y) .. fi endfor endfor cycle ; draw subpath (0,length p - 1) of p ; setbounds currentpicture to (0,-2)--(2,-2)--(2,.5)--(0,.5)--cycle ; currentpicture := currentpicture xsized 5cm ; pickup savedpen ; picture pic ; pic := currentpicture ; drawarrow llcorner pic--lrcorner pic ; drawarrow llcorner pic--ulcorner pic ; label.rt ("$x$", lrcorner pic) ; label.top("$y$", ulcorner pic) ; for x=0 step .5 until 2 : label.bot(decimal x,(x/2)[llcorner pic,lrcorner pic]) ; endfor for y=0 step .5 until 2.5 : label.lft(decimal (y-2),(y/2.5)[llcorner pic,ulcorner pic]) ; endfor \stopMPcode \stopTEXpage as this takes some time here's a cheat: \starttext \startbuffer[demo] % when an environment is used, load it here \startMPcode {doublefun} pen savedpen ; savedpen := currentpen ; pickup pencircle scaled .01 ; numeric stp ; stp := 1 ; path p ; p := for i=0 step stp until 1000 : for j=0 step stp until 1000 : hide(x := 2i/1000 ; y := 2.5j/1000 - 2 ;) if abs(2*(x**5)+x*y+y**5) < .002i/1000 : (x,y) .. fi endfor endfor cycle ; draw subpath (0,length p - 1) of p ; setbounds currentpicture to (0,-2)--(2,-2)--(2,.5)--(0,.5)--cycle ; currentpicture := currentpicture xsized 5cm ; pickup savedpen ; picture pic ; pic := currentpicture ; drawarrow llcorner pic--lrcorner pic ; drawarrow llcorner pic--ulcorner pic ; label.rt ("$x$", lrcorner pic) ; label.top("$y$", ulcorner pic) ; for x=0 step .5 until 2 : label.bot(decimal x,(x/2)[llcorner pic,lrcorner pic]) ; endfor ; for y=0 step .5 until 2.5 : label.lft(decimal (y-2),(y/2.5)[llcorner pic,ulcorner pic]) ; endfor ; \stopMPcode \stopbuffer \startTEXpage \typesetbuffer[demo] \stopTEXpage \stoptext a next run the already prepared buffer will be taken unles it has been changed. 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 ___
Re: [NTG-context] Implicit plots/level curves possible?
On Sun, Oct 7, 2018 at 7:14 PM Alan Braslau wrote: > > On Sun, 7 Oct 2018 17:25:35 +0200 > "Mikael P. Sundqvist" wrote: > > > ContourPlot[2 x^5 + x y + y^5 == 0, {x, 0, 2}, {y, -2, 1/2}] > > Brut force: > > \startTEXpage > \startMPcode {doublefun} > pen savedpen ; savedpen := currentpen ; > pickup pencircle scaled .01 ; > path p ; > p := for i=0 upto 1000 : >for j=0 upto 1000 : > hide(x := 2i/1000 ; y := 2.5j/1000 - 2 ;) > if abs(2*(x**5)+x*y+y**5) < .002i/1000 : (x,y) .. fi >endfor > endfor cycle ; > draw subpath (0,length p - 1) of p ; > setbounds currentpicture to (0,-2)--(2,-2)--(2,.5)--(0,.5)--cycle ; > currentpicture := currentpicture xsized 5cm ; > pickup savedpen ; > > picture pic ; pic := currentpicture ; > drawarrow llcorner pic--lrcorner pic ; > drawarrow llcorner pic--ulcorner pic ; > label.rt ("$x$", lrcorner pic) ; > label.top("$y$", ulcorner pic) ; > for x=0 step .5 until 2 : > label.bot(decimal x,(x/2)[llcorner pic,lrcorner pic]) ; > endfor > for y=0 step .5 until 2.5 : > label.lft(decimal (y-2),(y/2.5)[llcorner pic,ulcorner pic]) ; > endfor > \stopMPcode > \stopTEXpage > > > Alan Thanks both to luigi and to Alan! I'm impressed by your brute force method, Alan! In particular it learned me the "hide" command. I feel a bit ashamed. Earlier today I answered something else giving a reference to the metafun manual. Now I'm here not reading it carefully enough myself. In the helpers section it is clearly written how to import and plot data. Thus, I exported the data from the Mathematica plot (that file is pasted below in case anyone wants to try this themselves, sorry in advance for the big amount of text it generates in this email) and then importing it just worked fine. I like Alan's method very much, but it takes some time during compilation, so I will probably stick with importing. Working code below (where one can see that Alan's method is indeed working fine, at least with this example). Again, thanks to both luigi and Alan! \starttext \startTEXpage[offset=2bp] \startMPcode{doublefun} pen savedpen ; savedpen := currentpen ; pickup pencircle scaled .01 ; lua("MP = { } MP.data = table.load('mmadata.txt')") ; numeric n ; lua("mp.print('n := ',\#MP.data)") ; path mycurve; mycurve := lua("mp.pair(MP.data[1])") for i=2 upto n : .. lua("mp.pair(MP.data[" & decimal i & "])") endfor; draw mycurve withcolor darkred withpen pencircle scaled 0.04; path p ; p := for i=0 upto 1000 : for j=0 upto 1000 : hide(x := 2i/1000 ; y := 2.5j/1000 - 2 ;) if abs(2*(x**5)+x*y+y**5) < .002i/1000 : (x,y) .. fi endfor endfor cycle ; draw subpath (0,length p - 1) of p ; setbounds currentpicture to (0,-2)--(2,-2)--(2,.5)--(0,.5)--cycle ; currentpicture := currentpicture xsized 5cm ; pickup savedpen ; picture pic ; pic := currentpicture ; drawarrow llcorner pic--lrcorner pic ; drawarrow llcorner pic--ulcorner pic ; label.rt ("$x$", lrcorner pic) ; label.top("$y$", ulcorner pic) ; for x=0 step .5 until 2 : label.bot(decimal x,(x/2)[llcorner pic,lrcorner pic]) ; endfor for y=0 step .5 until 2.5 : label.lft(decimal (y-2),(y/2.5)[llcorner pic,ulcorner pic]) ; endfor \stopMPcode \stopTEXpage \stoptext %%% mmadata.txt %%% return {{1.7781332646374535, -2.0}, {1.7768209908069152, -1.9983615165933983}, {1.775510204081634, -1.9967215415115265}, {1.7664030082433901, -1.985873790716172}, {1.756875567501, -1.9744897959183674}, {1.7559890220139953, -1.973381069421283}, {1.7551020408163278, -1.9722667987462739}, {1.7455791311458495, -1.9608832289248324}, {1.7356495451060165, -1.9489795918367347}, {1.7351734376993888, -1.9483801416512756}, {1.7346938775510217, -1.9477751961654046}, {1.7247720468786834, -1.9358716760955252}, {1.714463398255413, -1.9234693877551021}, {1.7142857142857155, -1.9232445076564075}, {1.7093814232165505, -1.9173390239186459}, {1.7039827159442558, -1.910837931600294}, {1.6938775510204094, -1.8986359463002893}, {1.6935950869756253, -1.8983122637294494}, {1.6932881048717179, -1.8979591836734695}, {1.683212201778606, -1.8857806661440912}, {1.6734693877551032, -1.8739731304347957}, {1.6728341829160982, -1.873242985640593}, {1.6721459415303699, -1.8724489795918369}, {1.6624611568740721, -1.8606990641114929}, {1.653061224489797, -1.8492648538255463}, {1.6520932542108602, -1.8481487383588753}, {1.6510476808879386, -1.8469387755102042}, {1.6417306097129987, -1.8355918398995696}, {1.632653061224491, -1.824508389583349}, {1.6313733625579403, -1.82302819476176}, {1.6299953447379028, -1.8214285714285716}, {1.621021656483979, -1.8104576232725786}, {1.6122448979591848, -1.7997008180397196}, {1.6106756399677442, -1.7978799398362397}, {1.6089910811705357, -1.795918367346939}, {1.6003354664096425, -1.7852949527022344}, {1.5918367346938787, -1.7748390113411368}, {1.5900012943276478, -1.7727024637230948}, {1.5880371747643334, -1.7704081632653064}, {1.5796732875598591, -1.760102268101198}, {1.5714285714285725, -1.749919616723856
Re: [NTG-context] Implicit plots/level curves possible?
On Sun, 7 Oct 2018 17:25:35 +0200 "Mikael P. Sundqvist" wrote: > ContourPlot[2 x^5 + x y + y^5 == 0, {x, 0, 2}, {y, -2, 1/2}] Brut force: \startTEXpage \startMPcode {doublefun} pen savedpen ; savedpen := currentpen ; pickup pencircle scaled .01 ; path p ; p := for i=0 upto 1000 : for j=0 upto 1000 : hide(x := 2i/1000 ; y := 2.5j/1000 - 2 ;) if abs(2*(x**5)+x*y+y**5) < .002i/1000 : (x,y) .. fi endfor endfor cycle ; draw subpath (0,length p - 1) of p ; setbounds currentpicture to (0,-2)--(2,-2)--(2,.5)--(0,.5)--cycle ; currentpicture := currentpicture xsized 5cm ; pickup savedpen ; picture pic ; pic := currentpicture ; drawarrow llcorner pic--lrcorner pic ; drawarrow llcorner pic--ulcorner pic ; label.rt ("$x$", lrcorner pic) ; label.top("$y$", ulcorner pic) ; for x=0 step .5 until 2 : label.bot(decimal x,(x/2)[llcorner pic,lrcorner pic]) ; endfor for y=0 step .5 until 2.5 : label.lft(decimal (y-2),(y/2.5)[llcorner pic,ulcorner pic]) ; endfor \stopMPcode \stopTEXpage Alan ___ 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 ___
Re: [NTG-context] Implicit plots/level curves possible?
On Sun, Oct 7, 2018 at 5:26 PM Mikael P. Sundqvist wrote: > Dear list, > > Is it possiblet to make implicit plots (i.e. ploting the curves > described by equations) in MetaFun? I cannot find anything about them > in the MetaFun manual. > > For example, in Mathematica I can write something like > > ContourPlot[2 x^5 + x y + y^5 == 0, {x, 0, 2}, {y, -2, 1/2}] > you need some " Groebner basis computation " kernel to do this kind of computations. Have a look at https://www-fourier.ujf-grenoble.fr/~parisse/giac.html -- luigi ___ 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 ___
[NTG-context] Implicit plots/level curves possible?
Dear list, Is it possiblet to make implicit plots (i.e. ploting the curves described by equations) in MetaFun? I cannot find anything about them in the MetaFun manual. For example, in Mathematica I can write something like ContourPlot[2 x^5 + x y + y^5 == 0, {x, 0, 2}, {y, -2, 1/2}] to get the attached figure. The closest I have found which is something MetaPost-like, is the levelcurve command in mfpic (see for example this answer https://tex.stackexchange.com/a/405527/52406). If this is not currently possible in MetaFun, what is the simplest way to proceed (I want the same style of the plots as the other ones in my doc)? Is it to 1) somehow run mfpic from within ConTeXt? 2) export points from Mathematica and plot them in ConTeXt (how could that be done?). In the latter case, say I have the points (0,0), (0.4,1), and (0.8,2) exported to a file. What format should it be in to be easily imported and plot in MetaFun? 3) some other way? /Mikael ___ 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 ___