double eigenPartialPivLUSpeed(const Eigen::MatrixXd & inputMatrix){ int matrixSize = inputMatrix.rows(); Eigen::MatrixXd inputF = Eigen::VectorXd::LinSpaced(Eigen::Sequential,matrixSize,-2,2); double startTime = clock(); Eigen::PartialPivLU<Eigen::MatrixXd> LU (inputMatrix); Eigen::VectorXd solverSoln = LU.solve(inputF); double endTime = clock(); return (endTime - startTime)/CLOCKS_PER_SEC; }
void testBoundaryLRSolver(const std::string inputMatrixFileName,const std::string inputGraphFileName,const std::string outputFileName,const double iterInitTol,const int sizeThreshold,const int depth,user_IndexTree &usrTree){ Eigen::MatrixXd inputMatrix = readBinaryIntoMatrixXd(inputMatrixFileName); Eigen::SparseMatrix<double> inputGraph = readMtxIntoSparseMatrix(inputGraphFileName); HODLR_Matrix testHODLR(inputMatrix,inputGraph,sizeThreshold,usrTree); int matrixSize = inputMatrix.rows(); Eigen::VectorXd exactSoln = Eigen::VectorXd::LinSpaced(Eigen::Sequential,matrixSize,-2,2); Eigen::VectorXd inputF = inputMatrix * exactSoln; testHODLR.set_BoundaryDepth(depth); testHODLR.printResultInfo = true; Eigen::VectorXd solverSoln = testHODLR.iterative_Solve(inputF,100,1e-10,iterInitTol,"PS_Boundary","recLU"); Eigen::VectorXd difference = solverSoln - exactSoln; //double relError = difference.norm()/exactSoln.norm(); //std::cout<<relError<<std::endl; testHODLR.saveSolverInfo(outputFileName); double startTime = clock(); Eigen::PartialPivLU<Eigen::MatrixXd> LU (inputMatrix); solverSoln = LU.solve(inputF); double endTime = clock(); double LU_SolveTime = (endTime - startTime)/CLOCKS_PER_SEC; std::cout<<"LU Solve Time = "<<LU_SolveTime<<" seconds"<<std::endl; std::cout<<"Matrix Size = "<<inputMatrix.rows()<<std::endl; }
void ctms_decompositions() { const int maxSize = 16; const int size = 12; typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, 0, maxSize, maxSize> Matrix; typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1, 0, maxSize, 1> Vector; typedef Eigen::Matrix<std::complex<Scalar>, Eigen::Dynamic, Eigen::Dynamic, 0, maxSize, maxSize> ComplexMatrix; const Matrix A(Matrix::Random(size, size)), B(Matrix::Random(size, size)); Matrix X(size,size); const ComplexMatrix complexA(ComplexMatrix::Random(size, size)); const Matrix saA = A.adjoint() * A; const Vector b(Vector::Random(size)); Vector x(size); // Cholesky module Eigen::LLT<Matrix> LLT; LLT.compute(A); X = LLT.solve(B); x = LLT.solve(b); Eigen::LDLT<Matrix> LDLT; LDLT.compute(A); X = LDLT.solve(B); x = LDLT.solve(b); // Eigenvalues module Eigen::HessenbergDecomposition<ComplexMatrix> hessDecomp; hessDecomp.compute(complexA); Eigen::ComplexSchur<ComplexMatrix> cSchur(size); cSchur.compute(complexA); Eigen::ComplexEigenSolver<ComplexMatrix> cEigSolver; cEigSolver.compute(complexA); Eigen::EigenSolver<Matrix> eigSolver; eigSolver.compute(A); Eigen::SelfAdjointEigenSolver<Matrix> saEigSolver(size); saEigSolver.compute(saA); Eigen::Tridiagonalization<Matrix> tridiag; tridiag.compute(saA); // LU module Eigen::PartialPivLU<Matrix> ppLU; ppLU.compute(A); X = ppLU.solve(B); x = ppLU.solve(b); Eigen::FullPivLU<Matrix> fpLU; fpLU.compute(A); X = fpLU.solve(B); x = fpLU.solve(b); // QR module Eigen::HouseholderQR<Matrix> hQR; hQR.compute(A); X = hQR.solve(B); x = hQR.solve(b); Eigen::ColPivHouseholderQR<Matrix> cpQR; cpQR.compute(A); X = cpQR.solve(B); x = cpQR.solve(b); Eigen::FullPivHouseholderQR<Matrix> fpQR; fpQR.compute(A); // FIXME X = fpQR.solve(B); x = fpQR.solve(b); // SVD module Eigen::JacobiSVD<Matrix> jSVD; jSVD.compute(A, ComputeFullU | ComputeFullV); }
///////////////////////////////// MAIN ///////////////////////////////////////// int main( int argc, char* argv[] ) { // Wrap everything in a try block. Do this every time, // because exceptions will be thrown for problems. try { // Define the command line object. TCLAP::CmdLine cmd("Solve linear least square fit.", ' ', "LeSLiE 0.1"); // add the input file as a required unlabeled value to the command line TCLAP::UnlabeledValueArg<int> lsqOrder( "order", "integer indicating maximal degree of polynomial basis", true, 1, "int", cmd ); TCLAP::UnlabeledValueArg<std::string> inpFile( "filename", ".csv file containing x and y values", true, "default", "string", cmd ); // Parse the args. cmd.parse( argc, argv ); const int degree = lsqOrder.getValue(); const std::string filename = inpFile.getValue(); // typedefs typedef double scalar_type; typedef Eigen::Matrix<scalar_type,Eigen::Dynamic,Eigen::Dynamic> matrix_type; typedef Eigen::Matrix<scalar_type,Eigen::Dynamic,1> vector_type; //////////////////////////// READ FROM FILE ////////////////////////////////// filereader<scalar_type,vector_type> myreader(filename); // set up data pair // std::cout << myreader.getFileName() << std::endl; vector_type xs, ys; myreader(xs,ys); ////////////////////////////////////////////////////////////////////////////// ///////////////////// SETTING UP PURE FUNCTION SPACE ///////////////////////// // initialize dim-0 space FunctionSpace1d<scalar_type,vector_type,matrix_type, Function1d<scalar_type,vector_type,Polynomial1d> > mySpace(0); // fill in basis for (unsigned int k=0; k<=degree; ++k) { mySpace.addFunction(Function1d<scalar_type,vector_type,Polynomial1d>(k)); } /////////// ALTERNATIVE OF SETTING UP MIXED FUNCTION SPACE /////////////////// vector_type myShape(1); myShape(0) = 3; std::cout << POLYNOMIAL<scalar_type,vector_type>(5,myShape) << std::endl; scalar_type (*myfunpt)(scalar_type,vector_type) = POLYNOMIAL<scalar_type,vector_type>; std::cout << myfunpt(6,myShape) << std::endl; Function1d_PT<scalar_type,vector_type> myPoly(myfunpt,myShape); std::cout << myPoly.evaluate(29) << std::endl; // set up space for testing purposes FunctionSpace1d<scalar_type,vector_type,matrix_type, Function1d_PT<scalar_type,vector_type> > testSpace(0); // fill in basis // ADD ARRAY OF FUNCTIONS for (int p=0; p<=degree; ++p) { myShape(0) = p; scalar_type (*myfunpt)(scalar_type,vector_type) = POLYNOMIAL<scalar_type,vector_type>; testSpace.addFunction( Function1d_PT<scalar_type,vector_type>(myfunpt,myShape) ); } // ADD A SINGLE FUNCTION - SPACE ENRICHED WITH LOG (COMPARED TO mySpace) scalar_type (*lastpt)(scalar_type,vector_type) = LOG<scalar_type,vector_type>; myShape(0) = 1; std::cout << myShape << std::endl; testSpace.addFunction(Function1d_PT<scalar_type,vector_type>(lastpt,myShape)); ///////////// END OF ALTERNATIVE OF SETTING UP MIXED FUNCTION SPACE ////////// // Define matrix /* // CASE A - mySpace matrix_type Mat(xs.size(),mySpace.getDim()); Mat.setZero(); vector_type rhs(mySpace.getDim()), sol(mySpace.getDim()); // Compute matrix mySpace.evaluate(xs,Mat); */ // CASE B - testSpace matrix_type Mat(xs.size(),testSpace.getDim()); Mat.setZero(); vector_type rhs(testSpace.getDim()), sol(testSpace.getDim()); // Compute matrix testSpace.evaluate(xs,Mat); // compute rhs rhs.noalias() = Mat.transpose()*ys; // TODO: ADD ROUTINE THAT CHECKS SOLVABILITY OF THE SYSTEM // This is an issue especially when MIXED function spaces are allowed. Eigen::PartialPivLU<matrix_type> plu; plu.compute(Mat.transpose()*Mat); sol = plu.solve(rhs); // Display solution - SHORT std::cout << std::endl; std::cout << "Solution:" << std::endl; std::cout << sol << std::endl << std::endl;; // Display solution - LONG std::cout << "Solution:" << std::endl; for (unsigned int i=0; i<sol.size(); ++i) { std::cout << "beta_" << i << "\t=\t" << sol(i) << std::endl; } // Display solution - CSV OPTIMIZED std::cout << "Solution:" << std::endl; for (unsigned int i=0; i<sol.size(); ++i) { std::cout << "beta_" << i << "," << sol(i) << std::endl; } //filewriter<scalar_type,vector_type> mywriter(filename+".OUT"); //mywriter(sol); return 0; } catch (TCLAP::ArgException &e) // catch any exceptions { std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl; } }