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>
62 #include <blaze/math/Functions.h>
63 #include <blaze/math/shims/Equal.h>
65 #include <blaze/math/shims/IsNaN.h>
69 #include <blaze/math/Vector.h>
70 #include <blaze/util/Assert.h>
72 #include <blaze/util/EnableIf.h>
73 #include <blaze/util/Types.h>
76 
77 
78 namespace blaze {
79 
80 //=================================================================================================
81 //
82 // GLOBAL OPERATORS
83 //
84 //=================================================================================================
85 
86 //*************************************************************************************************
89 template< typename T1, bool TF1, typename T2, bool TF2 >
90 inline bool operator==( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
91 
92 template< typename T1, bool TF1, typename T2, bool TF2 >
93 inline bool operator==( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs );
94 
95 template< typename T1, bool TF1, typename T2, bool TF2 >
96 inline bool operator==( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
97 
98 template< typename T1, typename T2, bool TF >
99 inline typename EnableIf< IsNumeric<T2>, bool >::Type
100  operator==( const DenseVector<T1,TF>& vec, T2 scalar );
101 
102 template< typename T1, typename T2, bool TF >
103 inline typename EnableIf< IsNumeric<T1>, bool >::Type
104  operator==( T1 scalar, const DenseVector<T2,TF>& vec );
105 
106 template< typename T1, bool TF1, typename T2, bool TF2 >
107 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
108 
109 template< typename T1, bool TF1, typename T2, bool TF2 >
110 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs );
111 
112 template< typename T1, bool TF1, typename T2, bool TF2 >
113 inline bool operator!=( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
114 
115 template< typename T1, typename T2, bool TF >
116 inline typename EnableIf< IsNumeric<T2>, bool >::Type
117  operator!=( const DenseVector<T1,TF>& vec, T2 scalar );
118 
119 template< typename T1, typename T2, bool TF >
120 inline typename EnableIf< IsNumeric<T1>, bool >::Type
121  operator!=( T1 scalar, const DenseVector<T2,TF>& vec );
123 //*************************************************************************************************
124 
125 
126 //*************************************************************************************************
134 template< typename T1 // Type of the left-hand side dense vector
135  , bool TF1 // Transpose flag of the left-hand side dense vector
136  , typename T2 // Type of the right-hand side dense vector
137  , bool TF2 > // Transpose flag of the right-hand side dense vector
138 inline bool operator==( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
139 {
140  typedef typename T1::CompositeType CT1;
141  typedef typename T2::CompositeType CT2;
142 
143  // Early exit in case the vector sizes don't match
144  if( (~lhs).size() != (~rhs).size() ) return false;
145 
146  // Evaluation of the two dense vector operands
147  CT1 a( ~lhs );
148  CT2 b( ~rhs );
149 
150  // In order to compare the two vectors, the data values of the lower-order data
151  // type are converted to the higher-order data type within the equal function.
152  for( size_t i=0; i<a.size(); ++i )
153  if( !equal( a[i], b[i] ) ) return false;
154  return true;
155 }
156 //*************************************************************************************************
157 
158 
159 //*************************************************************************************************
167 template< typename T1 // Type of the left-hand side dense vector
168  , bool TF1 // Transpose flag of the left-hand side dense vector
169  , typename T2 // Type of the right-hand side sparse vector
170  , bool TF2 > // Transpose flag of the right-hand side sparse vector
171 inline bool operator==( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
172 {
173  typedef typename T1::CompositeType CT1;
174  typedef typename T2::CompositeType CT2;
176 
177  // Early exit in case the vector sizes don't match
178  if( (~lhs).size() != (~rhs).size() ) return false;
179 
180  // Evaluation of the dense vector and sparse vector operand
181  CT1 a( ~lhs );
182  CT2 b( ~rhs );
183 
184  // In order to compare the two vectors, the data values of the lower-order data
185  // type are converted to the higher-order data type within the equal function.
186  size_t i( 0 );
187 
188  for( ConstIterator element=b.begin(); element!=b.end(); ++element, ++i ) {
189  for( ; i<element->index(); ++i ) {
190  if( !isDefault( a[i] ) ) return false;
191  }
192  if( !equal( element->value(), a[i] ) ) return false;
193  }
194  for( ; i<a.size(); ++i ) {
195  if( !isDefault( a[i] ) ) return false;
196  }
197 
198  return true;
199 }
200 //*************************************************************************************************
201 
202 
203 //*************************************************************************************************
211 template< typename T1 // Type of the left-hand side sparse vector
212  , bool TF1 // Transpose flag of the left-hand side sparse vector
213  , typename T2 // Type of the right-hand side dense vector
214  , bool TF2 > // Transpose flag of the right-hand side dense vector
215 inline bool operator==( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
216 {
217  return ( rhs == lhs );
218 }
219 //*************************************************************************************************
220 
221 
222 //*************************************************************************************************
234 template< typename T1 // Type of the left-hand side dense vector
235  , typename T2 // Type of the right-hand side scalar
236  , bool TF > // Transpose flag
237 inline typename EnableIf< IsNumeric<T2>, bool >::Type
238  operator==( const DenseVector<T1,TF>& vec, T2 scalar )
239 {
240  typedef typename T1::CompositeType CT1;
241 
242  // Evaluation of the dense vector operand
243  CT1 a( ~vec );
244 
245  // In order to compare the vector and the scalar value, the data values of the lower-order
246  // data type are converted to the higher-order data type within the equal function.
247  for( size_t i=0; i<a.size(); ++i )
248  if( !equal( a[i], scalar ) ) return false;
249  return true;
250 }
251 //*************************************************************************************************
252 
253 
254 //*************************************************************************************************
266 template< typename T1 // Type of the left-hand side scalar
267  , typename T2 // Type of the right-hand side dense vector
268  , bool TF > // Transpose flag
269 inline typename EnableIf< IsNumeric<T1>, bool >::Type
270  operator==( T1 scalar, const DenseVector<T2,TF>& vec )
271 {
272  return ( vec == scalar );
273 }
274 //*************************************************************************************************
275 
276 
277 //*************************************************************************************************
285 template< typename T1 // Type of the left-hand side dense vector
286  , bool TF1 // Transpose flag of the left-hand side dense vector
287  , typename T2 // Type of the right-hand side dense vector
288  , bool TF2 > // Transpose flag of the right-hand side dense vector
289 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
290 {
291  return !( lhs == rhs );
292 }
293 //*************************************************************************************************
294 
295 
296 //*************************************************************************************************
304 template< typename T1 // Type of the left-hand side dense vector
305  , bool TF1 // Transpose flag of the left-hand side dense vector
306  , typename T2 // Type of the right-hand side sparse vector
307  , bool TF2 > // Transpose flag of the right-hand side sparse vector
308 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
309 {
310  return !( lhs == rhs );
311 }
312 //*************************************************************************************************
313 
314 
315 //*************************************************************************************************
323 template< typename T1 // Type of the left-hand side sparse vector
324  , bool TF1 // Transpose flag of the left-hand side sparse vector
325  , typename T2 // Type of the right-hand side dense vector
326  , bool TF2 > // Transpose flag of the right-hand side dense vector
327 inline bool operator!=( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
328 {
329  return !( rhs == lhs );
330 }
331 //*************************************************************************************************
332 
333 
334 //*************************************************************************************************
346 template< typename T1 // Type of the left-hand side dense vector
347  , typename T2 // Type of the right-hand side scalar
348  , bool TF > // Transpose flag
349 inline typename EnableIf< IsNumeric<T2>, bool >::Type
350  operator!=( const DenseVector<T1,TF>& vec, T2 scalar )
351 {
352  return !( vec == scalar );
353 }
354 //*************************************************************************************************
355 
356 
357 //*************************************************************************************************
369 template< typename T1 // Type of the left-hand side scalar
370  , typename T2 // Type of the right-hand side vector
371  , bool TF > // Transpose flag
372 inline typename EnableIf< IsNumeric<T1>, bool >::Type
373  operator!=( T1 scalar, const DenseVector<T2,TF>& vec )
374 {
375  return !( vec == scalar );
376 }
377 //*************************************************************************************************
378 
379 
380 
381 
382 //=================================================================================================
383 //
384 // GLOBAL FUNCTIONS
385 //
386 //=================================================================================================
387 
388 //*************************************************************************************************
391 template< typename VT, bool TF >
392 bool isnan( const DenseVector<VT,TF>& dv );
393 
394 template< typename VT, bool TF >
395 typename CMathTrait<typename VT::ElementType>::Type length( const DenseVector<VT,TF>& dv );
396 
397 template< typename VT, bool TF >
398 const typename VT::ElementType sqrLength( const DenseVector<VT,TF>& dv );
399 
400 template< typename VT, bool TF >
401 const typename VT::ElementType min( const DenseVector<VT,TF>& dv );
402 
403 template< typename VT, bool TF >
404 const typename VT::ElementType max( const DenseVector<VT,TF>& dv );
406 //*************************************************************************************************
407 
408 
409 //*************************************************************************************************
429 template< typename VT // Type of the dense vector
430  , bool TF > // Transpose flag
431 bool isnan( const DenseVector<VT,TF>& dv )
432 {
433  typedef typename VT::CompositeType CT;
434 
435  CT a( ~dv ); // Evaluation of the dense vector operand
436 
437  for( size_t i=0UL; i<a.size(); ++i ) {
438  if( isnan( a[i] ) ) return true;
439  }
440  return false;
441 }
442 //*************************************************************************************************
443 
444 
445 //*************************************************************************************************
478 template< typename VT // Type of the dense vector
479  , bool TF > // Transpose flag
481 {
482  typedef typename VT::ElementType ElementType;
483  typedef typename CMathTrait<ElementType>::Type LengthType;
484 
486 
487  LengthType sum( 0 );
488  for( size_t i=0UL; i<(~dv).size(); ++i )
489  sum += sq( (~dv)[i] );
490  return std::sqrt( sum );
491 }
492 //*************************************************************************************************
493 
494 
495 //*************************************************************************************************
508 template< typename VT // Type of the dense vector
509  , bool TF > // Transpose flag
510 const typename VT::ElementType sqrLength( const DenseVector<VT,TF>& dv )
511 {
512  typedef typename VT::ElementType ElementType;
513 
515 
516  ElementType sum( 0 );
517  for( size_t i=0UL; i<(~dv).size(); ++i )
518  sum += sq( (~dv)[i] );
519  return sum;
520 }
521 //*************************************************************************************************
522 
523 
524 //*************************************************************************************************
536 template< typename VT // Type of the dense vector
537  , bool TF > // Transpose flag
538 const typename VT::ElementType min( const DenseVector<VT,TF>& dv )
539 {
540  using blaze::min;
541 
542  typedef typename VT::ElementType ET;
543  typedef typename VT::CompositeType CT;
544 
545  CT a( ~dv ); // Evaluation of the dense vector operand
546 
547  if( a.size() == 0UL ) return ET();
548 
549  ET minimum( a[0] );
550  for( size_t i=1UL; i<a.size(); ++i )
551  minimum = min( minimum, a[i] );
552  return minimum;
553 }
554 //*************************************************************************************************
555 
556 
557 //*************************************************************************************************
569 template< typename VT // Type of the dense vector
570  , bool TF > // Transpose flag
571 const typename VT::ElementType max( const DenseVector<VT,TF>& dv )
572 {
573  using blaze::max;
574 
575  typedef typename VT::ElementType ET;
576  typedef typename VT::CompositeType CT;
577 
578  CT a( ~dv ); // Evaluation of the dense vector operand
579 
580  if( a.size() == 0UL ) return ET();
581 
582  ET maximum( a[0] );
583  for( size_t i=1UL; i<a.size(); ++i )
584  maximum = max( maximum, a[i] );
585  return maximum;
586 }
587 //*************************************************************************************************
588 
589 } // namespace blaze
590 
591 #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 &lt;cmath&gt; 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:4555
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:2375
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:637
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 evaluation expression.
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2379
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:2373
CMathTrait< typename VT::ElementType >::Type length(const DenseVector< VT, TF > &dv)
Calculation of the dense vector length .
Definition: DenseVector.h:480
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:405
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.
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:105
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:510
const VT::ElementType min(const SparseVector< VT, TF > &sv)
Returns the smallest element of the sparse vector.
Definition: SparseVector.h:348
Header file for the dense vector absolute value expression.