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 
73 #include <blaze/math/Matrix.h>
74 #include <blaze/math/shims/Equal.h>
76 #include <blaze/math/shims/IsNaN.h>
80 #include <blaze/util/Assert.h>
81 #include <blaze/util/Types.h>
83 
84 
85 namespace blaze {
86 
87 //=================================================================================================
88 //
89 // GLOBAL OPERATORS
90 //
91 //=================================================================================================
92 
93 //*************************************************************************************************
96 template< typename T1, typename T2, bool SO >
97 inline bool operator==( const SparseMatrix<T1,false>& lhs, const SparseMatrix<T2,false>& rhs );
98 
99 template< typename T1, typename T2, bool SO >
100 inline bool operator==( const SparseMatrix<T1,true>& lhs, const SparseMatrix<T2,true>& rhs );
101 
102 template< typename T1, typename T2, bool SO >
103 inline bool operator==( const SparseMatrix<T1,SO>& lhs, const SparseMatrix<T2,!SO>& rhs );
104 
105 template< typename T1, bool SO1, typename T2, bool SO2 >
106 inline bool operator!=( const SparseMatrix<T1,SO1>& lhs, const SparseMatrix<T2,SO2>& rhs );
108 //*************************************************************************************************
109 
110 
111 //*************************************************************************************************
119 template< typename T1 // Type of the left-hand side sparse matrix
120  , typename T2 > // Type of the right-hand side sparse matrix
121 inline bool operator==( const SparseMatrix<T1,false>& lhs, const SparseMatrix<T2,false>& rhs )
122 {
123  typedef typename T1::CompositeType CT1;
124  typedef typename T2::CompositeType CT2;
125  typedef typename RemoveReference<CT1>::Type::ConstIterator LhsConstIterator;
126  typedef typename RemoveReference<CT2>::Type::ConstIterator RhsConstIterator;
127 
128  // Early exit in case the matrix sizes don't match
129  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
130  return false;
131 
132  // Evaluation of the two sparse matrix operands
133  CT1 A( ~lhs );
134  CT2 B( ~rhs );
135 
136  // In order to compare the two matrices, the data values of the lower-order data
137  // type are converted to the higher-order data type within the equal function.
138  for( size_t i=0; i<A.rows(); ++i )
139  {
140  const LhsConstIterator lend( A.end(i) );
141  const RhsConstIterator rend( B.end(i) );
142 
143  LhsConstIterator lelem( A.begin(i) );
144  RhsConstIterator relem( B.begin(i) );
145 
146  while( lelem != lend && relem != rend ) {
147  if( lelem->index() < relem->index() && !isDefault( (lelem++)->value() ) )
148  return false;
149  else if( lelem->index() > relem->index() && !isDefault( (relem++)->value() ) )
150  return false;
151  else if( !equal( (lelem++)->value(), (relem++)->value() ) )
152  return false;
153  }
154 
155  while( lelem != lend ) {
156  if( !isDefault( (lelem++)->value() ) )
157  return false;
158  }
159 
160  while( relem != rend ) {
161  if( !isDefault( (relem++)->value() ) )
162  return false;
163  }
164  }
165 
166  return true;
167 }
168 //*************************************************************************************************
169 
170 
171 //*************************************************************************************************
179 template< typename T1 // Type of the left-hand side sparse matrix
180  , typename T2 > // Type of the right-hand side sparse matrix
181 inline bool operator==( const SparseMatrix<T1,true>& lhs, const SparseMatrix<T2,true>& rhs )
182 {
183  typedef typename T1::CompositeType CT1;
184  typedef typename T2::CompositeType CT2;
185  typedef typename RemoveReference<CT1>::Type::ConstIterator LhsConstIterator;
186  typedef typename RemoveReference<CT2>::Type::ConstIterator RhsConstIterator;
187 
188  // Early exit in case the matrix sizes don't match
189  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
190  return false;
191 
192  // Evaluation of the two sparse matrix operands
193  CT1 A( ~lhs );
194  CT2 B( ~rhs );
195 
196  // In order to compare the two matrices, the data values of the lower-order data
197  // type are converted to the higher-order data type within the equal function.
198  for( size_t j=0; j<A.columns(); ++j )
199  {
200  const LhsConstIterator lend( A.end(j) );
201  const RhsConstIterator rend( B.end(j) );
202 
203  LhsConstIterator lelem( A.begin(j) );
204  RhsConstIterator relem( B.begin(j) );
205 
206  while( lelem != lend && relem != rend ) {
207  if( lelem->index() < relem->index() && !isDefault( (lelem++)->value() ) )
208  return false;
209  else if( lelem->index() > relem->index() && !isDefault( (relem++)->value() ) )
210  return false;
211  else if( !equal( (lelem++)->value(), (relem++)->value() ) )
212  return false;
213  }
214 
215  while( lelem != lend ) {
216  if( !isDefault( (lelem++)->value() ) )
217  return false;
218  }
219 
220  while( relem != rend ) {
221  if( !isDefault( (relem++)->value() ) )
222  return false;
223  }
224  }
225 
226  return true;
227 }
228 //*************************************************************************************************
229 
230 
231 //*************************************************************************************************
239 template< typename T1 // Type of the left-hand side sparse matrix
240  , typename T2 // Type of the right-hand side sparse matrix
241  , bool SO > // Storage order
242 inline bool operator==( const SparseMatrix<T1,SO>& lhs, const SparseMatrix<T2,!SO>& rhs )
243 {
244  const typename T2::TransposeType tmp( ~rhs );
245  return ( ~lhs == tmp );
246 }
247 //*************************************************************************************************
248 
249 
250 //*************************************************************************************************
258 template< typename T1 // Type of the left-hand side sparse matrix
259  , bool SO1 // Storage order of the left-hand side sparse matrix
260  , typename T2 // Type of the right-hand side sparse matrix
261  , bool SO2 > // Storage order of the right-hand side sparse matrix
262 inline bool operator!=( const SparseMatrix<T1,SO1>& lhs, const SparseMatrix<T2,SO2>& rhs )
263 {
264  return !( lhs == rhs );
265 }
266 //*************************************************************************************************
267 
268 
269 
270 
271 //=================================================================================================
272 //
273 // GLOBAL FUNCTIONS
274 //
275 //=================================================================================================
276 
277 //*************************************************************************************************
280 template< typename MT, bool SO >
281 bool isnan( const SparseMatrix<MT,SO>& sm );
282 
283 template< typename MT, bool SO >
284 bool isDiagonal( const SparseMatrix<MT,SO>& sm );
285 
286 template< typename MT, bool SO >
287 bool isSymmetric( const SparseMatrix<MT,SO>& sm );
288 
289 template< typename MT, bool SO >
290 const typename MT::ElementType min( const SparseVector<MT,SO>& sm );
291 
292 template< typename MT, bool SO >
293 const typename MT::ElementType max( const SparseVector<MT,SO>& sm );
295 //*************************************************************************************************
296 
297 
298 //*************************************************************************************************
318 template< typename MT // Type of the sparse matrix
319  , bool SO > // Storage order
320 bool isnan( const SparseMatrix<MT,SO>& sm )
321 {
322  typedef typename MT::CompositeType CT;
324 
325  CT A( ~sm ); // Evaluation of the sparse matrix operand
326 
327  if( SO == rowMajor ) {
328  for( size_t i=0UL; i<A.rows(); ++i ) {
329  for( ConstIterator element=A.begin(i); element!=A.end(i); ++element )
330  if( isnan( element->value() ) ) return true;
331  }
332  }
333  else {
334  for( size_t j=0UL; j<A.columns(); ++j ) {
335  for( ConstIterator element=A.begin(j); element!=A.end(j); ++element )
336  if( isnan( element->value() ) ) return true;
337  }
338  }
339 
340  return false;
341 }
342 //*************************************************************************************************
343 
344 
345 //*************************************************************************************************
364 template< typename MT // Type of the sparse matrix
365  , bool SO > // Storage order
367 {
368  typedef typename MT::ConstIterator ConstIterator;
369 
370  const size_t rows ( (~sm).rows() );
371  const size_t columns( (~sm).columns() );
372 
373  if( rows != columns ) return false;
374 
375  if( SO == rowMajor ) {
376  for( size_t i=0UL; i<rows; ++i ) {
377  for( ConstIterator element=(~sm).begin(i); element!=(~sm).end(i); ++element )
378  if( element->index() != i && !isDefault( element->value() ) )
379  return false;
380  }
381  }
382  else {
383  for( size_t j=0UL; j<columns; ++j ) {
384  for( ConstIterator element=(~sm).begin(j); element!=(~sm).end(j); ++element )
385  if( element->index() != j && !isDefault( element->value() ) )
386  return false;
387  }
388  }
389 
390  return true;
391 }
392 //*************************************************************************************************
393 
394 
395 //*************************************************************************************************
402 template< typename MT // Type of the sparse matrix
403  , bool SO > // Storage order
405 {
406  typedef typename MT::ConstIterator ConstIterator;
407 
408  const size_t rows ( (~sm).rows() );
409  const size_t columns( (~sm).columns() );
410 
411  if( rows != columns ) return false;
412 
413  if( SO == rowMajor ) {
414  for( size_t i=0UL; i<rows; ++i ) {
415  for( ConstIterator element=(~sm).begin(i); element!=(~sm).end(i); ++element )
416  {
417  const size_t index( element->index() );
418 
419  if( isDefault( element->value() ) )
420  continue;
421 
422  const ConstIterator pos( (~sm).lowerBound( index, i ) );
423  if( pos == (~sm).end(index) || pos->index() != i || !equal( pos->value(), element->value() ) )
424  return false;
425  }
426  }
427  }
428  else {
429  for( size_t j=0UL; j<columns; ++j ) {
430  for( ConstIterator element=(~sm).begin(j); element!=(~sm).end(j); ++element )
431  {
432  const size_t index( element->index() );
433 
434  if( isDefault( element->value() ) )
435  continue;
436 
437  const ConstIterator pos( (~sm).lowerBound( j, index ) );
438  if( pos == (~sm).end(index) || pos->index() != j || !equal( pos->value(), element->value() ) )
439  return false;
440  }
441  }
442  }
443 
444  return true;
445 }
446 //*************************************************************************************************
447 
448 
449 //*************************************************************************************************
461 template< typename MT // Type of the sparse matrix
462  , bool SO > // Storage order
463 const typename MT::ElementType min( const SparseMatrix<MT,SO>& sm )
464 {
465  using blaze::min;
466 
467  typedef typename MT::ElementType ET;
468  typedef typename MT::CompositeType CT;
470 
471  CT A( ~sm ); // Evaluation of the sparse matrix operand
472 
473  const size_t nonzeros( A.nonZeros() );
474 
475  if( nonzeros == 0UL ) {
476  return ET();
477  }
478 
479  ET minimum = ET();
480  if( nonzeros == A.rows() * A.columns() ) {
481  minimum = A.begin( 0UL )->value();
482  }
483 
484  const size_t index( ( SO == rowMajor )?( A.rows() ):( A.columns() ) );
485 
486  for( size_t i=0UL; i<index; ++i ) {
487  const ConstIterator end( A.end( i ) );
488  ConstIterator element( A.begin( i ) );
489  for( ; element!=end; ++element )
490  minimum = min( minimum, element->value() );
491  }
492 
493  return minimum;
494 }
495 //*************************************************************************************************
496 
497 
498 //*************************************************************************************************
510 template< typename MT // Type of the sparse matrix
511  , bool SO > // Storage order
512 const typename MT::ElementType max( const SparseMatrix<MT,SO>& sm )
513 {
514  using blaze::max;
515 
516  typedef typename MT::ElementType ET;
517  typedef typename MT::CompositeType CT;
519 
520  CT A( ~sm ); // Evaluation of the sparse matrix operand
521 
522  const size_t nonzeros( A.nonZeros() );
523 
524  if( nonzeros == 0UL ) {
525  return ET();
526  }
527 
528  ET maximum = ET();
529  if( nonzeros == A.rows() * A.columns() ) {
530  maximum = A.begin( 0UL )->value();
531  }
532 
533  const size_t index( ( SO == rowMajor )?( A.rows() ):( A.columns() ) );
534 
535  for( size_t i=0UL; i<index; ++i ) {
536  const ConstIterator end( A.end( i ) );
537  ConstIterator element( A.begin( i ) );
538  for( ; element!=end; ++element )
539  maximum = max( maximum, element->value() );
540  }
541 
542  return maximum;
543 }
544 //*************************************************************************************************
545 
546 } // namespace blaze
547 
548 #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:685
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:4642
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:723
Header file for the sparse matrix SMP implementation.
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:2408
bool isnan(const DenseMatrix< MT, SO > &dm)
Checks the given dense matrix for not-a-number elements.
Definition: DenseMatrix.h:640
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:107
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:2412
Header file for the transpose sparse matrix/transpose sparse matrix addition expression.
Header file for the dense matrix SMP implementation.
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:2405
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2406
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:408
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 serial evaluation 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:170
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:351
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:154