diff --git a/lib/numbers/matrix.js b/lib/numbers/matrix.js index 017fa4b..16d0d10 100644 --- a/lib/numbers/matrix.js +++ b/lib/numbers/matrix.js @@ -20,6 +20,22 @@ var matrix = exports; +var ERROR_MATRIX_NOT_SQUARE = 'Matrix must be square.', + ERROR_VECTOR_NOT_2D = 'Only two dimensional operations are supported at this time.'; + +/** + * Check to see if a point is 2D. Used in all 2D vector functions. + * Throw error if it's not. + * + * @param {Array} point in question. + * @return {undefined} nothing is returned. + */ +matrix._check2DVector = function(point) { + if (point.length !== 2) { + throw new Error(ERROR_VECTOR_NOT_2D); + } +}; + /** * Return a deep copy of the input matrix. * @@ -45,6 +61,7 @@ matrix.deepCopy = function(arr) { * Return true if matrix is square, false otherwise. * * @param {Array} arr + * @return {Boolean} */ matrix.isSquare = function(arr) { if (!Array.isArray(arr)) { @@ -55,7 +72,7 @@ matrix.isSquare = function(arr) { var rows = arr.length; for (var i = 0; i < rows; i++) { - if (arr[i].length != rows) return false; + if (arr[i].length !== rows) return false; } return true; @@ -69,29 +86,29 @@ matrix.isSquare = function(arr) { * @return {Array} summed matrix. */ matrix.addition = function (arrA, arrB) { - if ((arrA.length === arrB.length) && (arrA[0].length === arrB[0].length)) { - var result = new Array(arrA.length), - i; - - if (!arrA[0].length) { - // The arrays are vectors. - for (i = 0; i < arrA.length; i++) { - result[i] = arrA[i] + arrB[i]; - } - } else { - for (i = 0; i < arrA.length; i++) { - result[i] = new Array(arrA[i].length); - - for (var j = 0; j < arrA[i].length; j++) { - result[i][j] = arrA[i][j] + arrB[i][j]; - } - } - } + if (arrA.length !== arrB.length || arrA[0].length !== arrB[0].length) { + throw new Error('Matrix mismatch'); + } - return result; + var result = new Array(arrA.length), + i; + + if (!arrA[0].length) { + // The arrays are vectors. + for (i = 0; i < arrA.length; i++) { + result[i] = arrA[i] + arrB[i]; + } } else { - throw new Error('Matrix mismatch'); + for (i = 0; i < arrA.length; i++) { + result[i] = new Array(arrA[i].length); + + for (var j = 0; j < arrA[i].length; j++) { + result[i][j] = arrA[i][j] + arrB[i][j]; + } + } } + + return result; }; /** @@ -102,29 +119,29 @@ matrix.addition = function (arrA, arrB) { * @return {Array} subtracted matrix. */ matrix.subtraction = function (arrA, arrB) { - if ((arrA.length === arrB.length) && (arrA[0].length === arrB[0].length)) { - var result = new Array(arrA.length), - i; - - if (!arrA[0].length) { - // The arrays are vectors. - for (i = 0; i < arrA.length; i++) { - result[i] = arrA[i] - arrB[i]; - } - } else { - for (i = 0; i < arrA.length; i++) { - result[i] = new Array(arrA[i].length); - - for (var j = 0; j < arrA[i].length; j++) { - result[i][j] = arrA[i][j] - arrB[i][j]; - } - } - } + if (arrA.length !== arrB.length || arrA[0].length !== arrB[0].length) { + throw new Error("Matrix mismatch"); + } - return result; + var result = new Array(arrA.length), + i; + + if (!arrA[0].length) { + // The arrays are vectors. + for (i = 0; i < arrA.length; i++) { + result[i] = arrA[i] - arrB[i]; + } } else { - throw new Error('Matrix mismatch'); + for (i = 0; i < arrA.length; i++) { + result[i] = new Array(arrA[i].length); + + for (var j = 0; j < arrA[i].length; j++) { + result[i][j] = arrA[i][j] - arrB[i][j]; + } + } } + + return result; }; /** @@ -191,15 +208,15 @@ matrix.identity = function (n) { * @return {Array} dot product. */ matrix.dotproduct = function (vectorA, vectorB) { - if (vectorA.length === vectorB.length) { - var result = 0; - for (var i = 0; i < vectorA.length; i++) { - result += vectorA[i] * vectorB[i]; - } - return result; - } else { + if (vectorA.length !== vectorB.length) { throw new Error("Vector mismatch"); } + + var result = 0; + for (var i = 0; i < vectorA.length; i++) { + result += vectorA[i] * vectorB[i]; + } + return result; }; /** @@ -213,24 +230,24 @@ matrix.dotproduct = function (vectorA, vectorB) { * @return {Array} result of multiplied matrices. */ matrix.multiply = function (arrA, arrB) { - if (arrA[0].length === arrB.length) { - var result = new Array(arrA.length); - - for (var x = 0; x < arrA.length; x++) { - result[x] = new Array(arrB[0].length); - } + if (arrA[0].length !== arrB.length) { + throw new Error("Matrix mismatch"); + } - var arrB_T = matrix.transpose(arrB); - - for (var i = 0; i < result.length; i++) { - for (var j = 0; j < result[i].length; j++) { - result[i][j] = matrix.dotproduct(arrA[i],arrB_T[j]); - } + var result = new Array(arrA.length); + + for (var x = 0; x < arrA.length; x++) { + result[x] = new Array(arrB[0].length); + } + + var arrB_T = matrix.transpose(arrB); + + for (var i = 0; i < result.length; i++) { + for (var j = 0; j < result[i].length; j++) { + result[i][j] = matrix.dotproduct(arrA[i],arrB_T[j]); } - return result; - } else { - throw new Error("Array mismatch"); } + return result; }; /** @@ -247,14 +264,13 @@ matrix.determinant = function (m) { var row, col; var diagLeft, diagRight; - if (numRow !== numCol) { - throw new Error("Not a square matrix."); + if (!matrix.isSquare(m)) { + throw new Error(ERROR_MATRIX_NOT_SQUARE); } if (numRow === 1) { return m[0][0]; - } - else if (numRow === 2) { + } else if (numRow === 2) { return m[0][0] * m[1][1] - m[0][1] * m[1][0]; } @@ -293,7 +309,7 @@ matrix.determinant = function (m) { */ matrix.lupDecomposition = function(arr) { if (!matrix.isSquare(arr)) { - throw new Error("Matrix must be square."); + throw new Error(ERROR_MATRIX_NOT_SQUARE); } var size = arr.length; @@ -384,19 +400,17 @@ matrix.lupDecomposition = function(arr) { * @return {Array} vector. */ matrix.rotate = function (point, degree, direction) { - if (point.length === 2) { - var negate = direction === 'clockwise' ? -1 : 1; - var radians = degree * (Math.PI / 180); + matrix._check2DVector(point); - var transformation = [ - [Math.cos(radians), -1 * negate * Math.sin(radians)], - [negate * Math.sin(radians), Math.cos(radians)] - ]; + var negate = direction === 'clockwise' ? -1 : 1; + var radians = degree * (Math.PI / 180); - return matrix.multiply(transformation, point); - } else { - throw new Error('Only two dimensional operations are supported at this time'); - } + var transformation = [ + [Math.cos(radians), -1 * negate * Math.sin(radians)], + [negate * Math.sin(radians), Math.cos(radians)] + ]; + + return matrix.multiply(transformation, point); }; /** @@ -408,17 +422,14 @@ matrix.rotate = function (point, degree, direction) { * @return {Array} vector. */ matrix.scale = function (point, sx, sy) { - if (point.length === 2) { + matrix._check2DVector(point); - var transformation = [ - [sx, 0], - [0, sy] - ]; + var transformation = [ + [sx, 0], + [0, sy] + ]; - return matrix.multiply(transformation, point); - } else { - throw new Error('Only two dimensional operations are supported at this time'); - } + return matrix.multiply(transformation, point); }; /** @@ -430,19 +441,17 @@ matrix.scale = function (point, sx, sy) { * @return {Array} vector. */ matrix.shear = function (point, k, direction) { - if (point.length === 2) { - var xplaceholder = direction === 'xaxis' ? k : 0; - var yplaceholder = direction === 'yaxis' ? k : 0; + matrix._check2DVector(point); - var transformation = [ - [1, xplaceholder], - [yplaceholder, 1] - ]; + var xplaceholder = direction === 'xaxis' ? k : 0; + var yplaceholder = direction === 'yaxis' ? k : 0; - return matrix.multiply(transformation, point); - } else { - throw new Error('Only two dimensional operations are supported at this time'); - } + var transformation = [ + [1, xplaceholder], + [yplaceholder, 1] + ]; + + return matrix.multiply(transformation, point); }; /** @@ -454,30 +463,25 @@ matrix.shear = function (point, k, direction) { * @return {Array} vector. */ matrix.affine = function (point, tx, ty) { - if (point.length === 2) { - - var transformation = [ - [1, 0, tx], - [0, 1, ty], - [0, 0, 1 ] - ]; - - var newpoint = [ - [point[0][0]], - [point[1][0]], - [1] - ]; - - var transformed = matrix.multiply(transformation, newpoint); - - return [ - [transformed[0][0]], - [transformed[1][0]] - ]; - - } else { - throw new Error('Only two dimensional operations are supported at this time'); - } + matrix._check2DVector(point); + var transformation = [ + [1, 0, tx], + [0, 1, ty], + [0, 0, 1 ] + ]; + + var newpoint = [ + [point[0][0]], + [point[1][0]], + [1] + ]; + + var transformed = matrix.multiply(transformation, newpoint); + + return [ + [transformed[0][0]], + [transformed[1][0]] + ]; }; /** @@ -646,29 +650,28 @@ matrix.rowReduce = function(m, epsilon) { * @return {Array} inverted matrix. */ matrix.inverse = function(m) { + if (!matrix.isSquare(m)) { + throw new Error(ERROR_MATRIX_NOT_SQUARE); + } + var n = m.length, + identity = matrix.identity(n), i; - if (n === m[0].length) { - var identity = matrix.identity(n); - - // AI - for(i=0; i= M[0].length) { throw new Error('The specified column must be between 0 and the number of columns - 1.'); } for (var i=0; i= L.length) { - throw new Error('The desired order of the rows must start at 0 and end at the number of rows - 1.'); + throw new Error('The desired order of the columns must start at 0 and end at the number of columns - 1.'); } else { result.push(matrix.getCol(M, L[i]) ); } @@ -1155,6 +1158,28 @@ matrix.isLowerBand = function(M,p) { return result; }; +/** + * Add all of the elements in an array together except for the i'th one. + * This is a helper function for determining diagonal dominance, and it + * should be noted that each element is passed to Math.abs() beforehand. + * + * @param {Array} array + * @param {Int} index of element to ignore. + * @return {Number} sum. + */ +sumNondiagonalElements = function(arr, i) { + var sum = 0, + j; + + for (j=0; j= M[0].length) { throw new Error('The specified column must be between 0 and the number of columns - 1.'); } for (var i=0; i= L.length) { - throw new Error('The desired order of the rows must start at 0 and end at the number of rows - 1.'); + throw new Error('The desired order of the columns must start at 0 and end at the number of columns - 1.'); } else { result.push(matrix.getCol(M, L[i]) ); } @@ -2275,6 +2278,28 @@ matrix.isLowerBand = function(M,p) { return result; }; +/** + * Add all of the elements in an array together except for the i'th one. + * This is a helper function for determining diagonal dominance, and it + * should be noted that each element is passed to Math.abs() beforehand. + * + * @param {Array} array + * @param {Int} index of element to ignore. + * @return {Number} sum. + */ +sumNondiagonalElements = function(arr, i) { + var sum = 0, + j; + + for (j=0; je;e++){if("number"!=typeof r[e])throw new Error("All elements in array must be numbers");t-=r[e]}return t}throw new Error("Input must be of type Array")},n.product=function(r){if("[object Array]"===Object.prototype.toString.call(r)){var t=r[0];if("number"!=typeof t)throw new Error("All elements in array must be numbers");for(var e=1,n=r.length;n>e;e++){if("number"!=typeof r[e])throw new Error("All elements in array must be numbers");t*=r[e]}return t}throw new Error("Input must be of type Array")},n.square=function(r){return r*r},n.binomial=function(r,t){function e(r,t){return r>=0&&0===t?1:0===r&&t>0?0:n[r]&&n[r][t]>0?n[r][t]:(n[r]||(n[r]=[]),n[r][t]=e(r-1,t-1)+e(r-1,t),n[r][t])}var n=[];return e(r,t)},n.factorial=function(r){for(var t=2,e=1;r>=t;)e*=t++;return e},n.gcd=function(r,t){var e;if(r=+r,t=+t,r!==r||t!==t)return 0/0;if(1/0===r||r===-1/0||1/0===t||t===-1/0)return 1/0;if(r%1!==0||t%1!==0)throw new Error("Can only operate on integers");for(;t;)e=r%t,r=t,t=e;return r>0?r:-r},n.lcm=function(r,t){return Math.abs(r*t)/n.gcd(r,t)},n.random=function(r,t,e){if(0===r.length)throw new Error("Empty array");if(t>r.length&&!e)throw new Error("Quantity requested exceeds size of array");if(e===!0){var o,a=[];for(o=0;t>o;o++)a[o]=r[Math.floor(Math.random()*r.length)];return a}return n.shuffle(r).slice(0,t)},n.shuffle=function(r){for(var t,e,n=r.length;n;)e=Math.floor(Math.random()*n--),t=r[n],r[n]=r[e],r[e]=t;return r},n.max=function(r){if(!Array.isArray(r))throw new Error("Input must be of type Array");for(var t,e=-1/0,n=0,o=r.length;o>n;n++)if(t=+r[n],t>e&&(e=t),t!==t)return 0/0;return e},n.min=function(r){if(!Array.isArray(r))throw new Error("Input must be of type Array");for(var t,e=+1/0,n=0,o=r.length;o>n;n++)if(t=+r[n],e>t&&(e=t),t!==t)return 0/0;return e},n.range=function(r,t,e){var n,o,a=0;for(arguments.length<=1&&(t=r||0,r=0),e=e||1,r>t&&(e=0-Math.abs(e)),o=Math.max(Math.ceil((t-r)/e)+1,0),n=new Array(o);o>a;)n[a++]=r,r+=e;return n},n.isInt=function(r){return r%1===0},n.divMod=function(r,t){return n.isInt(r)&&n.isInt(t)?[Math.floor(r/t),r%t]:!1},n.powerMod=function(r,t,e){if(-1>t)return Math.pow(r,t)%e;if(0===t)return 1%e;if(t>=1){for(var o=1;t>0;)t%2===1&&(o=o*r%e),r=r*r%e,t>>=1;return o}return-1===t?n.modInverse(r,e):1>t?n.powerMod(r,Math.pow(t,-1),e):void 0},n.egcd=function(r,t){if(r=+r,t=+t,r!==r||t!==t)return[0/0,0/0,0/0];if(1/0===r||r===-1/0||1/0===t||t===-1/0)return[1/0,1/0,1/0];if(r%1!==0||t%1!==0)throw new Error("Can only operate on integers");var e,n,o,a,i=0>r?-1:1,u=0>t?-1:1,f=0,s=1,h=1,l=0;for(r=Math.abs(r),t=Math.abs(t);0!==r;)e=Math.floor(t/r),n=t%r,o=f-h*e,a=s-l*e,t=r,r=n,f=h,s=l,h=o,l=a;return[t,i*f,u*s]},n.modInverse=function(r,t){var e=n.egcd(r,t);if(1!=e[0])throw new Error("No modular inverse exists");return e[1]%t},n.numbersEqual=function(r,t,e){return e>r-t&&r-t>-e},n.fallingFactorial=function(r,t){var e=r-t+1,n=1;if(0>r)throw new Error("n cannot be negative");if(t>r)return 0;for(;r>=e;)n*=e++;return n}},{}],4:[function(r,t,e){function n(r,t,e){var n=(t+e)/2,o=Math.abs(e-t)/6;return o*(r(t)+4*r(n)+r(e))}function o(r,t,e,a,i){var u=t+e,f=n(r,t,u),s=n(r,u,e);return Math.abs(f+s-a)<=15*i?f+s+(f+s-a)/15:o(r,t,u,i/2,f)+o(r,u,e,i/2,s)}var a=r("../numbers"),i=e;i.pointDiff=function(r,t){var e=r(t-.001),n=r(t+.001);return(n-e)/.002},i.Riemann=function(r,t,e,n,o){var a,i=(e-t)/n,u=0;if("function"==typeof o)for(a=t;e>a;a+=i)u+=r(o(a,a+i));else for(a=t;e>a;a+=i)u+=r(a);return u*i},i.adaptiveSimpson=function(r,t,e,i){return i="undefined"==typeof i?a.EPSILON:i,o(r,t,e,n(r,t,e),i)},i.limit=function(r,t,e){if("left"===e)return r(t-1e-15);if("right"===e)return r(t+1e-15);if("middle"===e)return(i.limit(r,t,"left")+i.limit(r,t,"right"))/2;throw new Error("Approach not provided")},i.StirlingGamma=function(r){return Math.sqrt(2*Math.PI/r)*Math.pow(r/Math.E,r)},i.LanczosGamma=function(r){var t,e=[.9999999999998099,676.5203681218851,-1259.1392167224028,771.3234287776531,-176.6150291621406,12.507343278686905,-.13857109526572012,9984369578019572e-21,1.5056327351493116e-7],n=7;if(.5>r)return Math.PI/(Math.sin(Math.PI*r)*i.LanczosGamma(1-r));r-=1;var o=e[0],a=r+n+.5;for(t=1;t=t)throw new Error("Please use a positive integer for N.");var e=[];t=Math.ceil(t);for(var n=2;ne;e++){var n=(this.phase()+2*Math.PI*e)/r,a=Math.pow(this.magnitude(),1/r);t[e]=new o(a*Math.cos(n),a*Math.sin(n))}return t},o.prototype.sin=function(){var r=new o(Math.E,0),t=new o(0,1),e=new o(0,-1),n=r.complexPow(t.multiply(this)).subtract(r.complexPow(e.multiply(this)));return n.divide(new o(0,2))},o.prototype.cos=function(){var r=new o(Math.E,0),t=new o(0,1),e=new o(0,-1),n=r.complexPow(t.multiply(this)).add(r.complexPow(e.multiply(this)));return n.divide(new o(2,0))},o.prototype.tan=function(){return this.sin().divide(this.cos())},o.prototype.equals=function(r,t){return n.numbersEqual(this.re,r.re,t)&&n.numbersEqual(this.im,r.im,t)},t.exports=o},{"../numbers":2}],6:[function(r,t,e){var n=r("../numbers"),o=n.complex,a=e;a.segment=function(r,t,e){for(var n=[],o=t;o=t)return[new o(r[0],0)];if(Math.log(t)/Math.LN2%1!==0)throw new Error("Array length must be integer power of 2");for(var e=a.fft(a.segment(r,0,2)),n=a.fft(a.segment(r,1,2)),i=[],u=t/2,f=0;t>f;f++){var s=-2*Math.PI*f/t,h=new o(Math.cos(s),Math.sin(s));i[f]=u>f?e[f].add(h.multiply(n[f])):e[f-u].subtract(h.multiply(n[f-u]))}return i}},{"../numbers":2}],7:[function(r,t,e){var n=e;n.fibonacci=function(r){for(var t,e=function(r){for(var t,e=[];r>0;)t=2>r?r:r%2,r=Math.floor(r/2),e.push(t);return e.reverse()},n=1,o=0,a=1,i=e(r),u=0;ue;e++)if(r[e].length!=t)return!1;return!0},n.addition=function(r,t){if(r.length===t.length&&r[0].length===t[0].length){var e,n=new Array(r.length);if(r[0].length)for(e=0;ee;e++){t[e]=new Array(r);for(var n=0;r>n;n++)t[e][n]=e===n?1:0}return t},n.dotproduct=function(r,t){if(r.length===t.length){for(var e=0,n=0;ne;e++){for(n=r[0][e],o=r[0][e],t=1;a>t;t++)o*=r[t][((e+t)%i+i)%i],n*=r[t][((e-t)%i+i)%i];u+=o-n}return u},n.lupDecomposition=function(r){if(!n.isSquare(r))throw new Error("Matrix must be square.");var t,e=r.length,o=n.deepCopy(r),a=n.transpose(n.identity(e)),i=new Array(e);this.getL=function(r){for(var t=r[0].length,e=n.identity(t),o=0;t>o;o++)for(var a=0;t>a;a++)o>a&&(e[o][a]=r[o][a]);return e},this.getU=function(r){for(var t=r[0].length,e=n.identity(t),o=0;t>o;o++)for(var a=0;t>a;a++)a>=o&&(e[o][a]=r[o][a]);return e};for(var u=0;e>u;u++){var f;for(f=0;e>f;f++)i[f]=o[f][u];for(f=0;e>f;f++){t=o[f];for(var s=Math.min(f,u),h=0,l=0;s>l;l++)h+=t[l]*i[l];t[u]=i[f]-=h}var m=u;for(f=u+1;e>f;f++)Math.abs(i[f])>Math.abs(i[m])&&(m=f);if(m!=u&&(o=n.rowSwitch(o,m,u),a=n.rowSwitch(a,m,u)),e>u&&0!==o[u][u])for(f=u+1;e>f;f++)o[f][u]/=o[u][u]}return[this.getL(o),this.getU(o),a]},n.rotate=function(r,t,e){if(2===r.length){var o="clockwise"===e?-1:1,a=t*(Math.PI/180),i=[[Math.cos(a),-1*o*Math.sin(a)],[o*Math.sin(a),Math.cos(a)]];return n.multiply(i,r)}throw new Error("Only two dimensional operations are supported at this time")},n.scale=function(r,t,e){if(2===r.length){var o=[[t,0],[0,e]];return n.multiply(o,r)}throw new Error("Only two dimensional operations are supported at this time")},n.shear=function(r,t,e){if(2===r.length){var o="xaxis"===e?t:0,a="yaxis"===e?t:0,i=[[1,o],[a,1]];return n.multiply(i,r)}throw new Error("Only two dimensional operations are supported at this time")},n.affine=function(r,t,e){if(2===r.length){var o=[[1,0,t],[0,1,e],[0,0,1]],a=[[r[0][0]],[r[1][0]],[1]],i=n.multiply(o,a);return[[i[0][0]],[i[1][0]]]}throw new Error("Only two dimensional operations are supported at this time")},n.rowScale=function(r,t,e){for(var n=new Array(r.length),o=0;oMath.abs(r[s][f])&&(s=e);var h=r[f];if(r[f]=r[s],r[s]=h,Math.abs(r[f][f])<=a)return r;for(e=f;++e=0;){for(o=r[f][f],e=-1;++e=f;)r[e][n]-=r[f][n]*r[e][f]/o;for(r[f][f]/=o,n=i-1;++nt;t++)r[t]=r[t].concat(o[t]);for(r=n.GaussJordanEliminate(r),t=0;e>t;t++)r[t]=r[t].slice(e);return r}throw new Error("The given matrix must be square")},n.getCol=function(r,t){var e=[];if(0>t)throw new Error("The specified column must be a positive integer.");if(t>=r[0].length)throw new Error("The specified column must be between 0 and the number of columns - 1.");for(var n=0;n=t.length)throw new Error("The desired order of the rows must start at 0 and end at the number of rows - 1.");e.push(r[t[n]])}return e},n.reorderCols=function(r,t){var e=[];if(void 0===t)throw new Error("Please enter a desired reordering array.");if(t.length!==r[0].length)throw new Error("The reordered matrix must have the same number of columns as the original matrix.");for(var o=0;o=t.length)throw new Error("The desired order of the rows must start at 0 and end at the number of rows - 1.");e.push(n.getCol(r,t[o]))}return n.transpose(e)},n.reverseRows=function(r){for(var t=[],e=r.length-1;e>-1;e--)t.push(e);return n.reorderRows(r,t)},n.reverseCols=function(r){for(var t=[],e=r.length-1;e>-1;e--)t.push(e);return n.reorderCols(r,t)},n.zeros=function(r,t){var e=new Array(r);if(1>r||1>t)throw new Error("The matrix dimensions must be positive integers.");r=Math.ceil(r),t=Math.ceil(t);for(var n=0;r>n;n++){for(var o=new Array(t),a=0;t>a;a++)o[a]=0;e[n]=o}return e},n.zigzag=function(r,t,e){if(1>=r)throw new Error("Matrix size must be at least 2x2.");r=Math.ceil(r);var o=n.zeros(r,r),a=function(t){var e,n,o,a,i,u=!1,f=r*r,s=1,h=1;for(t[0][0]=f,t[r-1][r-1]=s,a=1;r>a;a++)u?(f-=4*h,s+=4*h,h++):(f--,s++),t[0][a]=f,t[r-1][r-1-a]=s,u=!u;var l=!0;for(a=1;r>a;a++){for(e=0,n=a,o=t[e][n],i=1;a+1>i;i++)l?o-=1:o+=1,e++,n--,t[e][n]=o;l=!l}for(l=r%2===0?!0:!1,a=1;r-1>a;a++){for(e=r-1,n=a,o=t[e][n],i=1;r-a>i;i++)l?o--:o++,e--,n++,t[e][n]=o;l=!l}return t},i=function(r){return n.transpose(a(r))},u=function(r){return n.reverseCols(a(r))},f=function(r){return n.reverseRows(m(u(r)))},s=function(r){return n.reverseRows(a(r))},h=function(r){return n.reverseRows(i(r))},l=function(r){return n.reverseCols(n.reverseRows(a(r)))},m=function(r){return n.transpose(l(r))};if("BR"===t&&"H"===e)return a(o);if("BR"===t&&"V"===e)return i(o);if("BL"===t&&"H"===e)return u(o);if("BL"===t&&"V"===e)return f(o);if("TR"===t&&"H"===e)return s(o);if("TR"===t&&"V"===e)return h(o);if("TL"===t&&"H"===e)return l(o);if("TL"===t&&"V"===e)return m(o);throw new Error("Enter the direction (V,H) and corner (BR,BL,TR,TL) correctly.")},n.vectorNorm=function(r,t){if(!Array.isArray(r)||0===r.length)throw new Error("Vector must be an array of at least length 1.");if("undefined"!=typeof t&&"number"!=typeof t)throw new Error("Norm order must be a number.");t="undefined"==typeof t?2:t;var e,n,o=r.length,a=0;switch(t){case 1/0:for(n=0;o>n;n++)e=Math.abs(r[n]),e>a&&(a=e);break;case-1/0:for(a=1/0,n=0;o>n;n++)e=Math.abs(r[n]),a>e&&(a=e);break;default:for(n=0;o>n;n++)a+=Math.pow(Math.abs(r[n]),t);a=Math.pow(a,1/t)}return a},n.matrixNorm=function(r,t){if(!Array.isArray(r)||0===r.length||!Array.isArray(r[0]))throw new Error("Matrix must be an array of at least length 1.");if("undefined"!=typeof t&&"number"!=typeof t&&null!==t)throw new Error("Norm order must be a number or null.");t="undefined"==typeof t?null:t;var e,n,o,a=r.length,i=r[0].length,u=0;switch(t){case 1/0:for(n=0;a>n;n++){for(e=0,o=0;i>o;o++)e+=Math.abs(r[n][o]);e>u&&(u=e)}break;case-1/0:for(u=1/0,n=0;a>n;n++){for(e=0,o=0;i>o;o++)e+=Math.abs(r[n][o]);u>e&&(u=e)}break;case 1:for(n=0;i>n;n++){for(e=0,o=0;a>o;o++)e+=Math.abs(r[o][n]);e>u&&(u=e)}break;case-1:for(u=1/0,n=0;i>n;n++){for(e=0,o=0;a>o;o++)e+=Math.abs(r[o][n]);u>e&&(u=e)}break;case null:for(n=0;a>n;n++)for(o=0;i>o;o++)u+=Math.pow(r[n][o],2);u=Math.pow(u,.5);break;case 2:throw new Error("Singular values are not yet supported in numbers.js.");case-2:throw new Error("Singular values are not yet supported in numbers.js.");default:for(n=0;a>n;n++)for(o=0;i>o;o++)u+=Math.pow(Math.abs(r[n][o]),t);u=Math.pow(u,1/t)}return u},n.isUpperBand=function(r,t){if(!Array.isArray(r)||!Array.isArray(r[0])||r.length<2)throw new Error("Matrix must be an array of at least dimension 2.");if("number"!=typeof t||0>t||t%1!==0)throw new Error("Upper bandwidth must be a nonzero integer.");for(var e=!0,n=r[0].length,o=0,a=t+1;n>a;a++){if(0!==r[o][a]){e=!1;break}o++}return e},n.isLowerBand=function(r,t){if(!Array.isArray(r)||!Array.isArray(r[0])||r.length<2)throw new Error("Matrix must be an array of at least dimension 2.");if("number"!=typeof t||0>t||t%1!==0)throw new Error("Lower bandwidth must be a nonzero integer.");for(var e=!0,n=r.length,o=0,a=t+1;n>a;a++){if(0!==r[a][o]){e=!1;break}o++}return e},n.isRowDD=function(r){var t=r.length;if(t!==r[0].length)throw new Error("The given matrix must be square.");if(void 0===r[0][0])throw new Error("Input must be a matrix.");for(var e=0;t>e;e++){var n,o=r[e],a=o[e],i=0;for(n=0;e>n;n++)i+=Math.abs(o[n]);for(n=e+1;t>n;n++)i+=Math.abs(o[n]);if(Math.abs(a)e;e++){var n,o=r[e],a=o[e],i=0;for(n=0;e>n;n++)i+=Math.abs(o[n]);for(n=e+1;t>n;n++)i+=Math.abs(o[n]);if(Math.abs(a)<=i)return!1}return!0},n.isColumnDD=function(r){var t=r.length;if(t!==r[0].length)throw new Error("The given matrix must be square.");if(void 0===r[0][0])throw new Error("Input must be a matrix.");for(var e=0;t>e;e++){var o,a=n.getCol(r,e),i=a[e],u=0;for(o=0;e>o;o++)u+=Math.abs(a[o]);for(o=e+1;t>o;o++)u+=Math.abs(a[o]);if(Math.abs(i)e;e++){var o,a=n.getCol(r,e),i=a[e],u=0;for(o=0;e>o;o++)u+=Math.abs(a[o]);for(o=e+1;t>o;o++)u+=Math.abs(a[o]);if(Math.abs(i)<=u)return!1}return!0}},{}],9:[function(r,t,e){var n=r("./basic"),o=e;o.simple=function(r){if(isNaN(r)||!isFinite(r)||r%1||2>r)return!1;if(r%2===0)return 2===r;if(r%3===0)return 3===r;for(var t=5,e=Math.sqrt(r);e>=t;t+=6)if(r%t===0||r%(t+2)===0)return!1;return!0},o.factorization=function(r){r=Math.floor(r);for(var t,e,n=[],o=Math.sqrt,a=r>1&&isFinite(r);a;){if(t=o(r),e=2,r%e)for(e=3;r%e&&(e+=2)t?r:e,n.push(e),a=e!==r,r/=e}return n},o.millerRabin=function(r,t){if(1===arguments.length&&(t=20),2===r)return!0;if(!n.isInt(r)||1>=r||r%2===0)return!1;for(var e=0,o=r-1;;){var a=n.divMod(o,2),i=a[0],u=a[1];if(1===u)break;e+=1,o=i}for(var f=function(t){if(1===n.powerMod(t,o,r))return!1;for(var a=0;e>a;a++)if(n.powerMod(t,Math.pow(2,a)*o,r)===r-1)return!1;return!0},s=0;t>s;s++){var h=2+Math.floor(Math.random()*(r-2-2));if(f(h))return!1}return!0},o.sieve=function(r){if(2>r)return[];for(var t=[2],e=3;r>=e;e++){var n=!1;for(var o in t)n=n||0===e%t[o];n||t.push(e)}return t},o.coprime=function(r,t){return 1===n.gcd(r,t)},o.getPerfectPower=function(r){var t=o.getPrimePower(r);return t&&t[1]>1?t:!1},o.getPrimePower=function(r){if(2>r)return!1;if(o.millerRabin(r))return[r,1];if(r%2===0)return[2,r.toString(2).length-1];var t=o.factorization(r);if(!t)return!1;for(var e=t.length,n=0;e>n;n++)for(var a=0,i=0;r>=a;){if(a=Math.pow(t[n],i),a/r===1)return[t[n],i];i++}return!1}},{"./basic":3}],10:[function(r,t,e){var n=(r("./basic"),e),o=Math.random;n.setGenerator=function(r){if("function"!=typeof r)throw new Error("Must pass a function");o=r},n.sample=function(r,t,e){var n=[];n.length=e;for(var a=0;e>a;a++)n[a]=r+(t-r)*o();return n},n.boxMullerTransform=function(r,t){arguments.length<=1&&(t=1),0===arguments.length&&(r=0);var e,n=0,a=0;do n=2*o()-1,a=2*o()-1,e=n*n+a*a;while(0===e||e>1);var i=Math.sqrt(-2*Math.log(e)/e),u=n*i,f=a*i;return u=r+u*t,f=r+f*t,[u,f]},n.irwinHall=function(r,t){1===arguments.length&&(t=0);for(var e=0,n=0;r>n;n++)e+=o();return e-t},n.bates=function(r,t,e){arguments.length<=2&&(e=0),1===arguments.length&&(t=1);for(var n=0,a=0;r>a;a++)n+=(t-e)*o()+e;return n/r},n.distribution={},n.distribution.normal=function(r,t,e){return arguments.length<=2&&(e=1),1===arguments.length&&(t=0),n.distribution.boxMuller(r,t,e)},n.distribution.logNormal=function(r,t,e){arguments.length<=2&&(e=1),1===arguments.length&&(t=0);var o=function(r){return Math.exp(r)};return n.distribution.boxMuller(r,t,e).map(o)},n.distribution.boxMuller=function(r,t,e,o){arguments.length<=3&&(o=!1),arguments.length<=2&&(e=1),1===arguments.length&&(t=0);for(var a=[],i=0;r>i;i++){var u=n.boxMullerTransform(t,e);a.push(o?u:u[0])}return a},n.distribution.irwinHall=function(r,t,e){arguments.length<=2&&(e=0),1===arguments.length&&(t=r);for(var o=new Array(r),a=0;r>a;a++)o[a]=n.irwinHall(t,e);return o},n.distribution.irwinHallNormal=function(r){return n.distribution.irwinHall(r,12,6)},n.distribution.bates=function(r,t,e){arguments.length<=2&&(e=0),1===arguments.length&&(t=r);for(var o=new Array(r),a=0;r>a;a++)o[a]=n.bates(r,t,e);return o}},{"./basic":3}],11:[function(r,t,e){var n=r("./basic"),o=e;o.mean=function(r){var t=r.length,e=n.sum(r);return e/t},o.median=function(r){return o.quantile(r,1,2)},o.mode=function(r){for(var t={},e=0,n=r.length;n>e;e++)void 0===t[r[e]]?t[r[e]]=0:t[r[e]]++;var o;for(var a in t)t.hasOwnProperty(a)&&(void 0===o||t[a]>t[o])&&(o=a);return Number(o)},o.quantile=function(r,t,e){var n,o,a;return 0===t?Math.min.apply(null,r):t===e?Math.max.apply(null,r):(n=r.slice(0),n.sort(function(r,t){return r-t}),o=n.length,a=o*t/e,a%1===0?.5*n[a-1]+.5*n[a]:n[Math.floor(a)])},o.report=function(r){return{mean:o.mean(r),firstQuartile:o.quantile(r,1,4),median:o.median(r),thirdQuartile:o.quantile(r,3,4),standardDev:o.standardDev(r)}},o.standardDev=function(r){for(var t=r.length,e=o.mean(r),a=[],i=0;iu;u++)o+=r[u]*t[u];return(o-a*i/e)/e}throw new Error("Array mismatch")}},{"./basic":3}]},{},[1])(1)}); \ No newline at end of file +!function(r){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=r();else if("function"==typeof define&&define.amd)define([],r);else{var t;"undefined"!=typeof window?t=window:"undefined"!=typeof global?t=global:"undefined"!=typeof self&&(t=self),t.numbers=r()}}(function(){return function r(t,e,n){function o(i,u){if(!e[i]){if(!t[i]){var f="function"==typeof require&&require;if(!u&&f)return f(i,!0);if(a)return a(i,!0);var s=new Error("Cannot find module '"+i+"'");throw s.code="MODULE_NOT_FOUND",s}var h=e[i]={exports:{}};t[i][0].call(h.exports,function(r){var e=t[i][1][r];return o(e?e:r)},h,h.exports,r,t,e,n)}return e[i].exports}for(var a="function"==typeof require&&require,i=0;ie;e++){if("number"!=typeof r[e])throw new Error("All elements in array must be numbers");t-=r[e]}return t}throw new Error("Input must be of type Array")},n.product=function(r){if("[object Array]"===Object.prototype.toString.call(r)){var t=r[0];if("number"!=typeof t)throw new Error("All elements in array must be numbers");for(var e=1,n=r.length;n>e;e++){if("number"!=typeof r[e])throw new Error("All elements in array must be numbers");t*=r[e]}return t}throw new Error("Input must be of type Array")},n.square=function(r){return r*r},n.binomial=function(r,t){function e(r,t){return r>=0&&0===t?1:0===r&&t>0?0:n[r]&&n[r][t]>0?n[r][t]:(n[r]||(n[r]=[]),n[r][t]=e(r-1,t-1)+e(r-1,t),n[r][t])}var n=[];return e(r,t)},n.factorial=function(r){for(var t=2,e=1;r>=t;)e*=t++;return e},n.gcd=function(r,t){var e;if(r=+r,t=+t,r!==r||t!==t)return 0/0;if(1/0===r||r===-1/0||1/0===t||t===-1/0)return 1/0;if(r%1!==0||t%1!==0)throw new Error("Can only operate on integers");for(;t;)e=r%t,r=t,t=e;return r>0?r:-r},n.lcm=function(r,t){return Math.abs(r*t)/n.gcd(r,t)},n.random=function(r,t,e){if(0===r.length)throw new Error("Empty array");if(t>r.length&&!e)throw new Error("Quantity requested exceeds size of array");if(e===!0){var o,a=[];for(o=0;t>o;o++)a[o]=r[Math.floor(Math.random()*r.length)];return a}return n.shuffle(r).slice(0,t)},n.shuffle=function(r){for(var t,e,n=r.length;n;)e=Math.floor(Math.random()*n--),t=r[n],r[n]=r[e],r[e]=t;return r},n.max=function(r){if(!Array.isArray(r))throw new Error("Input must be of type Array");for(var t,e=-1/0,n=0,o=r.length;o>n;n++)if(t=+r[n],t>e&&(e=t),t!==t)return 0/0;return e},n.min=function(r){if(!Array.isArray(r))throw new Error("Input must be of type Array");for(var t,e=+1/0,n=0,o=r.length;o>n;n++)if(t=+r[n],e>t&&(e=t),t!==t)return 0/0;return e},n.range=function(r,t,e){var n,o,a=0;for(arguments.length<=1&&(t=r||0,r=0),e=e||1,r>t&&(e=0-Math.abs(e)),o=Math.max(Math.ceil((t-r)/e)+1,0),n=new Array(o);o>a;)n[a++]=r,r+=e;return n},n.isInt=function(r){return r%1===0},n.divMod=function(r,t){return n.isInt(r)&&n.isInt(t)?[Math.floor(r/t),r%t]:!1},n.powerMod=function(r,t,e){if(-1>t)return Math.pow(r,t)%e;if(0===t)return 1%e;if(t>=1){for(var o=1;t>0;)t%2===1&&(o=o*r%e),r=r*r%e,t>>=1;return o}return-1===t?n.modInverse(r,e):1>t?n.powerMod(r,Math.pow(t,-1),e):void 0},n.egcd=function(r,t){if(r=+r,t=+t,r!==r||t!==t)return[0/0,0/0,0/0];if(1/0===r||r===-1/0||1/0===t||t===-1/0)return[1/0,1/0,1/0];if(r%1!==0||t%1!==0)throw new Error("Can only operate on integers");var e,n,o,a,i=0>r?-1:1,u=0>t?-1:1,f=0,s=1,h=1,l=0;for(r=Math.abs(r),t=Math.abs(t);0!==r;)e=Math.floor(t/r),n=t%r,o=f-h*e,a=s-l*e,t=r,r=n,f=h,s=l,h=o,l=a;return[t,i*f,u*s]},n.modInverse=function(r,t){var e=n.egcd(r,t);if(1!=e[0])throw new Error("No modular inverse exists");return e[1]%t},n.numbersEqual=function(r,t,e){return e>r-t&&r-t>-e},n.fallingFactorial=function(r,t){var e=r-t+1,n=1;if(0>r)throw new Error("n cannot be negative");if(t>r)return 0;for(;r>=e;)n*=e++;return n}},{}],4:[function(r,t,e){function n(r,t,e){var n=(t+e)/2,o=Math.abs(e-t)/6;return o*(r(t)+4*r(n)+r(e))}function o(r,t,e,a,i){var u=t+e,f=n(r,t,u),s=n(r,u,e);return Math.abs(f+s-a)<=15*i?f+s+(f+s-a)/15:o(r,t,u,i/2,f)+o(r,u,e,i/2,s)}var a=r("../numbers"),i=e;i.pointDiff=function(r,t){var e=r(t-.001),n=r(t+.001);return(n-e)/.002},i.Riemann=function(r,t,e,n,o){var a,i=(e-t)/n,u=0;if("function"==typeof o)for(a=t;e>a;a+=i)u+=r(o(a,a+i));else for(a=t;e>a;a+=i)u+=r(a);return u*i},i.adaptiveSimpson=function(r,t,e,i){return i="undefined"==typeof i?a.EPSILON:i,o(r,t,e,n(r,t,e),i)},i.limit=function(r,t,e){if("left"===e)return r(t-1e-15);if("right"===e)return r(t+1e-15);if("middle"===e)return(i.limit(r,t,"left")+i.limit(r,t,"right"))/2;throw new Error("Approach not provided")},i.StirlingGamma=function(r){return Math.sqrt(2*Math.PI/r)*Math.pow(r/Math.E,r)},i.LanczosGamma=function(r){var t,e=[.9999999999998099,676.5203681218851,-1259.1392167224028,771.3234287776531,-176.6150291621406,12.507343278686905,-.13857109526572012,9984369578019572e-21,1.5056327351493116e-7],n=7;if(.5>r)return Math.PI/(Math.sin(Math.PI*r)*i.LanczosGamma(1-r));r-=1;var o=e[0],a=r+n+.5;for(t=1;t=t)throw new Error("Please use a positive integer for N.");var e=[];t=Math.ceil(t);for(var n=2;ne;e++){var n=(this.phase()+2*Math.PI*e)/r,a=Math.pow(this.magnitude(),1/r);t[e]=new o(a*Math.cos(n),a*Math.sin(n))}return t},o.prototype.sin=function(){var r=new o(Math.E,0),t=new o(0,1),e=new o(0,-1),n=r.complexPow(t.multiply(this)).subtract(r.complexPow(e.multiply(this)));return n.divide(new o(0,2))},o.prototype.cos=function(){var r=new o(Math.E,0),t=new o(0,1),e=new o(0,-1),n=r.complexPow(t.multiply(this)).add(r.complexPow(e.multiply(this)));return n.divide(new o(2,0))},o.prototype.tan=function(){return this.sin().divide(this.cos())},o.prototype.equals=function(r,t){return n.numbersEqual(this.re,r.re,t)&&n.numbersEqual(this.im,r.im,t)},t.exports=o},{"../numbers":2}],6:[function(r,t,e){var n=r("../numbers"),o=n.complex,a=e;a.segment=function(r,t,e){for(var n=[],o=t;o=t)return[new o(r[0],0)];if(Math.log(t)/Math.LN2%1!==0)throw new Error("Array length must be integer power of 2");for(var e=a.fft(a.segment(r,0,2)),n=a.fft(a.segment(r,1,2)),i=[],u=t/2,f=0;t>f;f++){var s=-2*Math.PI*f/t,h=new o(Math.cos(s),Math.sin(s));i[f]=u>f?e[f].add(h.multiply(n[f])):e[f-u].subtract(h.multiply(n[f-u]))}return i}},{"../numbers":2}],7:[function(r,t,e){var n=e;n.fibonacci=function(r){for(var t,e=function(r){for(var t,e=[];r>0;)t=2>r?r:r%2,r=Math.floor(r/2),e.push(t);return e.reverse()},n=1,o=0,a=1,i=e(r),u=0;ue;e++)if(r[e].length!==t)return!1;return!0},n.addition=function(r,t){if(r.length!==t.length||r[0].length!==t[0].length)throw new Error("Matrix mismatch");var e,n=new Array(r.length);if(r[0].length)for(e=0;ee;e++){t[e]=new Array(r);for(var n=0;r>n;n++)t[e][n]=e===n?1:0}return t},n.dotproduct=function(r,t){if(r.length!==t.length)throw new Error("Vector mismatch");for(var e=0,n=0;ne;e++){for(a=r[0][e],i=r[0][e],t=1;u>t;t++)i*=r[t][((e+t)%f+f)%f],a*=r[t][((e-t)%f+f)%f];s+=i-a}return s},n.lupDecomposition=function(r){if(!n.isSquare(r))throw new Error(o);var t,e=r.length,a=n.deepCopy(r),i=n.transpose(n.identity(e)),u=new Array(e);this.getL=function(r){for(var t=r[0].length,e=n.identity(t),o=0;t>o;o++)for(var a=0;t>a;a++)o>a&&(e[o][a]=r[o][a]);return e},this.getU=function(r){for(var t=r[0].length,e=n.identity(t),o=0;t>o;o++)for(var a=0;t>a;a++)a>=o&&(e[o][a]=r[o][a]);return e};for(var f=0;e>f;f++){var s;for(s=0;e>s;s++)u[s]=a[s][f];for(s=0;e>s;s++){t=a[s];for(var h=Math.min(s,f),l=0,c=0;h>c;c++)l+=t[c]*u[c];t[f]=u[s]-=l}var m=f;for(s=f+1;e>s;s++)Math.abs(u[s])>Math.abs(u[m])&&(m=s);if(m!=f&&(a=n.rowSwitch(a,m,f),i=n.rowSwitch(i,m,f)),e>f&&0!==a[f][f])for(s=f+1;e>s;s++)a[s][f]/=a[f][f]}return[this.getL(a),this.getU(a),i]},n.rotate=function(r,t,e){n._check2DVector(r);var o="clockwise"===e?-1:1,a=t*(Math.PI/180),i=[[Math.cos(a),-1*o*Math.sin(a)],[o*Math.sin(a),Math.cos(a)]];return n.multiply(i,r)},n.scale=function(r,t,e){n._check2DVector(r);var o=[[t,0],[0,e]];return n.multiply(o,r)},n.shear=function(r,t,e){n._check2DVector(r);var o="xaxis"===e?t:0,a="yaxis"===e?t:0,i=[[1,o],[a,1]];return n.multiply(i,r)},n.affine=function(r,t,e){n._check2DVector(r);var o=[[1,0,t],[0,1,e],[0,0,1]],a=[[r[0][0]],[r[1][0]],[1]],i=n.multiply(o,a);return[[i[0][0]],[i[1][0]]]},n.rowScale=function(r,t,e){for(var n=new Array(r.length),o=0;oMath.abs(r[s][f])&&(s=e);var h=r[f];if(r[f]=r[s],r[s]=h,Math.abs(r[f][f])<=a)return r;for(e=f;++e=0;){for(o=r[f][f],e=-1;++e=f;)r[e][n]-=r[f][n]*r[e][f]/o;for(r[f][f]/=o,n=i-1;++nt;t++)r[t]=r[t].concat(a[t]);for(r=n.GaussJordanEliminate(r),t=0;e>t;t++)r[t]=r[t].slice(e);return r},n.getCol=function(r,t){var e=new Array(r.length);if(0>t)throw new Error("The specified column must be a positive integer.");if(t>=r[0].length)throw new Error("The specified column must be between 0 and the number of columns - 1.");for(var n=0;n=t.length)throw new Error("The desired order of the rows must start at 0 and end at the number of rows - 1.");e.push(r[t[n]])}return e},n.reorderCols=function(r,t){var e=[];if(void 0===t)throw new Error("Please enter a desired reordering array.");if(t.length!==r[0].length)throw new Error("The reordered matrix must have the same number of columns as the original matrix.");for(var o=0;o=t.length)throw new Error("The desired order of the columns must start at 0 and end at the number of columns - 1.");e.push(n.getCol(r,t[o]))}return n.transpose(e)},n.reverseRows=function(r){for(var t=[],e=r.length-1;e>-1;e--)t.push(e);return n.reorderRows(r,t)},n.reverseCols=function(r){for(var t=[],e=r.length-1;e>-1;e--)t.push(e);return n.reorderCols(r,t)},n.zeros=function(r,t){var e=new Array(r);if(1>r||1>t)throw new Error("The matrix dimensions must be positive integers.");r=Math.ceil(r),t=Math.ceil(t);for(var n=0;r>n;n++){for(var o=new Array(t),a=0;t>a;a++)o[a]=0;e[n]=o}return e},n.zigzag=function(r,t,e){if(1>=r)throw new Error("Matrix size must be at least 2x2.");r=Math.ceil(r);var o=n.zeros(r,r),a=function(t){var e,n,o,a,i,u=!1,f=r*r,s=1,h=1;for(t[0][0]=f,t[r-1][r-1]=s,a=1;r>a;a++)u?(f-=4*h,s+=4*h,h++):(f--,s++),t[0][a]=f,t[r-1][r-1-a]=s,u=!u;var l=!0;for(a=1;r>a;a++){for(e=0,n=a,o=t[e][n],i=1;a+1>i;i++)l?o-=1:o+=1,e++,n--,t[e][n]=o;l=!l}for(l=r%2===0?!0:!1,a=1;r-1>a;a++){for(e=r-1,n=a,o=t[e][n],i=1;r-a>i;i++)l?o--:o++,e--,n++,t[e][n]=o;l=!l}return t},i=function(r){return n.transpose(a(r))},u=function(r){return n.reverseCols(a(r))},f=function(r){return n.reverseRows(c(u(r)))},s=function(r){return n.reverseRows(a(r))},h=function(r){return n.reverseRows(i(r))},l=function(r){return n.reverseCols(n.reverseRows(a(r)))},c=function(r){return n.transpose(l(r))};if("BR"===t&&"H"===e)return a(o);if("BR"===t&&"V"===e)return i(o);if("BL"===t&&"H"===e)return u(o);if("BL"===t&&"V"===e)return f(o);if("TR"===t&&"H"===e)return s(o);if("TR"===t&&"V"===e)return h(o);if("TL"===t&&"H"===e)return l(o);if("TL"===t&&"V"===e)return c(o);throw new Error("Enter the direction (V,H) and corner (BR,BL,TR,TL) correctly.")},n.vectorNorm=function(r,t){if(!Array.isArray(r)||0===r.length)throw new Error("Vector must be an array of at least length 1.");if("undefined"!=typeof t&&"number"!=typeof t)throw new Error("Norm order must be a number.");t="undefined"==typeof t?2:t;var e,n,o=r.length,a=0;switch(t){case 1/0:for(n=0;o>n;n++)e=Math.abs(r[n]),e>a&&(a=e);break;case-1/0:for(a=1/0,n=0;o>n;n++)e=Math.abs(r[n]),a>e&&(a=e);break;default:for(n=0;o>n;n++)a+=Math.pow(Math.abs(r[n]),t);a=Math.pow(a,1/t)}return a},n.matrixNorm=function(r,t){if(!Array.isArray(r)||0===r.length||!Array.isArray(r[0]))throw new Error("Matrix must be an array of at least length 1.");if("undefined"!=typeof t&&"number"!=typeof t&&null!==t)throw new Error("Norm order must be a number or null.");t="undefined"==typeof t?null:t;var e,n,o,a=r.length,i=r[0].length,u=0;switch(t){case 1/0:for(n=0;a>n;n++){for(e=0,o=0;i>o;o++)e+=Math.abs(r[n][o]);e>u&&(u=e)}break;case-1/0:for(u=1/0,n=0;a>n;n++){for(e=0,o=0;i>o;o++)e+=Math.abs(r[n][o]);u>e&&(u=e)}break;case 1:for(n=0;i>n;n++){for(e=0,o=0;a>o;o++)e+=Math.abs(r[o][n]);e>u&&(u=e)}break;case-1:for(u=1/0,n=0;i>n;n++){for(e=0,o=0;a>o;o++)e+=Math.abs(r[o][n]);u>e&&(u=e)}break;case null:for(n=0;a>n;n++)for(o=0;i>o;o++)u+=Math.pow(r[n][o],2);u=Math.pow(u,.5);break;case 2:throw new Error("Singular values are not yet supported in numbers.js.");case-2:throw new Error("Singular values are not yet supported in numbers.js.");default:for(n=0;a>n;n++)for(o=0;i>o;o++)u+=Math.pow(Math.abs(r[n][o]),t);u=Math.pow(u,1/t)}return u},n.isUpperBand=function(r,t){if(!Array.isArray(r)||!Array.isArray(r[0])||r.length<2)throw new Error("Matrix must be an array of at least dimension 2.");if("number"!=typeof t||0>t||t%1!==0)throw new Error("Upper bandwidth must be a nonzero integer.");for(var e=!0,n=r[0].length,o=0,a=t+1;n>a;a++){if(0!==r[o][a]){e=!1;break}o++}return e},n.isLowerBand=function(r,t){if(!Array.isArray(r)||!Array.isArray(r[0])||r.length<2)throw new Error("Matrix must be an array of at least dimension 2.");if("number"!=typeof t||0>t||t%1!==0)throw new Error("Lower bandwidth must be a nonzero integer.");for(var e=!0,n=r.length,o=0,a=t+1;n>a;a++){if(0!==r[a][o]){e=!1;break}o++}return e},sumNondiagonalElements=function(r,t){var e,n=0;for(e=0;t>e;e++)n+=Math.abs(r[e]);for(e=t+1;ee;e++){var a=r[e],i=a[e],u=sumNondiagonalElements(a,e);if(Math.abs(i)e;e++){var a=r[e],i=a[e],u=sumNondiagonalElements(a,e);if(Math.abs(i)<=u)return!1}return!0},n.isColumnDD=function(r){if(!n.isSquare)throw new Error(o);for(var t=r.length,e=0;t>e;e++){var a=n.getCol(r,e),i=a[e],u=sumNondiagonalElements(a,e);if(Math.abs(i)e;e++){var a=n.getCol(r,e),i=a[e],u=sumNondiagonalElements(a,e);if(Math.abs(i)<=u)return!1}return!0}},{}],9:[function(r,t,e){var n=r("./basic"),o=e;o.simple=function(r){if(isNaN(r)||!isFinite(r)||r%1||2>r)return!1;if(r%2===0)return 2===r;if(r%3===0)return 3===r;for(var t=5,e=Math.sqrt(r);e>=t;t+=6)if(r%t===0||r%(t+2)===0)return!1;return!0},o.factorization=function(r){r=Math.floor(r);for(var t,e,n=[],o=Math.sqrt,a=r>1&&isFinite(r);a;){if(t=o(r),e=2,r%e)for(e=3;r%e&&(e+=2)t?r:e,n.push(e),a=e!==r,r/=e}return n},o.millerRabin=function(r,t){if(1===arguments.length&&(t=20),2===r)return!0;if(!n.isInt(r)||1>=r||r%2===0)return!1;for(var e=0,o=r-1;;){var a=n.divMod(o,2),i=a[0],u=a[1];if(1===u)break;e+=1,o=i}for(var f=function(t){if(1===n.powerMod(t,o,r))return!1;for(var a=0;e>a;a++)if(n.powerMod(t,Math.pow(2,a)*o,r)===r-1)return!1;return!0},s=0;t>s;s++){var h=2+Math.floor(Math.random()*(r-2-2));if(f(h))return!1}return!0},o.sieve=function(r){if(2>r)return[];for(var t=[2],e=3;r>=e;e++){var n=!1;for(var o in t)n=n||0===e%t[o];n||t.push(e)}return t},o.coprime=function(r,t){return 1===n.gcd(r,t)},o.getPerfectPower=function(r){var t=o.getPrimePower(r);return t&&t[1]>1?t:!1},o.getPrimePower=function(r){if(2>r)return!1;if(o.millerRabin(r))return[r,1];if(r%2===0)return[2,r.toString(2).length-1];var t=o.factorization(r);if(!t)return!1;for(var e=t.length,n=0;e>n;n++)for(var a=0,i=0;r>=a;){if(a=Math.pow(t[n],i),a/r===1)return[t[n],i];i++}return!1}},{"./basic":3}],10:[function(r,t,e){var n=(r("./basic"),e),o=Math.random;n.setGenerator=function(r){if("function"!=typeof r)throw new Error("Must pass a function");o=r},n.sample=function(r,t,e){var n=[];n.length=e;for(var a=0;e>a;a++)n[a]=r+(t-r)*o();return n},n.boxMullerTransform=function(r,t){arguments.length<=1&&(t=1),0===arguments.length&&(r=0);var e,n=0,a=0;do n=2*o()-1,a=2*o()-1,e=n*n+a*a;while(0===e||e>1);var i=Math.sqrt(-2*Math.log(e)/e),u=n*i,f=a*i;return u=r+u*t,f=r+f*t,[u,f]},n.irwinHall=function(r,t){1===arguments.length&&(t=0);for(var e=0,n=0;r>n;n++)e+=o();return e-t},n.bates=function(r,t,e){arguments.length<=2&&(e=0),1===arguments.length&&(t=1);for(var n=0,a=0;r>a;a++)n+=(t-e)*o()+e;return n/r},n.distribution={},n.distribution.normal=function(r,t,e){return arguments.length<=2&&(e=1),1===arguments.length&&(t=0),n.distribution.boxMuller(r,t,e)},n.distribution.logNormal=function(r,t,e){arguments.length<=2&&(e=1),1===arguments.length&&(t=0);var o=function(r){return Math.exp(r)};return n.distribution.boxMuller(r,t,e).map(o)},n.distribution.boxMuller=function(r,t,e,o){arguments.length<=3&&(o=!1),arguments.length<=2&&(e=1),1===arguments.length&&(t=0);for(var a=[],i=0;r>i;i++){var u=n.boxMullerTransform(t,e);a.push(o?u:u[0])}return a},n.distribution.irwinHall=function(r,t,e){arguments.length<=2&&(e=0),1===arguments.length&&(t=r);for(var o=new Array(r),a=0;r>a;a++)o[a]=n.irwinHall(t,e);return o},n.distribution.irwinHallNormal=function(r){return n.distribution.irwinHall(r,12,6)},n.distribution.bates=function(r,t,e){arguments.length<=2&&(e=0),1===arguments.length&&(t=r);for(var o=new Array(r),a=0;r>a;a++)o[a]=n.bates(r,t,e);return o}},{"./basic":3}],11:[function(r,t,e){var n=r("./basic"),o=e;o.mean=function(r){var t=r.length,e=n.sum(r);return e/t},o.median=function(r){return o.quantile(r,1,2)},o.mode=function(r){for(var t={},e=0,n=r.length;n>e;e++)void 0===t[r[e]]?t[r[e]]=0:t[r[e]]++;var o;for(var a in t)t.hasOwnProperty(a)&&(void 0===o||t[a]>t[o])&&(o=a);return Number(o)},o.quantile=function(r,t,e){var n,o,a;return 0===t?Math.min.apply(null,r):t===e?Math.max.apply(null,r):(n=r.slice(0),n.sort(function(r,t){return r-t}),o=n.length,a=o*t/e,a%1===0?.5*n[a-1]+.5*n[a]:n[Math.floor(a)])},o.report=function(r){return{mean:o.mean(r),firstQuartile:o.quantile(r,1,4),median:o.median(r),thirdQuartile:o.quantile(r,3,4),standardDev:o.standardDev(r)}},o.standardDev=function(r){for(var t=r.length,e=o.mean(r),a=[],i=0;iu;u++)o+=r[u]*t[u];return(o-a*i/e)/e}throw new Error("Array mismatch")}},{"./basic":3}]},{},[1])(1)}); \ No newline at end of file diff --git a/test/matrix.test.js b/test/matrix.test.js index bb7354a..4ba3043 100644 --- a/test/matrix.test.js +++ b/test/matrix.test.js @@ -204,7 +204,7 @@ suite('numbers', function() { assert.throws(function() { matrix.determinant(m3); }, - /Not a square matrix/ + /Matrix must be square./ ); done(); });