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 <boost/type_traits/remove_reference.hpp>
49 #include <blaze/math/Functions.h>
50 #include <blaze/math/shims/Equal.h>
53 #include <blaze/math/Vector.h>
54 #include <blaze/util/Assert.h>
55 #include <blaze/util/EnableIf.h>
56 #include <blaze/util/Types.h>
58 
59 
60 namespace blaze {
61 
62 //=================================================================================================
63 //
64 // GLOBAL OPERATORS
65 //
66 //=================================================================================================
67 
68 //*************************************************************************************************
71 template< typename T1, bool TF1, typename T2, bool TF2 >
72 inline bool operator==( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
73 
74 template< typename T1, bool TF1, typename T2, bool TF2 >
75 inline bool operator==( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs );
76 
77 template< typename T1, bool TF1, typename T2, bool TF2 >
78 inline bool operator==( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
79 
80 template< typename T1, typename T2, bool TF >
81 inline typename EnableIf< IsNumeric<T2>, bool >::Type
82  operator==( const DenseVector<T1,TF>& vec, T2 scalar );
83 
84 template< typename T1, typename T2, bool TF >
85 inline typename EnableIf< IsNumeric<T1>, bool >::Type
86  operator==( T1 scalar, const DenseVector<T2,TF>& vec );
87 
88 template< typename T1, bool TF1, typename T2, bool TF2 >
89 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
90 
91 template< typename T1, bool TF1, typename T2, bool TF2 >
92 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs );
93 
94 template< typename T1, bool TF1, typename T2, bool TF2 >
95 inline bool operator!=( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs );
96 
97 template< typename T1, typename T2, bool TF >
98 inline typename EnableIf< IsNumeric<T2>, bool >::Type
99  operator!=( const DenseVector<T1,TF>& vec, T2 scalar );
100 
101 template< typename T1, typename T2, bool TF >
102 inline typename EnableIf< IsNumeric<T1>, bool >::Type
103  operator!=( T1 scalar, const DenseVector<T2,TF>& vec );
105 //*************************************************************************************************
106 
107 
108 //*************************************************************************************************
116 template< typename T1 // Type of the left-hand side dense vector
117  , bool TF1 // Transpose flag of the left-hand side dense vector
118  , typename T2 // Type of the right-hand side dense vector
119  , bool TF2 > // Transpose flag of the right-hand side dense vector
120 inline bool operator==( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
121 {
122  typedef typename T1::CompositeType CT1;
123  typedef typename T2::CompositeType CT2;
124 
125  // Early exit in case the vector sizes don't match
126  if( (~lhs).size() != (~rhs).size() ) return false;
127 
128  // Evaluation of the two dense vector operands
129  CT1 a( ~lhs );
130  CT2 b( ~rhs );
131 
132  // In order to compare the two vectors, the data values of the lower-order data
133  // type are converted to the higher-order data type within the equal function.
134  for( size_t i=0; i<a.size(); ++i )
135  if( !equal( a[i], b[i] ) ) return false;
136  return true;
137 }
138 //*************************************************************************************************
139 
140 
141 //*************************************************************************************************
149 template< typename T1 // Type of the left-hand side dense vector
150  , bool TF1 // Transpose flag of the left-hand side dense vector
151  , typename T2 // Type of the right-hand side sparse vector
152  , bool TF2 > // Transpose flag of the right-hand side sparse vector
153 inline bool operator==( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
154 {
155  typedef typename T1::CompositeType CT1;
156  typedef typename T2::CompositeType CT2;
157  typedef typename boost::remove_reference<CT2>::type::ConstIterator ConstIterator;
158 
159  // Early exit in case the vector sizes don't match
160  if( (~lhs).size() != (~rhs).size() ) return false;
161 
162  // Evaluation of the dense vector and sparse vector operand
163  CT1 a( ~lhs );
164  CT2 b( ~rhs );
165 
166  // In order to compare the two vectors, the data values of the lower-order data
167  // type are converted to the higher-order data type within the equal function.
168  size_t i( 0 );
169 
170  for( ConstIterator element=b.begin(); element!=b.end(); ++element, ++i ) {
171  for( ; i<element->index(); ++i ) {
172  if( !isDefault( a[i] ) ) return false;
173  }
174  if( !equal( element->value(), a[i] ) ) return false;
175  }
176  for( ; i<a.size(); ++i ) {
177  if( !isDefault( a[i] ) ) return false;
178  }
179 
180  return true;
181 }
182 //*************************************************************************************************
183 
184 
185 //*************************************************************************************************
193 template< typename T1 // Type of the left-hand side sparse vector
194  , bool TF1 // Transpose flag of the left-hand side sparse vector
195  , typename T2 // Type of the right-hand side dense vector
196  , bool TF2 > // Transpose flag of the right-hand side dense vector
197 inline bool operator==( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
198 {
199  return ( rhs == lhs );
200 }
201 //*************************************************************************************************
202 
203 
204 //*************************************************************************************************
216 template< typename T1 // Type of the left-hand side dense vector
217  , typename T2 // Type of the right-hand side scalar
218  , bool TF > // Transpose flag
219 inline typename EnableIf< IsNumeric<T2>, bool >::Type
220  operator==( const DenseVector<T1,TF>& vec, T2 scalar )
221 {
222  typedef typename T1::CompositeType CT1;
223 
224  // Evaluation of the dense vector operand
225  CT1 a( ~vec );
226 
227  // In order to compare the vector and the scalar value, the data values of the lower-order
228  // data type are converted to the higher-order data type within the equal function.
229  for( size_t i=0; i<a.size(); ++i )
230  if( !equal( a[i], scalar ) ) return false;
231  return true;
232 }
233 //*************************************************************************************************
234 
235 
236 //*************************************************************************************************
248 template< typename T1 // Type of the left-hand side scalar
249  , typename T2 // Type of the right-hand side dense vector
250  , bool TF > // Transpose flag
251 inline typename EnableIf< IsNumeric<T1>, bool >::Type
252  operator==( T1 scalar, const DenseVector<T2,TF>& vec )
253 {
254  return ( vec == scalar );
255 }
256 //*************************************************************************************************
257 
258 
259 //*************************************************************************************************
267 template< typename T1 // Type of the left-hand side dense vector
268  , bool TF1 // Transpose flag of the left-hand side dense vector
269  , typename T2 // Type of the right-hand side dense vector
270  , bool TF2 > // Transpose flag of the right-hand side dense vector
271 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
272 {
273  return !( lhs == rhs );
274 }
275 //*************************************************************************************************
276 
277 
278 //*************************************************************************************************
286 template< typename T1 // Type of the left-hand side dense vector
287  , bool TF1 // Transpose flag of the left-hand side dense vector
288  , typename T2 // Type of the right-hand side sparse vector
289  , bool TF2 > // Transpose flag of the right-hand side sparse vector
290 inline bool operator!=( const DenseVector<T1,TF1>& lhs, const SparseVector<T2,TF2>& rhs )
291 {
292  return !( lhs == rhs );
293 }
294 //*************************************************************************************************
295 
296 
297 //*************************************************************************************************
305 template< typename T1 // Type of the left-hand side sparse vector
306  , bool TF1 // Transpose flag of the left-hand side sparse vector
307  , typename T2 // Type of the right-hand side dense vector
308  , bool TF2 > // Transpose flag of the right-hand side dense vector
309 inline bool operator!=( const SparseVector<T1,TF1>& lhs, const DenseVector<T2,TF2>& rhs )
310 {
311  return !( rhs == lhs );
312 }
313 //*************************************************************************************************
314 
315 
316 //*************************************************************************************************
328 template< typename T1 // Type of the left-hand side dense vector
329  , typename T2 // Type of the right-hand side scalar
330  , bool TF > // Transpose flag
331 inline typename EnableIf< IsNumeric<T2>, bool >::Type
332  operator!=( const DenseVector<T1,TF>& vec, T2 scalar )
333 {
334  return !( vec == scalar );
335 }
336 //*************************************************************************************************
337 
338 
339 //*************************************************************************************************
351 template< typename T1 // Type of the left-hand side scalar
352  , typename T2 // Type of the right-hand side vector
353  , bool TF > // Transpose flag
354 inline typename EnableIf< IsNumeric<T1>, bool >::Type
355  operator!=( T1 scalar, const DenseVector<T2,TF>& vec )
356 {
357  return !( vec == scalar );
358 }
359 //*************************************************************************************************
360 
361 
362 
363 
364 //=================================================================================================
365 //
366 // GLOBAL FUNCTIONS
367 //
368 //=================================================================================================
369 
370 //*************************************************************************************************
373 template< typename VT, bool TF >
374 inline const typename VT::ElementType min( const DenseVector<VT,TF>& dv );
375 
376 template< typename VT, bool TF >
377 inline const typename VT::ElementType max( const DenseVector<VT,TF>& dv );
379 //*************************************************************************************************
380 
381 
382 //*************************************************************************************************
393 template< typename VT // Type of the dense vector
394  , bool TF > // Transpose flag
395 inline const typename VT::ElementType min( const DenseVector<VT,TF>& dv )
396 {
397  using blaze::min;
398 
399  typedef typename VT::ElementType ET;
400 
401  if( (~dv).size() == 0 ) return ET();
402 
403  ET minimum( (~dv)[0] );
404  for( size_t i=1; i<(~dv).size(); ++i )
405  minimum = min( minimum, (~dv)[i] );
406  return minimum;
407 }
408 //*************************************************************************************************
409 
410 
411 //*************************************************************************************************
422 template< typename VT // Type of the dense vector
423  , bool TF > // Transpose flag
424 inline const typename VT::ElementType max( const DenseVector<VT,TF>& dv )
425 {
426  using blaze::max;
427 
428  typedef typename VT::ElementType ET;
429 
430  if( (~dv).size() == 0 ) return ET();
431 
432  ET maximum( (~dv)[0] );
433  for( size_t i=1; i<(~dv).size(); ++i )
434  maximum = max( maximum, (~dv)[i] );
435  return maximum;
436 }
437 //*************************************************************************************************
438 
439 } // namespace blaze
440 
441 #endif