All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DMatScalarDivExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_DMATSCALARDIVEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_DMATSCALARDIVEXPR_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
49 #include <blaze/util/Assert.h>
54 #include <blaze/util/EnableIf.h>
55 #include <blaze/util/InvalidType.h>
57 #include <blaze/util/SelectType.h>
58 #include <blaze/util/Types.h>
61 
62 
63 namespace blaze {
64 
65 //=================================================================================================
66 //
67 // CLASS DMATSCALARDIVEXPRHELPER
68 //
69 //=================================================================================================
70 
71 //*************************************************************************************************
78 template< typename MT // Type of the left-hand side dense matrix
79  , typename ST // Type of the right-hand side scalar value
80  , bool SO > // Storage order
82 {
83  public:
84  //**Type definitions****************************************************************************
87  //**********************************************************************************************
88 
89  //**********************************************************************************************
91  enum { value = IsFloatingPoint<ScalarType>::value };
92  //**********************************************************************************************
93 
94  //**Type definitions****************************************************************************
96  typedef typename SelectType< value,
99  //**********************************************************************************************
100 
101  private:
102  //**Compile time checks*************************************************************************
109  //**********************************************************************************************
110 };
111 //*************************************************************************************************
112 
113 
114 
115 
116 //=================================================================================================
117 //
118 // CLASS DMATSCALARDIVEXPR
119 //
120 //=================================================================================================
121 
122 //*************************************************************************************************
129 template< typename MT // Type of the left-hand side dense matrix
130  , typename ST // Type of the right-hand side scalar value
131  , bool SO > // Storage order
132 class DMatScalarDivExpr : public DenseMatrix< DMatScalarDivExpr<MT,ST,SO>, SO >
133  , private Expression
134  , private Computation
135 {
136  private:
137  //**Type definitions****************************************************************************
138  typedef typename MT::ResultType RT;
139  typedef typename MT::ReturnType RN;
140  typedef typename MT::CompositeType CT;
141  //**********************************************************************************************
142 
143  //**Return type evaluation**********************************************************************
145 
150  enum { returnExpr = !IsTemporary<RN>::value };
151 
154  //**********************************************************************************************
155 
156  //**Evaluation strategy*************************************************************************
158 
164  enum { useAssign = RequiresEvaluation<MT>::value };
165 
167 
168  template< typename MT2 >
169  struct UseAssign {
170  enum { value = useAssign };
171  };
173  //**********************************************************************************************
174 
175  public:
176  //**Type definitions****************************************************************************
179  typedef typename ResultType::OppositeType OppositeType;
180  typedef typename ResultType::TransposeType TransposeType;
181  typedef typename ResultType::ElementType ElementType;
182 
185 
188 
190  typedef typename SelectType< IsExpression<MT>::value, const MT, const MT& >::Type LeftOperand;
191 
194  //**********************************************************************************************
195 
196  //**Compilation flags***************************************************************************
198  enum { vectorizable = 0 };
199  //**********************************************************************************************
200 
201  //**Constructor*********************************************************************************
207  explicit inline DMatScalarDivExpr( const MT& matrix, ST scalar )
208  : matrix_( matrix ) // Left-hand side dense matrix of the division expression
209  , scalar_( scalar ) // Right-hand side scalar of the division expression
210  {}
211  //**********************************************************************************************
212 
213  //**Access operator*****************************************************************************
220  inline ReturnType operator()( size_t i, size_t j ) const {
221  BLAZE_INTERNAL_ASSERT( i < matrix_.rows() , "Invalid row access index" );
222  BLAZE_INTERNAL_ASSERT( j < matrix_.columns(), "Invalid column access index" );
223  return matrix_(i,j) / scalar_;
224  }
225  //**********************************************************************************************
226 
227  //**Rows function*******************************************************************************
232  inline size_t rows() const {
233  return matrix_.rows();
234  }
235  //**********************************************************************************************
236 
237  //**Columns function****************************************************************************
242  inline size_t columns() const {
243  return matrix_.columns();
244  }
245  //**********************************************************************************************
246 
247  //**Left operand access*************************************************************************
252  inline LeftOperand leftOperand() const {
253  return matrix_;
254  }
255  //**********************************************************************************************
256 
257  //**Right operand access************************************************************************
262  inline RightOperand rightOperand() const {
263  return scalar_;
264  }
265  //**********************************************************************************************
266 
267  //**********************************************************************************************
273  template< typename T >
274  inline bool canAlias( const T* alias ) const {
275  return matrix_.canAlias( alias );
276  }
277  //**********************************************************************************************
278 
279  //**********************************************************************************************
285  template< typename T >
286  inline bool isAliased( const T* alias ) const {
287  return matrix_.isAliased( alias );
288  }
289  //**********************************************************************************************
290 
291  private:
292  //**Member variables****************************************************************************
295  //**********************************************************************************************
296 
297  //**Assignment to row-major dense matrices******************************************************
311  template< typename MT2 > // Type of the target dense matrix
312  friend inline typename EnableIf< UseAssign<MT2> >::Type
314  {
316 
317  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
318  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
319 
320  assign( ~lhs, rhs.matrix_ );
321  (~lhs) /= rhs.scalar_;
322  }
324  //**********************************************************************************************
325 
326  //**Assignment to column-major dense matrices***************************************************
340  template< typename MT2 > // Type of the target dense matrix
341  friend inline typename EnableIf< UseAssign<MT2> >::Type
343  {
345 
346  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
347  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
348 
349  assign( ~lhs, rhs.matrix_ );
350  (~lhs) /= rhs.scalar_;
351  }
353  //**********************************************************************************************
354 
355  //**Assignment to row-major sparse matrices*****************************************************
369  template< typename MT2 > // Type of the target sparse matrix
370  friend inline typename EnableIf< UseAssign<MT2> >::Type
371  assign( SparseMatrix<MT2,false>& lhs, const DMatScalarDivExpr& rhs )
372  {
374 
375  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
376  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
377 
378  assign( ~lhs, rhs.matrix_ );
379 
380  for( size_t i=0UL; i<(~lhs).rows(); ++i )
381  {
382  typename MT2::Iterator element( (~lhs).begin(i) );
383  const typename MT2::Iterator end( (~lhs).end(i) );
384 
385  for( ; element!=end; ++element )
386  element->value() /= rhs.scalar_;
387  }
388  }
390  //**********************************************************************************************
391 
392  //**Assignment to column-major sparse matrices**************************************************
406  template< typename MT2 > // Type of the target sparse matrix
407  friend inline typename EnableIf< UseAssign<MT2> >::Type
408  assign( SparseMatrix<MT2,true>& lhs, const DMatScalarDivExpr& rhs )
409  {
411 
412  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
413  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
414 
415  assign( ~lhs, rhs.matrix_ );
416 
417  for( size_t j=0UL; j<(~lhs).columns(); ++j )
418  {
419  typename MT2::Iterator element( (~lhs).begin(j) );
420  const typename MT2::Iterator end( (~lhs).end(j) );
421 
422  for( ; element!=end; ++element )
423  element->value() /= rhs.scalar_;
424  }
425  }
427  //**********************************************************************************************
428 
429  //**Addition assignment to dense matrices*******************************************************
443  template< typename MT2 // Type of the target dense matrix
444  , bool SO2 > // Storage order of the target dense matrix
445  friend inline typename EnableIf< UseAssign<MT2> >::Type
446  addAssign( DenseMatrix<MT2,SO2>& lhs, const DMatScalarDivExpr& rhs )
447  {
449 
452  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
453 
454  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
455  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
456 
457  const ResultType tmp( rhs );
458  addAssign( ~lhs, tmp );
459  }
461  //**********************************************************************************************
462 
463  //**Addition assignment to sparse matrices******************************************************
464  // No special implementation for the addition assignment to sparse matrices.
465  //**********************************************************************************************
466 
467  //**Subtraction assignment to dense matrices****************************************************
481  template< typename MT2 // Type of the target dense matrix
482  , bool SO2 > // Storage order of the target dense matrix
483  friend inline typename EnableIf< UseAssign<MT2> >::Type
484  subAssign( DenseMatrix<MT2,SO2>& lhs, const DMatScalarDivExpr& rhs )
485  {
487 
490  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
491 
492  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
493  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
494 
495  const ResultType tmp( rhs );
496  subAssign( ~lhs, tmp );
497  }
499  //**********************************************************************************************
500 
501  //**Subtraction assignment to sparse matrices***************************************************
502  // No special implementation for the subtraction assignment to sparse matrices.
503  //**********************************************************************************************
504 
505  //**Multiplication assignment to dense matrices*************************************************
506  // No special implementation for the multiplication assignment to dense matrices.
507  //**********************************************************************************************
508 
509  //**Multiplication assignment to sparse matrices************************************************
510  // No special implementation for the multiplication assignment to sparse matrices.
511  //**********************************************************************************************
512 
513  //**Compile time checks*************************************************************************
522  //**********************************************************************************************
523 };
524 //*************************************************************************************************
525 
526 
527 
528 
529 //=================================================================================================
530 //
531 // GLOBAL BINARY ARITHMETIC OPERATORS
532 //
533 //=================================================================================================
534 
535 //*************************************************************************************************
557 template< typename T1 // Type of the left-hand side dense matrix
558  , bool SO // Storage order of the left-hand side dense matrix
559  , typename T2 > // Type of the right-hand side scalar
560 inline const typename EnableIf< IsNumeric<T2>
561  , typename DMatScalarDivExprHelper<T1,T2,SO>::Type >::Type
562  operator/( const DenseMatrix<T1,SO>& mat, T2 scalar )
563 {
565 
566  BLAZE_USER_ASSERT( scalar != T2(0), "Division by zero detected" );
567 
568  typedef DMatScalarDivExprHelper<T1,T2,SO> Helper;
569  typedef typename Helper::ScalarType ScalarType;
570 
571  if( Helper::value ) {
572  return typename Helper::Type( ~mat, ScalarType(1)/ScalarType(scalar) );
573  }
574  else {
575  return typename Helper::Type( ~mat, scalar );
576  }
577 }
578 //*************************************************************************************************
579 
580 
581 
582 
583 //=================================================================================================
584 //
585 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
586 //
587 //=================================================================================================
588 
589 //*************************************************************************************************
602 template< typename MT // Type of the dense matrix of the left-hand side expression
603  , typename ST1 // Type of the scalar of the left-hand side expression
604  , bool SO // Storage order of the dense matrix
605  , typename ST2 > // Type of the right-hand side scalar
606 inline const typename EnableIf< IsFloatingPoint< typename DivTrait<ST2,ST1>::Type >
607  , typename MultExprTrait< DMatScalarDivExpr<MT,ST1,SO>, ST2 >::Type >::Type
608  operator*( const DMatScalarDivExpr<MT,ST1,SO>& mat, ST2 scalar )
609 {
611 
612  return mat.leftOperand() * ( scalar / mat.rightOperand() );
613 }
615 //*************************************************************************************************
616 
617 
618 //*************************************************************************************************
631 template< typename ST1 // Type of the left-hand side scalar
632  , typename MT // Type of the dense matrix of the right-hand side expression
633  , typename ST2 // Type of the scalar of the right-hand side expression
634  , bool SO > // Storage order of the dense matrix
635 inline const typename EnableIf< IsFloatingPoint< typename DivTrait<ST1,ST2>::Type >
636  , typename MultExprTrait< ST1, DMatScalarDivExpr<MT,ST2,SO> >::Type >::Type
637  operator*( ST1 scalar, const DMatScalarDivExpr<MT,ST2,SO>& mat )
638 {
640 
641  return mat.leftOperand() * ( scalar / mat.rightOperand() );
642 }
644 //*************************************************************************************************
645 
646 
647 //*************************************************************************************************
660 template< typename MT // Type of the dense matrix of the left-hand side expression
661  , typename ST1 // Type of the scalar of the left-hand side expression
662  , bool SO // Storage order of the dense matrix
663  , typename ST2 > // Type of the right-hand side scalar
664 inline const typename EnableIf< IsNumeric<ST2>
665  , typename DMatScalarDivExprHelper<MT,typename MultTrait<ST1,ST2>::Type,SO>::Type >::Type
666  operator/( const DMatScalarDivExpr<MT,ST1,SO>& mat, ST2 scalar )
667 {
669 
670  BLAZE_USER_ASSERT( scalar != ST2(0), "Division by zero detected" );
671 
672  typedef typename MultTrait<ST1,ST2>::Type MultType;
673  typedef DMatScalarDivExprHelper<MT,MultType,SO> Helper;
674 
675  if( Helper::value ) {
676  return typename Helper::Type( mat.leftOperand(), MultType(1)/( mat.rightOperand() * scalar ) );
677  }
678  else {
679  return typename Helper::Type( mat.leftOperand(), mat.rightOperand() * scalar );
680  }
681 }
683 //*************************************************************************************************
684 
685 
686 
687 
688 //=================================================================================================
689 //
690 // GLOBAL OPERATORS
691 //
692 //=================================================================================================
693 
694 //*************************************************************************************************
706 template< typename MT // Type of the left-hand side dense matrix
707  , typename ST // Type of the right-hand side scalar value
708  , bool SO > // Storage order
709 inline typename RowExprTrait< DMatScalarDivExpr<MT,ST,SO> >::Type
710  row( const DMatScalarDivExpr<MT,ST,SO>& dm, size_t index )
711 {
713 
714  return row( dm.leftOperand(), index ) / dm.rightOperand();
715 }
717 //*************************************************************************************************
718 
719 
720 //*************************************************************************************************
732 template< typename MT // Type of the left-hand side dense matrix
733  , typename ST // Type of the right-hand side scalar value
734  , bool SO > // Storage order
735 inline typename ColumnExprTrait< DMatScalarDivExpr<MT,ST,SO> >::Type
736  column( const DMatScalarDivExpr<MT,ST,SO>& dm, size_t index )
737 {
739 
740  return column( dm.leftOperand(), index ) / dm.rightOperand();
741 }
743 //*************************************************************************************************
744 
745 
746 
747 
748 //=================================================================================================
749 //
750 // DMATSCALARMULTEXPRTRAIT SPECIALIZATIONS
751 //
752 //=================================================================================================
753 
754 //*************************************************************************************************
756 template< typename MT, typename ST1, typename ST2 >
757 struct DMatScalarMultExprTrait< DMatScalarDivExpr<MT,ST1,false>, ST2 >
758 {
759  private:
760  //**********************************************************************************************
761  enum { condition = IsFloatingPoint<typename DivTrait<ST1,ST2>::Type>::value };
762  //**********************************************************************************************
763 
764  //**********************************************************************************************
765  typedef typename DMatScalarMultExprTrait<MT,typename DivTrait<ST1,ST2>::Type>::Type T1;
766  typedef DMatScalarMultExpr< DMatScalarDivExpr<MT,ST1,false>, ST2, false > T2;
767  //**********************************************************************************************
768 
769  public:
770  //**********************************************************************************************
771  typedef typename SelectType< IsDenseMatrix<MT>::value && IsRowMajorMatrix<MT>::value &&
772  IsNumeric<ST1>::value && IsNumeric<ST2>::value
773  , typename SelectType<condition,T1,T2>::Type
774  , INVALID_TYPE >::Type Type;
775  //**********************************************************************************************
776 };
778 //*************************************************************************************************
779 
780 
781 
782 
783 //=================================================================================================
784 //
785 // TDMATSCALARMULTEXPRTRAIT SPECIALIZATIONS
786 //
787 //=================================================================================================
788 
789 //*************************************************************************************************
791 template< typename MT, typename ST1, typename ST2 >
792 struct TDMatScalarMultExprTrait< DMatScalarDivExpr<MT,ST1,true>, ST2 >
793 {
794  private:
795  //**********************************************************************************************
796  enum { condition = IsFloatingPoint<typename DivTrait<ST1,ST2>::Type>::value };
797  //**********************************************************************************************
798 
799  //**********************************************************************************************
800  typedef typename DMatScalarMultExprTrait<MT,typename DivTrait<ST1,ST2>::Type>::Type T1;
801  typedef DMatScalarMultExpr< DMatScalarDivExpr<MT,ST1,true>, ST2, true > T2;
802  //**********************************************************************************************
803 
804  public:
805  //**********************************************************************************************
806  typedef typename SelectType< IsDenseMatrix<MT>::value && IsColumnMajorMatrix<MT>::value &&
807  IsNumeric<ST1>::value && IsNumeric<ST2>::value
808  , typename SelectType<condition,T1,T2>::Type
809  , INVALID_TYPE >::Type Type;
810  //**********************************************************************************************
811 };
813 //*************************************************************************************************
814 
815 
816 
817 
818 //=================================================================================================
819 //
820 // ROWEXPRTRAIT SPECIALIZATIONS
821 //
822 //=================================================================================================
823 
824 //*************************************************************************************************
826 template< typename MT, typename ST, bool SO >
827 struct RowExprTrait< DMatScalarDivExpr<MT,ST,SO> >
828 {
829  public:
830  //**********************************************************************************************
831  typedef typename DivExprTrait< typename RowExprTrait<const MT>::Type, ST >::Type Type;
832  //**********************************************************************************************
833 };
835 //*************************************************************************************************
836 
837 
838 
839 
840 //=================================================================================================
841 //
842 // COLUMNEXPRTRAIT SPECIALIZATIONS
843 //
844 //=================================================================================================
845 
846 //*************************************************************************************************
848 template< typename MT, typename ST, bool SO >
849 struct ColumnExprTrait< DMatScalarDivExpr<MT,ST,SO> >
850 {
851  public:
852  //**********************************************************************************************
853  typedef typename DivExprTrait< typename ColumnExprTrait<const MT>::Type, ST >::Type Type;
854  //**********************************************************************************************
855 };
857 //*************************************************************************************************
858 
859 } // namespace blaze
860 
861 #endif