All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SparseMatrix.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_SPARSEMATRIX_H_
23 #define _BLAZE_MATH_SPARSEMATRIX_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
59 #include <blaze/math/Matrix.h>
60 #include <blaze/math/shims/Equal.h>
63 #include <blaze/util/Assert.h>
64 #include <blaze/util/Types.h>
66 
67 
68 namespace blaze {
69 
70 //=================================================================================================
71 //
72 // GLOBAL OPERATORS
73 //
74 //=================================================================================================
75 
76 //*************************************************************************************************
79 template< typename T1, typename T2, bool SO >
80 inline bool operator==( const SparseMatrix<T1,false>& lhs, const SparseMatrix<T2,false>& rhs );
81 
82 template< typename T1, typename T2, bool SO >
83 inline bool operator==( const SparseMatrix<T1,true>& lhs, const SparseMatrix<T2,true>& rhs );
84 
85 template< typename T1, typename T2, bool SO >
86 inline bool operator==( const SparseMatrix<T1,SO>& lhs, const SparseMatrix<T2,!SO>& rhs );
87 
88 template< typename T1, bool SO1, typename T2, bool SO2 >
89 inline bool operator!=( const SparseMatrix<T1,SO1>& lhs, const SparseMatrix<T2,SO2>& rhs );
91 //*************************************************************************************************
92 
93 
94 //*************************************************************************************************
102 template< typename T1 // Type of the left-hand side sparse matrix
103  , typename T2 > // Type of the right-hand side sparse matrix
104 inline bool operator==( const SparseMatrix<T1,false>& lhs, const SparseMatrix<T2,false>& rhs )
105 {
106  typedef typename T1::CompositeType CT1;
107  typedef typename T2::CompositeType CT2;
108  typedef typename RemoveReference<CT1>::Type::ConstIterator LhsConstIterator;
109  typedef typename RemoveReference<CT2>::Type::ConstIterator RhsConstIterator;
110 
111  // Early exit in case the matrix sizes don't match
112  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
113  return false;
114 
115  // Evaluation of the two sparse matrix operands
116  CT1 A( ~lhs );
117  CT2 B( ~rhs );
118 
119  // In order to compare the two matrices, the data values of the lower-order data
120  // type are converted to the higher-order data type within the equal function.
121  for( size_t i=0; i<A.rows(); ++i )
122  {
123  const LhsConstIterator lend( A.end(i) );
124  const RhsConstIterator rend( B.end(i) );
125 
126  LhsConstIterator lelem( A.begin(i) );
127  RhsConstIterator relem( B.begin(i) );
128 
129  while( lelem < lend && relem < rend ) {
130  if( lelem->index() < relem->index() && !isDefault( (lelem++)->value() ) )
131  return false;
132  else if( lelem->index() > relem->index() && !isDefault( (relem++)->value() ) )
133  return false;
134  else if( !equal( (lelem++)->value(), (relem++)->value() ) )
135  return false;
136  }
137 
138  while( lelem < lend ) {
139  if( !isDefault( (lelem++)->value() ) )
140  return false;
141  }
142 
143  while( relem < rend ) {
144  if( !isDefault( (relem++)->value() ) )
145  return false;
146  }
147  }
148 
149  return true;
150 }
151 //*************************************************************************************************
152 
153 
154 //*************************************************************************************************
162 template< typename T1 // Type of the left-hand side sparse matrix
163  , typename T2 > // Type of the right-hand side sparse matrix
164 inline bool operator==( const SparseMatrix<T1,true>& lhs, const SparseMatrix<T2,true>& rhs )
165 {
166  typedef typename T1::CompositeType CT1;
167  typedef typename T2::CompositeType CT2;
168  typedef typename RemoveReference<CT1>::Type::ConstIterator LhsConstIterator;
169  typedef typename RemoveReference<CT2>::Type::ConstIterator RhsConstIterator;
170 
171  // Early exit in case the matrix sizes don't match
172  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
173  return false;
174 
175  // Evaluation of the two sparse matrix operands
176  CT1 A( ~lhs );
177  CT2 B( ~rhs );
178 
179  // In order to compare the two matrices, the data values of the lower-order data
180  // type are converted to the higher-order data type within the equal function.
181  for( size_t j=0; j<A.columns(); ++j )
182  {
183  const LhsConstIterator lend( A.end(j) );
184  const RhsConstIterator rend( B.end(j) );
185 
186  LhsConstIterator lelem( A.begin(j) );
187  RhsConstIterator relem( B.begin(j) );
188 
189  while( lelem < lend && relem < rend ) {
190  if( lelem->index() < relem->index() && !isDefault( (lelem++)->value() ) )
191  return false;
192  else if( lelem->index() > relem->index() && !isDefault( (relem++)->value() ) )
193  return false;
194  else if( !equal( (lelem++)->value(), (relem++)->value() ) )
195  return false;
196  }
197 
198  while( lelem < lend ) {
199  if( !isDefault( (lelem++)->value() ) )
200  return false;
201  }
202 
203  while( relem < rend ) {
204  if( !isDefault( (relem++)->value() ) )
205  return false;
206  }
207  }
208 
209  return true;
210 }
211 //*************************************************************************************************
212 
213 
214 //*************************************************************************************************
222 template< typename T1 // Type of the left-hand side sparse matrix
223  , typename T2 // Type of the right-hand side sparse matrix
224  , bool SO > // Storage order
225 inline bool operator==( const SparseMatrix<T1,SO>& lhs, const SparseMatrix<T2,!SO>& rhs )
226 {
227  const typename T2::TransposeType tmp( ~rhs );
228  return ( ~lhs == tmp );
229 }
230 //*************************************************************************************************
231 
232 
233 //*************************************************************************************************
241 template< typename T1 // Type of the left-hand side sparse matrix
242  , bool SO1 // Storage order of the left-hand side sparse matrix
243  , typename T2 // Type of the right-hand side sparse matrix
244  , bool SO2 > // Storage order of the right-hand side sparse matrix
245 inline bool operator!=( const SparseMatrix<T1,SO1>& lhs, const SparseMatrix<T2,SO2>& rhs )
246 {
247  return !( lhs == rhs );
248 }
249 //*************************************************************************************************
250 
251 } // namespace blaze
252 
253 #endif