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_DENSE_DENSEVECTOR_H_
36 #define _BLAZE_MATH_DENSE_DENSEVECTOR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <cmath>
46 #include <blaze/math/Functions.h>
47 #include <blaze/math/shims/Equal.h>
49 #include <blaze/math/shims/IsNaN.h>
53 #include <blaze/util/Assert.h>
55 #include <blaze/util/EnableIf.h>
56 #include <blaze/util/Types.h>
59 
60 
61 namespace blaze {
62 
63 //=================================================================================================
64 //
65 // GLOBAL OPERATORS
66 //
67 //=================================================================================================
68 
69 //*************************************************************************************************
72 template< typename T1, bool TF1, typename T2, bool TF2 >
73 inline bool operator==( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
74 
75 template< typename T1, bool TF1, typename T2, bool TF2 >
76 inline bool operator==( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs );
77 
78 template< typename T1, bool TF1, typename T2, bool TF2 >
79 inline bool operator==( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
80 
81 template< typename T1, typename T2, bool TF >
82 inline typename EnableIf< IsNumeric<T2>, bool >::Type
83  operator==( const DenseVector<T1,TF>& vec, T2 scalar );
84 
85 template< typename T1, typename T2, bool TF >
86 inline typename EnableIf< IsNumeric<T1>, bool >::Type
87  operator==( T1 scalar, const DenseVector<T2,TF>& vec );
88 
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 );
106 //*************************************************************************************************
107 
108 
109 //*************************************************************************************************
117 template< typename T1 // Type of the left-hand side dense vector
118  , bool TF1 // Transpose flag of the left-hand side dense vector
119  , typename T2 // Type of the right-hand side dense vector
120  , bool TF2 > // Transpose flag of the right-hand side dense vector
121 inline bool operator==( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
122 {
123  typedef typename T1::CompositeType CT1;
124  typedef typename T2::CompositeType CT2;
125 
126  // Early exit in case the vector sizes don't match
127  if( (~lhs).size() != (~rhs).size() ) return false;
128 
129  // Evaluation of the two dense vector operands
130  CT1 a( ~lhs );
131  CT2 b( ~rhs );
132 
133  // In order to compare the two vectors, the data values of the lower-order data
134  // type are converted to the higher-order data type within the equal function.
135  for( size_t i=0; i<a.size(); ++i )
136  if( !equal( a[i], b[i] ) ) return false;
137  return true;
138 }
139 //*************************************************************************************************
140 
141 
142 //*************************************************************************************************
150 template< typename T1 // Type of the left-hand side dense vector
151  , bool TF1 // Transpose flag of the left-hand side dense vector
152  , typename T2 // Type of the right-hand side sparse vector
153  , bool TF2 > // Transpose flag of the right-hand side sparse vector
154 inline bool operator==( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
155 {
156  typedef typename T1::CompositeType CT1;
157  typedef typename T2::CompositeType CT2;
159 
160  // Early exit in case the vector sizes don't match
161  if( (~lhs).size() != (~rhs).size() ) return false;
162 
163  // Evaluation of the dense vector and sparse vector operand
164  CT1 a( ~lhs );
165  CT2 b( ~rhs );
166 
167  // In order to compare the two vectors, the data values of the lower-order data
168  // type are converted to the higher-order data type within the equal function.
169  size_t i( 0 );
170 
171  for( ConstIterator element=b.begin(); element!=b.end(); ++element, ++i ) {
172  for( ; i<element->index(); ++i ) {
173  if( !isDefault( a[i] ) ) return false;
174  }
175  if( !equal( element->value(), a[i] ) ) return false;
176  }
177  for( ; i<a.size(); ++i ) {
178  if( !isDefault( a[i] ) ) return false;
179  }
180 
181  return true;
182 }
183 //*************************************************************************************************
184 
185 
186 //*************************************************************************************************
194 template< typename T1 // Type of the left-hand side sparse vector
195  , bool TF1 // Transpose flag of the left-hand side sparse vector
196  , typename T2 // Type of the right-hand side dense vector
197  , bool TF2 > // Transpose flag of the right-hand side dense vector
198 inline bool operator==( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
199 {
200  return ( rhs == lhs );
201 }
202 //*************************************************************************************************
203 
204 
205 //*************************************************************************************************
217 template< typename T1 // Type of the left-hand side dense vector
218  , typename T2 // Type of the right-hand side scalar
219  , bool TF > // Transpose flag
220 inline typename EnableIf< IsNumeric<T2>, bool >::Type
221  operator==( const DenseVector<T1,TF>& vec, T2 scalar )
222 {
223  typedef typename T1::CompositeType CT1;
224 
225  // Evaluation of the dense vector operand
226  CT1 a( ~vec );
227 
228  // In order to compare the vector and the scalar value, the data values of the lower-order
229  // data type are converted to the higher-order data type within the equal function.
230  for( size_t i=0; i<a.size(); ++i )
231  if( !equal( a[i], scalar ) ) return false;
232  return true;
233 }
234 //*************************************************************************************************
235 
236 
237 //*************************************************************************************************
249 template< typename T1 // Type of the left-hand side scalar
250  , typename T2 // Type of the right-hand side dense vector
251  , bool TF > // Transpose flag
252 inline typename EnableIf< IsNumeric<T1>, bool >::Type
253  operator==( T1 scalar, const DenseVector<T2,TF>& vec )
254 {
255  return ( vec == scalar );
256 }
257 //*************************************************************************************************
258 
259 
260 //*************************************************************************************************
268 template< typename T1 // Type of the left-hand side dense vector
269  , bool TF1 // Transpose flag of the left-hand side dense vector
270  , typename T2 // Type of the right-hand side dense vector
271  , bool TF2 > // Transpose flag of the right-hand side dense vector
272 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
273 {
274  return !( lhs == rhs );
275 }
276 //*************************************************************************************************
277 
278 
279 //*************************************************************************************************
287 template< typename T1 // Type of the left-hand side dense vector
288  , bool TF1 // Transpose flag of the left-hand side dense vector
289  , typename T2 // Type of the right-hand side sparse vector
290  , bool TF2 > // Transpose flag of the right-hand side sparse vector
291 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
292 {
293  return !( lhs == rhs );
294 }
295 //*************************************************************************************************
296 
297 
298 //*************************************************************************************************
306 template< typename T1 // Type of the left-hand side sparse vector
307  , bool TF1 // Transpose flag of the left-hand side sparse vector
308  , typename T2 // Type of the right-hand side dense vector
309  , bool TF2 > // Transpose flag of the right-hand side dense vector
310 inline bool operator!=( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
311 {
312  return !( rhs == lhs );
313 }
314 //*************************************************************************************************
315 
316 
317 //*************************************************************************************************
329 template< typename T1 // Type of the left-hand side dense vector
330  , typename T2 // Type of the right-hand side scalar
331  , bool TF > // Transpose flag
332 inline typename EnableIf< IsNumeric<T2>, bool >::Type
333  operator!=( const DenseVector<T1,TF>& vec, T2 scalar )
334 {
335  return !( vec == scalar );
336 }
337 //*************************************************************************************************
338 
339 
340 //*************************************************************************************************
352 template< typename T1 // Type of the left-hand side scalar
353  , typename T2 // Type of the right-hand side vector
354  , bool TF > // Transpose flag
355 inline typename EnableIf< IsNumeric<T1>, bool >::Type
356  operator!=( T1 scalar, const DenseVector<T2,TF>& vec )
357 {
358  return !( vec == scalar );
359 }
360 //*************************************************************************************************
361 
362 
363 
364 
365 //=================================================================================================
366 //
367 // GLOBAL FUNCTIONS
368 //
369 //=================================================================================================
370 
371 //*************************************************************************************************
374 template< typename VT, bool TF >
375 bool isnan( const DenseVector<VT,TF>& dv );
376 
377 template< typename VT, bool TF >
378 typename CMathTrait<typename VT::ElementType>::Type length( const DenseVector<VT,TF>& dv );
379 
380 template< typename VT, bool TF >
381 const typename VT::ElementType sqrLength( const DenseVector<VT,TF>& dv );
382 
383 template< typename VT, bool TF >
384 const typename VT::ElementType min( const DenseVector<VT,TF>& dv );
385 
386 template< typename VT, bool TF >
387 const typename VT::ElementType max( const DenseVector<VT,TF>& dv );
389 //*************************************************************************************************
390 
391 
392 //*************************************************************************************************
412 template< typename VT // Type of the dense vector
413  , bool TF > // Transpose flag
414 bool isnan( const DenseVector<VT,TF>& dv )
415 {
416  typedef typename VT::CompositeType CT;
417 
418  CT a( ~dv ); // Evaluation of the dense vector operand
419 
420  for( size_t i=0UL; i<a.size(); ++i ) {
421  if( isnan( a[i] ) ) return true;
422  }
423  return false;
424 }
425 //*************************************************************************************************
426 
427 
428 //*************************************************************************************************
461 template< typename VT // Type of the dense vector
462  , bool TF > // Transpose flag
464 {
465  typedef typename VT::ElementType ElementType;
466  typedef typename CMathTrait<ElementType>::Type LengthType;
467 
469 
470  LengthType sum( 0 );
471  for( size_t i=0UL; i<(~dv).size(); ++i )
472  sum += sq( (~dv)[i] );
473  return std::sqrt( sum );
474 }
475 //*************************************************************************************************
476 
477 
478 //*************************************************************************************************
491 template< typename VT // Type of the dense vector
492  , bool TF > // Transpose flag
493 const typename VT::ElementType sqrLength( const DenseVector<VT,TF>& dv )
494 {
495  typedef typename VT::ElementType ElementType;
496 
498 
499  ElementType sum( 0 );
500  for( size_t i=0UL; i<(~dv).size(); ++i )
501  sum += sq( (~dv)[i] );
502  return sum;
503 }
504 //*************************************************************************************************
505 
506 
507 //*************************************************************************************************
519 template< typename VT // Type of the dense vector
520  , bool TF > // Transpose flag
521 const typename VT::ElementType min( const DenseVector<VT,TF>& dv )
522 {
523  using blaze::min;
524 
525  typedef typename VT::ElementType ET;
526  typedef typename VT::CompositeType CT;
527 
528  CT a( ~dv ); // Evaluation of the dense vector operand
529 
530  if( a.size() == 0UL ) return ET();
531 
532  ET minimum( a[0] );
533  for( size_t i=1UL; i<a.size(); ++i )
534  minimum = min( minimum, a[i] );
535  return minimum;
536 }
537 //*************************************************************************************************
538 
539 
540 //*************************************************************************************************
552 template< typename VT // Type of the dense vector
553  , bool TF > // Transpose flag
554 const typename VT::ElementType max( const DenseVector<VT,TF>& dv )
555 {
556  using blaze::max;
557 
558  typedef typename VT::ElementType ET;
559  typedef typename VT::CompositeType CT;
560 
561  CT a( ~dv ); // Evaluation of the dense vector operand
562 
563  if( a.size() == 0UL ) return ET();
564 
565  ET maximum( a[0] );
566  for( size_t i=1UL; i<a.size(); ++i )
567  maximum = max( maximum, a[i] );
568  return maximum;
569 }
570 //*************************************************************************************************
571 
572 } // namespace blaze
573 
574 #endif
Header file for the isnan shim.
BLAZE_ALWAYS_INLINE int16_t sum(const sse_int16_t &a)
Returns the sum of all elements in the 16-bit integral intrinsic vector.
Definition: Reduction.h:63
const MT::ElementType max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:994
Constraint on the data type.
Header file for mathematical functions.
Header file for the SparseVector base class.
Header file for the square shim.
BLAZE_ALWAYS_INLINE size_t size(const Vector< VT, TF > &vector)
Returns the current size/dimension of the vector.
Definition: Vector.h:258
double Type
Return type of the functions for integral and double arguments.
Definition: CMathTrait.h:79
const Quaternion< Type > sq(const Quaternion< Type > &m)
Squaring the given quaternion.
Definition: Quaternion.h:1034
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:2478
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:609
Header file for the vector transpose flag types.
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2482
const MT::ElementType min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:947
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 equal shim.
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2476
CMathTrait< typename VT::ElementType >::Type length(const DenseVector< VT, TF > &dv)
Calculation of the dense vector length .
Definition: DenseVector.h:463
Header file for the EnableIf class template.
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.
bool equal(const T1 &a, const T2 &b)
Generic equality check.
Definition: Equal.h:376
#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
Header file for the isDefault shim.
BLAZE_ALWAYS_INLINE bool isDefault(const NonNumericProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: NonNumericProxy.h:874
Header file for the RemoveReference type trait.
Header file for the cmath trait.
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
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:493