mirror of
https://github.com/BelfrySCAD/BOSL2.git
synced 2025-01-04 03:09:45 +00:00
Added median()
This commit is contained in:
parent
412dd9e260
commit
3ac23e15e6
2 changed files with 35 additions and 16 deletions
29
math.scad
29
math.scad
|
@ -519,8 +519,8 @@ function product(v, i=0, tot=undef) = i>=len(v)? tot : product(v, i+1, ((tot==un
|
||||||
|
|
||||||
// Function: mean()
|
// Function: mean()
|
||||||
// Description:
|
// Description:
|
||||||
// Returns the mean of all entries in the given array.
|
// Returns the arithmatic mean/average of all entries in the given array.
|
||||||
// If passed an array of vectors, returns a vector of mean of each part.
|
// If passed a list of vectors, returns a vector of the mean of each part.
|
||||||
// Arguments:
|
// Arguments:
|
||||||
// v = The list of values to get the mean of.
|
// v = The list of values to get the mean of.
|
||||||
// Example:
|
// 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 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
|
// Section: Matrix math
|
||||||
|
|
||||||
// Function: linear_solve()
|
// Function: linear_solve()
|
||||||
|
@ -546,15 +565,15 @@ function linear_solve(A,b) =
|
||||||
)
|
)
|
||||||
assert(len(b)==m,str("Incompatible matrix and vector",dim,len(b)))
|
assert(len(b)==m,str("Incompatible matrix and vector",dim,len(b)))
|
||||||
let (
|
let (
|
||||||
qr = m<n ? qr_factor(transpose(A)) : qr_factor(A),
|
qr = m<n? qr_factor(transpose(A)) : qr_factor(A),
|
||||||
maxdim = max(n,m),
|
maxdim = max(n,m),
|
||||||
mindim = min(n,m),
|
mindim = min(n,m),
|
||||||
Q = submatrix(qr[0],[0:maxdim-1], [0:mindim-1]),
|
Q = submatrix(qr[0],[0:maxdim-1], [0:mindim-1]),
|
||||||
R = submatrix(qr[1],[0:mindim-1], [0:mindim-1]),
|
R = submatrix(qr[1],[0:mindim-1], [0:mindim-1]),
|
||||||
zeros = [for(i=[0:mindim-1]) if (approx(R[i][i],0)) i]
|
zeros = [for(i=[0:mindim-1]) if (approx(R[i][i],0)) i]
|
||||||
)
|
)
|
||||||
zeros != [] ? undef :
|
zeros != []? undef :
|
||||||
m<n ? Q*back_substitute(R,b,transpose=true) :
|
m<n? Q*back_substitute(R,b,transpose=true) :
|
||||||
back_substitute(R, transpose(Q)*b);
|
back_substitute(R, transpose(Q)*b);
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
//////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
BOSL_VERSION = [2,0,197];
|
BOSL_VERSION = [2,0,198];
|
||||||
|
|
||||||
|
|
||||||
// Section: BOSL Library Version Functions
|
// Section: BOSL Library Version Functions
|
||||||
|
|
Loading…
Reference in a new issue