ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Implicit plots/level curves possible?
@ 2018-10-07 15:25 Mikael P. Sundqvist
  2018-10-07 15:35 ` luigi scarso
  2018-10-07 17:14 ` Alan Braslau
  0 siblings, 2 replies; 14+ messages in thread
From: Mikael P. Sundqvist @ 2018-10-07 15:25 UTC (permalink / raw)
  To: mailing list for ConTeXt users

[-- Attachment #1: Type: text/plain, Size: 963 bytes --]

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

[-- Attachment #2: implicitplot.png --]
[-- Type: image/png, Size: 9963 bytes --]

[-- Attachment #3: Type: text/plain, Size: 492 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] 14+ messages in thread

* Re: Implicit plots/level curves possible?
  2018-10-07 15:25 Implicit plots/level curves possible? Mikael P. Sundqvist
@ 2018-10-07 15:35 ` luigi scarso
  2018-10-07 17:14 ` Alan Braslau
  1 sibling, 0 replies; 14+ messages in thread
From: luigi scarso @ 2018-10-07 15:35 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 551 bytes --]

On Sun, Oct 7, 2018 at 5:26 PM Mikael P. Sundqvist <mickep@gmail.com> 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

[-- Attachment #1.2: Type: text/html, Size: 1107 bytes --]

[-- Attachment #2: Type: text/plain, Size: 492 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] 14+ messages in thread

* Re: Implicit plots/level curves possible?
  2018-10-07 15:25 Implicit plots/level curves possible? Mikael P. Sundqvist
  2018-10-07 15:35 ` luigi scarso
@ 2018-10-07 17:14 ` Alan Braslau
  2018-10-07 18:02   ` Mikael P. Sundqvist
  2018-10-07 18:03   ` Hans Hagen
  1 sibling, 2 replies; 14+ messages in thread
From: Alan Braslau @ 2018-10-07 17:14 UTC (permalink / raw)
  To: Mikael P. Sundqvist; +Cc: mailing list for ConTeXt users

On Sun, 7 Oct 2018 17:25:35 +0200
"Mikael P. Sundqvist" <mickep@gmail.com> 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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Implicit plots/level curves possible?
  2018-10-07 17:14 ` Alan Braslau
@ 2018-10-07 18:02   ` Mikael P. Sundqvist
  2018-10-07 18:03   ` Hans Hagen
  1 sibling, 0 replies; 14+ messages in thread
From: Mikael P. Sundqvist @ 2018-10-07 18:02 UTC (permalink / raw)
  To: braslau.list, mailing list for ConTeXt users

On Sun, Oct 7, 2018 at 7:14 PM Alan Braslau <braslau.list@comcast.net> wrote:
>
> On Sun, 7 Oct 2018 17:25:35 +0200
> "Mikael P. Sundqvist" <mickep@gmail.com> 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.7568733335567501,
-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.7499196167238567}, {1.5693516154762674, -1.7474941541240552},
{1.5671360578036797, -1.7448979591836737}, {1.559036453200205,
-1.7348779028875008}, {1.5510204081632664, -1.7249390383587966},
{1.5487279818399662, -1.722253288006166}, {1.5462903226481057,
-1.7193877551020411}, {1.5384263887311262, -1.7096200753105835},
{1.5306122448979602, -1.6998934176492682}, {1.5281318676901015,
-1.6969780225302318}, {1.5255027353966268, -1.6938775510204085},
{1.5178446192795407, -1.6843268789618004}, {1.510204081632654,
-1.6747786118581103}, {1.507564851086154, -1.6716663851219011},
{1.5047762510118174, -1.6683673469387759}, {1.4972927780116314,
-1.6589962723834215}, {1.489795918367348, -1.6495901709353737},
{1.487028622577334, -1.6463162625946608}, {1.484114030093085,
-1.6428571428571432}, {1.476772615243479, -1.633626067680347},
{1.4693877551020418, -1.6243233124136804}, {1.466524994743585,
-1.6209253892235815}, {1.463519457517736, -1.6173469387755106},
{1.4562860084349807, -1.6082139180277042}, {1.4489795918367356,
-1.5989728942363095}, {1.44605591266623, -1.59549133365701},
{1.4429961632027681, -1.591836734693878}, {1.4358349731623732,
-1.5827573039551985}, {1.4285714285714295, -1.5735333853838596},
{1.4256234654289337, -1.5700114845403652}, {1.422548045280916,
-1.5663265306122454}, {1.415421675175695, -1.5572535182752807},
{1.4081632653061233, -1.5479988341700672}, {1.4052298987613066,
-1.5444830347116336}, {1.4021792960326345, -1.5408163265306127},
{1.39504844365984, -1.5316996495068342}, {1.3877551020408172,
-1.522362834087483}, {1.3848776289504767, -1.5189029638119056},
{1.3818944309730468, -1.51530612244898}, {1.3747177858315758,
-1.506092563628899}, {1.367346938775511, -1.4966184871011081},
{1.3645692581604423, -1.4932680191361836}, {1.3616983215613736,
-1.4897959183673475}, {1.3544324030201833, -1.4804288839798747},
{1.346938775510205, -1.4707583643151105}, {1.3443075913151248,
-1.467574694529565}, {1.3415962320825263, -1.4642857142857149},
{1.3341952083963358, -1.4547049690964184}, {1.3265306122448988,
-1.444774463977538}, {1.3240956547188925, -1.4418192071115903},
{1.3215938613494602, -1.4387755102040822}, {1.3140093465326064,
-1.4289168882628152}, {1.3061224489795926, -1.4186581668443676},
{1.3039367166080325, -1.4159974715869}, {1.3016973899944826,
-1.4132653061224496}, {1.2938782149996626, -1.4030603945157294},
{1.2857142857142865, -1.3924001890023634}, {1.2838343098483223,
-1.3901050718732724}, {1.2819135342629109, -1.387755102040817},
{1.2738054882248795, -1.377130894820943}, {1.2653061224489803,
-1.3659905323563684}, {1.2637922570174938, -1.3641372297485423},
{1.262249607399565, -1.3622448979591844}, {1.253795143864733,
-1.3511234171078605}, {1.2448979591836742, -1.3394184331288994},
{1.2438146981370093, -1.3380887701858828}, {1.242713589935552,
-1.3367346938775517}, {1.233851491968893, -1.3250325738143955},
{1.224489795918368, -1.3126723089083476}, {1.2239061213449884,
-1.3119540830126435}, {1.223314210449941, -1.311224489795919},
{1.2139792072421658, -1.298852521559539}, {1.204081632653062,
-1.2857397050294284}, {1.204071396831069, -1.2857270804917775},
{1.2040610387115078, -1.2857142857142865}, {1.203739283948535,
-1.2852863498336278}, {1.1941828495239635, -1.2725775605440268},
{1.1848728666718258, -1.2602040816326538}, {1.1843146571968737,
-1.2594025968712566}, {1.1836734693877558, -1.2584845639037323},
{1.1744675565464748, -1.2462012686026223}, {1.1658355990824183,
-1.2346938775510212}, {1.1646422985936957, -1.2329726369619636},
{1.1632653061224496, -1.2309927268418497}, {1.1548396662050255,
-1.2197157233661688}, {1.146961508332401, -1.2091836734693886},
{1.1450604755890992, -1.206429507554444}, {1.1428571428571435,
-1.2032486753632181}, {1.1353055778633054, -1.1931129256300532},
{1.1282625364233536, -1.183673469387756}, {1.1255758606927269,
-1.179764868011644}, {1.1224489795918373, -1.175233743918532},
{1.1158722500035858, -1.1663841772914378}, {1.1097518806321112,
-1.1581632653061233}, {1.1061957117731829, -1.1529696459978087},
{1.1020408163265312, -1.1469280138295148}, {1.0965472538980585,
-1.1395200142600814}, {1.0914441707559086, -1.1326530612244907},
{1.0869279281417792, -1.1260339673737978}, {1.081632653061225,
-1.1183103043424962}, {1.0773388321633344, -1.1125101332652214},
{1.073355678223837, -1.107142857142858}, {1.067781111626598,
-1.0989470798545091}, {1.0612244897959189, -1.0893581918849886},
{1.0582559623906629, -1.0853433123177956}, {1.0555045640821168,
-1.0816326530612255}, {1.0487646327800382, -1.0716972702494436},
{1.0408163265306127, -1.0600480676685298}, {1.0393084259327305,
-1.0580073247269455}, {1.0379111746931935, -1.0561224489795928},
{1.0298887022230219, -1.044271775282449}, {1.0215875701388464,
-1.032086503489885}, {1.0205861803415766, -1.0306122448979602},
{1.0205062844470683, -1.030489593420758}, {1.0204081632653066,
-1.0303327731966592}, {1.0111583837617015, -1.0166642651958337},
{1.00328045071553, -1.0051020408163276}, {1.0018506740328479,
-1.002788698275268}, {1.0000000000000004, -0.9998247144637208},
{0.9925846949533242, -0.9888609680430401}, {0.9862689729210781,
-0.979591836734695}, {0.9833620492224978, -0.9748790711249405},
{0.9795918367346943, -0.9688331607067846}, {0.9741844045384619,
-0.9608409228983529}, {0.9695743609940414, -0.9540816326530623},
{0.965053495511301, -0.9467443551006712}, {0.9591836734693882,
-0.9373286299987242}, {0.9559711254640683, -0.9325871135780796},
{0.9532266775860438, -0.9285714285714297}, {0.9469391680816696,
-0.9183668562244452}, {0.938775510204082, -0.9052822699905541},
{0.937959568860365, -0.9040811511694432}, {0.9372602079240614,
-0.9030612244897971}, {0.932154118799089, -0.8947844852335558},
{0.9290293101409223, -0.889733770487114}, {0.9213915388935029,
-0.8775510204081645}, {0.9201452591357462, -0.8753286301619515},
{0.9183673469387759, -0.8722133960823307}, {0.9113179542701124,
-0.8608525571623611}, {0.9057724934360561, -0.8520408163265318},
{0.9025494510675557, -0.8463029820839243}, {0.8979591836734697,
-0.8382820726300806}, {0.8938418613841069, -0.8316772651066027},
{0.8905848785525916, -0.8265306122448992}, {0.8851973504937649,
-0.8169726996378975}, {0.8775510204081636, -0.8036731357607297},
{0.8766181331775866, -0.8021865172014878}, {0.8758745616162483,
-0.8010204081632666}, {0.8717992018019936, -0.7938306349055541},
{0.8680942600934959, -0.7873311544749686}, {0.8673469387755105,
-0.7859605093610692}, {0.8613297469419768, -0.775510204081634},
{0.8596225972442848, -0.7724105289548499}, {0.8571428571428574,
-0.7677089093672168}, {0.8512196268844187, -0.7574040378230499},
{0.8468700937133944, -0.7500000000000013}, {0.8428873710051891,
-0.742309153590454}, {0.8367346938775513, -0.7307482141475812},
{0.8346278319927692, -0.7271233732743463}, {0.8330615296525217,
-0.7244897959183687}, {0.8264429761188443, -0.7118442390351198},
{0.826081710585806, -0.7111735668036873}, {0.8194566789043612,
-0.6989795918367361}, {0.8182906984321618, -0.6965243820618401},
{0.8163265306122451, -0.692527944882672}, {0.8102098917412865,
-0.6811151863438016}, {0.8059895119698924, -0.6734693877551035},
{0.8022037022277904, -0.6656127191540392}, {0.795918367346939,
-0.6530291338493389}, {0.7942732569585857, -0.6500155716589124},
{0.7931154678616339, -0.6479591836734708}, {0.788660951262447,
-0.6388874135678558}, {0.7864004340254593, -0.6343463962436878},
{0.7857142857142859, -0.6329068417862055}, {0.7804103584118951,
-0.6224489795918382}, {0.7785555188421481, -0.6186423361411941},
{0.7755102040816328, -0.6121243496531662}, {0.7707813831152447,
-0.6028498017181907}, {0.7676512010413118, -0.5969387755102056},
{0.7630775810320478, -0.5869693502405542}, {0.756393422065975,
-0.5730427979906334}, {0.7556055756464799, -0.571428571428573},
{0.7554321624629047, -0.5710159193703505}, {0.7551020408163267,
-0.570269643943723}, {0.7477770851084874, -0.5550745619817394},
{0.7448979591836736, -0.5488453809132513}, {0.7434519183830882,
-0.5459183673469403}, {0.7401801447477782, -0.5390605333509934},
{0.7346938775510206, -0.5270849482518927}, {0.73263840127533,
-0.522977508609921}, {0.7312918221599828, -0.5204081632653077},
{0.7268626402744253, -0.5106191166695636}, {0.7251230694566684,
-0.5068614693016152}, {0.719290705088944, -0.4948979591836751},
{0.7175705956538513, -0.49079185747350396}, {0.7142857142857144,
-0.48340643564105806}, {0.7100516847737509, -0.47468029199199685},
{0.7073448200023936, -0.4693877551020424}, {0.7025599580091759,
-0.45853474636608277}, {0.6982590979989207, -0.44935448474355033},
{0.6956311984283484, -0.4438775510204097}, {0.6950349244773347,
-0.4424308341992517}, {0.6938775510204083, -0.43980218908236496},
{0.6874491920686455, -0.4264027956284805}, {0.6833632684014233,
-0.4183673469387771}, {0.679858385623683, -0.410381099603051},
{0.6734693877551021, -0.39675537677370076}, {0.6722520475111009,
-0.3943788181621459}, {0.6714258258398694, -0.39285714285714446},
{0.6684972039320424, -0.3866419130783198}, {0.6645518711486437,
-0.3784938345335849}, {0.663265306122449, -0.3757402709557924},
{0.6590341526839318, -0.3673469387755118}, {0.6567489985166309,
-0.362737221241968}, {0.653061224489796, -0.3550365584154535},
{0.6488886553973445, -0.34705244605944346}, {0.6459894882213929,
-0.3418367346938791}, {0.6409564082075948, -0.3314575509649979},
{0.6339173473315975, -0.31790688824613117}, {0.6330768946052754,
-0.31632653061224647}, {0.6329213741527878, -0.31599113945187396},
{0.6326530612244898, -0.3154525754364381}, {0.6246825464459771,
-0.3007794700037548}, {0.6189168694975438, -0.29081632653061384},
{0.6163241005493615, -0.2857173232928916}, {0.6122448979591837,
-0.2782350162535417}, {0.6078288465185949, -0.27082618674971715},
{0.6043303428709713, -0.26530612244898116}, {0.5991788406190746,
-0.2561284900424849}, {0.5918367346938775, -0.24389776210463143},
{0.5903555073062609, -0.2416474526018694}, {0.5890627018230726,
-0.2397959183673485}, {0.5813397934240215, -0.22740689087303578},
{0.5784627297192345, -0.22307841214904475}, {0.5726038361977244,
-0.21428571428571586}, {0.5720710062542617, -0.213482670753603},
{0.5714285714285714, -0.21248006602652858}, {0.5625661740932898,
-0.19985350687318523}, {0.5543350120343092, -0.1887755102040832},
{0.5528254139340698, -0.18651925299057753}, {0.5510204081632653,
-0.18398157362696888}, {0.5428318611475172, -0.1735009898921355},
{0.5343987007027057, -0.16326530612245055}, {0.5325694176273844,
-0.16081884021066897}, {0.5306122448979591, -0.15834558885056343},
{0.5220230735267438, -0.148491566254837}, {0.512199147700871,
-0.1377551020408179}, {0.5111792551518377, -0.13653613514183702},
{0.510204081632653, -0.13542917534952764}, {0.5000261945383969,
-0.12496725682700528}, {0.49977963171669737, -0.12472453964587339},
{0.48979591836734687, -0.11506195678238179}, {0.48846717572160814,
-0.11390582626635866}, {0.48648252199319625, -0.11224489795918524},
{0.4765565181086078, -0.10328394420097649}, {0.4693877551020408,
-0.0970644987305743}, {0.4642917736480328, -0.09310467069506256},
{0.45583994657295973, -0.08673469387755259}, {0.44897959183673464,
-0.0812621502122962}, {0.43903522242696985, -0.07430423211534663},
{0.43869682455543724, -0.07407794889754166}, {0.4385461132586065,
-0.07397959183673626}, {0.4285714285714285, -0.06746833097322409},
{0.42515540023421394, -0.06549452521743815}, {0.4183673469387754,
-0.06126980011662488}, {0.418285103919423, -0.061224489795919934},
{0.4112599946137656, -0.057353578161365895}, {0.4081632653061224,
-0.05550762097783089}, {0.40024525152723367, -0.05132697257230899},
{0.3969603869705881, -0.04971788363370523}, {0.3877551020408163,
-0.04521174212933679}, {0.3820952329763974, -0.0427891220448109},
{0.3681688759031847, -0.03674170712388045}, {0.36734693877551017,
-0.03641942544183239}, {0.36690258896515276, -0.036269722977234034},
{0.36531106184529016, -0.03571428571428728}, {0.35108097561619805,
-0.030536535581794755}, {0.346938775510204, -0.028976072037454753},
{0.33963400053754317, -0.026583316998461205}, {0.33490176337515043,
-0.02525034680147161}, {0.32727188645789995, -0.022959183673470954},
{0.32653061224489793, -0.022736585235885894}, {0.3182130453173142,
-0.02060104029213431}, {0.31334917811298524, -0.019237493049396325},
{0.30612244897959184, -0.017563430698495447}, {0.30102370046108823,
-0.016577517280784146}, {0.28860697981479555, -0.013819949258291905},
{0.2857142857142857, -0.01332777433117424}, {0.2834618449596721,
-0.013019632575921659}, {0.26695317768842314, -0.010204081632654627},
{0.2655082234184484, -0.009951455420818617}, {0.26530612244897955,
-0.009908754497390013}, {0.26502946042836784, -0.009858254106889978},
{0.24701765087206728, -0.007554467022162315}, {0.24489795918367344,
-0.007194003754251076}, {0.24217254033624375, -0.006797308073367503},
{0.22818919952098876, -0.005579827129377822}, {0.22448979591836732,
-0.005079447159325402}, {0.21997601564642139, -0.004561856292722218},
{0.20904667370867258, -0.003997780313140377}, {0.2040816326530612,
-0.0034693311989266035}, {0.19827874791776576, -0.002950475713535316},
{0.18962026246209016, -0.002770590289735809}, {0.18367346938775508,
-0.002276228754648394}, {0.17695870487613444, -0.0018106259931288168},
{0.16994475754904617, -0.0018547673494080982}, {0.16326530612244897,
-0.0014210391046479282}, {0.15592141581127092, -0.001024218743682052},
{0.1500574537158482, -0.001203693059272969}, {0.14285714285714285,
-0.0008329879260291356}, {0.13509232585153358,
-0.0004980603756430477}, {0.1299958383315521, -0.0007705082080104071},
{0.12244897959183673, -0.00044962729960941125}, {0.11441150696269169,
-0.00015724084622331484}, {0.10979505323831777,
-0.0005112854929206802}, {0.10204081632653061,
-0.00021683572040633557}, {0.093829631052776, 0.00005989995953862605},
{0.08948471441215075, -0.0003890049439967871}, {0.08163265306122448,
-0.00008881812385440856}, {0.07330397446341344,
0.00020676661460917144}, {0.06908320845627795,
-0.00038068330720514645}, {0.061224489795918366,
-0.00002810596445531298}, {0.05279149292677095, 0.000337164453779645},
{0.0485800339458205, -0.0004994473636443092}, {0.04081632653061224,
0}, {0.03222508810558834, 0.000534966398625247},
{0.027827852574969683, -0.0009294699955751749}, {0.02040816326530612,
0}, {0.011337910781976428, 0.0011337339715074921}, {0,
-0.010204027425056751}, {0., 0.002538576406170707}}
%%% End of file
___________________________________________________________________________________
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] 14+ messages in thread

* Re: Implicit plots/level curves possible?
  2018-10-07 17:14 ` Alan Braslau
  2018-10-07 18:02   ` Mikael P. Sundqvist
@ 2018-10-07 18:03   ` Hans Hagen
  2018-10-08 20:00     ` Aditya Mahajan
  1 sibling, 1 reply; 14+ messages in thread
From: Hans Hagen @ 2018-10-07 18:03 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Alan Braslau, Mikael P. Sundqvist

On 10/7/2018 7:14 PM, Alan Braslau wrote:
> On Sun, 7 Oct 2018 17:25:35 +0200
> "Mikael P. Sundqvist" <mickep@gmail.com> 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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Implicit plots/level curves possible?
  2018-10-07 18:03   ` Hans Hagen
@ 2018-10-08 20:00     ` Aditya Mahajan
  2018-10-08 20:20       ` Alan Braslau
  0 siblings, 1 reply; 14+ messages in thread
From: Aditya Mahajan @ 2018-10-08 20:00 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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" <mickep@gmail.com> 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.
<to be read again>
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Implicit plots/level curves possible?
  2018-10-08 20:00     ` Aditya Mahajan
@ 2018-10-08 20:20       ` Alan Braslau
  2018-10-08 21:25         ` Aditya Mahajan
  0 siblings, 1 reply; 14+ messages in thread
From: Alan Braslau @ 2018-10-08 20:20 UTC (permalink / raw)
  To: Aditya Mahajan; +Cc: mailing list for ConTeXt users

On Mon, 8 Oct 2018 16:00:10 -0400 (EDT)
Aditya Mahajan <adityam@umich.edu> 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" <mickep@gmail.com> 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.
> <to be read again>
> 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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Implicit plots/level curves possible?
  2018-10-08 20:20       ` Alan Braslau
@ 2018-10-08 21:25         ` Aditya Mahajan
  2018-10-08 21:53           ` Alan Braslau
                             ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Aditya Mahajan @ 2018-10-08 21:25 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On Mon, 8 Oct 2018, Alan Braslau wrote:

> On Mon, 8 Oct 2018 16:00:10 -0400 (EDT)
> Aditya Mahajan <adityam@umich.edu> 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" <mickep@gmail.com> 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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Implicit plots/level curves possible?
  2018-10-08 21:25         ` Aditya Mahajan
@ 2018-10-08 21:53           ` Alan Braslau
  2018-10-08 21:56           ` Aditya Mahajan
  2018-10-08 22:03           ` Hans Hagen
  2 siblings, 0 replies; 14+ messages in thread
From: Alan Braslau @ 2018-10-08 21:53 UTC (permalink / raw)
  To: Aditya Mahajan; +Cc: mailing list for ConTeXt users

On Mon, 8 Oct 2018 17:25:24 -0400 (EDT)
Aditya Mahajan <adityam@umich.edu> 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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Implicit plots/level curves possible?
  2018-10-08 21:25         ` Aditya Mahajan
  2018-10-08 21:53           ` Alan Braslau
@ 2018-10-08 21:56           ` Aditya Mahajan
  2018-10-09 22:23             ` Alan Braslau
  2018-10-08 22:03           ` Hans Hagen
  2 siblings, 1 reply; 14+ messages in thread
From: Aditya Mahajan @ 2018-10-08 21:56 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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 <adityam@umich.edu> 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" <mickep@gmail.com> 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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Implicit plots/level curves possible?
  2018-10-08 21:25         ` Aditya Mahajan
  2018-10-08 21:53           ` Alan Braslau
  2018-10-08 21:56           ` Aditya Mahajan
@ 2018-10-08 22:03           ` Hans Hagen
  2018-10-09 11:12             ` Mikael P. Sundqvist
  2 siblings, 1 reply; 14+ messages in thread
From: Hans Hagen @ 2018-10-08 22:03 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Aditya Mahajan

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 <adityam@umich.edu> 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" <mickep@gmail.com> 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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Implicit plots/level curves possible?
  2018-10-08 22:03           ` Hans Hagen
@ 2018-10-09 11:12             ` Mikael P. Sundqvist
  0 siblings, 0 replies; 14+ messages in thread
From: Mikael P. Sundqvist @ 2018-10-09 11:12 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 5345 bytes --]

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<x<1.5 and -1<y<1).

/Mikael (who hopes for some support for these kind of things in MetaFun in
the future)

On Tue, Oct 9, 2018 at 12:03 AM Hans Hagen <j.hagen@xs4all.nl> 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 <adityam@umich.edu> 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" <mickep@gmail.com> 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
>
> ___________________________________________________________________________________

[-- Attachment #1.2: Type: text/html, Size: 7681 bytes --]

[-- Attachment #2: Type: text/plain, Size: 492 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] 14+ messages in thread

* Re: Implicit plots/level curves possible?
  2018-10-08 21:56           ` Aditya Mahajan
@ 2018-10-09 22:23             ` Alan Braslau
  2018-10-11 13:55               ` Aditya Mahajan
  0 siblings, 1 reply; 14+ messages in thread
From: Alan Braslau @ 2018-10-09 22:23 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On Mon, 8 Oct 2018 17:56:24 -0400 (EDT)
Aditya Mahajan <adityam@umich.edu> 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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Implicit plots/level curves possible?
  2018-10-09 22:23             ` Alan Braslau
@ 2018-10-11 13:55               ` Aditya Mahajan
  0 siblings, 0 replies; 14+ messages in thread
From: Aditya Mahajan @ 2018-10-11 13:55 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On Tue, 9 Oct 2018, Alan Braslau wrote:

> On Mon, 8 Oct 2018 17:56:24 -0400 (EDT)
> Aditya Mahajan <adityam@umich.edu> 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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2018-10-11 13:55 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-07 15:25 Implicit plots/level curves possible? Mikael P. Sundqvist
2018-10-07 15:35 ` luigi scarso
2018-10-07 17:14 ` Alan Braslau
2018-10-07 18:02   ` Mikael P. Sundqvist
2018-10-07 18:03   ` Hans Hagen
2018-10-08 20:00     ` Aditya Mahajan
2018-10-08 20:20       ` Alan Braslau
2018-10-08 21:25         ` Aditya Mahajan
2018-10-08 21:53           ` Alan Braslau
2018-10-08 21:56           ` Aditya Mahajan
2018-10-09 22:23             ` Alan Braslau
2018-10-11 13:55               ` Aditya Mahajan
2018-10-08 22:03           ` Hans Hagen
2018-10-09 11:12             ` Mikael P. Sundqvist

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