time-to-botec/squiggle/node_modules/jstat/dist/jstat.min.js

2 lines
72 KiB
JavaScript
Raw Normal View History

(function(window,factory){if(typeof exports==="object"){module.exports=factory()}else if(typeof define==="function"&&define.amd){define(factory)}else{window.jStat=factory()}})(this,function(){var jStat=function(Math,undefined){var concat=Array.prototype.concat;var slice=Array.prototype.slice;var toString=Object.prototype.toString;function calcRdx(n,m){var val=n>m?n:m;return Math.pow(10,17-~~(Math.log(val>0?val:-val)*Math.LOG10E))}var isArray=Array.isArray||function isArray(arg){return toString.call(arg)==="[object Array]"};function isFunction(arg){return toString.call(arg)==="[object Function]"}function isNumber(num){return typeof num==="number"?num-num===0:false}function toVector(arr){return concat.apply([],arr)}function jStat(){return new jStat._init(arguments)}jStat.fn=jStat.prototype;jStat._init=function _init(args){if(isArray(args[0])){if(isArray(args[0][0])){if(isFunction(args[1]))args[0]=jStat.map(args[0],args[1]);for(var i=0;i<args[0].length;i++)this[i]=args[0][i];this.length=args[0].length}else{this[0]=isFunction(args[1])?jStat.map(args[0],args[1]):args[0];this.length=1}}else if(isNumber(args[0])){this[0]=jStat.seq.apply(null,args);this.length=1}else if(args[0]instanceof jStat){return jStat(args[0].toArray())}else{this[0]=[];this.length=1}return this};jStat._init.prototype=jStat.prototype;jStat._init.constructor=jStat;jStat.utils={calcRdx:calcRdx,isArray:isArray,isFunction:isFunction,isNumber:isNumber,toVector:toVector};jStat._random_fn=Math.random;jStat.setRandom=function setRandom(fn){if(typeof fn!=="function")throw new TypeError("fn is not a function");jStat._random_fn=fn};jStat.extend=function extend(obj){var i,j;if(arguments.length===1){for(j in obj)jStat[j]=obj[j];return this}for(i=1;i<arguments.length;i++){for(j in arguments[i])obj[j]=arguments[i][j]}return obj};jStat.rows=function rows(arr){return arr.length||1};jStat.cols=function cols(arr){return arr[0].length||1};jStat.dimensions=function dimensions(arr){return{rows:jStat.rows(arr),cols:jStat.cols(arr)}};jStat.row=function row(arr,index){if(isArray(index)){return index.map(function(i){return jStat.row(arr,i)})}return arr[index]};jStat.rowa=function rowa(arr,i){return jStat.row(arr,i)};jStat.col=function col(arr,index){if(isArray(index)){var submat=jStat.arange(arr.length).map(function(){return new Array(index.length)});index.forEach(function(ind,i){jStat.arange(arr.length).forEach(function(j){submat[j][i]=arr[j][ind]})});return submat}var column=new Array(arr.length);for(var i=0;i<arr.length;i++)column[i]=[arr[i][index]];return column};jStat.cola=function cola(arr,i){return jStat.col(arr,i).map(function(a){return a[0]})};jStat.diag=function diag(arr){var nrow=jStat.rows(arr);var res=new Array(nrow);for(var row=0;row<nrow;row++)res[row]=[arr[row][row]];return res};jStat.antidiag=function antidiag(arr){var nrow=jStat.rows(arr)-1;var res=new Array(nrow);for(var i=0;nrow>=0;nrow--,i++)res[i]=[arr[i][nrow]];return res};jStat.transpose=function transpose(arr){var obj=[];var objArr,rows,cols,j,i;if(!isArray(arr[0]))arr=[arr];rows=arr.length;cols=arr[0].length;for(i=0;i<cols;i++){objArr=new Array(rows);for(j=0;j<rows;j++)objArr[j]=arr[j][i];obj.push(objArr)}return obj.length===1?obj[0]:obj};jStat.map=function map(arr,func,toAlter){var row,nrow,ncol,res,col;if(!isArray(arr[0]))arr=[arr];nrow=arr.length;ncol=arr[0].length;res=toAlter?arr:new Array(nrow);for(row=0;row<nrow;row++){if(!res[row])res[row]=new Array(ncol);for(col=0;col<ncol;col++)res[row][col]=func(arr[row][col],row,col)}return res.length===1?res[0]:res};jStat.cumreduce=function cumreduce(arr,func,toAlter){var row,nrow,ncol,res,col;if(!isArray(arr[0]))arr=[arr];nrow=arr.length;ncol=arr[0].length;res=toAlter?arr:new Array(nrow);for(row=0;row<nrow;row++){if(!res[row])res[row]=new Array(ncol);if(ncol>0)res[row][0]=arr[row][0];for(col=1;col<ncol;col++)res[row][col]=func(res[row][col-1],arr[row][col])}return res.length===1?res[0]:res};jStat.alter=function alter(arr,func){return jStat.map(arr,func,true)};jStat.create=function create(rows,cols,func){var res=new Array(rows);var i,j;if(isFunction(cols)){func