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 <blaze/math/Aliases.h>
45 #include <blaze/math/Exception.h>
46 #include <blaze/math/shims/Equal.h>
48 #include <blaze/math/shims/IsNaN.h>
50 #include <blaze/math/shims/Pow2.h>
51 #include <blaze/math/shims/Sqrt.h>
55 #include <blaze/util/Assert.h>
58 #include <blaze/util/EnableIf.h>
59 #include <blaze/util/Types.h>
62 
63 
64 namespace blaze {
65 
66 //=================================================================================================
67 //
68 // GLOBAL OPERATORS
69 //
70 //=================================================================================================
71 
72 //*************************************************************************************************
75 template< typename T1, typename T2, bool TF >
76 auto operator==( const DenseVector<T1,TF>& vec, T2 scalar )
77  -> EnableIf_t< IsNumeric_v<T2>, bool >;
78 
79 template< typename T1, typename T2, bool TF >
80 auto operator==( T1 scalar, const DenseVector<T2,TF>& vec )
81  -> EnableIf_t< IsNumeric_v<T1>, bool >;
82 
83 template< typename T1, typename T2, bool TF >
84 auto operator!=( const DenseVector<T1,TF>& vec, T2 scalar )
85  -> EnableIf_t< IsNumeric_v<T2>, bool >;
86 
87 template< typename T1, typename T2, bool TF >
88 auto operator!=( T1 scalar, const DenseVector<T2,TF>& vec )
89  -> EnableIf_t< IsNumeric_v<T1>, bool >;
90 
91 template< typename VT, bool TF, typename ST >
92 auto operator*=( DenseVector<VT,TF>& vec, ST scalar )
93  -> EnableIf_t< IsNumeric_v<ST>, VT& >;
94 
95 template< typename VT, bool TF, typename ST >
96 auto operator*=( DenseVector<VT,TF>&& vec, ST scalar )
97  -> EnableIf_t< IsNumeric_v<ST>, VT& >;
98 
99 template< typename VT, bool TF, typename ST >
100 auto operator/=( DenseVector<VT,TF>& vec, ST scalar )
101  -> EnableIf_t< IsNumeric_v<ST>, VT& >;
102 
103 template< typename VT, bool TF, typename ST >
104 auto operator/=( DenseVector<VT,TF>&& vec, ST scalar )
105  -> EnableIf_t< IsNumeric_v<ST>, VT& >;
107 //*************************************************************************************************
108 
109 
110 //*************************************************************************************************
122 template< typename T1 // Type of the left-hand side dense vector
123  , typename T2 // Type of the right-hand side scalar
124  , bool TF > // Transpose flag
125 inline auto operator==( const DenseVector<T1,TF>& vec, T2 scalar )
126  -> EnableIf_t< IsNumeric_v<T2>, bool >
127 {
128  using CT1 = CompositeType_t<T1>;
129 
130  // Evaluation of the dense vector operand
131  CT1 a( ~vec );
132 
133  // In order to compare the vector and the scalar value, the data values of the lower-order
134  // data 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], scalar ) ) return false;
137  return true;
138 }
139 //*************************************************************************************************
140 
141 
142 //*************************************************************************************************
154 template< typename T1 // Type of the left-hand side scalar
155  , typename T2 // Type of the right-hand side dense vector
156  , bool TF > // Transpose flag
157 inline auto operator==( T1 scalar, const DenseVector<T2,TF>& vec )
158  -> EnableIf_t< IsNumeric_v<T1>, bool >
159 {
160  return ( vec == scalar );
161 }
162 //*************************************************************************************************
163 
164 
165 //*************************************************************************************************
177 template< typename T1 // Type of the left-hand side dense vector
178  , typename T2 // Type of the right-hand side scalar
179  , bool TF > // Transpose flag
180 inline auto operator!=( const DenseVector<T1,TF>& vec, T2 scalar )
181  -> EnableIf_t< IsNumeric_v<T2>, bool >
182 {
183  return !( vec == scalar );
184 }
185 //*************************************************************************************************
186 
187 
188 //*************************************************************************************************
200 template< typename T1 // Type of the left-hand side scalar
201  , typename T2 // Type of the right-hand side vector
202  , bool TF > // Transpose flag
203 inline auto operator!=( T1 scalar, const DenseVector<T2,TF>& vec )
204  -> EnableIf_t< IsNumeric_v<T1>, bool >
205 {
206  return !( vec == scalar );
207 }
208 //*************************************************************************************************
209 
210 
211 //*************************************************************************************************
224 template< typename VT // Type of the left-hand side dense vector
225  , bool TF // Transpose flag
226  , typename ST > // Data type of the right-hand side scalar
227 inline auto operator*=( DenseVector<VT,TF>& vec, ST scalar )
229 {
230  if( IsRestricted_v<VT> ) {
231  if( !tryMult( ~vec, 0UL, (~vec).size(), scalar ) ) {
232  BLAZE_THROW_INVALID_ARGUMENT( "Invalid scaling of restricted vector" );
233  }
234  }
235 
236  BLAZE_DECLTYPE_AUTO( left, derestrict( ~vec ) );
237 
238  smpAssign( left, left * scalar );
239 
240  BLAZE_INTERNAL_ASSERT( isIntact( ~vec ), "Invariant violation detected" );
241 
242  return ~vec;
243 }
244 //*************************************************************************************************
245 
246 
247 //*************************************************************************************************
260 template< typename VT // Type of the left-hand side dense vector
261  , bool TF // Transpose flag
262  , typename ST > // Data type of the right-hand side scalar
263 inline auto operator*=( DenseVector<VT,TF>&& vec, ST scalar )
265 {
266  return operator*=( ~vec, scalar );
267 }
268 //*************************************************************************************************
269 
270 
271 //*************************************************************************************************
286 template< typename VT // Type of the left-hand side dense vector
287  , bool TF // Transpose flag
288  , typename ST > // Data type of the right-hand side scalar
289 inline auto operator/=( DenseVector<VT,TF>& vec, ST scalar )
291 {
292  BLAZE_USER_ASSERT( !isZero( scalar ), "Division by zero detected" );
293 
294  if( IsRestricted_v<VT> ) {
295  if( !tryDiv( ~vec, 0UL, (~vec).size(), scalar ) ) {
296  BLAZE_THROW_INVALID_ARGUMENT( "Invalid scaling of restricted vector" );
297  }
298  }
299 
300  BLAZE_DECLTYPE_AUTO( left, derestrict( ~vec ) );
301 
302  smpAssign( left, left / scalar );
303 
304  BLAZE_INTERNAL_ASSERT( isIntact( ~vec ), "Invariant violation detected" );
305 
306  return ~vec;
307 }
308 //*************************************************************************************************
309 
310 
311 //*************************************************************************************************
326 template< typename VT // Type of the left-hand side dense vector
327  , bool TF // Transpose flag
328  , typename ST > // Data type of the right-hand side scalar
329 inline auto operator/=( DenseVector<VT,TF>&& vec, ST scalar )
331 {
332  return operator/=( ~vec, scalar );
333 }
334 //*************************************************************************************************
335 
336 
337 
338 
339 //=================================================================================================
340 //
341 // GLOBAL FUNCTIONS
342 //
343 //=================================================================================================
344 
345 //*************************************************************************************************
348 template< typename VT, bool TF >
349 bool isnan( const DenseVector<VT,TF>& dv );
350 
351 template< typename VT, bool TF >
352 bool isDivisor( const DenseVector<VT,TF>& dv );
353 
354 template< bool RF, typename VT, bool TF >
355 bool isUniform( const DenseVector<VT,TF>& dv );
356 
357 template< typename VT, bool TF >
358 const ElementType_t<VT> sqrLength( const DenseVector<VT,TF>& dv );
359 
360 template< typename VT, bool TF >
361 auto length( const DenseVector<VT,TF>& dv ) -> decltype( sqrt( sqrLength( ~dv ) ) );
363 //*************************************************************************************************
364 
365 
366 //*************************************************************************************************
386 template< typename VT // Type of the dense vector
387  , bool TF > // Transpose flag
388 bool isnan( const DenseVector<VT,TF>& dv )
389 {
390  CompositeType_t<VT> a( ~dv ); // Evaluation of the dense vector operand
391 
392  for( size_t i=0UL; i<a.size(); ++i ) {
393  if( isnan( a[i] ) ) return true;
394  }
395  return false;
396 }
397 //*************************************************************************************************
398 
399 
400 //*************************************************************************************************
416 template< typename VT // Type of the dense vector
417  , bool TF > // Transpose flag
418 bool isDivisor( const DenseVector<VT,TF>& dv )
419 {
420  CompositeType_t<VT> a( ~dv ); // Evaluation of the dense vector operand
421 
422  for( size_t i=0UL; i<a.size(); ++i ) {
423  if( !isDivisor( a[i] ) ) return false;
424  }
425  return true;
426 }
427 //*************************************************************************************************
428 
429 
430 //*************************************************************************************************
463 template< bool RF // Relaxation flag
464  , typename VT // Type of the dense vector
465  , bool TF > // Transpose flag
466 bool isUniform( const DenseVector<VT,TF>& dv )
467 {
468  if( IsUniform_v<VT> || (~dv).size() < 2UL )
469  return true;
470 
471  CompositeType_t<VT> a( ~dv ); // Evaluation of the dense vector operand
472 
473  const auto& cmp( a[0UL] );
474 
475  for( size_t i=1UL; i<a.size(); ++i ) {
476  if( !equal<RF>( a[i], cmp ) )
477  return false;
478  }
479 
480  return true;
481 }
482 //*************************************************************************************************
483 
484 
485 //*************************************************************************************************
518 template< bool RF // Relaxation flag
519  , typename VT // Type of the dense vector
520  , bool TF > // Transpose flag
521 bool isZero( const DenseVector<VT,TF>& dv )
522 {
523  if( IsZero_v<VT> || (~dv).size() == 0UL )
524  return true;
525 
526  CompositeType_t<VT> a( ~dv ); // Evaluation of the dense vector operand
527 
528  for( size_t i=0UL; i<a.size(); ++i ) {
529  if( !isZero<RF>( a[i] ) )
530  return false;
531  }
532 
533  return true;
534 }
535 //*************************************************************************************************
536 
537 
538 //*************************************************************************************************
551 template< typename VT // Type of the dense vector
552  , bool TF > // Transpose flag
554 {
556 
558 
559  ElementType sum( 0 );
560  for( size_t i=0UL; i<(~dv).size(); ++i )
561  sum += pow2( (~dv)[i] );
562  return sum;
563 }
564 //*************************************************************************************************
565 
566 
567 //*************************************************************************************************
604 template< typename VT // Type of the dense vector
605  , bool TF > // Transpose flag
606 inline auto length( const DenseVector<VT,TF>& dv ) -> decltype( sqrt( sqrLength( ~dv ) ) )
607 {
608  return sqrt( sqrLength( ~dv ) );
609 }
610 //*************************************************************************************************
611 
612 } // namespace blaze
613 
614 #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.
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
auto operator/=(DenseMatrix< MT, SO > &mat, ST scalar) -> EnableIf_t< IsNumeric_v< ST >, MT & >
Division assignment operator for the division of a dense matrix by a scalar value ( )...
Definition: DenseMatrix.h:354
Header file for basic type definitions.
Header file for the isZero shim.
Header file for the DenseVector base class.
Header file for the decltype(auto) workaround.
typename T::ElementType ElementType_t
Alias declaration for nested ElementType type definitions.The ElementType_t alias declaration provide...
Definition: Aliases.h:170
auto length(const DenseVector< VT, TF > &dv) -> decltype(sqrt(sqrLength(~dv)))
Calculation of the length (magnitude) of the dense vector .
Definition: DenseVector.h:606
bool isZero(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is 0.
Definition: DiagonalProxy.h:673
typename EnableIf< Condition, T >::Type EnableIf_t
Auxiliary type for the EnableIf class template.The EnableIf_t alias declaration provides a convenient...
Definition: EnableIf.h:138
Header file for the sqrt shim.
Header file for the IsUniform type trait.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
const ElementType_t< VT > sqrLength(const DenseVector< VT, TF > &dv)
Calculation of the square length (magnitude) of the dense vector .
Definition: DenseVector.h:553
Type ElementType
Type of the compressed matrix elements.
Definition: CompressedMatrix.h:3080
decltype(auto) sum(const DenseMatrix< MT, SO > &dm)
Reduces the given dense matrix by means of addition.
Definition: DMatReduceExpr.h:2146
constexpr bool operator==(const NegativeAccuracy< A > &lhs, const T &rhs)
Equality comparison between a NegativeAccuracy object and a floating point value. ...
Definition: Accuracy.h:253
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:713
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:849
constexpr bool operator!=(const NegativeAccuracy< A > &lhs, const T &rhs)
Inequality comparison between a NegativeAccuracy object and a floating point value.
Definition: Accuracy.h:293
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:418
Header file for run time assertion macros.
typename T::CompositeType CompositeType_t
Alias declaration for nested CompositeType type definitions.The CompositeType_t alias declaration pro...
Definition: Aliases.h:90
#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 IsZero type trait.
decltype(auto) pow2(const Proxy< PT, RT > &proxy)
Computing the square value of the represented element.
Definition: Proxy.h:1384
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition: Matrix.h:530
auto smpAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs) -> EnableIf_t< IsDenseMatrix_v< MT1 > >
Default implementation of the SMP assignment of a matrix to a dense matrix.
Definition: DenseMatrix.h:100
Header file for the RemoveReference type trait.
decltype(auto) sqrt(const DenseMatrix< MT, SO > &dm)
Computes the square root of each single element of the dense matrix dm.
Definition: DMatMapExpr.h:1453
auto operator*=(DenseMatrix< MT, SO > &mat, ST scalar) -> EnableIf_t< IsNumeric_v< ST >, MT & >
Multiplication assignment operator for the multiplication of a dense matrix and a scalar value ( )...
Definition: DenseMatrix.h:290
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:263
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
bool equal(const SharedValue< T1 > &lhs, const SharedValue< T2 > &rhs)
Equality check for a two shared values.
Definition: SharedValue.h:342