cheshirekow  v0.1.0
RValue.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Josh Bialkowski (jbialk@mit.edu)
3  *
4  * This file is part of mpblocks.
5  *
6  * mpblocks is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * mpblocks is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with mpblocks. If not, see <http://www.gnu.org/licenses/>.
18  */
27 #ifndef MPBLOCKS_CUDA_LINALG_RVALUE_H_
28 #define MPBLOCKS_CUDA_LINALG_RVALUE_H_
29 
30 #include <cmath>
31 
32 namespace mpblocks {
33 namespace cuda {
34 namespace linalg {
35 
37 template <typename Scalar, class Exp> class Scale;
38 
40 template <typename Scalar, class Mat>
41 class RValue
42 {
43  public:
44  typedef unsigned int Size_t;
45 
48  Size_t size() const
49  {
50  return static_cast<Mat const&>(*this).size();
51  }
52 
55  Size_t rows() const
56  {
57  return static_cast<Mat const&>(*this).rows();
58  }
59 
62  Size_t cols() const
63  {
64  return static_cast<Mat const&>(*this).cols();
65  }
66 
69  Scalar operator[]( Size_t i ) const
70  {
71  return static_cast<Mat const&>(*this)[i];
72  }
73 
76  Scalar operator()( Size_t i, Size_t j ) const
77  {
78  return static_cast<Mat const&>(*this)(i,j);
79  }
80 
82  Scalar norm_squared() const
83  {
84  Scalar r=0;
85  for(int i=0; i < size(); i++)
86  r += (*this)[i] * (*this)[i];
87  return r;
88  }
89 
91  Scalar norm() const
92  {
93  return std::sqrt( norm_squared() );
94  }
95 
98  {
99  Scalar scale = 1.0/norm();
100  return Scale<Scalar,Mat>( scale, static_cast<Mat const&>(*this) );
101  }
102 
104  Scalar maxCoeff()
105  {
106  Scalar r = (*this)[0];
107  for(int i=1; i < size(); i++)
108  if( (*this)[i] > r )
109  r = (*this)[i];
110  return r;
111  }
112 
113 
114 };
115 
116 
117 
118 
119 } // linalg
120 } // cuda
121 } // mpblocks
122 
123 
124 
125 
126 
127 #endif // MATRIXEXPRESSION_H_
__device__ __host__ Scalar operator[](Size_t i) const
return the evaluated i'th element of a vector expression
Definition: RValue.h:69
__device__ __host__ Size_t cols() const
return the columns of a matrix expression
Definition: RValue.h:62
__device__ __host__ Size_t size() const
return the size for a vector
Definition: RValue.h:48
__device__ __host__ Scalar maxCoeff()
Definition: RValue.h:104
__device__ __host__ Scale< Scalar, Mat > normalized() const
Definition: RValue.h:97
__device__ __host__ Size_t rows() const
return the rows of a matrix expression
Definition: RValue.h:55
#define __device__
Definition: fakecuda.h:34
__device__ __host__ Scalar norm() const
Definition: RValue.h:91
#define __host__
Definition: fakecuda.h:37
expression template for rvalues
Definition: RValue.h:41
__device__ __host__ Scalar norm_squared() const
Definition: RValue.h:82
__device__ __host__ Scalar operator()(Size_t i, Size_t j) const
return the evaluated (i,j)'th element of a matrix expression
Definition: RValue.h:76
forward declared for normalized()
Definition: RValue.h:37