classe 'Vector' : ajout de deux constructeurs et surcharge de l'operateur 'ostream...
[ProjetInfo.git] / math / src / vector.cc
blob543db0268017775184f9a2096498d967d491ec86
1 #include "vector.h"
3 using namespace std;
5 Vector::Vector () : m_components ()
9 Vector::Vector (const unsigned int dimension) : m_components (dimension, 0)
13 Vector::Vector (const double cmp1, const double cmp2, const double cmp3) : m_components(3)
15 m_components.at(0) = cmp1;
16 m_components.at(1) = cmp2;
17 m_components.at(2) = cmp3;
20 Vector::Vector (double components[], const unsigned int size) : m_components (size)
22 for (unsigned int i(0) ; i < size ; ++i) {
23 m_components.at(i) = components[i];
27 Vector::Vector (std::vector<double> components) : m_components (components)
31 Vector::Vector (const unsigned int dimension, STANDARD_BASIS aVector, const unsigned int i) : m_components (dimension, 0)
33 if (i > dimension or i == 0) {
34 throw logic_error ("Vector constructor : cannot instantiate vector !");
36 switch (aVector) {
37 case ei:
38 m_components.at(i-1) = 1;
39 break;
43 Vector::Vector (const Vector &otherVector) : m_components (otherVector.m_components)
47 //===================================================================
49 unsigned int Vector::dimension () const
51 return m_components.size();
54 //===================================================================
56 //Operator =
57 void Vector::operator = (const Vector &otherVector)
59 m_components = otherVector.m_components;
62 //Arithmetic operators +, +=
63 Vector Vector::operator + (const Vector &otherVector) const
65 return ( Vector(*this) += otherVector );
68 Vector& Vector::operator += (const Vector &otherVector)
70 const unsigned int SIZE (m_components.size());
72 if (SIZE != otherVector.m_components.size()) {
73 throw logic_error ("Vectors do not have the same dimension !");
76 for (unsigned int i(0) ; i < SIZE ; ++i) {
77 m_components.at(i) += otherVector.m_components.at(i);
80 return (*this);
83 //Arithmetic operators -, -=
84 Vector Vector::operator - (const Vector &otherVector) const
86 return ( Vector(*this) -= otherVector );
89 Vector& Vector::operator -= (const Vector &otherVector)
91 const unsigned int SIZE (m_components.size());
93 if (SIZE != otherVector.m_components.size()) {
94 throw logic_error ("Vectors do not have the same dimension !");
97 for (unsigned int i(0) ; i < SIZE ; ++i) {
98 m_components.at(i) -= otherVector.m_components.at(i);
101 return (*this);
104 //Multiplication by a scalar operators *, *=
105 Vector Vector::operator * (const double scalar) const
107 return ( Vector(*this) *= scalar );
110 Vector& Vector::operator *= (const double scalar)
112 const unsigned int SIZE (m_components.size());
114 for (unsigned int i(0) ; i < SIZE ; ++i) {
115 m_components.at(i) *= scalar;
117 return *this;
120 //Inner product operator
121 double Vector::operator * (const Vector &otherVector) const
123 return (this->dotProduct(otherVector));
126 Vector Vector::operator / (const double scalar) const
128 return ( Vector(*this) /= scalar );
131 Vector& Vector::operator /= (const double scalar)
133 if (scalar == 0) {
134 throw domain_error ("Vector::operator /= : Division by zero occured !");
137 const unsigned int SIZE (m_components.size());
139 for (unsigned int i(0) ; i < SIZE ; ++i) {
140 m_components.at(i) /= scalar;
142 return *this;
145 /*Cross product operator : /!\ only works for vector in dimension 3 /!\*/
146 Vector Vector::operator ^ (const Vector &otherVector) const
148 if ( m_components.size() != 3 ) {
149 throw logic_error ("Cross product only defined in dimension 3");
151 else if ( otherVector.dimension() != 3 ) {
152 throw logic_error ("Cross product only defined in dimension 3");
155 Vector result;
157 result.addComponent ( (m_components.at(1) * otherVector.m_components.at(2)) - ((m_components.at(2) * otherVector.m_components.at(1))) );
158 result.addComponent ( (m_components.at(2) * otherVector.m_components.at(0)) - ((m_components.at(0) * otherVector.m_components.at(2))) );
159 result.addComponent ( (m_components.at(0) * otherVector.m_components.at(1)) - ((m_components.at(1) * otherVector.m_components.at(0))) );
160 return result;
164 //Equality operators ==, !=
165 bool Vector::operator == (const Vector &otherVector) const
167 return (m_components == otherVector.m_components);
170 bool Vector::operator != (const Vector &otherVector) const
172 return not(m_components == otherVector.m_components);
175 /*Index operators [] read-only
176 /!\ index is understood as a mathematical index, not as a general C++ index /!\
177 See method 'at()' for the C++ like getter
179 double Vector::operator [] (const double index) const
181 if (index > m_components.size() or index == 0) {
182 throw out_of_range ("Vector::operator [] : Index is out of range !");
184 return ( m_components.at(index-1) );
187 /*Index operators [] read-only
188 /!\ index is understood as a mathematical index, not as a general C++ index /!\
189 See method 'at()' for the C++ like getter
191 double& Vector::operator [] (const double index)
193 if (index > m_components.size() or index == 0) {
194 throw out_of_range ("Vector::operator [] : Index is out of range !");
196 return ( m_components.at(index-1) );
199 ostream& operator << (ostream& os, const Vector& aVector)
201 const unsigned int SIZE (aVector.dimension());
203 for (unsigned int i(0) ; i < SIZE ; ++i) {
204 os << aVector.at(i) << ' ';
206 return os;
209 //===================================================================
211 /*Can throw out_of_range exception
212 C++ like getter*/
213 double Vector::at (const unsigned int index) const
215 if (index >= m_components.size()) {
216 throw out_of_range ("Vector::at () : Index is out of range !");
218 return ( m_components.at(index) );
221 double& Vector::at (const unsigned int index)
223 if (index >= m_components.size() ) {
224 throw out_of_range ("Vector::at() : Index is out of range !");
226 return ( m_components.at(index) );
229 double Vector::dotProduct (const Vector &otherVector) const
231 double result(0);
233 const unsigned int SIZE(m_components.size());
235 if ( SIZE == otherVector.components().size()) {
236 for (unsigned int i(0) ; i < SIZE ; ++i) {
237 result += ( m_components.at(i) * otherVector.components().at(i) );
240 else {
241 throw logic_error ("Vectors do not have the same dimension !");
244 return result;
247 //===================================================================
249 void Vector::addComponent (double value)
251 m_components.push_back (value);
254 double Vector::magnitude () const
256 return sqrt ( dotProduct(*this) );
259 double Vector::magnitude2 () const
261 return ( dotProduct(*this) );
264 void Vector::show () const
266 const unsigned int SIZE(m_components.size());
268 for (unsigned int i(0) ; i < SIZE ; ++i) {
269 cout << m_components.at(i) << endl;
271 cout << endl;