CPP/Eigen/SimplicialLDLT

From ProgrammingExamples
< CPP
Jump to: navigation, search

SimplicialLDLT.cpp

#include <iostream>
 
#include <Eigen/Sparse>
 
int main(int, char *[])
{
  // Create matrix
  typedef Eigen::SparseMatrix<double> SparseMatrixType;
  SparseMatrixType A(2, 2);
 
  // Create the right-hand-side vector
  Eigen::VectorXd b(2);
 
  // Fill matrix
  A.coeffRef(0, 0) += 1;
  A.coeffRef(1, 1) += 3;
 
  // Fill vector
  b[0] = 1;
  b[1] = 2;
 
  // Solve the (symmetric) system
  Eigen::SimplicialLDLT<SparseMatrixType> sparseSolver(A);
  Eigen::VectorXd x = sparseSolver.solve(b);
  if(sparseSolver.info() != Eigen::Success)
  {
    throw std::runtime_error("Decomposition failed!");
  }
 
  std::cout << "Result: " << x << std::endl;
 
  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
PROJECT(SimplicialLDLT)
 
# Set the CMake variable to the value of the environment variable.
# If the build is absolutely fresh, this will be the case if the CMake variable EIGEN3_INCLUDE_DIR is not set.
if(NOT EIGEN3_INCLUDE_DIR)
  set(EIGEN3_INCLUDE_DIR $ENV{EIGEN3_INCLUDE_DIR})
  message("Set EIGEN3_INCLUDE_DIR to ${EIGEN3_INCLUDE_DIR} from environment variable of the same name.")
else()
  message("EIGEN3_INCLUDE_DIR is ${EIGEN3_INCLUDE_DIR}")
endif()
 
if(NOT EIGEN3_FOUND)
  FIND_PACKAGE(Eigen3 REQUIRED) #requires FindEigen3.cmake to be in the source directory
  include_directories(${EIGEN3_INCLUDE_DIR})
endif()
 
ADD_EXECUTABLE(SimplicialLDLT SimplicialLDLT.cpp )