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>
44 #include <blaze/math/Aliases.h>
47 #include <blaze/math/Functions.h>
48 #include <blaze/math/shims/Equal.h>
51 #include <blaze/math/shims/IsNaN.h>
52 #include <blaze/math/shims/Sqrt.h>
55 #include <blaze/util/Assert.h>
57 #include <blaze/util/EnableIf.h>
58 #include <blaze/util/Types.h>
61 
62 
63 namespace blaze {
64 
65 //=================================================================================================
66 //
67 // GLOBAL OPERATORS
68 //
69 //=================================================================================================
70 
71 //*************************************************************************************************
74 template< typename T1, bool TF1, typename T2, bool TF2 >
75 inline bool operator==( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
76 
77 template< typename T1, bool TF1, typename T2, bool TF2 >
78 inline bool operator==( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs );
79 
80 template< typename T1, bool TF1, typename T2, bool TF2 >
81 inline bool operator==( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
82 
83 template< typename T1, typename T2, bool TF >
84 inline EnableIf_<IsNumeric<T2>, bool > operator==( const DenseVector<T1,TF>& vec, T2 scalar );
85 
86 template< typename T1, typename T2, bool TF >
87 inline EnableIf_<IsNumeric<T1>, bool > 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 EnableIf_<IsNumeric<T2>, bool > operator!=( const DenseVector<T1,TF>& vec, T2 scalar );
100 
101 template< typename T1, typename T2, bool TF >
102 inline EnableIf_<IsNumeric<T1>, bool > operator!=( T1 scalar, const DenseVector<T2,TF>& vec );
104 //*************************************************************************************************
105 
106 
107 //*************************************************************************************************
115 template< typename T1 // Type of the left-hand side dense vector
116  , bool TF1 // Transpose flag of the left-hand side dense vector
117  , typename T2 // Type of the right-hand side dense vector
118  , bool TF2 > // Transpose flag of the right-hand side dense vector
119 inline bool operator==( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
120 {
121  typedef CompositeType_<T1> CT1;
122  typedef CompositeType_<T2> CT2;
123 
124  // Early exit in case the vector sizes don't match
125  if( (~lhs).size() != (~rhs).size() ) return false;
126 
127  // Evaluation of the two dense vector operands
128  CT1 a( ~lhs );
129  CT2 b( ~rhs );
130 
131  // In order to compare the two vectors, the data values of the lower-order data
132  // type are converted to the higher-order data type within the equal function.
133  for( size_t i=0; i<a.size(); ++i )
134  if( !equal( a[i], b[i] ) ) return false;
135  return true;
136 }
137 //*************************************************************************************************
138 
139 
140 //*************************************************************************************************
148 template< typename T1 // Type of the left-hand side dense vector
149  , bool TF1 // Transpose flag of the left-hand side dense vector
150  , typename T2 // Type of the right-hand side sparse vector
151  , bool TF2 > // Transpose flag of the right-hand side sparse vector
152 inline bool operator==( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
153 {
154  typedef CompositeType_<T1> CT1;
155  typedef CompositeType_<T2> CT2;
157 
158  // Early exit in case the vector sizes don't match
159  if( (~lhs).size() != (~rhs).size() ) return false;
160 
161  // Evaluation of the dense vector and sparse vector operand
162  CT1 a( ~lhs );
163  CT2 b( ~rhs );
164 
165  // In order to compare the two vectors, the data values of the lower-order data
166  // type are converted to the higher-order data type within the equal function.
167  size_t i( 0 );
168 
169  for( ConstIterator element=b.begin(); element!=b.end(); ++element, ++i ) {
170  for( ; i<element->index(); ++i ) {
171  if( !isDefault( a[i] ) ) return false;
172  }
173  if( !equal( element->value(), a[i] ) ) return false;
174  }
175  for( ; i<a.size(); ++i ) {
176  if( !isDefault( a[i] ) ) return false;
177  }
178 
179  return true;
180 }
181 //*************************************************************************************************
182 
183 
184 //*************************************************************************************************
192 template< typename T1 // Type of the left-hand side sparse vector
193  , bool TF1 // Transpose flag of the left-hand side sparse vector
194  , typename T2 // Type of the right-hand side dense vector
195  , bool TF2 > // Transpose flag of the right-hand side dense vector
196 inline bool operator==( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
197 {
198  return ( rhs == lhs );
199 }
200 //*************************************************************************************************
201 
202 
203 //*************************************************************************************************
215 template< typename T1 // Type of the left-hand side dense vector
216  , typename T2 // Type of the right-hand side scalar
217  , bool TF > // Transpose flag
218 inline EnableIf_<IsNumeric<T2>, bool > operator==( const DenseVector<T1,TF>& vec, T2 scalar )
219 {
220  typedef CompositeType_<T1> CT1;
221 
222  // Evaluation of the dense vector operand
223  CT1 a( ~vec );
224 
225  // In order to compare the vector and the scalar value, the data values of the lower-order
226  // data type are converted to the higher-order data type within the equal function.
227  for( size_t i=0; i<a.size(); ++i )
228  if( !equal( a[i], scalar ) ) return false;
229  return true;
230 }
231 //*************************************************************************************************
232 
233 
234 //*************************************************************************************************
246 template< typename T1 // Type of the left-hand side scalar
247  , typename T2 // Type of the right-hand side dense vector
248  , bool TF > // Transpose flag
249 inline EnableIf_<IsNumeric<T1>, bool > operator==( T1 scalar, const DenseVector<T2,TF>& vec )
250 {
251  return ( vec == scalar );
252 }
253 //*************************************************************************************************
254 
255 
256 //*************************************************************************************************
264 template< typename T1 // Type of the left-hand side dense vector
265  , bool TF1 // Transpose flag of the left-hand side dense vector
266  , typename T2 // Type of the right-hand side dense vector
267  , bool TF2 > // Transpose flag of the right-hand side dense vector
268 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
269 {
270  return !( lhs == rhs );
271 }
272 //*************************************************************************************************
273 
274 
275 //*************************************************************************************************
283 template< typename T1 // Type of the left-hand side dense vector
284  , bool TF1 // Transpose flag of the left-hand side dense vector
285  , typename T2 // Type of the right-hand side sparse vector
286  , bool TF2 > // Transpose flag of the right-hand side sparse vector
287 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
288 {
289  return !( lhs == rhs );
290 }
291 //*************************************************************************************************
292 
293 
294 //*************************************************************************************************
302 template< typename T1 // Type of the left-hand side sparse vector
303  , bool TF1 // Transpose flag of the left-hand side sparse vector
304  , typename T2 // Type of the right-hand side dense vector
305  , bool TF2 > // Transpose flag of the right-hand side dense vector
306 inline bool operator!=( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
307 {
308  return !( rhs == lhs );
309 }
310 //*************************************************************************************************
311 
312 
313 //*************************************************************************************************
325 template< typename T1 // Type of the left-hand side dense vector
326  , typename T2 // Type of the right-hand side scalar
327  , bool TF > // Transpose flag
328 inline EnableIf_<IsNumeric<T2>, bool > operator!=( const DenseVector<T1,TF>& vec, T2 scalar )
329 {
330  return !( vec == scalar );
331 }
332 //*************************************************************************************************
333 
334 
335 //*************************************************************************************************
347 template< typename T1 // Type of the left-hand side scalar
348  , typename T2 // Type of the right-hand side vector
349  , bool TF > // Transpose flag
350 inline EnableIf_<IsNumeric<T1>, bool > operator!=( T1 scalar, const DenseVector<T2,TF>& vec )
351 {
352  return !( vec == scalar );
353 }
354 //*************************************************************************************************
355 
356 
357 
358 
359 //=================================================================================================
360 //
361 // GLOBAL FUNCTIONS
362 //
363 //=================================================================================================
364 
365 //*************************************************************************************************
368 template< typename VT, bool TF >
369 bool isnan( const DenseVector<VT,TF>& dv );
370 
371 template< typename VT, bool TF >
372 bool isDivisor( const DenseVector<VT,TF>& dv );
373 
374 template< typename VT, bool TF >
375 bool isUniform( const DenseVector<VT,TF>& dv );
376 
377 template< typename VT, bool TF >
378 const ElementType_<VT> sqrLength( const DenseVector<VT,TF>& dv );
379 
380 template< typename VT, bool TF >
381 inline auto length( const DenseVector<VT,TF>& dv ) -> decltype( sqrt( sqrLength( ~dv ) ) );
382 
383 template< typename VT, bool TF >
384 const ElementType_<VT> min( const DenseVector<VT,TF>& dv );
385 
386 template< typename VT, bool TF >
387 const ElementType_<VT> 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  CompositeType_<VT> a( ~dv ); // Evaluation of the dense vector operand
417 
418  for( size_t i=0UL; i<a.size(); ++i ) {
419  if( isnan( a[i] ) ) return true;
420  }
421  return false;
422 }
423 //*************************************************************************************************
424 
425 
426 //*************************************************************************************************
442 template< typename VT // Type of the dense vector
443  , bool TF > // Transpose flag
444 bool isDivisor( const DenseVector<VT,TF>& dv )
445 {
446  CompositeType_<VT> a( ~dv ); // Evaluation of the dense vector operand
447 
448  for( size_t i=0UL; i<a.size(); ++i ) {
449  if( !isDivisor( a[i] ) ) return false;
450  }
451  return true;
452 }
453 //*************************************************************************************************
454 
455 
456 //*************************************************************************************************
482 template< typename VT // Type of the dense vector
483  , bool TF > // Transpose flag
484 bool isUniform( const DenseVector<VT,TF>& dv )
485 {
486  typedef CompositeType_<VT> CT;
488 
489  if( (~dv).size() < 2UL )
490  return true;
491 
492  CT a( ~dv ); // Evaluation of the dense vector operand
493 
494  ConstReference cmp( (~dv)[0UL] );
495 
496  for( size_t i=1UL; i<(~dv).size(); ++i ) {
497  if( (~dv)[i] != cmp )
498  return false;
499  }
500 
501  return true;
502 }
503 //*************************************************************************************************
504 
505 
506 //*************************************************************************************************
519 template< typename VT // Type of the dense vector
520  , bool TF > // Transpose flag
522 {
524 
526 
527  ElementType sum( 0 );
528  for( size_t i=0UL; i<(~dv).size(); ++i )
529  sum += sq( (~dv)[i] );
530  return sum;
531 }
532 //*************************************************************************************************
533 
534 
535 //*************************************************************************************************
572 template< typename VT // Type of the dense vector
573  , bool TF > // Transpose flag
574 inline auto length( const DenseVector<VT,TF>& dv ) -> decltype( sqrt( sqrLength( ~dv ) ) )
575 {
576  return sqrt( sqrLength( ~dv ) );
577 }
578 //*************************************************************************************************
579 
580 
581 //*************************************************************************************************
593 template< typename VT // Type of the dense vector
594  , bool TF > // Transpose flag
596 {
597  using blaze::min;
598 
599  typedef ElementType_<VT> ET;
600  typedef CompositeType_<VT> CT;
601 
602  CT a( ~dv ); // Evaluation of the dense vector operand
603 
604  if( a.size() == 0UL ) return ET();
605 
606  ET minimum( a[0] );
607  for( size_t i=1UL; i<a.size(); ++i )
608  minimum = min( minimum, a[i] );
609  return minimum;
610 }
611 //*************************************************************************************************
612 
613 
614 //*************************************************************************************************
626 template< typename VT // Type of the dense vector
627  , bool TF > // Transpose flag
629 {
630  using blaze::max;
631 
632  typedef ElementType_<VT> ET;
633  typedef CompositeType_<VT> CT;
634 
635  CT a( ~dv ); // Evaluation of the dense vector operand
636 
637  if( a.size() == 0UL ) return ET();
638 
639  ET maximum( a[0] );
640  for( size_t i=1UL; i<a.size(); ++i )
641  maximum = max( maximum, a[i] );
642  return maximum;
643 }
644 //*************************************************************************************************
645 
646 } // namespace blaze
647 
648 #endif
Header file for the isnan shim.
Header file for auxiliary alias declarations.
Constraint on the data type.
Header file for mathematical functions.
Header file for basic type definitions.
Header file for the SparseVector base class.
BLAZE_ALWAYS_INLINE const complex< int8_t > sum(const SIMDcint8 &a) noexcept
Returns the sum of all elements in the 8-bit integral complex SIMD vector.
Definition: Reduction.h:63
Header file for the square shim.
BLAZE_ALWAYS_INLINE size_t size(const Vector< VT, TF > &vector) noexcept
Returns the current size/dimension of the vector.
Definition: Vector.h:258
const ElementType_< VT > sqrLength(const DenseVector< VT, TF > &dv)
Calculation of the dense vector square length .
Definition: DenseVector.h:521
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1669
Header file for the DenseVector base class.
const ElementType_< MT > max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1716
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:573
Header file for the vector transpose flag types.
auto length(const DenseVector< VT, TF > &dv) -> decltype(sqrt(sqrLength(~dv)))
Calculation of the dense vector length .
Definition: DenseVector.h:574
Header file for the sqrt shim.
typename T::CompositeType CompositeType_
Alias declaration for nested CompositeType type definitions.The CompositeType_ alias declaration prov...
Definition: Aliases.h:83
BLAZE_ALWAYS_INLINE constexpr MultExprTrait_< T, T > sq(const T &a) noexcept(noexcept(a *a))
Squaring the given value/object.
Definition: Square.h:66
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2647
typename T::ElementType ElementType_
Alias declaration for nested ElementType type definitions.The ElementType_ alias declaration provides...
Definition: Aliases.h:163
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.
bool isnan(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is not a number.
Definition: DiagonalProxy.h:655
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2641
const Type & ConstReference
Reference to a constant matrix value.
Definition: CompressedMatrix.h:2645
const DMatForEachExpr< MT, Sqrt, SO > sqrt(const DenseMatrix< MT, SO > &dm)
Computes the square root of each single element of the dense matrix dm.
Definition: DMatForEachExpr.h:1282
Header file for the EnableIf class template.
Header file for the IsNumeric type trait.
bool isDivisor(const DenseVector< VT, TF > &dv)
Returns whether the given dense vector is a valid divisor.
Definition: DenseVector.h:444
Header file for run time assertion macros.
bool equal(const T1 &a, const T2 &b)
Generic equality check.
Definition: Equal.h:73
#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:61
Header file for the isDefault shim.
Header file for the RemoveReference type trait.
typename T::ConstReference ConstReference_
Alias declaration for nested ConstReference type definitions.The ConstReference_ alias declaration pr...
Definition: Aliases.h:143
typename T::ConstIterator ConstIterator_
Alias declaration for nested ConstIterator type definitions.The ConstIterator_ alias declaration prov...
Definition: Aliases.h:103
bool isUniform(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is a uniform matrix.
Definition: DenseMatrix.h:982
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:110
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 isDivisor shim.
bool operator!=(const NegativeAccuracy< A > &lhs, const T &rhs)
Inequality comparison between a NegativeAccuracy object and a floating point value.
Definition: Accuracy.h:289