classe 'Vector' : fin de l'implementation des operateurs et methodes 'at()'.
[ProjetInfo.git] / vector.cc
blobb41ffbd453a06c3018cb8a80c518f278f659fa7f
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 (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 !");
22 switch (aVector) {
23 case ei:
24 m_components.at(i-1) = 1;
25 break;
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 //===================================================================
43 //Operator =
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);
67 return (*this);
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);
88 return (*this);
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;
104 return *this;
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)
120 if (scalar == 0) {
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;
129 return *this;
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.components().size() != 3 ) {
139 throw logic_error ("Cross product only defined in dimension 3");
142 Vector result;
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))) );
147 return result;
151 //Equality operators ==, !=
152 Vector Vector::operator == (const Vector &otherVector) const
154 return (m_components == otherVector.m_components);
157 Vector 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
189 C++ like getter*/
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
208 double result(0);
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) );
217 else {
218 throw logic_error ("Vectors do not have the same dimension !");
221 return result;
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;
248 cout << endl;