All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DenseVector.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_DENSEVECTOR_H_
36 #define _BLAZE_MATH_DENSEVECTOR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <cmath>
63 #include <blaze/math/Functions.h>
64 #include <blaze/math/shims/Equal.h>
66 #include <blaze/math/shims/IsNaN.h>
72 #include <blaze/math/Vector.h>
73 #include <blaze/util/Assert.h>
75 #include <blaze/util/EnableIf.h>
76 #include <blaze/util/Types.h>
79 
80 
81 namespace blaze {
82 
83 //=================================================================================================
84 //
85 // GLOBAL OPERATORS
86 //
87 //=================================================================================================
88 
89 //*************************************************************************************************
92 template< typename T1, bool TF1, typename T2, bool TF2 >
93 inline bool operator==( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
94 
95 template< typename T1, bool TF1, typename T2, bool TF2 >
96 inline bool operator==( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs );
97 
98 template< typename T1, bool TF1, typename T2, bool TF2 >
99 inline bool operator==( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
100 
101 template< typename T1, typename T2, bool TF >
102 inline typename EnableIf< IsNumeric<T2>, bool >::Type
103  operator==( const DenseVector<T1,TF>& vec, T2 scalar );
104 
105 template< typename T1, typename T2, bool TF >
106 inline typename EnableIf< IsNumeric<T1>, bool >::Type
107  operator==( T1 scalar, const DenseVector<T2,TF>& vec );
108 
109 template< typename T1, bool TF1, typename T2, bool TF2 >
110 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
111 
112 template< typename T1, bool TF1, typename T2, bool TF2 >
113 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs );
114 
115 template< typename T1, bool TF1, typename T2, bool TF2 >
116 inline bool operator!=( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
117 
118 template< typename T1, typename T2, bool TF >
119 inline typename EnableIf< IsNumeric<T2>, bool >::Type
120  operator!=( const DenseVector<T1,TF>& vec, T2 scalar );
121 
122 template< typename T1, typename T2, bool TF >
123 inline typename EnableIf< IsNumeric<T1>, bool >::Type
124  operator!=( T1 scalar, const DenseVector<T2,TF>& vec );
126 //*************************************************************************************************
127 
128 
129 //*************************************************************************************************
137 template< typename T1 // Type of the left-hand side dense vector
138  , bool TF1 // Transpose flag of the left-hand side dense vector
139  , typename T2 // Type of the right-hand side dense vector
140  , bool TF2 > // Transpose flag of the right-hand side dense vector
141 inline bool operator==( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
142 {
143  typedef typename T1::CompositeType CT1;
144  typedef typename T2::CompositeType CT2;
145 
146  // Early exit in case the vector sizes don't match
147  if( (~lhs).size() != (~rhs).size() ) return false;
148 
149  // Evaluation of the two dense vector operands
150  CT1 a( ~lhs );
151  CT2 b( ~rhs );
152 
153  // In order to compare the two vectors, the data values of the lower-order data
154  // type are converted to the higher-order data type within the equal function.
155  for( size_t i=0; i<a.size(); ++i )
156  if( !equal( a[i], b[i] ) ) return false;
157  return true;
158 }
159 //*************************************************************************************************
160 
161 
162 //*************************************************************************************************
170 template< typename T1 // Type of the left-hand side dense vector
171  , bool TF1 // Transpose flag of the left-hand side dense vector
172  , typename T2 // Type of the right-hand side sparse vector
173  , bool TF2 > // Transpose flag of the right-hand side sparse vector
174 inline bool operator==( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
175 {
176  typedef typename T1::CompositeType CT1;
177  typedef typename T2::CompositeType CT2;
179 
180  // Early exit in case the vector sizes don't match
181  if( (~lhs).size() != (~rhs).size() ) return false;
182 
183  // Evaluation of the dense vector and sparse vector operand
184  CT1 a( ~lhs );
185  CT2 b( ~rhs );
186 
187  // In order to compare the two vectors, the data values of the lower-order data
188  // type are converted to the higher-order data type within the equal function.
189  size_t i( 0 );
190 
191  for( ConstIterator element=b.begin(); element!=b.end(); ++element, ++i ) {
192  for( ; i<element->index(); ++i ) {
193  if( !isDefault( a[i] ) ) return false;
194  }
195  if( !equal( element->value(), a[i] ) ) return false;
196  }
197  for( ; i<a.size(); ++i ) {
198  if( !isDefault( a[i] ) ) return false;
199  }
200 
201  return true;
202 }
203 //*************************************************************************************************
204 
205 
206 //*************************************************************************************************
214 template< typename T1 // Type of the left-hand side sparse vector
215  , bool TF1 // Transpose flag of the left-hand side sparse vector
216  , typename T2 // Type of the right-hand side dense vector
217  , bool TF2 > // Transpose flag of the right-hand side dense vector
218 inline bool operator==( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
219 {
220  return ( rhs == lhs );
221 }
222 //*************************************************************************************************
223 
224 
225 //*************************************************************************************************
237 template< typename T1 // Type of the left-hand side dense vector
238  , typename T2 // Type of the right-hand side scalar
239  , bool TF > // Transpose flag
240 inline typename EnableIf< IsNumeric<T2>, bool >::Type
241  operator==( const DenseVector<T1,TF>& vec, T2 scalar )
242 {
243  typedef typename T1::CompositeType CT1;
244 
245  // Evaluation of the dense vector operand
246  CT1 a( ~vec );
247 
248  // In order to compare the vector and the scalar value, the data values of the lower-order
249  // data type are converted to the higher-order data type within the equal function.
250  for( size_t i=0; i<a.size(); ++i )
251  if( !equal( a[i], scalar ) ) return false;
252  return true;
253 }
254 //*************************************************************************************************
255 
256 
257 //*************************************************************************************************
269 template< typename T1 // Type of the left-hand side scalar
270  , typename T2 // Type of the right-hand side dense vector
271  , bool TF > // Transpose flag
272 inline typename EnableIf< IsNumeric<T1>, bool >::Type
273  operator==( T1 scalar, const DenseVector<T2,TF>& vec )
274 {
275  return ( vec == scalar );
276 }
277 //*************************************************************************************************
278 
279 
280 //*************************************************************************************************
288 template< typename T1 // Type of the left-hand side dense vector
289  , bool TF1 // Transpose flag of the left-hand side dense vector
290  , typename T2 // Type of the right-hand side dense vector
291  , bool TF2 > // Transpose flag of the right-hand side dense vector
292 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
293 {
294  return !( lhs == rhs );
295 }
296 //*************************************************************************************************
297 
298 
299 //*************************************************************************************************
307 template< typename T1 // Type of the left-hand side dense vector
308  , bool TF1 // Transpose flag of the left-hand side dense vector
309  , typename T2 // Type of the right-hand side sparse vector
310  , bool TF2 > // Transpose flag of the right-hand side sparse vector
311 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
312 {
313  return !( lhs == rhs );
314 }
315 //*************************************************************************************************
316 
317 
318 //*************************************************************************************************
326 template< typename T1 // Type of the left-hand side sparse vector
327  , bool TF1 // Transpose flag of the left-hand side sparse vector
328  , typename T2 // Type of the right-hand side dense vector
329  , bool TF2 > // Transpose flag of the right-hand side dense vector
330 inline bool operator!=( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
331 {
332  return !( rhs == lhs );
333 }
334 //*************************************************************************************************
335 
336 
337 //*************************************************************************************************
349 template< typename T1 // Type of the left-hand side dense vector
350  , typename T2 // Type of the right-hand side scalar
351  , bool TF > // Transpose flag
352 inline typename EnableIf< IsNumeric<T2>, bool >::Type
353  operator!=( const DenseVector<T1,TF>& vec, T2 scalar )
354 {
355  return !( vec == scalar );
356 }
357 //*************************************************************************************************
358 
359 
360 //*************************************************************************************************
372 template< typename T1 // Type of the left-hand side scalar
373  , typename T2 // Type of the right-hand side vector
374  , bool TF > // Transpose flag
375 inline typename EnableIf< IsNumeric<T1>, bool >::Type
376  operator!=( T1 scalar, const DenseVector<T2,TF>& vec )
377 {
378  return !( vec == scalar );
379 }
380 //*************************************************************************************************
381 
382 
383 
384 
385 //=================================================================================================
386 //
387 // GLOBAL FUNCTIONS
388 //
389 //=================================================================================================
390 
391 //*************************************************************************************************
394 template< typename VT, bool TF >
395 bool isnan( const DenseVector<VT,TF>& dv );
396 
397 template< typename VT, bool TF >
398 typename CMathTrait<typename VT::ElementType>::Type length( const DenseVector<VT,TF>& dv );
399 
400 template< typename VT, bool TF >
401 const typename VT::ElementType sqrLength( const DenseVector<VT,TF>& dv );
402 
403 template< typename VT, bool TF >
404 const typename VT::ElementType min( const DenseVector<VT,TF>& dv );
405 
406 template< typename VT, bool TF >
407 const typename VT::ElementType max( const DenseVector<VT,TF>& dv );
409 //*************************************************************************************************
410 
411 
412 //*************************************************************************************************
432 template< typename VT // Type of the dense vector
433  , bool TF > // Transpose flag
434 bool isnan( const DenseVector<VT,TF>& dv )
435 {
436  typedef typename VT::CompositeType CT;
437 
438  CT a( ~dv ); // Evaluation of the dense vector operand
439 
440  for( size_t i=0UL; i<a.size(); ++i ) {
441  if( isnan( a[i] ) ) return true;
442  }
443  return false;
444 }
445 //*************************************************************************************************
446 
447 
448 //*************************************************************************************************
481 template< typename VT // Type of the dense vector
482  , bool TF > // Transpose flag
484 {
485  typedef typename VT::ElementType ElementType;
486  typedef typename CMathTrait<ElementType>::Type LengthType;
487 
489 
490  LengthType sum( 0 );
491  for( size_t i=0UL; i<(~dv).size(); ++i )
492  sum += sq( (~dv)[i] );
493  return std::sqrt( sum );
494 }
495 //*************************************************************************************************
496 
497 
498 //*************************************************************************************************
511 template< typename VT // Type of the dense vector
512  , bool TF > // Transpose flag
513 const typename VT::ElementType sqrLength( const DenseVector<VT,TF>& dv )
514 {
515  typedef typename VT::ElementType ElementType;
516 
518 
519  ElementType sum( 0 );
520  for( size_t i=0UL; i<(~dv).size(); ++i )
521  sum += sq( (~dv)[i] );
522  return sum;
523 }
524 //*************************************************************************************************
525 
526 
527 //*************************************************************************************************
539 template< typename VT // Type of the dense vector
540  , bool TF > // Transpose flag
541 const typename VT::ElementType min( const DenseVector<VT,TF>& dv )
542 {
543  using blaze::min;
544 
545  typedef typename VT::ElementType ET;
546  typedef typename VT::CompositeType CT;
547 
548  CT a( ~dv ); // Evaluation of the dense vector operand
549 
550  if( a.size() == 0UL ) return ET();
551 
552  ET minimum( a[0] );
553  for( size_t i=1UL; i<a.size(); ++i )
554  minimum = min( minimum, a[i] );
555  return minimum;
556 }
557 //*************************************************************************************************
558 
559 
560 //*************************************************************************************************
572 template< typename VT // Type of the dense vector
573  , bool TF > // Transpose flag
574 const typename VT::ElementType max( const DenseVector<VT,TF>& dv )
575 {
576  using blaze::max;
577 
578  typedef typename VT::ElementType ET;
579  typedef typename VT::CompositeType CT;
580 
581  CT a( ~dv ); // Evaluation of the dense vector operand
582 
583  if( a.size() == 0UL ) return ET();
584 
585  ET maximum( a[0] );
586  for( size_t i=1UL; i<a.size(); ++i )
587  maximum = max( maximum, a[i] );
588  return maximum;
589 }
590 //*************************************************************************************************
591 
592 } // namespace blaze
593 
594 #endif
Header file for the isnan shim.
Constraint on the data type.
Header file for mathematical functions.
Header file for the dense vector/dense vector multiplication expression.
Header file for the SparseVector base class.
Header file for the dense vector/dense vector subtraction expression.
double Type
Return type of the functions for integral and double arguments.
Definition: CMathTrait.h:79
bool isDefault(const DynamicMatrix< Type, SO > &m)
Returns whether the given dense matrix is in default state.
Definition: DynamicMatrix.h:4642
const Quaternion< Type > sq(const Quaternion< Type > &m)
Squaring the given quaternion.
Definition: Quaternion.h:1034
int16_t sum(const sse_int16_t &a)
Returns the sum of all elements in the 16-bit integral intrinsic vector.
Definition: Reduction.h:62
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:2408
Header file for the DenseVector base class.
bool isnan(const DenseMatrix< MT, SO > &dm)
Checks the given dense matrix for not-a-number elements.
Definition: DenseMatrix.h:640
Header file for the dense vector transpose expression.
Header file for the vector transpose flag types.
Header file for the dense vector/sparse vector addition expression.
Header file for the dense vector/scalar division expression.
Header file for the dense vector SMP implementation.
Header file for the dense vector evaluation expression.
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2412
Header file for the dense vector/sparse vector cross product expression.
Header file for the dense vector/dense vector cross product expression.
Base class for N-dimensional dense vectors.The DenseVector class is a base class for all arbitrarily ...
Definition: DenseVector.h:70
Header file for the dense vector/dense vector inner product expression.
Header file for the dense vector/sparse vector subtraction expression.
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2406
CMathTrait< typename VT::ElementType >::Type length(const DenseVector< VT, TF > &dv)
Calculation of the dense vector length .
Definition: DenseVector.h:483
Header file for the dense vector serial evaluation expression.
Header file for the EnableIf class template.
Header file for the equal shim.
Header file for the IsNumeric type trait.
Removal of reference modifiers.The RemoveCV type trait removes any reference modifiers from the given...
Definition: RemoveReference.h:69
Header file for run time assertion macros.
Header file for all basic Vector functionality.
bool equal(const T1 &a, const T2 &b)
Generic equality check.
Definition: Equal.h:352
Header file for the square shim.
#define BLAZE_CONSTRAINT_MUST_BE_NUMERIC_TYPE(T)
Constraint on the data type.In case the given data type T is not a numeric (integral or floating poin...
Definition: Numeric.h:79
const VT::ElementType max(const SparseVector< VT, TF > &sv)
Returns the largest element of the sparse vector.
Definition: SparseVector.h:408
Header file for the isDefault shim.
Header file for the RemoveReference type trait.
Header file for the cmath trait.
Header file for the sparse vector/dense vector subtraction expression.
Header file for the dense vector/scalar multiplication expression.
Header file for the sparse vector/sparse vector cross product expression.
Header file for the sparse vector SMP implementation.
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:108
bool operator==(const NegativeAccuracy< A > &lhs, const T &rhs)
Equality comparison between a NegativeAccuracy object and a floating point value. ...
Definition: Accuracy.h:249
Header file for the sparse vector/sparse vector cross product expression.
Header file for the dense vector/dense vector addition expression.
bool operator!=(const NegativeAccuracy< A > &lhs, const T &rhs)
Inequality comparison between a NegativeAccuracy object and a floating point value.
Definition: Accuracy.h:289
Header file for basic type definitions.
const VT::ElementType sqrLength(const DenseVector< VT, TF > &dv)
Calculation of the dense vector square length .
Definition: DenseVector.h:513
const VT::ElementType min(const SparseVector< VT, TF > &sv)
Returns the smallest element of the sparse vector.
Definition: SparseVector.h:351
Header file for the dense vector absolute value expression.