So-Bogus
A c++ sparse block matrix library aimed at Second Order cone problems
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
ProductGaussSeidelUtils.hpp
1 
2 /*
3  * This file is part of bogus, a C++ sparse block matrix library.
4  *
5  * Copyright 2016 Gilles Daviet <gdaviet@gmail.com>
6  *
7  * This Source Code Form is subject to the terms of the Mozilla Public
8  * License, v. 2.0. If a copy of the MPL was not distributed with this
9  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11 
12 #ifndef BOGUS_PRODUCT_GAUSS_SEIDEL_UTILS_HPP
13 #define BOGUS_PRODUCT_GAUSS_SEIDEL_UTILS_HPP
14 
15 #include "../Utils/CppTools.hpp"
16 
17 #include <vector>
18 
19 namespace bogus {
20 
21 namespace block_solvers_impl {
22 
24 
31 template < typename Type, bool IsScalar >
33 
34  typedef Type Scalar;
35  ConstantArray<Scalar> array ;
36  DiagonalMatrixWrapper( const Scalar& s = 1)
37  : array(s) { }
38 
39  bool valid() const { return true; }
40  Scalar get() const {
41  return array[0] ;
42  }
43  const ConstantArray<Scalar>& asArray() const {
44  return array ;
45  }
46 } ;
47 template< typename Type >
48 struct DiagonalMatrixWrapper < Type, false >
49 {
50  typedef typename Type::Index Index ;
51  typedef typename Type::BlockPtr BlockPtr ;
52 
54  : m_matrixPtr( BOGUS_NULL_PTR(const Type) )
55  { }
56  DiagonalMatrixWrapper( const Type& diag)
57  : m_matrixPtr(&diag)
58  { computeBlockIndices() ; }
59 
60  bool valid() const { return m_matrixPtr; }
61  const Type& get() const {
62  return *m_matrixPtr ;
63  }
64  const DiagonalMatrixWrapper& asArray() const {
65  return *this ;
66  }
67 
68  void computeBlockIndices() ;
69 
70  inline bool has_element( const Index i ) const
71  { return m_blockIndices[i] != Type::InvalidBlockPtr ; }
72 
73  // \warning has_block(i) must be true
74  inline const typename Type::BlockType& operator[]( const Index i ) const
75  { return m_matrixPtr->block(m_blockIndices[i]) ; }
76 
77 private:
78  const Type* m_matrixPtr ;
79  std::vector< BlockPtr > m_blockIndices ;
80 } ;
81 
83 template <typename MType, typename DType, bool Precompute >
84 struct DMtStorage {
85 
86  DMtStorage()
87  : m_M( BOGUS_NULL_PTR( const MType) ),
88  m_D( BOGUS_NULL_PTR( const DType) )
89  {}
90 
91  void compute( const MType& M, const DType& D ) {
92  m_M = &M ;
93  m_D = &D ;
94  }
95 
96  template< typename Rhs, typename Intermediate, typename Res >
97  void multiply( const Rhs& rhs, Intermediate &itm, Res& res ) const ;
98 
99  template< typename Rhs, typename Res >
100  void colMultiply( typename MType::Index col, const Rhs& rhs, Res & res ) const ;
101  template< typename Rhs, typename Res >
102  void rowMultiply( typename MType::Index row, const Rhs& rhs, Res & res ) const ;
103 
104  const MType* m_M ;
105  const DType* m_D ;
106 };
107 
108 template <typename MType, typename DType >
109 struct DMtStorage< MType, DType, true >
110 {
111  DMtStorage()
112  : m_M( BOGUS_NULL_PTR( const MType) )
113  {}
114 
115  void compute( const MType& M, const DType& D );
116 
117  template< typename Rhs, typename Intermediate, typename Res >
118  void multiply( const Rhs& rhs, Intermediate &itm, Res& res ) const ;
119 
120  template< typename Rhs, typename Res >
121  void colMultiply( typename MType::Index col, const Rhs& rhs, Res & res ) const ;
122  template< typename Rhs, typename Res >
123  void rowMultiply( typename MType::Index row, const Rhs& rhs, Res & res ) const ;
124 
125 private:
126  const MType* m_M ;
127  typedef typename MType::template MutableImpl< typename MType::TransposeBlockType, false, true >::Type DMtType ;
128  DMtType m_DMt ;
129 };
130 
131 } //block_solvers_impl
132 
133 } //bogus
134 
135 #endif
Utility struct to precompute or reference the (D M&#39;) part of the product.
Definition: ProductGaussSeidelUtils.hpp:84
Wrapper for the block-diagonal matrix of the ProductGaussSeidel solver.
Definition: ProductGaussSeidelUtils.hpp:32
Definition: CppTools.hpp:221