All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SparseMatrix.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_SPARSEMATRIX_H_
36 #define _BLAZE_MATH_SPARSEMATRIX_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
72 #include <blaze/math/Matrix.h>
73 #include <blaze/math/shims/Equal.h>
75 #include <blaze/math/shims/IsNaN.h>
77 #include <blaze/util/Assert.h>
78 #include <blaze/util/Types.h>
80 
81 
82 namespace blaze {
83 
84 //=================================================================================================
85 //
86 // GLOBAL OPERATORS
87 //
88 //=================================================================================================
89 
90 //*************************************************************************************************
93 template< typename T1, typename T2, bool SO >
94 inline bool operator==( const SparseMatrix<T1,false>& lhs, const SparseMatrix<T2,false>& rhs );
95 
96 template< typename T1, typename T2, bool SO >
97 inline bool operator==( const SparseMatrix<T1,true>& lhs, const SparseMatrix<T2,true>& rhs );
98 
99 template< typename T1, typename T2, bool SO >
100 inline bool operator==( const SparseMatrix<T1,SO>& lhs, const SparseMatrix<T2,!SO>& rhs );
101 
102 template< typename T1, bool SO1, typename T2, bool SO2 >
103 inline bool operator!=( const SparseMatrix<T1,SO1>& lhs, const SparseMatrix<T2,SO2>& rhs );
105 //*************************************************************************************************
106 
107 
108 //*************************************************************************************************
116 template< typename T1 // Type of the left-hand side sparse matrix
117  , typename T2 > // Type of the right-hand side sparse matrix
118 inline bool operator==( const SparseMatrix<T1,false>& lhs, const SparseMatrix<T2,false>& rhs )
119 {
120  typedef typename T1::CompositeType CT1;
121  typedef typename T2::CompositeType CT2;
122  typedef typename RemoveReference<CT1>::Type::ConstIterator LhsConstIterator;
123  typedef typename RemoveReference<CT2>::Type::ConstIterator RhsConstIterator;
124 
125  // Early exit in case the matrix sizes don't match
126  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
127  return false;
128 
129  // Evaluation of the two sparse matrix operands
130  CT1 A( ~lhs );
131  CT2 B( ~rhs );
132 
133  // In order to compare the two matrices, the data values of the lower-order data
134  // type are converted to the higher-order data type within the equal function.
135  for( size_t i=0; i<A.rows(); ++i )
136  {
137  const LhsConstIterator lend( A.end(i) );
138  const RhsConstIterator rend( B.end(i) );
139 
140  LhsConstIterator lelem( A.begin(i) );
141  RhsConstIterator relem( B.begin(i) );
142 
143  while( lelem != lend && relem != rend ) {
144  if( lelem->index() < relem->index() && !isDefault( (lelem++)->value() ) )
145  return false;
146  else if( lelem->index() > relem->index() && !isDefault( (relem++)->value() ) )
147  return false;
148  else if( !equal( (lelem++)->value(), (relem++)->value() ) )
149  return false;
150  }
151 
152  while( lelem != lend ) {
153  if( !isDefault( (lelem++)->value() ) )
154  return false;
155  }
156 
157  while( relem != rend ) {
158  if( !isDefault( (relem++)->value() ) )
159  return false;
160  }
161  }
162 
163  return true;
164 }
165 //*************************************************************************************************
166 
167 
168 //*************************************************************************************************
176 template< typename T1 // Type of the left-hand side sparse matrix
177  , typename T2 > // Type of the right-hand side sparse matrix
178 inline bool operator==( const SparseMatrix<T1,true>& lhs, const SparseMatrix<T2,true>& rhs )
179 {
180  typedef typename T1::CompositeType CT1;
181  typedef typename T2::CompositeType CT2;
182  typedef typename RemoveReference<CT1>::Type::ConstIterator LhsConstIterator;
183  typedef typename RemoveReference<CT2>::Type::ConstIterator RhsConstIterator;
184 
185  // Early exit in case the matrix sizes don't match
186  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
187  return false;
188 
189  // Evaluation of the two sparse matrix operands
190  CT1 A( ~lhs );
191  CT2 B( ~rhs );
192 
193  // In order to compare the two matrices, the data values of the lower-order data
194  // type are converted to the higher-order data type within the equal function.
195  for( size_t j=0; j<A.columns(); ++j )
196  {
197  const LhsConstIterator lend( A.end(j) );
198  const RhsConstIterator rend( B.end(j) );
199 
200  LhsConstIterator lelem( A.begin(j) );
201  RhsConstIterator relem( B.begin(j) );
202 
203  while( lelem != lend && relem != rend ) {
204  if( lelem->index() < relem->index() && !isDefault( (lelem++)->value() ) )
205  return false;
206  else if( lelem->index() > relem->index() && !isDefault( (relem++)->value() ) )
207  return false;
208  else if( !equal( (lelem++)->value(), (relem++)->value() ) )
209  return false;
210  }
211 
212  while( lelem != lend ) {
213  if( !isDefault( (lelem++)->value() ) )
214  return false;
215  }
216 
217  while( relem != rend ) {
218  if( !isDefault( (relem++)->value() ) )
219  return false;
220  }
221  }
222 
223  return true;
224 }
225 //*************************************************************************************************
226 
227 
228 //*************************************************************************************************
236 template< typename T1 // Type of the left-hand side sparse matrix
237  , typename T2 // Type of the right-hand side sparse matrix
238  , bool SO > // Storage order
239 inline bool operator==( const SparseMatrix<T1,SO>& lhs, const SparseMatrix<T2,!SO>& rhs )
240 {
241  const typename T2::TransposeType tmp( ~rhs );
242  return ( ~lhs == tmp );
243 }
244 //*************************************************************************************************
245 
246 
247 //*************************************************************************************************
255 template< typename T1 // Type of the left-hand side sparse matrix
256  , bool SO1 // Storage order of the left-hand side sparse matrix
257  , typename T2 // Type of the right-hand side sparse matrix
258  , bool SO2 > // Storage order of the right-hand side sparse matrix
259 inline bool operator!=( const SparseMatrix<T1,SO1>& lhs, const SparseMatrix<T2,SO2>& rhs )
260 {
261  return !( lhs == rhs );
262 }
263 //*************************************************************************************************
264 
265 
266 
267 
268 //=================================================================================================
269 //
270 // GLOBAL FUNCTIONS
271 //
272 //=================================================================================================
273 
274 //*************************************************************************************************
277 template< typename MT, bool SO >
278 bool isnan( const SparseMatrix<MT,SO>& sm );
279 
280 template< typename MT, bool SO >
281 bool isDiagonal( const SparseMatrix<MT,SO>& sm );
282 
283 template< typename MT, bool SO >
284 bool isSymmetric( const SparseMatrix<MT,SO>& sm );
285 
286 template< typename MT, bool SO >
287 const typename MT::ElementType min( const SparseVector<MT,SO>& sm );
288 
289 template< typename MT, bool SO >
290 const typename MT::ElementType max( const SparseVector<MT,SO>& sm );
292 //*************************************************************************************************
293 
294 
295 //*************************************************************************************************
315 template< typename MT // Type of the sparse matrix
316  , bool SO > // Storage order
317 bool isnan( const SparseMatrix<MT,SO>& sm )
318 {
319  typedef typename MT::CompositeType CT;
321 
322  CT A( ~sm ); // Evaluation of the sparse matrix operand
323 
324  if( SO == rowMajor ) {
325  for( size_t i=0UL; i<A.rows(); ++i ) {
326  for( ConstIterator element=A.begin(i); element!=A.end(i); ++element )
327  if( isnan( element->value() ) ) return true;
328  }
329  }
330  else {
331  for( size_t j=0UL; j<A.columns(); ++j ) {
332  for( ConstIterator element=A.begin(j); element!=A.end(j); ++element )
333  if( isnan( element->value() ) ) return true;
334  }
335  }
336 
337  return false;
338 }
339 //*************************************************************************************************
340 
341 
342 //*************************************************************************************************
361 template< typename MT // Type of the sparse matrix
362  , bool SO > // Storage order
364 {
365  typedef typename MT::ConstIterator ConstIterator;
366 
367  const size_t rows ( (~sm).rows() );
368  const size_t columns( (~sm).columns() );
369 
370  if( rows != columns ) return false;
371 
372  if( SO == rowMajor ) {
373  for( size_t i=0UL; i<rows; ++i ) {
374  for( ConstIterator element=(~sm).begin(i); element!=(~sm).end(i); ++element )
375  if( element->index() != i && !isDefault( element->value() ) )
376  return false;
377  }
378  }
379  else {
380  for( size_t j=0UL; j<columns; ++j ) {
381  for( ConstIterator element=(~sm).begin(j); element!=(~sm).end(j); ++element )
382  if( element->index() != j && !isDefault( element->value() ) )
383  return false;
384  }
385  }
386 
387  return true;
388 }
389 //*************************************************************************************************
390 
391 
392 //*************************************************************************************************
399 template< typename MT // Type of the sparse matrix
400  , bool SO > // Storage order
402 {
403  typedef typename MT::ConstIterator ConstIterator;
404 
405  const size_t rows ( (~sm).rows() );
406  const size_t columns( (~sm).columns() );
407 
408  if( rows != columns ) return false;
409 
410  if( SO == rowMajor ) {
411  for( size_t i=0UL; i<rows; ++i ) {
412  for( ConstIterator element=(~sm).begin(i); element!=(~sm).end(i); ++element )
413  {
414  const size_t index( element->index() );
415 
416  if( isDefault( element->value() ) )
417  continue;
418 
419  const ConstIterator pos( (~sm).lowerBound( index, i ) );
420  if( pos == (~sm).end(index) || pos->index() != i || !equal( pos->value(), element->value() ) )
421  return false;
422  }
423  }
424  }
425  else {
426  for( size_t j=0UL; j<columns; ++j ) {
427  for( ConstIterator element=(~sm).begin(j); element!=(~sm).end(j); ++element )
428  {
429  const size_t index( element->index() );
430 
431  if( isDefault( element->value() ) )
432  continue;
433 
434  const ConstIterator pos( (~sm).lowerBound( j, index ) );
435  if( pos == (~sm).end(index) || pos->index() != j || !equal( pos->value(), element->value() ) )
436  return false;
437  }
438  }
439  }
440 
441  return true;
442 }
443 //*************************************************************************************************
444 
445 
446 //*************************************************************************************************
458 template< typename MT // Type of the sparse matrix
459  , bool SO > // Storage order
460 const typename MT::ElementType min( const SparseMatrix<MT,SO>& sm )
461 {
462  using blaze::min;
463 
464  typedef typename MT::ElementType ET;
465  typedef typename MT::CompositeType CT;
467 
468  CT A( ~sm ); // Evaluation of the sparse matrix operand
469 
470  const size_t nonzeros( A.nonZeros() );
471 
472  if( nonzeros == 0UL ) {
473  return ET();
474  }
475 
476  ET minimum = ET();
477  if( nonzeros == A.rows() * A.columns() ) {
478  minimum = A.begin( 0UL )->value();
479  }
480 
481  const size_t index( ( SO == rowMajor )?( A.rows() ):( A.columns() ) );
482 
483  for( size_t i=0UL; i<index; ++i ) {
484  const ConstIterator end( A.end( i ) );
485  ConstIterator element( A.begin( i ) );
486  for( ; element!=end; ++element )
487  minimum = min( minimum, element->value() );
488  }
489 
490  return minimum;
491 }
492 //*************************************************************************************************
493 
494 
495 //*************************************************************************************************
507 template< typename MT // Type of the sparse matrix
508  , bool SO > // Storage order
509 const typename MT::ElementType max( const SparseMatrix<MT,SO>& sm )
510 {
511  using blaze::max;
512 
513  typedef typename MT::ElementType ET;
514  typedef typename MT::CompositeType CT;
516 
517  CT A( ~sm ); // Evaluation of the sparse matrix operand
518 
519  const size_t nonzeros( A.nonZeros() );
520 
521  if( nonzeros == 0UL ) {
522  return ET();
523  }
524 
525  ET maximum = ET();
526  if( nonzeros == A.rows() * A.columns() ) {
527  maximum = A.begin( 0UL )->value();
528  }
529 
530  const size_t index( ( SO == rowMajor )?( A.rows() ):( A.columns() ) );
531 
532  for( size_t i=0UL; i<index; ++i ) {
533  const ConstIterator end( A.end( i ) );
534  ConstIterator element( A.begin( i ) );
535  for( ; element!=end; ++element )
536  maximum = max( maximum, element->value() );
537  }
538 
539  return maximum;
540 }
541 //*************************************************************************************************
542 
543 } // namespace blaze
544 
545 #endif
Header file for the isnan shim.
bool isDiagonal(const DenseMatrix< MT, SO > &dm)
Checks if the give dense matrix is diagonal.
Definition: DenseMatrix.h:682
Header file for the dense vector/sparse vector outer product expression.
Header file for the transpose sparse vector/transpose sparse matrix multiplication expression...
Header file for the transpose sparse matrix/sparse matrix subtraction expression. ...
bool isDefault(const DynamicMatrix< Type, SO > &m)
Returns whether the given dense matrix is in default state.
Definition: DynamicMatrix.h:4622
Header file for the transpose sparse matrix/transpose sparse matrix subtraction expression.
bool isSymmetric(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is symmetric.
Definition: DenseMatrix.h:720
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:2384
bool isnan(const DenseMatrix< MT, SO > &dm)
Checks the given dense matrix for not-a-number elements.
Definition: DenseMatrix.h:637
Header file for the sparse matrix/transpose sparse matrix addition expression.
Header file for the sparse matrix/transpose sparse matrix multiplication expression.
Base class for sparse matrices.The SparseMatrix class is a base class for all sparse matrix classes...
Definition: Forward.h:104
Header file for the transpose sparse matrix/dense vector multiplication expression.
Header file for the SparseMatrix base class.
Header file for the sparse matrix/transpose sparse matrix subtraction expression. ...
Header file for the transpose sparse matrix/sparse vector multiplication expression.
Header file for the matrix storage order types.
Header file for the sparse matrix absolute value expression.
Header file for the transpose dense vector/sparse matrix multiplication expression.
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2388
Header file for the transpose sparse matrix/transpose sparse matrix addition expression.
Header file for the DenseMatrix base class.
Header file for the sparse matrix/scalar multiplication expression.
Header file for the sparse vector/dense vector outer product expression.
Header file for the transpose sparse matrix/sparse matrix multiplication expression.
Header file for the sparse vector/sparse vector outer product expression.
CompressedMatrix< Type, false > TransposeType
Transpose type for expression template evaluations.
Definition: CompressedMatrix.h:2381
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2382
Header file for the equal shim.
Header file for all basic Matrix functionality.
Header file for the transpose sparse vector/sparse matrix multiplication expression.
Removal of reference modifiers.The RemoveCV type trait removes any reference modifiers from the given...
Definition: RemoveReference.h:69
Header file for run time assertion macros.
bool equal(const T1 &a, const T2 &b)
Generic equality check.
Definition: Equal.h:352
Header file for the sparse matrix transpose expression.
const VT::ElementType max(const SparseVector< VT, TF > &sv)
Returns the largest element of the sparse vector.
Definition: SparseVector.h:405
Header file for the isDefault shim.
Header file for the transpose sparse matrix/transpose sparse matrix multiplication expression...
Header file for the sparse matrix/scalar division expression.
Header file for the sparse matrix/sparse matrix addition expression.
Header file for the RemoveReference type trait.
Header file for the sparse matrix evaluation expression.
const bool rowMajor
Storage order flag for row-major matrices.
Definition: StorageOrder.h:71
Header file for the sparse matrix/dense vector multiplication expression.
Header file for the sparse matrix/sparse vector multiplication expression.
bool operator==(const NegativeAccuracy< A > &lhs, const T &rhs)
Equality comparison between a NegativeAccuracy object and a floating point value. ...
Definition: Accuracy.h:249
size_t columns(const Matrix< MT, SO > &m)
Returns the current number of columns of the matrix.
Definition: Matrix.h:154
bool operator!=(const NegativeAccuracy< A > &lhs, const T &rhs)
Inequality comparison between a NegativeAccuracy object and a floating point value.
Definition: Accuracy.h:289
Header file for basic type definitions.
const VT::ElementType min(const SparseVector< VT, TF > &sv)
Returns the smallest element of the sparse vector.
Definition: SparseVector.h:348
Header file for the transpose dense vector/transpose sparse matrix multiplication expression...
Header file for the sparse matrix/sparse matrix subtraction expression.
Header file for the sparse matrix/sparse matrix multiplication expression.
size_t rows(const Matrix< MT, SO > &m)
Returns the current number of rows of the matrix.
Definition: Matrix.h:138