So-Bogus
A c++ sparse block matrix library aimed at Second Order cone problems
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
Expressions.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_BLOCK_EXPRESSIONS_HPP
13 #define BOGUS_BLOCK_EXPRESSIONS_HPP
14 
15 #include "BlockObjectBase.hpp"
16 
17 namespace bogus
18 {
19 
21 template <typename MatrixT>
22 struct Transpose : public BlockObjectBase< Transpose< MatrixT > >
23 {
26  typedef typename Traits::PlainObjectType PlainObjectType ;
27  typedef typename Traits::Index Index ;
28  typedef typename Base::Scalar Scalar ;
29 
30  const PlainObjectType &matrix ;
31 
32  Transpose( const PlainObjectType &m ) : matrix( m.derived() ) {}
33 
34  typename Base::ConstTransposeReturnType transpose() const { return matrix ; }
35 
36  template < bool DoTranspose, typename RhsT, typename ResT >
37  void multiply( const RhsT& rhs, ResT& res, Scalar alpha = 1, Scalar beta = 0 ) const
38  {
39  matrix.template multiply< !DoTranspose >( rhs, res, alpha, beta ) ;
40  }
41 
42  Index rows() const { return matrix.cols() ; }
43  Index cols() const { return matrix.rows() ; }
44 
45  Index rowsOfBlocks() const { return matrix.colsOfBlocks() ; }
46  Index colsOfBlocks() const { return matrix.rowsOfBlocks() ; }
47  Index blockRows( Index row ) const { return matrix.blockCols( row ) ; }
48  Index blockCols( Index col ) const { return matrix.blockRows( col ) ; }
49  const Index *rowOffsets( ) const { return matrix.colOffsets( ) ; }
50  const Index *colOffsets( ) const { return matrix.rowOffsets( ) ; }
51 } ;
52 
53 
54 template <typename MatrixT>
55 struct BlockMatrixTraits< Transpose< MatrixT > >
56 {
58  typedef typename OrigTraits::Index Index ;
59  typedef typename OrigTraits::Scalar Scalar ;
60 
61  enum {
62  is_transposed = 1,
63  is_temporary = 1,
64  is_symmetric = OrigTraits::is_symmetric
65  } ;
66  enum {
67  RowsPerBlock = OrigTraits::ColsPerBlock,
68  ColsPerBlock = OrigTraits::RowsPerBlock
69  };
70 
71  typedef typename OrigTraits::PlainObjectType PlainObjectType ;
72  typedef const PlainObjectType& ConstTransposeReturnType ;
73  typedef PlainObjectType TransposeObjectType ;
74 
75 } ;
76 
77 template < typename ObjectT, bool IsTemporary >
79 {
80  typedef const ObjectT& ConstValue ;
81 } ;
82 template < typename ObjectT >
83 struct BlockStorage< ObjectT, true >
84 {
85  typedef const ObjectT ConstValue ;
86 } ;
87 
88 
89 template < typename ObjectT >
91 {
92  typedef ObjectT ObjectType ;
93  typedef typename ObjectT::PlainObjectType PlainObjectType ;
94 
96  enum { do_transpose = Traits::is_transposed } ;
97  typedef typename Traits::Scalar Scalar ;
98 
99  typename BlockStorage< ObjectT, Traits::is_temporary >::ConstValue object ;
100  Scalar scaling ;
101 
102  BlockOperand( const ObjectT & o, Scalar s = 1 )
103  : object(o), scaling(s)
104  {}
105 } ;
106 
107 template < template < typename, typename > class BlockOp, typename LhsMatrixT, typename RhsMatrixT>
108 struct BinaryBlockOp : public BlockObjectBase< BlockOp< LhsMatrixT, RhsMatrixT > >
109 {
110 
112  typedef typename Base::PlainObjectType PlainObjectType ;
113 
116 
117  typedef typename Lhs::PlainObjectType PlainLhsMatrixType ;
118  typedef typename Rhs::PlainObjectType PlainRhsMatrixType ;
119 
120  const Lhs lhs ;
121  const Rhs rhs ;
122  enum { transposeLhs = Lhs::do_transpose,
123  transposeRhs = Rhs::do_transpose };
124 
125  BinaryBlockOp( const LhsMatrixT& l,const RhsMatrixT& r,
126  typename Lhs::Scalar lscaling = 1, typename Lhs::Scalar rscaling = 1 )
127  : lhs( l, lscaling ), rhs ( r, rscaling )
128  {}
129 
130 } ;
131 
132 
133 template <typename LhsMatrixT, typename RhsMatrixT>
134 struct Product : public BinaryBlockOp< Product, LhsMatrixT, RhsMatrixT >
135 {
137  typedef typename Base::Scalar Scalar ;
138  typedef typename Base::Index Index ;
139 
140  Product( const LhsMatrixT& l, const RhsMatrixT &r,
141  typename Base::Lhs::Scalar lscaling = 1, typename Base::Lhs::Scalar rscaling = 1 )
142  : Base( l, r, lscaling, rscaling )
143  {}
144 
145  typename Base::ConstTransposeReturnType transpose() const
146  {
147  return typename Base::ConstTransposeReturnType (
148  Base::rhs.object.transpose(), Base::lhs.object.transpose(),
149  Base::rhs.scaling, Base::lhs.scaling ) ;
150  }
151 
152  template < bool DoTranspose, typename RhsT, typename ResT >
153  void multiply( const RhsT& rhs, ResT& res, Scalar alpha = 1, Scalar beta = 0 ) const ;
154 
155  Index rows() const { return Base::lhs.object.rows() ; }
156  Index cols() const { return Base::rhs.object.cols() ; }
157 
158  Index rowsOfBlocks() const { return Base::lhs.object.rowsOfBlocks() ; }
159  Index colsOfBlocks() const { return Base::rhs.object.colsOfBlocks() ; }
160  Index blockRows( Index row ) const { return Base::lhs.object.blockRows( row ) ; }
161  Index blockCols( Index col ) const { return Base::rhs.object.blockCols( col ) ; }
162  const Index *rowOffsets( ) const { return Base::lhs.object.rowOffsets( ) ; }
163  const Index *colOffsets( ) const { return Base::rhs.object.colOffsets( ) ; }
164 } ;
165 
166 template <typename LhsMatrixT, typename RhsMatrixT>
167 struct BlockMatrixTraits< Product< LhsMatrixT, RhsMatrixT > >
168 {
171 
172  typedef typename LhsTraits::Index Index ;
173  typedef typename LhsTraits::Scalar Scalar ;
174 
175  enum { is_transposed = 0,
176  is_temporary = 1,
177  is_symmetric = 0 } ;
178 
180 
181  typedef typename LhsTraits::PlainObjectType::BlockType LhsBlockType ;
182  typedef typename RhsTraits::PlainObjectType::BlockType RhsBlockType ;
183 
184  typedef typename BlockBlockProductTraits< LhsBlockType, RhsBlockType,
185  LhsTraits::is_transposed, RhsTraits::is_transposed >::ReturnType ResBlockType ;
186 
187  typedef typename LhsTraits::PlainObjectType
188  ::template MutableImpl< ResBlockType, false >::Type PlainObjectType ;
189 
190  enum {
191  RowsPerBlock = LhsTraits::RowsPerBlock,
192  ColsPerBlock = RhsTraits::ColsPerBlock
193  };
194 
199 } ;
200 
201 template <typename LhsMatrixT, typename RhsMatrixT>
202 struct Addition : public BinaryBlockOp< Addition, LhsMatrixT, RhsMatrixT >
203 {
205  typedef typename Base::Scalar Scalar ;
206  typedef typename Base::Index Index ;
207 
208  Addition( const LhsMatrixT& l, const RhsMatrixT &r,
209  typename Base::Lhs::Scalar lscaling = 1, typename Base::Lhs::Scalar rscaling = 1 )
210  : Base( l, r, lscaling, rscaling )
211  {}
212 
213  typename Base::ConstTransposeReturnType transpose() const
214  {
215  return typename Base::ConstTransposeReturnType (
216  Base::lhs.object.transpose(), Base::rhs.object.transpose(),
217  Base::lhs.scaling, Base::rhs.scaling ) ;
218  }
219 
220  template < bool DoTranspose, typename RhsT, typename ResT >
221  void multiply( const RhsT& rhs, ResT& res, Scalar alpha = 1, Scalar beta = 0 ) const
222  {
223  Base::lhs.object.template multiply< DoTranspose >( rhs, res, alpha*Base::lhs.scaling, beta ) ;
224  Base::rhs.object.template multiply< DoTranspose >( rhs, res, alpha*Base::rhs.scaling, 1 ) ;
225  }
226 
227  Index rows() const { return Base::lhs.object.rows() ; }
228  Index cols() const { return Base::lhs.object.cols() ; }
229 
230  Index rowsOfBlocks() const { return Base::lhs.object.rowsOfBlocks() ; }
231  Index colsOfBlocks() const { return Base::lhs.object.colsOfBlocks() ; }
232  Index blockRows( Index row ) const { return Base::lhs.object.blockRows( row ) ; }
233  Index blockCols( Index col ) const { return Base::lhs.object.blockCols( col ) ; }
234  const Index *rowOffsets( ) const { return Base::lhs.object.rowOffsets( ) ; }
235  const Index *colOffsets( ) const { return Base::lhs.object.colOffsets( ) ; }
236 } ;
237 
238 template <typename LhsMatrixT, typename RhsMatrixT>
239 struct BlockMatrixTraits< Addition< LhsMatrixT, RhsMatrixT > >
240 {
241 
243  typedef typename OrigTraits::Index Index ;
244  typedef typename OrigTraits::Scalar Scalar ;
245 
246  typedef typename OrigTraits::PlainObjectType::BlockType ResBlockType ;
247 
248  typedef typename OrigTraits::PlainObjectType
249  ::template MutableImpl< ResBlockType, false >::Type PlainObjectType ;
250 
251  enum { is_transposed = 0,
252  is_temporary = 1,
255  } ;
256  enum {
257  RowsPerBlock = OrigTraits::RowsPerBlock,
258  ColsPerBlock = OrigTraits::ColsPerBlock
259  };
260 
264  typedef ConstTransposeReturnType TransposeObjectType ;
265 } ;
266 
267 template <typename MatrixT>
268 struct Scaling : public BlockObjectBase< Scaling< MatrixT > >
269 {
271  typedef typename Operand::PlainObjectType PlainOperandMatrixType ;
272  Operand operand ;
273 
274  enum { transposeOperand = Operand::do_transpose };
275 
277 
278  typedef typename Base::Scalar Scalar ;
279  typedef typename Base::Index Index ;
280 
281  Scaling( const MatrixT &object, const typename MatrixT::Scalar scaling )
282  : operand( object, scaling )
283  {}
284 
285  typename Base::ConstTransposeReturnType transpose() const
286  {
287  return typename Base::ConstTransposeReturnType (
288  operand.object.transpose(), operand.scaling ) ;
289  }
290 
291  template < bool DoTranspose, typename RhsT, typename ResT >
292  void multiply( const RhsT& rhs, ResT& res, Scalar alpha = 1, Scalar beta = 0 ) const
293  {
294  operand.object.template multiply< DoTranspose >( rhs, res, alpha*operand.scaling, beta ) ;
295  }
296 
297  typename Base::Index rows() const { return operand.object.rows() ; }
298  typename Base::Index cols() const { return operand.object.cols() ; }
299 
300  Index rowsOfBlocks() const { return operand.object.rowsOfBlocks() ; }
301  Index colsOfBlocks() const { return operand.object.colsOfBlocks() ; }
302  Index blockRows( Index row ) const { return operand.object.blockRows( row ) ; }
303  Index blockCols( Index col ) const { return operand.object.blockCols( col ) ; }
304  const Index *rowOffsets( ) const { return operand.object.rowOffsets( ) ; }
305  const Index *colOffsets( ) const { return operand.object.colOffsets( ) ; }
306 } ;
307 
308 template <typename MatrixT>
309 struct BlockMatrixTraits< Scaling< MatrixT > >
310 {
311 
313  typedef typename OrigTraits::Index Index ;
314  typedef typename OrigTraits::Scalar Scalar ;
315 
316  enum { is_symmetric = OrigTraits::is_symmetric,
317  is_transposed = 0,
318  is_temporary = 1 } ;
319  enum {
320  RowsPerBlock = OrigTraits::RowsPerBlock,
321  ColsPerBlock = OrigTraits::ColsPerBlock
322  };
323 
324  typedef typename OrigTraits::PlainObjectType PlainObjectType ;
325 
329 } ;
330 
331 template < typename ObjectT >
332 struct BlockOperand< Scaling< ObjectT > > : public BlockOperand< ObjectT >
333 {
334  typedef BlockOperand< ObjectT > Base ;
335 
336  BlockOperand( const Scaling< ObjectT > & o, typename Base::Scalar s = 1 )
337  : Base(o.operand.object, s*o.operand.scaling)
338  {}
339  BlockOperand( const typename Base::ObjectType & o, typename Base::Scalar s = 1 )
340  : Base(o, s)
341  {}
342 } ;
343 
344 }
345 
346 
347 #endif // EXPRESSIONS_HPP
Definition: Traits.hpp:19
Base class for Transpose views of a BlockObjectBase.
Definition: Expressions.hpp:22
Definition: Expressions.hpp:202
Definition: Expressions.hpp:268
Defines the return type of the product of two blocks potentially transposed.
Definition: Traits.hpp:64
Definition: Expressions.hpp:134
Base class for anything block.
Definition: BlockObjectBase.hpp:22
Definition: Expressions.hpp:108
Definition: Expressions.hpp:78
Definition: Expressions.hpp:90