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 <cmath>
44 #include <blaze/math/Aliases.h>
46 #include <blaze/math/shims/Equal.h>
48 #include <blaze/math/shims/IsNaN.h>
49 #include <blaze/math/shims/Sqrt.h>
56 #include <blaze/util/Assert.h>
57 #include <blaze/util/Types.h>
59 
60 
61 namespace blaze {
62 
63 //=================================================================================================
64 //
65 // GLOBAL OPERATORS
66 //
67 //=================================================================================================
68 
69 //*************************************************************************************************
72 template< typename T1, bool TF1, typename T2, bool TF2 >
73 inline bool operator==( const SparseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs );
74 
75 template< typename T1, bool TF1, typename T2, bool TF2 >
76 inline bool operator!=( const SparseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs );
78 //*************************************************************************************************
79 
80 
81 //*************************************************************************************************
89 template< typename T1 // Type of the left-hand side sparse vector
90  , bool TF1 // Transpose flag of the left-hand side sparse vector
91  , typename T2 // Type of the right-hand side sparse vector
92  , bool TF2 > // Transpose flag of the right-hand side sparse vector
93 inline bool operator==( const SparseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
94 {
95  using CT1 = CompositeType_<T1>;
96  using CT2 = CompositeType_<T2>;
97  using LhsConstIterator = ConstIterator_< RemoveReference_<CT1> >;
98  using RhsConstIterator = ConstIterator_< RemoveReference_<CT2> >;
99 
100  // Early exit in case the vector sizes don't match
101  if( (~lhs).size() != (~rhs).size() ) return false;
102 
103  // Evaluation of the two sparse vector operands
104  CT1 a( ~lhs );
105  CT2 b( ~rhs );
106 
107  // In order to compare the two vectors, the data values of the lower-order data
108  // type are converted to the higher-order data type within the equal function.
109  const LhsConstIterator lend( a.end() );
110  const RhsConstIterator rend( b.end() );
111 
112  LhsConstIterator lelem( a.begin() );
113  RhsConstIterator relem( b.begin() );
114 
115  while( lelem != lend && relem != rend )
116  {
117  if( isDefault( lelem->value() ) ) { ++lelem; continue; }
118  if( isDefault( relem->value() ) ) { ++relem; continue; }
119 
120  if( lelem->index() != relem->index() || !equal( lelem->value(), relem->value() ) ) {
121  return false;
122  }
123  else {
124  ++lelem;
125  ++relem;
126  }
127  }
128 
129  while( lelem != lend ) {
130  if( !isDefault( lelem->value() ) )
131  return false;
132  ++lelem;
133  }
134 
135  while( relem != rend ) {
136  if( !isDefault( relem->value() ) )
137  return false;
138  ++relem;
139  }
140 
141  return true;
142 }
143 //*************************************************************************************************
144 
145 
146 //*************************************************************************************************
154 template< typename T1 // Type of the left-hand side sparse vector
155  , bool TF1 // Transpose flag of the left-hand side sparse vector
156  , typename T2 // Type of the right-hand side sparse vector
157  , bool TF2 > // Transpose flag of the right-hand side sparse vector
158 inline bool operator!=( const SparseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
159 {
160  return !( lhs == rhs );
161 }
162 //*************************************************************************************************
163 
164 
165 
166 
167 //=================================================================================================
168 //
169 // GLOBAL FUNCTIONS
170 //
171 //=================================================================================================
172 
173 //*************************************************************************************************
176 template< typename VT, bool TF >
177 bool isnan( const SparseVector<VT,TF>& sv );
178 
179 template< typename VT, bool TF >
180 bool isUniform( const SparseVector<VT,TF>& dv );
181 
182 template< typename VT, bool TF >
184 
185 template< typename VT, bool TF >
186 inline auto length( const SparseVector<VT,TF>& sv ) -> decltype( sqrt( sqrLength( ~sv ) ) );
187 
188 template< typename VT, bool TF >
189 const ElementType_<VT> min( const SparseVector<VT,TF>& sv );
190 
191 template< typename VT, bool TF >
192 const ElementType_<VT> max( const SparseVector<VT,TF>& sv );
194 //*************************************************************************************************
195 
196 
197 //*************************************************************************************************
217 template< typename VT // Type of the sparse vector
218  , bool TF > // Transpose flag
219 inline bool isnan( const SparseVector<VT,TF>& sv )
220 {
221  using CT = CompositeType_<VT>;
223 
224  CT a( ~sv ); // Evaluation of the sparse vector operand
225 
226  const ConstIterator end( a.end() );
227  for( ConstIterator element=a.begin(); element!=end; ++element ) {
228  if( isnan( element->value() ) ) return true;
229  }
230  return false;
231 }
232 //*************************************************************************************************
233 
234 
235 //*************************************************************************************************
261 template< typename VT // Type of the sparse vector
262  , bool TF > // Transpose flag
264 {
265  using CT = CompositeType_<VT>;
268 
269  if( IsUniform<VT>::value || (~sv).size() < 2UL )
270  return true;
271 
272  CT a( ~sv ); // Evaluation of the sparse vector operand
273 
274  if( a.nonZeros() != a.size() )
275  {
276  for( ConstIterator element=a.begin(); element!=a.end(); ++element ) {
277  if( !isDefault( element->value() ) )
278  return false;
279  }
280  }
281  else
282  {
283  ConstReference cmp( a[0] );
284  ConstIterator element( a.begin() );
285 
286  ++element;
287 
288  for( ; element!=a.end(); ++element ) {
289  if( element->value() != cmp )
290  return false;
291  }
292  }
293 
294  return true;
295 }
296 //*************************************************************************************************
297 
298 
299 //*************************************************************************************************
312 template< typename VT // Type of the sparse vector
313  , bool TF > // Transpose flag
315 {
318 
320 
321  ElementType sum( 0 );
322  for( ConstIterator element=(~sv).begin(); element!=(~sv).end(); ++element )
323  sum += sq( element->value() );
324  return sum;
325 }
326 //*************************************************************************************************
327 
328 
329 //*************************************************************************************************
366 template< typename VT // Type of the sparse vector
367  , bool TF > // Transpose flag
368 inline auto length( const SparseVector<VT,TF>& sv ) -> decltype( sqrt( sqrLength( ~sv ) ) )
369 {
370  return sqrt( sqrLength( ~sv ) );
371 }
372 //*************************************************************************************************
373 
374 
375 //*************************************************************************************************
397 template< typename VT // Type of the sparse vector
398  , bool TF > // Transpose flag
400 {
401  using blaze::min;
402 
403  using ET = ElementType_<VT>;
404  using CT = CompositeType_<VT>;
406 
407  CT a( ~sv ); // Evaluation of the sparse vector operand
408 
409  const ConstIterator end( a.end() );
410  ConstIterator element( a.begin() );
411 
412  if( element == end ) {
413  return ET();
414  }
415  else if( a.nonZeros() == a.size() ) {
416  ET minimum( element->value() );
417  ++element;
418  for( ; element!=end; ++element )
419  minimum = min( minimum, element->value() );
420  return minimum;
421  }
422  else {
423  ET minimum = ET();
424  for( ; element!=end; ++element )
425  minimum = min( minimum, element->value() );
426  return minimum;
427  }
428 }
429 //*************************************************************************************************
430 
431 
432 //*************************************************************************************************
454 template< typename VT // Type of the sparse vector
455  , bool TF > // Transpose flag
457 {
458  using blaze::max;
459 
460  using ET = ElementType_<VT>;
461  using CT = CompositeType_<VT>;
463 
464  CT a( ~sv ); // Evaluation of the sparse vector operand
465 
466  const ConstIterator end( a.end() );
467  ConstIterator element( a.begin() );
468 
469  if( element == end ) {
470  return ET();
471  }
472  else if( a.nonZeros() == a.size() ) {
473  ET maximum( element->value() );
474  ++element;
475  for( ; element!=end; ++element )
476  maximum = max( maximum, element->value() );
477  return maximum;
478  }
479  else {
480  ET maximum = ET();
481  for( ; element!=end; ++element )
482  maximum = max( maximum, element->value() );
483  return maximum;
484  }
485 }
486 //*************************************************************************************************
487 
488 } // namespace blaze
489 
490 #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
BLAZE_ALWAYS_INLINE MT::Iterator begin(Matrix< MT, SO > &matrix, size_t i)
Returns an iterator to the first element of row/column i.
Definition: Matrix.h:198
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
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
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
BLAZE_ALWAYS_INLINE 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:264
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
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
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:600