diff --git a/math.scad b/math.scad index 010c307..2733f06 100644 --- a/math.scad +++ b/math.scad @@ -377,9 +377,6 @@ function log_rands(minval, maxval, factor, N=1, seed=undef) = // Section: GCD/GCF, LCM -// If argument is a list return it. Otherwise return a singleton list containing the argument. -function _force_list(x) = is_list(x) ? x : [x]; - // Function: gcd() // Usage: // gcd(a,b) @@ -415,7 +412,7 @@ function _lcmlist(a) = function lcm(a,b=[]) = !is_list(a) && !is_list(b) ? _lcm(a,b) : let( - arglist = concat(_force_list(a),_force_list(b)) + arglist = concat((is_list(a)?a:[a]), (is_list(b)?b:[b])) ) assert(len(arglist)>0,"invalid call to lcm with empty list(s)") _lcmlist(arglist); @@ -539,34 +536,29 @@ function mean(v) = sum(v)/len(v); // the least squares solution is returned. If A is underdetermined, the minimal norm solution is returned. // If A is rank deficient or singular then linear_solve returns `undef`. function linear_solve(A,b) = - let( - dim = array_dim(A), - m=dim[0], n=dim[1] - ) - assert(len(b)==m,str("Incompatible matrix and vector",dim,len(b))) - let ( - qr = mj ? 0 : qr[1][i][j]]] - ) - [qr[0],Rzero]; + let( + dim = array_dim(A), + m = dim[0], + n = dim[1] + ) + assert(len(dim)==2) + let( + qr =_qr_factor(A, column=0, m = m, n=m, Q=ident(m)), + Rzero = [ + for(i=[0:m-1]) [ + for(j=[0:n-1]) + i>j ? 0 : qr[1][i][j] + ] + ] + ) [qr[0],Rzero]; function _qr_factor(A,Q, column, m, n) = - column >= min(m-1,n) ? [Q,A] : - let( - x = [for(i=[column:1:m-1]) A[i][column]], - alpha = (x[0]<=0 ? 1 : -1) * norm(x), - u = x - concat([alpha],replist(0,m-1)), - v = u / norm(u), - Qc = ident(len(x)) - 2*transpose([v])*[v], - Qf = [for(i=[0:m-1]) [for(j=[0:m-1]) i= min(m-1,n) ? [Q,A] : + let( + x = [for(i=[column:1:m-1]) A[i][column]], + alpha = (x[0]<=0 ? 1 : -1) * norm(x), + u = x - concat([alpha],replist(0,m-1)), + v = u / norm(u), + Qc = ident(len(x)) - 2*transpose([v])*[v], + Qf = [for(i=[0:m-1]) [for(j=[0:m-1]) i=5, "Need five points for 3rd derivative estimate") - closed ? - [ for(i=[0:1:L-1]) (-data[(L+i-2)%L]+2*data[(L+i-1)%L]-2*data[(i+1)%L]+data[(i+2)%L])/2/h3] : - let( - first=(-5*data[0]+18*data[1]-24*data[2]+14*data[3]-3*data[4])/2, - second=(-3*data[0]+10*data[1]-12*data[2]+6*data[3]-data[4])/2, - last=(5*data[L-1]-18*data[L-2]+24*data[L-3]-14*data[L-4]+3*data[L-5])/2, - prelast=(3*data[L-1]-10*data[L-2]+12*data[L-3]-6*data[L-4]+data[L-5])/2 - ) - [ - first/h3, - second/h3, - for(i=[2:1:L-3]) (-data[i-2]+2*data[i-1]-2*data[i+1]+data[i+2])/2/h3, - prelast/h3, - last/h3 - ]; + let( + L = len(data), + h3 = h*h*h + ) + assert(L>=5, "Need five points for 3rd derivative estimate") + closed? [ + for(i=[0:1:L-1]) + (-data[(L+i-2)%L]+2*data[(L+i-1)%L]-2*data[(i+1)%L]+data[(i+2)%L])/2/h3 + ] : + let( + first=(-5*data[0]+18*data[1]-24*data[2]+14*data[3]-3*data[4])/2, + second=(-3*data[0]+10*data[1]-12*data[2]+6*data[3]-data[4])/2, + last=(5*data[L-1]-18*data[L-2]+24*data[L-3]-14*data[L-4]+3*data[L-5])/2, + prelast=(3*data[L-1]-10*data[L-2]+12*data[L-3]-6*data[L-4]+data[L-5])/2 + ) [ + first/h3, + second/h3, + for(i=[2:1:L-3]) (-data[i-2]+2*data[i-1]-2*data[i+1]+data[i+2])/2/h3, + prelast/h3, + last/h3 + ]; + // vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap diff --git a/version.scad b/version.scad index 80fa107..889a910 100644 --- a/version.scad +++ b/version.scad @@ -8,7 +8,7 @@ ////////////////////////////////////////////////////////////////////// -BOSL_VERSION = [2,0,140]; +BOSL_VERSION = [2,0,141]; // Section: BOSL Library Version Functions