public static void LeastSquares(Matrix x, Matrix A, Matrix b) { // use svd // for overdetermined systems A*x = b // x = V * diag(1/wj) * U T * b // NRC p. 66 int m = A.m; int n = A.n; Matrix U = new Matrix(m, n), V = new Matrix(n, n), w = new Matrix(n, 1), W = new Matrix(n, n); A.SVD(U, w, V); w.Reciprocal(); W.Diag(w); Matrix M = new Matrix(n, n); M.Mult(V, W); Matrix N = new Matrix(n, m); N.MultAAT(M, U); x.Mult(N, b); }