All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DenseMatrix.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_DENSEMATRIX_H_
23 #define _BLAZE_MATH_DENSEMATRIX_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <boost/type_traits/remove_reference.hpp>
73 #include <blaze/math/Matrix.h>
74 #include <blaze/math/shims/Equal.h>
77 #include <blaze/util/Assert.h>
78 #include <blaze/util/EnableIf.h>
79 #include <blaze/util/Types.h>
81 
82 
83 namespace blaze {
84 
85 //=================================================================================================
86 //
87 // GLOBAL OPERATORS
88 //
89 //=================================================================================================
90 
91 //*************************************************************************************************
94 template< typename T1, typename T2 >
95 inline bool operator==( const DenseMatrix<T1,false>& lhs, const DenseMatrix<T2,false>& rhs );
96 
97 template< typename T1, typename T2 >
98 inline bool operator==( const DenseMatrix<T1,true>& lhs, const DenseMatrix<T2,true>& rhs );
99 
100 template< typename T1, typename T2, bool SO >
101 inline bool operator==( const DenseMatrix<T1,SO>& lhs, const DenseMatrix<T2,!SO>& rhs );
102 
103 template< typename T1, typename T2, bool SO >
104 inline bool operator==( const DenseMatrix<T1,SO>& lhs, const SparseMatrix<T2,false>& rhs );
105 
106 template< typename T1, typename T2, bool SO >
107 inline bool operator==( const DenseMatrix<T1,SO>& lhs, const SparseMatrix<T2,true>& rhs );
108 
109 template< typename T1, bool SO1, typename T2, bool SO2 >
110 inline bool operator==( const SparseMatrix<T1,SO1>& lhs, const DenseMatrix<T2,SO2>& rhs );
111 
112 template< typename T1, typename T2 >
113 inline typename EnableIf< IsNumeric<T2>, bool >::Type
114  operator==( const DenseMatrix<T1,false>& mat, T2 scalar );
115 
116 template< typename T1, typename T2 >
117 inline typename EnableIf< IsNumeric<T2>, bool >::Type
118  operator==( const DenseMatrix<T1,true>& mat, T2 scalar );
119 
120 template< typename T1, typename T2, bool SO >
121 inline typename EnableIf< IsNumeric<T2>, bool >::Type
122  operator==( T1 scalar, const DenseMatrix<T2,SO>& mat );
123 
124 template< typename T1, bool SO1, typename T2, bool SO2 >
125 inline bool operator!=( const DenseMatrix<T1,SO1>& lhs, const DenseMatrix<T2,SO2>& rhs );
126 
127 template< typename T1, bool SO1, typename T2, bool SO2 >
128 inline bool operator!=( const DenseMatrix<T1,SO1>& lhs, const SparseMatrix<T2,SO2>& rhs );
129 
130 template< typename T1, bool SO1, typename T2, bool SO2 >
131 inline bool operator!=( const SparseMatrix<T1,SO1>& lhs, const DenseMatrix<T2,SO2>& rhs );
132 
133 template< typename T1, typename T2, bool SO >
134 inline typename EnableIf< IsNumeric<T2>, bool >::Type
135  operator!=( const DenseMatrix<T1,SO>& mat, T2 scalar );
136 
137 template< typename T1, typename T2, bool SO >
138 inline typename EnableIf< IsNumeric<T2>, bool >::Type
139  operator!=( T1 scalar, const DenseMatrix<T2,SO>& mat );
141 //*************************************************************************************************
142 
143 
144 //*************************************************************************************************
152 template< typename T1 // Type of the left-hand side dense matrix
153  , typename T2 > // Type of the right-hand side dense matrix
154 inline bool operator==( const DenseMatrix<T1,false>& lhs, const DenseMatrix<T2,false>& rhs )
155 {
156  typedef typename T1::CompositeType CT1;
157  typedef typename T2::CompositeType CT2;
158 
159  // Early exit in case the matrix sizes don't match
160  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
161  return false;
162 
163  // Evaluation of the two dense matrix operands
164  CT1 A( ~lhs );
165  CT2 B( ~rhs );
166 
167  // In order to compare the two matrices, the data values of the lower-order data
168  // type are converted to the higher-order data type within the equal function.
169  for( size_t i=0; i<A.rows(); ++i ) {
170  for( size_t j=0; j<A.columns(); ++j ) {
171  if( !equal( A(i,j), B(i,j) ) ) return false;
172  }
173  }
174 
175  return true;
176 }
177 //*************************************************************************************************
178 
179 
180 //*************************************************************************************************
188 template< typename T1 // Type of the left-hand side dense matrix
189  , typename T2 > // Type of the right-hand side dense matrix
190 inline bool operator==( const DenseMatrix<T1,true>& lhs, const DenseMatrix<T2,true>& rhs )
191 {
192  typedef typename T1::CompositeType CT1;
193  typedef typename T2::CompositeType CT2;
194 
195  // Early exit in case the matrix sizes don't match
196  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
197  return false;
198 
199  // Evaluation of the two dense matrix operands
200  CT1 A( ~lhs );
201  CT2 B( ~rhs );
202 
203  // In order to compare the two matrices, the data values of the lower-order data
204  // type are converted to the higher-order data type within the equal function.
205  for( size_t j=0; j<A.columns(); ++j ) {
206  for( size_t i=0; i<A.rows(); ++i ) {
207  if( !equal( A(i,j), B(i,j) ) ) return false;
208  }
209  }
210 
211  return true;
212 }
213 //*************************************************************************************************
214 
215 
216 //*************************************************************************************************
224 template< typename T1 // Type of the left-hand side dense matrix
225  , typename T2 // Type of the right-hand side dense matrix
226  , bool SO > // Storage order
227 inline bool operator==( const DenseMatrix<T1,SO>& lhs, const DenseMatrix<T2,!SO>& rhs )
228 {
229  typedef typename T1::CompositeType CT1;
230  typedef typename T2::CompositeType CT2;
231 
232  // Early exit in case the matrix sizes don't match
233  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
234  return false;
235 
236  // Evaluation of the two dense matrix operands
237  CT1 A( ~lhs );
238  CT2 B( ~rhs );
239 
240  // In order to compare the two matrices, the data values of the lower-order data
241  // type are converted to the higher-order data type within the equal function.
242  const size_t rows ( A.rows() );
243  const size_t columns( A.columns() );
244  const size_t block ( 16 );
245 
246  for( size_t ii=0; ii<rows; ii+=block ) {
247  const size_t iend( ( rows < ii+block )?( rows ):( ii+block ) );
248  for( size_t jj=0; jj<columns; jj+=block ) {
249  const size_t jend( ( columns < jj+block )?( columns ):( jj+block ) );
250  for( size_t i=ii; i<iend; ++i ) {
251  for( size_t j=jj; j<jend; ++j ) {
252  if( !equal( A(i,j), B(i,j) ) ) return false;
253  }
254  }
255  }
256  }
257 
258  return true;
259 }
260 //*************************************************************************************************
261 
262 
263 //*************************************************************************************************
271 template< typename T1 // Type of the left-hand side dense matrix
272  , typename T2 // Type of the right-hand side sparse matrix
273  , bool SO > // Storage order of the left-hand side dense matrix
274 inline bool operator==( const DenseMatrix<T1,SO>& lhs, const SparseMatrix<T2,false>& rhs )
275 {
276  typedef typename T1::CompositeType CT1;
277  typedef typename T2::CompositeType CT2;
278  typedef typename boost::remove_reference<CT2>::type::ConstIterator ConstIterator;
279 
280  // Early exit in case the matrix sizes don't match
281  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
282  return false;
283 
284  // Evaluation of the dense matrix and sparse matrix operand
285  CT1 A( ~lhs );
286  CT2 B( ~rhs );
287 
288  // In order to compare the two matrices, the data values of the lower-order data
289  // type are converted to the higher-order data type within the equal function.
290  size_t j( 0 );
291 
292  for( size_t i=0; i<B.rows(); ++i ) {
293  j = 0;
294  for( ConstIterator element=B.begin(i); element!=B.end(i); ++element, ++j ) {
295  for( ; j<element->index(); ++j ) {
296  if( !isDefault( A(i,j) ) ) return false;
297  }
298  if( !equal( element->value(), A(i,j) ) ) return false;
299  }
300  for( ; j<A.columns(); ++j ) {
301  if( !isDefault( A(i,j) ) ) return false;
302  }
303  }
304 
305  return true;
306 }
307 //*************************************************************************************************
308 
309 
310 //*************************************************************************************************
318 template< typename T1 // Type of the left-hand side dense matrix
319  , typename T2 // Type of the right-hand side sparse matrix
320  , bool SO > // Storage order of the left-hand side dense matrix
321 inline bool operator==( const DenseMatrix<T1,SO>& lhs, const SparseMatrix<T2,true>& rhs )
322 {
323  typedef typename T1::CompositeType CT1;
324  typedef typename T2::CompositeType CT2;
325  typedef typename boost::remove_reference<CT2>::type::ConstIterator ConstIterator;
326 
327  // Early exit in case the matrix sizes don't match
328  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
329  return false;
330 
331  // Evaluation of the dense matrix and sparse matrix operand
332  CT1 A( ~lhs );
333  CT2 B( ~rhs );
334 
335  // In order to compare the two matrices, the data values of the lower-order data
336  // type are converted to the higher-order data type within the equal function.
337  size_t i( 0 );
338 
339  for( size_t j=0; j<B.columns(); ++j ) {
340  i = 0;
341  for( ConstIterator element=B.begin(j); element!=B.end(j); ++element, ++i ) {
342  for( ; i<element->index(); ++i ) {
343  if( !isDefault( A(i,j) ) ) return false;
344  }
345  if( !equal( element->value(), A(i,j) ) ) return false;
346  }
347  for( ; i<A.rows(); ++i ) {
348  if( !isDefault( A(i,j) ) ) return false;
349  }
350  }
351 
352  return true;
353 }
354 //*************************************************************************************************
355 
356 
357 //*************************************************************************************************
365 template< typename T1 // Type of the left-hand side sparse matrix
366  , bool SO1 // Storage order of the left-hand side sparse matrix
367  , typename T2 // Type of the right-hand side dense matrix
368  , bool SO2 > // Storage order of the right-hand side sparse matrix
369 inline bool operator==( const SparseMatrix<T1,SO1>& lhs, const DenseMatrix<T2,SO2>& rhs )
370 {
371  return ( rhs == lhs );
372 }
373 //*************************************************************************************************
374 
375 
376 //*************************************************************************************************
388 template< typename T1 // Type of the left-hand side dense matrix
389  , typename T2 > // Type of the right-hand side scalar
390 inline typename EnableIf< IsNumeric<T2>, bool >::Type
391  operator==( const DenseMatrix<T1,false>& mat, T2 scalar )
392 {
393  typedef typename T1::CompositeType CT1;
394 
395  // Evaluation of the dense matrix operand
396  CT1 A( ~mat );
397 
398  // In order to compare the matrix and the scalar value, the data values of the lower-order
399  // data type are converted to the higher-order data type within the equal function.
400  for( size_t i=0; i<A.rows(); ++i ) {
401  for( size_t j=0; j<A.columns(); ++j ) {
402  if( !equal( A(i,j), scalar ) ) return false;
403  }
404  }
405 
406  return true;
407 }
408 //*************************************************************************************************
409 
410 
411 //*************************************************************************************************
423 template< typename T1 // Type of the left-hand side dense matrix
424  , typename T2 > // Type of the right-hand side scalar
425 inline typename EnableIf< IsNumeric<T2>, bool >::Type
426  operator==( const DenseMatrix<T1,true>& mat, T2 scalar )
427 {
428  typedef typename T1::CompositeType CT1;
429 
430  // Evaluation of the dense matrix operand
431  CT1 A( ~mat );
432 
433  // In order to compare the matrix and the scalar value, the data values of the lower-order
434  // data type are converted to the higher-order data type within the equal function.
435  for( size_t j=0; j<A.columns(); ++j ) {
436  for( size_t i=0; i<A.rows(); ++i ) {
437  if( !equal( A(i,j), scalar ) ) return false;
438  }
439  }
440 
441  return true;
442 }
443 //*************************************************************************************************
444 
445 
446 //*************************************************************************************************
458 template< typename T1 // Type of the left-hand side scalar
459  , typename T2 // Type of the right-hand side dense matrix
460  , bool SO > // Storage order
461 inline typename EnableIf< IsNumeric<T1>, bool >::Type
462  operator==( T1 scalar, const DenseMatrix<T2,SO>& mat )
463 {
464  return ( mat == scalar );
465 }
466 //*************************************************************************************************
467 
468 
469 //*************************************************************************************************
477 template< typename T1 // Type of the left-hand side dense matrix
478  , bool SO1 // Storage order of the left-hand side dense matrix
479  , typename T2 // Type of the right-hand side dense matrix
480  , bool SO2 > // Storage order of the right-hand side dense matrix
481 inline bool operator!=( const DenseMatrix<T1,SO1>& lhs, const DenseMatrix<T2,SO2>& rhs )
482 {
483  return !( lhs == rhs );
484 }
485 //*************************************************************************************************
486 
487 
488 //*************************************************************************************************
496 template< typename T1 // Type of the left-hand side dense matrix
497  , bool SO1 // Storage order of the left-hand side dense matrix
498  , typename T2 // Type of the right-hand side sparse matrix
499  , bool SO2 > // Storage order of the right-hand side sparse matrix
500 inline bool operator!=( const DenseMatrix<T1,SO1>& lhs, const SparseMatrix<T2,SO2>& rhs )
501 {
502  return !( lhs == rhs );
503 }
504 //*************************************************************************************************
505 
506 
507 //*************************************************************************************************
515 template< typename T1 // Type of the left-hand side sparse matrix
516  , bool SO1 // Storage order of the left-hand side sparse matrix
517  , typename T2 // Type of the right-hand side dense matrix
518  , bool SO2 > // Storage order right-hand side dense matrix
519 inline bool operator!=( const SparseMatrix<T1,SO1>& lhs, const DenseMatrix<T2,SO2>& rhs )
520 {
521  return !( rhs == lhs );
522 }
523 //*************************************************************************************************
524 
525 
526 //*************************************************************************************************
538 template< typename T1 // Type of the left-hand side dense matrix
539  , typename T2 // Type of the right-hand side scalar
540  , bool SO > // Storage order
541 inline typename EnableIf< IsNumeric<T2>, bool >::Type
542  operator!=( const DenseMatrix<T1,SO>& mat, T2 scalar )
543 {
544  return !( mat == scalar );
545 }
546 //*************************************************************************************************
547 
548 
549 //*************************************************************************************************
561 template< typename T1 // Type of the left-hand side scalar
562  , typename T2 // Type of the right-hand side dense matrix
563  , bool SO > // Storage order
564 inline typename EnableIf< IsNumeric<T1>, bool >::Type
565  operator!=( T1 scalar, const DenseMatrix<T2,SO>& mat )
566 {
567  return !( mat == scalar );
568 }
569 //*************************************************************************************************
570 
571 } // namespace blaze
572 
573 #endif