So-Bogus
A c++ sparse block matrix library aimed at Second Order cone problems
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
DynamicExpressions.hpp
1 /*
2  * This file is part of bogus, a C++ sparse block matrix library.
3  *
4  * Copyright 2015 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_BLOCK_DYNAMIC_EXPRESSIONS_HPP
12 #define BOGUS_BLOCK_DYNAMIC_EXPRESSIONS_HPP
13 
14 #include "Expressions.hpp"
15 #include "DynamicExpressions.hpp"
16 
17 #include <list>
18 
19 namespace bogus
20 {
21 
23 template <typename Expression>
24 struct NarySum : public BlockObjectBase< NarySum< Expression > > {
25 
28 
29  typedef typename Traits::Index Index ;
30  typedef typename Base::Scalar Scalar ;
31 
32  // Using list as we dont assume existence of c++11 vector::emplace_back
33  typedef std::list< Scaling< Expression > > Sum ;
34  Sum members ;
35 
36  NarySum( const Index rows, const Index cols )
37  : m_rows( rows ), m_cols( cols )
38  {
39  }
40  NarySum ( const Expression &expr )
41  : m_rows( expr.rows() ), m_cols( expr.cols() )
42  {
43  (*this) += expr ;
44  }
45  NarySum ( const Scaling< Expression > &expr )
46  : m_rows( expr.rows() ), m_cols( expr.cols() )
47  {
48  (*this) += expr ;
49  }
50 
51  NarySum &operator+=( const Scaling< Expression > &expr )
52  {
53  assert( expr.rows() == m_rows && expr.cols() == m_cols ) ;
54  members.push_back( expr ) ;
55  return *this ;
56  }
57 
58  NarySum &operator-=( const Scaling< Expression > &expr )
59  {
60  return (*this) += Scaling< Expression >( expr.operand.object, -expr.operand.scaling ) ;
61  }
62 
63  NarySum &operator+=( const Expression &expr )
64  {
65  return (*this) += Scaling< Expression >( expr, 1 ) ;
66  }
67 
68  NarySum &operator-=( const Expression &expr )
69  {
70  return (*this) += Scaling< Expression >( expr, -1 ) ;
71  }
72 
73  NarySum &operator+=( const NarySum< Expression > &other )
74  {
75  assert( other.rows() == m_rows && other.cols() == m_cols ) ;
76  members.insert( members.begin(), other.members.begin(), other.members.end() ) ;
77  return *this ;
78  }
79 
80  NarySum &operator-=( const NarySum< Expression > &other )
81  {
82  assert( other.rows() == m_rows && other.cols() == m_cols ) ;
83  for( typename Sum::const_iterator it = other.members.begin() ; it != other.members.end() ; ++ it )
84  {
85  (*this) -= *it ;
86  }
87  return *this ;
88  }
89 
90  typename Base::ConstTransposeReturnType transpose() const {
91  typename Base::ConstTransposeReturnType transposed_sum( m_cols, m_rows ) ;
92  for( typename Sum::const_iterator it = members.begin() ; it != members.end() ; ++ it )
93  {
94  transposed_sum += it->transpose() ;
95  }
96 
97  return transposed_sum ;
98  }
99 
100  template < bool DoTranspose, typename RhsT, typename ResT >
101  void multiply( const RhsT& rhs, ResT& res, Scalar alpha = 1, Scalar beta = 0 ) const ;
102 
103  bool empty() const { return members.begin() == members.end() ; }
104 
105  Index rows() const { return m_rows ; }
106  Index cols() const { return m_cols ; }
107 
108  Index rowsOfBlocks() const { assert(!empty()) ; return members.front().colsOfBlocks() ; }
109  Index colsOfBlocks() const { assert(!empty()) ; return members.front().rowsOfBlocks() ; }
110  Index blockRows( Index row ) const { assert(!empty()) ; return members.front().blockCols( row ) ; }
111  Index blockCols( Index col ) const { assert(!empty()) ; return members.front().blockRows( col ) ; }
112  const Index *rowOffsets( ) const { assert(!empty()) ; return members.front().colOffsets( ) ; }
113  const Index *colOffsets( ) const { assert(!empty()) ; return members.front().rowOffsets( ) ; }
114 
115 private:
116  // Used for consistency checks only
117  const Index m_rows ;
118  const Index m_cols ;
119 };
120 
121 template <typename Expression>
122 struct BlockMatrixTraits< NarySum< Expression > >
123  : public BlockMatrixTraits< Addition< Expression, Expression > >
124 {
125  enum {
126  is_temporary = 0
127  };
128 
129  typedef typename Expression::PlainObjectType PlainObjectType ;
132 } ;
133 
134 
135 } //bogus
136 
137 
138 #endif
Definition: Traits.hpp:19
Definition: Expressions.hpp:268
Base class for anything block.
Definition: BlockObjectBase.hpp:22
Sum of n similar expressions.
Definition: DynamicExpressions.hpp:24
Definition: DynamicExpressions.hpp:122