// Copyright (c) 2007, Philippe Ivaldi. // Version: $Id: graph_pi.asy,v 0.0 "2007/01/27 10:35:52" Philippe Ivaldi Exp $ // Last modified: Fri Mar 28 15:57:10 CET 2008 // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or (at // your option) any later version. // This program is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA // 02110-1301, USA. // Commentary: // THANKS: // BUGS: // INSTALLATION: // copier ce fichier dans le sous-répertoire $HOME/.asy // Move this file in the sub-directory $HOME/.asy //CODE: import graph; import base_pi; import markers; usepackage("mathrsfs"); int None=0, onX=1, onY=2, onXY=3; // Real functions typedef real realfunction(real); struct graphicrules {// used to comunicate graphicrules to cartesianaxis. real xmin,xmax; real ymin,ymax; bool xcrop,ycrop; void set(picture pic=currentpicture){ xlimits(pic,xmin, xmax, xcrop); ylimits(pic,ymin, ymax, ycrop); } }; graphicrules init() {return new graphicrules;} graphicrules graphicrules = new graphicrules; void graph_pi_exitfunction(){}; /*ANCgraphicrulesANC*/ void graphicrules(picture pic=currentpicture, real unit=1cm, real xunit=unit != 0 ? unit : 0, real yunit=unit != 0 ? unit : 0, real xmin=-infinity, real xmax=infinity, real ymin=-infinity, real ymax=infinity, bool crop=NoCrop, bool xcrop=crop, bool ycrop=crop) { graphicrules.xmin=xmin; graphicrules.xmax=xmax; graphicrules.ymin=ymin; graphicrules.xmax=xmax; graphicrules.ymax=ymax; graphicrules.xcrop=xcrop; graphicrules.ycrop=ycrop; pic.unitsize(x=xunit,y=yunit); graphicrules.set(pic); graph_pi_exitfunction = new void() { graphicrules.set(pic); }; } ticklabel NoZero(string s=defaultformat) { return new string(real x) { if (x!=0) return format(s,x); else return ""; }; } ticklabel labelfrac(real ep=1/10^5, real factor=1, string symbol="", bool signin=false, bool symbolin=true, bool displaystyle=false, bool zero=true) { return new string(real x) { return texfrac(rational(x/factor), symbol, signin, symbolin, displaystyle, zero); }; } ticklabel labelfrac=labelfrac(); // *=======================================================* // *....................Graph paper....................* // *=======================================================* /*ANCmillimeterpaperANC*/ picture millimeterpaper(picture pic=currentpicture, pair O=(0,0), real xmin=infinity, real xmax=infinity, real ymin=infinity, real ymax=infinity, pen p=.5bp+orange) { picture opic; real cofx = pic.xunitsize/cm, cofy = pic.yunitsize/cm; real xmin = (xmin == infinity) ? pic.userMin().x*cofx : xmin*cofx, xmax = (xmax == infinity) ? pic.userMax().x*cofx : xmax*cofx, ymin = (ymin == infinity) ? pic.userMin().y*cofy : ymin*cofy, ymax = (ymax == infinity) ? pic.userMax().y*cofy : ymax*cofy; path ph = (xmin*cm, 0)--(xmax*cm, 0), pv = (0, ymin*cm)--(0, ymax*cm); real [] step={5, 1, .5, .1}; pen [] p_={ p, scale(.7)*p, scale(.4)*p, scale(.2)*p}; for (int j=0; j<4; ++j) { for (real i=O.y; i <= ymax; i += step[j]) { draw(opic, shift(0, i*cm) * ph, p_[j]); } for (real i=O.y; i >= ymin ; i -= step[j]) { draw(opic, shift(0, i*cm) * ph, p_[j]); } for (real i=O.x; i <= xmax; i += step[j]) { draw(opic, shift(i*cm, 0) * pv, p_[j]); } for (real i=O.x; i >= xmin; i -= step[j]) { draw(opic, shift(i*cm, 0) * pv, p_[j]); } } return opic; } // *=======================================================* // *.....................Axis and grid.....................* // *=======================================================* /*ANCgridANC*/ void grid(picture pic=currentpicture, real xmin=pic.userMin().x, real xmax=pic.userMax().x, real ymin=pic.userMin().y, real ymax=pic.userMax().y, real xStep=1, real xstep=.5, real yStep=1, real ystep=.5, pen pTick=nullpen, pen ptick=grey, bool above=false) { draw(pic,box((xmin,ymin),(xmax,ymax)),invisible); xaxis(pic, BottomTop, xmin, xmax, Ticks("%",extend=true,Step=xStep,step=xstep,pTick=pTick,ptick=ptick), above=above, p=nullpen); yaxis(pic, LeftRight, ymin, ymax, Ticks("%",extend=true,Step=yStep,step=ystep,pTick=pTick,ptick=ptick), above=above, p=nullpen); } /*ANCcartesianaxisANC*/ void cartesianaxis(picture pic=currentpicture, Label Lx=Label("$x$",align=2S), Label Ly=Label("$y$",align=2W), real xmin=-infinity, real xmax=infinity, real ymin=-infinity, real ymax=infinity, real extrawidth=1, real extraheight=extrawidth, pen p=currentpen, ticks xticks=Ticks("%",pTick=nullpen, ptick=grey), ticks yticks=Ticks("%",pTick=nullpen, ptick=grey), bool viewxaxis=true, bool viewyaxis=true, bool above=true, arrowbar arrow=Arrow) { graphicrules.set(pic); xmin=(xmin == -infinity) ? pic.userMin().x : xmin; xmax=(xmax == infinity) ? pic.userMax().x : xmax; ymin=(ymin == -infinity) ? pic.userMin().y : ymin; ymax=(ymax == infinity) ? pic.userMax().y : ymax; extraheight= pic.yunitsize != 0 ? cm*extraheight/(2*pic.yunitsize) : 0; extrawidth = pic.xunitsize != 0 ? cm*extrawidth/(2*pic.xunitsize) : 0; if (viewxaxis) { yequals(pic, Lx, 0, xmin-extrawidth, xmax+extrawidth, p, above, arrow=arrow); yequals(pic, 0, xmin, xmax, p, xticks, above); } if (viewyaxis) { xequals(pic, Ly, 0, ymin-extraheight, ymax+extraheight, p, above, arrow=arrow); xequals(pic, 0, ymin, ymax, p, yticks, above); } } real labelijmargin=1; /*ANClabeloijANC*/ void labeloij(picture pic=currentpicture, Label Lo=Label("$O$",NoFill), Label Li=Label("$\overrightarrow{\imath}$",NoFill), Label Lj=Label("$\overrightarrow{\jmath}$",NoFill), pen p=scale(2)*currentpen, pair diro=SW, pair diri=labelijmargin*S, pair dirj=labelijmargin*1.5*W, filltype filltype=NoFill, arrowbar arrow=Arrow(2mm), marker marker=dot) { if (Lo.filltype==NoFill) Lo.filltype=filltype; if (Li.filltype==NoFill) Li.filltype=filltype; if (Lj.filltype==NoFill) Lj.filltype=filltype; labelx(pic, Lo, 0, diro, p); draw(pic, Li, (0,0)--(1,0), diri, p, arrow); draw(pic, Lj, (0,0)--(0,1), dirj, p, arrow); if(marker != nomarker) draw(pic, (0,0), p, marker); } real labelIJmargin=1; /*ANClabeloIJANC*/ void labeloIJ(picture pic=currentpicture, Label Lo=Label("$O$",NoFill), Label LI=Label("$I$",NoFill), Label LJ=Label("$J$",NoFill), pair diro=SW, pair dirI=labelIJmargin*S, pair dirJ=labelIJmargin*W, pen p=currentpen, filltype filltype=NoFill, marker marker=dot) { if (Lo.filltype==NoFill) Lo.filltype=filltype; if (LI.filltype==NoFill) LI.filltype=filltype; if (LJ.filltype==NoFill) LJ.filltype=filltype; labelx(pic, LI, 1, dirI, p); labely(pic, LJ, 1, dirJ, p); labelx(pic, Lo, 0, diro, p); if(marker != nomarker) draw(pic, (0,0), p, marker); } // *=======================================================* // *....................Recursivegraph.....................* // *=======================================================* typedef void recursiveroutime(picture, real F(real), real, int, int, Label, align, pen, arrowbar, arrowbar, margin, Label, marker); /*ANCrecursiveoptionANC*/ recursiveroutime recursiveoption(Label L="u", bool labelbegin=true, bool labelend=true, bool labelinner=true, bool labelalternate=false, string format="", int labelplace=onX, pen px=nullpen, pen py=nullpen, bool startonyaxis=false, arrowbar circuitarrow=None, marker automarker=marker(cross(4)), marker xaxismarker=nomarker, marker yaxismarker=nomarker, marker xmarker=nomarker, marker fmarker=nomarker) { return new void(picture pic, real F(real), real u0, int n0, int n, Label L_, align align, pen p, arrowbar arrow, arrowbar bar, margin margin, Label legend, marker marker_) { real [] u; u[n0]=u0; for(int i=n0+1;in0) { if (px!=invisible) { draw(pic,(u[i],u[i])--(u[i],0),px); if (addlabelautomark) add(pic,automarker.f, (u[i],u[i])); } } if (i>n0) { if (py!=invisible) { if (addlabelautomark) add(pic,automarker.f, (u[i-1],u[i])); draw(pic,(u[i-1],u[i])--(0,u[i]),py); } } if (Le) L.s=format(format == "" ? defaultformat : format,u[i]); else if (Lno) L.s=""; else if (fe) L.s="$" + Ls + "_{" + (string) i + "}$"; else L.s="$" + Ls + "_{" + (string) i + "}" + format(format,u[i]) + "$"; if (labelplace==1 || labelplace==3) {//Label on xaxis L.position((u[i],0)); L.align(L.align,S); if (labelalternate && i!=n0) {L.align(-L.align.dir);} L.s=baseline(L.s,"$p_{1234567890}$"); if (addlabelautomark) label(pic,L); if (xaxismarker==nomarker && addlabelautomark) add(pic,automarker.f, (u[i],0)); } if (labelplace==2 || labelplace==3) {//Label on yaxis L.position((0,u[i])); L.align(L.align,W); if (labelalternate && i!=n0) {L.align(-L.align.dir);} L.s=baseline(L.s,"$w_{1234567890}$"); if (addlabelautomark) label(pic,L); if (yaxismarker==nomarker && addlabelautomark) add(pic,automarker.f, (0,u[i])); } add(pic,xaxismarker.f, (u[i],0)); if (i>n0 || startonyaxis) add(pic,yaxismarker.f, (0,u[i])); if (i>n0 || startonyaxis) add(pic,xmarker.f, (u[i],u[i])); if (i>n0) add(pic,fmarker.f, (u[i-1],u[i])); } }; } recursiveroutime DefaultRecursiveOption=recursiveoption(); struct recursivegraph { real f(real); real u0; int n0; int n; recursiveroutime recursiveroutime=DefaultRecursiveOption; void draw(picture pic=currentpicture, Label L, align align, pen p, arrowbar arrow, arrowbar bar, margin margin, Label legend, marker marker) {recursiveroutime(pic, f, u0, n0, n, L, align, p, arrow, bar, margin, legend, marker);}; }; recursivegraph operator init() {return new recursivegraph;} recursivegraph recursivegraph(real F(real), real u0, int n0=0, int n) { recursivegraph orec= new recursivegraph; orec.f=F; orec.u0=u0; orec.n0=n0; orec.n=n; return orec; } void draw(picture pic=currentpicture, Label L="", recursivegraph g, recursiveroutime lr=DefaultRecursiveOption,align align=NoAlign, pen p=currentpen, arrowbar arrow=None, arrowbar bar=None, margin margin=NoMargin, Label legend="", marker marker=nomarker) { g.recursiveroutime=lr; g.draw(pic, L, align, p, arrow, bar, margin, legend, marker); } guide graph(picture pic=currentpicture, real f(real), int n=ngraph, interpolate join=operator --) { return graph(pic, f, a=pic.userMin().x, b=pic.userMax().x, n, join); } /*ANCgraphpoint(...)ANC*/ void graphpoint(picture pic=currentpicture, Label L="", real f(real), real xCoordinate, real xmin=0, real ymin=0, int draw=onXY, pen px=nullpen, pen py=px, arrowbar arrow=None, arrowbar bar=None, margin marginy=NoMargin, margin marginx=NoMargin, bool extend=false, bool extendx=extend, bool extendy=extend, Label legendx="", Label legendy="", marker markerx=nomarker, marker markery=nomarker) {/*DOC Mark a point on a curve defined by real f(real). DOC*/ /*EXAgraphpoint(...)EXA*/ real xmax,ymax; if (extendy) { xmin=pic.userMin().x; xmax=pic.userMax().x; } else xmax=xCoordinate; if (extendx) { ymin=pic.userMin().y; ymax=pic.userMax().y; } else ymax=f(xCoordinate); px = (px==nullpen) ? currentpen+linetype("6 6") : px; py = (py==nullpen) ? currentpen+linetype("6 6") : py; L.align(L.align,NE); label(pic, L, (xCoordinate,f(xCoordinate))); if (draw==onX || draw==onXY) draw(pic, (xCoordinate,ymin)--(xCoordinate,ymax), p=px, arrow=arrow, bar=bar, margin=marginx, legend=legendx, marker=markerx); if (draw==onY || draw==onXY) draw(pic, (xmin,ymax)--(xmax,ymax), p=py, arrow=arrow, bar=bar, margin=marginy, legend=legendy, marker=markery); } // *=======================================================* // *.....................About tangent.....................* // *=======================================================* /*ANCtangentANC*/ path tangent(path g, real x, path b=box(userMin(currentpicture),userMax(currentpicture))) {//Return the tangent with the maximun size allowed by b (cyclic path) if (!cyclic(b)) abort("tangent: path b is not a cyclic path..."); pair pt=point(g,intersectsv(g,x)[0]); real t=intersectp(g,pt); real rt=intersectp(reverse(g),pt); pair dirr=dir(g,t); pair dll=intersectionpointsd(b,pt,shift(pt)*dirr)[0]; pair dlr=intersectionpointsd(b,pt,shift(pt)*(-dirr))[0]; return dll--dlr; } /*ANCaddtangentANC*/ void addtangent(picture pic=currentpicture, path g, pair pt,//Point on the path g real size=infinity,//ABSOLUTE size of the tangent line (infinity=maximun size according the size of the pic) bool drawright=true,//Draw the tangent at the right bool drawleft=true,//... left pair v=(infinity,infinity),//A finite value forces the value of the derivative pair vr=v,//A finite value forces the value of the derivative at right pair vl=v,//A finite value forces the value of the derivative at left arrowbar arrow=null,//null=automatic determination margin margin=NoMargin,//Useful with size=infinity Label legend="", pen p=currentpen, real dt=2,//Increase this number can help to discern tangent at the right and tgt at the left. bool differentiable=true)//Set it "true" maybe useful if you are sure that "g is differentiable" at this point. { arrowbar arrow_=arrow; pair dir_r,dir_l; if (intersect(g,pt).length<2) abort("addtangent: the point is not on the path."); real t=intersectp(g,pt); if (!differentiable) { path subpa,subpb; subpa=subpath(g,0,t-dt/2); subpb=subpath(g,t+dt/2,length(g)); dir_l=(vl.x