Blaze  3.6
SparseVector.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_SPARSE_SPARSEVECTOR_H_
36 #define _BLAZE_MATH_SPARSE_SPARSEVECTOR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <utility>
44 #include <blaze/math/Aliases.h>
45 #include <blaze/math/Exception.h>
47 #include <blaze/math/shims/Equal.h>
49 #include <blaze/math/shims/IsNaN.h>
51 #include <blaze/math/shims/Pow2.h>
52 #include <blaze/math/shims/Sqrt.h>
62 #include <blaze/util/Assert.h>
63 #include <blaze/util/mpl/If.h>
64 #include <blaze/util/Types.h>
69 
70 
71 namespace blaze {
72 
73 //=================================================================================================
74 //
75 // GLOBAL OPERATORS
76 //
77 //=================================================================================================
78 
79 //*************************************************************************************************
82 template< typename VT, bool TF, typename ST >
83 auto operator*=( SparseVector<VT,TF>& vec, ST scalar )
84  -> EnableIf_t< IsNumeric_v<ST>, VT& >;
85 
86 template< typename VT, bool TF, typename ST >
87 auto operator*=( SparseVector<VT,TF>&& vec, ST scalar )
88  -> EnableIf_t< IsNumeric_v<ST>, VT& >;
89 
90 template< typename VT, bool TF, typename ST >
91 auto operator/=( SparseVector<VT,TF>& vec, ST scalar )
92  -> EnableIf_t< IsNumeric_v<ST>, VT& >;
93 
94 template< typename VT, bool TF, typename ST >
95 auto operator/=( SparseVector<VT,TF>&& vec, ST scalar )
96  -> EnableIf_t< IsNumeric_v<ST>, VT& >;
98 //*************************************************************************************************
99 
100 
101 //*************************************************************************************************
114 template< typename VT // Type of the left-hand side sparse vector
115  , bool TF // Transpose flag
116  , typename ST > // Data type of the right-hand side scalar
117 inline auto operator*=( SparseVector<VT,TF>& vec, ST scalar )
119 {
120  if( IsRestricted_v<VT> ) {
121  if( !tryMult( ~vec, 0UL, (~vec).size(), scalar ) ) {
122  BLAZE_THROW_INVALID_ARGUMENT( "Invalid scaling of restricted vector" );
123  }
124  }
125 
126  if( !IsResizable_v< ElementType_t<VT> > && isZero( scalar ) )
127  {
128  reset( ~vec );
129  }
130  else
131  {
132  decltype(auto) left( derestrict( ~vec ) );
133 
134  const auto last( left.end() );
135  for( auto element=left.begin(); element!=last; ++element ) {
136  element->value() *= scalar;
137  }
138  }
139 
140  BLAZE_INTERNAL_ASSERT( isIntact( ~vec ), "Invariant violation detected" );
141 
142  return ~vec;
143 }
144 //*************************************************************************************************
145 
146 
147 //*************************************************************************************************
160 template< typename VT // Type of the left-hand side sparse vector
161  , bool TF // Transpose flag
162  , typename ST > // Data type of the right-hand side scalar
163 inline auto operator*=( SparseVector<VT,TF>&& vec, ST scalar )
165 {
166  return operator*=( ~vec, scalar );
167 }
168 //*************************************************************************************************
169 
170 
171 //*************************************************************************************************
186 template< typename VT // Type of the left-hand side sparse vector
187  , bool TF // Transpose flag
188  , typename ST > // Data type of the right-hand side scalar
189 inline auto operator/=( SparseVector<VT,TF>& vec, ST scalar )
191 {
192  BLAZE_USER_ASSERT( !isZero( scalar ), "Division by zero detected" );
193 
194  if( IsRestricted_v<VT> ) {
195  if( !tryDiv( ~vec, 0UL, (~vec).size(), scalar ) ) {
196  BLAZE_THROW_INVALID_ARGUMENT( "Invalid scaling of restricted vector" );
197  }
198  }
199 
201  IsFloatingPoint_v< UnderlyingBuiltin_t<ST> >
202  , If_t< IsComplex_v< UnderlyingNumeric_t<VT> > && IsBuiltin_v<ST>
205  , ST >;
206 
207  decltype(auto) left( derestrict( ~vec ) );
208 
209  if( IsInvertible_v<ScalarType> ) {
210  const ScalarType tmp( ScalarType(1)/static_cast<ScalarType>( scalar ) );
211  for( auto element=left.begin(); element!=left.end(); ++element ) {
212  element->value() *= tmp;
213  }
214  }
215  else {
216  for( auto element=left.begin(); element!=left.end(); ++element ) {
217  element->value() /= scalar;
218  }
219  }
220 
221  BLAZE_INTERNAL_ASSERT( isIntact( ~vec ), "Invariant violation detected" );
222 
223  return ~vec;
224 }
225 //*************************************************************************************************
226 
227 
228 //*************************************************************************************************
243 template< typename VT // Type of the left-hand side sparse vector
244  , bool TF // Transpose flag
245  , typename ST > // Data type of the right-hand side scalar
246 inline auto operator/=( SparseVector<VT,TF>&& vec, ST scalar )
248 {
249  return operator/=( ~vec, scalar );
250 }
251 //*************************************************************************************************
252 
253 
254 
255 
256 //=================================================================================================
257 //
258 // GLOBAL FUNCTIONS
259 //
260 //=================================================================================================
261 
262 //*************************************************************************************************
265 template< typename VT, bool TF >
266 bool isnan( const SparseVector<VT,TF>& sv );
267 
268 template< bool RF, typename VT, bool TF >
269 bool isUniform( const SparseVector<VT,TF>& sv );
270 
271 template< bool RF, typename VT, bool TF >
272 bool isZero( const SparseVector<VT,TF>& sv );
274 //*************************************************************************************************
275 
276 
277 //*************************************************************************************************
297 template< typename VT // Type of the sparse vector
298  , bool TF > // Transpose flag
299 inline bool isnan( const SparseVector<VT,TF>& sv )
300 {
301  using CT = CompositeType_t<VT>;
302 
303  CT a( ~sv ); // Evaluation of the sparse vector operand
304 
305  const auto end( a.end() );
306  for( auto element=a.begin(); element!=end; ++element ) {
307  if( isnan( element->value() ) ) return true;
308  }
309  return false;
310 }
311 //*************************************************************************************************
312 
313 
314 //*************************************************************************************************
347 template< bool RF // Relaxation flag
348  , typename VT // Type of the sparse vector
349  , bool TF > // Transpose flag
351 {
352  using CT = CompositeType_t<VT>;
353 
354  if( IsUniform_v<VT> || (~sv).size() < 2UL )
355  return true;
356 
357  CT a( ~sv ); // Evaluation of the sparse vector operand
358 
359  if( a.nonZeros() != a.size() )
360  {
361  const auto end( a.end() );
362  for( auto element=a.begin(); element!=end; ++element ) {
363  if( !isDefault<RF>( element->value() ) )
364  return false;
365  }
366  }
367  else
368  {
369  const auto& cmp( a[0] );
370  auto element( a.begin() );
371  const auto end( a.end() );
372 
373  ++element;
374 
375  for( ; element!=end; ++element ) {
376  if( !equal<RF>( element->value(), cmp ) )
377  return false;
378  }
379  }
380 
381  return true;
382 }
383 //*************************************************************************************************
384 
385 
386 //*************************************************************************************************
419 template< bool RF // Relaxation flag
420  , typename VT // Type of the sparse vector
421  , bool TF > // Transpose flag
422 bool isZero( const SparseVector<VT,TF>& sv )
423 {
424  if( IsZero_v<VT> || (~sv).nonZeros() == 0UL )
425  return true;
426 
427  CompositeType_t<VT> a( ~sv ); // Evaluation of the sparse vector operand
428 
429  const auto end( a.end() );
430  for( auto element=a.begin(); element!=end; ++element ) {
431  if( !isZero<RF>( element->value() ) )
432  return false;
433  }
434 
435  return true;
436 }
437 //*************************************************************************************************
438 
439 
440 //*************************************************************************************************
454 template< typename VT // Type of the sparse vector
455  , bool TF // Transpose flag
456  , typename... Args > // Type of the erase arguments
457 auto erase( SparseVector<VT,TF>& sv, Args&&... args )
458  -> decltype( (~sv).erase( std::forward<Args>( args )... ) )
459 {
460  return (~sv).erase( std::forward<Args>( args )... );
461 }
463 //*************************************************************************************************
464 
465 } // namespace blaze
466 
467 #endif
Header file for the isnan shim.
Header file for the UnderlyingNumeric type trait.
#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,...
Definition: Assert.h:117
Header file for basic type definitions.
Header file for the SparseVector base class.
typename If< Condition, T1, T2 >::Type If_t
Auxiliary alias template for the If class template.The If_t alias template provides a convenient shor...
Definition: If.h:109
Header file for the isZero shim.
typename DivTrait< T1, T2 >::Type DivTrait_t
Auxiliary alias declaration for the DivTrait class template.The DivTrait_t alias declaration provides...
Definition: DivTrait.h:239
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:595
size_t nonZeros(const Matrix< MT, SO > &matrix)
Returns the total number of non-zero elements in the matrix.
Definition: Matrix.h:584
typename T::ElementType ElementType_t
Alias declaration for nested ElementType type definitions.The ElementType_t alias declaration provide...
Definition: Aliases.h:170
bool isZero(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is 0.
Definition: DiagonalProxy.h:677
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.
constexpr bool IsResizable_v
Auxiliary variable template for the IsResizable type trait.The IsResizable_v variable template provid...
Definition: IsResizable.h:133
Header file for the IsUniform type trait.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
Header file for the If class template.
Header file for the IsFloatingPoint type trait.
Header file for the UnderlyingBuiltin type trait.
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:717
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:1638
MT::Iterator end(Matrix< MT, SO > &matrix, size_t i)
Returns an iterator just past the last element of row/column i.
Definition: Matrix.h:438
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:558
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
Header file for the division trait.
Header file for the IsZero type trait.
Header file for the isDefault shim.
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition: Matrix.h:530
Header file for the RemoveReference type trait.
Header file for the IsInvertible type trait.
Header file for the IsBuiltin type trait.
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:146
bool isIntact(const DiagonalMatrix< MT, SO, DF > &m)
Returns whether the invariants of the given diagonal matrix are intact.
Definition: DiagonalMatrix.h:264
Header file for the IsComplex type trait.
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:494
Header file for the IsResizable type trait.
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,...
Definition: Assert.h:101