mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-25 15:07:57 +00:00
implemented size validation in method size
This commit is contained in:
parent
4b1eeaf79b
commit
cbfbf91d6b
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user