// DGESV uses the LU factorization to compute solution // to a real system of linear equations, A * X = B, // where A is square (N,N) and X, B are (N,NRHS). // // If the system is over or under-determined, // (i.e. A is not square), then pass the problem // to the Least-squares solver (DGELSS) below. //--------------------------------------------------------- void umSOLVE(const DMat& mat, const DMat& B, DMat& X) //--------------------------------------------------------- { if (!mat.ok()) {umWARNING("umSOLVE()", "system is empty"); return;} if (!mat.is_square()) { umSOLVE_LS(mat, B, X); // return a least-squares solution. return; } DMat A(mat); // work with copy of input X = B; // initialize result with RHS int rows=A.num_rows(), LDA=A.num_rows(), cols=A.num_cols(); int LDB=B.num_rows(), NRHS=B.num_cols(), info=0; if (rows<1) {umWARNING("umSOLVE()", "system is empty"); return;} IVec ipiv(rows); // Solve the system. GESV(rows, NRHS, A.data(), LDA, ipiv.data(), X.data(), LDB, info); if (info < 0) { X = 0.0; umERROR("umSOLVE(A,B, X)", "Error in input argument (%d)\nNo solution computed.", -info); } else if (info > 0) { X = 0.0; umERROR("umSOLVE(A,B, X)", "\nINFO = %d. U(%d,%d) was exactly zero." "\nThe factorization has been completed, but the factor U is " "\nexactly singular, so the solution could not be computed.", info, info, info); } }
void SpinAdapted::xsolve_AxeqB(const Matrix& a, const ColumnVector& b, ColumnVector& x) { FORTINT ar = a.Nrows(); int bc = 1; int info=0; FORTINT* ipiv = new FORTINT[ar]; double* bwork = new double[ar]; for(int i = 0;i<ar;++i) bwork[i] = b.element(i); double* workmat = new double[ar*ar]; for(int i = 0;i<ar;++i) for(int j = 0;j<ar;++j) workmat[i*ar+j] = a.element(j,i); GESV(ar, bc, workmat, ar, ipiv, bwork, ar, info); delete[] ipiv; delete[] workmat; for(int i = 0;i<ar;++i) x.element(i) = bwork[i]; delete[] bwork; if(info != 0) { pout << "Xsolve failed with info error " << info << endl; abort(); } }
// DGESV computes the solution to a real system of linear // equations, A*x = b, where A is an N-by-N matrix, and // x and b are N-by-1 vectors. The LU decomposition // with partial pivoting and row interchanges is used to // factor A as A = P*L*U, where P is a permutation matrix, // L is unit lower triangular, and U is upper triangular. // The system is solved using this factored form of A. //--------------------------------------------------------- void umSOLVE(const DMat& mat, const DVec& b, DVec& x) //--------------------------------------------------------- { // Work with copies of input arrays. DMat A(mat); x = b; int NRHS = 1; int LDA = A.num_rows(); int rows = A.num_rows(); int cols = A.num_cols(); int info = 0; if (rows != cols) { umERROR("umSOLVE(DMat, DVec)", "Matrix A (%d,%d) is not square.\n" "For a Least-Squares solution, see umSOLVE_LS(A,B).", rows, cols); } if (rows < 1) { umLOG(1, "Empty system passed into umSOLVE().\n"); return; } IVec ipiv(rows, 0); GESV (rows, NRHS, A.data(), LDA, ipiv.data(), x.data(), rows, info); if (info < 0) { x = 0.0; umERROR("umSOLVE(DMat&, DVec&)", "Error in input argument (%d)\nNo solution computed.", -info); } else if (info > 0) { x = 0.0; umERROR("umSOLVE(DMat&, DVec&)", "\nINFO = %d. U(%d,%d) was exactly zero." "\nThe factorization has been completed, but the factor U is " "\nexactly singular, so the solution could not be computed.", info, info, info); } }