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/Functions.h>
47 #include <blaze/math/shims/Equal.h>
49 #include <blaze/math/shims/IsNaN.h>
50 #include <blaze/math/shims/Sqrt.h>
54 #include <blaze/util/Assert.h>
55 #include <blaze/util/Types.h>
57 
58 
59 namespace blaze {
60 
61 //=================================================================================================
62 //
63 // GLOBAL OPERATORS
64 //
65 //=================================================================================================
66 
67 //*************************************************************************************************
70 template< typename T1, bool TF1, typename T2, bool TF2 >
71 inline bool operator==( const SparseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs );
72 
73 template< typename T1, bool TF1, typename T2, bool TF2 >
74 inline bool operator!=( const SparseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs );
76 //*************************************************************************************************
77 
78 
79 //*************************************************************************************************
87 template< typename T1 // Type of the left-hand side sparse vector
88  , bool TF1 // Transpose flag of the left-hand side sparse vector
89  , typename T2 // Type of the right-hand side sparse vector
90  , bool TF2 > // Transpose flag of the right-hand side sparse vector
91 inline bool operator==( const SparseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
92 {
93  typedef CompositeType_<T1> CT1;
94  typedef CompositeType_<T2> CT2;
95  typedef ConstIterator_< RemoveReference_<CT1> > LhsConstIterator;
96  typedef ConstIterator_< RemoveReference_<CT2> > RhsConstIterator;
97 
98  // Early exit in case the vector sizes don't match
99  if( (~lhs).size() != (~rhs).size() ) return false;
100 
101  // Evaluation of the two sparse vector operands
102  CT1 a( ~lhs );
103  CT2 b( ~rhs );
104 
105  // In order to compare the two vectors, the data values of the lower-order data
106  // type are converted to the higher-order data type within the equal function.
107  const LhsConstIterator lend( a.end() );
108  const RhsConstIterator rend( b.end() );
109 
110  LhsConstIterator lelem( a.begin() );
111  RhsConstIterator relem( b.begin() );
112 
113  while( lelem != lend && relem != rend )
114  {
115  if( isDefault( lelem->value() ) ) { ++lelem; continue; }
116  if( isDefault( relem->value() ) ) { ++relem; continue; }
117 
118  if( lelem->index() != relem->index() || !equal( lelem->value(), relem->value() ) ) {
119  return false;
120  }
121  else {
122  ++lelem;
123  ++relem;
124  }
125  }
126 
127  while( lelem != lend ) {
128  if( !isDefault( lelem->value() ) )
129  return false;
130  ++lelem;
131  }
132 
133  while( relem != rend ) {
134  if( !isDefault( relem->value() ) )
135  return false;
136  ++relem;
137  }
138 
139  return true;
140 }
141 //*************************************************************************************************
142 
143 
144 //*************************************************************************************************
152 template< typename T1 // Type of the left-hand side sparse vector
153  , bool TF1 // Transpose flag of the left-hand side sparse vector
154  , typename T2 // Type of the right-hand side sparse vector
155  , bool TF2 > // Transpose flag of the right-hand side sparse vector
156 inline bool operator!=( const SparseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
157 {
158  return !( lhs == rhs );
159 }
160 //*************************************************************************************************
161 
162 
163 
164 
165 //=================================================================================================
166 //
167 // GLOBAL FUNCTIONS
168 //
169 //=================================================================================================
170 
171 //*************************************************************************************************
174 template< typename VT, bool TF >
175 bool isnan( const SparseVector<VT,TF>& sv );
176 
177 template< typename VT, bool TF >
178 bool isUniform( const SparseVector<VT,TF>& dv );
179 
180 template< typename VT, bool TF >
181 const ElementType_<VT> sqrLength( const SparseVector<VT,TF>& sv );
182 
183 template< typename VT, bool TF >
184 inline auto length( const SparseVector<VT,TF>& sv ) -> decltype( sqrt( sqrLength( ~sv ) ) );
185 
186 template< typename VT, bool TF >
187 const ElementType_<VT> min( const SparseVector<VT,TF>& sv );
188 
189 template< typename VT, bool TF >
190 const ElementType_<VT> max( const SparseVector<VT,TF>& sv );
192 //*************************************************************************************************
193 
194 
195 //*************************************************************************************************
215 template< typename VT // Type of the sparse vector
216  , bool TF > // Transpose flag
217 inline bool isnan( const SparseVector<VT,TF>& sv )
218 {
219  typedef CompositeType_<VT> CT;
221 
222  CT a( ~sv ); // Evaluation of the sparse vector operand
223 
224  const ConstIterator end( a.end() );
225  for( ConstIterator element=a.begin(); element!=end; ++element ) {
226  if( isnan( element->value() ) ) return true;
227  }
228  return false;
229 }
230 //*************************************************************************************************
231 
232 
233 //*************************************************************************************************
259 template< typename VT // Type of the sparse vector
260  , bool TF > // Transpose flag
262 {
263  typedef CompositeType_<VT> CT;
266 
267  if( (~sv).size() < 2UL )
268  return true;
269 
270  CT a( ~sv ); // Evaluation of the sparse vector operand
271 
272  if( (~sv).nonZeros() != (~sv).size() )
273  {
274  for( ConstIterator element=(~sv).begin(); element!=(~sv).end(); ++element ) {
275  if( !isDefault( element->value() ) )
276  return false;
277  }
278  }
279  else
280  {
281  ConstReference cmp( (~sv)[0] );
282  ConstIterator element( (~sv).begin() );
283 
284  ++element;
285 
286  for( ; element!=(~sv).end(); ++element ) {
287  if( element->value() != cmp )
288  return false;
289  }
290  }
291 
292  return true;
293 }
294 //*************************************************************************************************
295 
296 
297 //*************************************************************************************************
310 template< typename VT // Type of the sparse vector
311  , bool TF > // Transpose flag
313 {
316 
318 
319  ElementType sum( 0 );
320  for( ConstIterator element=(~sv).begin(); element!=(~sv).end(); ++element )
321  sum += sq( element->value() );
322  return sum;
323 }
324 //*************************************************************************************************
325 
326 
327 //*************************************************************************************************
364 template< typename VT // Type of the sparse vector
365  , bool TF > // Transpose flag
366 inline auto length( const SparseVector<VT,TF>& sv ) -> decltype( sqrt( sqrLength( ~sv ) ) )
367 {
368  return sqrt( sqrLength( ~sv ) );
369 }
370 //*************************************************************************************************
371 
372 
373 //*************************************************************************************************
395 template< typename VT // Type of the sparse vector
396  , bool TF > // Transpose flag
398 {
399  using blaze::min;
400 
401  typedef ElementType_<VT> ET;
402  typedef CompositeType_<VT> CT;
404 
405  CT a( ~sv ); // Evaluation of the sparse vector operand
406 
407  const ConstIterator end( a.end() );
408  ConstIterator element( a.begin() );
409 
410  if( element == end ) {
411  return ET();
412  }
413  else if( a.nonZeros() == a.size() ) {
414  ET minimum( element->value() );
415  ++element;
416  for( ; element!=end; ++element )
417  minimum = min( minimum, element->value() );
418  return minimum;
419  }
420  else {
421  ET minimum = ET();
422  for( ; element!=end; ++element )
423  minimum = min( minimum, element->value() );
424  return minimum;
425  }
426 }
427 //*************************************************************************************************
428 
429 
430 //*************************************************************************************************
452 template< typename VT // Type of the sparse vector
453  , bool TF > // Transpose flag
455 {
456  using blaze::max;
457 
458  typedef ElementType_<VT> ET;
459  typedef CompositeType_<VT> CT;
461 
462  CT a( ~sv ); // Evaluation of the sparse vector operand
463 
464  const ConstIterator end( a.end() );
465  ConstIterator element( a.begin() );
466 
467  if( element == end ) {
468  return ET();
469  }
470  else if( a.nonZeros() == a.size() ) {
471  ET maximum( element->value() );
472  ++element;
473  for( ; element!=end; ++element )
474  maximum = max( maximum, element->value() );
475  return maximum;
476  }
477  else {
478  ET maximum = ET();
479  for( ; element!=end; ++element )
480  maximum = max( maximum, element->value() );
481  return maximum;
482  }
483 }
484 //*************************************************************************************************
485 
486 } // namespace blaze
487 
488 #endif
Header file for the isnan shim.
Header file for auxiliary alias declarations.
Constraint on the data type.
Header file for mathematical functions.
Header file for basic type definitions.
Header file for the SparseVector base class.
BLAZE_ALWAYS_INLINE const complex< int8_t > sum(const SIMDcint8 &a) noexcept
Returns the sum of all elements in the 8-bit integral complex SIMD vector.
Definition: Reduction.h:63
Header file for the square shim.
BLAZE_ALWAYS_INLINE size_t size(const Vector< VT, TF > &vector) noexcept
Returns the current size/dimension of the vector.
Definition: Vector.h:258
const ElementType_< VT > sqrLength(const DenseVector< VT, TF > &dv)
Calculation of the dense vector square length .
Definition: DenseVector.h:521
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:188
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1669
BLAZE_ALWAYS_INLINE size_t nonZeros(const Matrix< MT, SO > &matrix)
Returns the total number of non-zero elements in the matrix.
Definition: Matrix.h:384
const ElementType_< MT > max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1716
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:573
Header file for the vector transpose flag types.
auto length(const DenseVector< VT, TF > &dv) -> decltype(sqrt(sqrLength(~dv)))
Calculation of the dense vector length .
Definition: DenseVector.h:574
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
BLAZE_ALWAYS_INLINE constexpr MultExprTrait_< T, T > sq(const T &a) noexcept(noexcept(a *a))
Squaring the given value/object.
Definition: Square.h:66
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2647
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:655
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:254
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2641
const Type & ConstReference
Reference to a constant matrix value.
Definition: CompressedMatrix.h:2645
const DMatForEachExpr< MT, Sqrt, SO > sqrt(const DenseMatrix< MT, SO > &dm)
Computes the square root of each single element of the dense matrix dm.
Definition: DMatForEachExpr.h:1282
Header file for run time assertion macros.
bool equal(const T1 &a, const T2 &b)
Generic equality check.
Definition: Equal.h:73
#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
bool isUniform(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is a uniform matrix.
Definition: DenseMatrix.h:982
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:110
bool operator==(const NegativeAccuracy< A > &lhs, const T &rhs)
Equality comparison between a NegativeAccuracy object and a floating point value. ...
Definition: Accuracy.h:249
bool operator!=(const NegativeAccuracy< A > &lhs, const T &rhs)
Inequality comparison between a NegativeAccuracy object and a floating point value.
Definition: Accuracy.h:289