User:AnatolV/Helper Functions

From Rosetta Code
Revision as of 20:48, 3 April 2017 by rosettacode>AnatolV (adding funcs, editing)

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

<<coming soon>>