NOMAD Source  Version 4.0.0 Beta
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Direction.cpp
Go to the documentation of this file.
1 /**
2  \file Direction.cpp
3  \brief Custom class for directions (implementation)
4  \author Sebastien Le Digabel and Viviane Rochon Montplaisir
5  \date March 2017
6  \see Direction.hpp
7  */
8 #include "../Math/Direction.hpp"
9 
10 // Assignment operator
11 const NOMAD::Direction& NOMAD::Direction::operator=(const NOMAD::Direction& dir)
12 {
13  NOMAD::ArrayOfDouble::operator=(dir);
14  return *this;
15 }
16 
17 /*---------------------------------------------------*/
18 /* Squared Norm of direction V, viewed as a vector. */
19 /*---------------------------------------------------*/
20 const NOMAD::Double NOMAD::Direction::squaredNorm() const
21 {
22  // VRM - To generalize to other norms.
23  NOMAD::Double squaredNorm = 0;
24 
25  for (size_t i = 0; i < size(); i++)
26  {
27  squaredNorm += _array[i] * _array[i];
28  }
29 
30  return squaredNorm;
31 }
32 
33 /*---------------------------------------------------*/
34 /* Infinite Norm of direction V, viewed as a vector. */
35 /*---------------------------------------------------*/
36 const NOMAD::Double NOMAD::Direction::infiniteNorm() const
37 {
38  // VRM - To generalize to other norms.
39  NOMAD::Double infiniteNorm = 0;
40 
41  for (size_t i = 0; i < size(); i++)
42  {
43  infiniteNorm = NOMAD::max(infiniteNorm, _array[i].abs());
44  }
45 
46  return infiniteNorm;
47 }
48 
49 
50 /*-------------------------------------------*/
51 /* Norm of direction X, viewed as a vector. */
52 /* Using norm L2. */
53 /*-------------------------------------------*/
54 const NOMAD::Double NOMAD::Direction::norm() const
55 {
56  // VRM TODO Generalize to other norms.
57  NOMAD::Double dnorm = this->squaredNorm();
58 
59  dnorm = sqrt(dnorm.todouble());
60 
61  return dnorm;
62 }
63 
64 
65 const NOMAD::Double NOMAD::Direction::dotProduct(const NOMAD::Direction& dir1,
66  const NOMAD::Direction& dir2)
67 {
68  NOMAD::Double dot = 0.0;
69 
70  size_t size = dir1.size();
71  if (size != dir2.size())
72  {
73  std::string err = "Dot product: vectors are not of the same size: \n";
74  err += dir1.display() + "\n";
75  err += dir2.display();
76  throw NOMAD::Exception(__FILE__, __LINE__, err);
77  }
78 
79  for (size_t i = 0; i < size; i++)
80  {
81  dot += dir1[i] * dir2[i];
82  }
83 
84  return dot;
85 }
86 
87 
88 const NOMAD::Double NOMAD::Direction::cos(const NOMAD::Direction& dir1,
89  const NOMAD::Direction& dir2)
90 {
91  NOMAD::Double cos = 0.0;
92 
93  NOMAD::Double norm1 = dir1.norm();
94  NOMAD::Double norm2 = dir2.norm();
95  if (0.0 == norm1 || 0.0 == norm2)
96  {
97  std::string err = "Cosine: a vector is of size 0";
98  throw NOMAD::Exception(__FILE__, __LINE__, err);
99  }
100 
101  cos = dotProduct(dir1, dir2) / (norm1 * norm2);
102 
103  return cos;
104 }
105 
106 std::ostream& NOMAD::operator<< (std::ostream& out, const NOMAD::Direction& dir)
107 {
108  out << dir.display();
109  return out;
110 }
std::ostream & operator<<(std::ostream &os, const Algorithm &algo)
Operator to write parameters used for hot restart.
Double max(const Double d1, const Double d2)
Largest of two values &gt;=.
Definition: Double.hpp:583