User:AnatolV/Helper Functions: Difference between revisions

m
m (→‎String Functions: a duplicate deleted)
m (→‎String Functions: minor edit)
 
(One intermediate revision by the same user not shown)
Line 309:
</lang>
==String Functions==
##The first 4 are already here on RC. A few others I will keep on hold (hoping to create tasks later).
 
<lang parigp>
Line 354:
</lang>
 
=PARI/GP Helper Functions=
PARI/GP built-in plotting functions are good only for instant draft math function curves plotting.
Not to mention bad color support and scaling.<br>
In addition, there are no built-in string manipulation functions, except of concatenation.<br>
So, presented here helper functions help to fix some of these shortcomings. At the same time, they simplify some often used common actions.
==Plotting Helper Functions==
Functions presented here are directly or inderectly related to plotting.
<lang parigp>
\\ 2 old plotting helper functions 3/2/16
\\ HFGP#1 insm(): Check if x,y are inside matrix mat (+/- p deep).
insm(mat,x,y,p=0)={my(xz=#mat[1,],yz=#mat[,1]);
return(x+p>0 && x+p<=xz && y+p>0 && y+p<=yz && x-p>0 && x-p<=xz && y-p>0 && y-p<=yz)}
 
\\ HFGP#2 plotmat(): Simple plotting using a square matrix mat (filled with 0/1).
plotmat(mat)={
my(xz=#mat[1,],yz=#mat[,1],vx=List(),vy=vx,x,y);
for(i=1,yz, for(j=1,xz, if(mat[i,j]==0, next, listput(vx,i); listput(vy,j))));
print(" *** matrix(",xz,"x",yz,") ",#vy, " DOTS");
plothraw(Vec(vx),Vec(vy));
}
 
\\ 2 new plotting helper functions 11/27/16
\\ HFGP#3 wrtmat(): Writing file fn containing X,Y coordinates from matrix mat.
\\ Created primarily for using file in Gnuplot, also for re-plotting.
wrtmat(mat, fn)={
my(xz=#mat[1,],yz=#mat[,1],ws,d=0);
for(i=1,yz, for(j=1,xz, if(mat[i,j]==0, next, d++; ws=Str(i," ",j); write(fn,ws))));
print(" *** matrix(",xz,"x",yz,") ",d, " DOTS put in ",fn);
}
 
\\ HFGP#4 plotff(): Plotting from a file written by the wrtmat().
\\ Saving possibly huge generation time if re-plotting needed.
plotff(fn)={
my(F,nf,vx=List(),vy=vx,Vr);
F=readstr(fn); nf=#F;
print(" *** Plotting from: ", fn, " - ", nf, " DOTS");
for(i=1,nf, Vr=stok(F[i],","); listput(vx,eval(Vr[1])); listput(vy,eval(Vr[2])));
plothraw(Vec(vx),Vec(vy));
}
 
\\ HFGP#5 iPlotmat() Improved simple plotting using matrix mat (color and scaling added).
\\ Matrix should be filled with 0/1. 7/6/16
\\ Writing a dump file fn according to the flag - dflg (0-no/1-yes).
\\ A dump file added mostly for the gnuplot plotting, also for re-plotting.
iPlotmat(mat,clr,fn="dfn.dat",dflg=0)={
my(xz=#mat[1,],yz=#mat[,1],vx=List(),vy=vx,xmin,xmax,ymin,ymax,c=0.625);
\\ Dumping if requested (dflg=1).
if (dflg==1,wrtmat(mat, fn));
for(i=1,yz, for(j=1,xz, if(mat[i,j]==0, next, listput(vx,i); listput(vy,j))));
xmin=listmin(vx); xmax=listmax(vx); ymin=listmin(vy); ymax=listmax(vy);
plotinit(0); plotcolor(0,clr);
plotscale(0, xmin,xmax,ymin,ymax);
plotpoints(0, Vec(vx)*c,Vec(vy));
plotdraw([0,xmin,ymin]);
if (dflg==0, print(" *** matrix: ",xz,"x",yz,", ",#vy," DOTS"));
}
 
\\ HFGP#6 iPlotV2(): Improved plotting from a file written by the wrtmat(). (color added)
\\ Saving possibly huge generation time if re-plotting needed. 7/6/16
iPlotV2(fn, clr)={
my(F,nf,vx=List(),vy=vx,Vr,xmin,xmax,ymin,ymax,c=0.625);
F=readstr(fn); nf=#F;
print(" *** Plotting from: ", fn, " - ", nf, " DOTS");
for(i=1,nf, Vr=stok(F[i]," "); listput(vx,eval(Vr[1])); listput(vy,eval(Vr[2])));
xmin=listmin(vx); xmax=listmax(vx); ymin=listmin(vy); ymax=listmax(vy);
plotinit(0); plotcolor(0,clr);
plotscale(0, xmin,xmax,ymin,ymax);
plotpoints(0, Vec(vx)*c,Vec(vy));
plotdraw([0,xmin,ymin]);
}
 
\\ HFGP#7 Plot the line from x1,y1 to x2,y2. 4/11/16
plotline(x1,y1,x2,y2,w=0)={plotmove(w, x1,y1);plotrline(w,x2-x1,y2-y1);}
 
\\ HFGP#8 Convert value expressed in degrees to radians. 5/7/16 (next 3 - same date)
rad2(degs)={return(degs*Pi/180.0)}
 
\\ HFGP#9 Convert value expressed in radians to degrees.
deg2(rads)={return(rads*180.0/Pi)} \\value in rads×180.0 /p
 
\\ HFGP#10 Convert Polar coordinates r,a to Cartesian.
cartes2(r,a,rndf=0)={my(v,x,y); x=r*cos(a); y=r*sin(a);
if(rndf==0, return([x,y]), return(round([x,y])))}
 
\\ HFGP#11 Convert Cartesian coordinates x,y to polar.
polar2(x,y)={my(v,r,a); r=sqrt(x^2+y^2); a=atan(y/x); return([r,a])}
</lang>
=gnuplot Helper File-Functions=
File for the load command is the only possible imitation of the fine function in the '''gnuplot'''.<br><br>
Anonymous user