5 Vector::Vector () : m_components ()
9 Vector::Vector (const unsigned int dimension
) : m_components (dimension
, 0)
13 Vector::Vector (std::vector
<double> components
) : m_components (components
)
17 Vector::Vector (const unsigned int dimension
, STANDARD_BASIS aVector
, const unsigned int i
) : m_components (dimension
, 0)
19 if (i
> dimension
or i
== 0) {
20 throw logic_error ("Vector constructor : cannot instantiate vector !");
24 m_components
.at(i
-1) = 1;
29 Vector::Vector (const Vector
&otherVector
)
31 m_components
= otherVector
.m_components
;
34 //===================================================================
36 unsigned int Vector::dimension () const
38 return m_components
.size();
41 //===================================================================
44 void Vector::operator = (const Vector
&otherVector
)
46 m_components
= otherVector
.m_components
;
49 //Arithmetic operators +, +=
50 Vector
Vector::operator + (const Vector
&otherVector
) const
52 return ( Vector(*this) += otherVector
);
55 Vector
& Vector::operator += (const Vector
&otherVector
)
57 const unsigned int SIZE (m_components
.size());
59 if (SIZE
!= otherVector
.m_components
.size()) {
60 throw logic_error ("Vectors do not have the same dimension !");
63 for (unsigned int i(0) ; i
< SIZE
; ++i
) {
64 m_components
.at(i
) += otherVector
.m_components
.at(i
);
70 //Arithmetic operators -, -=
71 Vector
Vector::operator - (const Vector
&otherVector
) const
73 return ( Vector(*this) -= otherVector
);
76 Vector
& Vector::operator -= (const Vector
&otherVector
)
78 const unsigned int SIZE (m_components
.size());
80 if (SIZE
!= otherVector
.m_components
.size()) {
81 throw logic_error ("Vectors do not have the same dimension !");
84 for (unsigned int i(0) ; i
< SIZE
; ++i
) {
85 m_components
.at(i
) -= otherVector
.m_components
.at(i
);
91 //Multiplication by a scalar operators *, *=
92 Vector
Vector::operator * (const double scalar
) const
94 return ( Vector(*this) *= scalar
);
97 Vector
& Vector::operator *= (const double scalar
)
99 const unsigned int SIZE (m_components
.size());
101 for (unsigned int i(0) ; i
< SIZE
; ++i
) {
102 m_components
.at(i
) *= scalar
;
107 //Inner product operator
108 double Vector::operator * (const Vector
&otherVector
) const
110 return (this->dotProduct(otherVector
));
113 Vector
Vector::operator / (const double scalar
) const
115 return ( Vector(*this) /= scalar
);
118 Vector
& Vector::operator /= (const double scalar
)
121 throw domain_error ("Vector::operator /= : Division by zero occured !");
124 const unsigned int SIZE (m_components
.size());
126 for (unsigned int i(0) ; i
< SIZE
; ++i
) {
127 m_components
.at(i
) /= scalar
;
132 /*Cross product operator : /!\ only works for vector in dimension 3 /!\*/
133 Vector
Vector::operator ^ (const Vector
&otherVector
) const
135 if ( m_components
.size() != 3 ) {
136 throw logic_error ("Cross product only defined in dimension 3");
138 else if ( otherVector
.dimension() != 3 ) {
139 throw logic_error ("Cross product only defined in dimension 3");
144 result
.addComponent ( (m_components
.at(1) * otherVector
.m_components
.at(2)) - ((m_components
.at(2) * otherVector
.m_components
.at(1))) );
145 result
.addComponent ( (m_components
.at(2) * otherVector
.m_components
.at(0)) - ((m_components
.at(0) * otherVector
.m_components
.at(2))) );
146 result
.addComponent ( (m_components
.at(0) * otherVector
.m_components
.at(1)) - ((m_components
.at(1) * otherVector
.m_components
.at(0))) );
151 //Equality operators ==, !=
152 bool Vector::operator == (const Vector
&otherVector
) const
154 return (m_components
== otherVector
.m_components
);
157 bool Vector::operator != (const Vector
&otherVector
) const
159 return not(m_components
== otherVector
.m_components
);
162 /*Index operators [] read-only
163 /!\ index is understood as a mathematical index, not as a general C++ index /!\
164 See method 'at()' for the C++ like getter
166 double Vector::operator [] (const double index
) const
168 if (index
> m_components
.size() or index
== 0) {
169 throw out_of_range ("Vector::operator [] : Index is out of range !");
171 return ( m_components
.at(index
-1) );
174 /*Index operators [] read-only
175 /!\ index is understood as a mathematical index, not as a general C++ index /!\
176 See method 'at()' for the C++ like getter
178 double& Vector::operator [] (const double index
)
180 if (index
> m_components
.size() or index
== 0) {
181 throw out_of_range ("Vector::operator [] : Index is out of range !");
183 return ( m_components
.at(index
-1) );
186 //===================================================================
188 /*Can throw out_of_range exception
190 double Vector::at (const unsigned int index
) const
192 if (index
>= m_components
.size()) {
193 throw out_of_range ("Vector::at () : Index is out of range !");
195 return ( m_components
.at(index
) );
198 double& Vector::at (const unsigned int index
)
200 if (index
>= m_components
.size() ) {
201 throw out_of_range ("Vector::at() : Index is out of range !");
203 return ( m_components
.at(index
) );
206 double Vector::dotProduct (const Vector
&otherVector
) const
210 const unsigned int SIZE(m_components
.size());
212 if ( SIZE
== otherVector
.components().size()) {
213 for (unsigned int i(0) ; i
< SIZE
; ++i
) {
214 result
+= ( m_components
.at(i
) * otherVector
.components().at(i
) );
218 throw logic_error ("Vectors do not have the same dimension !");
224 //===================================================================
226 void Vector::addComponent (double value
)
228 m_components
.push_back (value
);
231 double Vector::magnitude () const
233 return sqrt ( dotProduct(*this) );
236 double Vector::magnitude2 () const
238 return ( dotProduct(*this) );
241 void Vector::show () const
243 const unsigned int SIZE(m_components
.size());
245 for (unsigned int i(0) ; i
< SIZE
; ++i
) {
246 cout
<< m_components
.at(i
) << endl
;