# EigenSymmetric

vtk-examples/Cxx/Math/EigenSymmetric

### Description¶

At the moment, this is a pretty messy process, so here are some helper functions that will hopefully eventually be wrapped into a class/interface.

Question

### Code¶

EigenSymmetric.cxx

#include <vtkMath.h>

namespace {
/* allocate memory for an nrow x ncol matrix */
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 = 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);
delete[] m;
}

} // namespace

int main(int, char*[])
{
// Create this matrix
// 1 2 3
// 2 5 9
// 3 9 8

double** a = create_matrix<double>(3, 3);
a = 1;
a = 2;
a = 3;
a = 2;
a = 5;
a = 9;
a = 3;
a = 9;
a = 8;

// create a matrix for the eigenvectors to be stored in
double** w = create_matrix<double>(3, 3);

// create a vector for the eigenvalues to be stored in
double v;

// compute the eigenvalues/vectors
vtkMath::Jacobi(a, v, w);

// output eigen values
std::cout << "Eigen values (decreasing order): " << std::endl;
for (int i = 0; i < 3; i++)
{
std::cout << v[i] << " ";
}

// output eigenvectors
std::cout << "Eigen vectors (same order as eigen values): " << std::endl;
for (int i = 0; i < 3; i++)
{
// the columns of w are the eigenvectors
std::cout << "Eigen vector " << i << " : " << w[i] << " " << w[i]
<< " " << w[i] << std::endl;
}

// clean up
free_matrix(w);
free_matrix(a);

return EXIT_SUCCESS;
}


### CMakeLists.txt¶

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(EigenSymmetric)

find_package(VTK COMPONENTS
CommonCore
)

if (NOT VTK_FOUND)
message(FATAL_ERROR "EigenSymmetric: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
target_link_libraries(EigenSymmetric PRIVATE ${VTK_LIBRARIES} ) # vtk_module_autoinit is needed vtk_module_autoinit( TARGETS EigenSymmetric MODULES${VTK_LIBRARIES}
)


cd EigenSymmetric/build


If VTK is installed:

cmake ..


If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

cmake -DVTK_DIR:PATH=/home/me/vtk_build ..


Build the project:

make


and run it:

./EigenSymmetric


WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.