ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
structured-matrices.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 
27 // include necessary system headers
28 #include <iostream>
29 
30 // ViennaCL headers
35 #include "viennacl/linalg/prod.hpp"
36 
37 // Boost headers
38 #include "boost/numeric/ublas/vector.hpp"
39 #include "boost/numeric/ublas/matrix.hpp"
40 #include "boost/numeric/ublas/matrix_proxy.hpp"
41 #include "boost/numeric/ublas/io.hpp"
42 
47 int main()
48 {
49  typedef float ScalarType;
50 
51  std::size_t size = 4;
52 
56  boost::numeric::ublas::vector<ScalarType> ublas_vec(size);
57  boost::numeric::ublas::matrix<ScalarType> ublas_circulant(size, size);
58  boost::numeric::ublas::matrix<ScalarType> ublas_hankel(size, size);
59  boost::numeric::ublas::matrix<ScalarType> ublas_toeplitz(size, size);
60  boost::numeric::ublas::matrix<ScalarType> ublas_vandermonde(size, size);
61 
62  for (std::size_t i = 0; i < size; i++)
63  for (std::size_t j = 0; j < size; j++)
64  {
65  ublas_circulant(i,j) = static_cast<ScalarType>((i - j + size) % size);
66  ublas_hankel(i,j) = static_cast<ScalarType>((i + j) % (2 * size));
67  ublas_toeplitz(i,j) = static_cast<ScalarType>(i) - static_cast<ScalarType>(j);
68  ublas_vandermonde(i,j) = std::pow(ScalarType(1.0) + ScalarType(i)/ScalarType(1000.0), ScalarType(j));
69  }
70 
74  viennacl::vector<ScalarType> vcl_vec(size);
75  viennacl::vector<ScalarType> vcl_result(size);
76  viennacl::circulant_matrix<ScalarType> vcl_circulant(size, size);
77  viennacl::hankel_matrix<ScalarType> vcl_hankel(size, size);
78  viennacl::toeplitz_matrix<ScalarType> vcl_toeplitz(size, size);
79  viennacl::vandermonde_matrix<ScalarType> vcl_vandermonde(size, size);
80 
81  // copy matrices:
82  viennacl::copy(ublas_circulant, vcl_circulant);
83  viennacl::copy(ublas_hankel, vcl_hankel);
84  viennacl::copy(ublas_toeplitz, vcl_toeplitz);
85  viennacl::copy(ublas_vandermonde, vcl_vandermonde);
86 
87  // fill vectors:
88  for (std::size_t i = 0; i < size; i++)
89  {
90  ublas_vec[i] = ScalarType(i);
91  vcl_vec[i] = ScalarType(i);
92  }
93 
97  std::cout << "Circulant matrix before addition: " << vcl_circulant << std::endl << std::endl;
98  vcl_circulant += vcl_circulant;
99  std::cout << "Circulant matrix after addition: " << vcl_circulant << std::endl << std::endl;
100 
105  std::cout << "Hankel matrix before manipulation: " << vcl_hankel << std::endl << std::endl;
106  vcl_hankel(1, 2) = ScalarType(3.14);
107  std::cout << "Hankel matrix after manipulation: " << vcl_hankel << std::endl << std::endl;
108 
109  std::cout << "Vandermonde matrix before manipulation: " << vcl_vandermonde << std::endl << std::endl;
110  vcl_vandermonde(1) = ScalarType(1.1); //NOTE: Write access only via row index
111  std::cout << "Vandermonde matrix after manipulation: " << vcl_vandermonde << std::endl << std::endl;
112 
116  std::cout << "Toeplitz matrix: " << vcl_toeplitz << std::endl;
117  std::cout << "Vector: " << vcl_vec << std::endl << std::endl;
118  vcl_result = viennacl::linalg::prod(vcl_toeplitz, vcl_vec);
119  std::cout << "Result of matrix-vector product: " << vcl_result << std::endl << std::endl;
120 
124  std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
125 
126  return EXIT_SUCCESS;
127 }
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
A Hankel matrix class.
Definition: forwards.h:412
A Vandermonde matrix class.
Definition: forwards.h:418
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:102
vcl_size_t size(VectorType const &vec)
Generic routine for obtaining the size of a vector (ViennaCL, uBLAS, etc.)
Definition: size.hpp:235
Implementation of the hankel_matrix class for efficient manipulation of Hankel matrices. Experimental.
Implementation of the circulant_matrix class for efficient manipulation of circulant matrices...
Implementation of the vandermonde_matrix class for efficient manipulation of Vandermonde matrices...
void copy(std::vector< NumericT > &cpu_vec, circulant_matrix< NumericT, AlignmentV > &gpu_mat)
Copies a circulant matrix from the std::vector to the OpenCL device (either GPU or multi-core CPU) ...
float ScalarType
Definition: fft_1d.cpp:42
A Toeplitz matrix class.
Definition: forwards.h:415
A Circulant matrix class.
int main()
Implementation of the toeplitz_matrix class for efficient manipulation of Toeplitz matrices...