MatDoub SVD::nullspace(Doub thresh = -1.){ Int j,jj,nn=0; MatDoub nullsp(n,nullity(thresh)); for (j=0;j<n;j++) { if (w[j] <= tsh) { for (jj=0;jj<n;jj++) nullsp[jj][nn] = v[jj][j]; nn++; } } return nullsp; }
DoubleVector* hansen_gaussian_elimination_v2(DoubleMatrix A, DoubleVector b) { unsigned int rows = mtl::mat::num_rows(A); unsigned int cols = mtl::mat::num_cols(A); if(rows == cols) { std::cout << "det(A) = " << det(A) << std::endl << std::endl; if(boost::numeric::zero_in(det(A)) == true) throw std::string("Cannot approximate bounding box as solution set is unbounded"); } if(rows == cols) { DoubleMatrix mid; mid = m(A); if(boost::numeric::zero_in(det(mid)) == false) { DoubleMatrix M(rows, cols); DoubleVector r(mtl::num_rows(b), (DoubleInterval)0); DoubleMatrix B(rows, cols); B = mtl::mat::inv(mid); M = B * A; r = B * b; std::cout << "M: " << std::endl; std::cout << M << std::endl; std::cout << "r: " << std::endl; std::cout << r << std::endl << std::endl; A = M; b = r; } } DoubleMatrix New_A(rows, cols); DoubleVector New_b(mtl::num_rows(b), (DoubleInterval)0); New_A = A; New_b = b; for(unsigned int k = 1; k <= rows; k++) { for(unsigned int i = 1; i <= rows; i++) { for(unsigned int j = 1; j <= cols; j++) { if(i <= k && j <= cols) {} else if(i > k && j > k && boost::numeric::interval_lib::posne(A(k-1,k-1), (DoubleInterval)0) == true) New_A(i-1,j-1) = A(i-1,j-1) - ((A(i-1,k-1) * A(k-1,j-1))/A(k-1,k-1)); else New_A(i-1,j-1) = 0; if(i <= k) New_b[i-1] = b[i-1]; else if(i > k && boost::numeric::interval_lib::posne(A(k-1,k-1), (DoubleInterval)0) == true) New_b[i-1] = b[i-1] - ((A(i-1,k-1)/A(k-1,k-1))*b[k-1]); } } A = New_A; b = New_b; } std::cout << "A_reduced: " << std::endl; std::cout << A << std::endl; unsigned int rk = rank(A); unsigned int nul = nullity(A); std::cout << "rk(A) = " << rk << " nul(A) = " << nul << std::endl << std::endl; std::cout << "b_reduced: " << std::endl; std::cout << b << std::endl << std::endl; if(cols == rk && rows > rk) { DoubleMatrix A_smaller(rk,rk); DoubleVector b_smaller(rk, (DoubleInterval)0); for(unsigned int i = 0; i < rk; i++) { b_smaller[i] = b[i]; for(unsigned int j = 0; j < rk; j++) A_smaller(i,j) = A(i,j); } std::cout << "New A = " << std::endl; std::cout << A_smaller << std::endl; std::cout << "New b = " << std::endl; std::cout << b_smaller << std::endl << std::endl; return hansen_gaussian_elimination_v2(A_smaller, b_smaller); } else if(cols > rk) { std::cout << "The system of equations has no exact solutions" << std::endl << std::endl; return NULL; } DoubleVector *x = new DoubleVector(cols, (DoubleInterval)0); *x = mtl::mat::upper_trisolve(A,b); return x; }