From cbfbf91d6bbe6c8e4e30f96f88d3116e31e08803 Mon Sep 17 00:00:00 2001 From: josdejong Date: Thu, 14 Mar 2013 20:23:55 +0100 Subject: [PATCH] implemented size validation in method size --- src/function/matrix/eye.js | 5 ++- src/function/matrix/size.js | 77 ++++++++++++++++++++++++++++++++++--- 2 files changed, 74 insertions(+), 8 deletions(-) diff --git a/src/function/matrix/eye.js b/src/function/matrix/eye.js index d7a424316..49c5e72f7 100644 --- a/src/function/matrix/eye.js +++ b/src/function/matrix/eye.js @@ -36,7 +36,6 @@ function eye (m, n) { } } - // TODO: add support for n dimensional matrices // TODO: use zeros(m, n) instead, then fill the diagonal with ones var res = []; for (var r = 0; r < rows; r++) { @@ -47,6 +46,7 @@ function eye (m, n) { res[r] = row; } + // fill in ones on the diagonal var min = Math.min(rows, cols); for (var d = 0; d < min; d++) { res[d][d] = 1; @@ -55,7 +55,8 @@ function eye (m, n) { return res; } -math.eye = eye; +// TODO: export method eye to math +// math.eye = eye; /** * Function documentation diff --git a/src/function/matrix/size.js b/src/function/matrix/size.js index 51c044677..a0af85d52 100644 --- a/src/function/matrix/size.js +++ b/src/function/matrix/size.js @@ -25,6 +25,8 @@ function size (x) { } if (x instanceof Array) { + var s = getSize(x); + validate(x, s); return [getSize(x)]; } // TODO: implement matrix support @@ -41,11 +43,8 @@ function getSize (x) { if (x instanceof Array) { var sizeX = x.length; if (sizeX) { - var sizeI = getSize(x[0]); - - // TODO: validate whether the other elements have the same size - - return [sizeX].concat(sizeI); + var size0 = getSize(x[0]); + return [sizeX].concat(size0); } else { return [sizeX]; @@ -56,7 +55,73 @@ function getSize (x) { } } -math.size = size; +/** + * Verify whether each element in an n dimensional array has the correct size + * @param {Array | Object} array Array to be validated + * @param {Number[]} size Array with dimensions + * @param {Number} [dim] Current dimension + * @throw Error + */ +function validate(array, size, dim) { + var i, + len = array.length; + if (!dim) { + dim = 0; + } + + if (len != size[dim]) { + throw new Error('Dimension mismatch (' + len + ' != ' + size[dim] + ')'); + } + + if (dim < size.length - 1) { + // recursively validate each child array + var dimNext = dim + 1; + for (i = 0; i < len; i++) { + var child = array[i]; + if (!(child instanceof Array)) { + throw new Error('Dimension mismatch ' + + '(' + (size.length - 1) + ' < ' + size.length + ')'); + } + validate(array[i], size, dimNext); + } + } + else { + // last dimension. none of the childs may be an array + for (i = 0; i < len; i++) { + if (array[i] instanceof Array) { + throw new Error('Dimension mismatch ' + + '(' + (size.length + 1) + ' > ' + size.length + ')'); + } + } + } + + return true; +} + +/** + * Compare two arrays + * @param a + * @param b + * @return {Boolean} equal True if both arrays are equal, else false + */ +function compare(a, b) { + var len = a.length; + if (len != b.length) { + return false; + } + + for (var i = 0; i < len; i++) { + if (a[i] != b[i]) { + return false; + } + } + + return true; +} + + +// TODO: export method size to math +// math.size = size; /** * Function documentation