From 3ac23e15e646182a814b9fbac5b313f2d1bde591 Mon Sep 17 00:00:00 2001 From: Revar Desmera Date: Tue, 17 Mar 2020 01:13:47 -0700 Subject: [PATCH] Added median() --- math.scad | 49 ++++++++++++++++++++++++++++++++++--------------- version.scad | 2 +- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/math.scad b/math.scad index e6cee05..e1d9d18 100644 --- a/math.scad +++ b/math.scad @@ -433,8 +433,8 @@ 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) = - assert(is_consistent(v), "Input to sum is non-numeric or inconsistent") - len(v) == 0 ? dflt : _sum(v,v[0]*0); + assert(is_consistent(v), "Input to sum is non-numeric or inconsistent") + 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); @@ -519,8 +519,8 @@ function product(v, i=0, tot=undef) = i>=len(v)? tot : product(v, i+1, ((tot==un // Function: mean() // Description: -// Returns the mean of all entries in the given array. -// If passed an array of vectors, returns a vector of mean of each part. +// Returns the arithmatic mean/average of all entries in the given array. +// If passed a list of vectors, returns a vector of the mean of each part. // Arguments: // v = The list of values to get the mean of. // Example: @@ -529,6 +529,25 @@ function product(v, i=0, tot=undef) = i>=len(v)? tot : product(v, i+1, ((tot==un function mean(v) = sum(v)/len(v); +// Function: median() +// Usage: +// x = median(v); +// Description: +// Given a list of numbers or vectors, finds the median value or midpoint. +// If passed a list of vectors, returns the vector of the median of each part. +function median(v) = + assert(is_list(v)) + assert(len(v)>0) + is_vector(v[0])? ( + assert(is_consistent(v)) + [ + for (i=idx(v[0])) + let(vals = subindex(v,i)) + (min(vals)+max(vals))/2 + ] + ) : (min(v)+max(v))/2; + + // Section: Matrix math // Function: linear_solve() @@ -538,23 +557,23 @@ 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) = - assert(is_matrix(A)) - assert(is_vector(b)) + assert(is_matrix(A)) + assert(is_vector(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 = m0 && - (is_undef(m) || len(A)==m) && - is_vector(A[0]) && - (is_undef(n) || len(A[0])==n) && - is_consistent(A); + is_list(A) && len(A)>0 && + (is_undef(m) || len(A)==m) && + is_vector(A[0]) && + (is_undef(n) || len(A[0])==n) && + is_consistent(A); // Section: Comparisons and Logic diff --git a/version.scad b/version.scad index edb2f80..960c71b 100644 --- a/version.scad +++ b/version.scad @@ -8,7 +8,7 @@ ////////////////////////////////////////////////////////////////////// -BOSL_VERSION = [2,0,197]; +BOSL_VERSION = [2,0,198]; // Section: BOSL Library Version Functions