Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
BlockMatrix.hpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File: BlockMatrix.hpp
4 //
5 // For more information, please see: http://www.nektar.info
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description:
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
36 #ifndef NEKTAR_LIB_UTILITIES_LINEAR_ALGEBRA_BLOCK_MATRIX_HPP
37 #define NEKTAR_LIB_UTILITIES_LINEAR_ALGEBRA_BLOCK_MATRIX_HPP
38 
40 
45 
46 #include <boost/shared_ptr.hpp>
47 #include <boost/foreach.hpp>
48 
49 namespace Nektar
50 {
51  template<typename DataType, typename InnerMatrixType>
52  class NekMatrix<NekMatrix<DataType, InnerMatrixType>, BlockMatrixTag> : public ConstMatrix<typename NekMatrix<DataType, InnerMatrixType>::NumberType>
53  {
54  public:
57  typedef typename InnerType::NumberType NumberType;
59 
60  // Each inner matrix type can possible return references or value types from GetValue.
61  // Query the type here to find out.
62 
63  typedef typename InnerType::GetValueType GetValueType;
64  typedef typename InnerType::ConstGetValueType ConstGetValueType;
65 
66  public:
67  template<typename MatrixType>
68  class iterator_base
69  {
70  public:
71  typedef typename MatrixType::InnerType IteratorInnerType;
73 
74  public:
75  iterator_base(MatrixType& m, unsigned int curRow, unsigned int curCol) :
76  m_matrix(m),
77  m_curRow(curRow),
78  m_curColumn(curCol)
79  {
80  }
81 
83  m_matrix(m),
84  m_curRow(std::numeric_limits<unsigned int>::max()),
85  m_curColumn(std::numeric_limits<unsigned int>::max())
86  {
87  }
88 
89  iterator_base(const iterator_base<MatrixType>& rhs) :
90  m_matrix(rhs.m_matrix),
91  m_curRow(rhs.m_curRow),
92  m_curColumn(rhs.m_curColumn)
93  {
94  }
95 
96  void operator++()
97  {
98  if( m_curRow != std::numeric_limits<unsigned int>::max() )
99  {
100  boost::tie(m_curRow, m_curColumn) = StoragePolicy::Advance(
101  m_matrix.GetRows(), m_matrix.GetColumns(), m_curRow, m_curColumn);
102  }
103  }
104 
105  NumberType operator*()
106  {
107  return m_matrix(m_curRow, m_curColumn);
108  }
109 
110  bool operator==(const iterator_base<MatrixType>& rhs)
111  {
112  return m_curRow == rhs.m_curRow && m_curColumn == rhs.m_curColumn;
113  }
114 
115  bool operator!=(const iterator_base<MatrixType>& rhs)
116  {
117  return !(*this == rhs);
118  }
119 
120  private:
121  iterator_base<MatrixType>& operator=(const iterator_base<MatrixType>& rhs);
122 
124  //boost::shared_ptr<IteratorInnerType> m_curBlock;
125  unsigned int m_curRow;
126  unsigned int m_curColumn;
127 
128  };
129 
130  typedef iterator_base<ThisType> iterator;
131  typedef iterator_base<const ThisType> const_iterator;
132 
133  public:
135 
136  LIB_UTILITIES_EXPORT NekMatrix(unsigned int numberOfBlockRows, unsigned int numberOfBlockColumns,
137  unsigned int rowsPerBlock, unsigned int columnsPerBlock,
138  MatrixStorage type = eFULL);
139 
140  LIB_UTILITIES_EXPORT NekMatrix(unsigned int numberOfBlockRows, unsigned int numberOfBlockColumns,
141  const unsigned int* rowsPerBlock, const unsigned int* columnsPerBlock,
142  MatrixStorage type = eFULL);
143 
144  LIB_UTILITIES_EXPORT NekMatrix(unsigned int numberOfBlockRows, unsigned int numberOfBlockColumns,
145  const Array<OneD, const unsigned int>& rowsPerBlock, const Array<OneD, const unsigned int>& columnsPerBlock,
146  MatrixStorage type = eFULL);
147 
149  const Array<OneD, const unsigned int>& columnsPerBlock,
150  MatrixStorage type = eFULL);
151 
152  LIB_UTILITIES_EXPORT NekMatrix(const ThisType& rhs) ;
153 
154  LIB_UTILITIES_EXPORT unsigned int GetRequiredStorageSize() const;
155 
156  LIB_UTILITIES_EXPORT unsigned int CalculateBlockIndex(unsigned int row, unsigned int column) const;
157 
158  LIB_UTILITIES_EXPORT const InnerType* GetBlockPtr(unsigned int row, unsigned int column) const;
159 
160  LIB_UTILITIES_EXPORT boost::shared_ptr<const InnerType> GetBlock(unsigned int row, unsigned int column) const;
161 
162  LIB_UTILITIES_EXPORT boost::shared_ptr<InnerType>& GetBlock(unsigned int row, unsigned int column);
163 
164  LIB_UTILITIES_EXPORT void SetBlock(unsigned int row, unsigned int column, boost::shared_ptr<InnerType>& m);
165 
166  LIB_UTILITIES_EXPORT ConstGetValueType operator()(unsigned int row, unsigned int col) const;
167 
168  LIB_UTILITIES_EXPORT unsigned int GetStorageSize() const;
169 
170  LIB_UTILITIES_EXPORT unsigned int GetNumberOfBlockRows() const;
171 
172  LIB_UTILITIES_EXPORT unsigned int GetNumberOfBlockColumns() const;
173 
174  LIB_UTILITIES_EXPORT unsigned int GetNumberOfRowsInBlockRow(unsigned int blockRow) const;
175 
176  LIB_UTILITIES_EXPORT unsigned int GetNumberOfColumnsInBlockColumn(unsigned int blockCol) const;
177 
178  LIB_UTILITIES_EXPORT void GetBlockSizes(Array<OneD, unsigned int>& rowSizes,
179  Array<OneD, unsigned int>& colSizes) const;
180 
181  LIB_UTILITIES_EXPORT iterator begin();
182  LIB_UTILITIES_EXPORT iterator end();
183  LIB_UTILITIES_EXPORT const_iterator begin() const;
184  LIB_UTILITIES_EXPORT const_iterator end() const;
185 
186  public:
187  LIB_UTILITIES_EXPORT static ThisType CreateWrapper(const ThisType& rhs);
188 
189  LIB_UTILITIES_EXPORT static boost::shared_ptr<ThisType> CreateWrapper(const boost::shared_ptr<ThisType>& rhs);
190 
191  private:
192 
193  LIB_UTILITIES_EXPORT static unsigned int GetNumberOfElementsInBlock(unsigned int block, unsigned int totalBlocks, const Array<OneD, unsigned int>& sizes);
194 
195  LIB_UTILITIES_EXPORT void Initialize(const unsigned int* rowsPerBlock, const unsigned int* columnsPerBlock);
196 
197  LIB_UTILITIES_EXPORT virtual typename boost::call_traits<NumberType>::value_type v_GetValue(unsigned int row, unsigned int column) const;
198 
199  LIB_UTILITIES_EXPORT virtual unsigned int v_GetStorageSize() const;
200 
201  LIB_UTILITIES_EXPORT virtual void v_Transpose();
202 
204  boost::shared_ptr<InnerType> m_nullBlockPtr;
207  unsigned int m_storageSize;
208  unsigned int m_numberOfBlockRows;
210  static NumberType m_zeroElement;
211  };
212 
213  template<typename DataType, typename InnerMatrixType>
214  typename NekMatrix<NekMatrix<DataType, InnerMatrixType>, BlockMatrixTag>::NumberType
215  NekMatrix<NekMatrix<DataType, InnerMatrixType>, BlockMatrixTag>::m_zeroElement(0);
216 
217  template<typename InnerMatrixType>
220  {
222  result.Transpose();
223  return result;
224  }
225 }
226 
227 
228 #endif //NEKTAR_LIB_UTILITIES_LINEAR_ALGEBRA_BLOCK_MATRIX_HPP
STL namespace.
#define LIB_UTILITIES_EXPORT
NekMatrix< InnerMatrixType, BlockMatrixTag > Transpose(NekMatrix< InnerMatrixType, BlockMatrixTag > &rhs)
iterator_base(MatrixType &m, unsigned int curRow, unsigned int curCol)
Definition: BlockMatrix.hpp:75