All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DVecScalarDivExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_DVECSCALARDIVEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_DVECSCALARDIVEXPR_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
46 #include <blaze/util/Assert.h>
51 #include <blaze/util/EnableIf.h>
52 #include <blaze/util/InvalidType.h>
54 #include <blaze/util/SelectType.h>
55 #include <blaze/util/Types.h>
58 
59 
60 namespace blaze {
61 
62 //=================================================================================================
63 //
64 // CLASS DVECSCALARDIVEXPRHELPER
65 //
66 //=================================================================================================
67 
68 //*************************************************************************************************
75 template< typename VT // Type of the left-hand side dense vector
76  , typename ST // Type of the right-hand side scalar value
77  , bool TF > // Transpose flag
79 {
80  public:
81  //**Type definitions****************************************************************************
84  //**********************************************************************************************
85 
86  //**********************************************************************************************
88  enum { value = IsFloatingPoint<ScalarType>::value };
89  //**********************************************************************************************
90 
91  //**Type definitions****************************************************************************
93  typedef typename SelectType< value,
96  //**********************************************************************************************
97 
98  private:
99  //**Compile time checks*************************************************************************
106  //**********************************************************************************************
107 };
108 //*************************************************************************************************
109 
110 
111 
112 
113 //=================================================================================================
114 //
115 // CLASS DVECSCALARDIVEXPR
116 //
117 //=================================================================================================
118 
119 //*************************************************************************************************
126 template< typename VT // Type of the left-hand side dense vector
127  , typename ST // Type of the right-hand side scalar value
128  , bool TF > // Transpose flag
129 class DVecScalarDivExpr : public DenseVector< DVecScalarDivExpr<VT,ST,TF>, TF >
130  , private Expression
131  , private Computation
132 {
133  private:
134  //**Type definitions****************************************************************************
135  typedef typename VT::ResultType RT;
136  typedef typename VT::ReturnType RN;
137  typedef typename VT::CompositeType CT;
138  //**********************************************************************************************
139 
140  //**Return type evaluation**********************************************************************
142 
147  enum { returnExpr = !IsTemporary<RN>::value };
148 
151  //**********************************************************************************************
152 
153  //**Evaluation strategy*************************************************************************
155 
161  enum { useAssign = RequiresEvaluation<VT>::value };
162 
164 
165  template< typename VT2 >
166  struct UseAssign {
167  enum { value = useAssign };
168  };
170  //**********************************************************************************************
171 
172  public:
173  //**Type definitions****************************************************************************
176  typedef typename ResultType::TransposeType TransposeType;
177  typedef typename ResultType::ElementType ElementType;
179 
182 
185 
187  typedef typename SelectType< IsExpression<VT>::value, const VT, const VT& >::Type LeftOperand;
188 
191  //**********************************************************************************************
192 
193  //**Compilation flags***************************************************************************
195  enum { vectorizable = 0 };
196  //**********************************************************************************************
197 
198  //**Constructor*********************************************************************************
204  explicit inline DVecScalarDivExpr( const VT& vector, ST scalar )
205  : vector_( vector ) // Left-hand side dense vector of the division expression
206  , scalar_( scalar ) // Right-hand side scalar of the division expression
207  {}
208  //**********************************************************************************************
209 
210  //**Subscript operator**************************************************************************
216  inline ReturnType operator[]( size_t index ) const {
217  BLAZE_INTERNAL_ASSERT( index < vector_.size(), "Invalid vector access index" );
218  return vector_[index] / scalar_;
219  }
220  //**********************************************************************************************
221 
222  //**Size function*******************************************************************************
227  inline size_t size() const {
228  return vector_.size();
229  }
230  //**********************************************************************************************
231 
232  //**Left operand access*************************************************************************
237  inline LeftOperand leftOperand() const {
238  return vector_;
239  }
240  //**********************************************************************************************
241 
242  //**Right operand access************************************************************************
247  inline RightOperand rightOperand() const {
248  return scalar_;
249  }
250  //**********************************************************************************************
251 
252  //**********************************************************************************************
258  template< typename T >
259  inline bool canAlias( const T* alias ) const {
260  return vector_.canAlias( alias );
261  }
262  //**********************************************************************************************
263 
264  //**********************************************************************************************
270  template< typename T >
271  inline bool isAliased( const T* alias ) const {
272  return vector_.isAliased( alias );
273  }
274  //**********************************************************************************************
275 
276  private:
277  //**Member variables****************************************************************************
280  //**********************************************************************************************
281 
282  //**Assignment to dense vectors*****************************************************************
296  template< typename VT2 > // Type of the target dense vector
297  friend inline typename EnableIf< UseAssign<VT2> >::Type
298  assign( DenseVector<VT2,TF>& lhs, const DVecScalarDivExpr& rhs )
299  {
301 
302  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
303 
304  assign( ~lhs, rhs.vector_ );
305  (~lhs) /= rhs.scalar_;
306  }
308  //**********************************************************************************************
309 
310  //**Assignment to sparse vectors****************************************************************
324  template< typename VT2 > // Type of the target sparse vector
325  friend inline typename EnableIf< UseAssign<VT2> >::Type
326  assign( SparseVector<VT2,TF>& lhs, const DVecScalarDivExpr& rhs )
327  {
329 
330  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
331 
332  assign( ~lhs, rhs.vector_ );
333  (~lhs) /= rhs.scalar_;
334  }
336  //**********************************************************************************************
337 
338  //**Addition assignment to dense vectors********************************************************
352  template< typename VT2 > // Type of the target dense vector
353  friend inline typename EnableIf< UseAssign<VT2> >::Type
354  addAssign( DenseVector<VT2,TF>& lhs, const DVecScalarDivExpr& rhs )
355  {
357 
360  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
361 
362  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
363 
364  const ResultType tmp( rhs );
365  addAssign( ~lhs, tmp );
366  }
368  //**********************************************************************************************
369 
370  //**Addition assignment to sparse vectors*******************************************************
371  // No special implementation for the addition assignment to sparse vectors.
372  //**********************************************************************************************
373 
374  //**Subtraction assignment to dense vectors*****************************************************
388  template< typename VT2 > // Type of the target dense vector
389  friend inline typename EnableIf< UseAssign<VT2> >::Type
390  subAssign( DenseVector<VT2,TF>& lhs, const DVecScalarDivExpr& rhs )
391  {
393 
396  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
397 
398  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
399 
400  const ResultType tmp( rhs );
401  subAssign( ~lhs, tmp );
402  }
404  //**********************************************************************************************
405 
406  //**Subtraction assignment to sparse vectors****************************************************
407  // No special implementation for the subtraction assignment to sparse vectors.
408  //**********************************************************************************************
409 
410  //**Multiplication assignment to dense vectors**************************************************
424  template< typename VT2 > // Type of the target dense vector
425  friend inline typename EnableIf< UseAssign<VT2> >::Type
426  multAssign( DenseVector<VT2,TF>& lhs, const DVecScalarDivExpr& rhs )
427  {
429 
432  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
433 
434  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
435 
436  const ResultType tmp( rhs );
437  multAssign( ~lhs, tmp );
438  }
440  //**********************************************************************************************
441 
442  //**Multiplication assignment to sparse vectors*************************************************
443  // No special implementation for the multiplication assignment to sparse vectors.
444  //**********************************************************************************************
445 
446  //**Multiplication assignment to sparse vectors*************************************************
447  // No special implementation for the multiplication assignment to sparse vectors.
448  //**********************************************************************************************
449 
450  //**Compile time checks*************************************************************************
459  //**********************************************************************************************
460 };
461 //*************************************************************************************************
462 
463 
464 
465 
466 //=================================================================================================
467 //
468 // GLOBAL BINARY ARITHMETIC OPERATORS
469 //
470 //=================================================================================================
471 
472 //*************************************************************************************************
496 template< typename T1 // Type of the left-hand side dense vector
497  , typename T2 // Type of the right-hand side scalar
498  , bool TF > // Transpose flag
499 inline const typename EnableIf< IsNumeric<T2>,
500  typename DVecScalarDivExprHelper<T1,T2,TF>::Type >::Type
501  operator/( const DenseVector<T1,TF>& vec, T2 scalar )
502 {
504 
505  BLAZE_USER_ASSERT( scalar != T2(0), "Division by zero detected" );
506 
507  typedef DVecScalarDivExprHelper<T1,T2,TF> Helper;
508  typedef typename Helper::ScalarType ScalarType;
509 
510  if( Helper::value ) {
511  return typename Helper::Type( ~vec, ScalarType(1)/ScalarType(scalar) );
512  }
513  else {
514  return typename Helper::Type( ~vec, scalar );
515  }
516 }
517 //*************************************************************************************************
518 
519 
520 
521 
522 //=================================================================================================
523 //
524 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
525 //
526 //=================================================================================================
527 
528 //*************************************************************************************************
541 template< typename VT // Type of the dense vector of the left-hand side expression
542  , typename ST1 // Type of the scalar of the left-hand side expression
543  , bool TF // Transpose flag of the dense vector
544  , typename ST2 > // Type of the right-hand side scalar
545 inline const typename EnableIf< IsFloatingPoint< typename DivTrait<ST2,ST1>::Type >
546  , typename MultExprTrait< DVecScalarDivExpr<VT,ST1,TF>, ST2 >::Type >::Type
547  operator*( const DVecScalarDivExpr<VT,ST1,TF>& vec, ST2 scalar )
548 {
550 
551  return vec.leftOperand() * ( scalar / vec.rightOperand() );
552 }
554 //*************************************************************************************************
555 
556 
557 //*************************************************************************************************
570 template< typename ST1 // Type of the left-hand side scalar
571  , typename VT // Type of the dense vector of the right-hand side expression
572  , typename ST2 // Type of the scalar of the right-hand side expression
573  , bool TF > // Transpose flag of the dense vector
574 inline const typename EnableIf< IsFloatingPoint< typename DivTrait<ST1,ST2>::Type >
575  , typename MultExprTrait< ST1, DVecScalarDivExpr<VT,ST2,TF> >::Type >::Type
576  operator*( ST1 scalar, const DVecScalarDivExpr<VT,ST2,TF>& vec )
577 {
579 
580  return vec.leftOperand() * ( scalar / vec.rightOperand() );
581 }
583 //*************************************************************************************************
584 
585 
586 //*************************************************************************************************
599 template< typename VT // Type of the dense vector of the left-hand side expression
600  , typename ST1 // Type of the scalar of the left-hand side expression
601  , bool TF // Transpose flag of the dense vector
602  , typename ST2 > // Type of the right-hand side scalar
603 inline const typename EnableIf< IsNumeric<ST2>
604  , typename DVecScalarDivExprHelper<VT,typename MultTrait<ST1,ST2>::Type,TF>::Type >::Type
605  operator/( const DVecScalarDivExpr<VT,ST1,TF>& vec, ST2 scalar )
606 {
608 
609  BLAZE_USER_ASSERT( scalar != ST2(0), "Division by zero detected" );
610 
611  typedef typename MultTrait<ST1,ST2>::Type MultType;
612  typedef DVecScalarDivExprHelper<VT,MultType,TF> Helper;
613 
614  if( Helper::value ) {
615  return typename Helper::Type( vec.leftOperand(), MultType(1)/( vec.rightOperand() * scalar ) );
616  }
617  else {
618  return typename Helper::Type( vec.leftOperand(), vec.rightOperand() * scalar );
619  }
620 }
622 //*************************************************************************************************
623 
624 
625 
626 
627 //=================================================================================================
628 //
629 // DVECSCALARMULTEXPRTRAIT SPECIALIZATIONS
630 //
631 //=================================================================================================
632 
633 //*************************************************************************************************
635 template< typename VT, typename ST1, typename ST2 >
636 struct DVecScalarMultExprTrait< DVecScalarDivExpr<VT,ST1,false>, ST2 >
637 {
638  private:
639  //**********************************************************************************************
640  enum { condition = IsFloatingPoint<typename DivTrait<ST1,ST2>::Type>::value };
641  //**********************************************************************************************
642 
643  //**********************************************************************************************
644  typedef typename DVecScalarMultExprTrait<VT,typename DivTrait<ST1,ST2>::Type>::Type T1;
645  typedef DVecScalarMultExpr< DVecScalarDivExpr<VT,ST1,false>, ST2, false > T2;
646  //**********************************************************************************************
647 
648  public:
649  //**********************************************************************************************
650  typedef typename SelectType< IsDenseVector<VT>::value && !IsTransposeVector<VT>::value &&
651  IsNumeric<ST1>::value && IsNumeric<ST2>::value
652  , typename SelectType<condition,T1,T2>::Type
653  , INVALID_TYPE >::Type Type;
654  //**********************************************************************************************
655 };
657 //*************************************************************************************************
658 
659 
660 
661 
662 //=================================================================================================
663 //
664 // TDVECSCALARMULTEXPRTRAIT SPECIALIZATIONS
665 //
666 //=================================================================================================
667 
668 //*************************************************************************************************
670 template< typename VT, typename ST1, typename ST2 >
671 struct TDVecScalarMultExprTrait< DVecScalarDivExpr<VT,ST1,true>, ST2 >
672 {
673  private:
674  //**********************************************************************************************
675  enum { condition = IsFloatingPoint<typename DivTrait<ST1,ST2>::Type>::value };
676  //**********************************************************************************************
677 
678  //**********************************************************************************************
679  typedef typename DVecScalarMultExprTrait<VT,typename DivTrait<ST1,ST2>::Type>::Type T1;
680  typedef DVecScalarMultExpr< DVecScalarDivExpr<VT,ST1,true>, ST2, true > T2;
681  //**********************************************************************************************
682 
683  public:
684  //**********************************************************************************************
685  typedef typename SelectType< IsDenseVector<VT>::value && IsTransposeVector<VT>::value &&
686  IsNumeric<ST1>::value && IsNumeric<ST2>::value
687  , typename SelectType<condition,T1,T2>::Type
688  , INVALID_TYPE >::Type Type;
689  //**********************************************************************************************
690 };
692 //*************************************************************************************************
693 
694 } // namespace blaze
695 
696 #endif