cheshirekow  v0.1.0
Matrix.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_MATRIX_H_
28 #define MPBLOCKS_CUDA_LINALG_MATRIX_H_
29 
30 #include <iostream>
31 #include <cassert>
32 
33 namespace mpblocks {
34 namespace cuda {
35 namespace linalg {
36 
37 
38 template <typename Scalar, int ROWS, int COLS>
39 class Matrix :
40  public LValue< Scalar, Matrix<Scalar,ROWS,COLS> >,
41  public RValue< Scalar, Matrix<Scalar,ROWS,COLS> >
42 {
43  public:
46 
47 
48  protected:
49  Scalar m_data[ROWS*COLS];
50 
51  public:
53  int size() const { return ROWS*COLS; }
54 
56  int rows() const { return ROWS; }
57 
59  int cols() const { return COLS; }
60 
63  Scalar& operator[](int i)
64  {
65  return m_data[i];
66  }
67 
70  Scalar const& operator[](int i) const
71  {
72  return m_data[i];
73  }
74 
77  Scalar& operator()(int i, int j)
78  {
79 // assert(i<ROWS && j<COLS);
80  return m_data[i*COLS +j];
81  }
82 
85  Scalar const& operator()(int i, int j) const
86  {
87 // assert(i<ROWS && j<COLS);
88  return m_data[i*COLS +j];
89  }
90 
93  Matrix(){}
94 
95 
97  template <typename Exp>
99  Matrix( RValue<Scalar,Exp> const& exp )
100  {
101 // assert( exp.rows() == rows() );
102 // assert( exp.cols() == cols() );
103 
104  for( int i=0; i < rows(); i++)
105  {
106  for(int j=0; j < cols(); j++)
107  {
108  (*this)(i,j) = exp(i,j);
109  }
110  }
111  }
112 };
113 
114 
115 
116 
117 
118 
119 
120 } // linalg
121 } // cuda
122 } // mpblocks
123 
124 
125 
126 
127 
128 #endif // LINALG_H_
__device__ __host__ Scalar const & operator[](int i) const
vector accessor
Definition: Matrix.h:70
__device__ __host__ Scalar & operator()(int i, int j)
matrix accessor
Definition: Matrix.h:77
__device__ __host__ Scalar & operator[](int i)
vector accessor
Definition: Matrix.h:63
__device__ __host__ Matrix()
Default constructor.
Definition: Matrix.h:93
LValue< Scalar, Matrix_t > LValue_t
Definition: Matrix.h:45
__device__ __host__ int rows() const
Definition: Matrix.h:56
expression template for rvalues
Definition: LValue.h:36
#define __device__
Definition: fakecuda.h:34
__device__ __host__ Matrix(RValue< Scalar, Exp > const &exp)
Construct from any MatrixExpression:
Definition: Matrix.h:99
__device__ __host__ Scalar const & operator()(int i, int j) const
matrix accessor
Definition: Matrix.h:85
Matrix< Scalar, ROWS, COLS > Matrix_t
Definition: Matrix.h:44
#define __host__
Definition: fakecuda.h:37
__device__ __host__ int size() const
Definition: Matrix.h:53
expression template for rvalues
Definition: RValue.h:41
Scalar m_data[ROWS *COLS]
Definition: Matrix.h:49
__device__ __host__ int cols() const
Definition: Matrix.h:59