ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
tql.cpp
Go to the documentation of this file.
1 /* =========================================================================
2  Copyright (c) 2010-2015, Institute for Microelectronics,
3  Institute for Analysis and Scientific Computing,
4  TU Wien.
5  Portions of this software are copyright by UChicago Argonne, LLC.
6 
7  -----------------
8  ViennaCL - The Vienna Computing Library
9  -----------------
10 
11  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
12 
13  (A list of authors and contributors can be found in the PDF manual)
14 
15  License: MIT (X11), see file LICENSE in the base directory
16 ============================================================================= */
17 
18 
19 
24 /*
25 *
26 * Test file for tql-algorithm
27 *
28 */
29 
30 // include necessary system headers
31 #include <iostream>
32 
33 #ifndef NDEBUG
34  #define NDEBUG
35 #endif
36 
37 #define VIENNACL_WITH_UBLAS
38 
39 //include basic scalar and vector types of ViennaCL
40 #include "viennacl/scalar.hpp"
41 #include "viennacl/vector.hpp"
42 #include "viennacl/linalg/tql2.hpp"
43 
44 #define EPS 10.0e-5
45 
46 
47 
48 namespace ublas = boost::numeric::ublas;
49 typedef float ScalarType;
50 
51 
52 // Test the eigenvectors
53 // Perform the multiplication (T - lambda * I) * Q, with the original tridiagonal matrx T, the
54 // eigenvalues lambda and the eigenvectors in Q. Result has to be 0.
55 
56 template <typename MatrixLayout>
58  std::vector<ScalarType> & eigenvalues,
59  std::vector<ScalarType> & d,
60  std::vector<ScalarType> & e)
61 {
62  std::size_t Q_size = Q.size2();
63  ScalarType value = 0;
64 
65  for(std::size_t j = 0; j < Q_size; j++)
66  {
67  // calculate first row
68  value = (d[0]- eigenvalues[j]) * Q(0, j) + e[1] * Q(1, j);
69  if (value > EPS)
70  return false;
71 
72  // calcuate inner rows
73  for(std::size_t i = 1; i < Q_size - 1; i++)
74  {
75  value = e[i] * Q(i - 1, j) + (d[i]- eigenvalues[j]) * Q(i, j) + e[i + 1] * Q(i + 1, j);
76  if (value > EPS)
77  return false;
78  }
79 
80  // calculate last row
81  value = e[Q_size - 1] * Q(Q_size - 2, j) + (d[Q_size - 1] - eigenvalues[j]) * Q(Q_size - 1, j);
82  if (value > EPS)
83  return false;
84  }
85  return true;
86 }
87 
88 
93 template <typename MatrixLayout>
95 {
96  std::size_t sz = 220;
97 
99  std::vector<ScalarType> d(sz), e(sz), d_ref(sz), e_ref(sz);
100 
101  std::cout << "Testing matrix of size " << sz << "-by-" << sz << std::endl << std::endl;
102 
103  // Initialize diagonal and superdiagonal elements
104  for(unsigned int i = 0; i < sz; ++i)
105  {
106  d[i] = ((float)(i % 9)) - 4.5f;
107  e[i] = ((float)(i % 5)) - 4.5f;
108  }
109  e[0] = 0.0f;
110  d_ref = d;
111  e_ref = e;
112 
113 //---Run the tql2 algorithm-----------------------------------
114  viennacl::linalg::tql2(Q, d, e);
115 
116 
117 // ---Test the computed eigenvalues and eigenvectors
118  if(!test_eigen_val_vec<MatrixLayout>(Q, d, d_ref, e_ref))
119  exit(EXIT_FAILURE);
120 /*
121  for( unsigned int i = 0; i < sz; ++i)
122  std::cout << "Eigenvalue " << i << "= " << d[i] << std::endl;
123  */
124 }
125 
126 int main()
127 {
128 
129  std::cout << std::endl << "COMPUTATION OF EIGENVALUES AND EIGENVECTORS" << std::endl;
130  std::cout << std::endl << "Testing QL algorithm for symmetric tridiagonal row-major matrices..." << std::endl;
131  test_qr_method_sym<viennacl::row_major>();
132 
133  std::cout << std::endl << "Testing QL algorithm for symmetric tridiagonal column-major matrices..." << std::endl;
134  test_qr_method_sym<viennacl::column_major>();
135 
136  std::cout << std::endl <<"--------TEST SUCCESSFULLY COMPLETED----------" << std::endl;
137 }
void tql2(matrix_base< SCALARTYPE, F > &Q, VectorType &d, VectorType &e)
Definition: tql2.hpp:131
Implementation of the tql2-algorithm for eigenvalue computations.
A dense matrix class.
Definition: forwards.h:375
Represents a vector consisting of 1 at a given index and zeros otherwise. To be used as an initialize...
Definition: matrix_def.hpp:69
void test_qr_method_sym()
Definition: tql.cpp:94
bool test_eigen_val_vec(viennacl::matrix< ScalarType, MatrixLayout > &Q, std::vector< ScalarType > &eigenvalues, std::vector< ScalarType > &d, std::vector< ScalarType > &e)
Definition: tql.cpp:57
float ScalarType
Definition: tql.cpp:49
size_type size2() const
Returns the number of columns.
Definition: matrix_def.hpp:226
int main()
Definition: tql.cpp:126
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
float ScalarType
Definition: fft_1d.cpp:42
#define EPS
Definition: tql.cpp:44
Implementation of the ViennaCL scalar class.