VectorXd PatchFit::lls(MatrixXd J, VectorXd b) { // check input dimensions if (J.rows() != b.rows()) cerr << "Wrong dimensions" << endl; VectorXd a (J.cols()); //output parameters JacobiSVD<MatrixXd> svd(J, ComputeThinU | ComputeThinV); VectorXd sv = svd.singularValues(); MatrixXd V = svd.matrixV(); VectorXd pv = sv.array().inverse(); VectorXd pvSq = pv.array()*pv.array(); if (b.any()) a = V * pvSq.asDiagonal() * V.adjoint() * J.adjoint() * b; else a = V.rightCols(1); return a; }