Blaze 3.9
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>
66#include <blaze/util/Assert.h>
67#include <blaze/util/EnableIf.h>
68#include <blaze/util/mpl/If.h>
69#include <blaze/util/Types.h>
74
75
76namespace blaze {
77
78//=================================================================================================
79//
80// GLOBAL OPERATORS
81//
82//=================================================================================================
83
84//*************************************************************************************************
87template< typename VT, bool TF, typename ST >
88auto operator*=( SparseVector<VT,TF>& vec, ST scalar )
89 -> EnableIf_t< IsScalar_v<ST>, VT& >;
90
91template< typename VT, bool TF, typename ST >
92auto operator*=( SparseVector<VT,TF>&& vec, ST scalar )
93 -> EnableIf_t< IsScalar_v<ST>, VT& >;
94
95template< typename VT, bool TF, typename ST >
96auto operator/=( SparseVector<VT,TF>& vec, ST scalar )
97 -> EnableIf_t< IsScalar_v<ST>, VT& >;
98
99template< typename VT, bool TF, typename ST >
100auto operator/=( SparseVector<VT,TF>&& vec, ST scalar )
101 -> EnableIf_t< IsScalar_v<ST>, VT& >;
103//*************************************************************************************************
104
105
106//*************************************************************************************************
119template< typename VT // Type of the left-hand side sparse vector
120 , bool TF // Transpose flag
121 , typename ST > // Data type of the right-hand side scalar
122inline auto operator*=( SparseVector<VT,TF>& vec, ST scalar )
124{
125 if( IsRestricted_v<VT> ) {
126 if( !tryMult( *vec, 0UL, (*vec).size(), scalar ) ) {
127 BLAZE_THROW_INVALID_ARGUMENT( "Invalid scaling of restricted vector" );
128 }
129 }
130
131 if( !IsResizable_v< ElementType_t<VT> > && isZero( scalar ) )
132 {
133 reset( *vec );
134 }
135 else
136 {
137 decltype(auto) left( derestrict( *vec ) );
138
139 const auto last( left.end() );
140 for( auto element=left.begin(); element!=last; ++element ) {
141 element->value() *= scalar;
142 }
143 }
144
145 BLAZE_INTERNAL_ASSERT( isIntact( *vec ), "Invariant violation detected" );
146
147 return *vec;
148}
149//*************************************************************************************************
150
151
152//*************************************************************************************************
165template< typename VT // Type of the left-hand side sparse vector
166 , bool TF // Transpose flag
167 , typename ST > // Data type of the right-hand side scalar
168inline auto operator*=( SparseVector<VT,TF>&& vec, ST scalar )
170{
171 return operator*=( *vec, scalar );
172}
173//*************************************************************************************************
174
175
176//*************************************************************************************************
191template< typename VT // Type of the left-hand side sparse vector
192 , bool TF // Transpose flag
193 , typename ST > // Data type of the right-hand side scalar
194inline auto operator/=( SparseVector<VT,TF>& vec, ST scalar )
196{
197 BLAZE_USER_ASSERT( !isZero( scalar ), "Division by zero detected" );
198
199 if( IsRestricted_v<VT> ) {
200 if( !tryDiv( *vec, 0UL, (*vec).size(), scalar ) ) {
201 BLAZE_THROW_INVALID_ARGUMENT( "Invalid scaling of restricted vector" );
202 }
203 }
204
206 IsFloatingPoint_v< UnderlyingBuiltin_t<ST> >
207 , If_t< IsComplex_v< UnderlyingScalar_t<VT> > && IsBuiltin_v<ST>
210 , ST >;
211
212 decltype(auto) left( derestrict( *vec ) );
213
214 if( IsInvertible_v<ScalarType> ) {
215 const ScalarType tmp( ScalarType(1)/static_cast<ScalarType>( scalar ) );
216 for( auto element=left.begin(); element!=left.end(); ++element ) {
217 element->value() *= tmp;
218 }
219 }
220 else {
221 for( auto element=left.begin(); element!=left.end(); ++element ) {
222 element->value() /= scalar;
223 }
224 }
225
226 BLAZE_INTERNAL_ASSERT( isIntact( *vec ), "Invariant violation detected" );
227
228 return *vec;
229}
230//*************************************************************************************************
231
232
233//*************************************************************************************************
248template< typename VT // Type of the left-hand side sparse vector
249 , bool TF // Transpose flag
250 , typename ST > // Data type of the right-hand side scalar
251inline auto operator/=( SparseVector<VT,TF>&& vec, ST scalar )
253{
254 return operator/=( *vec, scalar );
255}
256//*************************************************************************************************
257
258
259
260
261//=================================================================================================
262//
263// GLOBAL FUNCTIONS
264//
265//=================================================================================================
266
267//*************************************************************************************************
270template< typename VT, bool TF >
271bool isnan( const SparseVector<VT,TF>& sv );
272
273template< typename VT, bool TF >
274bool isinf( const SparseVector<VT,TF>& sv );
275
276template< typename VT, bool TF >
277bool isfinite( const SparseVector<VT,TF>& sv );
278
279template< RelaxationFlag RF, typename VT, bool TF >
280bool isUniform( const SparseVector<VT,TF>& sv );
281
282template< RelaxationFlag RF, typename VT, bool TF >
283bool isZero( const SparseVector<VT,TF>& sv );
285//*************************************************************************************************
286
287
288//*************************************************************************************************
305template< typename VT // Type of the sparse vector
306 , bool TF > // Transpose flag
307inline bool isnan( const SparseVector<VT,TF>& sv )
308{
310 return false;
311
312 using CT = CompositeType_t<VT>;
313
314 CT a( *sv ); // Evaluation of the sparse vector operand
315
316 const auto end( a.end() );
317 for( auto element=a.begin(); element!=end; ++element ) {
318 if( isnan( element->value() ) ) return true;
319 }
320 return false;
321}
322//*************************************************************************************************
323
324
325//*************************************************************************************************
342template< typename VT // Type of the sparse vector
343 , bool TF > // Transpose flag
344inline bool isinf( const SparseVector<VT,TF>& sv )
345{
347 return false;
348
349 using CT = CompositeType_t<VT>;
350
351 CT a( *sv ); // Evaluation of the sparse vector operand
352
353 const auto end( a.end() );
354 for( auto element=a.begin(); element!=end; ++element ) {
355 if( isinf( element->value() ) ) return true;
356 }
357 return false;
358}
359//*************************************************************************************************
360
361
362//*************************************************************************************************
379template< typename VT // Type of the sparse vector
380 , bool TF > // Transpose flag
381inline bool isfinite( const SparseVector<VT,TF>& sv )
382{
384 return true;
385
386 using CT = CompositeType_t<VT>;
387
388 CT a( *sv ); // Evaluation of the sparse vector operand
389
390 const auto end( a.end() );
391 for( auto element=a.begin(); element!=end; ++element ) {
392 if( !isfinite( element->value() ) ) return false;
393 }
394 return true;
395}
396//*************************************************************************************************
397
398
399//*************************************************************************************************
432template< RelaxationFlag RF // Relaxation flag
433 , typename VT // Type of the sparse vector
434 , bool TF > // Transpose flag
436{
437 using CT = CompositeType_t<VT>;
438
439 if( IsUniform_v<VT> || (*sv).size() < 2UL )
440 return true;
441
442 CT a( *sv ); // Evaluation of the sparse vector operand
443
444 if( a.nonZeros() != a.size() )
445 {
446 const auto end( a.end() );
447 for( auto element=a.begin(); element!=end; ++element ) {
448 if( !isDefault<RF>( element->value() ) )
449 return false;
450 }
451 }
452 else
453 {
454 const auto& cmp( a[0] );
455 auto element( a.begin() );
456 const auto end( a.end() );
457
458 ++element;
459
460 for( ; element!=end; ++element ) {
461 if( !equal<RF>( element->value(), cmp ) )
462 return false;
463 }
464 }
465
466 return true;
467}
468//*************************************************************************************************
469
470
471//*************************************************************************************************
504template< RelaxationFlag RF // Relaxation flag
505 , typename VT // Type of the sparse vector
506 , bool TF > // Transpose flag
508{
509 if( IsZero_v<VT> || (*sv).nonZeros() == 0UL )
510 return true;
511
512 CompositeType_t<VT> a( *sv ); // Evaluation of the sparse vector operand
513
514 const auto end( a.end() );
515 for( auto element=a.begin(); element!=end; ++element ) {
516 if( !isZero<RF>( element->value() ) )
517 return false;
518 }
519
520 return true;
521}
522//*************************************************************************************************
523
524
525//*************************************************************************************************
539template< typename VT // Type of the sparse vector
540 , bool TF // Transpose flag
541 , typename... Args > // Type of the erase arguments
542auto erase( SparseVector<VT,TF>& sv, Args&&... args )
543 -> decltype( (*sv).erase( std::forward<Args>( args )... ) )
544{
545 return (*sv).erase( std::forward<Args>( args )... );
546}
548//*************************************************************************************************
549
550} // namespace blaze
551
552#endif
Header file for auxiliary alias declarations.
typename T::CompositeType CompositeType_t
Alias declaration for nested CompositeType type definitions.
Definition: Aliases.h:110
typename T::ElementType ElementType_t
Alias declaration for nested ElementType type definitions.
Definition: Aliases.h:190
Header file for run time assertion macros.
Header file for the division trait.
Header file for the EnableIf class template.
Header file for the If class template.
Header file for the IsBuiltin type trait.
Header file for the IsComplex type trait.
Header file for the isDefault shim.
Header file for the isfinite shim.
Header file for the IsFloatingPoint type trait.
Header file for the isinf shim.
Header file for the IsInvertible type trait.
Header file for the isnan shim.
Header file for the IsResizable type trait.
Header file for the IsRestricted type trait.
Header file for the IsScalar type trait.
Header file for the IsUniform type trait.
Constraint on the data type.
Header file for the relaxation flag enumeration.
Header file for the RemoveReference type trait.
Header file for the UnderlyingBuiltin type trait.
Header file for the UnderlyingScalar type trait.
Base class for sparse vectors.
Definition: SparseVector.h:72
Header file for the SparseVector base class.
bool isIntact(const DiagonalMatrix< MT, SO, DF > &m)
Returns whether the invariants of the given diagonal matrix are intact.
Definition: DiagonalMatrix.h:207
typename DivTrait< T1, T2 >::Type DivTrait_t
Auxiliary alias declaration for the DivTrait class template.
Definition: DivTrait.h:164
typename UnderlyingBuiltin< T >::Type UnderlyingBuiltin_t
Auxiliary alias declaration for the UnderlyingBuiltin type trait.
Definition: UnderlyingBuiltin.h:117
constexpr bool IsResizable_v
Auxiliary variable template for the IsResizable type trait.
Definition: IsResizable.h:136
RelaxationFlag
Relaxation flag for strict or relaxed semantics.
Definition: RelaxationFlag.h:66
constexpr void reset(Matrix< MT, SO > &matrix)
Resetting the given matrix.
Definition: Matrix.h:806
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:584
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.
Definition: Assert.h:101
#define BLAZE_USER_ASSERT(expr, msg)
Run time assertion macro for user checks.
Definition: Assert.h:117
bool isfinite(const SparseVector< VT, TF > &sv)
Checks the given sparse vector for finite elements.
Definition: SparseVector.h:381
bool isnan(const SparseVector< VT, TF > &sv)
Checks the given sparse vector for not-a-number elements.
Definition: SparseVector.h:307
bool isinf(const SparseVector< VT, TF > &sv)
Checks the given sparse vector for infinite elements.
Definition: SparseVector.h:344
auto operator*=(SparseVector< VT, TF > &&vec, ST scalar) -> EnableIf_t< IsScalar_v< ST >, VT & >
Multiplication assignment operator for the multiplication of a temporary sparse vector and a scalar (...
Definition: SparseVector.h:168
bool isUniform(const SparseVector< VT, TF > &sv)
Checks if the given sparse vector is a uniform vector.
Definition: SparseVector.h:435
auto operator/=(SparseVector< VT, TF > &&vec, ST scalar) -> EnableIf_t< IsScalar_v< ST >, VT & >
Division assignment operator for the division of a temporary sparse vector by a scalar value ( ).
Definition: SparseVector.h:251
bool isZero(const SparseVector< VT, TF > &sv)
Checks if the given sparse vector is a zero vector.
Definition: SparseVector.h:507
constexpr bool IsFloatingPoint_v
Auxiliary variable template for the IsFloatingPoint type trait.
Definition: IsFloatingPoint.h:95
typename EnableIf< Condition, T >::Type EnableIf_t
Auxiliary type for the EnableIf class template.
Definition: EnableIf.h:138
typename If< Condition >::template Type< T1, T2 > If_t
Auxiliary alias template for the If class template.
Definition: If.h:108
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.
Definition: Exception.h:235
Header file for the exception macros of the math module.
Header file for the equal shim.
Header file for the isZero shim.
Header file for the pow2 shim.
Header file for the sqrt shim.
Header file for the IsZero type trait.
Header file for basic type definitions.