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>
42 #include <blaze/util/Assert.h>
44 #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 
133  enum { canAlias = CanAlias<VT>::value };
134  //**********************************************************************************************
135 
136  //**Constructor*********************************************************************************
141  explicit inline DVecAbsExpr( const VT& dv )
142  : dv_( dv ) // Dense vector of the absolute value expression
143  {}
144  //**********************************************************************************************
145 
146  //**Subscript operator**************************************************************************
152  inline ReturnType operator[]( size_t index ) const {
153  using std::abs;
154  BLAZE_INTERNAL_ASSERT( index < dv_.size(), "Invalid vector access index" );
155  return abs( dv_[index] );
156  }
157  //**********************************************************************************************
158 
159  //**Size function*******************************************************************************
164  inline size_t size() const {
165  return dv_.size();
166  }
167  //**********************************************************************************************
168 
169  //**Operand access******************************************************************************
174  inline Operand operand() const {
175  return dv_;
176  }
177  //**********************************************************************************************
178 
179  //**********************************************************************************************
185  template< typename T >
186  inline bool isAliased( const T* alias ) const {
187  return CanAlias<VT>::value && dv_.isAliased( alias );
188  }
189  //**********************************************************************************************
190 
191  private:
192  //**Member variables****************************************************************************
194  //**********************************************************************************************
195 
196  //**Assignment to dense vectors*****************************************************************
210  template< typename VT2 > // Type of the target dense vector
211  friend inline typename EnableIf< UseAssign<VT2> >::Type
212  assign( DenseVector<VT2,TF>& lhs, const DVecAbsExpr& rhs )
213  {
214  using std::abs;
215 
216  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
217 
218  assign( ~lhs, rhs.dv_ );
219 
220  const size_t size( rhs.size() );
221  for( size_t i=0UL; i<size; ++i ) {
222  (~lhs)[i] = abs( (~lhs)[i] );
223  }
224  }
226  //**********************************************************************************************
227 
228  //**Assignment to sparse vectors****************************************************************
242  template< typename VT2 > // Type of the target sparse vector
243  friend inline typename EnableIf< UseAssign<VT2> >::Type
244  assign( SparseVector<VT2,TF>& lhs, const DVecAbsExpr& rhs )
245  {
248  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
249 
250  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
251 
252  const ResultType tmp( rhs );
253  assign( ~lhs, tmp );
254  }
256  //**********************************************************************************************
257 
258  //**Addition assignment to dense vectors********************************************************
272  template< typename VT2 > // Type of the target dense vector
273  friend inline typename EnableIf< UseAssign<VT2> >::Type
274  addAssign( DenseVector<VT2,TF>& lhs, const DVecAbsExpr& rhs )
275  {
278  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
279 
280  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
281 
282  const ResultType tmp( rhs );
283  addAssign( ~lhs, tmp );
284  }
286  //**********************************************************************************************
287 
288  //**Addition assignment to sparse vectors*******************************************************
289  // No special implementation for the addition assignment to sparse vectors.
290  //**********************************************************************************************
291 
292  //**Subtraction assignment to dense vectors*****************************************************
306  template< typename VT2 > // Type of the target dense vector
307  friend inline typename EnableIf< UseAssign<VT2> >::Type
308  subAssign( DenseVector<VT2,TF>& lhs, const DVecAbsExpr& rhs )
309  {
312  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
313 
314  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
315 
316  const ResultType tmp( rhs );
317  subAssign( ~lhs, tmp );
318  }
320  //**********************************************************************************************
321 
322  //**Subtraction assignment to sparse vectors****************************************************
323  // No special implementation for the subtraction assignment to sparse vectors.
324  //**********************************************************************************************
325 
326  //**Multiplication assignment to dense vectors**************************************************
340  template< typename VT2 > // Type of the target dense vector
341  friend inline typename EnableIf< UseAssign<VT2> >::Type
342  multAssign( DenseVector<VT2,TF>& lhs, const DVecAbsExpr& rhs )
343  {
346  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
347 
348  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
349 
350  const ResultType tmp( rhs );
351  multAssign( ~lhs, tmp );
352  }
354  //**********************************************************************************************
355 
356  //**Multiplication assignment to sparse vectors*************************************************
357  // No special implementation for the multiplication assignment to sparse vectors.
358  //**********************************************************************************************
359 
360  //**Compile time checks*************************************************************************
365  //**********************************************************************************************
366 };
367 //*************************************************************************************************
368 
369 
370 
371 
372 //=================================================================================================
373 //
374 // GLOBAL OPERATORS
375 //
376 //=================================================================================================
377 
378 //*************************************************************************************************
395 template< typename VT // Type of the dense vector
396  , bool TF > // Transpose flag
397 inline const DVecAbsExpr<VT,TF> abs( const DenseVector<VT,TF>& dv )
398 {
399  return DVecAbsExpr<VT,TF>( ~dv );
400 }
401 //*************************************************************************************************
402 
403 
404 
405 
406 //=================================================================================================
407 //
408 // GLOBAL RESTRUCTURING OPERATORS
409 //
410 //=================================================================================================
411 
412 //*************************************************************************************************
423 template< typename VT // Type of the dense vector
424  , bool TF > // Transpose flag
425 inline const DVecAbsExpr<VT,TF>& abs( const DVecAbsExpr<VT,TF>& dv )
426 {
427  return dv;
428 }
430 //*************************************************************************************************
431 
432 } // namespace blaze
433 
434 #endif