diff --git a/math.scad b/math.scad index d270831..010c307 100644 --- a/math.scad +++ b/math.scad @@ -532,6 +532,43 @@ function mean(v) = sum(v)/len(v); // Section: Matrix math +// Function: linear_solve() +// Usage: linear_solve(A,b) +// Description: +// Solves the linear system Ax=b. If A is square and non-singular the unique solution is returned. If A is overdetermined +// 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 = m