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/shims/Equal.h>
50 #include <blaze/math/shims/IsNaN.h>
51 #include <blaze/math/shims/Sqrt.h>
57 #include <blaze/util/Assert.h>
59 #include <blaze/util/EnableIf.h>
60 #include <blaze/util/Types.h>
63 
64 
65 namespace blaze {
66 
67 //=================================================================================================
68 //
69 // GLOBAL OPERATORS
70 //
71 //=================================================================================================
72 
73 //*************************************************************************************************
76 template< typename T1, bool TF1, typename T2, bool TF2 >
77 inline bool operator==( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
78 
79 template< typename T1, bool TF1, typename T2, bool TF2 >
80 inline bool operator==( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs );
81 
82 template< typename T1, bool TF1, typename T2, bool TF2 >
83 inline bool operator==( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
84 
85 template< typename T1, typename T2, bool TF >
86 inline EnableIf_<IsNumeric<T2>, bool > operator==( const DenseVector<T1,TF>& vec, T2 scalar );
87 
88 template< typename T1, typename T2, bool TF >
89 inline EnableIf_<IsNumeric<T1>, bool > operator==( T1 scalar, const DenseVector<T2,TF>& vec );
90 
91 template< typename T1, bool TF1, typename T2, bool TF2 >
92 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
93 
94 template< typename T1, bool TF1, typename T2, bool TF2 >
95 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs );
96 
97 template< typename T1, bool TF1, typename T2, bool TF2 >
98 inline bool operator!=( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
99 
100 template< typename T1, typename T2, bool TF >
101 inline EnableIf_<IsNumeric<T2>, bool > operator!=( const DenseVector<T1,TF>& vec, T2 scalar );
102 
103 template< typename T1, typename T2, bool TF >
104 inline EnableIf_<IsNumeric<T1>, bool > 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  using CT1 = CompositeType_<T1>;
124  using CT2 = CompositeType_<T2>;
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  using CT1 = CompositeType_<T1>;
157  using CT2 = CompositeType_<T2>;
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 EnableIf_<IsNumeric<T2>, bool > operator==( const DenseVector<T1,TF>& vec, T2 scalar )
221 {
222  using CT1 = CompositeType_<T1>;
223 
224  // Evaluation of the dense vector operand
225  CT1 a( ~vec );
226 
227  // In order to compare the vector and the scalar value, the data values of the lower-order
228  // data type are converted to the higher-order data type within the equal function.
229  for( size_t i=0; i<a.size(); ++i )
230  if( !equal( a[i], scalar ) ) return false;
231  return true;
232 }
233 //*************************************************************************************************
234 
235 
236 //*************************************************************************************************
248 template< typename T1 // Type of the left-hand side scalar
249  , typename T2 // Type of the right-hand side dense vector
250  , bool TF > // Transpose flag
251 inline EnableIf_<IsNumeric<T1>, bool > operator==( T1 scalar, const DenseVector<T2,TF>& vec )
252 {
253  return ( vec == scalar );
254 }
255 //*************************************************************************************************
256 
257 
258 //*************************************************************************************************
266 template< typename T1 // Type of the left-hand side dense vector
267  , bool TF1 // Transpose flag of the left-hand side dense vector
268  , typename T2 // Type of the right-hand side dense vector
269  , bool TF2 > // Transpose flag of the right-hand side dense vector
270 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
271 {
272  return !( lhs == rhs );
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 sparse vector
288  , bool TF2 > // Transpose flag of the right-hand side sparse vector
289 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
290 {
291  return !( lhs == rhs );
292 }
293 //*************************************************************************************************
294 
295 
296 //*************************************************************************************************
304 template< typename T1 // Type of the left-hand side sparse vector
305  , bool TF1 // Transpose flag of the left-hand side sparse vector
306  , typename T2 // Type of the right-hand side dense vector
307  , bool TF2 > // Transpose flag of the right-hand side dense vector
308 inline bool operator!=( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
309 {
310  return !( rhs == lhs );
311 }
312 //*************************************************************************************************
313 
314 
315 //*************************************************************************************************
327 template< typename T1 // Type of the left-hand side dense vector
328  , typename T2 // Type of the right-hand side scalar
329  , bool TF > // Transpose flag
330 inline EnableIf_<IsNumeric<T2>, bool > operator!=( const DenseVector<T1,TF>& vec, T2 scalar )
331 {
332  return !( vec == scalar );
333 }
334 //*************************************************************************************************
335 
336 
337 //*************************************************************************************************
349 template< typename T1 // Type of the left-hand side scalar
350  , typename T2 // Type of the right-hand side vector
351  , bool TF > // Transpose flag
352 inline EnableIf_<IsNumeric<T1>, bool > operator!=( T1 scalar, const DenseVector<T2,TF>& vec )
353 {
354  return !( vec == scalar );
355 }
356 //*************************************************************************************************
357 
358 
359 
360 
361 //=================================================================================================
362 //
363 // GLOBAL FUNCTIONS
364 //
365 //=================================================================================================
366 
367 //*************************************************************************************************
370 template< typename VT, bool TF >
371 bool isnan( const DenseVector<VT,TF>& dv );
372 
373 template< typename VT, bool TF >
374 bool isDivisor( const DenseVector<VT,TF>& dv );
375 
376 template< typename VT, bool TF >
377 bool isUniform( const DenseVector<VT,TF>& dv );
378 
379 template< typename VT, bool TF >
381 
382 template< typename VT, bool TF >
383 inline auto length( const DenseVector<VT,TF>& dv ) -> decltype( sqrt( sqrLength( ~dv ) ) );
384 
385 template< typename VT, bool TF >
386 const ElementType_<VT> min( const DenseVector<VT,TF>& dv );
387 
388 template< typename VT, bool TF >
389 const ElementType_<VT> max( const DenseVector<VT,TF>& dv );
391 //*************************************************************************************************
392 
393 
394 //*************************************************************************************************
414 template< typename VT // Type of the dense vector
415  , bool TF > // Transpose flag
416 bool isnan( const DenseVector<VT,TF>& dv )
417 {
418  CompositeType_<VT> 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 //*************************************************************************************************
444 template< typename VT // Type of the dense vector
445  , bool TF > // Transpose flag
446 bool isDivisor( const DenseVector<VT,TF>& dv )
447 {
448  CompositeType_<VT> a( ~dv ); // Evaluation of the dense vector operand
449 
450  for( size_t i=0UL; i<a.size(); ++i ) {
451  if( !isDivisor( a[i] ) ) return false;
452  }
453  return true;
454 }
455 //*************************************************************************************************
456 
457 
458 //*************************************************************************************************
484 template< typename VT // Type of the dense vector
485  , bool TF > // Transpose flag
486 bool isUniform( const DenseVector<VT,TF>& dv )
487 {
488  using CT = CompositeType_<VT>;
490 
491  if( IsUniform<VT>::value || (~dv).size() < 2UL )
492  return true;
493 
494  CT a( ~dv ); // Evaluation of the dense vector operand
495 
496  ConstReference cmp( a[0UL] );
497 
498  for( size_t i=1UL; i<a.size(); ++i ) {
499  if( a[i] != cmp )
500  return false;
501  }
502 
503  return true;
504 }
505 //*************************************************************************************************
506 
507 
508 //*************************************************************************************************
521 template< typename VT // Type of the dense vector
522  , bool TF > // Transpose flag
524 {
526 
528 
529  ElementType sum( 0 );
530  for( size_t i=0UL; i<(~dv).size(); ++i )
531  sum += sq( (~dv)[i] );
532  return sum;
533 }
534 //*************************************************************************************************
535 
536 
537 //*************************************************************************************************
574 template< typename VT // Type of the dense vector
575  , bool TF > // Transpose flag
576 inline auto length( const DenseVector<VT,TF>& dv ) -> decltype( sqrt( sqrLength( ~dv ) ) )
577 {
578  return sqrt( sqrLength( ~dv ) );
579 }
580 //*************************************************************************************************
581 
582 
583 //*************************************************************************************************
595 template< typename VT // Type of the dense vector
596  , bool TF > // Transpose flag
598 {
599  using blaze::min;
600 
601  using ET = ElementType_<VT>;
602  using CT = CompositeType_<VT>;
603 
604  CT a( ~dv ); // Evaluation of the dense vector operand
605 
606  if( a.size() == 0UL ) return ET();
607 
608  ET minimum( a[0] );
609  for( size_t i=1UL; i<a.size(); ++i )
610  minimum = min( minimum, a[i] );
611  return minimum;
612 }
613 //*************************************************************************************************
614 
615 
616 //*************************************************************************************************
628 template< typename VT // Type of the dense vector
629  , bool TF > // Transpose flag
631 {
632  using blaze::max;
633 
634  using ET = ElementType_<VT>;
635  using CT = CompositeType_<VT>;
636 
637  CT a( ~dv ); // Evaluation of the dense vector operand
638 
639  if( a.size() == 0UL ) return ET();
640 
641  ET maximum( a[0] );
642  for( size_t i=1UL; i<a.size(); ++i )
643  maximum = max( maximum, a[i] );
644  return maximum;
645 }
646 //*************************************************************************************************
647 
648 } // namespace blaze
649 
650 #endif
Header file for the isnan shim.
Header file for auxiliary alias declarations.
Headerfile for the generic min algorithm.
Constraint on the data type.
Header file for basic type definitions.
Header file for the SparseVector base class.
Header file for the square shim.
constexpr bool equal(const T1 &a, const T2 &b)
Generic equality check.
Definition: Equal.h:76
BLAZE_ALWAYS_INLINE size_t size(const Vector< VT, TF > &vector) noexcept
Returns the current size/dimension of the vector.
Definition: Vector.h:265
const ElementType_< VT > sqrLength(const DenseVector< VT, TF > &dv)
Calculation of the square length (magnitude) of the dense vector .
Definition: DenseVector.h:523
Compile time check for uniform vectors and matrices.This type trait tests whether or not the given te...
Definition: IsUniform.h:67
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1762
Header file for the DenseVector base class.
const Type & ConstReference
Reference to a constant matrix value.
Definition: CompressedMatrix.h:3085
const ElementType_< MT > max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1809
Header file for the vector transpose flag types.
auto length(const DenseVector< VT, TF > &dv) -> decltype(sqrt(sqrLength(~dv)))
Calculation of the length (magnitude) of the dense vector .
Definition: DenseVector.h:576
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
Headerfile for the generic max algorithm.
BLAZE_ALWAYS_INLINE constexpr MultExprTrait_< T, T > sq(const T &a) noexcept(noexcept(a *a))
Squaring the given value/object.
Definition: Square.h:66
Header file for the IsUniform type trait.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
Type ElementType
Type of the compressed matrix elements.
Definition: CompressedMatrix.h:3081
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:3087
constexpr bool operator==(const NegativeAccuracy< A > &lhs, const T &rhs)
Equality comparison between a NegativeAccuracy object and a floating point value. ...
Definition: Accuracy.h:250
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:682
bool isUniform(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is a uniform matrix.
Definition: DenseMatrix.h:1010
constexpr bool operator!=(const NegativeAccuracy< A > &lhs, const T &rhs)
Inequality comparison between a NegativeAccuracy object and a floating point value.
Definition: Accuracy.h:290
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:446
BLAZE_ALWAYS_INLINE ValueType_< T > sum(const SIMDi8< T > &a) noexcept
Returns the sum of all elements in the 8-bit integral SIMD vector.
Definition: Reduction.h:65
Header file for run time assertion macros.
#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
decltype(auto) sqrt(const DenseMatrix< MT, SO > &dm)
Computes the square root of each single element of the dense matrix dm.
Definition: DMatMapExpr.h:1448
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:130
Header file for the isDivisor shim.
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:600