All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SMatTSMatAddExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_SMATTSMATADDEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_SMATTSMATADDEXPR_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <stdexcept>
31 #include <boost/type_traits/remove_reference.hpp>
46 #include <blaze/util/Assert.h>
47 #include <blaze/util/SelectType.h>
48 #include <blaze/util/Types.h>
49 
50 
51 namespace blaze {
52 
53 //=================================================================================================
54 //
55 // CLASS SMATTSMATADDEXPR
56 //
57 //=================================================================================================
58 
59 //*************************************************************************************************
66 template< typename MT1 // Type of the left-hand side sparse matrix
67  , typename MT2 > // Type of the right-hand side sparse matrix
68 class SMatTSMatAddExpr : public SparseMatrix< SMatTSMatAddExpr<MT1,MT2>, false >
69  , private Expression
70  , private Computation
71 {
72  private:
73  //**Type definitions****************************************************************************
74  typedef typename MT1::ResultType RT1;
75  typedef typename MT2::ResultType RT2;
76  typedef typename MT1::ReturnType RN1;
77  typedef typename MT2::ReturnType RN2;
78  typedef typename MT1::CompositeType CT1;
79  typedef typename MT2::CompositeType CT2;
80  //**********************************************************************************************
81 
82  //**Return type evaluation**********************************************************************
84 
89  enum { returnExpr = !IsTemporary<RN1>::value && !IsTemporary<RN2>::value };
90 
93  //**********************************************************************************************
94 
95  public:
96  //**Type definitions****************************************************************************
99  typedef typename ResultType::OppositeType OppositeType;
100  typedef typename ResultType::TransposeType TransposeType;
101  typedef typename ResultType::ElementType ElementType;
102 
105 
107  typedef const ResultType CompositeType;
108 
110  typedef typename SelectType< IsExpression<MT1>::value, const MT1, const MT1& >::Type LeftOperand;
111 
113  typedef typename SelectType< IsExpression<MT2>::value, const MT2, const MT2& >::Type RightOperand;
114  //**********************************************************************************************
115 
116  //**Compilation flags***************************************************************************
120  //**********************************************************************************************
121 
122  //**Constructor*********************************************************************************
128  explicit inline SMatTSMatAddExpr( const MT1& lhs, const MT2& rhs )
129  : lhs_( lhs ) // Left-hand side sparse matrix of the addition expression
130  , rhs_( rhs ) // Right-hand side sparse matrix of the addition expression
131  {
132  BLAZE_INTERNAL_ASSERT( lhs.rows() == rhs.rows() , "Invalid number of rows" );
133  BLAZE_INTERNAL_ASSERT( lhs.columns() == rhs.columns(), "Invalid number of columns" );
134  }
135  //**********************************************************************************************
136 
137  //**Access operator*****************************************************************************
144  inline ReturnType operator()( size_t i, size_t j ) const {
145  BLAZE_INTERNAL_ASSERT( i < lhs_.rows() , "Invalid row access index" );
146  BLAZE_INTERNAL_ASSERT( j < lhs_.columns(), "Invalid column access index" );
147  return lhs_(i,j) + rhs_(i,j);
148  }
149  //**********************************************************************************************
150 
151  //**Rows function*******************************************************************************
156  inline size_t rows() const {
157  return lhs_.rows();
158  }
159  //**********************************************************************************************
160 
161  //**Columns function****************************************************************************
166  inline size_t columns() const {
167  return lhs_.columns();
168  }
169  //**********************************************************************************************
170 
171  //**NonZeros function***************************************************************************
176  inline size_t nonZeros() const {
177  return lhs_.nonZeros() + rhs_.nonZeros();
178  }
179  //**********************************************************************************************
180 
181  //**NonZeros function***************************************************************************
187  inline size_t nonZeros( size_t i ) const {
188  return lhs_.nonZeros(i) + rhs_.nonZeros(i);
189  }
190  //**********************************************************************************************
191 
192  //**Left operand access*************************************************************************
197  inline LeftOperand leftOperand() const {
198  return lhs_;
199  }
200  //**********************************************************************************************
201 
202  //**Right operand access************************************************************************
207  inline RightOperand rightOperand() const {
208  return rhs_;
209  }
210  //**********************************************************************************************
211 
212  //**********************************************************************************************
218  template< typename T >
219  inline bool isAliased( const T* alias ) const {
221  !RequiresEvaluation<MT1>::value && lhs_.isAliased( alias ) ) ||
223  !RequiresEvaluation<MT2>::value && rhs_.isAliased( alias ) );
224  }
225  //**********************************************************************************************
226 
227  private:
228  //**Member variables****************************************************************************
231  //**********************************************************************************************
232 
233  //**Assignment to dense matrices****************************************************************
245  template< typename MT // Type of the target dense matrix
246  , bool SO > // Storage order of the target dense matrix
247  friend inline void assign( DenseMatrix<MT,SO>& lhs, const SMatTSMatAddExpr& rhs )
248  {
249  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
250  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
251 
252  typedef typename boost::remove_reference<CT2>::type::ConstIterator RightIterator;
253 
254  assign( ~lhs, rhs.lhs_ );
255 
257  addAssign( ~lhs, rhs.rhs_ );
258  }
259  else
260  {
261  CT2 B( rhs.rhs_ ); // Evaluation of the right-hand side sparse matrix operand
262 
263  BLAZE_INTERNAL_ASSERT( B.rows() == rhs.rhs_.rows() , "Invalid number of rows" );
264  BLAZE_INTERNAL_ASSERT( B.columns() == rhs.rhs_.columns(), "Invalid number of columns" );
265  BLAZE_INTERNAL_ASSERT( B.rows() == (~lhs).rows() , "Invalid number of rows" );
266  BLAZE_INTERNAL_ASSERT( B.columns() == (~lhs).columns() , "Invalid number of columns" );
267 
268  for( size_t j=0UL; j<(~lhs).columns(); ++j ) {
269  const RightIterator end( B.end(j) );
270  for( RightIterator element=B.begin(j); element!=end; ++element ) {
271  if( isDefault( (~lhs)(element->index(),j) ) )
272  (~lhs)(element->index(),j) = element->value();
273  else
274  (~lhs)(element->index(),j) += element->value();
275  }
276  }
277  }
278  }
280  //**********************************************************************************************
281 
282  //**Assignment to sparse matrices***************************************************************
294  template< typename MT > // Type of the target sparse matrix
295  friend inline void assign( SparseMatrix<MT,false>& lhs, const SMatTSMatAddExpr& rhs )
296  {
297  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
298  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
299 
300  typedef typename boost::remove_reference<CT1>::type::ConstIterator LeftIterator;
301  typedef typename RT2::OppositeType::ConstIterator RightIterator;
302 
303  // Evaluation of the left-hand side sparse matrix operand
304  CT1 A( rhs.lhs_ );
305 
306  // Evaluation of the right-hand side sparse matrix operand
307  const typename RT2::OppositeType B( rhs.rhs_ );
308 
309  BLAZE_INTERNAL_ASSERT( A.rows() == rhs.lhs_.rows() , "Invalid number of rows" );
310  BLAZE_INTERNAL_ASSERT( A.columns() == rhs.lhs_.columns(), "Invalid number of columns" );
311  BLAZE_INTERNAL_ASSERT( B.rows() == rhs.rhs_.rows() , "Invalid number of rows" );
312  BLAZE_INTERNAL_ASSERT( B.columns() == rhs.rhs_.columns(), "Invalid number of columns" );
313  BLAZE_INTERNAL_ASSERT( A.rows() == (~lhs).rows() , "Invalid number of rows" );
314  BLAZE_INTERNAL_ASSERT( A.columns() == (~lhs).columns() , "Invalid number of columns" );
315 
316  for( size_t i=0UL; i<(~lhs).rows(); ++i )
317  {
318  const LeftIterator lend( A.end(i) );
319  const RightIterator rend( B.end(i) );
320 
321  LeftIterator l( A.begin(i) );
322  RightIterator r( B.begin(i) );
323  size_t nonzeros( A.nonZeros(i) + B.nonZeros(i) );
324 
325  for( ; l!=lend && r!=rend; ++l ) {
326  while( r->index() < l->index() && ++r != rend ) {}
327  if( r!=rend && l->index() == r->index() ) {
328  --nonzeros;
329  ++r;
330  }
331  }
332 
333  BLAZE_INTERNAL_ASSERT( nonzeros <= A.columns(), "Invalid number of non-zero elements predicted" );
334 
335  (~lhs).reserve( i, nonzeros );
336 
337  l = A.begin(i);
338  r = B.begin(i);
339 
340  while( l != lend && r != rend )
341  {
342  if( l->index() < r->index() ) {
343  (~lhs).append( i, l->index(), l->value() );
344  ++l;
345  }
346  else if( l->index() > r->index() ) {
347  (~lhs).append( i, r->index(), r->value() );
348  ++r;
349  }
350  else {
351  (~lhs).append( i, l->index(), l->value()+r->value() );
352  ++l;
353  ++r;
354  }
355  }
356 
357  while( l != lend ) {
358  (~lhs).append( i, l->index(), l->value() );
359  ++l;
360  }
361 
362  while( r != rend ) {
363  (~lhs).append( i, r->index(), r->value() );
364  ++r;
365  }
366  }
367  }
369  //**********************************************************************************************
370 
371  //**Assignment to sparse matrices***************************************************************
383  template< typename MT > // Type of the target sparse matrix
384  friend inline void assign( SparseMatrix<MT,true>& lhs, const SMatTSMatAddExpr& rhs )
385  {
386  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
387  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
388 
389  typedef typename RT1::OppositeType::ConstIterator LeftIterator;
390  typedef typename boost::remove_reference<CT2>::type::ConstIterator RightIterator;
391 
392  // Evaluation of the left-hand side sparse matrix operand
393  const typename RT1::OppositeType A( rhs.lhs_ );
394 
395  // Evaluation of the right-hand side sparse matrix operand
396  CT2 B( rhs.rhs_ );
397 
398  BLAZE_INTERNAL_ASSERT( A.rows() == rhs.lhs_.rows() , "Invalid number of rows" );
399  BLAZE_INTERNAL_ASSERT( A.columns() == rhs.lhs_.columns(), "Invalid number of columns" );
400  BLAZE_INTERNAL_ASSERT( B.rows() == rhs.rhs_.rows() , "Invalid number of rows" );
401  BLAZE_INTERNAL_ASSERT( B.columns() == rhs.rhs_.columns(), "Invalid number of columns" );
402  BLAZE_INTERNAL_ASSERT( A.rows() == (~lhs).rows() , "Invalid number of rows" );
403  BLAZE_INTERNAL_ASSERT( A.columns() == (~lhs).columns() , "Invalid number of columns" );
404 
405  for( size_t j=0UL; j<(~lhs).columns(); ++j )
406  {
407  const LeftIterator lend( A.end(j) );
408  const RightIterator rend( B.end(j) );
409 
410  LeftIterator l( A.begin(j) );
411  RightIterator r( B.begin(j) );
412  size_t nonzeros( A.nonZeros(j) + B.nonZeros(j) );
413 
414  for( ; l!=lend && r!=rend; ++l ) {
415  while( r->index() < l->index() && ++r != rend ) {}
416  if( r!=rend && l->index() == r->index() ) {
417  --nonzeros;
418  ++r;
419  }
420  }
421 
422  BLAZE_INTERNAL_ASSERT( nonzeros <= A.rows(), "Invalid number of non-zero elements predicted" );
423 
424  (~lhs).reserve( j, nonzeros );
425 
426  l = A.begin(j);
427  r = B.begin(j);
428 
429  while( l != lend && r != rend )
430  {
431  if( l->index() < r->index() ) {
432  (~lhs).append( l->index(), j, l->value() );
433  ++l;
434  }
435  else if( l->index() > r->index() ) {
436  (~lhs).append( r->index(), j, r->value() );
437  ++r;
438  }
439  else {
440  (~lhs).append( l->index(), j, l->value()+r->value() );
441  ++l;
442  ++r;
443  }
444  }
445 
446  while( l != lend ) {
447  (~lhs).append( l->index(), j, l->value() );
448  ++l;
449  }
450 
451  while( r != rend ) {
452  (~lhs).append( r->index(), j, r->value() );
453  ++r;
454  }
455  }
456  }
458  //**********************************************************************************************
459 
460  //**Addition assignment to dense matrices*******************************************************
473  template< typename MT // Type of the target dense matrix
474  , bool SO > // Storage order of the target dense matrix
475  friend inline void addAssign( DenseMatrix<MT,SO>& lhs, const SMatTSMatAddExpr& rhs )
476  {
477  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
478  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
479 
480  addAssign( ~lhs, rhs.lhs_ );
481  addAssign( ~lhs, rhs.rhs_ );
482  }
484  //**********************************************************************************************
485 
486  //**Addition assignment to sparse matrices******************************************************
487  // No special implementation for the addition assignment to sparse matrices.
488  //**********************************************************************************************
489 
490  //**Subtraction assignment to dense matrices****************************************************
503  template< typename MT // Type of the target dense matrix
504  , bool SO > // Storage order of the target dense matrix
505  friend inline void subAssign( DenseMatrix<MT,SO>& lhs, const SMatTSMatAddExpr& rhs )
506  {
507  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
508  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
509 
510  subAssign( ~lhs, rhs.lhs_ );
511  subAssign( ~lhs, rhs.rhs_ );
512  }
514  //**********************************************************************************************
515 
516  //**Subtraction assignment to sparse matrices***************************************************
517  // No special implementation for the subtraction assignment to sparse matrices.
518  //**********************************************************************************************
519 
520  //**Multiplication assignment to dense matrices*************************************************
521  // No special implementation for the multiplication assignment to dense matrices.
522  //**********************************************************************************************
523 
524  //**Multiplication assignment to sparse matrices************************************************
525  // No special implementation for the multiplication assignment to sparse matrices.
526  //**********************************************************************************************
527 
528  //**Compile time checks*************************************************************************
535  //**********************************************************************************************
536 };
537 //*************************************************************************************************
538 
539 
540 
541 
542 //=================================================================================================
543 //
544 // GLOBAL BINARY ARITHMETIC OPERATORS
545 //
546 //=================================================================================================
547 
548 //*************************************************************************************************
577 template< typename T1 // Type of the left-hand side sparse matrix
578  , typename T2 > // Type of the right-hand side sparse matrix
579 inline const SMatTSMatAddExpr<T1,T2>
581 {
582  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
583  throw std::invalid_argument( "Matrix sizes do not match" );
584 
585  return SMatTSMatAddExpr<T1,T2>( ~lhs, ~rhs );
586 }
587 //*************************************************************************************************
588 
589 
590 //*************************************************************************************************
619 template< typename T1 // Type of the left-hand side sparse matrix
620  , typename T2 > // Type of the right-hand side sparse matrix
621 inline const SMatTSMatAddExpr<T2,T1>
623 {
624  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
625  throw std::invalid_argument( "Matrix sizes do not match" );
626 
627  return SMatTSMatAddExpr<T2,T1>( ~rhs, ~lhs );
628 }
629 //*************************************************************************************************
630 
631 } // namespace blaze
632 
633 #endif