TBTK
Need a break? Support the development by playing Polarity Puzzles
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  const Vector2d operator+(const Vector2d &rhs) const;
52 
54  const Vector2d operator-(const Vector2d &rhs) const;
55 
57  const Vector2d operator-() const;
58 
60  const Vector2d operator*(double rhs) const;
61 
63  friend const Vector2d operator*(double lhs, const Vector2d &rhs);
64 
66  const Vector2d operator/(double rhs) const;
67 
70  const Vector2d unit() const;
71 
74  const Vector2d perpendicular(const Vector2d &v) const;
75 
78  const Vector2d parallel(const Vector2d &v) const;
79 
81  double norm() const;
82 
84  static double dotProduct(const Vector2d &lhs, const Vector2d &rhs);
85 
87  const std::vector<double> getStdVector() const;
88 
90  friend std::ostream& operator<<(std::ostream &stream, const Vector2d &v);
91 };
92 
93 inline const Vector2d Vector2d::operator+(const Vector2d &rhs) const{
94  Vector2d result;
95 
96  result.x = x + rhs.x;
97  result.y = y + rhs.y;
98 
99  return result;
100 }
101 
102 inline const Vector2d Vector2d::operator-(const Vector2d &rhs) const{
103  Vector2d result;
104 
105  result.x = x - rhs.x;
106  result.y = y - rhs.y;
107 
108  return result;
109 }
110 
111 inline const Vector2d Vector2d::operator-() const{
112  Vector2d result;
113 
114  result.x = -x;
115  result.y = -y;
116 
117  return result;
118 }
119 
120 inline const Vector2d Vector2d::operator*(double rhs) const{
121  Vector2d result;
122 
123  result.x = x*rhs;
124  result.y = y*rhs;
125 
126  return result;
127 }
128 
129 inline const Vector2d operator*(double lhs, const Vector2d &rhs){
130  Vector2d result;
131 
132  result.x = lhs*rhs.x;
133  result.y = lhs*rhs.y;
134 
135  return result;
136 }
137 
138 inline const Vector2d Vector2d::operator/(double rhs) const{
139  Vector2d result;
140 
141  result.x = x/rhs;
142  result.y = y/rhs;
143 
144  return result;
145 }
146 
147 inline const Vector2d Vector2d::unit() const{
148  return (*this)/norm();
149 }
150 
151 inline const Vector2d Vector2d::perpendicular(const Vector2d &v) const{
152  return *this - dotProduct(*this, v.unit())*v.unit();
153 }
154 
155 inline const Vector2d Vector2d::parallel(const Vector2d &v) const{
156  return dotProduct(*this, v.unit())*v.unit();
157 }
158 
159 inline double Vector2d::norm() const{
160  return sqrt(x*x + y*y);
161 }
162 
163 inline double Vector2d::dotProduct(const Vector2d &lhs, const Vector2d &rhs){
164  return lhs.x*rhs.x + lhs.y*rhs.y;
165 }
166 
167 inline const std::vector<double> Vector2d::getStdVector() const{
168  std::vector<double> result;
169 
170  result.push_back(x);
171  result.push_back(y);
172 
173  return result;
174 }
175 
176 inline std::ostream& operator<<(std::ostream &stream, const Vector2d &v){
177  stream << "(" << v.x << ", " << v.y << ")";
178 
179  return stream;
180 }
181 
182 }; //End namespace TBTK
183 
184 #endif
TBTK::Vector2d::getStdVector
const std::vector< double > getStdVector() const
Definition: Vector2d.h:167
TBTK::Vector2d::operator+
const Vector2d operator+(const Vector2d &rhs) const
Definition: Vector2d.h:93
TBTK::Vector2d
Definition: Vector2d.h:33
TBTK::Vector2d::parallel
const Vector2d parallel(const Vector2d &v) const
Definition: Vector2d.h:155
TBTK::Vector2d::dotProduct
static double dotProduct(const Vector2d &lhs, const Vector2d &rhs)
Definition: Vector2d.h:163
TBTK::Vector2d::operator<<
friend std::ostream & operator<<(std::ostream &stream, const Vector2d &v)
Definition: Vector2d.h:176
TBTK::Vector2d::operator/
const Vector2d operator/(double rhs) const
Definition: Vector2d.h:138
TBTK::Vector2d::y
double y
Definition: Vector2d.h:39
TBTK::Vector2d::operator*
const Vector2d operator*(double rhs) const
Definition: Vector2d.h:120
TBTK::Vector2d::unit
const Vector2d unit() const
Definition: Vector2d.h:147
TBTK::Vector2d::perpendicular
const Vector2d perpendicular(const Vector2d &v) const
Definition: Vector2d.h:151
TBTK::Vector2d::Vector2d
Vector2d()
TBTK::Vector2d::operator-
const Vector2d operator-() const
Definition: Vector2d.h:111
TBTK::operator<<
std::ostream & operator<<(std::ostream &stream, const HoppingAmplitude &hoppingAmplitude)
Definition: HoppingAmplitude.h:315
TBTK::operator*
const Vector2d operator*(double lhs, const Vector2d &rhs)
Definition: Vector2d.h:129
TBTK::Vector2d::norm
double norm() const
Definition: Vector2d.h:159
TBTK::Vector2d::x
double x
Definition: Vector2d.h:36