All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SparseVector.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_SPARSEVECTOR_H_
23 #define _BLAZE_MATH_SPARSEVECTOR_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <cmath>
46 #include <blaze/math/shims/Equal.h>
51 #include <blaze/math/Vector.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 inline typename CMathTrait<typename VT::ElementType>::Type length( const SparseVector<VT,TF>& dv );
175 
176 template< typename VT, bool TF >
177 inline const typename VT::ElementType sqrLength( const SparseVector<VT,TF>& dv );
178 
179 template< typename VT, bool TF >
180 inline const typename VT::ElementType min( const SparseVector<VT,TF>& sv );
181 
182 template< typename VT, bool TF >
183 inline const typename VT::ElementType max( const SparseVector<VT,TF>& sv );
185 //*************************************************************************************************
186 
187 
188 //*************************************************************************************************
220 template< typename VT // Type of the sparse vector
221  , bool TF > // Transpose flag
223 {
224  typedef typename VT::ElementType ElementType;
225  typedef typename VT::ConstIterator ConstIterator;
226  typedef typename CMathTrait<ElementType>::Type LengthType;
227 
229 
230  LengthType sum( 0 );
231  for( ConstIterator element=(~sv).begin(); element!=(~sv).end(); ++element )
232  sum += sq( element->value() );
233  return std::sqrt( sum );
234 }
235 //*************************************************************************************************
236 
237 
238 //*************************************************************************************************
250 template< typename VT // Type of the sparse vector
251  , bool TF > // Transpose flag
252 inline const typename VT::ElementType sqrLength( const SparseVector<VT,TF>& sv )
253 {
254  typedef typename VT::ElementType ElementType;
255  typedef typename VT::ConstIterator ConstIterator;
256 
258 
259  ElementType sum( 0 );
260  for( ConstIterator element=(~sv).begin(); element!=(~sv).end(); ++element )
261  sum += sq( element->value() );
262  return sum;
263 }
264 //*************************************************************************************************
265 
266 
267 //*************************************************************************************************
287 template< typename VT // Type of the sparse vector
288  , bool TF > // Transpose flag
289 inline const typename VT::ElementType min( const SparseVector<VT,TF>& sv )
290 {
291  using blaze::min;
292 
293  typedef typename VT::ElementType ET;
294  typedef typename VT::CompositeType SV;
295  typedef typename RemoveReference<SV>::Type::ConstIterator ConstIterator;
296 
297  SV a( ~sv ); // Evaluation of the sparse vector operand
298 
299  const ConstIterator end( a.end() );
300  ConstIterator element( a.begin() );
301 
302  if( element == end ) {
303  return ET();
304  }
305  else if( a.nonZeros() == a.size() ) {
306  ET minimum( element->value() );
307  ++element;
308  for( ; element!=end; ++element )
309  minimum = min( minimum, element->value() );
310  return minimum;
311  }
312  else {
313  ET minimum = ET();
314  for( ; element!=end; ++element )
315  minimum = min( minimum, element->value() );
316  return minimum;
317  }
318 }
319 //*************************************************************************************************
320 
321 
322 //*************************************************************************************************
342 template< typename VT // Type of the sparse vector
343  , bool TF > // Transpose flag
344 inline const typename VT::ElementType max( const SparseVector<VT,TF>& sv )
345 {
346  using blaze::max;
347 
348  typedef typename VT::ElementType ET;
349  typedef typename VT::CompositeType SV;
350  typedef typename RemoveReference<SV>::Type::ConstIterator ConstIterator;
351 
352  SV a( ~sv ); // Evaluation of the sparse vector operand
353 
354  const ConstIterator end( a.end() );
355  ConstIterator element( a.begin() );
356 
357  if( element == end ) {
358  return ET();
359  }
360  else if( a.nonZeros() == a.size() ) {
361  ET maximum( element->value() );
362  ++element;
363  for( ; element!=end; ++element )
364  maximum = max( maximum, element->value() );
365  return maximum;
366  }
367  else {
368  ET maximum = ET();
369  for( ; element!=end; ++element )
370  maximum = max( maximum, element->value() );
371  return maximum;
372  }
373 }
374 //*************************************************************************************************
375 
376 } // namespace blaze
377 
378 #endif