All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TSMatSMatSubExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_TSMATSMATSUBEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_TSMATSMATSUBEXPR_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 TSMATSMATSUBEXPR
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 TSMatSMatSubExpr : public SparseMatrix< TSMatSMatSubExpr<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 TSMatSMatSubExpr( const MT1& lhs, const MT2& rhs )
129  : lhs_( lhs ) // Left-hand side sparse matrix of the subtraction expression
130  , rhs_( rhs ) // Right-hand side sparse matrix of the subtraction 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 TSMatSMatSubExpr& 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  subAssign( ~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 i=0UL; i<(~lhs).rows(); ++i ) {
269  const RightIterator end( B.end(i) );
270  for( RightIterator element=B.begin(i); element!=end; ++element ) {
271  if( isDefault( (~lhs)(i,element->index()) ) )
272  (~lhs)(i,element->index()) = -element->value();
273  else
274  (~lhs)(i,element->index()) -= element->value();
275  }
276  }
277  }
278  }
280  //**********************************************************************************************
281 
282  //**Assignment to row-major sparse matrices*****************************************************
295  template< typename MT > // Type of the target sparse matrix
296  friend inline void assign( SparseMatrix<MT,false>& lhs, const TSMatSMatSubExpr& rhs )
297  {
298  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
299  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
300 
301  typedef typename RT1::OppositeType::ConstIterator LeftIterator;
302  typedef typename boost::remove_reference<CT2>::type::ConstIterator RightIterator;
303 
304  // Evaluation of the left-hand side sparse matrix operand
305  const typename RT1::OppositeType A( rhs.lhs_ );
306 
307  // Evaluation of the right-hand side sparse matrix operand
308  CT2 B( rhs.rhs_ );
309 
310  BLAZE_INTERNAL_ASSERT( A.rows() == rhs.lhs_.rows() , "Invalid number of rows" );
311  BLAZE_INTERNAL_ASSERT( A.columns() == rhs.lhs_.columns(), "Invalid number of columns" );
312  BLAZE_INTERNAL_ASSERT( B.rows() == rhs.rhs_.rows() , "Invalid number of rows" );
313  BLAZE_INTERNAL_ASSERT( B.columns() == rhs.rhs_.columns(), "Invalid number of columns" );
314  BLAZE_INTERNAL_ASSERT( A.rows() == (~lhs).rows() , "Invalid number of rows" );
315  BLAZE_INTERNAL_ASSERT( A.columns() == (~lhs).columns() , "Invalid number of columns" );
316 
317  for( size_t i=0UL; i<(~lhs).rows(); ++i )
318  {
319  const LeftIterator lend( A.end(i) );
320  const RightIterator rend( B.end(i) );
321 
322  LeftIterator l( A.begin(i) );
323  RightIterator r( B.begin(i) );
324  size_t nonzeros( A.nonZeros(i) + B.nonZeros(i) );
325 
326  for( ; l!=lend && r!=rend; ++l ) {
327  while( r->index() < l->index() && ++r != rend ) {}
328  if( r!=rend && l->index() == r->index() ) {
329  --nonzeros;
330  ++r;
331  }
332  }
333 
334  BLAZE_INTERNAL_ASSERT( nonzeros <= A.columns(), "Invalid number of non-zero elements predicted" );
335 
336  (~lhs).reserve( i, nonzeros );
337 
338  l = A.begin(i);
339  r = B.begin(i);
340 
341  while( l != lend && r != rend )
342  {
343  if( l->index() < r->index() ) {
344  (~lhs).append( i, l->index(), l->value() );
345  ++l;
346  }
347  else if( l->index() > r->index() ) {
348  (~lhs).append( i, r->index(), -r->value() );
349  ++r;
350  }
351  else {
352  (~lhs).append( i, l->index(), l->value()-r->value() );
353  ++l;
354  ++r;
355  }
356  }
357 
358  while( l != lend ) {
359  (~lhs).append( i, l->index(), l->value() );
360  ++l;
361  }
362 
363  while( r != rend ) {
364  (~lhs).append( i, r->index(), -r->value() );
365  ++r;
366  }
367  }
368  }
370  //**********************************************************************************************
371 
372  //**Assignment to column-major sparse matrices**************************************************
385  template< typename MT > // Type of the target sparse matrix
386  friend inline void assign( SparseMatrix<MT,true>& lhs, const TSMatSMatSubExpr& rhs )
387  {
388  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
389  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
390 
391  typedef typename boost::remove_reference<CT1>::type::ConstIterator LeftIterator;
392  typedef typename RT2::OppositeType::ConstIterator RightIterator;
393 
394  // Evaluation of the left-hand side sparse matrix operand
395  CT1 A( rhs.lhs_ );
396 
397  // Evaluation of the right-hand side sparse matrix operand
398  const typename RT2::OppositeType B( rhs.rhs_ );
399 
400  BLAZE_INTERNAL_ASSERT( A.rows() == rhs.lhs_.rows() , "Invalid number of rows" );
401  BLAZE_INTERNAL_ASSERT( A.columns() == rhs.lhs_.columns(), "Invalid number of columns" );
402  BLAZE_INTERNAL_ASSERT( B.rows() == rhs.rhs_.rows() , "Invalid number of rows" );
403  BLAZE_INTERNAL_ASSERT( B.columns() == rhs.rhs_.columns(), "Invalid number of columns" );
404  BLAZE_INTERNAL_ASSERT( A.rows() == (~lhs).rows() , "Invalid number of rows" );
405  BLAZE_INTERNAL_ASSERT( A.columns() == (~lhs).columns() , "Invalid number of columns" );
406 
407  for( size_t j=0UL; j<(~lhs).columns(); ++j )
408  {
409  const LeftIterator lend( A.end(j) );
410  const RightIterator rend( B.end(j) );
411 
412  LeftIterator l( A.begin(j) );
413  RightIterator r( B.begin(j) );
414  size_t nonzeros( A.nonZeros(j) + B.nonZeros(j) );
415 
416  for( ; l!=lend && r!=rend; ++l ) {
417  while( r->index() < l->index() && ++r != rend ) {}
418  if( r!=rend && l->index() == r->index() ) {
419  --nonzeros;
420  ++r;
421  }
422  }
423 
424  BLAZE_INTERNAL_ASSERT( nonzeros <= A.rows(), "Invalid number of non-zero elements predicted" );
425 
426  (~lhs).reserve( j, nonzeros );
427 
428  l = A.begin(j);
429  r = B.begin(j);
430 
431  while( l != lend && r != rend )
432  {
433  if( l->index() < r->index() ) {
434  (~lhs).append( l->index(), j, l->value() );
435  ++l;
436  }
437  else if( l->index() > r->index() ) {
438  (~lhs).append( r->index(), j, -r->value() );
439  ++r;
440  }
441  else {
442  (~lhs).append( l->index(), j, l->value()-r->value() );
443  ++l;
444  ++r;
445  }
446  }
447 
448  while( l != lend ) {
449  (~lhs).append( l->index(), j, l->value() );
450  ++l;
451  }
452 
453  while( r != rend ) {
454  (~lhs).append( r->index(), j, -r->value() );
455  ++r;
456  }
457  }
458  }
460  //**********************************************************************************************
461 
462  //**Addition assignment to dense matrices*******************************************************
475  template< typename MT // Type of the target dense matrix
476  , bool SO > // Storage order of the target dense matrix
477  friend inline void addAssign( DenseMatrix<MT,SO>& lhs, const TSMatSMatSubExpr& rhs )
478  {
479  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
480  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
481 
482  addAssign( ~lhs, rhs.lhs_ );
483  subAssign( ~lhs, rhs.rhs_ );
484  }
486  //**********************************************************************************************
487 
488  //**Addition assignment to sparse matrices******************************************************
489  // No special implementation for the addition assignment to sparse matrices.
490  //**********************************************************************************************
491 
492  //**Subtraction assignment to dense matrices****************************************************
505  template< typename MT // Type of the target dense matrix
506  , bool SO > // Storage order of the target dense matrix
507  friend inline void subAssign( DenseMatrix<MT,SO>& lhs, const TSMatSMatSubExpr& rhs )
508  {
509  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
510  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
511 
512  subAssign( ~lhs, rhs.lhs_ );
513  addAssign( ~lhs, rhs.rhs_ );
514  }
516  //**********************************************************************************************
517 
518  //**Subtraction assignment to sparse matrices***************************************************
519  // No special implementation for the subtraction assignment to sparse matrices.
520  //**********************************************************************************************
521 
522  //**Multiplication assignment to dense matrices*************************************************
523  // No special implementation for the multiplication assignment to dense matrices.
524  //**********************************************************************************************
525 
526  //**Multiplication assignment to sparse matrices************************************************
527  // No special implementation for the multiplication assignment to sparse matrices.
528  //**********************************************************************************************
529 
530  //**Compile time checks*************************************************************************
537  //**********************************************************************************************
538 };
539 //*************************************************************************************************
540 
541 
542 
543 
544 //=================================================================================================
545 //
546 // GLOBAL BINARY ARITHMETIC OPERATORS
547 //
548 //=================================================================================================
549 
550 //*************************************************************************************************
579 template< typename T1 // Type of the left-hand side sparse matrix
580  , typename T2 > // Type of the right-hand side sparse matrix
581 inline const TSMatSMatSubExpr<T1,T2>
583 {
584  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
585  throw std::invalid_argument( "Matrix sizes do not match" );
586 
587  return TSMatSMatSubExpr<T1,T2>( ~lhs, ~rhs );
588 }
589 //*************************************************************************************************
590 
591 } // namespace blaze
592 
593 #endif