From 6337c0ecfd24db9b20b6b0c091364438eb00adef Mon Sep 17 00:00:00 2001 From: "U-DAMIEN\\DamienN" Date: Sat, 6 Mar 2010 20:07:00 +0100 Subject: [PATCH] classe 'Vector' : definition et implementation des constructeurs. Definition des operateurs, suppression des methodes devenue caduques. --- vector.cc | 187 +++++++++++++++++++++++++++++++++++++++++++++----------------- vector.h | 50 +++++++++++++++-- 2 files changed, 180 insertions(+), 57 deletions(-) diff --git a/vector.cc b/vector.cc index c832695..a987020 100644 --- a/vector.cc +++ b/vector.cc @@ -2,73 +2,174 @@ using namespace std; -void Vector::addComponent (double value) +Vector::Vector () : m_components () { - m_components.push_back (value); } -void Vector::show () const +Vector::Vector (const unsigned int dimension) : m_components (dimension, 0) { - const unsigned int SIZE(m_components.size()); +} - for (unsigned int i(0) ; i < SIZE ; ++i) { - cout << m_components.at(i) << endl; +Vector::Vector (std::vector components) : m_components (components) +{ +} + +Vector::Vector (const unsigned int dimension, STANDARD_BASIS aVector, const unsigned int i) : m_components (dimension, 0) +{ + if (i > dimension or i == 0) { + throw logic_error ("Vector constructor : cannot instantiate vector !"); + } + switch (aVector) { + case ei: + m_components.at(i-1) = 1; + break; } - cout << endl; } -bool Vector::equals (const Vector &otherVector) const +Vector::Vector (const Vector &otherVector) { - return ( m_components == otherVector.components() ); + m_components = otherVector.m_components; } -Vector Vector::add (const Vector &otherVector) const +//=================================================================== + +//Operator = +void Vector::operator = (const Vector &otherVector) { - Vector result; - const unsigned int SIZE(m_components.size()); +} - if ( SIZE == otherVector.components().size() ) { - for (unsigned int i(0) ; i < SIZE ; ++i) { - result.addComponent ( m_components.at(i) + otherVector.components().at(i) ); - } - } - else { - throw logic_error ("Vectors do not have the same dimension !"); - } +//Arithmetic operators +, += +Vector Vector::operator + (const Vector &otherVector) const +{ - return result; } -Vector Vector::substract (const Vector &otherVector) const +Vector& Vector::operator += (const Vector &otherVector) { - Vector result; - const unsigned int SIZE(m_components.size()); +} - if ( SIZE == otherVector.components().size() ) { - for (unsigned int i(0) ; i < SIZE ; ++i) { - result.addComponent ( m_components.at(i) - otherVector.components().at(i) ); - } +//Arithmetic operators -, -= +Vector Vector::operator - (const Vector &otherVector) const +{ + +} + +Vector& Vector::operator -= (const Vector &otherVector) +{ + +} + +//Multiplication by a scalar operators *, *= +Vector Vector::operator * (const double scalar) const +{ + +} + +Vector& Vector::operator *= (const double scalar) +{ + +} + +//Inner product operator +double Vector::operator * (const Vector &otherVector) const +{ + +} + +Vector Vector::operator / (const double scalar) const +{ + +} + +Vector Vector::operator /= (const double scalar) +{ + +} + +/*Cross product operator : /!\ only works for vector in dimension 3 /!\*/ +Vector Vector::operator ^ (const Vector &otherVector) const +{ + if ( m_components.size() != 3 ) { + throw logic_error ("Cross product only defined in dimension 3"); } - else { - throw logic_error ("Vectors do not have the same dimension !"); + else if ( otherVector.components().size() != 3 ) { + throw logic_error ("Cross product only defined in dimension 3"); } + Vector result; + + result.addComponent ( (m_components.at(1) * otherVector.components().at(2)) - ((m_components.at(2) * otherVector.components().at(1))) ); + result.addComponent ( (m_components.at(2) * otherVector.components().at(0)) - ((m_components.at(0) * otherVector.components().at(2))) ); + result.addComponent ( (m_components.at(0) * otherVector.components().at(1)) - ((m_components.at(1) * otherVector.components().at(0))) ); return result; + } -Vector Vector::externalProduct (const double scalar) const +//Equality operators ==, != +Vector Vector::operator == (const Vector &otherVector) const { - Vector result; +} + +Vector Vector::operator != (const Vector &otherVector) const +{ + +} + +/*Index operators [] read-only + /!\ index is understood as a mathematical index, not as a general C++ index /!\ + See method 'at()' for the C++ like getter +*/ +double Vector::operator [] (const double index) const +{ + +} + +/*Index operators [] read-only + /!\ index is understood as a mathematical index, not as a general C++ index /!\ + See method 'at()' for the C++ like getter +*/ +double& Vector::operator [] (const double index) +{ + +} + +//=================================================================== + +/*Can throw out_of_range exception + C++ like getter*/ +double Vector::at (const unsigned int index) const +{ + +} + +double& Vector::at (const unsigned int index) +{ + +} + +//=================================================================== + +void Vector::addComponent (double value) +{ + m_components.push_back (value); +} + +void Vector::show () const +{ const unsigned int SIZE(m_components.size()); for (unsigned int i(0) ; i < SIZE ; ++i) { - result.addComponent ( m_components.at(i) * scalar ); + cout << m_components.at(i) << endl; } + cout << endl; +} - return result; +bool Vector::equals (const Vector &otherVector) const +{ + return ( m_components == otherVector.components() ); } double Vector::dotProduct (const Vector &otherVector) const @@ -89,24 +190,6 @@ double Vector::dotProduct (const Vector &otherVector) const return result; } -Vector Vector::crossProduct (const Vector &otherVector) const -{ - if ( m_components.size() != 3 ) { - throw logic_error ("Cross product only defined in dimension 3"); - } - else if ( otherVector.components().size() != 3 ) { - throw logic_error ("Cross product only defined in dimension 3"); - } - - Vector result; - - result.addComponent ( (m_components.at(1) * otherVector.components().at(2)) - ((m_components.at(2) * otherVector.components().at(1))) ); - result.addComponent ( (m_components.at(2) * otherVector.components().at(0)) - ((m_components.at(0) * otherVector.components().at(2))) ); - result.addComponent ( (m_components.at(0) * otherVector.components().at(1)) - ((m_components.at(1) * otherVector.components().at(0))) ); - - return result; -} - double Vector::magnitude () const { return sqrt ( magnitude2() ); diff --git a/vector.h b/vector.h index 6e1d516..5da9827 100644 --- a/vector.h +++ b/vector.h @@ -10,6 +10,17 @@ class Vector { public: + enum STANDARD_BASIS {ei}; //Allows instantiation of particular vectors of Rn + + //Constructors and destructors + Vector (); + Vector (const unsigned int dimension); + Vector (std::vector components); + /*Can throw logic_error exceptions + /!\ Be aware that the index 'i' is understood as a mathematical index, not a general C++ index /!\*/ + Vector (const unsigned int dimension, STANDARD_BASIS aVector, const unsigned int i); + Vector (const Vector &otherVector); + const std::vector& components () const {return m_components;} void setComponents (const std::vector &newVector) {m_components = newVector;} @@ -17,12 +28,41 @@ class Vector void show () const; bool equals (const Vector &otherVector) const; - Vector add (const Vector &otherVector) const; - Vector substract (const Vector &otherVector) const; + void operator = (const Vector &otherVector); + + //All operators can throw logic_error exceptions + + Vector operator + (const Vector &otherVector) const; + Vector& operator += (const Vector &otherVector); + + Vector operator - (const Vector &otherVector) const; + Vector& operator -= (const Vector &otherVector); + + Vector operator * (const double scalar) const; + Vector& operator *= (const double scalar); + double operator * (const Vector &otherVector) const; //Dot product of two vectors + + //Can throw domain_error exceptions + Vector operator / (const double scalar) const; + Vector operator /= (const double scalar); + + Vector operator ^ (const Vector &otherVector) const; + + Vector operator == (const Vector &otherVector) const; + Vector operator != (const Vector &otherVector) const; + + /*Can throw out_of_range exceptions + /!\ index is understood as a mathematical index, not as a general C++ index /!\ + See method 'at()' for the C++ like getter*/ + double operator [] (const double index) const; + double& operator [] (const double index); + + /*Can throw out_of_range exception + C++ like getter*/ + double at (const unsigned int index) const; + double& at (const unsigned int index); - Vector externalProduct (const double scalar) const; - double dotProduct (const Vector &otherVector) const; - Vector crossProduct (const Vector &otherVector) const; + double dotProduct (const Vector &otherVector) const; //Calculate Euclidea standard product double magnitude () const; double magnitude2 () const; -- 2.11.4.GIT