So-Bogus
A c++ sparse block matrix library aimed at Second Order cone problems
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
CompoundMatrix.hpp
1 /*
2  * This file is part of bogus, a C++ sparse block matrix library.
3  *
4  * Copyright 2016 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 #ifndef BOGUS_COMPOUND_BLOCK_MATRIX_HPP
12 #define BOGUS_COMPOUND_BLOCK_MATRIX_HPP
13 
14 #include "IterableBlockObject.hpp"
15 
16 #include "Access.hpp"
17 
18 namespace bogus {
19 
21 
29 template< bool ColWise, typename MatrixT1, typename MatrixT2 >
30 class CompoundBlockMatrix : public IterableBlockObject<CompoundBlockMatrix< ColWise, MatrixT1, MatrixT2 > > {
31 public:
32  // side-by-side (ColWise = true)
34 
35  typedef typename Base::Index Index ;
36  typedef typename Base::Scalar Scalar ;
37  typedef typename Base::ConstTransposeReturnType ConstTransposeReturnType ;
38 
40 
41  // BlockObjectBase
42 
43  Index rows() const { return m_first.rows() ; }
44  Index cols() const { return m_first.cols()+m_second.cols() ; }
45 
46  Index blockRows( Index row ) const { return m_first.blockRows( row ) ; }
47  Index blockCols( Index col ) const {
48  const Index off = col - secondBegin() ;
49  return off < 0 ? m_first.blockCols( col ) : m_second.blockCols( off ) ;
50  }
51 
52  Index rowsOfBlocks() const { return m_first.rowsOfBlocks() ; }
53  Index colsOfBlocks() const { return m_first.colsOfBlocks() + m_second.colsOfBlocks() ; }
54 
55  const Index *rowOffsets( ) const { return m_first.rowOffsets() ; }
56  const Index *colOffsets( ) const { return data_pointer(m_offsets) ; }
57 
58  ConstTransposeReturnType transpose() const { return Transpose< CompoundBlockMatrix >( *this ) ; }
59 
60  template < bool DoTranspose, typename RhsT, typename ResT >
61  void multiply( const RhsT& rhs, ResT& res, Scalar alpha = 1, Scalar beta = 0 ) const ;
62 
63  // Iterable Block Object
64 
65  Index size() const { return m_first.size() + m_second.size() ; }
66 
67  template <typename Func>
68  void eachBlockOfRow( const Index row, Func func ) const {
69  m_first .derived().eachBlockOfRow( row, func ) ;
70  m_second.derived().eachBlockOfRow( row, func ) ;
71  }
72  template <typename Func>
73  void eachBlockOfCol( const Index col, Func func ) const {
74  const Index off = col - secondBegin() ;
75  if( off < 0 )
76  m_first .derived().eachBlockOfCol( col, func ) ;
77  else
78  m_second.derived().eachBlockOfCol( off, func ) ;
79  }
80 
81  template < bool DoTranspose, typename RhsT, typename ResT, typename PreOp >
82  void rowMultiplyPrecompose( const Index row, const RhsT& rhs, ResT& res, const PreOp &op ) const ;
83 
84  template < bool DoTranspose, typename RhsT, typename ResT, typename PostOp >
85  void colMultiplyPostcompose( const Index col, const RhsT& rhs, ResT& res, const PostOp &op ) const ;
86 
87 
88  // Accessors
89 
90  const IterableBlockObject< MatrixT1 >& first() const { return m_first; }
91  const IterableBlockObject< MatrixT2 >& second() const { return m_second; }
92  const Index* compoundOffsets() const { return m_compoundOffsets ; }
93  Index secondBegin() const { return m_first.colsOfBlocks() ; }
94 
95 private:
96  const IterableBlockObject< MatrixT1 >& m_first;
97  const IterableBlockObject< MatrixT2 >& m_second;
98  Index m_compoundOffsets[3] ;
99  std::vector< Index > m_offsets ;
100 
101 };
102 
103 template< bool ColWise, typename MatrixT1, typename MatrixT2 >
104 struct BlockMatrixTraits< CompoundBlockMatrix< ColWise, MatrixT1, MatrixT2 > >
105  : public BlockMatrixTraits< BlockObjectBase< CompoundBlockMatrix< ColWise, MatrixT1, MatrixT2 > > >
106 {
109 
110  typedef typename OrigTraits::Scalar Scalar;
111  typedef typename OrigTraits::Index Index;
112 
113  enum {
114  RowsPerBlock = SwapIf<
115  ColWise || ((int)OrigTraits::RowsPerBlock) == (int)OtherTraits::RowsPerBlock,
116  internal::DYNAMIC, OrigTraits::RowsPerBlock >::First,
117  ColsPerBlock = SwapIf<
118  !ColWise || ((int)OrigTraits::ColsPerBlock) == (int)OtherTraits::ColsPerBlock,
119  internal::DYNAMIC, OrigTraits::ColsPerBlock >::First
120  };
121 } ;
122 
123 template< typename MatrixT1, typename MatrixT2 >
124 class CompoundBlockMatrix<false,MatrixT1, MatrixT2>
125  : public IterableBlockObject<CompoundBlockMatrix< false, MatrixT1, MatrixT2 > > {
126 public:
127  // one-atop-the-other (ColWise = false)
129 
130  typedef typename Base::Index Index ;
131  typedef typename Base::Scalar Scalar ;
132  typedef typename Base::ConstTransposeReturnType ConstTransposeReturnType ;
133 
135 
136  // BlockObjectBase
137 
138  Index cols() const { return m_first.cols() ; }
139  Index rows() const { return m_first.rows()+m_second.rows() ; }
140 
141  Index blockCols( Index col ) const { return m_first.blockCols( col ) ; }
142  Index blockRows( Index row ) const {
143  const Index off = row - secondBegin() ;
144  return off < 0 ? m_first.blockRows( row ) : m_second.blockRows( off ) ;
145  }
146 
147  Index colsOfBlocks() const { return m_first.colsOfBlocks() ; }
148  Index rowsOfBlocks() const { return m_first.rowsOfBlocks() + m_second.rowsOfBlocks() ; }
149 
150  const Index *colOffsets( ) const { return m_first.colOffsets() ; }
151  const Index *rowOffsets( ) const { return data_pointer(m_offsets) ; }
152 
153  ConstTransposeReturnType transpose() const { return Transpose< CompoundBlockMatrix >( *this ) ; }
154 
155  template < bool DoTranspose, typename RhsT, typename ResT >
156  void multiply( const RhsT& rhs, ResT& res, Scalar alpha = 1, Scalar beta = 0 ) const ;
157 
158  // Iterable Block Object
159 
160  Index size() const { return m_first.size() + m_second.size() ; }
161 
162  template <typename Func>
163  void eachBlockOfCol( const Index col, Func func ) const {
164  m_first .derived().eachBlockOfRow( col, func ) ;
165  m_second.derived().eachBlockOfRow( col, func ) ;
166  }
167  template <typename Func>
168  void eachBlockOfRow( const Index row, Func func ) const {
169  const Index off = row - secondBegin() ;
170  if( off < 0 )
171  m_first .derived().eachBlockOfCol( row, func ) ;
172  else
173  m_second.derived().eachBlockOfCol( off, func ) ;
174  }
175 
176  template < bool DoTranspose, typename RhsT, typename ResT, typename PreOp >
177  void rowMultiplyPrecompose( const Index row, const RhsT& rhs, ResT& res, const PreOp &op ) const ;
178 
179  template < bool DoTranspose, typename RhsT, typename ResT, typename PostOp >
180  void colMultiplyPostcompose( const Index col, const RhsT& rhs, ResT& res, const PostOp &op ) const ;
181 
182 
183  // Accessors
184 
185  const IterableBlockObject< MatrixT1 >& first() const { return m_first; }
186  const IterableBlockObject< MatrixT2 >& second() const { return m_second; }
187  const Index* compoundOffsets() const { return m_compoundOffsets ; }
188  Index secondBegin() const { return m_first.rowsOfBlocks() ; }
189 
190 private:
191  const IterableBlockObject< MatrixT1 >& m_first;
192  const IterableBlockObject< MatrixT2 >& m_second;
193  Index m_compoundOffsets[3] ;
194  std::vector< Index > m_offsets ;
195 } ;
196 
197 } //bogus
198 
199 #endif
Index blockRows(Index row) const
Returns the number of rows of a given block row.
Definition: BlockObjectBase.hpp:51
Index rowsOfBlocks() const
Returns the number of block rows of the matrix.
Definition: BlockObjectBase.hpp:56
Definition: Traits.hpp:19
Index cols() const
Returns the total number of columns of the matrix ( expanding blocks )
Definition: BlockObjectBase.hpp:48
const Derived & derived() const
Returns a const reference to the implementation.
Definition: BlockObjectBase.hpp:25
Index colsOfBlocks() const
Returns the number of block columns of the matrix.
Definition: BlockObjectBase.hpp:58
const Index * colOffsets() const
Returns an array containing the first index of each column.
Definition: BlockObjectBase.hpp:63
Base class for matrix-like objects that define a block structure, but not a block type...
Definition: IterableBlockObject.hpp:22
Base class for Transpose views of a BlockObjectBase.
Definition: Expressions.hpp:22
const Index * rowOffsets() const
Returns an array containing the first index of each row.
Definition: BlockObjectBase.hpp:61
Index rows() const
Returns the total number of rows of the matrix ( expanding blocks )
Definition: BlockObjectBase.hpp:46
Definition: CppTools.hpp:49
Index blockCols(Index col) const
Returns the number of columns of a given block columns.
Definition: BlockObjectBase.hpp:53
Index size() const
Returns the total number of blocks of the matrix.
Definition: IterableBlockObject.hpp:33
A matrix made by concatenating two other matrices of possibly different types.
Definition: CompoundMatrix.hpp:30