TBTK
Vector2d.h
Go to the documentation of this file.
1 /* Copyright 2018 Kristofer Bj√∂rnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
23 #ifndef COM_DAFER45_TBTK_VECTOR_2D
24 #define COM_DAFER45_TBTK_VECTOR_2D
25 
26 #include <cmath>
27 #include <initializer_list>
28 #include <ostream>
29 #include <vector>
30 
31 namespace TBTK{
32 
33 class Vector2d{
34 public:
36  double x;
37 
39  double y;
40 
42  Vector2d();
43 
45  Vector2d(std::initializer_list<double> components);
46 
48  Vector2d(const std::vector<double> &components);
49 
51  ~Vector2d();
52 
54  const Vector2d operator+(const Vector2d &rhs) const;
55 
57  const Vector2d operator-(const Vector2d &rhs) const;
58 
60  const Vector2d operator-() const;
61 
63  const Vector2d operator*(double rhs) const;
64 
66  friend const Vector2d operator*(double lhs, const Vector2d &rhs);
67 
69  const Vector2d operator/(double rhs) const;
70 
73  const Vector2d unit() const;
74 
77  const Vector2d perpendicular(const Vector2d &v) const;
78 
81  const Vector2d parallel(const Vector2d &v) const;
82 
84  double norm() const;
85 
87  static double dotProduct(const Vector2d &lhs, const Vector2d &rhs);
88 
90  const std::vector<double> getStdVector() const;
91 
93  friend std::ostream& operator<<(std::ostream &stream, const Vector2d &v);
94 };
95 
96 inline const Vector2d Vector2d::operator+(const Vector2d &rhs) const{
97  Vector2d result;
98 
99  result.x = x + rhs.x;
100  result.y = y + rhs.y;
101 
102  return result;
103 }
104 
105 inline const Vector2d Vector2d::operator-(const Vector2d &rhs) const{
106  Vector2d result;
107 
108  result.x = x - rhs.x;
109  result.y = y - rhs.y;
110 
111  return result;
112 }
113 
114 inline const Vector2d Vector2d::operator-() const{
115  Vector2d result;
116 
117  result.x = -x;
118  result.y = -y;
119 
120  return result;
121 }
122 
123 inline const Vector2d Vector2d::operator*(double rhs) const{
124  Vector2d result;
125 
126  result.x = x*rhs;
127  result.y = y*rhs;
128 
129  return result;
130 }
131 
132 inline const Vector2d operator*(double lhs, const Vector2d &rhs){
133  Vector2d result;
134 
135  result.x = lhs*rhs.x;
136  result.y = lhs*rhs.y;
137 
138  return result;
139 }
140 
141 inline const Vector2d Vector2d::operator/(double rhs) const{
142  Vector2d result;
143 
144  result.x = x/rhs;
145  result.y = y/rhs;
146 
147  return result;
148 }
149 
150 inline const Vector2d Vector2d::unit() const{
151  return (*this)/norm();
152 }
153 
154 inline const Vector2d Vector2d::perpendicular(const Vector2d &v) const{
155  return *this - dotProduct(*this, v.unit())*v.unit();
156 }
157 
158 inline const Vector2d Vector2d::parallel(const Vector2d &v) const{
159  return dotProduct(*this, v.unit())*v.unit();
160 }
161 
162 inline double Vector2d::norm() const{
163  return sqrt(x*x + y*y);
164 }
165 
166 inline double Vector2d::dotProduct(const Vector2d &lhs, const Vector2d &rhs){
167  return lhs.x*rhs.x + lhs.y*rhs.y;
168 }
169 
170 inline const std::vector<double> Vector2d::getStdVector() const{
171  std::vector<double> result;
172 
173  result.push_back(x);
174  result.push_back(y);
175 
176  return result;
177 }
178 
179 inline std::ostream& operator<<(std::ostream &stream, const Vector2d &v){
180  stream << "(" << v.x << ", " << v.y << ")";
181 
182  return stream;
183 }
184 
185 }; //End namespace TBTK
186 
187 #endif
double y
Definition: Vector2d.h:39
const std::vector< double > getStdVector() const
Definition: Vector2d.h:170
Definition: Vector2d.h:33
const Vector2d operator+(const Vector2d &rhs) const
Definition: Vector2d.h:96
double norm() const
Definition: Vector2d.h:162
const Vector2d perpendicular(const Vector2d &v) const
Definition: Vector2d.h:154
const Vector2d operator-() const
Definition: Vector2d.h:114
double x
Definition: Vector2d.h:36
const Vector2d operator*(double rhs) const
Definition: Vector2d.h:123
const Vector2d unit() const
Definition: Vector2d.h:150
static double dotProduct(const Vector2d &lhs, const Vector2d &rhs)
Definition: Vector2d.h:166
Definition: ModelFactory.h:35
friend std::ostream & operator<<(std::ostream &stream, const Vector2d &v)
Definition: Vector2d.h:179
const Vector2d operator/(double rhs) const
Definition: Vector2d.h:141
const Vector2d parallel(const Vector2d &v) const
Definition: Vector2d.h:158