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>
45 #include <blaze/math/Functions.h>
46 #include <blaze/math/shims/Equal.h>
48 #include <blaze/math/shims/IsNaN.h>
53 #include <blaze/util/Assert.h>
54 #include <blaze/util/Types.h>
56 
57 
58 namespace blaze {
59 
60 //=================================================================================================
61 //
62 // GLOBAL OPERATORS
63 //
64 //=================================================================================================
65 
66 //*************************************************************************************************
69 template< typename T1, bool TF1, typename T2, bool TF2 >
70 inline bool operator==( const SparseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs );
71 
72 template< typename T1, bool TF1, typename T2, bool TF2 >
73 inline bool operator!=( const SparseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs );
75 //*************************************************************************************************
76 
77 
78 //*************************************************************************************************
86 template< typename T1 // Type of the left-hand side sparse vector
87  , bool TF1 // Transpose flag of the left-hand side sparse vector
88  , typename T2 // Type of the right-hand side sparse vector
89  , bool TF2 > // Transpose flag of the right-hand side sparse vector
90 inline bool operator==( const SparseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
91 {
92  typedef typename T1::CompositeType CT1;
93  typedef typename T2::CompositeType CT2;
94  typedef typename RemoveReference<CT1>::Type::ConstIterator LhsConstIterator;
95  typedef typename RemoveReference<CT2>::Type::ConstIterator RhsConstIterator;
96 
97  // Early exit in case the vector sizes don't match
98  if( (~lhs).size() != (~rhs).size() ) return false;
99 
100  // Evaluation of the two sparse vector operands
101  CT1 a( ~lhs );
102  CT2 b( ~rhs );
103 
104  // In order to compare the two vectors, the data values of the lower-order data
105  // type are converted to the higher-order data type within the equal function.
106  const LhsConstIterator lend( a.end() );
107  const RhsConstIterator rend( b.end() );
108 
109  LhsConstIterator lelem( a.begin() );
110  RhsConstIterator relem( b.begin() );
111 
112  while( lelem != lend && relem != rend )
113  {
114  if( isDefault( lelem->value() ) ) { ++lelem; continue; }
115  if( isDefault( relem->value() ) ) { ++relem; continue; }
116 
117  if( lelem->index() != relem->index() || !equal( lelem->value(), relem->value() ) ) {
118  return false;
119  }
120  else {
121  ++lelem;
122  ++relem;
123  }
124  }
125 
126  while( lelem != lend ) {
127  if( !isDefault( lelem->value() ) )
128  return false;
129  ++lelem;
130  }
131 
132  while( relem != rend ) {
133  if( !isDefault( relem->value() ) )
134  return false;
135  ++relem;
136  }
137 
138  return true;
139 }
140 //*************************************************************************************************
141 
142 
143 //*************************************************************************************************
151 template< typename T1 // Type of the left-hand side sparse vector
152  , bool TF1 // Transpose flag of the left-hand side sparse vector
153  , typename T2 // Type of the right-hand side sparse vector
154  , bool TF2 > // Transpose flag of the right-hand side sparse vector
155 inline bool operator!=( const SparseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
156 {
157  return !( lhs == rhs );
158 }
159 //*************************************************************************************************
160 
161 
162 
163 
164 //=================================================================================================
165 //
166 // GLOBAL FUNCTIONS
167 //
168 //=================================================================================================
169 
170 //*************************************************************************************************
173 template< typename VT, bool TF >
174 bool isnan( const SparseVector<VT,TF>& sv );
175 
176 template< typename VT, bool TF >
177 bool isUniform( const SparseVector<VT,TF>& dv );
178 
179 template< typename VT, bool TF >
180 typename CMathTrait<typename VT::ElementType>::Type length( const SparseVector<VT,TF>& sv );
181 
182 template< typename VT, bool TF >
183 const typename VT::ElementType sqrLength( const SparseVector<VT,TF>& sv );
184 
185 template< typename VT, bool TF >
186 const typename VT::ElementType min( const SparseVector<VT,TF>& sv );
187 
188 template< typename VT, bool TF >
189 const typename VT::ElementType max( const SparseVector<VT,TF>& sv );
191 //*************************************************************************************************
192 
193 
194 //*************************************************************************************************
214 template< typename VT // Type of the sparse vector
215  , bool TF > // Transpose flag
216 inline bool isnan( const SparseVector<VT,TF>& sv )
217 {
218  typedef typename VT::CompositeType CT;
220 
221  CT a( ~sv ); // Evaluation of the sparse vector operand
222 
223  const ConstIterator end( a.end() );
224  for( ConstIterator element=a.begin(); element!=end; ++element ) {
225  if( isnan( element->value() ) ) return true;
226  }
227  return false;
228 }
229 //*************************************************************************************************
230 
231 
232 //*************************************************************************************************
258 template< typename VT // Type of the sparse vector
259  , bool TF > // Transpose flag
261 {
262  typedef typename VT::CompositeType CT;
265 
266  if( (~sv).size() < 2UL )
267  return true;
268 
269  CT a( ~sv ); // Evaluation of the sparse vector operand
270 
271  if( (~sv).nonZeros() != (~sv).size() )
272  {
273  for( ConstIterator element=(~sv).begin(); element!=(~sv).end(); ++element ) {
274  if( !isDefault( element->value() ) )
275  return false;
276  }
277  }
278  else
279  {
280  ConstReference cmp( (~sv)[0] );
281  ConstIterator element( (~sv).begin() );
282 
283  ++element;
284 
285  for( ; element!=(~sv).end(); ++element ) {
286  if( element->value() != cmp )
287  return false;
288  }
289  }
290 
291  return true;
292 }
293 //*************************************************************************************************
294 
295 
296 //*************************************************************************************************
329 template< typename VT // Type of the sparse vector
330  , bool TF > // Transpose flag
332 {
333  typedef typename VT::ElementType ElementType;
334  typedef typename VT::ConstIterator ConstIterator;
335  typedef typename CMathTrait<ElementType>::Type LengthType;
336 
338 
339  LengthType sum( 0 );
340  for( ConstIterator element=(~sv).begin(); element!=(~sv).end(); ++element )
341  sum += sq( element->value() );
342  return std::sqrt( sum );
343 }
344 //*************************************************************************************************
345 
346 
347 //*************************************************************************************************
360 template< typename VT // Type of the sparse vector
361  , bool TF > // Transpose flag
362 const typename VT::ElementType sqrLength( const SparseVector<VT,TF>& sv )
363 {
364  typedef typename VT::ElementType ElementType;
365  typedef typename VT::ConstIterator ConstIterator;
366 
368 
369  ElementType sum( 0 );
370  for( ConstIterator element=(~sv).begin(); element!=(~sv).end(); ++element )
371  sum += sq( element->value() );
372  return sum;
373 }
374 //*************************************************************************************************
375 
376 
377 //*************************************************************************************************
399 template< typename VT // Type of the sparse vector
400  , bool TF > // Transpose flag
401 const typename VT::ElementType min( const SparseVector<VT,TF>& sv )
402 {
403  using blaze::min;
404 
405  typedef typename VT::ElementType ET;
406  typedef typename VT::CompositeType CT;
408 
409  CT a( ~sv ); // Evaluation of the sparse vector operand
410 
411  const ConstIterator end( a.end() );
412  ConstIterator element( a.begin() );
413 
414  if( element == end ) {
415  return ET();
416  }
417  else if( a.nonZeros() == a.size() ) {
418  ET minimum( element->value() );
419  ++element;
420  for( ; element!=end; ++element )
421  minimum = min( minimum, element->value() );
422  return minimum;
423  }
424  else {
425  ET minimum = ET();
426  for( ; element!=end; ++element )
427  minimum = min( minimum, element->value() );
428  return minimum;
429  }
430 }
431 //*************************************************************************************************
432 
433 
434 //*************************************************************************************************
456 template< typename VT // Type of the sparse vector
457  , bool TF > // Transpose flag
458 const typename VT::ElementType max( const SparseVector<VT,TF>& sv )
459 {
460  using blaze::max;
461 
462  typedef typename VT::ElementType ET;
463  typedef typename VT::CompositeType CT;
465 
466  CT a( ~sv ); // Evaluation of the sparse vector operand
467 
468  const ConstIterator end( a.end() );
469  ConstIterator element( a.begin() );
470 
471  if( element == end ) {
472  return ET();
473  }
474  else if( a.nonZeros() == a.size() ) {
475  ET maximum( element->value() );
476  ++element;
477  for( ; element!=end; ++element )
478  maximum = max( maximum, element->value() );
479  return maximum;
480  }
481  else {
482  ET maximum = ET();
483  for( ; element!=end; ++element )
484  maximum = max( maximum, element->value() );
485  return maximum;
486  }
487 }
488 //*************************************************************************************************
489 
490 } // namespace blaze
491 
492 #endif
Header file for the isnan shim.
BLAZE_ALWAYS_INLINE int16_t sum(const sse_int16_t &a)
Returns the sum of all elements in the 16-bit integral intrinsic vector.
Definition: Reduction.h:63
const MT::ElementType max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1649
Constraint on the data type.
Header file for mathematical functions.
Header file for basic type definitions.
Header file for the SparseVector base class.
Header file for the square shim.
BLAZE_ALWAYS_INLINE size_t size(const Vector< VT, TF > &vector)
Returns the current size/dimension of the vector.
Definition: Vector.h:264
double Type
Return type of the functions for integral and double arguments.
Definition: CMathTrait.h:79
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:258
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:2507
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:386
bool isnan(const DenseMatrix< MT, SO > &dm)
Checks the given dense matrix for not-a-number elements.
Definition: DenseMatrix.h:640
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:861
Header file for the vector transpose flag types.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2511
const MT::ElementType min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1602
Header file for the equal shim.
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:195
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2505
CMathTrait< typename VT::ElementType >::Type length(const DenseVector< VT, TF > &dv)
Calculation of the dense vector length .
Definition: DenseVector.h:516
const Type & ConstReference
Reference to a constant matrix value.
Definition: CompressedMatrix.h:2509
Removal of reference modifiers.The RemoveCV type trait removes any reference modifiers from the given...
Definition: RemoveReference.h:69
Header file for run time assertion macros.
bool equal(const T1 &a, const T2 &b)
Generic equality check.
Definition: Equal.h:376
#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:79
Header file for the isDefault shim.
Header file for the RemoveReference type trait.
Header file for the cmath trait.
bool isUniform(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is a uniform matrix.
Definition: DenseMatrix.h:910
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:108
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
const MultExprTrait< T, T >::Type sq(const T &a)
Squaring the given value/object.
Definition: Square.h:65
const VT::ElementType sqrLength(const DenseVector< VT, TF > &dv)
Calculation of the dense vector square length .
Definition: DenseVector.h:546