@@ -4,10 +4,12 @@ // // 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; } @@ -19,18 +21,21 @@ 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]; } } 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){ var aValue, bValue; @@ -51,25 +56,31 @@ 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]; } 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){ result[i] = s * a[i]; } @@ -78,10 +89,12 @@ // 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; } @@ -95,11 +108,13 @@ 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; } @@ -117,10 +132,11 @@ //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) return result; @@ -141,10 +157,12 @@ 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; var dim1 = b.length; @@ -162,10 +180,11 @@ // 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]; var c = m[2][0]; @@ -205,12 +224,16 @@ [di*D, di*E, di*F], [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 var otherpoint = point.slice(0); @@ -234,11 +257,13 @@ // equation at: // 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]); var u = vector[0]/len;