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_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 typename CMathTrait<typename VT::ElementType>::Type length( const SparseVector<VT,TF>& sv );
178 
179 template< typename VT, bool TF >
180 const typename VT::ElementType sqrLength( const SparseVector<VT,TF>& sv );
181 
182 template< typename VT, bool TF >
183 const typename VT::ElementType min( const SparseVector<VT,TF>& sv );
184 
185 template< typename VT, bool TF >
186 const typename VT::ElementType max( const SparseVector<VT,TF>& sv );
188 //*************************************************************************************************
189 
190 
191 //*************************************************************************************************
211 template< typename VT // Type of the sparse vector
212  , bool TF > // Transpose flag
213 inline bool isnan( const SparseVector<VT,TF>& sv )
214 {
215  typedef typename VT::CompositeType CT;
217 
218  CT a( ~sv ); // Evaluation of the sparse vector operand
219 
220  const ConstIterator end( a.end() );
221  for( ConstIterator element=a.begin(); element!=end; ++element ) {
222  if( isnan( element->value() ) ) return true;
223  }
224  return false;
225 }
226 //*************************************************************************************************
227 
228 
229 //*************************************************************************************************
262 template< typename VT // Type of the sparse vector
263  , bool TF > // Transpose flag
265 {
266  typedef typename VT::ElementType ElementType;
267  typedef typename VT::ConstIterator ConstIterator;
268  typedef typename CMathTrait<ElementType>::Type LengthType;
269 
271 
272  LengthType sum( 0 );
273  for( ConstIterator element=(~sv).begin(); element!=(~sv).end(); ++element )
274  sum += sq( element->value() );
275  return std::sqrt( sum );
276 }
277 //*************************************************************************************************
278 
279 
280 //*************************************************************************************************
293 template< typename VT // Type of the sparse vector
294  , bool TF > // Transpose flag
295 const typename VT::ElementType sqrLength( const SparseVector<VT,TF>& sv )
296 {
297  typedef typename VT::ElementType ElementType;
298  typedef typename VT::ConstIterator ConstIterator;
299 
301 
302  ElementType sum( 0 );
303  for( ConstIterator element=(~sv).begin(); element!=(~sv).end(); ++element )
304  sum += sq( element->value() );
305  return sum;
306 }
307 //*************************************************************************************************
308 
309 
310 //*************************************************************************************************
332 template< typename VT // Type of the sparse vector
333  , bool TF > // Transpose flag
334 const typename VT::ElementType min( const SparseVector<VT,TF>& sv )
335 {
336  using blaze::min;
337 
338  typedef typename VT::ElementType ET;
339  typedef typename VT::CompositeType CT;
341 
342  CT a( ~sv ); // Evaluation of the sparse vector operand
343 
344  const ConstIterator end( a.end() );
345  ConstIterator element( a.begin() );
346 
347  if( element == end ) {
348  return ET();
349  }
350  else if( a.nonZeros() == a.size() ) {
351  ET minimum( element->value() );
352  ++element;
353  for( ; element!=end; ++element )
354  minimum = min( minimum, element->value() );
355  return minimum;
356  }
357  else {
358  ET minimum = ET();
359  for( ; element!=end; ++element )
360  minimum = min( minimum, element->value() );
361  return minimum;
362  }
363 }
364 //*************************************************************************************************
365 
366 
367 //*************************************************************************************************
389 template< typename VT // Type of the sparse vector
390  , bool TF > // Transpose flag
391 const typename VT::ElementType max( const SparseVector<VT,TF>& sv )
392 {
393  using blaze::max;
394 
395  typedef typename VT::ElementType ET;
396  typedef typename VT::CompositeType CT;
398 
399  CT a( ~sv ); // Evaluation of the sparse vector operand
400 
401  const ConstIterator end( a.end() );
402  ConstIterator element( a.begin() );
403 
404  if( element == end ) {
405  return ET();
406  }
407  else if( a.nonZeros() == a.size() ) {
408  ET maximum( element->value() );
409  ++element;
410  for( ; element!=end; ++element )
411  maximum = max( maximum, element->value() );
412  return maximum;
413  }
414  else {
415  ET maximum = ET();
416  for( ; element!=end; ++element )
417  maximum = max( maximum, element->value() );
418  return maximum;
419  }
420 }
421 //*************************************************************************************************
422 
423 } // namespace blaze
424 
425 #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:994
Constraint on the data type.
Header file for mathematical functions.
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:258
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 Quaternion< Type > sq(const Quaternion< Type > &m)
Squaring the given quaternion.
Definition: Quaternion.h:1034
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:2478
bool isnan(const DenseMatrix< MT, SO > &dm)
Checks the given dense matrix for not-a-number elements.
Definition: DenseMatrix.h:609
Header file for the vector transpose flag types.
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2482
const MT::ElementType min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:947
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:2476
CMathTrait< typename VT::ElementType >::Type length(const DenseVector< VT, TF > &dv)
Calculation of the dense vector length .
Definition: DenseVector.h:463
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.
BLAZE_ALWAYS_INLINE bool isDefault(const NonNumericProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: NonNumericProxy.h:874
Header file for the RemoveReference type trait.
Header file for the cmath trait.
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:493