All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DVecAbsExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_DVECABSEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_DVECABSEXPR_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <cmath>
41 #include <blaze/util/Assert.h>
43 #include <blaze/util/EnableIf.h>
45 #include <blaze/util/SelectType.h>
46 #include <blaze/util/Types.h>
47 
48 
49 namespace blaze {
50 
51 //=================================================================================================
52 //
53 // CLASS DVECABSEXPR
54 //
55 //=================================================================================================
56 
57 //*************************************************************************************************
64 template< typename VT // Type of the dense vector
65  , bool TF > // Transpose flag
66 class DVecAbsExpr : public DenseVector< DVecAbsExpr<VT,TF>, TF >
67  , private Expression
68  , private Computation
69 {
70  private:
71  //**Type definitions****************************************************************************
72  typedef typename VT::ResultType RT;
73  typedef typename VT::ReturnType RN;
74  typedef typename VT::CompositeType CT;
75  typedef typename VT::TransposeType TT;
76  typedef typename VT::ElementType ET;
77  //**********************************************************************************************
78 
79  //**Return type evaluation**********************************************************************
81 
86  enum { returnExpr = !IsTemporary<RN>::value };
87 
90  //**********************************************************************************************
91 
92  //**Evaluation strategy*************************************************************************
94 
100  enum { useAssign = RequiresEvaluation<VT>::value };
101 
103 
104  template< typename VT2 >
105  struct UseAssign {
106  enum { value = useAssign };
107  };
109  //**********************************************************************************************
110 
111  public:
112  //**Type definitions****************************************************************************
114  typedef RT ResultType;
115  typedef TT TransposeType;
116  typedef ET ElementType;
117 
120 
123 
125  typedef typename SelectType< IsExpression<VT>::value, const VT, const VT& >::Type Operand;
126  //**********************************************************************************************
127 
128  //**Compilation flags***************************************************************************
130  enum { vectorizable = 0 };
131  //**********************************************************************************************
132 
133  //**Constructor*********************************************************************************
138  explicit inline DVecAbsExpr( const VT& dv )
139  : dv_( dv ) // Dense vector of the absolute value expression
140  {}
141  //**********************************************************************************************
142 
143  //**Subscript operator**************************************************************************
149  inline ReturnType operator[]( size_t index ) const {
150  using std::abs;
151  BLAZE_INTERNAL_ASSERT( index < dv_.size(), "Invalid vector access index" );
152  return abs( dv_[index] );
153  }
154  //**********************************************************************************************
155 
156  //**Size function*******************************************************************************
161  inline size_t size() const {
162  return dv_.size();
163  }
164  //**********************************************************************************************
165 
166  //**Operand access******************************************************************************
171  inline Operand operand() const {
172  return dv_;
173  }
174  //**********************************************************************************************
175 
176  //**********************************************************************************************
182  template< typename T >
183  inline bool canAlias( const T* alias ) const {
184  return dv_.canAlias( alias );
185  }
186  //**********************************************************************************************
187 
188  //**********************************************************************************************
194  template< typename T >
195  inline bool isAliased( const T* alias ) const {
196  return dv_.isAliased( alias );
197  }
198  //**********************************************************************************************
199 
200  private:
201  //**Member variables****************************************************************************
203  //**********************************************************************************************
204 
205  //**Assignment to dense vectors*****************************************************************
219  template< typename VT2 > // Type of the target dense vector
220  friend inline typename EnableIf< UseAssign<VT2> >::Type
221  assign( DenseVector<VT2,TF>& lhs, const DVecAbsExpr& rhs )
222  {
224 
225  using std::abs;
226 
227  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
228 
229  assign( ~lhs, rhs.dv_ );
230 
231  const size_t size( rhs.size() );
232  for( size_t i=0UL; i<size; ++i ) {
233  (~lhs)[i] = abs( (~lhs)[i] );
234  }
235  }
237  //**********************************************************************************************
238 
239  //**Assignment to sparse vectors****************************************************************
253  template< typename VT2 > // Type of the target sparse vector
254  friend inline typename EnableIf< UseAssign<VT2> >::Type
255  assign( SparseVector<VT2,TF>& lhs, const DVecAbsExpr& rhs )
256  {
258 
261  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
262 
263  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
264 
265  const ResultType tmp( rhs );
266  assign( ~lhs, tmp );
267  }
269  //**********************************************************************************************
270 
271  //**Addition assignment to dense vectors********************************************************
285  template< typename VT2 > // Type of the target dense vector
286  friend inline typename EnableIf< UseAssign<VT2> >::Type
287  addAssign( DenseVector<VT2,TF>& lhs, const DVecAbsExpr& rhs )
288  {
290 
293  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
294 
295  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
296 
297  const ResultType tmp( rhs );
298  addAssign( ~lhs, tmp );
299  }
301  //**********************************************************************************************
302 
303  //**Addition assignment to sparse vectors*******************************************************
304  // No special implementation for the addition assignment to sparse vectors.
305  //**********************************************************************************************
306 
307  //**Subtraction assignment to dense vectors*****************************************************
321  template< typename VT2 > // Type of the target dense vector
322  friend inline typename EnableIf< UseAssign<VT2> >::Type
323  subAssign( DenseVector<VT2,TF>& lhs, const DVecAbsExpr& rhs )
324  {
326 
329  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
330 
331  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
332 
333  const ResultType tmp( rhs );
334  subAssign( ~lhs, tmp );
335  }
337  //**********************************************************************************************
338 
339  //**Subtraction assignment to sparse vectors****************************************************
340  // No special implementation for the subtraction assignment to sparse vectors.
341  //**********************************************************************************************
342 
343  //**Multiplication assignment to dense vectors**************************************************
357  template< typename VT2 > // Type of the target dense vector
358  friend inline typename EnableIf< UseAssign<VT2> >::Type
359  multAssign( DenseVector<VT2,TF>& lhs, const DVecAbsExpr& rhs )
360  {
362 
365  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
366 
367  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
368 
369  const ResultType tmp( rhs );
370  multAssign( ~lhs, tmp );
371  }
373  //**********************************************************************************************
374 
375  //**Multiplication assignment to sparse vectors*************************************************
376  // No special implementation for the multiplication assignment to sparse vectors.
377  //**********************************************************************************************
378 
379  //**Compile time checks*************************************************************************
384  //**********************************************************************************************
385 };
386 //*************************************************************************************************
387 
388 
389 
390 
391 //=================================================================================================
392 //
393 // GLOBAL OPERATORS
394 //
395 //=================================================================================================
396 
397 //*************************************************************************************************
414 template< typename VT // Type of the dense vector
415  , bool TF > // Transpose flag
416 inline const DVecAbsExpr<VT,TF> abs( const DenseVector<VT,TF>& dv )
417 {
419 
420  return DVecAbsExpr<VT,TF>( ~dv );
421 }
422 //*************************************************************************************************
423 
424 
425 
426 
427 //=================================================================================================
428 //
429 // GLOBAL RESTRUCTURING OPERATORS
430 //
431 //=================================================================================================
432 
433 //*************************************************************************************************
444 template< typename VT // Type of the dense vector
445  , bool TF > // Transpose flag
446 inline const DVecAbsExpr<VT,TF>& abs( const DVecAbsExpr<VT,TF>& dv )
447 {
449 
450  return dv;
451 }
453 //*************************************************************************************************
454 
455 } // namespace blaze
456 
457 #endif