CUV  0.9.201304091348
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
matrix_ops.hpp
1 //*LB*
2 // Copyright (c) 2010, University of Bonn, Institute for Computer Science VI
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 // * Neither the name of the University of Bonn
14 // nor the names of its contributors may be used to endorse or promote
15 // products derived from this software without specific prior written
16 // permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //*LE*
29 
30 
31 
32 
33 
34 #ifndef __MATRIX_OPS_HPP__
35 #define __MATRIX_OPS_HPP__
36 
38 
39 namespace cuv{
40 
41 
42 
43 
44  /***************************************************
45  * Get view on parts of matrix
46  * *************************************************/
63  template<class V, class M, class __memory_layout, class __index_type>
64  tensor<V,M,__memory_layout>* blockview(
65  tensor<V,M,__memory_layout> & matrix,
66  __index_type start_rows,
67  __index_type num_rows ,
68  __index_type start_cols,
69  __index_type num_cols);
70 
71 
72  /***************************************************
73  * BLAS3 stuff
74  ***************************************************/
95  template<class V, class M, class L>
96  void prod( tensor<V,M,L>& C,
97  const tensor<V,M,L>& A,
98  const tensor<V,M,L>& B,
99  char transA='n', char transB='n', const float& factAB=1.f, const float& factC=0.f);
100 
101 
103 // convenience: use transposed view instead of "t" and "n"
104  template<class V, class M, class L>
105  void prod(tensor<V,M,L>& C, const tensor<V,M,L>& A, const tensor<V,M, typename other_memory_layout<L>::type >& B, const float& factAB=1.f, const float& factC=0.f){
106  prod(C, A, *transposed_view_p(B), 'n', 't', factAB, factC);
107 }
109 // convenience: use transposed view instead of "t" and "n"
110  template<class V, class M, class L>
112  const tensor<V,M, typename other_memory_layout<L>::type >& A,
113  const tensor<V,M,L>& B,
114  const float& factAB=1.f, const float& factC=0.f){
115  prod(C, *transposed_view_p(A), B, 't', 'n', factAB, factC);
116 }
118  template<class V, class M, class L>
119  void prod(tensor<V,M,L>& C, const dia_matrix<V,M>& A, const tensor<V,M,L>& B, char transA='n', char transB='n', const float& factAB=1.f, const float& factC=0.f);
120 
128 template<class V, class M, class L>
129 void transpose(tensor<V,M, L>& dst, const tensor<V,M, L>& src);
130 
141  template<class V, class T, class M>
143 
145  template<class V, class T, class M>
146  std::auto_ptr<cuv::tensor<V,T,typename other_memory_layout<M>::type > > transposed_view(cuv::tensor<V,T,M>& src){
147  return std::auto_ptr<cuv::tensor<V,T,typename other_memory_layout<M>::type > >(transposed_view_p(src));
148  }
149 
151  template<class V, class T, class M>
153 
155  template<class V, class T, class M>
156  std::auto_ptr<const cuv::tensor<V,T,typename other_memory_layout<M>::type > > transposed_view(const cuv::tensor<V,T,M>& src){
157  return std::auto_ptr<const cuv::tensor<V,T,typename other_memory_layout<M>::type > >(transposed_view_p(src));
158  }
159  // end group blas3
161 
162  /***************************************************
163  * BLAS2 stuff
164  ***************************************************/
186  RF_ADD,
187  RF_MEAN,
188  RF_ADD_SQUARED,
189  RF_MAX,
190  RF_ARGMAX,
191  RF_ARGMIN,
192  RF_MIN,
193  RF_MULT,
194  RF_LOGADDEXP,
195  RF_ADDEXP,
196  };
197 
211  template<class V, class __value_type2, class M, class L>
212  void reduce_to_col(tensor<V, M>& dst, const tensor<__value_type2, M, L>& src, reduce_functor rf=RF_ADD, const __value_type2& factNew=1.f, const __value_type2& factOld=0.f);
213 
227  template<class V, class __value_type2, class M, class L>
228  void reduce_to_row(tensor<V, M>& dst, const tensor<__value_type2, M, L>& src, reduce_functor rf=RF_ADD, const __value_type2& factNew=1.f, const __value_type2& factOld=0.f);
229 
230 
238  template<class V, class M, class L>
239  tensor<V, M> sum(const tensor<V, M, L>& src, const int& axis){
240  cuvAssert(src.ndim()==2);
241  int tensor_length = 0;
242  if (axis==0){
243  tensor_length = src.shape()[1];
244  } else if (axis==1) {
245  tensor_length = src.shape()[0];
246  } else cuvAssert(false);
247 
248  tensor<V, M> dst(tensor_length);
249  if (axis==0){
250  reduce_to_row(dst, src);
251  } else if (axis==1) {
252  reduce_to_col(dst, src);
253  }
254  return dst;
255 
256  }
257 
258  // end of group reductions
260 
277  template<class V, class M>
278  void spmv(tensor<V, M>& dst, const dia_matrix<V, M>& A, const tensor<V, M>& v, char transA='n', const float& factAv=1.f, const float& factC=0.f);
279 
294  template<class V, class V2, class M, class L>
295  void matrix_op_vec(tensor<V,M,L>& Dst, const tensor<V,M,L>& Src, const tensor<V2,M>& v, int axis, BinaryFunctor bf, float factNew=1.f, float factOld=0.f, int n_params=0, float param0=0.f, float param1=0.f);
296 
297 
306  template<class V, class M, class L>
307  void matrix_plus_col(tensor<V, M, L>& A, const tensor<V, M>& v);
308 
317  template<class V, class M, class L>
318  void matrix_times_col(tensor<V, M, L>& A, const tensor<V, M>& v);
319 
328  template<class V, class M, class L>
329  void matrix_divide_col(tensor<V, M, L>& A, const tensor<V, M>& v);
330 
339  template<class V, class M, class L>
340  void matrix_plus_row(tensor<V, M, L>& A, const tensor<V, M>& v);
341 
350  template<class V, class M, class L>
351  void matrix_times_row(tensor<V, M, L>& A, const tensor<V, M>& v);
352 
361  template<class V, class M, class L>
362  void matrix_divide_row(tensor<V, M, L>& A, const tensor<V, M>& v); // end group blas2
364 } // cuv
365 
366 #endif