All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DenseVector.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_DENSEVECTOR_H_
23 #define _BLAZE_MATH_DENSEVECTOR_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <cmath>
49 #include <blaze/math/Functions.h>
50 #include <blaze/math/shims/Equal.h>
55 #include <blaze/math/Vector.h>
56 #include <blaze/util/Assert.h>
58 #include <blaze/util/EnableIf.h>
59 #include <blaze/util/Types.h>
62 
63 
64 namespace blaze {
65 
66 //=================================================================================================
67 //
68 // GLOBAL OPERATORS
69 //
70 //=================================================================================================
71 
72 //*************************************************************************************************
75 template< typename T1, bool TF1, typename T2, bool TF2 >
76 inline bool operator==( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
77 
78 template< typename T1, bool TF1, typename T2, bool TF2 >
79 inline bool operator==( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs );
80 
81 template< typename T1, bool TF1, typename T2, bool TF2 >
82 inline bool operator==( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
83 
84 template< typename T1, typename T2, bool TF >
85 inline typename EnableIf< IsNumeric<T2>, bool >::Type
86  operator==( const DenseVector<T1,TF>& vec, T2 scalar );
87 
88 template< typename T1, typename T2, bool TF >
89 inline typename EnableIf< IsNumeric<T1>, bool >::Type
90  operator==( T1 scalar, const DenseVector<T2,TF>& vec );
91 
92 template< typename T1, bool TF1, typename T2, bool TF2 >
93 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
94 
95 template< typename T1, bool TF1, typename T2, bool TF2 >
96 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs );
97 
98 template< typename T1, bool TF1, typename T2, bool TF2 >
99 inline bool operator!=( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
100 
101 template< typename T1, typename T2, bool TF >
102 inline typename EnableIf< IsNumeric<T2>, bool >::Type
103  operator!=( const DenseVector<T1,TF>& vec, T2 scalar );
104 
105 template< typename T1, typename T2, bool TF >
106 inline typename EnableIf< IsNumeric<T1>, bool >::Type
107  operator!=( T1 scalar, const DenseVector<T2,TF>& vec );
109 //*************************************************************************************************
110 
111 
112 //*************************************************************************************************
120 template< typename T1 // Type of the left-hand side dense vector
121  , bool TF1 // Transpose flag of the left-hand side dense vector
122  , typename T2 // Type of the right-hand side dense vector
123  , bool TF2 > // Transpose flag of the right-hand side dense vector
124 inline bool operator==( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
125 {
126  typedef typename T1::CompositeType CT1;
127  typedef typename T2::CompositeType CT2;
128 
129  // Early exit in case the vector sizes don't match
130  if( (~lhs).size() != (~rhs).size() ) return false;
131 
132  // Evaluation of the two dense vector operands
133  CT1 a( ~lhs );
134  CT2 b( ~rhs );
135 
136  // In order to compare the two vectors, the data values of the lower-order data
137  // type are converted to the higher-order data type within the equal function.
138  for( size_t i=0; i<a.size(); ++i )
139  if( !equal( a[i], b[i] ) ) return false;
140  return true;
141 }
142 //*************************************************************************************************
143 
144 
145 //*************************************************************************************************
153 template< typename T1 // Type of the left-hand side dense vector
154  , bool TF1 // Transpose flag of the left-hand side dense vector
155  , typename T2 // Type of the right-hand side sparse vector
156  , bool TF2 > // Transpose flag of the right-hand side sparse vector
157 inline bool operator==( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
158 {
159  typedef typename T1::CompositeType CT1;
160  typedef typename T2::CompositeType CT2;
161  typedef typename RemoveReference<CT2>::Type::ConstIterator ConstIterator;
162 
163  // Early exit in case the vector sizes don't match
164  if( (~lhs).size() != (~rhs).size() ) return false;
165 
166  // Evaluation of the dense vector and sparse vector operand
167  CT1 a( ~lhs );
168  CT2 b( ~rhs );
169 
170  // In order to compare the two vectors, the data values of the lower-order data
171  // type are converted to the higher-order data type within the equal function.
172  size_t i( 0 );
173 
174  for( ConstIterator element=b.begin(); element!=b.end(); ++element, ++i ) {
175  for( ; i<element->index(); ++i ) {
176  if( !isDefault( a[i] ) ) return false;
177  }
178  if( !equal( element->value(), a[i] ) ) return false;
179  }
180  for( ; i<a.size(); ++i ) {
181  if( !isDefault( a[i] ) ) return false;
182  }
183 
184  return true;
185 }
186 //*************************************************************************************************
187 
188 
189 //*************************************************************************************************
197 template< typename T1 // Type of the left-hand side sparse vector
198  , bool TF1 // Transpose flag of the left-hand side sparse vector
199  , typename T2 // Type of the right-hand side dense vector
200  , bool TF2 > // Transpose flag of the right-hand side dense vector
201 inline bool operator==( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
202 {
203  return ( rhs == lhs );
204 }
205 //*************************************************************************************************
206 
207 
208 //*************************************************************************************************
220 template< typename T1 // Type of the left-hand side dense vector
221  , typename T2 // Type of the right-hand side scalar
222  , bool TF > // Transpose flag
223 inline typename EnableIf< IsNumeric<T2>, bool >::Type
224  operator==( const DenseVector<T1,TF>& vec, T2 scalar )
225 {
226  typedef typename T1::CompositeType CT1;
227 
228  // Evaluation of the dense vector operand
229  CT1 a( ~vec );
230 
231  // In order to compare the vector and the scalar value, the data values of the lower-order
232  // data type are converted to the higher-order data type within the equal function.
233  for( size_t i=0; i<a.size(); ++i )
234  if( !equal( a[i], scalar ) ) return false;
235  return true;
236 }
237 //*************************************************************************************************
238 
239 
240 //*************************************************************************************************
252 template< typename T1 // Type of the left-hand side scalar
253  , typename T2 // Type of the right-hand side dense vector
254  , bool TF > // Transpose flag
255 inline typename EnableIf< IsNumeric<T1>, bool >::Type
256  operator==( T1 scalar, const DenseVector<T2,TF>& vec )
257 {
258  return ( vec == scalar );
259 }
260 //*************************************************************************************************
261 
262 
263 //*************************************************************************************************
271 template< typename T1 // Type of the left-hand side dense vector
272  , bool TF1 // Transpose flag of the left-hand side dense vector
273  , typename T2 // Type of the right-hand side dense vector
274  , bool TF2 > // Transpose flag of the right-hand side dense vector
275 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
276 {
277  return !( lhs == rhs );
278 }
279 //*************************************************************************************************
280 
281 
282 //*************************************************************************************************
290 template< typename T1 // Type of the left-hand side dense vector
291  , bool TF1 // Transpose flag of the left-hand side dense vector
292  , typename T2 // Type of the right-hand side sparse vector
293  , bool TF2 > // Transpose flag of the right-hand side sparse vector
294 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
295 {
296  return !( lhs == rhs );
297 }
298 //*************************************************************************************************
299 
300 
301 //*************************************************************************************************
309 template< typename T1 // Type of the left-hand side sparse vector
310  , bool TF1 // Transpose flag of the left-hand side sparse vector
311  , typename T2 // Type of the right-hand side dense vector
312  , bool TF2 > // Transpose flag of the right-hand side dense vector
313 inline bool operator!=( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
314 {
315  return !( rhs == lhs );
316 }
317 //*************************************************************************************************
318 
319 
320 //*************************************************************************************************
332 template< typename T1 // Type of the left-hand side dense vector
333  , typename T2 // Type of the right-hand side scalar
334  , bool TF > // Transpose flag
335 inline typename EnableIf< IsNumeric<T2>, bool >::Type
336  operator!=( const DenseVector<T1,TF>& vec, T2 scalar )
337 {
338  return !( vec == scalar );
339 }
340 //*************************************************************************************************
341 
342 
343 //*************************************************************************************************
355 template< typename T1 // Type of the left-hand side scalar
356  , typename T2 // Type of the right-hand side vector
357  , bool TF > // Transpose flag
358 inline typename EnableIf< IsNumeric<T1>, bool >::Type
359  operator!=( T1 scalar, const DenseVector<T2,TF>& vec )
360 {
361  return !( vec == scalar );
362 }
363 //*************************************************************************************************
364 
365 
366 
367 
368 //=================================================================================================
369 //
370 // GLOBAL FUNCTIONS
371 //
372 //=================================================================================================
373 
374 //*************************************************************************************************
377 template< typename VT, bool TF >
378 inline typename CMathTrait<typename VT::ElementType>::Type length( const DenseVector<VT,TF>& dv );
379 
380 template< typename VT, bool TF >
381 inline const typename VT::ElementType sqrLength( const DenseVector<VT,TF>& dv );
382 
383 template< typename VT, bool TF >
384 inline const typename VT::ElementType min( const DenseVector<VT,TF>& dv );
385 
386 template< typename VT, bool TF >
387 inline const typename VT::ElementType max( const DenseVector<VT,TF>& dv );
389 //*************************************************************************************************
390 
391 
392 //*************************************************************************************************
424 template< typename VT // Type of the dense vector
425  , bool TF > // Transpose flag
427 {
428  typedef typename VT::ElementType ElementType;
429  typedef typename CMathTrait<ElementType>::Type LengthType;
430 
432 
433  LengthType sum( 0 );
434  for( size_t i=0UL; i<(~dv).size(); ++i )
435  sum += sq( (~dv)[i] );
436  return std::sqrt( sum );
437 }
438 //*************************************************************************************************
439 
440 
441 //*************************************************************************************************
453 template< typename VT // Type of the dense vector
454  , bool TF > // Transpose flag
455 inline const typename VT::ElementType sqrLength( const DenseVector<VT,TF>& dv )
456 {
457  typedef typename VT::ElementType ElementType;
458 
460 
461  ElementType sum( 0 );
462  for( size_t i=0UL; i<(~dv).size(); ++i )
463  sum += sq( (~dv)[i] );
464  return sum;
465 }
466 //*************************************************************************************************
467 
468 
469 //*************************************************************************************************
480 template< typename VT // Type of the dense vector
481  , bool TF > // Transpose flag
482 inline const typename VT::ElementType min( const DenseVector<VT,TF>& dv )
483 {
484  using blaze::min;
485 
486  typedef typename VT::ElementType ET;
487 
488  if( (~dv).size() == 0 ) return ET();
489 
490  ET minimum( (~dv)[0] );
491  for( size_t i=1; i<(~dv).size(); ++i )
492  minimum = min( minimum, (~dv)[i] );
493  return minimum;
494 }
495 //*************************************************************************************************
496 
497 
498 //*************************************************************************************************
509 template< typename VT // Type of the dense vector
510  , bool TF > // Transpose flag
511 inline const typename VT::ElementType max( const DenseVector<VT,TF>& dv )
512 {
513  using blaze::max;
514 
515  typedef typename VT::ElementType ET;
516 
517  if( (~dv).size() == 0 ) return ET();
518 
519  ET maximum( (~dv)[0] );
520  for( size_t i=1; i<(~dv).size(); ++i )
521  maximum = max( maximum, (~dv)[i] );
522  return maximum;
523 }
524 //*************************************************************************************************
525 
526 } // namespace blaze
527 
528 #endif