mirror of
https://github.com/BelfrySCAD/BOSL2.git
synced 2025-01-01 09:49:45 +00:00
Added median()
This commit is contained in:
parent
412dd9e260
commit
3ac23e15e6
2 changed files with 35 additions and 16 deletions
49
math.scad
49
math.scad
|
@ -433,8 +433,8 @@ function lcm(a,b=[]) =
|
||||||
// sum([1,2,3]); // returns 6.
|
// sum([1,2,3]); // returns 6.
|
||||||
// sum([[1,2,3], [3,4,5], [5,6,7]]); // returns [9, 12, 15]
|
// sum([[1,2,3], [3,4,5], [5,6,7]]); // returns [9, 12, 15]
|
||||||
function sum(v, dflt=0) =
|
function sum(v, dflt=0) =
|
||||||
assert(is_consistent(v), "Input to sum is non-numeric or inconsistent")
|
assert(is_consistent(v), "Input to sum is non-numeric or inconsistent")
|
||||||
len(v) == 0 ? dflt : _sum(v,v[0]*0);
|
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);
|
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()
|
// 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()
|
||||||
|
@ -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.
|
// 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`.
|
// If A is rank deficient or singular then linear_solve returns `undef`.
|
||||||
function linear_solve(A,b) =
|
function linear_solve(A,b) =
|
||||||
assert(is_matrix(A))
|
assert(is_matrix(A))
|
||||||
assert(is_vector(b))
|
assert(is_vector(b))
|
||||||
let(
|
let(
|
||||||
dim = array_dim(A),
|
dim = array_dim(A),
|
||||||
m=dim[0], n=dim[1]
|
m=dim[0], n=dim[1]
|
||||||
)
|
)
|
||||||
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);
|
||||||
|
|
||||||
|
|
||||||
|
@ -571,7 +590,7 @@ function submatrix(M,ind1,ind2) = [for(i=ind1) [for(j=ind2) M[i][j] ] ];
|
||||||
// Calculates the QR factorization of the input matrix A and returns it as the list [Q,R]. This factorization can be
|
// Calculates the QR factorization of the input matrix A and returns it as the list [Q,R]. This factorization can be
|
||||||
// used to solve linear systems of equations.
|
// used to solve linear systems of equations.
|
||||||
function qr_factor(A) =
|
function qr_factor(A) =
|
||||||
assert(is_matrix(A))
|
assert(is_matrix(A))
|
||||||
let(
|
let(
|
||||||
dim = array_dim(A),
|
dim = array_dim(A),
|
||||||
m = dim[0],
|
m = dim[0],
|
||||||
|
@ -683,11 +702,11 @@ function determinant(M) =
|
||||||
// Returns true if A is a numeric matrix of height m and width n. If m or n
|
// Returns true if A is a numeric matrix of height m and width n. If m or n
|
||||||
// are omitted or set to undef then true is returned for any positive dimension.
|
// are omitted or set to undef then true is returned for any positive dimension.
|
||||||
function is_matrix(A,m,n) =
|
function is_matrix(A,m,n) =
|
||||||
is_list(A) && len(A)>0 &&
|
is_list(A) && len(A)>0 &&
|
||||||
(is_undef(m) || len(A)==m) &&
|
(is_undef(m) || len(A)==m) &&
|
||||||
is_vector(A[0]) &&
|
is_vector(A[0]) &&
|
||||||
(is_undef(n) || len(A[0])==n) &&
|
(is_undef(n) || len(A[0])==n) &&
|
||||||
is_consistent(A);
|
is_consistent(A);
|
||||||
|
|
||||||
|
|
||||||
// Section: Comparisons and Logic
|
// Section: Comparisons and Logic
|
||||||
|
|
|
@ -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