MultiAgentDecisionProcess  Release 0.2.1
VectorTools.h
Go to the documentation of this file.
1 
28 /* Only include this header file once. */
29 #ifndef _VECTORTOOLS_H_
30 #define _VECTORTOOLS_H_ 1
31 
32 /* the include directives */
33 #include "Globals.h"
34 #include <algorithm>
35 
36 namespace VectorTools{
37 
38  template <typename T>
39  bool Equal( const std::vector<T>& vec1, const std::vector<T>& vec2 )
40  {
41  size_t s1 = vec1.size();
42  if( s1 != vec2.size())
43  return false;
44 
45  return( std::equal(vec1.begin(), vec1.end(), vec2.begin() ) );
46 /*
47  typename std::vector< T >::const_iterator it1, it2;
48  it1 = vec1.begin();
49  it2 = vec2.begin();
50  while(it1 != vec1.end() )
51  {
52  if( (*it1) != (*it2) )
53  it1++;
54  it2++;
55  }
56  */
57 
58  }
59 
60  template <typename T>
61  T InnerProduct( const std::vector<T>& vec1, const std::vector<T>& vec2 )
62  {
63  size_t s1 = vec1.size();
64  if( s1 != vec2.size())
65  throw E("VectorTools::InnerProduct - vec sizes not equal");
66 
67  typename std::vector< T >::const_iterator it1, it2;
68  T inprod;
69  it1 = vec1.begin();
70  it2 = vec2.begin();
71  while(it1 != vec1.end() )
72  {
73  inprod += (*it1) * (*it2) ;
74  it1++;
75  it2++;
76  }
77  return(inprod);
78  }
79 
81 
84  template <typename T>
85  T VectorProduct( const std::vector<T>& vec )
86  {
87  if(vec.size() == 0)
88  throw E("IndexTools::VectorProduct - vector product of vec of size 0 is undefined!");
89 
90  T product = *(vec.begin());//the first element
91  typename std::vector< T >::const_iterator it;
92  it = vec.begin() + 1;//second element
93  while(it != vec.end() )
94  {
95  product = product * (*it);
96  it++;
97  }
98  return(product);
99  }
100 
101  template <typename T>
102  T MaxNorm( const std::vector<T>& vec )
103  {
104  T norm = 0.0 ;
105  for (Index i = 0 ; i < vec.size() ; i++)
106  norm = max (fabs (vec [i]), norm) ;
107  return norm;
108  }
109 
110 }
111 
112 #endif /* !_VECTORTOOLS_H_ */
113 
114 // Local Variables: ***
115 // mode:c++ ***
116 // End: ***