@@ -5,8 +5,10 @@ // function vector_add(a, b, result){ if(!result) result = []; + if(!a) throw "first argument to vector_add missing"; + if(!b) throw "second argument to vector_add missing"; if(a.length > b.lemgth){ var tmp = a; a = b; b = tmp; } @@ -20,8 +22,9 @@ return result; } function vector_cpy(result, a){ + if(!a) throw "first argument to vector_cpy missing"; for(var i = 0; i < a.length; ++i){ result[i] = a[i]; } } @@ -28,8 +31,10 @@ function vector_minus(a, b, result){ if(!result) result = []; + if(!a) throw "first argument to vector_minus missing"; + if(!b) throw "second argument to vector_minus missing"; var lim = Math.max(a.length, b.length); for(var i = 0; i < lim; ++i){ @@ -52,8 +57,11 @@ function vector_dot(a, b){ var lim = Math.min(a.length, b.length); + if(!a) throw "first argument to vector_dot missing"; + if(!b) throw "second argument to vector_dot missing"; + var result = 0; for(var i = 0; i < lim; ++i){ result += a[i] * b[i]; } @@ -61,14 +69,17 @@ return result; } function vector_norm(a){ + if(!a) throw "argument to vector_norm missing"; return Math.sqrt(vector_dot(a,a)); } function vector_scale(a, s, result){ + if(!a) throw "first argument to vector_scale missing"; + if(s === undefined || s === null) throw "scalar argument to vector_scale missing"; if(!result) result = []; for(var i = 0; i < a.length; ++i){ @@ -79,8 +90,10 @@ // returns true iff the zero tail padded vectors match function vector_cmp(a, b){ + if(!a) throw "first argument to vector_cmp missing"; + if(!b) throw "second argument to vector_cmp missing"; if(a.length > b.length){ var tmp = a; a = b; b = tmp; } @@ -96,9 +109,11 @@ return true; } -function midpoint(a, b, result){ +function vector_midpoint(a, b, result){ + if(!a) throw "first argument to vector_midpoint missing"; + if(!b) throw "second argument to vector_midpoint missing"; if(b.length > a.length){ var tmp = a; a = b; b = tmp; } @@ -118,8 +133,9 @@ //this function may not care, but the expected matrix format in this project is a list of column vectors. function matrix_transpose(matrix){ + if(!matrix) throw "argument to matrix_transpose missing"; var result = []; var dim0 = matrix.length; if(!dim0) @@ -142,8 +158,10 @@ function matrix_mult(a, b){ + if(!a) throw "first argument to matrix_mult missing"; + if(!b) throw "second argument to matrix_mult missing"; var result = []; var a = matrix_transpose(a); var dim0 = a.length; @@ -163,8 +181,9 @@ // finds the inverse of a 3 by 3 matrix, returns false if matrix has determinant zero. // function matrix33inv(m){ + if(!m) throw "argument to matrix33inv missing"; // the matrix format is a list of column vectors. var a = m[0][0]; var b = m[1][0]; @@ -206,10 +225,14 @@ [di*G, di*H, di*K]]; return result; } - + +// does not check if function is differentiable. + function compute_gradient(func, point, error){ + if(!func) throw "function argument to compute_gradient missing"; + if(!point) throw "point argument to compute_gradient missing"; if(!error) error = 0.00001; // find gradient @@ -235,9 +258,11 @@ // http://inside.mines.edu/~gmurray/ArbitraryAxisRotation/ArbitraryAxisRotation.html // creates a rotation matrix about the given vector. // function vector_rotation(vector, s){ - + if(!vector) throw "vector argument to vector_rotation missing"; + if(!s) throw "angle argument to vector_rotation missing"; + var len = Math.sqrt(vector[0]*vector[0] + vector[1]*vector[1] + vector[2]*vector[2]);