Go to the documentation of this file.00001 #ifndef VIENNACL_VANDERMONDE_MATRIX_HPP
00002 #define VIENNACL_VANDERMONDE_MATRIX_HPP
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <cmath>
00021
00026 #include "viennacl/forwards.h"
00027 #include "viennacl/vector.hpp"
00028 #include "viennacl/ocl/context.hpp"
00029
00030 #include "viennacl/fft.hpp"
00031
00032 #include "viennacl/linalg/vandermonde_matrix_operations.hpp"
00033
00034 namespace viennacl {
00040 template<class SCALARTYPE, unsigned int ALIGNMENT>
00041 class vandermonde_matrix
00042 {
00043 public:
00048 explicit vandermonde_matrix()
00049 {
00050 viennacl::linalg::kernels::fft<SCALARTYPE, 1>::init();
00051 }
00052
00059 explicit vandermonde_matrix(std::size_t rows, std::size_t cols) : elements_(rows)
00060 {
00061 assert(rows == cols && "Vandermonde matrix must be square in this release!");
00062 viennacl::linalg::kernels::fft<SCALARTYPE, 1>::init();
00063 }
00064
00071 void resize(std::size_t sz, bool preserve = true) {
00072 elements_.resize(sz, preserve);
00073 }
00074
00079 viennacl::ocl::handle<cl_mem> handle() const { return elements_.handle(); }
00080
00085 viennacl::vector<SCALARTYPE, ALIGNMENT> & elements() { return elements_; }
00086 viennacl::vector<SCALARTYPE, ALIGNMENT> const & elements() const { return elements_; }
00087
00091 std::size_t size1() const { return elements_.size(); }
00092
00096 std::size_t size2() const { return elements_.size(); }
00097
00103 std::size_t internal_size() const { return elements_.internal_size(); }
00104
00111 entry_proxy<SCALARTYPE> operator()(std::size_t row_index)
00112 {
00113 return elements_[row_index];
00114 }
00115
00123 SCALARTYPE operator()(std::size_t row_index, std::size_t col_index) const
00124 {
00125 assert(row_index < size1() && col_index < size2() && "Invalid access");
00126
00127 return pow(elements_[row_index], static_cast<int>(col_index));
00128 }
00129
00130 private:
00131 vandermonde_matrix(vandermonde_matrix const & t) {}
00132 vandermonde_matrix & operator=(vandermonde_matrix const & t) {}
00133
00134 viennacl::vector<SCALARTYPE, ALIGNMENT> elements_;
00135 };
00136
00143 template <typename SCALARTYPE, unsigned int ALIGNMENT>
00144 void copy(std::vector<SCALARTYPE>& cpu_vec, vandermonde_matrix<SCALARTYPE, ALIGNMENT>& gpu_mat)
00145 {
00146 assert(cpu_vec.size() == gpu_mat.size1() && "Size mismatch");
00147 copy(cpu_vec, gpu_mat.elements());
00148 }
00149
00156 template <typename SCALARTYPE, unsigned int ALIGNMENT>
00157 void copy(vandermonde_matrix<SCALARTYPE, ALIGNMENT>& gpu_mat, std::vector<SCALARTYPE>& cpu_vec)
00158 {
00159 assert(cpu_vec.size() == gpu_mat.size1() && "Size mismatch");
00160 copy(gpu_mat.elements(), cpu_vec);
00161 }
00162
00169 template <typename SCALARTYPE, unsigned int ALIGNMENT, typename MATRIXTYPE>
00170 void copy(vandermonde_matrix<SCALARTYPE, ALIGNMENT>& vander_src, MATRIXTYPE& com_dst)
00171 {
00172 std::size_t size = vander_src.size1();
00173 assert(size == com_dst.size1() && "Size mismatch");
00174 assert(size == com_dst.size2() && "Size mismatch");
00175 std::vector<SCALARTYPE> tmp(size);
00176 copy(vander_src, tmp);
00177
00178 for(std::size_t i = 0; i < size; i++) {
00179 for(std::size_t j = 0; j < size; j++) {
00180 com_dst(i, j) = pow(tmp[i], static_cast<int>(j));
00181 }
00182 }
00183 }
00184
00191 template <typename SCALARTYPE, unsigned int ALIGNMENT, typename MATRIXTYPE>
00192 void copy(MATRIXTYPE& com_src, vandermonde_matrix<SCALARTYPE, ALIGNMENT>& vander_dst)
00193 {
00194 std::size_t size = vander_dst.size1();
00195 assert(size == com_src.size1() && "Size mismatch");
00196 assert(size == com_src.size2() && "Size mismatch");
00197 std::vector<SCALARTYPE> tmp(size);
00198
00199 for(std::size_t i = 0; i < size; i++)
00200 tmp[i] = com_src(i, 1);
00201
00202 copy(tmp, vander_dst);
00203 }
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00219 template<class SCALARTYPE, unsigned int ALIGNMENT>
00220 std::ostream & operator<<(std::ostream& s, vandermonde_matrix<SCALARTYPE, ALIGNMENT>& gpu_matrix)
00221 {
00222 std::size_t size = gpu_matrix.size1();
00223 std::vector<SCALARTYPE> tmp(size);
00224 copy(gpu_matrix, tmp);
00225 s << "[" << size << "," << size << "](\n";
00226
00227 for(std::size_t i = 0; i < size; i++) {
00228 s << "(";
00229 for(std::size_t j = 0; j < size; j++) {
00230 s << pow(tmp[i], j);
00231 if(j < (size - 1)) s << ",";
00232 }
00233 s << ")";
00234 }
00235 s << ")";
00236 return s;
00237 }
00238
00239 }
00240
00241 #endif // _VIENNACL_VANDERMONDE_MATRIX_HPP