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 //===================================================================
37 void Vector::operator = (const Vector
&otherVector
)
42 //Arithmetic operators +, +=
43 Vector
Vector::operator + (const Vector
&otherVector
) const
48 Vector
& Vector::operator += (const Vector
&otherVector
)
53 //Arithmetic operators -, -=
54 Vector
Vector::operator - (const Vector
&otherVector
) const
59 Vector
& Vector::operator -= (const Vector
&otherVector
)
64 //Multiplication by a scalar operators *, *=
65 Vector
Vector::operator * (const double scalar
) const
70 Vector
& Vector::operator *= (const double scalar
)
75 //Inner product operator
76 double Vector::operator * (const Vector
&otherVector
) const
81 Vector
Vector::operator / (const double scalar
) const
86 Vector
Vector::operator /= (const double scalar
)
91 /*Cross product operator : /!\ only works for vector in dimension 3 /!\*/
92 Vector
Vector::operator ^ (const Vector
&otherVector
) const
94 if ( m_components
.size() != 3 ) {
95 throw logic_error ("Cross product only defined in dimension 3");
97 else if ( otherVector
.components().size() != 3 ) {
98 throw logic_error ("Cross product only defined in dimension 3");
103 result
.addComponent ( (m_components
.at(1) * otherVector
.components().at(2)) - ((m_components
.at(2) * otherVector
.components().at(1))) );
104 result
.addComponent ( (m_components
.at(2) * otherVector
.components().at(0)) - ((m_components
.at(0) * otherVector
.components().at(2))) );
105 result
.addComponent ( (m_components
.at(0) * otherVector
.components().at(1)) - ((m_components
.at(1) * otherVector
.components().at(0))) );
110 //Equality operators ==, !=
111 Vector
Vector::operator == (const Vector
&otherVector
) const
116 Vector
Vector::operator != (const Vector
&otherVector
) const
121 /*Index operators [] read-only
122 /!\ index is understood as a mathematical index, not as a general C++ index /!\
123 See method 'at()' for the C++ like getter
125 double Vector::operator [] (const double index
) const
130 /*Index operators [] read-only
131 /!\ index is understood as a mathematical index, not as a general C++ index /!\
132 See method 'at()' for the C++ like getter
134 double& Vector::operator [] (const double index
)
139 //===================================================================
141 /*Can throw out_of_range exception
143 double Vector::at (const unsigned int index
) const
148 double& Vector::at (const unsigned int index
)
153 //===================================================================
155 void Vector::addComponent (double value
)
157 m_components
.push_back (value
);
160 void Vector::show () const
162 const unsigned int SIZE(m_components
.size());
164 for (unsigned int i(0) ; i
< SIZE
; ++i
) {
165 cout
<< m_components
.at(i
) << endl
;
170 bool Vector::equals (const Vector
&otherVector
) const
172 return ( m_components
== otherVector
.components() );
175 double Vector::dotProduct (const Vector
&otherVector
) const
179 const unsigned int SIZE(m_components
.size());
181 if ( SIZE
== otherVector
.components().size() ) {
182 for (unsigned int i(0) ; i
< SIZE
; ++i
) {
183 result
+= ( m_components
.at(i
) * otherVector
.components().at(i
) );
187 throw logic_error ("Vectors do not have the same dimension !");
193 double Vector::magnitude () const
195 return sqrt ( magnitude2() );
198 double Vector::magnitude2 () const
200 return ( dotProduct(*this) );
203 unsigned int Vector::dimension () const
205 return m_components
.size();