Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
SparseMatrix.h
1 /*
2 Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7 
8 Redistributions of source code must retain the above copyright notice, this list of
9 conditions and the following disclaimer. Redistributions in binary form must reproduce
10 the above copyright notice, this list of conditions and the following disclaimer
11 in the documentation and/or other materials provided with the distribution.
12 
13 Neither the name of the Johns Hopkins University nor the names of its contributors
14 may be used to endorse or promote products derived from this software without specific
15 prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGE.
27 */
28 
29 #ifndef __SPARSEMATRIX_HPP
30 #define __SPARSEMATRIX_HPP
31 
32 #include "Vector.h"
33 #include "Array.h"
34 
35 template <class T>
37 {
38  MatrixEntry( void ) { N =-1; Value = 0; }
39  MatrixEntry( int i ) { N = i; Value = 0; }
40  MatrixEntry( int i , T v ) { N = i; Value = v; }
41  int N;
42  T Value;
43 };
44 
45 template<class T> class SparseMatrix
46 {
47 private:
48  bool _contiguous;
49  int _maxEntriesPerRow;
50 public:
51  int rows;
52  Pointer( int ) rowSizes;
53  Pointer( Pointer( MatrixEntry< T > ) ) m_ppElements;
54  Pointer( MatrixEntry< T > ) operator[] ( int idx ) { return m_ppElements[idx]; }
55  ConstPointer( MatrixEntry< T > ) operator[] ( int idx ) const { return m_ppElements[idx]; }
56 
57  SparseMatrix( void );
58  SparseMatrix( int rows );
59  SparseMatrix( int rows , int maxEntriesPerRow );
60  void Resize( int rows );
61  void Resize( int rows , int maxEntriesPerRow );
62  void SetRowSize( int row , int count );
63  int Entries( void ) const;
64 
65  SparseMatrix( const SparseMatrix& M );
66  ~SparseMatrix();
67 
68  void SetZero();
69  void SetIdentity();
70 
71  SparseMatrix<T>& operator = (const SparseMatrix<T>& M);
72 
73  SparseMatrix<T> operator * (const T& V) const;
74  SparseMatrix<T>& operator *= (const T& V);
75 
76 
77  SparseMatrix<T> operator * (const SparseMatrix<T>& M) const;
78  SparseMatrix<T> Multiply( const SparseMatrix<T>& M ) const;
79  SparseMatrix<T> MultiplyTranspose( const SparseMatrix<T>& Mt ) const;
80 
81  template<class T2>
82  PoissonVector<T2> operator * (const PoissonVector<T2>& V) const;
83  template<class T2>
84  PoissonVector<T2> Multiply( const PoissonVector<T2>& V ) const;
85  template<class T2>
86  void Multiply( const PoissonVector<T2>& In , PoissonVector<T2>& Out , int threads=1 ) const;
87 
88 
89  SparseMatrix<T> Transpose() const;
90 
91  static int Solve (const SparseMatrix<T>& M,const PoissonVector<T>& b, int iters,PoissonVector<T>& solution,const T eps=1e-8);
92 
93  template<class T2>
94  static int SolveSymmetric( const SparseMatrix<T>& M , const PoissonVector<T2>& b , int iters , PoissonVector<T2>& solution , const T2 eps=1e-8 , int reset=1 , int threads=1 );
95 
96  bool write( FILE* fp ) const;
97  bool write( const char* fileName ) const;
98  bool read( FILE* fp );
99  bool read( const char* fileName );
100 };
101 
102 
103 template< class T2 >
105 {
106 private:
107  int _dim;
108 public:
109  std::vector< T2* > out;
110  MapReduceVector( void ) { _dim = 0; }
111  ~MapReduceVector( void )
112  {
113  if( _dim ) for( int t=0 ; t<int(out.size()) ; t++ ) delete[] out[t];
114  out.resize( 0 );
115  }
116  T2* operator[]( int t ) { return out[t]; }
117  const T2* operator[]( int t ) const { return out[t]; }
118  int threads( void ) const { return int(out.size()) ; }
119  void resize( size_t threads , int dim )
120  {
121  if( threads!=out.size() || _dim<dim )
122  {
123  for( int t=0 ; t<int(out.size()) ; t++ ) delete[] out[t];
124  out.resize( threads );
125  for( int t=0 ; t<int(out.size()) ; t++ ) out[t] = new T2[dim];
126  _dim = dim;
127  }
128  }
129 
130 };
131 
132 template< class T >
134 {
135 public:
136 
137  template< class T2 >
138  PoissonVector< T2 > operator * ( const PoissonVector<T2>& V ) const;
139 
140  template< class T2 >
141  PoissonVector< T2 > Multiply( const PoissonVector<T2>& V ) const;
142 
143  template< class T2 >
144  void Multiply( const PoissonVector<T2>& In, PoissonVector<T2>& Out , bool addDCTerm=false ) const;
145 
146  template< class T2 >
147  void Multiply( const PoissonVector<T2>& In, PoissonVector<T2>& Out , MapReduceVector< T2 >& OutScratch , bool addDCTerm=false ) const;
148 
149  template< class T2 >
150  void Multiply( const PoissonVector<T2>& In, PoissonVector<T2>& Out , std::vector< T2* >& OutScratch , const std::vector< int >& bounds ) const;
151 
152  template< class T2 >
153  static int Solve( const SparseSymmetricMatrix<T>& M , const PoissonVector<T2>& b , int iters , PoissonVector<T2>& solution , T2 eps=1e-8 , int reset=1 , int threads=0 , bool addDCTerm=false , bool solveNormal=false );
154 
155  template< class T2 >
156  static int Solve( const SparseSymmetricMatrix<T>& M , const PoissonVector<T2>& b , int iters , PoissonVector<T2>& solution , MapReduceVector<T2>& scratch , T2 eps=1e-8 , int reset=1 , bool addDCTerm=false , bool solveNormal=false );
157 #ifdef WIN32
158  template< class T2 >
159  static int SolveAtomic( const SparseSymmetricMatrix<T>& M , const PoissonVector<T2>& b , int iters , PoissonVector<T2>& solution , T2 eps=1e-8 , int reset=1 , int threads=0 , bool solveNormal=false );
160 #endif // WIN32
161  template<class T2>
162  static int Solve( const SparseSymmetricMatrix<T>& M , const PoissonVector<T2>& diagonal , const PoissonVector<T2>& b , int iters , PoissonVector<T2>& solution , int reset=1 );
163 
164  template< class T2 >
165  void getDiagonal( PoissonVector< T2 >& diagonal ) const;
166 };
167 
168 #ifndef DOXY_IGNORE_THIS
169 #include "SparseMatrix.inl"
170 #endif
171 
172 #endif
173