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>
59 #include <blaze/math/shims/Equal.h>
61 #include <blaze/math/shims/IsNaN.h>
65 #include <blaze/math/Vector.h>
67 #include <blaze/util/Assert.h>
68 #include <blaze/util/Types.h>
70 
71 
72 namespace blaze {
73 
74 //=================================================================================================
75 //
76 // GLOBAL OPERATORS
77 //
78 //=================================================================================================
79 
80 //*************************************************************************************************
83 template< typename T1, bool TF1, typename T2, bool TF2 >
84 inline bool operator==( const SparseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs );
85 
86 template< typename T1, bool TF1, typename T2, bool TF2 >
87 inline bool operator!=( const SparseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs );
89 //*************************************************************************************************
90 
91 
92 //*************************************************************************************************
100 template< typename T1 // Type of the left-hand side sparse vector
101  , bool TF1 // Transpose flag of the left-hand side sparse vector
102  , typename T2 // Type of the right-hand side sparse vector
103  , bool TF2 > // Transpose flag of the right-hand side sparse vector
104 inline bool operator==( const SparseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
105 {
106  typedef typename T1::CompositeType CT1;
107  typedef typename T2::CompositeType CT2;
108  typedef typename RemoveReference<CT1>::Type::ConstIterator LhsConstIterator;
109  typedef typename RemoveReference<CT2>::Type::ConstIterator RhsConstIterator;
110 
111  // Early exit in case the vector sizes don't match
112  if( (~lhs).size() != (~rhs).size() ) return false;
113 
114  // Evaluation of the two sparse vector operands
115  CT1 a( ~lhs );
116  CT2 b( ~rhs );
117 
118  // In order to compare the two vectors, the data values of the lower-order data
119  // type are converted to the higher-order data type within the equal function.
120  const LhsConstIterator lend( a.end() );
121  const RhsConstIterator rend( b.end() );
122 
123  LhsConstIterator lelem( a.begin() );
124  RhsConstIterator relem( b.begin() );
125 
126  while( lelem != lend && relem != rend )
127  {
128  if( isDefault( lelem->value() ) ) { ++lelem; continue; }
129  if( isDefault( relem->value() ) ) { ++relem; continue; }
130 
131  if( lelem->index() != relem->index() || !equal( lelem->value(), relem->value() ) ) {
132  return false;
133  }
134  else {
135  ++lelem;
136  ++relem;
137  }
138  }
139 
140  while( lelem != lend ) {
141  if( !isDefault( lelem->value() ) )
142  return false;
143  ++lelem;
144  }
145 
146  while( relem != rend ) {
147  if( !isDefault( relem->value() ) )
148  return false;
149  ++relem;
150  }
151 
152  return true;
153 }
154 //*************************************************************************************************
155 
156 
157 //*************************************************************************************************
165 template< typename T1 // Type of the left-hand side sparse vector
166  , bool TF1 // Transpose flag of the left-hand side sparse vector
167  , typename T2 // Type of the right-hand side sparse vector
168  , bool TF2 > // Transpose flag of the right-hand side sparse vector
169 inline bool operator!=( const SparseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
170 {
171  return !( lhs == rhs );
172 }
173 //*************************************************************************************************
174 
175 
176 
177 
178 //=================================================================================================
179 //
180 // GLOBAL FUNCTIONS
181 //
182 //=================================================================================================
183 
184 //*************************************************************************************************
187 template< typename VT, bool TF >
188 bool isnan( const SparseVector<VT,TF>& sv );
189 
190 template< typename VT, bool TF >
191 typename CMathTrait<typename VT::ElementType>::Type length( const SparseVector<VT,TF>& sv );
192 
193 template< typename VT, bool TF >
194 const typename VT::ElementType sqrLength( const SparseVector<VT,TF>& sv );
195 
196 template< typename VT, bool TF >
197 const typename VT::ElementType min( const SparseVector<VT,TF>& sv );
198 
199 template< typename VT, bool TF >
200 const typename VT::ElementType max( const SparseVector<VT,TF>& sv );
202 //*************************************************************************************************
203 
204 
205 //*************************************************************************************************
225 template< typename VT // Type of the sparse vector
226  , bool TF > // Transpose flag
227 inline bool isnan( const SparseVector<VT,TF>& sv )
228 {
229  typedef typename VT::CompositeType CT;
231 
232  CT a( ~sv ); // Evaluation of the sparse vector operand
233 
234  const ConstIterator end( a.end() );
235  for( ConstIterator element=a.begin(); element!=end; ++element ) {
236  if( isnan( element->value() ) ) return true;
237  }
238  return false;
239 }
240 //*************************************************************************************************
241 
242 
243 //*************************************************************************************************
276 template< typename VT // Type of the sparse vector
277  , bool TF > // Transpose flag
279 {
280  typedef typename VT::ElementType ElementType;
281  typedef typename VT::ConstIterator ConstIterator;
282  typedef typename CMathTrait<ElementType>::Type LengthType;
283 
285 
286  LengthType sum( 0 );
287  for( ConstIterator element=(~sv).begin(); element!=(~sv).end(); ++element )
288  sum += sq( element->value() );
289  return std::sqrt( sum );
290 }
291 //*************************************************************************************************
292 
293 
294 //*************************************************************************************************
307 template< typename VT // Type of the sparse vector
308  , bool TF > // Transpose flag
309 const typename VT::ElementType sqrLength( const SparseVector<VT,TF>& sv )
310 {
311  typedef typename VT::ElementType ElementType;
312  typedef typename VT::ConstIterator ConstIterator;
313 
315 
316  ElementType sum( 0 );
317  for( ConstIterator element=(~sv).begin(); element!=(~sv).end(); ++element )
318  sum += sq( element->value() );
319  return sum;
320 }
321 //*************************************************************************************************
322 
323 
324 //*************************************************************************************************
346 template< typename VT // Type of the sparse vector
347  , bool TF > // Transpose flag
348 const typename VT::ElementType min( const SparseVector<VT,TF>& sv )
349 {
350  using blaze::min;
351 
352  typedef typename VT::ElementType ET;
353  typedef typename VT::CompositeType CT;
355 
356  CT a( ~sv ); // Evaluation of the sparse vector operand
357 
358  const ConstIterator end( a.end() );
359  ConstIterator element( a.begin() );
360 
361  if( element == end ) {
362  return ET();
363  }
364  else if( a.nonZeros() == a.size() ) {
365  ET minimum( element->value() );
366  ++element;
367  for( ; element!=end; ++element )
368  minimum = min( minimum, element->value() );
369  return minimum;
370  }
371  else {
372  ET minimum = ET();
373  for( ; element!=end; ++element )
374  minimum = min( minimum, element->value() );
375  return minimum;
376  }
377 }
378 //*************************************************************************************************
379 
380 
381 //*************************************************************************************************
403 template< typename VT // Type of the sparse vector
404  , bool TF > // Transpose flag
405 const typename VT::ElementType max( const SparseVector<VT,TF>& sv )
406 {
407  using blaze::max;
408 
409  typedef typename VT::ElementType ET;
410  typedef typename VT::CompositeType CT;
412 
413  CT a( ~sv ); // Evaluation of the sparse vector operand
414 
415  const ConstIterator end( a.end() );
416  ConstIterator element( a.begin() );
417 
418  if( element == end ) {
419  return ET();
420  }
421  else if( a.nonZeros() == a.size() ) {
422  ET maximum( element->value() );
423  ++element;
424  for( ; element!=end; ++element )
425  maximum = max( maximum, element->value() );
426  return maximum;
427  }
428  else {
429  ET maximum = ET();
430  for( ; element!=end; ++element )
431  maximum = max( maximum, element->value() );
432  return maximum;
433  }
434 }
435 //*************************************************************************************************
436 
437 } // namespace blaze
438 
439 #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 &lt;cmath&gt; 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:4622
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:2384
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:637
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.
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2388
Header file for the sparse vector/scalar division expression.
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2382
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:480
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.
#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:405
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.
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:105
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:510
const VT::ElementType min(const SparseVector< VT, TF > &sv)
Returns the smallest element of the sparse vector.
Definition: SparseVector.h:348