Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Mar 4th, 2011  |  syntax: None  |  size: 1.81 KB  |  views: 136  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <iostream>
  2.  
  3. #include <vnl/vnl_vector.h>
  4. #include <vnl/vnl_sparse_matrix.h>
  5. #include <vnl/algo/vnl_sparse_lu.h>
  6.  
  7. int main()
  8. {
  9.   /*
  10.   octave:1> A=[3 2 -1; 2 -2 4; -1 .5 -1];
  11.   octave:2> b=[ 1; -2; 0];
  12.   octave:3> inv(A)*b
  13.   ans =
  14.  
  15.     1.00000
  16.     -2.00000
  17.     -2.00000
  18.   */
  19.  
  20.   vnl_sparse_matrix<double> A(3,3);
  21.   std::cout << "Rows: " << A.rows() << std::endl;
  22.   std::cout << "Cols: " << A.columns() << std::endl;
  23.   A(0,0) = 3;
  24.   A(0,1) = 2;
  25.   A(0,2) = -1;
  26.   A(1,0) = 2;
  27.   A(1,1) = -2;
  28.   A(1,2) = 4;
  29.   A(2,0) = -1;
  30.   A(2,1) = .5;
  31.   A(2,2) = -1;
  32.  
  33.   vnl_vector< double > b(3);
  34.   b(0) = 1;
  35.   b(1) = -2;
  36.   b(2) = 0;
  37.  
  38.   std::cout << "b = " << b << std::endl;
  39.  
  40.   //Ax - b = 0, solution should be x = (1 -2 -2)
  41.   vnl_vector< double > x(3);
  42.  
  43.   vnl_sparse_lu linear_solver(A, vnl_sparse_lu::estimate_condition);
  44.   linear_solver.solve(b,&x);
  45.   double det = linear_solver.determinant();
  46.   double rcond = linear_solver.rcond();
  47.   double upbnd = linear_solver.max_error_bound();
  48.  
  49.   std::cout << "x = " << x << std::endl;
  50.  
  51.   return 0;
  52. }
  53.  
  54.  
  55. --------------------
  56.  
  57. Project(SparseLinearSystem)
  58.  
  59. FIND_PACKAGE(VXL REQUIRED)
  60. INCLUDE(${VXL_CMAKE_DIR}/UseVXL.cmake)
  61.  
  62. ADD_EXECUTABLE(SparseLinearSystem SparseLinearSystem.cpp)
  63. TARGET_LINK_LIBRARIES(SparseLinearSystem vnl rrel)
  64.  
  65. -------------
  66.  
  67. In file included from /media/portable/Examples/c++/src/VXL/VNL/SparseLinearSystem/SparseLinearSystem.cpp:5:
  68. /home/doriad/src/vxl/core/vnl/algo/vnl_sparse_lu.h:20:29: error: sparse/spMatrix.h: No such file or directory
  69. In file included from /media/portable/Examples/c++/src/VXL/VNL/SparseLinearSystem/SparseLinearSystem.cpp:5:
  70. /home/doriad/src/vxl/core/vnl/algo/vnl_sparse_lu.h:111: error: ‘spMatrix’ does not name a type
  71. make[2]: *** [CMakeFiles/SparseLinearSystem.dir/SparseLinearSystem.o] Error 1