36 #define VIENNACL_WITH_UBLAS
52 namespace ublas = boost::numeric::ublas;
61 throw std::invalid_argument(
"File is not opened");
66 template <
typename MatrixLayout>
71 throw std::invalid_argument(
"File is not opened");
74 boost::numeric::ublas::matrix<ScalarType> h_A(A.
size1(), A.
size2());
76 for(std::size_t i = 0; i < h_A.size1(); i++) {
77 for(std::size_t j = 0; j < h_A.size2(); j++) {
91 ublas::matrix<ScalarType> A(A_orig.
size1(), A_orig.
size2());
93 for (
unsigned int i = 0; i < A.size1(); i++) {
94 for (
unsigned int j = 0; j < A.size2(); j++)
95 std::cout << std::setprecision(6) << std::fixed << A(i, j) <<
"\t";
96 std::cout << std::endl;
98 std::cout << std::endl;
105 for (
unsigned int i = 0; i < A.size1(); i++) {
106 for (
unsigned int j = 0; j < A.size2(); j++)
107 std::cout << std::setprecision(6) << std::fixed << A(i, j) <<
"\t";
108 std::cout << std::endl;
110 std::cout << std::endl;
117 for (
unsigned int i = 0; i < v.size(); i++)
118 std::cout << std::setprecision(6) << std::fixed << v[i] <<
",\t";
123 template <
typename MatrixType,
typename VCLMatrixType>
126 typedef typename MatrixType::value_type value_type;
128 ublas::matrix<value_type> vcl_A_cpu(vcl_A.size1(), vcl_A.size2());
132 for (std::size_t i=0; i<ublas_A.size1(); ++i)
134 for (std::size_t j=0; j<ublas_A.size2(); ++j)
136 if (std::abs(ublas_A(i,j) - vcl_A_cpu(i,j)) >
EPS *
std::max(std::abs(ublas_A(i, j)), std::abs(vcl_A_cpu(i, j))))
138 std::cout <<
"Error at index (" << i <<
", " << j <<
"): " << ublas_A(i,j) <<
" vs. " << vcl_A_cpu(i,j) << std::endl;
139 std::cout << std::endl <<
"TEST failed!" << std::endl;
144 std::cout <<
"PASSED!" << std::endl;
148 template <
typename VectorType>
152 for (std::size_t i=0; i<vec_A.size(); ++i)
154 if (std::abs(vec_A[i] - vec_B[i]) >
EPS)
156 std::cout <<
"Error at index (" << i <<
"): " << vec_A[i] <<
" vs " <<vec_B[i] << std::endl;
157 std::cout << std::endl <<
"TEST failed!" << std::endl;
161 std::cout <<
"PASSED!" << std::endl;
171 for (
unsigned int i = 0; i < v.size(); ++i)
172 v[i] = randomNumber();
182 template <
typename NumericT>
184 std::vector<NumericT> D,
189 std::size_t row_start = start + 1;
190 for(std::size_t i = 0; i < A.size2(); i++)
193 for(std::size_t j = row_start; j < A.size1(); j++)
194 ss = ss +(D[j] * A(j, i));
196 for(std::size_t j = row_start; j < A.size1(); j++)
197 A(j, i) = A(j, i) - (2 * D[j] * ss);
201 template <
typename NumericT>
203 std::vector<NumericT> D)
207 for(std::size_t i = 0; i < A.size1(); i++)
210 for(std::size_t j = 0; j < A.size2(); j++)
211 ss = ss + (D[j] * A(i, j));
215 for(std::size_t j = 0; j < A.size2(); j++)
216 A(i, j) = A(i, j) - (2 * D[j] * sum_Av);
221 template <
typename NumericT>
223 std::vector<NumericT> D,
228 ublas::matrix<NumericT> ubl_P(A_size1, A_size1);
229 ublas::matrix<ScalarType> I = ublas::identity_matrix<ScalarType>(Q.size1());
230 ublas::matrix<NumericT> Q_temp(Q.size1(), Q.size2());
232 for(std::size_t i = 0; i < Q.size1(); i++)
234 for(std::size_t j = 0; j < Q.size2(); j++)
236 Q_temp(i, j) = Q(i, j);
240 ubl_P = ublas::identity_matrix<NumericT>(A_size1);
243 for(std::size_t i = 0; i < A_size1; i++)
245 for(std::size_t j = 0; j < A_size1; j++)
247 ubl_P(i, j) = I(i, j) - beta * (D[i] * D[j]);
253 template <
typename NumericT>
255 std::vector<NumericT> & tmp1,
256 std::vector<NumericT> & tmp2,
260 for(
int i2 = m - 1; i2 >= l; i2--)
262 std::size_t i =
static_cast<std::size_t
>(i2);
263 for(std::size_t k = 0; k < Q.size1(); k++)
266 Q(k, i+1) = tmp2[i] * Q(k, i) + tmp1[i]*h;
267 Q(k, i) = tmp1[i] * Q(k, i) - tmp2[i]*h;
273 template <
typename NumericT>
275 std::vector<NumericT> & V,
276 std::size_t row_start,
277 std::size_t col_start,
282 for(std::size_t i = row_start; i < A.size1(); i++)
284 V[i - row_start] = A(i, col_start);
289 for(std::size_t i = col_start; i < A.size1(); i++)
291 V[i - col_start] = A(row_start, i);
296 template <
typename NumericT>
298 std::vector<NumericT> & D,
299 std::vector<NumericT> & S)
304 for(i = 0; i < size - 1; i++)
307 S[i + 1] = A(i, i + 1);
309 D[size - 1] = A(size - 1, size - 1);
313 template <
typename MatrixLayout>
316 std::cout <<
"Reading..." << std::endl;
320 std::fstream f(fn.c_str(), std::fstream::in);
326 std::vector<ScalarType> std_D(sz), std_E(sz), std_F(sz), std_G(sz), std_H(sz);
327 ublas::matrix<ScalarType> ubl_A(sz, sz), ubl_Q(sz, sz);
330 std::cout <<
"Testing matrix of size " << sz <<
"-by-" << sz << std::endl << std::endl;
339 std::cout << std::endl <<
"Testing house_update_left..." << std::endl;
346 std::cout << std::endl <<
"Testing house_update_right..." << std::endl;
356 std::cout << std::endl <<
"Testing house_update_QL..." << std::endl;
357 ubl_Q = ublas::identity_matrix<ScalarType>(ubl_Q.size1());
367 std::cout << std::endl <<
"Testing givens next..." << std::endl;
379 std::cout << std::endl <<
"Testing copy vec..." << std::endl;
387 std::cout << std::endl <<
"Testing bidiag pack..." << std::endl;
405 std::cout << std::endl <<
"Test qr_method_sym for row_major matrix" << std::endl;
406 test_qr_method_sym<viennacl::row_major>(
"../examples/testdata/eigen/symm5.example");
408 std::cout << std::endl <<
"Test qr_method_sym for column_major matrix" << std::endl;
409 test_qr_method_sym<viennacl::column_major>(
"../examples/testdata/eigen/symm5.example");
412 std::cout << std::endl <<
"--------TEST SUCCESSFULLY COMPLETED----------" << std::endl;
bool check_for_equality(MatrixType const &ublas_A, VCLMatrixType const &vcl_A)
void copy_vec(ublas::matrix< NumericT > &A, std::vector< NumericT > &V, std::size_t row_start, std::size_t col_start, bool copy_col)
Implementations of dense matrix related operations including matrix-vector products.
void copy_vec(matrix_base< SCALARTYPE > &A, vector_base< SCALARTYPE > &V, vcl_size_t row_start, vcl_size_t col_start, bool copy_col)
This function copies a row or a column from a matrix to a vector.
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
void house_update_A_right(matrix_base< NumericT > &A, vector_base< NumericT > &D)
This function applies a householder transformation to a matrix: A <- A * P with a householder reflect...
void test_qr_method_sym(const std::string &fn)
void house_update_QL(matrix_base< NumericT > &Q, vector_base< NumericT > &D, vcl_size_t A_size1)
This function updates the matrix Q, which is needed for the computation of the eigenvectors.
void house_update_QL(ublas::matrix< NumericT > &Q, std::vector< NumericT > D, std::size_t A_size1)
void finish()
Synchronizes the execution. finish() will only return after all compute kernels (CUDA, OpenCL) have completed.
T max(const T &lhs, const T &rhs)
Maximum.
void read_matrix_size(std::fstream &f, std::size_t &sz)
void house_update_A_right(ublas::matrix< NumericT > &A, std::vector< NumericT > D)
Common routines used for the QR method and SVD. Experimental.
vcl_size_t size(VectorType const &vec)
Generic routine for obtaining the size of a vector (ViennaCL, uBLAS, etc.)
void house_update_A_left(ublas::matrix< NumericT > &A, std::vector< NumericT > D, unsigned int start)
void fill_vector(std::vector< ScalarType > &v)
void givens_next(ublas::matrix< NumericT > &Q, std::vector< NumericT > &tmp1, std::vector< NumericT > &tmp2, int l, int m)
void bidiag_pack(matrix_base< NumericT > &A, VectorType &dh, VectorType &sh)
This function stores the diagonal and the superdiagonal of a matrix in two vectors.
result_of::size_type< T >::type start(T const &obj)
void house_update_A_left(matrix_base< NumericT > &A, vector_base< NumericT > &D, vcl_size_t start)
This function applies a householder transformation to a matrix. A <- P * A with a householder reflect...
size_type size2() const
Returns the number of columns.
size_type size1() const
Returns the number of rows.
void bidiag_pack(ublas::matrix< NumericT > &A, std::vector< NumericT > &D, std::vector< NumericT > &S)
Implementation of the QR method for eigenvalue computations. Experimental.
void prod(std::vector< std::map< IndexT, NumericT > > const &stl_A, std::vector< std::map< IndexT, NumericT > > const &stl_B, std::vector< std::map< IndexT, NumericT > > &stl_C)
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
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) ...
A small collection of sequential random number generators.
void read_matrix_body(std::fstream &f, viennacl::matrix< ScalarType, MatrixLayout > &A)
T min(const T &lhs, const T &rhs)
Minimum.
void matrix_print(viennacl::matrix< ScalarType > &A_orig)
Implementation of the ViennaCL scalar class.
void vector_print(std::vector< ScalarType > &v)
void givens_next(matrix_base< NumericT > &Q, vector_base< NumericT > &tmp1, vector_base< NumericT > &tmp2, int l, int m)
This function updates the matrix Q. It is part of the tql2 algorithm.