VTK/Examples/Broken/LUFactorization

From KitwarePublic

Jump to: navigation, search

How do you get L and U? What is p for?

LUFactorization.cxx

#include <vtkMath.h>
 
template<class TReal>
TReal **create_matrix ( long nrow, long ncol ) {
  typedef TReal* TRealPointer;
  TReal **m = new TRealPointer[nrow];
 
  TReal* block = ( TReal* ) calloc ( nrow*ncol, sizeof ( TReal ) );
  m[0] = block;
  for ( int row = 1; row < nrow; ++row )
  {
    m[ row ] = &block[ row * ncol ];
  }
  return m;
}
 
  /* free a TReal matrix allocated with matrix() */
  template<class TReal>
      void free_matrix ( TReal **m )
{
  free ( m[0] );
  delete[] m;
}
 
void OutputMatrix(double** a)
{
  std::cout << "[ " << a[0][0] << " " << a[0][1] << std::endl;
  std::cout << "  " << a[1][0] << " " << a[1][1] << " ]" << std::endl;
}
 
int main(int, char *[] )
{
  //create and populate matrix
  int n = 2;
  double **a = create_matrix<double> (n, n);
  a[0][0] = 4; a[0][1] = 3;
  a[1][0] = 6; a[1][1] = 3;
 
  //[4 3; 6 3] should decompose to [1 0; 1.5 1] * [4 3; 0 -1.5]
 
  std::cout << "a"<< std::endl;
  OutputMatrix(a);
 
  int p[2] = {0, 0}; //what is p for??
 
  //Decompose matrix A into LU form
  vtkMath::LUFactorLinearSystem(a, p, n);
 
  std::cout << "A decomposed:"<< std::endl;
  OutputMatrix(a);
 
  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
PROJECT(LUFactorization)
 
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
 
ADD_EXECUTABLE(LUFactorization LUFactorization.cxx)
TARGET_LINK_LIBRARIES(LUFactorization vtkHybrid)
Personal tools