diff --git a/math.scad b/math.scad index f8dc707..5ec3209 100644 --- a/math.scad +++ b/math.scad @@ -438,8 +438,11 @@ function lcm(a,b=[]) = // sum([1,2,3]); // returns 6. // sum([[1,2,3], [3,4,5], [5,6,7]]); // returns [9, 12, 15] function sum(v, dflt=0) = + is_vector(v) ? [for(i=v) 1]*v : assert(is_consistent(v), "Input to sum is non-numeric or inconsistent") - len(v) == 0 ? dflt : _sum(v,v[0]*0); + is_vector(v[0]) ? [for(i=v) 1]*v : + len(v) == 0 ? dflt : + _sum(v,v[0]*0); function _sum(v,_total,_i=0) = _i>=len(v) ? _total : _sum(v,_total+v[_i], _i+1); @@ -896,7 +899,13 @@ function count_true(l, nmax=undef, i=0, cnt=0) = // data[len(data)-1]. This function uses a symetric derivative approximation // for internal points, f'(t) = (f(t+h)-f(t-h))/2h. For the endpoints (when closed=false) the algorithm // uses a two point method if sufficient points are available: f'(t) = (3*(f(t+h)-f(t)) - (f(t+2*h)-f(t+h)))/2h. +// +// If `h` is a vector then it is assumed to be nonuniform, with h[i] giving the sampling distance +// between data[i+1] and data[i], and the data values will be linearly resampled at each corner +// to produce a uniform spacing for the derivative estimate. At the endpoints a single point method +// is used: f'(t) = (f(t+h)-f(t))/h. function deriv(data, h=1, closed=false) = + is_vector(h) ? _deriv_nonuniform(data, h, closed=closed) : let( L = len(data) ) closed? [ for(i=[0:1:L-1]) @@ -916,6 +925,28 @@ function deriv(data, h=1, closed=false) = ]; +function _dnu_calc(f1,fc,f2,h1,h2) = + let( + f1 = h2