ViennaCL - The Vienna Computing Library  1.5.2
viennacl/traits/size.hpp
Go to the documentation of this file.
00001 #ifndef VIENNACL_TRAITS_SIZE_HPP_
00002 #define VIENNACL_TRAITS_SIZE_HPP_
00003 
00004 /* =========================================================================
00005    Copyright (c) 2010-2014, Institute for Microelectronics,
00006                             Institute for Analysis and Scientific Computing,
00007                             TU Wien.
00008    Portions of this software are copyright by UChicago Argonne, LLC.
00009 
00010                             -----------------
00011                   ViennaCL - The Vienna Computing Library
00012                             -----------------
00013 
00014    Project Head:    Karl Rupp                   rupp@iue.tuwien.ac.at
00015 
00016    (A list of authors and contributors can be found in the PDF manual)
00017 
00018    License:         MIT (X11), see file LICENSE in the base directory
00019 ============================================================================= */
00020 
00025 #include <string>
00026 #include <fstream>
00027 #include <sstream>
00028 #include "viennacl/forwards.h"
00029 #include "viennacl/meta/result_of.hpp"
00030 #include "viennacl/meta/predicate.hpp"
00031 
00032 #ifdef VIENNACL_WITH_UBLAS
00033 #include <boost/numeric/ublas/matrix_sparse.hpp>
00034 #include <boost/numeric/ublas/matrix.hpp>
00035 #endif
00036 
00037 #ifdef VIENNACL_WITH_EIGEN
00038 #include <Eigen/Core>
00039 #include <Eigen/Sparse>
00040 #endif
00041 
00042 #ifdef VIENNACL_WITH_MTL4
00043 #include <boost/numeric/mtl/mtl.hpp>
00044 #endif
00045 
00046 #include <vector>
00047 #include <map>
00048 
00049 namespace viennacl
00050 {
00051 
00052   namespace traits
00053   {
00054     //
00055     // Resize: Change the size of vectors and matrices
00056     //
00058     template <typename MatrixType>
00059     void resize(MatrixType & matrix, vcl_size_t rows, vcl_size_t cols)
00060     {
00061       matrix.resize(rows, cols);
00062     }
00063 
00065     template <typename VectorType>
00066     void resize(VectorType & vec, vcl_size_t new_size)
00067     {
00068       vec.resize(new_size);
00069     }
00070 
00072     #ifdef VIENNACL_WITH_UBLAS
00073     //ublas needs separate treatment:
00074     template <typename ScalarType>
00075     void resize(boost::numeric::ublas::compressed_matrix<ScalarType> & matrix,
00076                 vcl_size_t rows,
00077                 vcl_size_t cols)
00078     {
00079       matrix.resize(rows, cols, false); //Note: omitting third parameter leads to compile time error (not implemented in ublas <= 1.42)
00080     }
00081     #endif
00082 
00083 
00084     #ifdef VIENNACL_WITH_MTL4
00085     template <typename ScalarType>
00086     void resize(mtl::compressed2D<ScalarType> & matrix,
00087                 vcl_size_t rows,
00088                 vcl_size_t cols)
00089     {
00090       matrix.change_dim(rows, cols);
00091     }
00092 
00093     template <typename ScalarType>
00094     void resize(mtl::dense_vector<ScalarType> & vec,
00095                 vcl_size_t new_size)
00096     {
00097       vec.change_dim(new_size);
00098     }
00099     #endif
00100 
00101     #ifdef VIENNACL_WITH_EIGEN
00102     inline void resize(Eigen::MatrixXf & m,
00103                        vcl_size_t new_rows,
00104                        vcl_size_t new_cols)
00105     {
00106       m.resize(new_rows, new_cols);
00107     }
00108 
00109     inline void resize(Eigen::MatrixXd & m,
00110                        vcl_size_t new_rows,
00111                        vcl_size_t new_cols)
00112     {
00113       m.resize(new_rows, new_cols);
00114     }
00115 
00116     template <typename T, int options>
00117     inline void resize(Eigen::SparseMatrix<T, options> & m,
00118                        vcl_size_t new_rows,
00119                        vcl_size_t new_cols)
00120     {
00121       m.resize(new_rows, new_cols);
00122     }
00123 
00124     inline void resize(Eigen::VectorXf & v,
00125                        vcl_size_t new_size)
00126     {
00127       v.resize(new_size);
00128     }
00129 
00130     inline void resize(Eigen::VectorXd & v,
00131                        vcl_size_t new_size)
00132     {
00133       v.resize(new_size);
00134     }
00135     #endif
00136 
00139     //
00140     // size: Returns the length of vectors
00141     //
00143     template <typename VectorType>
00144     vcl_size_t size(VectorType const & vec)
00145     {
00146       return vec.size();
00147     }
00148 
00150     template <typename SparseMatrixType, typename VectorType>
00151     typename viennacl::enable_if< viennacl::is_any_sparse_matrix<SparseMatrixType>::value,
00152                                   vcl_size_t >::type
00153     size(vector_expression<const SparseMatrixType, const VectorType, op_prod> const & proxy)
00154     {
00155       return proxy.lhs().size1();
00156     }
00157 
00158     template <typename T, unsigned int A, typename VectorType>
00159     vcl_size_t size(vector_expression<const circulant_matrix<T, A>, const VectorType, op_prod> const & proxy) { return proxy.lhs().size1();  }
00160 
00161     template <typename T, unsigned int A, typename VectorType>
00162     vcl_size_t size(vector_expression<const hankel_matrix<T, A>, const VectorType, op_prod> const & proxy) { return proxy.lhs().size1();  }
00163 
00164     template <typename T, unsigned int A, typename VectorType>
00165     vcl_size_t size(vector_expression<const toeplitz_matrix<T, A>, const VectorType, op_prod> const & proxy) { return proxy.lhs().size1();  }
00166 
00167     template <typename T, unsigned int A, typename VectorType>
00168     vcl_size_t size(vector_expression<const vandermonde_matrix<T, A>, const VectorType, op_prod> const & proxy) { return proxy.lhs().size1();  }
00169 
00170     template <typename NumericT, typename F>
00171     vcl_size_t size(vector_expression<const matrix_base<NumericT, F>, const vector_base<NumericT>, op_prod> const & proxy)  //matrix-vector product
00172     {
00173       return proxy.lhs().size1();
00174     }
00175 
00176     template <typename NumericT, typename F>
00177     vcl_size_t size(vector_expression<const matrix_expression<const matrix_base<NumericT, F>, const matrix_base<NumericT, F>, op_trans>,
00178                                       const vector_base<NumericT>,
00179                                       op_prod> const & proxy)  //transposed matrix-vector product
00180     {
00181       return proxy.lhs().lhs().size2();
00182     }
00183 
00184 
00185     #ifdef VIENNACL_WITH_MTL4
00186     template <typename ScalarType>
00187     vcl_size_t size(mtl::dense_vector<ScalarType> const & vec) { return vec.used_memory(); }
00188     #endif
00189 
00190     #ifdef VIENNACL_WITH_EIGEN
00191     inline vcl_size_t size(Eigen::VectorXf const & v) { return v.rows(); }
00192     inline vcl_size_t size(Eigen::VectorXd const & v) { return v.rows(); }
00193     #endif
00194 
00195     template <typename LHS, typename RHS, typename OP>
00196     vcl_size_t size(vector_expression<LHS, RHS, OP> const & proxy)
00197     {
00198       return size(proxy.lhs());
00199     }
00200 
00201     template <typename LHS, typename RHS>
00202     vcl_size_t size(vector_expression<LHS, const vector_tuple<RHS>, op_inner_prod> const & proxy)
00203     {
00204       return proxy.rhs().const_size();
00205     }
00206 
00210     //
00211     // size1: No. of rows for matrices
00212     //
00214     template <typename MatrixType>
00215     vcl_size_t
00216     size1(MatrixType const & mat) { return mat.size1(); }
00217 
00219     template <typename RowType>
00220     vcl_size_t
00221     size1(std::vector< RowType > const & mat) { return mat.size(); }
00222 
00223     #ifdef VIENNACL_WITH_EIGEN
00224     inline vcl_size_t size1(Eigen::MatrixXf const & m) { return static_cast<vcl_size_t>(m.rows()); }
00225     inline vcl_size_t size1(Eigen::MatrixXd const & m) { return static_cast<vcl_size_t>(m.rows()); }
00226     template <typename T, int options>
00227     inline vcl_size_t size1(Eigen::SparseMatrix<T, options> & m) { return static_cast<vcl_size_t>(m.rows()); }
00228     #endif
00229 
00230 #ifdef VIENNACL_WITH_MTL4
00231     template <typename SCALARTYPE, typename T>
00232     vcl_size_t size1(mtl::dense2D<SCALARTYPE, T> const & m) { return static_cast<vcl_size_t>(m.num_rows()); }
00233     template <typename SCALARTYPE>
00234     vcl_size_t size1(mtl::compressed2D<SCALARTYPE> const & m) { return static_cast<vcl_size_t>(m.num_rows()); }
00235 #endif
00236 
00239     //
00240     // size2: No. of columns for matrices
00241     //
00243     template <typename MatrixType>
00244     typename result_of::size_type<MatrixType>::type
00245     size2(MatrixType const & mat) { return mat.size2(); }
00246 
00248     #ifdef VIENNACL_WITH_EIGEN
00249     inline vcl_size_t size2(Eigen::MatrixXf const & m) { return m.cols(); }
00250     inline vcl_size_t size2(Eigen::MatrixXd const & m) { return m.cols(); }
00251     template <typename T, int options>
00252     inline vcl_size_t size2(Eigen::SparseMatrix<T, options> & m) { return m.cols(); }
00253     #endif
00254 
00255 #ifdef VIENNACL_WITH_MTL4
00256     template <typename SCALARTYPE, typename T>
00257     vcl_size_t size2(mtl::dense2D<SCALARTYPE, T> const & m) { return static_cast<vcl_size_t>(m.num_cols()); }
00258     template <typename SCALARTYPE>
00259     vcl_size_t size2(mtl::compressed2D<SCALARTYPE> const & m) { return static_cast<vcl_size_t>(m.num_cols()); }
00260 #endif
00261 
00263     //
00264     // internal_size: Returns the internal (padded) length of vectors
00265     //
00267     template <typename NumericT>
00268     vcl_size_t internal_size(vector_base<NumericT> const & vec)
00269     {
00270       return vec.internal_size();
00271     }
00272 
00273 
00274     //
00275     // internal_size1: No. of internal (padded) rows for matrices
00276     //
00278     template <typename NumericT, typename F>
00279     vcl_size_t internal_size1(matrix_base<NumericT, F> const & mat) { return mat.internal_size1(); }
00280 
00281 
00282     //
00283     // internal_size2: No. of internal (padded) columns for matrices
00284     //
00286     template <typename NumericT, typename F>
00287     vcl_size_t internal_size2(matrix_base<NumericT, F> const & mat) { return mat.internal_size2(); }
00288 
00289 
00290     template <typename LHS>
00291     vcl_size_t size(vector_expression<LHS, const int, op_matrix_diag> const & proxy)
00292     {
00293       int k = proxy.rhs();
00294       int A_size1 = static_cast<int>(size1(proxy.lhs()));
00295       int A_size2 = static_cast<int>(size2(proxy.lhs()));
00296 
00297       int row_depth = std::min(A_size1, A_size1 + k);
00298       int col_depth = std::min(A_size2, A_size2 - k);
00299 
00300       return std::min(row_depth, col_depth);
00301     }
00302 
00303     template <typename LHS>
00304     vcl_size_t size(vector_expression<LHS, const unsigned int, op_row> const & proxy)
00305     {
00306       return size2(proxy.lhs());
00307     }
00308 
00309     template <typename LHS>
00310     vcl_size_t size(vector_expression<LHS, const unsigned int, op_column> const & proxy)
00311     {
00312       return size1(proxy.lhs());
00313     }
00314 
00315 
00316   } //namespace traits
00317 } //namespace viennacl
00318 
00319 
00320 #endif