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 bool isUniform( const DenseVector<VT,TF>& dv );
379 
380 template< typename VT, bool TF >
381 typename CMathTrait<typename VT::ElementType>::Type length( const DenseVector<VT,TF>& dv );
382 
383 template< typename VT, bool TF >
384 const typename VT::ElementType sqrLength( const DenseVector<VT,TF>& dv );
385 
386 template< typename VT, bool TF >
387 const typename VT::ElementType min( const DenseVector<VT,TF>& dv );
388 
389 template< typename VT, bool TF >
390 const typename VT::ElementType max( const DenseVector<VT,TF>& dv );
392 //*************************************************************************************************
393 
394 
395 //*************************************************************************************************
415 template< typename VT // Type of the dense vector
416  , bool TF > // Transpose flag
417 bool isnan( const DenseVector<VT,TF>& dv )
418 {
419  typedef typename VT::CompositeType CT;
420 
421  CT a( ~dv ); // Evaluation of the dense vector operand
422 
423  for( size_t i=0UL; i<a.size(); ++i ) {
424  if( isnan( a[i] ) ) return true;
425  }
426  return false;
427 }
428 //*************************************************************************************************
429 
430 
431 //*************************************************************************************************
457 template< typename VT // Type of the dense vector
458  , bool TF > // Transpose flag
459 bool isUniform( const DenseVector<VT,TF>& dv )
460 {
461  typedef typename VT::CompositeType CT;
463 
464  if( (~dv).size() < 2UL )
465  return true;
466 
467  CT a( ~dv ); // Evaluation of the dense vector operand
468 
469  ConstReference cmp( (~dv)[0UL] );
470 
471  for( size_t i=1UL; i<(~dv).size(); ++i ) {
472  if( (~dv)[i] != cmp )
473  return false;
474  }
475 
476  return true;
477 }
478 //*************************************************************************************************
479 
480 
481 //*************************************************************************************************
514 template< typename VT // Type of the dense vector
515  , bool TF > // Transpose flag
517 {
518  typedef typename VT::ElementType ElementType;
519  typedef typename CMathTrait<ElementType>::Type LengthType;
520 
522 
523  LengthType sum( 0 );
524  for( size_t i=0UL; i<(~dv).size(); ++i )
525  sum += sq( (~dv)[i] );
526  return std::sqrt( sum );
527 }
528 //*************************************************************************************************
529 
530 
531 //*************************************************************************************************
544 template< typename VT // Type of the dense vector
545  , bool TF > // Transpose flag
546 const typename VT::ElementType sqrLength( const DenseVector<VT,TF>& dv )
547 {
548  typedef typename VT::ElementType ElementType;
549 
551 
552  ElementType sum( 0 );
553  for( size_t i=0UL; i<(~dv).size(); ++i )
554  sum += sq( (~dv)[i] );
555  return sum;
556 }
557 //*************************************************************************************************
558 
559 
560 //*************************************************************************************************
572 template< typename VT // Type of the dense vector
573  , bool TF > // Transpose flag
574 const typename VT::ElementType min( const DenseVector<VT,TF>& dv )
575 {
576  using blaze::min;
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 minimum( a[0] );
586  for( size_t i=1UL; i<a.size(); ++i )
587  minimum = min( minimum, a[i] );
588  return minimum;
589 }
590 //*************************************************************************************************
591 
592 
593 //*************************************************************************************************
605 template< typename VT // Type of the dense vector
606  , bool TF > // Transpose flag
607 const typename VT::ElementType max( const DenseVector<VT,TF>& dv )
608 {
609  using blaze::max;
610 
611  typedef typename VT::ElementType ET;
612  typedef typename VT::CompositeType CT;
613 
614  CT a( ~dv ); // Evaluation of the dense vector operand
615 
616  if( a.size() == 0UL ) return ET();
617 
618  ET maximum( a[0] );
619  for( size_t i=1UL; i<a.size(); ++i )
620  maximum = max( maximum, a[i] );
621  return maximum;
622 }
623 //*************************************************************************************************
624 
625 } // namespace blaze
626 
627 #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:1649
Constraint on the data type.
Header file for mathematical functions.
Header file for basic type definitions.
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:264
double Type
Return type of the functions for integral and double arguments.
Definition: CMathTrait.h:79
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:2507
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
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:861
Header file for the vector transpose flag types.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2511
const MT::ElementType min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1602
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:2505
CMathTrait< typename VT::ElementType >::Type length(const DenseVector< VT, TF > &dv)
Calculation of the dense vector length .
Definition: DenseVector.h:516
const Type & ConstReference
Reference to a constant matrix value.
Definition: CompressedMatrix.h:2509
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.
Header file for the RemoveReference type trait.
Header file for the cmath trait.
bool isUniform(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is a uniform matrix.
Definition: DenseMatrix.h:910
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
const MultExprTrait< T, T >::Type sq(const T &a)
Squaring the given value/object.
Definition: Square.h:65
const VT::ElementType sqrLength(const DenseVector< VT, TF > &dv)
Calculation of the dense vector square length .
Definition: DenseVector.h:546