User:AnatolV/Helper Functions: Difference between revisions
Content added Content deleted
(adding funcs, editing) |
(adding GP funcs) |
||
Line 71: | Line 71: | ||
// HFJS#1 Like in PARI/GP: return random number 0..max-1. |
// HFJS#1 Like in PARI/GP: return random number 0..max-1. |
||
function randgp(max) {return Math.floor(Math.random()*max)} |
function randgp(max) {return Math.floor(Math.random()*max)} |
||
// HFJS#2 Return random hex color. |
// HFJS#2 Return random hex color. |
||
function randhclr() { |
function randhclr() { |
||
Line 80: | Line 81: | ||
</lang> |
</lang> |
||
=PARI/GP Helper Functions= |
=PARI/GP Helper Functions= |
||
PARI/GP plotting functions are good only for instant draft math function curves plotting. |
|||
<<coming soon>> |
|||
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. |
|||
==Plotting Helper Functions== |
|||
Many of these functions are already here on RC. |
|||
<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 |
|||
iPlotmat(mat,clr)={ |
|||
my(xz=#mat[1,],yz=#mat[,1],vx=List(),vy=vx,xmin,xmax,ymin,ymax,c=0.625); |
|||
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]); |
|||
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> |
|||
==String Functions== |
|||
The first 4 are already here on RC. A few others I will keep on hold (hoping to create a tasks later). |
|||
<lang parigp> |
|||
\\ SF#1 ssubstr(): Returns the substring of the string str specified by the start |
|||
\\ position s and a length n. If n=0 then to the end of str. 3/5/16 |
|||
ssubstr(str,s=1,n=0)={ |
|||
my(vt=Vecsmall(str),ve,vr,vtn=#str,n1); |
|||
if(vtn==0,return("")); |
|||
if(s<1||s>vtn,return(str)); |
|||
n1=vtn-s+1; if(n==0,n=n1); if(n>n1,n=n1); |
|||
ve=vector(n,z,z-1+s); vr=vecextract(vt,ve); return(Strchr(vr)); |
|||
} |
|||
\\ SF#2 stok(): Tokenize a string str according to 1 character delimiter d. |
|||
\\ Return a list of tokens. 3/5/16 |
|||
\\ Note: It is using ssubstr(). |
|||
stok(str,d)={ |
|||
my(d1c=ssubstr(d,1,1),str=Str(str,d1c),vt=Vecsmall(str),d1=sasc(d1c), |
|||
Lr=List(),sn=#str,v1,p1=1,vo=32); |
|||
if(sn==1, return(List(""))); if(vt[sn-1]==d1,sn--); |
|||
for(i=1,sn, v1=vt[i]; |
|||
if(v1!=d1, vo=v1; next); |
|||
if(vo==d1||i==1, listput(Lr,""); p1=i+1; vo=v1; next); |
|||
if(i-p1>0, listput(Lr,ssubstr(str,p1,i-p1)); p1=i+1); |
|||
vo=v1; |
|||
); |
|||
return(Lr); |
|||
} |
|||
\\ SF#3 sreverse(): Return the reversed string str. 3/3/2016 |
|||
sreverse(str)={return(Strchr(Vecrev(Vecsmall(str))))} |
|||
\\ SF#4 srepeat(): Repeat a string str the specified number of times ntimes |
|||
\\ and return composed string. 3/3/2016 |
|||
srepeat(str,ntimes)={ |
|||
my(srez=str,nt=ntimes-1); |
|||
if(ntimes<1||#str==0,return("")); |
|||
if(ntimes==1,return(str)); |
|||
for(i=1,nt, srez=concat(srez,str)); |
|||
return(srez); |
|||
} |
|||
\\ SF#5 <<coming soon>> |
|||
</lang> |