So-Bogus
A c++ sparse block matrix library aimed at Second Order cone problems
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
BlockMatrixBase.hpp
1 /*
2  * This file is part of bogus, a C++ sparse block matrix library.
3  *
4  * Copyright 2013 Gilles Daviet <gdaviet@gmail.com>
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 */
10 
11 
12 #ifndef BOGUS_BLOCKMATRIX_HPP
13 #define BOGUS_BLOCKMATRIX_HPP
14 
15 #include "IterableBlockObject.hpp"
16 
17 namespace bogus
18 {
19 
21 template < typename Derived >
22 class BlockMatrixBase : public IterableBlockObject< Derived >
23 {
24 public:
26  typedef typename Traits::Index Index ;
27  typedef typename Traits::Scalar Scalar ;
28 
29  // Supplemental Traits interface for BlockMatrixBase
30  typedef typename Traits::BlockType BlockType ;
31  typedef typename Traits::BlockPtr BlockPtr ;
32  typedef typename Traits::BlocksArrayType BlocksArrayType ;
33 
35  using Base::derived ;
36 
38  static const BlockPtr InvalidBlockPtr ;
39 
40  BlockMatrixBase() : m_rows(0), m_cols(0)
41  {}
42 
43  virtual ~BlockMatrixBase()
44  {}
45 
47 
52  template < typename RhsT, typename ResT >
53  void splitRowMultiply( const Index row, const RhsT& rhs, ResT& res ) const
54  {
55  derived().splitRowMultiply( row, rhs, res ) ;
56  }
57 
59  BlockPtr blockPtr( Index row, Index col ) const
60  {
61  return derived().blockPtr( row, col ) ;
62  }
64  BlockPtr diagonalBlockPtr( Index row ) const
65  {
66  return derived().diagonalBlockPtr( row ) ;
67  }
68 
70  const BlockType& block( BlockPtr ptr ) const
71  {
72  return derived().block(ptr) ;
73  }
75  BlockType& block( BlockPtr ptr )
76  {
77  return derived().block(ptr) ;
78  }
79 
81  Index rows() const { return m_rows ; }
83  Index cols() const { return m_cols ; }
84 
86  Index size() const ;
87 
89  const typename Traits::BlocksArrayType& blocks() const { return m_blocks ; }
91  const BlockType* data() const { return data_pointer(m_blocks) ; }
93  BlockType* data() { return data_pointer(m_blocks) ; }
94 
96  BlockType& diagonal( const Index row )
97  { return block( diagonalBlockPtr( row ) ) ; }
99  const BlockType& diagonal( const Index row ) const
100  { return block( diagonalBlockPtr( row ) ) ; }
101 
103  BlockType& block( Index row, Index col )
104  { return block( blockPtr( row, col ) ) ; }
106  const BlockType& block( Index row, Index col ) const
107  { return block( blockPtr( row, col ) ) ; }
108 
109 
111  template <typename Func>
112  void eachBlockOfRow( const Index row, Func func ) const
113  { derived().template eachBlockOf<false, Func>(row, func) ; }
114 
116  template <typename Func>
117  void eachBlockOfCol( const Index col, Func func ) const
118  { derived().template eachBlockOf<true ,Func>(col, func) ; }
119 
122  {
123  RowsPerBlock = Traits::RowsPerBlock,
124  ColsPerBlock = Traits::ColsPerBlock,
125  has_row_major_blocks = BlockTraits< BlockType >::is_row_major,
126  has_square_or_dynamic_blocks = ColsPerBlock == RowsPerBlock,
127  has_fixed_rows_blocks = ((int) RowsPerBlock != internal::DYNAMIC ),
128  has_fixed_cols_blocks = ((int) ColsPerBlock != internal::DYNAMIC ),
129  has_fixed_size_blocks = has_fixed_cols_blocks && has_fixed_rows_blocks
130  } ;
131 
132 protected:
133  Index m_rows ;
134  Index m_cols ;
135 
136  BlocksArrayType m_blocks ;
137 } ;
138 
139 }
140 
141 #endif // BLOCKMATRIX_HPP
const Traits::BlocksArrayType & blocks() const
Access to blocks data.
Definition: BlockMatrixBase.hpp:89
Base class for dense and sparse block matrices, thought dense don&#39;t exist yet.
Definition: BlockMatrixBase.hpp:22
Definition: Traits.hpp:19
const Derived & derived() const
Returns a const reference to the implementation.
Definition: BlockObjectBase.hpp:25
BlockPtr diagonalBlockPtr(Index row) const
Return a BlockPtr to the block a (row, row) or InvalidBlockPtr if it does not exist.
Definition: BlockMatrixBase.hpp:64
Index cols() const
Returns the total number of columns of the matrix ( expanding blocks )
Definition: BlockMatrixBase.hpp:83
void splitRowMultiply(const Index row, const RhsT &rhs, ResT &res) const
Multiplies a given block-row of the matrix with rhs, omitting the diagonal block. ...
Definition: BlockMatrixBase.hpp:53
void eachBlockOfCol(const Index col, Func func) const
Iterates over each block of a given col. Calls func( row, block )
Definition: BlockMatrixBase.hpp:117
BlockType & block(BlockPtr ptr)
Returns a reference to a block using the result from blockPtr() or diagonalBlockPtr() ...
Definition: BlockMatrixBase.hpp:75
Base class for matrix-like objects that define a block structure, but not a block type...
Definition: IterableBlockObject.hpp:22
const BlockType & block(BlockPtr ptr) const
Returns a reference to a block using the result from blockPtr() or diagonalBlockPtr() ...
Definition: BlockMatrixBase.hpp:70
BlockPtr blockPtr(Index row, Index col) const
Return a BlockPtr to the block a (row, col) or InvalidBlockPtr if it does not exist.
Definition: BlockMatrixBase.hpp:59
const BlockType & block(Index row, Index col) const
Definition: BlockMatrixBase.hpp:106
BlockType & diagonal(const Index row)
Definition: BlockMatrixBase.hpp:96
static const BlockPtr InvalidBlockPtr
Return value of blockPtr( Index, Index ) for non-existing block.
Definition: BlockMatrixBase.hpp:38
Index rows() const
Returns the total number of rows of the matrix ( expanding blocks )
Definition: BlockMatrixBase.hpp:81
Definition: Traits.hpp:29
const BlockType & diagonal(const Index row) const
Definition: BlockMatrixBase.hpp:99
const BlockType * data() const
Access to blocks data as a raw pointer.
Definition: BlockMatrixBase.hpp:91
void eachBlockOfRow(const Index row, Func func) const
Iterates over each block of a given row. Calls func( col, block )
Definition: BlockMatrixBase.hpp:112
BlockType * data()
Access to blocks data as a raw pointer.
Definition: BlockMatrixBase.hpp:93
BlockType & block(Index row, Index col)
Definition: BlockMatrixBase.hpp:103
Index size() const
Returns the total number of blocks of the matrix.
CompileTimeProperties
Compile-time block properties.
Definition: BlockMatrixBase.hpp:121