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/Exception.h>
48 #include <blaze/math/shims/Equal.h>
51 #include <blaze/math/shims/IsNaN.h>
53 #include <blaze/math/shims/Pow2.h>
54 #include <blaze/math/shims/Sqrt.h>
60 #include <blaze/util/Assert.h>
63 #include <blaze/util/EnableIf.h>
64 #include <blaze/util/Types.h>
67 
68 
69 namespace blaze {
70 
71 //=================================================================================================
72 //
73 // GLOBAL OPERATORS
74 //
75 //=================================================================================================
76 
77 //*************************************************************************************************
80 template< typename T1, bool TF1, typename T2, bool TF2 >
81 inline bool operator==( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
82 
83 template< typename T1, bool TF1, typename T2, bool TF2 >
84 inline bool operator==( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs );
85 
86 template< typename T1, bool TF1, typename T2, bool TF2 >
87 inline bool operator==( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
88 
89 template< typename T1, typename T2, bool TF >
90 inline EnableIf_< IsNumeric<T2>, bool > operator==( const DenseVector<T1,TF>& vec, T2 scalar );
91 
92 template< typename T1, typename T2, bool TF >
93 inline EnableIf_< IsNumeric<T1>, bool > operator==( T1 scalar, const DenseVector<T2,TF>& vec );
94 
95 template< typename T1, bool TF1, typename T2, bool TF2 >
96 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
97 
98 template< typename T1, bool TF1, typename T2, bool TF2 >
99 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs );
100 
101 template< typename T1, bool TF1, typename T2, bool TF2 >
102 inline bool operator!=( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
103 
104 template< typename T1, typename T2, bool TF >
105 inline EnableIf_< IsNumeric<T2>, bool > operator!=( const DenseVector<T1,TF>& vec, T2 scalar );
106 
107 template< typename T1, typename T2, bool TF >
108 inline EnableIf_< IsNumeric<T1>, bool > operator!=( T1 scalar, const DenseVector<T2,TF>& vec );
109 
110 template< typename VT, bool TF, typename ST >
111 inline EnableIf_< IsNumeric<ST>, VT& > operator*=( DenseVector<VT,TF>& vec, ST scalar );
112 
113 template< typename VT, bool TF, typename ST >
114 inline EnableIf_< IsNumeric<ST>, VT& > operator*=( DenseVector<VT,TF>&& vec, ST scalar );
115 
116 template< typename VT, bool TF, typename ST >
117 inline EnableIf_< IsNumeric<ST>, VT& > operator/=( DenseVector<VT,TF>& vec, ST scalar );
118 
119 template< typename VT, bool TF, typename ST >
120 inline EnableIf_< IsNumeric<ST>, VT& > operator/=( DenseVector<VT,TF>&& vec, ST scalar );
122 //*************************************************************************************************
123 
124 
125 //*************************************************************************************************
133 template< typename T1 // Type of the left-hand side dense vector
134  , bool TF1 // Transpose flag of the left-hand side dense vector
135  , typename T2 // Type of the right-hand side dense vector
136  , bool TF2 > // Transpose flag of the right-hand side dense vector
137 inline bool operator==( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
138 {
139  using CT1 = CompositeType_<T1>;
140  using CT2 = CompositeType_<T2>;
141 
142  // Early exit in case the vector sizes don't match
143  if( (~lhs).size() != (~rhs).size() ) return false;
144 
145  // Evaluation of the two dense vector operands
146  CT1 a( ~lhs );
147  CT2 b( ~rhs );
148 
149  // In order to compare the two vectors, the data values of the lower-order data
150  // type are converted to the higher-order data type within the equal function.
151  for( size_t i=0; i<a.size(); ++i )
152  if( !equal( a[i], b[i] ) ) return false;
153  return true;
154 }
155 //*************************************************************************************************
156 
157 
158 //*************************************************************************************************
166 template< typename T1 // Type of the left-hand side dense vector
167  , bool TF1 // Transpose flag of the left-hand side dense vector
168  , typename T2 // Type of the right-hand side sparse vector
169  , bool TF2 > // Transpose flag of the right-hand side sparse vector
170 inline bool operator==( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
171 {
172  using CT1 = CompositeType_<T1>;
173  using CT2 = CompositeType_<T2>;
175 
176  // Early exit in case the vector sizes don't match
177  if( (~lhs).size() != (~rhs).size() ) return false;
178 
179  // Evaluation of the dense vector and sparse vector operand
180  CT1 a( ~lhs );
181  CT2 b( ~rhs );
182 
183  // In order to compare the two vectors, the data values of the lower-order data
184  // type are converted to the higher-order data type within the equal function.
185  size_t i( 0 );
186 
187  for( ConstIterator element=b.begin(); element!=b.end(); ++element, ++i ) {
188  for( ; i<element->index(); ++i ) {
189  if( !isDefault( a[i] ) ) return false;
190  }
191  if( !equal( element->value(), a[i] ) ) return false;
192  }
193  for( ; i<a.size(); ++i ) {
194  if( !isDefault( a[i] ) ) return false;
195  }
196 
197  return true;
198 }
199 //*************************************************************************************************
200 
201 
202 //*************************************************************************************************
210 template< typename T1 // Type of the left-hand side sparse vector
211  , bool TF1 // Transpose flag of the left-hand side sparse vector
212  , typename T2 // Type of the right-hand side dense vector
213  , bool TF2 > // Transpose flag of the right-hand side dense vector
214 inline bool operator==( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
215 {
216  return ( rhs == lhs );
217 }
218 //*************************************************************************************************
219 
220 
221 //*************************************************************************************************
233 template< typename T1 // Type of the left-hand side dense vector
234  , typename T2 // Type of the right-hand side scalar
235  , bool TF > // Transpose flag
236 inline EnableIf_< IsNumeric<T2>, bool > operator==( const DenseVector<T1,TF>& vec, T2 scalar )
237 {
238  using CT1 = CompositeType_<T1>;
239 
240  // Evaluation of the dense vector operand
241  CT1 a( ~vec );
242 
243  // In order to compare the vector and the scalar value, the data values of the lower-order
244  // data type are converted to the higher-order data type within the equal function.
245  for( size_t i=0; i<a.size(); ++i )
246  if( !equal( a[i], scalar ) ) return false;
247  return true;
248 }
249 //*************************************************************************************************
250 
251 
252 //*************************************************************************************************
264 template< typename T1 // Type of the left-hand side scalar
265  , typename T2 // Type of the right-hand side dense vector
266  , bool TF > // Transpose flag
267 inline EnableIf_< IsNumeric<T1>, bool > operator==( T1 scalar, const DenseVector<T2,TF>& vec )
268 {
269  return ( vec == scalar );
270 }
271 //*************************************************************************************************
272 
273 
274 //*************************************************************************************************
282 template< typename T1 // Type of the left-hand side dense vector
283  , bool TF1 // Transpose flag of the left-hand side dense vector
284  , typename T2 // Type of the right-hand side dense vector
285  , bool TF2 > // Transpose flag of the right-hand side dense vector
286 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
287 {
288  return !( lhs == rhs );
289 }
290 //*************************************************************************************************
291 
292 
293 //*************************************************************************************************
301 template< typename T1 // Type of the left-hand side dense vector
302  , bool TF1 // Transpose flag of the left-hand side dense vector
303  , typename T2 // Type of the right-hand side sparse vector
304  , bool TF2 > // Transpose flag of the right-hand side sparse vector
305 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
306 {
307  return !( lhs == rhs );
308 }
309 //*************************************************************************************************
310 
311 
312 //*************************************************************************************************
320 template< typename T1 // Type of the left-hand side sparse vector
321  , bool TF1 // Transpose flag of the left-hand side sparse vector
322  , typename T2 // Type of the right-hand side dense vector
323  , bool TF2 > // Transpose flag of the right-hand side dense vector
324 inline bool operator!=( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
325 {
326  return !( rhs == lhs );
327 }
328 //*************************************************************************************************
329 
330 
331 //*************************************************************************************************
343 template< typename T1 // Type of the left-hand side dense vector
344  , typename T2 // Type of the right-hand side scalar
345  , bool TF > // Transpose flag
346 inline EnableIf_< IsNumeric<T2>, bool > operator!=( const DenseVector<T1,TF>& vec, T2 scalar )
347 {
348  return !( vec == scalar );
349 }
350 //*************************************************************************************************
351 
352 
353 //*************************************************************************************************
365 template< typename T1 // Type of the left-hand side scalar
366  , typename T2 // Type of the right-hand side vector
367  , bool TF > // Transpose flag
368 inline EnableIf_< IsNumeric<T1>, bool > operator!=( T1 scalar, const DenseVector<T2,TF>& vec )
369 {
370  return !( vec == scalar );
371 }
372 //*************************************************************************************************
373 
374 
375 //*************************************************************************************************
388 template< typename VT // Type of the left-hand side dense vector
389  , bool TF // Transpose flag
390  , typename ST > // Data type of the right-hand side scalar
391 inline EnableIf_< IsNumeric<ST>, VT& > operator*=( DenseVector<VT,TF>& vec, ST scalar )
392 {
394  if( !tryMult( ~vec, 0UL, (~vec).size(), scalar ) ) {
395  BLAZE_THROW_INVALID_ARGUMENT( "Invalid scaling of restricted vector" );
396  }
397  }
398 
399  BLAZE_DECLTYPE_AUTO( left, derestrict( ~vec ) );
400 
401  smpAssign( left, left * scalar );
402 
403  BLAZE_INTERNAL_ASSERT( isIntact( ~vec ), "Invariant violation detected" );
404 
405  return ~vec;
406 }
407 //*************************************************************************************************
408 
409 
410 //*************************************************************************************************
423 template< typename VT // Type of the left-hand side dense vector
424  , bool TF // Transpose flag
425  , typename ST > // Data type of the right-hand side scalar
426 inline EnableIf_< IsNumeric<ST>, VT& > operator*=( DenseVector<VT,TF>&& vec, ST scalar )
427 {
428  return operator*=( ~vec, scalar );
429 }
430 //*************************************************************************************************
431 
432 
433 //*************************************************************************************************
448 template< typename VT // Type of the left-hand side dense vector
449  , bool TF // Transpose flag
450  , typename ST > // Data type of the right-hand side scalar
451 inline EnableIf_< IsNumeric<ST>, VT& > operator/=( DenseVector<VT,TF>& vec, ST scalar )
452 {
453  BLAZE_USER_ASSERT( !isZero( scalar ), "Division by zero detected" );
454 
456  if( !tryDiv( ~vec, 0UL, (~vec).size(), scalar ) ) {
457  BLAZE_THROW_INVALID_ARGUMENT( "Invalid scaling of restricted vector" );
458  }
459  }
460 
461  BLAZE_DECLTYPE_AUTO( left, derestrict( ~vec ) );
462 
463  smpAssign( left, left / scalar );
464 
465  BLAZE_INTERNAL_ASSERT( isIntact( ~vec ), "Invariant violation detected" );
466 
467  return ~vec;
468 }
469 //*************************************************************************************************
470 
471 
472 //*************************************************************************************************
487 template< typename VT // Type of the left-hand side dense vector
488  , bool TF // Transpose flag
489  , typename ST > // Data type of the right-hand side scalar
490 inline EnableIf_< IsNumeric<ST>, VT& > operator/=( DenseVector<VT,TF>&& vec, ST scalar )
491 {
492  return operator/=( ~vec, scalar );
493 }
494 //*************************************************************************************************
495 
496 
497 
498 
499 //=================================================================================================
500 //
501 // GLOBAL FUNCTIONS
502 //
503 //=================================================================================================
504 
505 //*************************************************************************************************
508 template< typename VT, bool TF >
509 bool isnan( const DenseVector<VT,TF>& dv );
510 
511 template< typename VT, bool TF >
512 bool isDivisor( const DenseVector<VT,TF>& dv );
513 
514 template< typename VT, bool TF >
515 bool isUniform( const DenseVector<VT,TF>& dv );
516 
517 template< typename VT, bool TF >
519 
520 template< typename VT, bool TF >
521 inline auto length( const DenseVector<VT,TF>& dv ) -> decltype( sqrt( sqrLength( ~dv ) ) );
522 
523 template< typename VT, bool TF >
524 const ElementType_<VT> min( const DenseVector<VT,TF>& dv );
525 
526 template< typename VT, bool TF >
527 const ElementType_<VT> max( const DenseVector<VT,TF>& dv );
529 //*************************************************************************************************
530 
531 
532 //*************************************************************************************************
552 template< typename VT // Type of the dense vector
553  , bool TF > // Transpose flag
554 bool isnan( const DenseVector<VT,TF>& dv )
555 {
556  CompositeType_<VT> a( ~dv ); // Evaluation of the dense vector operand
557 
558  for( size_t i=0UL; i<a.size(); ++i ) {
559  if( isnan( a[i] ) ) return true;
560  }
561  return false;
562 }
563 //*************************************************************************************************
564 
565 
566 //*************************************************************************************************
582 template< typename VT // Type of the dense vector
583  , bool TF > // Transpose flag
584 bool isDivisor( const DenseVector<VT,TF>& dv )
585 {
586  CompositeType_<VT> a( ~dv ); // Evaluation of the dense vector operand
587 
588  for( size_t i=0UL; i<a.size(); ++i ) {
589  if( !isDivisor( a[i] ) ) return false;
590  }
591  return true;
592 }
593 //*************************************************************************************************
594 
595 
596 //*************************************************************************************************
622 template< typename VT // Type of the dense vector
623  , bool TF > // Transpose flag
624 bool isUniform( const DenseVector<VT,TF>& dv )
625 {
626  using CT = CompositeType_<VT>;
628 
629  if( IsUniform<VT>::value || (~dv).size() < 2UL )
630  return true;
631 
632  CT a( ~dv ); // Evaluation of the dense vector operand
633 
634  ConstReference cmp( a[0UL] );
635 
636  for( size_t i=1UL; i<a.size(); ++i ) {
637  if( a[i] != cmp )
638  return false;
639  }
640 
641  return true;
642 }
643 //*************************************************************************************************
644 
645 
646 //*************************************************************************************************
659 template< typename VT // Type of the dense vector
660  , bool TF > // Transpose flag
662 {
664 
666 
667  ElementType sum( 0 );
668  for( size_t i=0UL; i<(~dv).size(); ++i )
669  sum += pow2( (~dv)[i] );
670  return sum;
671 }
672 //*************************************************************************************************
673 
674 
675 //*************************************************************************************************
712 template< typename VT // Type of the dense vector
713  , bool TF > // Transpose flag
714 inline auto length( const DenseVector<VT,TF>& dv ) -> decltype( sqrt( sqrLength( ~dv ) ) )
715 {
716  return sqrt( sqrLength( ~dv ) );
717 }
718 //*************************************************************************************************
719 
720 
721 //*************************************************************************************************
733 template< typename VT // Type of the dense vector
734  , bool TF > // Transpose flag
736 {
737  using blaze::min;
738 
739  using ET = ElementType_<VT>;
740  using CT = CompositeType_<VT>;
741 
742  CT a( ~dv ); // Evaluation of the dense vector operand
743 
744  if( a.size() == 0UL ) return ET();
745 
746  ET minimum( a[0] );
747  for( size_t i=1UL; i<a.size(); ++i )
748  minimum = min( minimum, a[i] );
749  return minimum;
750 }
751 //*************************************************************************************************
752 
753 
754 //*************************************************************************************************
766 template< typename VT // Type of the dense vector
767  , bool TF > // Transpose flag
769 {
770  using blaze::max;
771 
772  using ET = ElementType_<VT>;
773  using CT = CompositeType_<VT>;
774 
775  CT a( ~dv ); // Evaluation of the dense vector operand
776 
777  if( a.size() == 0UL ) return ET();
778 
779  ET maximum( a[0] );
780  for( size_t i=1UL; i<a.size(); ++i )
781  maximum = max( maximum, a[i] );
782  return maximum;
783 }
784 //*************************************************************************************************
785 
786 } // namespace blaze
787 
788 #endif
Header file for the isnan shim.
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.This macro encapsulates the default way o...
Definition: Exception.h:235
Header file for auxiliary alias declarations.
Headerfile for the generic min algorithm.
Constraint on the data type.
#define BLAZE_USER_ASSERT(expr, msg)
Run time assertion macro for user checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_USER_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERT flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:117
Header file for basic type definitions.
Header file for the SparseVector base class.
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:661
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:1903
Header file for the DenseVector base class.
const Type & ConstReference
Reference to a constant matrix value.
Definition: CompressedMatrix.h:3083
Header file for the decltype(auto) workaround.
const ElementType_< MT > max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1950
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:714
Compile time check for data types with restricted data access.This type trait tests whether the given...
Definition: IsRestricted.h:82
bool isZero(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is 0.
Definition: DiagonalProxy.h:670
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.
Header file for the IsUniform type trait.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
EnableIf_< IsDenseMatrix< MT1 > > smpAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the SMP assignment of a matrix to a dense matrix.
Definition: DenseMatrix.h:102
Type ElementType
Type of the compressed matrix elements.
Definition: CompressedMatrix.h:3079
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:3085
Header file for the isZero shim.
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:76
Header file for the pow2 shim.
Header file for the equal shim.
bool isnan(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is not a number.
Definition: DiagonalProxy.h:710
Header file for the exception macros of the math module.
bool isUniform(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is a uniform matrix.
Definition: DenseMatrix.h:1151
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:584
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.
BLAZE_ALWAYS_INLINEdecltype(auto) constexpr pow2(const T &a) noexcept(noexcept(a *a))
Squaring the given value/object.
Definition: Pow2.h:65
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
EnableIf_< IsNumeric< ST >, MT &> operator/=(DenseMatrix< MT, SO > &mat, ST scalar)
Division assignment operator for the division of a dense matrix by a scalar value ( )...
Definition: DenseMatrix.h:655
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 isIntact(const DiagonalMatrix< MT, SO, DF > &m)
Returns whether the invariants of the given diagonal matrix are intact.
Definition: DiagonalMatrix.h:254
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:628
EnableIf_< IsNumeric< ST >, MT &> operator*=(DenseMatrix< MT, SO > &mat, ST scalar)
Multiplication assignment operator for the multiplication of a dense matrix and a scalar value ( )...
Definition: DenseMatrix.h:593
Header file for the IsRestricted type trait.
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_INTERNAL_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERTION flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:101