ViennaCL - The Vienna Computing Library  1.5.2
matrix_size_deducer.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_TOOLS_MATRIX_SIZE_DEDUCER_HPP_
2 #define VIENNACL_TOOLS_MATRIX_SIZE_DEDUCER_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2014, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8  Portions of this software are copyright by UChicago Argonne, LLC.
9 
10  -----------------
11  ViennaCL - The Vienna Computing Library
12  -----------------
13 
14  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15 
16  (A list of authors and contributors can be found in the PDF manual)
17 
18  License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20 
25 #include <string>
26 #include <fstream>
27 #include <sstream>
28 #include <cmath>
29 #include <vector>
30 #include <map>
31 
32 #include "viennacl/forwards.h"
34 
35 namespace viennacl
36 {
37  namespace tools
38  {
39 
46  template <typename LHS, typename RHS, typename OP>
48  {
49  //Standard case: size1 from lhs, size2 from rhs (fits most cases)
50  static vcl_size_t size1(LHS & lhs, RHS & /*rhs*/) { return lhs.size1(); }
51  static vcl_size_t size2(LHS & /*lhs*/, RHS & rhs) { return rhs.size2(); }
52  };
53 
55  //special case: outer vector product:
56  template <typename ScalarType>
57  struct MATRIX_SIZE_DEDUCER<const viennacl::vector_base<ScalarType>,
58  const viennacl::vector_base<ScalarType>,
60  {
62  viennacl::vector_base<ScalarType> const & /*rhs*/) { return lhs.size(); }
63 
64  static vcl_size_t size2(viennacl::vector_base<ScalarType> const & /*lhs*/,
65  viennacl::vector_base<ScalarType> const & rhs) { return rhs.size(); }
66  };
67 
68 
69  //special case: multiplication with a scalar
70  template <typename LHS, typename RHS, typename OP, typename ScalarType>
71  struct MATRIX_SIZE_DEDUCER<const viennacl::matrix_expression<const LHS, const RHS, OP>,
72  const ScalarType,
74  {
76  ScalarType const & /*rhs*/) { return MATRIX_SIZE_DEDUCER<const LHS, const RHS, OP>::size1(lhs.lhs(), lhs.rhs()); }
77 
79  ScalarType const & /*rhs*/) { return MATRIX_SIZE_DEDUCER<const LHS, const RHS, OP>::size2(lhs.lhs(), lhs.rhs()); }
80  };
81 
82  //special case: multiplication with a scalar
83  template <typename T, typename F, typename ScalarType>
84  struct MATRIX_SIZE_DEDUCER<const viennacl::matrix_base<T, F>,
85  const ScalarType,
87  {
88  static vcl_size_t size1(viennacl::matrix_base<T, F> const & lhs,
89  ScalarType const & /*rhs*/) { return lhs.size1(); }
90 
91  static vcl_size_t size2(viennacl::matrix_base<T, F> const & lhs,
92  ScalarType const & /*rhs*/) { return lhs.size2(); }
93  };
94 
95 
96  //special case: division with a scalar
97  template <typename LHS, typename RHS, typename OP, typename ScalarType>
98  struct MATRIX_SIZE_DEDUCER<const viennacl::matrix_expression<const LHS, const RHS, OP>,
99  const ScalarType,
101  {
103  ScalarType const & /*rhs*/) { return MATRIX_SIZE_DEDUCER<const LHS, const RHS, OP>::size1(lhs.lhs(), lhs.rhs()); }
104 
106  ScalarType const & /*rhs*/) { return MATRIX_SIZE_DEDUCER<const LHS, const RHS, OP>::size2(lhs.lhs(), lhs.rhs()); }
107  };
108 
109  //special case: division with a scalar
110  template <typename T, typename F, typename ScalarType>
111  struct MATRIX_SIZE_DEDUCER<const viennacl::matrix_base<T, F>,
112  const ScalarType,
114  {
115  static vcl_size_t size1(viennacl::matrix_base<T, F> const & lhs,
116  ScalarType const & /*rhs*/) { return lhs.size1(); }
117 
118  static vcl_size_t size2(viennacl::matrix_base<T, F> const & lhs,
119  ScalarType const & /*rhs*/) { return lhs.size2(); }
120  };
121 
122  //special case: diagonal from vector
123  template <typename T>
124  struct MATRIX_SIZE_DEDUCER<const viennacl::vector_base<T>,
125  const int,
127  {
128  static vcl_size_t size1(viennacl::vector_base<T> const & lhs,
129  const int k) { return lhs.size() + static_cast<vcl_size_t>(std::fabs(double(k))); }
130 
131  static vcl_size_t size2(viennacl::vector_base<T> const & lhs,
132  const int k) { return lhs.size() + static_cast<vcl_size_t>(std::fabs(double(k))); }
133  };
134 
135 
136 
137 
138 
139 
140 
141 
142  //special case: transposed matrix-vector product: Return the number of rows of the matrix
143  template <typename MatrixType>
144  struct MATRIX_SIZE_DEDUCER<MatrixType,
145  MatrixType,
146  viennacl::op_trans>
147  {
148  static vcl_size_t size1(const MatrixType & lhs,
149  const MatrixType & /*rhs*/) { return lhs.size2(); }
150  static vcl_size_t size2(const MatrixType & lhs,
151  const MatrixType & /*rhs*/) { return lhs.size1(); }
152  };
153 
154  // A^T * B
155  template <typename ScalarType, typename T1, typename F2>
156  struct MATRIX_SIZE_DEDUCER<const viennacl::matrix_expression<T1,
157  T1, op_trans>,
158  const viennacl::matrix_base<ScalarType, F2>,
160  {
162  T1,
163  op_trans> const & lhs,
164  viennacl::matrix_base<ScalarType, F2> const & /*rhs*/) { return lhs.lhs().size2(); }
166  T1,
167  op_trans> const & /*lhs*/,
168  viennacl::matrix_base<ScalarType, F2> const & rhs) { return rhs.size2(); }
169  };
170 
171 
172  // A * B^T
173 
174  template <typename ScalarType, typename F1, typename T2>
175  struct MATRIX_SIZE_DEDUCER<const viennacl::matrix_base<ScalarType, F1>,
177  T2, op_trans>,
179  {
182  T2,
183  op_trans> const & /*rhs*/) { return lhs.size1(); }
186  T2,
187  op_trans> const & rhs) { return rhs.lhs().size1(); }
188  };
189 
190 
191 
192 
193  // A^T * B^T
194 
195  template <typename T1, typename T2>
196  struct MATRIX_SIZE_DEDUCER<const viennacl::matrix_expression<T1,
197  T1, op_trans>,
199  T2, op_trans>,
201  {
204 
205  static vcl_size_t size1(LHSType const & lhs,
206  RHSType const & /*rhs*/) { return lhs.lhs().size2(); }
207  static vcl_size_t size2(LHSType const & /*lhs*/,
208  RHSType const & rhs) { return rhs.lhs().size1(); }
209  };
211  }
212 }
213 
214 #endif
215 
A tag class representing multiplication by a scalar.
Definition: forwards.h:74
static vcl_size_t size2(LHS &, RHS &rhs)
Definition: matrix_size_deducer.hpp:51
std::size_t vcl_size_t
Definition: forwards.h:58
Adapter classes for sparse matrices made of the STL type std::vector >
size_type size2() const
Returns the number of columns.
Definition: matrix.hpp:627
A tag class representing a matrix given by a vector placed on a certain (off-)diagonal.
Definition: forwards.h:141
Common base class for dense vectors, vector ranges, and vector slices.
Definition: forwards.h:205
This file provides the forward declarations for the main types used within ViennaCL.
A tag class representing division.
Definition: forwards.h:80
RHS & rhs() const
Get right hand side operand.
Definition: matrix.hpp:177
LHS & lhs() const
Get left hand side operand.
Definition: matrix.hpp:174
A dense matrix class.
Definition: forwards.h:290
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:29
Deduces the size of the resulting vector represented by a vector_expression from the operands...
Definition: matrix_size_deducer.hpp:47
static vcl_size_t size1(LHS &lhs, RHS &)
Definition: matrix_size_deducer.hpp:50
size_type size() const
Returns the length of the vector (cf. std::vector)
Definition: vector.hpp:837
A tag class representing matrix-matrix products.
Definition: forwards.h:78
Expression template class for representing a tree of expressions which ultimately result in a matrix...
Definition: forwards.h:283
A tag class representing matrix-vector products and element-wise multiplications. ...
Definition: forwards.h:76
size_type size1() const
Returns the number of rows.
Definition: matrix.hpp:625