User:AnatolV/Helper Functions: Difference between revisions

m
(adding gnuplot sample, editing)
m (→‎String Functions: minor edit)
 
(5 intermediate revisions by the same user not shown)
Line 2:
;Note:
* Good helper function usually encapsulates typical set of actions for current tasks, supplies often used default values, interacting with other helper functions, etc.
* Many samples of using presented functions can be found here on RC.<br><br>
* Many of these functions are already here on RC. But some of them are upgraded. So, this is the place to get latest versions.
<br><br>
=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.<br>
Line 75 ⟶ 77:
It reminds me OOP (Object Oriented Programming) hype! But OOP is dead already, and
now declarative JavaScript is hyped??
Don't let you be foolishtricked again. Remember: you would need to learn and use "monster" libraries!?<br><br>
What "declarative JavaScript" is promising (and actually partially giving you by presented new functions, tags, etc.)?<br>
This is a fantastically sounding: "You are telling any JavaScript what to do, but not programming actually".
Think! It is the same as good HFJS is doing! For example, using randhclr() and pmat01() is the same
as "telling a JavaScript": "Use the random hex color and plot a matrix you have". It's just 1 statement: pmat01(mat,randhclr()).<br><br>
pmat01(mat,randhclr()).
 
In other words, look at these new libraries as a probable set of good helper functions for your projects.<br>
So, all these new libraries are good for the certain type of projects. Especially, especially if a company, which
have created this particular library and using it for inner projects. The good such sample is Facebook JavaScript library React.js, which- is having/supporting a lotsolid ofcompany uniqueand functionswill andsupport this tagslibrary. Think again:
The good such sample is Facebook's JavaScript library React.js, which is having/supporting a lot of unique functions
do you need all of them?
and tags.<br> Think again: do you need all of them?
 
<lang javascript>
// HFJS#3old: v.1.1.Plot Likeany inmatrix PARI/GP:mat return(filled random numberwith 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)
}
 
// HFJS#3: Plot any matrix mat (filled with 0,1)
function pmat01(mat, color) {
// DCLs
var cvs = document.getElementById('canvIdcvsId');
var ctx = cvs.getContext("2d");
var w = cvs.width; var h = cvs.height;
Line 115 ⟶ 107:
}//fend i
}//func end
 
// VOE.js v. 2.0 Generic JavaScript Plotting Helper Functions
// aev 10/01/17
// HFJS#1 Like in PARI/GP: return random number 0..max-1. 6/17/16
function randgp(max) {return Math.floor(Math.random()*max)}
 
// HFJS#2 Return random hex color as #RRGGBB. 3/10/17
function randhclr() {
return "#"+
("00"+randgp(256).toString(16)).slice(-2)+
("00"+randgp(256).toString(16)).slice(-2)+
("00"+randgp(256).toString(16)).slice(-2)
}
 
// HFJS#3: v.2.0 Plot any matrix mat (filled with 0 & not 0 items). 7/23/16
// Set error p in the page: <p id="epId"></p>.
// (Note: this is optional, see below);
// fgc/bgc - FG/BG colors; sc - scale (0..max); rt - rotate 90 degree
// if true (once).
function pmat01(mat, fgc, bgc, sc, rt) {
// Setting basic vars for canvas and matrix
var cvs = document.getElementById('cvsId');
var ctx = cvs.getContext("2d");
ctx.save();
var w = cvs.width, h = cvs.height;
var m = mat[0].length, n = mat.length, k=0;
console.log("MAT rxc", n, "x", m); // n - rows. m - cols
if(n<21&&m<21) {matl2cons("MAT2PLOT",mat)}
// Setting BG and plotting FG colors
ctx.fillStyle=bgc; ctx.fillRect(0,0,w,h);
ctx.fillStyle=fgc;
if(sc!=1) {ctx.scale(sc,sc)};
// Plotting "dots" (!=0 values in matrix) & counting them.
// n - rows. m - cols
for(var i=0; i<n; i++) {
for(var j=0; j<m; j++) {
if(mat[i][j]!=0) { k++;
if(rt&&n==m) {ctx.fillRect(i,j,1,1)} else {ctx.fillRect(j,i,1,1)}
};
}//fend j
}//fend i
// Outputting matrix "signature": matrix sizes & number of dots.
var ms="Matrix("+n+"x"+m+") "+k+" dots";
// Set error p
//var ep=document.getElementById("epId").innerHTML;
//document.getElementById("epId").innerHTML=ep+"<br>"+ms;
console.log("Matrix 'signature':", ms); //matrix "signature"
ctx.restore();
}//func end
 
// HFJS#4: Helper mini-functions for testing. 7/21/16
// Log title and matrix mat to console
function matl2cons(title,mat) {console.log(title); console.log(mat.join`\n`)}
// Print title to document
function pttl2doc(title) {document.write('<b>'+title+'</b><br />')}
// Print title and matrix mat to document
function matp2doc(title,mat) {
document.write('<b>'+title+'</b>:<br />');
for (var i = 0; i < mat.length; i++) {
document.write('&nbsp;&nbsp;'+mat[i].join(' ')+'<br />') }}
// Fractal matrix ASCII "pretty" printing to document.
// mat should be filled with 0 and not 0 integer;
// chr is a char substituting latter.
function matpp2doc(title,mat,chr) {
var i,j,re='',e; var m=mat.length; var n=mat[0].length;
document.write('&nbsp;&nbsp;<b>'+title+'</b>:<pre>');
for(var i=0; i<m; i++) {
for(var j=0; j<n; j++) {
e='&nbsp;'; if(mat[i][j]!=0) {e=chr}; re+=e;
}//fend j
document.write('&nbsp;&nbsp;'+re+'<br />'); re='';
}//fend i
document.write('</pre>');
}
 
// HFJS#5: Return the Kronecker product of the a and b matrices. 8/19/16
// Note: both a and b must be matrices, i.e., 2D rectangular arrays.
function mkp2(a,b) {
var m=a.length, n=a[0].length, p=b.length, q=b[0].length;
var rtn=m*p, ctn=n*q; var r=new Array(rtn);
// Building final matrix filled with zeros.
for (var i=0; i<rtn; i++) {r[i]=new Array(ctn)
for (var j=0;j<ctn;j++) {r[i][j]=0} }
// Calculating final KP matrix.
for (var i=0; i<m; i++) {
for (var j=0; j<n; j++) {
for (var k=0; k<p; k++) {
for (var l=0; l<q; l++) {
r[p*i+k][q*j+l]=a[i][j]*b[k][l];
}}}}//all4forend
return(r);
}
 
// HFJS#6: Kronecker power of a matrix. Where: m - initial matrix, n - power
function matkronpow(m,n) {
if(n<2) {return(m)}; var fm=m;
for(var i=1; i<n; i++) {fm=mkp2(fm,m)};
return(fm);
}
 
// HFJS#7: Create and plot Kronecker product based fractal from matrix m
// (filled with 0 & not 0 integers); ord - order. 8/19/16
function cpmat(m, ord, fgc, bgc, sc, rt) {
var kpr=matkronpow(m,ord);
pmat01(kpr, fgc, bgc, sc, rt);
}
 
// HFJS#8: Return the Kronecker product based fractal matrix width. 4/2/17
function fmw(m,ord) {var w0=m.length, w=Math.pow(w0, ord); return(w) }
//
//VOE.js END
</lang>
 
Line 122 ⟶ 225:
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.
Many of these functions are already here on RC.
<lang parigp>
\\ 2 old plotting helper functions 3/2/16
Line 158 ⟶ 262:
 
\\ HFGP#5 iPlotmat() Improved simple plotting using matrix mat (color and scaling added).
\\ Matrix should be filled with 0/1. 7/6/16, upgraded 4/8/17
\\ 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);
Line 205 ⟶ 309:
</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>
Anonymous user