User:AnatolV/Helper Functions: Difference between revisions

From Rosetta Code
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>

Revision as of 23:25, 4 April 2017

This page presents only helper functions that are generic enough. They were used in one or more contributions on RC and will be used again and again.
Note: Many samples of using these functions can be found here on RC.

R Helper Functions

One of the R's great powers is its unlimited number of great and good packages, virtually thousands of them. For any applications big or small you can find a package.
E.g., in the case of Voronoi diagram there are many of packages: deldir, alphahull, dismo, ggplot, ggplot2, tripack, CGAL, etc. Not to mention all linked packages. Do you need random colors? Again, find a few packages more...
So, sometimes you can save time and a hard drive space avoiding downloading and installing packages, and using a few helper functions instead.

Note
  • All plotting helper functions are using matrix mat or 2 vectors X,Y from the dump file created by plotmat().
  • The file names used are without extension (which will be added as ".png", ".dmp" and ".dat" when needed).
  • Requesting dump file is useful if the generating/plotting time is big. Having a dump file makes it easy and fast to repeat plotting with different colors, titles, etc.
  • If number of generated dots is very big then plotting from a dump file could be very slow too. Actually, plotv2() shows almost "pure" plotting time.
Works with: R version 3.3.1 and above

<lang r>

    1. HFR#1 plotmat(): Simple plotting using matrix mat (filled with 0/1). v. 8/31/16
  1. Where: mat - matrix; fn - file name; clr - color; ttl - plot title;
  2. dflg - writing dump file flag (0-no/1-yes): psz - picture size.

plotmat <- function(mat, fn, clr, ttl, dflg=0, psz=600) {

 m <- nrow(mat); d <- 0;
 X=NULL; Y=NULL;
 pf = paste0(fn, ".png"); df = paste0(fn, ".dmp");
 for (i in 1:m) {
   for (j in 1:m) {if(mat[i,j]==0){next} else {d=d+1; X[d] <- i; Y[d] <- j;} }
 };
 cat(" *** Matrix(", m,"x",m,")", d, "DOTS\n");
 # Dumping if requested (dflg=1).
 if (dflg==1) {dump(c("X","Y"), df); cat(" *** Dump file:", df, "\n")};
 # Plotting
 plot(X,Y, main=ttl, axes=FALSE, xlab="", ylab="", col=clr, pch=20);
 dev.copy(png, filename=pf, width=psz, height=psz);
 # Cleaning 
 dev.off(); graphics.off();

}

    1. HFR#2 plotv2(): Simple plotting using 2 vectors (dumped into ".dmp" file).
  1. Where: fn - file name; clr - color; ttl - plot title; psz - picture size.
  2. v. 8/31/16

plotv2 <- function(fn, clr, ttl, psz=600) {

 cat(" *** START:", date(), "clr=", clr, "psz=", psz, "\n");
 cat(" *** File name -", fn, "\n");
 pf = paste0(fn, ".png"); df = paste0(fn, ".dmp");
 source(df);
 d <- length(X);
 cat(" *** Source dump-file:", df, d, "DOTS\n");
 cat(" *** Plot file -", pf, "\n");
 # Plotting
 plot(X, Y, main=ttl, axes=FALSE, xlab="", ylab="", col=clr, pch=20);
 # Writing png-file
 dev.copy(png, filename=pf, width=psz, height=psz);
 # Cleaning 
 dev.off(); graphics.off();
 cat(" *** END:", date(), "\n");

}

    1. HFR#3 randHclr(): Return random hex color.

randHclr <- function() {

 m=255; r=g=b=0;
 r <- sample(0:m, 1, replace=TRUE);
 g <- sample(0:m, 1, replace=TRUE);
 b <- sample(0:m, 1, replace=TRUE);
 return(rgb(r,g,b,maxColorValue=m));

} </lang>

JavaScript Helper Functions

JavaScript has many standard libraries becoming embedded objects. e.g., Math, Date, Array, RegExp, etc. But also there are many custom "monster" like (in size and documentation) libraries, e.g., D3.js, Node.js, Riot.js, jQuery, Dojo, YUI, etc.
The problem with these libraries is that they are forcing you to learn, actually, "new" language. In addition, there is no guarantee, that they would be stable and maintained well.
The good alternative is always to use just 2-3 small helper functions if it is possible for your task. <lang javascript> // HFJS#1 Like in PARI/GP: return random number 0..max-1. function randgp(max) {return Math.floor(Math.random()*max)}

// HFJS#2 Return random hex color. function randhclr() {

 return "#"+
 ("00"+randgp(256).toString(16)).slice(-2)+
 ("00"+randgp(256).toString(16)).slice(-2)+
 ("00"+randgp(256).toString(16)).slice(-2)

} </lang>

PARI/GP Helper Functions

PARI/GP plotting functions are good only for instant draft math function curves plotting. Not to mention bad color support and scaling.
In addition, there are no built-in string manipulation functions, except of concatenation.
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>