コード例 #1
0
ファイル: bisect.cpp プロジェクト: andiselinger/viennacl-dev
////////////////////////////////////////////////////////////////////////////////
//! Run a simple test
////////////////////////////////////////////////////////////////////////////////
bool runTest(unsigned int mat_size)
{
    bool bResult = false;

    std::vector<NumericT> diagonal(mat_size);
    std::vector<NumericT> superdiagonal(mat_size);
    std::vector<NumericT> eigenvalues_bisect(mat_size);

    // -------------Initialize data-------------------
    // Fill the diagonal and superdiagonal elements of the vector
    initInputData(diagonal, superdiagonal, mat_size);

    // -------Start the bisection algorithm------------
    std::cout << "Start the bisection algorithm" << std::endl;
    std::cout << "Matrix size: " << mat_size << std::endl;
    bResult = viennacl::linalg::bisect(diagonal, superdiagonal, eigenvalues_bisect);
    // Exit if an error occured during the execution of the algorithm
    if (bResult == false)
     return false;

    // ---------------Check the results---------------
    // The results of the bisection algorithm will be checked with the tql algorithm
    // Initialize Data for tql1 algorithm
    std::vector<NumericT> diagonal_tql(mat_size);
    std::vector<NumericT> superdiagonal_tql(mat_size);
    diagonal_tql = diagonal;
    superdiagonal_tql = superdiagonal;

    // Start the tql algorithm
    std::cout << "Start the tql algorithm..." << std::endl;
    viennacl::linalg::tql1<NumericT>(mat_size, diagonal_tql, superdiagonal_tql);

    // Ensure that eigenvalues from tql1 algorithm are sorted in ascending order
    std::sort(diagonal_tql.begin(), diagonal_tql.end());

    // Compare the results from the bisection algorithm with the results
    // from the tql algorithm.
    std::cout << "Start comparison..." << std::endl;
    for (unsigned int i = 0; i < mat_size; i++)
    {
       if (std::abs(diagonal_tql[i] - eigenvalues_bisect[i]) > EPS)
       {
         std::cout << std::setprecision(12) << diagonal_tql[i] << "  != " << eigenvalues_bisect[i] << "\n";
         return false;
       }
    }
/*
    // ------------Print the results---------------
    std::cout << "mat_size = " << mat_size << std::endl;
    for (unsigned int i = 0; i < mat_size; ++i)
    {
      std::cout << "Eigenvalue " << i << ":  \tbisect: " << std::setprecision(14) << eigenvalues_bisect[i] << "\ttql: " << diagonal_tql[i] << std::endl;
    }
*/
  return bResult;
    
}
コード例 #2
0
ファイル: bisect.cpp プロジェクト: 10imaging/viennacl
/**
* The main program is now as follows:
**/
int main()
{
    typedef float NumericT;

    bool bResult = false;
    unsigned int mat_size = 30;

/**
*   Create STL-vectors holding the diagonal, the superdiagonal, and the computed eigenvalues:
**/
    std::vector<NumericT> diagonal(mat_size);
    std::vector<NumericT> superdiagonal(mat_size);
    std::vector<NumericT> eigenvalues_bisect(mat_size);

/**
* Initialize the data with the helper routine defined earlier:
**/
    initInputData(diagonal, superdiagonal, mat_size);


/**
* Run the bisection algorithm for the provided input
**/
    std::cout << "Start the bisection algorithm" << std::endl;
    bResult = viennacl::linalg::bisect(diagonal, superdiagonal, eigenvalues_bisect);
    std::cout << std::endl << "---TUTORIAL COMPLETED---" << std::endl;

/**
* Uncomment the following code to also have the results printed:
**/
/*
    // ------------Print the results---------------
    std::cout << "mat_size = " << mat_size << std::endl;
    for (unsigned int i = 0; i < mat_size; ++i)
    {
      std::cout << "Eigenvalue " << i << ": " << std::setprecision(8) << eigenvalues_bisect[i] << std::endl;
    }
*/
    exit(bResult == true ? EXIT_SUCCESS : EXIT_FAILURE);
}