51 dense_matrix(std::size_t rows, std::size_t cols) : elements_(rows * cols), rows_(rows), cols_(cols) {}
53 T &
operator()(std::size_t i, std::size_t j) {
return elements_[i*cols_ + j]; }
54 T
const &
operator()(std::size_t i, std::size_t j)
const {
return elements_[i*cols_ + j]; }
56 std::size_t
size1()
const {
return rows_; }
57 std::size_t
size2()
const {
return cols_; }
61 for (std::size_t i = 0; i < other.
size1(); i++)
62 for (std::size_t j = 0; j < other.
size2(); j++)
63 elements_[i*cols_ + j] = other.elements_[i*cols_+j];
68 std::vector<T> elements_;
74 std::ostream & operator<<(std::ostream & os, dense_matrix<T>
const & mat)
76 std::cout <<
"[" << mat.size1() <<
"," << mat.size2() <<
"](";
77 for (std::size_t i=0; i<mat.size1(); ++i)
80 for (std::size_t j=0; j<mat.size2(); ++j)
81 std::cout << mat(i,j) <<
",";
89 template<
typename ScalarType>
96 for (std::size_t i = 0; i < m1.
size1(); i++)
97 for (std::size_t j = 0; j < m1.
size2(); j++)
99 df += (
m1(i,j) - m2(i,j)) * (
m1(i,j) - m2(i,j));
100 d1 +=
m1(i,j) *
m1(i,j);
101 d2 += m2(i,j) * m2(i,j);
107 return std::sqrt(df / std::max<ScalarType>(d1, d2));
111 template<
typename ScalarType>
117 for (std::size_t i = 0; i < vec.size(); i++)
119 df = df + pow(vec[i] - ref[i], 2);
120 norm_ref += ref[i] * ref[i];
123 return std::sqrt(df / norm_ref);
126 template<
typename ScalarType>
133 for (std::size_t i = 0; i < vec.size(); i++)
135 df = std::max<ScalarType>(std::fabs(vec[i] - ref[i]), df);
136 mx = std::max<ScalarType>(std::fabs(vec[i]), mx);
140 if (norm_max < df / mx)
149 template<
typename ScalarType>
153 std::vector<ScalarType> s_normal(2 * w * h);
157 for (
unsigned int i = 0; i < s_normal.size(); i+=2) {
162 std::cout << normal << std::endl;
164 std::cout << normal << std::endl;
169 template<
typename ScalarType>
172 std::size_t TOEPLITZ_SIZE = 47;
179 std::vector<ScalarType> input_ref(TOEPLITZ_SIZE);
180 std::vector<ScalarType> result_ref(TOEPLITZ_SIZE);
185 for (std::size_t i = 0; i < TOEPLITZ_SIZE; i++)
186 for (std::size_t j = 0; j < TOEPLITZ_SIZE; j++)
188 m1(i,j) =
static_cast<ScalarType>(i) - static_cast<ScalarType>(j);
192 for (std::size_t i = 0; i < TOEPLITZ_SIZE; i++)
205 for (std::size_t i = 0; i < m1.
size1(); i++)
208 for (std::size_t j = 0; j < m1.
size2(); j++)
209 entry +=
m1(i,j) * input_ref[j];
211 result_ref[i] = entry;
215 std::cout <<
"Matrix-Vector Product: " <<
diff_max(input_ref, result_ref);
216 if (
diff_max(input_ref, result_ref) < epsilon)
217 std::cout <<
" [OK]" << std::endl;
220 for (std::size_t i=0; i<input_ref.size(); ++i)
221 std::cout <<
"Should: " << result_ref[i] <<
", is: " << input_ref[i] << std::endl;
222 std::cout <<
" [FAILED]" << std::endl;
230 vcl_toeplitz1 += vcl_toeplitz2;
232 for (std::size_t i = 0; i < m1.
size1(); i++)
233 for (std::size_t j = 0; j < m1.
size2(); j++)
237 std::cout <<
"Matrix Addition: " <<
diff(m1, m2);
238 if (
diff(m1, m2) < epsilon)
239 std::cout <<
" [OK]" << std::endl;
242 std::cout <<
" [FAILED]" << std::endl;
249 vcl_toeplitz1(2,4) = 42;
251 for (std::size_t i=0; i<m1.
size1(); ++i)
253 if (i + 2 < m1.
size2())
258 std::cout <<
"Element manipulation: " <<
diff(m1, m2);
259 if (
diff(m1, m2) < epsilon)
260 std::cout <<
" [OK]" << std::endl;
263 std::cout <<
" [FAILED]" << std::endl;
270 template<
typename ScalarType>
273 std::size_t CIRCULANT_SIZE = 53;
280 std::vector<ScalarType> input_ref(CIRCULANT_SIZE);
281 std::vector<ScalarType> result_ref(CIRCULANT_SIZE);
286 for (std::size_t i = 0; i <
m1.size1(); i++)
287 for (std::size_t j = 0; j <
m1.size2(); j++)
293 for (std::size_t i = 0; i < input_ref.size(); i++)
306 for (std::size_t i = 0; i <
m1.size1(); i++)
309 for (std::size_t j = 0; j <
m1.size2(); j++)
310 entry +=
m1(i,j) * input_ref[j];
312 result_ref[i] = entry;
316 std::cout <<
"Matrix-Vector Product: " <<
diff_max(input_ref, result_ref);
317 if (
diff_max(input_ref, result_ref) < epsilon)
318 std::cout <<
" [OK]" << std::endl;
321 for (std::size_t i=0; i<input_ref.size(); ++i)
322 std::cout <<
"Should: " << result_ref[i] <<
", is: " << input_ref[i] << std::endl;
323 std::cout <<
" [FAILED]" << std::endl;
331 vcl_circulant1 += vcl_circulant2;
333 for (std::size_t i = 0; i <
m1.size1(); i++)
334 for (std::size_t j = 0; j <
m1.size2(); j++)
338 std::cout <<
"Matrix Addition: " <<
diff(
m1, m2);
339 if (
diff(
m1, m2) < epsilon)
340 std::cout <<
" [OK]" << std::endl;
343 std::cout <<
" [FAILED]" << std::endl;
350 vcl_circulant1(4,2) = 42;
352 for (std::size_t i = 0; i <
m1.size1(); i++)
353 for (std::size_t j = 0; j <
m1.size2(); j++)
355 if ((i - j +
m1.size1()) %
m1.size1() == 2)
360 std::cout <<
"Element manipulation: " <<
diff(
m1, m2);
361 if (
diff(
m1, m2) < epsilon)
362 std::cout <<
" [OK]" << std::endl;
365 std::cout <<
" [FAILED]" << std::endl;
372 template<
typename ScalarType>
375 std::size_t VANDERMONDE_SIZE = 61;
383 std::vector<ScalarType> input_ref(VANDERMONDE_SIZE);
384 std::vector<ScalarType> result_ref(VANDERMONDE_SIZE);
389 for (std::size_t i = 0; i <
m1.size1(); i++)
390 for (std::size_t j = 0; j <
m1.size2(); j++)
396 for (std::size_t i = 0; i < input_ref.size(); i++)
409 for (std::size_t i = 0; i <
m1.size1(); i++)
412 for (std::size_t j = 0; j <
m1.size2(); j++)
413 entry +=
m1(i,j) * input_ref[j];
415 result_ref[i] = entry;
419 std::cout <<
"Matrix-Vector Product: " <<
diff_max(input_ref, result_ref);
420 if (
diff_max(input_ref, result_ref) < epsilon)
421 std::cout <<
" [OK]" << std::endl;
424 for (std::size_t i=0; i<input_ref.size(); ++i)
425 std::cout <<
"Should: " << result_ref[i] <<
", is: " << input_ref[i] << std::endl;
426 std::cout <<
" [FAILED]" << std::endl;
439 vcl_vandermonde1(4) =
static_cast<ScalarType>(1.0001);
441 for (std::size_t j = 0; j <
m1.size2(); j++)
447 std::cout <<
"Element manipulation: " <<
diff(
m1, m2);
448 if (
diff(
m1, m2) < epsilon)
449 std::cout <<
" [OK]" << std::endl;
452 std::cout <<
" [FAILED]" << std::endl;
459 template<
typename ScalarType>
462 std::size_t HANKEL_SIZE = 7;
469 std::vector<ScalarType> input_ref(HANKEL_SIZE);
470 std::vector<ScalarType> result_ref(HANKEL_SIZE);
475 for (std::size_t i = 0; i <
m1.size1(); i++)
476 for (std::size_t j = 0; j <
m1.size2(); j++)
482 for (std::size_t i = 0; i < input_ref.size(); i++)
495 for (std::size_t i = 0; i <
m1.size1(); i++)
498 for (std::size_t j = 0; j <
m1.size2(); j++)
499 entry +=
m1(i,j) * input_ref[j];
501 result_ref[i] = entry;
505 std::cout <<
"Matrix-Vector Product: " <<
diff_max(input_ref, result_ref);
506 if (
diff_max(input_ref, result_ref) < epsilon)
507 std::cout <<
" [OK]" << std::endl;
510 for (std::size_t i=0; i<input_ref.size(); ++i)
511 std::cout <<
"Should: " << result_ref[i] <<
", is: " << input_ref[i] << std::endl;
512 std::cout <<
" [FAILED]" << std::endl;
520 vcl_hankel1 += vcl_hankel2;
522 for (std::size_t i = 0; i <
m1.size1(); i++)
523 for (std::size_t j = 0; j <
m1.size2(); j++)
527 std::cout <<
"Matrix Addition: " <<
diff(
m1, m2);
528 if (
diff(
m1, m2) < epsilon)
529 std::cout <<
" [OK]" << std::endl;
532 std::cout <<
" [FAILED]" << std::endl;
539 vcl_hankel1(4,2) = 42;
541 for (std::size_t i = 0; i <
m1.size1(); i++)
542 for (std::size_t j = 0; j <
m1.size2(); j++)
544 if ((i + j) % (2*
m1.size1()) == 6)
549 std::cout <<
"Element manipulation: " <<
diff(
m1, m2);
550 if (
diff(
m1, m2) < epsilon)
551 std::cout <<
" [OK]" << std::endl;
554 std::cout <<
" [FAILED]" << std::endl;
563 std::cout << std::endl;
564 std::cout <<
"----------------------------------------------" << std::endl;
565 std::cout <<
"----------------------------------------------" << std::endl;
566 std::cout <<
"## Test :: Structured Matrices" << std::endl;
567 std::cout <<
"----------------------------------------------" << std::endl;
568 std::cout <<
"----------------------------------------------" << std::endl;
569 std::cout << std::endl;
573 std::cout <<
"# Testing setup:" << std::endl;
574 std::cout <<
" eps: " << eps << std::endl;
575 std::cout <<
" numeric: float" << std::endl;
576 std::cout << std::endl;
577 std::cout <<
" -- Vandermonde matrix -- " << std::endl;
578 if (vandermonde_test<float>(static_cast<float>(eps)) == EXIT_FAILURE)
581 std::cout <<
" -- Circulant matrix -- " << std::endl;
582 if (circulant_test<float>(static_cast<float>(eps)) == EXIT_FAILURE)
585 std::cout <<
" -- Toeplitz matrix -- " << std::endl;
586 if (toeplitz_test<float>(static_cast<float>(eps)) == EXIT_FAILURE)
589 std::cout <<
" -- Hankel matrix -- " << std::endl;
590 if (hankel_test<float>(static_cast<float>(eps)) == EXIT_FAILURE)
594 std::cout << std::endl;
600 std::cout << std::endl;
601 std::cout <<
"# Testing setup:" << std::endl;
602 std::cout <<
" eps: " << eps << std::endl;
603 std::cout <<
" numeric: double" << std::endl;
604 std::cout << std::endl;
606 std::cout <<
" -- Vandermonde matrix -- " << std::endl;
607 if (vandermonde_test<double>(eps) == EXIT_FAILURE)
610 std::cout <<
" -- Circulant matrix -- " << std::endl;
611 if (circulant_test<double>(eps) == EXIT_FAILURE)
614 std::cout <<
" -- Toeplitz matrix -- " << std::endl;
615 if (toeplitz_test<double>(eps) == EXIT_FAILURE)
618 std::cout <<
" -- Hankel matrix -- " << std::endl;
619 if (hankel_test<double>(eps) == EXIT_FAILURE)
623 std::cout << std::endl;
624 std::cout <<
"------- Test completed --------" << std::endl;
625 std::cout << std::endl;
dense_matrix(std::size_t rows, std::size_t cols)
std::size_t size2() const
int vandermonde_test(ScalarType epsilon)
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
ScalarType diff(dense_matrix< ScalarType > const &m1, dense_matrix< ScalarType > const &m2)
int hankel_test(ScalarType epsilon)
vcl_size_t size2() const
Returns the number of columns of the matrix.
vcl_size_t size2() const
Returns the number of columns of the matrix.
int toeplitz_test(ScalarType epsilon)
vcl_size_t size1() const
Returns the number of rows of the matrix.
viennacl::ocl::device const & current_device()
Convenience function for returning the active device in the current context.
std::size_t size1() const
A Vandermonde matrix class.
dense_matrix & operator+=(dense_matrix const &other)
int circulant_test(ScalarType epsilon)
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
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...
vcl_size_t size1() const
Returns the number of rows of the matrix.
ScalarType diff_max(std::vector< ScalarType > &vec, std::vector< ScalarType > &ref)
vcl_size_t size1() const
Returns the number of rows of the matrix.
T & operator()(std::size_t i, std::size_t j)
Implementation of the vandermonde_matrix class for efficient manipulation of Vandermonde matrices...
void transpose(viennacl::matrix< NumericT, viennacl::row_major, AlignmentV > &input)
Inplace_transpose matrix.
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) ...
All routines related to the Fast Fourier Transform. Experimental.
A Circulant matrix class.
viennacl::matrix< float > m1
vcl_size_t size2() const
Returns the number of columns of the matrix.
T const & operator()(std::size_t i, std::size_t j) const
Implementation of the toeplitz_matrix class for efficient manipulation of Toeplitz matrices...
void fast_copy(const const_vector_iterator< SCALARTYPE, ALIGNMENT > &gpu_begin, const const_vector_iterator< SCALARTYPE, ALIGNMENT > &gpu_end, CPU_ITERATOR cpu_begin)