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