ViennaCL - The Vienna Computing Library  1.6.2
Free open-source GPU-accelerated linear algebra and solver library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
qr_method.cpp
Go to the documentation of this file.
1 /* =========================================================================
2  Copyright (c) 2010-2014, 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 
25 // System headers:
26 #include <iostream>
27 #include <fstream>
28 #include <stdexcept>
29 #include <vector>
30 
31 // ViennaCL headers:
32 #include "viennacl/linalg/prod.hpp"
34 
35 
39 template <typename ScalarType>
40 void initialize(viennacl::matrix<ScalarType> & A, std::vector<ScalarType> & v)
41 {
42  // System matrix:
43  ScalarType M[9][9] = {{ 4, 1, -2, 2, -7, 3, 9, -6, -2},
44  { 1, -2, 0, 1, -1, 5, 4, 7, 3},
45  {-2, 0, 3, 2, 0, 3, 6, 1, -1},
46  { 2, 1, 2, 1, 4, 5, 6, 7, 8},
47  {-7, -1, 0, 4, 5, 4, 9, 1, -8},
48  { 3, 5, 3, 5, 4, 9, -3, 3, 3},
49  { 9, 4, 6, 6, 9, -3, 3, 6, -7},
50  {-6, 7, 1, 7, 1, 3, 6, 2, 6},
51  {-2, 3, -1, 8, -8, 3, -7, 6, 1}};
52 
53  for(std::size_t i = 0; i < 9; i++)
54  for(std::size_t j = 0; j < 9; j++)
55  A(i, j) = M[i][j];
56 
57  // Known eigenvalues:
58  ScalarType V[9] = {ScalarType(12.6005), ScalarType(19.5905), ScalarType(8.06067), ScalarType(2.95074), ScalarType(0.223506),
59  ScalarType(24.3642), ScalarType(-9.62084), ScalarType(-13.8374), ScalarType(-18.3319)};
60 
61  for(std::size_t i = 0; i < 9; i++)
62  v[i] = V[i];
63 }
64 
68 template <typename ScalarType>
69 void vector_print(std::vector<ScalarType>& v )
70 {
71  for (unsigned int i = 0; i < v.size(); i++)
72  std::cout << std::setprecision(6) << std::fixed << v[i] << "\t";
73  std::cout << std::endl;
74 }
75 
79 int main()
80 {
81  // Change to 'double' if supported by your hardware.
82  typedef float ScalarType;
83 
84  std::cout << "Testing matrix of size " << 9 << "-by-" << 9 << std::endl;
85 
86  viennacl::matrix<ScalarType> A_input(9,9);
88  std::vector<ScalarType> eigenvalues_ref(9);
89  std::vector<ScalarType> eigenvalues(9);
90 
91  initialize(A_input, eigenvalues_ref);
92 
93  std::cout << std::endl <<"Input matrix: " << std::endl;
94  std::cout << A_input << std::endl;
95 
96 
105  std::cout << "Calculation..." << std::endl;
106  viennacl::linalg::qr_method_sym(A_input, Q, eigenvalues);
107 
111  std::cout << std::endl << "Eigenvalues:" << std::endl;
112  vector_print(eigenvalues);
113  std::cout << std::endl << "Reference eigenvalues:" << std::endl;
114  vector_print(eigenvalues_ref);
115  std::cout << std::endl;
116  std::cout << "Eigenvectors - each column is an eigenvector" << std::endl;
117  std::cout << Q << std::endl;
118 
122  std::cout << std::endl;
123  std::cout << "------- Tutorial completed --------" << std::endl;
124  std::cout << std::endl;
125 
126  return EXIT_SUCCESS;
127 }
128 
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
A dense matrix class.
Definition: forwards.h:374
float ScalarType
Definition: qr_method.cpp:49
int main()
Definition: qr_method.cpp:307
Implementation of the QR method for eigenvalue computations. Experimental.
float ScalarType
Definition: fft_1d.cpp:42
void vector_print(std::vector< ScalarType > &v)
void qr_method_sym(viennacl::matrix< SCALARTYPE > &A, viennacl::matrix< SCALARTYPE > &Q, std::vector< SCALARTYPE > &D)
Definition: qr-method.hpp:806