TBTK
VectorNd.h
Go to the documentation of this file.
1 /* Copyright 2017 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_ND
24 #define COM_DAFER45_TBTK_VECTOR_ND
25 
26 #include <cmath>
27 #include <initializer_list>
28 #include <ostream>
29 #include <vector>
30 
31 namespace TBTK{
32 
33 class VectorNd{
34 public:
36  VectorNd(unsigned int size);
37 
39  VectorNd(const VectorNd &vectorNd);
40 
42  VectorNd(VectorNd &&vectorNd);
43 
45  VectorNd(std::initializer_list<double> components);
46 
48  VectorNd(const std::vector<double> &components);
49 
51  ~VectorNd();
52 
54  VectorNd& operator=(const VectorNd &vectorNd);
55 
57  VectorNd& operator=(VectorNd &&vectorNd);
58 
60  double& operator[](unsigned int n);
61 
63  const double& operator[](unsigned int n) const;
64 
66  const VectorNd operator+(const VectorNd &rhs) const;
67 
69  const VectorNd operator-(const VectorNd &rhs) const;
70 
72  const VectorNd operator-() const;
73 
75  const VectorNd operator*(double rhs) const;
76 
78  friend const VectorNd operator*(double lhs, const VectorNd &rhs);
79 
81  const VectorNd operator/(double rhs) const;
82 
85  const VectorNd unit() const;
86 
89  const VectorNd parallel(const VectorNd &v) const;
90 
92  double norm() const;
93 
95  static double dotProduct(const VectorNd &lhs, const VectorNd &rhs);
96 
98  const std::vector<double> getStdVector() const;
99 
101  friend std::ostream& operator<<(std::ostream &stream, const VectorNd &v);
102 private:
104  unsigned int size;
105 
107  double *data;
108 };
109 
110 inline double& VectorNd::operator[](unsigned int n){
111  return data[n];
112 }
113 
114 inline const double& VectorNd::operator[](unsigned int n) const{
115  return data[n];
116 }
117 
118 inline const VectorNd VectorNd::operator+(const VectorNd &rhs) const{
119  TBTKAssert(
120  size == rhs.size,
121  "VectorNd::operator+()",
122  "Incompatible dimensions. Left hand side has " << size
123  << " components, while the right hand side has " << rhs.size
124  << " components.",
125  ""
126  );
127 
128  VectorNd result(size);
129  for(unsigned int n = 0; n < size; n++)
130  result.data[n] = data[n] + rhs.data[n];
131 
132  return result;
133 }
134 
135 inline const VectorNd VectorNd::operator-(const VectorNd &rhs) const{
136  TBTKAssert(
137  size == rhs.size,
138  "VectorNd::operator-()",
139  "Incompatible dimensions. Left hand side has " << size
140  << " components, while the right hand side has " << rhs.size
141  << " components.",
142  ""
143  );
144 
145  VectorNd result(size);
146  for(unsigned int n = 0; n < size; n++)
147  result.data[n] = data[n] - rhs.data[n];
148 
149  return result;
150 }
151 
152 inline const VectorNd VectorNd::operator-() const{
153  VectorNd result(size);
154  for(unsigned int n = 0; n < size; n++)
155  result.data[n] = -data[n];
156 
157  return result;
158 }
159 
160 inline const VectorNd VectorNd::operator*(double rhs) const{
161  VectorNd result(size);
162  for(unsigned int n = 0; n < size; n++)
163  result.data[n] = data[n]*rhs;
164 
165  return result;
166 }
167 
168 inline const VectorNd operator*(double lhs, const VectorNd &rhs){
169  VectorNd result(rhs.size);
170  for(unsigned int n = 0; n < rhs.size; n++)
171  result.data[n] = lhs*rhs.data[n];
172 
173  return result;
174 }
175 
176 inline const VectorNd VectorNd::operator/(double rhs) const{
177  VectorNd result(size);
178  for(unsigned int n = 0; n < size; n++)
179  result.data[n] = data[n]/rhs;
180 
181  return result;
182 }
183 
184 inline const VectorNd VectorNd::unit() const{
185  return (*this)/norm();
186 }
187 
188 inline const VectorNd VectorNd::parallel(const VectorNd &v) const{
189  TBTKAssert(
190  size == v.size,
191  "VectorNd::parallel()",
192  "Incompatible dimensions.",
193  ""
194  );
195 
196  return dotProduct(*this, v.unit())*v.unit();
197 }
198 
199 inline double VectorNd::norm() const{
200  return sqrt(dotProduct(*this, *this));
201 }
202 
203 inline double VectorNd::dotProduct(const VectorNd &lhs, const VectorNd &rhs){
204  TBTKAssert(
205  lhs.size == rhs.size,
206  "VectorNd::dotProduct()",
207  "Incompatible dimensions. Left hand side has " << lhs.size
208  << " components, while the right hand side has " << rhs.size
209  << " components.",
210  ""
211  );
212 
213  double dp = 0;
214  for(unsigned int n = 0; n < lhs.size; n++)
215  dp += lhs.data[n]*rhs.data[n];
216 
217  return dp;
218 }
219 
220 inline const std::vector<double> VectorNd::getStdVector() const{
221  std::vector<double> result;
222  for(unsigned int n = 0; n < size; n++)
223  result.push_back(data[n]);
224 
225  return result;
226 }
227 
228 inline std::ostream& operator<<(std::ostream &stream, const VectorNd &v){
229  stream << "(";
230  for(unsigned int n = 0; n < v.size; n++){
231  if(n != 0)
232  stream << ", ";
233  stream << v.data[n];
234  }
235 
236  return stream;
237 }
238 
239 }; //End namespace TBTK
240 
241 #endif
VectorNd(unsigned int size)
static double dotProduct(const VectorNd &lhs, const VectorNd &rhs)
Definition: VectorNd.h:203
VectorNd & operator=(const VectorNd &vectorNd)
const VectorNd parallel(const VectorNd &v) const
Definition: VectorNd.h:188
const VectorNd operator+(const VectorNd &rhs) const
Definition: VectorNd.h:118
Definition: VectorNd.h:33
const VectorNd unit() const
Definition: VectorNd.h:184
double & operator[](unsigned int n)
Definition: VectorNd.h:110
Definition: ModelFactory.h:35
const VectorNd operator-() const
Definition: VectorNd.h:152
const VectorNd operator/(double rhs) const
Definition: VectorNd.h:176
double norm() const
Definition: VectorNd.h:199
friend std::ostream & operator<<(std::ostream &stream, const VectorNd &v)
Definition: VectorNd.h:228
const std::vector< double > getStdVector() const
Definition: VectorNd.h:220
const VectorNd operator*(double rhs) const
Definition: VectorNd.h:160