TBTK
Vector3d.h
Go to the documentation of this file.
1 /* Copyright 2016 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
24 #define COM_DAFER45_TBTK_VECTOR
25 
26 #include <initializer_list>
27 #include <vector>
28 #include <math.h>
29 
30 namespace TBTK{
31 
33 class Vector3d{
34 public:
36  double x;
37 
39  double y;
40 
42  double z;
43 
45  Vector3d();
46 
48  Vector3d(std::initializer_list<double> components);
49 
51  Vector3d(const std::vector<double> &components);
52 
54  ~Vector3d();
55 
57  const Vector3d operator+(const Vector3d &rhs) const;
58 
60  const Vector3d operator-(const Vector3d &rhs) const;
61 
63  const Vector3d operator*(const Vector3d &rhs) const;
64 
66  const Vector3d operator*(double rhs);
67 
69  friend const Vector3d operator*(double lhs, const Vector3d &rhs);
70 
72  const Vector3d operator/(double rhs) const;
73 
75  double norm() const;
76 
78  static double dotProduct(const Vector3d &lhs, const Vector3d &rhs);
79 
81  const std::vector<double> getStdVector() const;
82 };
83 
84 inline const Vector3d Vector3d::operator+(const Vector3d &rhs) const{
85  Vector3d result;
86 
87  result.x = x + rhs.x;
88  result.y = y + rhs.y;
89  result.z = z + rhs.z;
90 
91  return result;
92 }
93 
94 inline const Vector3d Vector3d::operator-(const Vector3d &rhs) const{
95  Vector3d result;
96 
97  result.x = x - rhs.x;
98  result.y = y - rhs.y;
99  result.z = z - rhs.z;
100 
101  return result;
102 }
103 
104 inline const Vector3d Vector3d::operator*(const Vector3d &rhs) const{
105  Vector3d result;
106 
107  result.x = y*rhs.z - z*rhs.y;
108  result.y = z*rhs.x - x*rhs.z;
109  result.z = x*rhs.y - y*rhs.x;
110 
111  return result;
112 }
113 
114 inline const Vector3d Vector3d::operator*(double rhs){
115  Vector3d result;
116 
117  result.x = x*rhs;
118  result.y = y*rhs;
119  result.z = z*rhs;
120 
121  return result;
122 }
123 
124 inline const Vector3d operator*(double lhs, Vector3d &rhs){
125  Vector3d result;
126 
127  result.x = lhs*rhs.x;
128  result.y = lhs*rhs.y;
129  result.z = lhs*rhs.z;
130 
131  return result;
132 }
133 
134 inline const Vector3d Vector3d::operator/(double rhs) const{
135  Vector3d result;
136 
137  result.x = x/rhs;
138  result.y = y/rhs;
139  result.z = z/rhs;
140 
141  return result;
142 }
143 
144 inline double Vector3d::norm() const{
145  return sqrt(x*x + y*y + z*z);
146 }
147 
148 inline double Vector3d::dotProduct(const Vector3d &lhs, const Vector3d &rhs){
149  return lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z;
150 }
151 
152 inline const std::vector<double> Vector3d::getStdVector() const{
153  std::vector<double> result;
154 
155  result.push_back(x);
156  result.push_back(y);
157  result.push_back(z);
158 
159  return result;
160 }
161 
162 }; //End namespace TBTK
163 
164 #endif
Vector3d()
Definition: Vector3d.cpp:28
double norm() const
Definition: Vector3d.h:144
const Vector3d operator-(const Vector3d &rhs) const
Definition: Vector3d.h:94
double z
Definition: Vector3d.h:42
const Vector3d operator*(const Vector3d &rhs) const
Definition: Vector3d.h:104
double y
Definition: Vector3d.h:39
Definition: Vector3d.h:33
Definition: AbstractOperator.h:26
~Vector3d()
Definition: Vector3d.cpp:59
double x
Definition: Vector3d.h:36
const std::vector< double > getStdVector() const
Definition: Vector3d.h:152
static double dotProduct(const Vector3d &lhs, const Vector3d &rhs)
Definition: Vector3d.h:148
const Vector3d operator/(double rhs) const
Definition: Vector3d.h:134
const Vector3d operator+(const Vector3d &rhs) const
Definition: Vector3d.h:84