Skip to content

Instantly share code, notes, and snippets.

@philtsip
Last active March 11, 2022 21:12
Show Gist options
  • Save philtsip/6993133 to your computer and use it in GitHub Desktop.
Save philtsip/6993133 to your computer and use it in GitHub Desktop.
JavaScript Array Math Utils: Math.log Math.logArray Math.expArray Math.diffArray Math.roundArray Array.prototype.transpose addArray pastorFuture. Use these together for forecasting functions. Example: Math.roundArray(Math.diffArray(Math.logArray([10, 100, 1000, 10000])), 3);
/* JAVASCRIPT ARRAY MATH UTILS
Use these together for forecasting functions
Example: Math.roundArray(Math.diffArray(Math.logArray([10, 100, 1000, 10000])), 3);
Math.log
Math.logArray
Math.expArray
Math.diffArray
Math.roundArray
Array.prototype.transpose
addArray
pastorFuture
*/
// ARRAY MATH
// Math.log(number, base), e.g. Math.log(10, 2)
Math.log = (function() {
var log = Math.log;
return function(n, base) {
return log(n)/(base ? log(base) : 1);
};
})();
// Math.logArray([array], optional_base)
// Assumes natural log if base is omitted. Returns an array of logarithms
// Example: Math.logArray([5, 2], 10); Math.logArray([20, 7, Math.E]);
Math.logArray = (function() {
return function(input_array, base) {
var output_array = [];
if (input_array instanceof Array) {
for (var i = 0; i < input_array.length; i++) {
output_array.push(Math.log(input_array[i], base));
}
return output_array;
}
else
return null;
};
})();
// Math.expArray(array), e.g. Math.expArray([5, 2]);
Math.expArray = (function() {
return function(input_array) {
var output_array = [];
if (input_array instanceof Array) {
for (var i = 0; i < input_array.length; i++) {
output_array.push(Math.exp(input_array[i]));
}
return output_array;
}
else
return null;
};
})();
// Math.diffArray([array])
// Returns an array of differences between array elements. First element returned is NaN, null evaluates to 0
// Example: Math.diffArray([5, 2]);
Math.diffArray = (function() {
return function(input_array) {
var output_array = [];
if (input_array instanceof Array) {
for (var i = 0; i < input_array.length; i++) {
output_array.push(input_array[i] - input_array[i-1]);
}
return output_array;
}
else
return null;
};
})();
// Math.roundArray([array], optional_decimal_places)
// Assumes 0 decimal places as the default
// Example: Math.roundArray([5.2222, 2.5555], 3);
// Will not work for ([1.005],2) which comes out as [1] instead of [1.01],
// see: http://stackoverflow.com/questions/11832914/round-up-to-2-decimal-places-in-javascript#comment26122771_11832950
Math.roundArray = (function() {
return function(input_array, decimals) {
var output_array = [];
if (!decimals)
decimals = 0;
if (input_array instanceof Array) {
for (var i = 0; i < input_array.length; i++) {
output_array.push(Math.round(input_array[i] * Math.pow(10,decimals)) / Math.pow(10,decimals));
}
return output_array;
}
else
return null;
};
})();
// ARRAY TRANSFORMATIONS
// http://stackoverflow.com/questions/4492678/to-swap-rows-with-columns-of-matrix-in-javascript-or-jquery
Array.prototype.transpose = function() {
// Calculate the width and height of the Array
var a = this,
w = a.length ? a.length : 0,
h = a[0] instanceof Array ? a[0].length : 0;
// In case it is a zero matrix, no transpose routine needed.
if(h === 0 || w === 0) { return []; }
/**
* @var {Number} i Counter
* @var {Number} j Counter
* @var {Array} t Transposed data is stored in this array.
*/
var i, j, t = [];
// Loop through every item in the outer array (height)
for(i=0; i<h; i++) {
// Insert a new row (array)
t[i] = [];
// Loop through every item per item in outer array (width)
for(j=0; j<w; j++) {
// Save transposed data.
t[i][j] = a[j][i];
}
}
return t;
};
// assume both arrays are sorted by first column
// second array must have exactly 2 columns
// first array must include all years
// a = [[2007,2], [2008, 4]];
// b = [[2008, 18],[2009, 4], [2010, 8]];
function addArray (output_array, second_array) {
if (second_array.length > 0 && second_array[0].length == 2) {
// iterate through each row of output_array
for (var i = 0; i < output_array.length; i++) {
var j = 0;
while ((j < second_array.length) && (output_array[i][0] > second_array[j][0]))
j++;
if ((j < second_array.length) && (output_array[i][0] == second_array[j][0]))
output_array[i].push(second_array[j][1]);
else
output_array[i].push(null);
}
}
return output_array;
}
// assume array is sorted by first column
// assume array has exactly 2 columns
// a = [[2008, 18],[2009, 4], [2010, 8], [2012, 8], [2013, 8], [2014, 8]];
function pastorFuture(array) {
var array_length = array.length;
// if (array_length)
// var array_width = array[0].length;
var year = new Date().getFullYear();
if (array_length && array[0][0] <= year && array[array_length-1][0] > year) {
for (var i = 0; i < array_length; i++) {
if (array[i][0] < year)
array[i].push(null);
else if (array[i][0] == year)
array[i].push(array[i][1]);
else
array[i] = [array[i][0],null,array[i][1]];
}
}
return array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment