All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SparseVector.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_SPARSEVECTOR_H_
36 #define _BLAZE_MATH_SPARSEVECTOR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <cmath>
60 #include <blaze/math/shims/Equal.h>
62 #include <blaze/math/shims/IsNaN.h>
68 #include <blaze/math/Vector.h>
70 #include <blaze/util/Assert.h>
71 #include <blaze/util/Types.h>
73 
74 
75 namespace blaze {
76 
77 //=================================================================================================
78 //
79 // GLOBAL OPERATORS
80 //
81 //=================================================================================================
82 
83 //*************************************************************************************************
86 template< typename T1, bool TF1, typename T2, bool TF2 >
87 inline bool operator==( const SparseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs );
88 
89 template< typename T1, bool TF1, typename T2, bool TF2 >
90 inline bool operator!=( const SparseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs );
92 //*************************************************************************************************
93 
94 
95 //*************************************************************************************************
103 template< typename T1 // Type of the left-hand side sparse vector
104  , bool TF1 // Transpose flag of the left-hand side sparse vector
105  , typename T2 // Type of the right-hand side sparse vector
106  , bool TF2 > // Transpose flag of the right-hand side sparse vector
107 inline bool operator==( const SparseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
108 {
109  typedef typename T1::CompositeType CT1;
110  typedef typename T2::CompositeType CT2;
111  typedef typename RemoveReference<CT1>::Type::ConstIterator LhsConstIterator;
112  typedef typename RemoveReference<CT2>::Type::ConstIterator RhsConstIterator;
113 
114  // Early exit in case the vector sizes don't match
115  if( (~lhs).size() != (~rhs).size() ) return false;
116 
117  // Evaluation of the two sparse vector operands
118  CT1 a( ~lhs );
119  CT2 b( ~rhs );
120 
121  // In order to compare the two vectors, the data values of the lower-order data
122  // type are converted to the higher-order data type within the equal function.
123  const LhsConstIterator lend( a.end() );
124  const RhsConstIterator rend( b.end() );
125 
126  LhsConstIterator lelem( a.begin() );
127  RhsConstIterator relem( b.begin() );
128 
129  while( lelem != lend && relem != rend )
130  {
131  if( isDefault( lelem->value() ) ) { ++lelem; continue; }
132  if( isDefault( relem->value() ) ) { ++relem; continue; }
133 
134  if( lelem->index() != relem->index() || !equal( lelem->value(), relem->value() ) ) {
135  return false;
136  }
137  else {
138  ++lelem;
139  ++relem;
140  }
141  }
142 
143  while( lelem != lend ) {
144  if( !isDefault( lelem->value() ) )
145  return false;
146  ++lelem;
147  }
148 
149  while( relem != rend ) {
150  if( !isDefault( relem->value() ) )
151  return false;
152  ++relem;
153  }
154 
155  return true;
156 }
157 //*************************************************************************************************
158 
159 
160 //*************************************************************************************************
168 template< typename T1 // Type of the left-hand side sparse vector
169  , bool TF1 // Transpose flag of the left-hand side sparse vector
170  , typename T2 // Type of the right-hand side sparse vector
171  , bool TF2 > // Transpose flag of the right-hand side sparse vector
172 inline bool operator!=( const SparseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
173 {
174  return !( lhs == rhs );
175 }
176 //*************************************************************************************************
177 
178 
179 
180 
181 //=================================================================================================
182 //
183 // GLOBAL FUNCTIONS
184 //
185 //=================================================================================================
186 
187 //*************************************************************************************************
190 template< typename VT, bool TF >
191 bool isnan( const SparseVector<VT,TF>& sv );
192 
193 template< typename VT, bool TF >
194 typename CMathTrait<typename VT::ElementType>::Type length( const SparseVector<VT,TF>& sv );
195 
196 template< typename VT, bool TF >
197 const typename VT::ElementType sqrLength( const SparseVector<VT,TF>& sv );
198 
199 template< typename VT, bool TF >
200 const typename VT::ElementType min( const SparseVector<VT,TF>& sv );
201 
202 template< typename VT, bool TF >
203 const typename VT::ElementType max( const SparseVector<VT,TF>& sv );
205 //*************************************************************************************************
206 
207 
208 //*************************************************************************************************
228 template< typename VT // Type of the sparse vector
229  , bool TF > // Transpose flag
230 inline bool isnan( const SparseVector<VT,TF>& sv )
231 {
232  typedef typename VT::CompositeType CT;
234 
235  CT a( ~sv ); // Evaluation of the sparse vector operand
236 
237  const ConstIterator end( a.end() );
238  for( ConstIterator element=a.begin(); element!=end; ++element ) {
239  if( isnan( element->value() ) ) return true;
240  }
241  return false;
242 }
243 //*************************************************************************************************
244 
245 
246 //*************************************************************************************************
279 template< typename VT // Type of the sparse vector
280  , bool TF > // Transpose flag
282 {
283  typedef typename VT::ElementType ElementType;
284  typedef typename VT::ConstIterator ConstIterator;
285  typedef typename CMathTrait<ElementType>::Type LengthType;
286 
288 
289  LengthType sum( 0 );
290  for( ConstIterator element=(~sv).begin(); element!=(~sv).end(); ++element )
291  sum += sq( element->value() );
292  return std::sqrt( sum );
293 }
294 //*************************************************************************************************
295 
296 
297 //*************************************************************************************************
310 template< typename VT // Type of the sparse vector
311  , bool TF > // Transpose flag
312 const typename VT::ElementType sqrLength( const SparseVector<VT,TF>& sv )
313 {
314  typedef typename VT::ElementType ElementType;
315  typedef typename VT::ConstIterator ConstIterator;
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 //*************************************************************************************************
349 template< typename VT // Type of the sparse vector
350  , bool TF > // Transpose flag
351 const typename VT::ElementType min( const SparseVector<VT,TF>& sv )
352 {
353  using blaze::min;
354 
355  typedef typename VT::ElementType ET;
356  typedef typename VT::CompositeType CT;
358 
359  CT a( ~sv ); // Evaluation of the sparse vector operand
360 
361  const ConstIterator end( a.end() );
362  ConstIterator element( a.begin() );
363 
364  if( element == end ) {
365  return ET();
366  }
367  else if( a.nonZeros() == a.size() ) {
368  ET minimum( element->value() );
369  ++element;
370  for( ; element!=end; ++element )
371  minimum = min( minimum, element->value() );
372  return minimum;
373  }
374  else {
375  ET minimum = ET();
376  for( ; element!=end; ++element )
377  minimum = min( minimum, element->value() );
378  return minimum;
379  }
380 }
381 //*************************************************************************************************
382 
383 
384 //*************************************************************************************************
406 template< typename VT // Type of the sparse vector
407  , bool TF > // Transpose flag
408 const typename VT::ElementType max( const SparseVector<VT,TF>& sv )
409 {
410  using blaze::max;
411 
412  typedef typename VT::ElementType ET;
413  typedef typename VT::CompositeType CT;
415 
416  CT a( ~sv ); // Evaluation of the sparse vector operand
417 
418  const ConstIterator end( a.end() );
419  ConstIterator element( a.begin() );
420 
421  if( element == end ) {
422  return ET();
423  }
424  else if( a.nonZeros() == a.size() ) {
425  ET maximum( element->value() );
426  ++element;
427  for( ; element!=end; ++element )
428  maximum = max( maximum, element->value() );
429  return maximum;
430  }
431  else {
432  ET maximum = ET();
433  for( ; element!=end; ++element )
434  maximum = max( maximum, element->value() );
435  return maximum;
436  }
437 }
438 //*************************************************************************************************
439 
440 } // namespace blaze
441 
442 #endif
Header file for the isnan shim.
Constraint on the data type.
Header file for the sparse vector transpose expression.
Header file for the SparseVector base class.
Header file for the sparse vector/sparse vector addition expression.
double Type
Return type of the functions for integral and double arguments.
Definition: CMathTrait.h:79
bool isDefault(const DynamicMatrix< Type, SO > &m)
Returns whether the given dense matrix is in default state.
Definition: DynamicMatrix.h:4642
const Quaternion< Type > sq(const Quaternion< Type > &m)
Squaring the given quaternion.
Definition: Quaternion.h:1034
int16_t sum(const sse_int16_t &a)
Returns the sum of all elements in the 16-bit integral intrinsic vector.
Definition: Reduction.h:62
Header file for the sparse vector/sparse vector multiplication expression.
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:2408
Header file for the DenseVector base class.
bool isnan(const DenseMatrix< MT, SO > &dm)
Checks the given dense matrix for not-a-number elements.
Definition: DenseMatrix.h:640
Header file for the sparse vector/dense vector inner product expression.
Header file for the sparse vector/sparse vector subtraction expression.
Header file for the vector transpose flag types.
Header file for the dense vector/sparse vector multiplication expression.
Header file for the dense vector SMP implementation.
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2412
Header file for the sparse vector/scalar division expression.
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2406
Header file for the sparse vector absolute value expression.
CMathTrait< typename VT::ElementType >::Type length(const DenseVector< VT, TF > &dv)
Calculation of the dense vector length .
Definition: DenseVector.h:483
Header file for the sparse vector/sparse vector inner product expression.
Header file for the equal shim.
Header file for the sparse vector evaluation expression.
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.
Header file for all basic Vector functionality.
bool equal(const T1 &a, const T2 &b)
Generic equality check.
Definition: Equal.h:352
Header file for the square shim.
Header file for the sparse vector serial evaluation expression.
#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
const VT::ElementType max(const SparseVector< VT, TF > &sv)
Returns the largest element of the sparse vector.
Definition: SparseVector.h:408
Header file for the isDefault shim.
Header file for the RemoveReference type trait.
Header file for the cmath trait.
Header file for the dense vector/sparse vector inner product expression.
Header file for the sparse vector/sparse vector multiplication expression.
Header file for the sparse vector/scalar multiplication expression.
Header file for the sparse vector SMP implementation.
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
Header file for basic type definitions.
const VT::ElementType sqrLength(const DenseVector< VT, TF > &dv)
Calculation of the dense vector square length .
Definition: DenseVector.h:513
const VT::ElementType min(const SparseVector< VT, TF > &sv)
Returns the smallest element of the sparse vector.
Definition: SparseVector.h:351