Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F100865328
vector.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, Feb 3, 10:13
Size
6 KB
Mime Type
text/x-c
Expires
Wed, Feb 5, 10:13 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
24047287
Attached To
rSYMKIT symkit
vector.cpp
View Options
#include "vector.h"
#include "iosymkit.h" // pour implementer ostream<<
#include <cmath> // pour sqrt()
#include <algorithm>
#include "svector.h"
using namespace std;
Vector::Vector() : components(DEFAULT_VECTOR_SIZE) {}
// Costructeur sur la base d'une liste de valuers
//
Vector::Vector(const initializer_list<double> &init) : components(init.begin(), init.end()) {}
// Constructeur sur la base d'un std::vector
//
Vector::Vector(const vector<double>& init) : components(init) {}
Vector::Vector(const Vector& init)
{
*this = init;
}
//Destructeur
Vector::~Vector(){}
//Permet, par un simple operator =, d'affecter un Vector à un autre
Vector& Vector::operator=(const Vector& v) {
/* clear current memory */
this->clear();
/* assure allocation size */
this->components.reserve(v.size());
/* copy all components */
for (auto comp : v)
this->components.push_back(comp);
return *this;
}
/* for auto loop */
Vector::iterator Vector::begin()
{
return components.begin();
}
Vector::iterator Vector::end()
{
return components.end();
}
Vector::const_iterator Vector::begin() const
{
return components.begin();
}
Vector::const_iterator Vector::end() const
{
return components.end();
}
Vector Vector::unit() const
{
return (*this) / this->module();
}
/*
* cette fonctions était démandée en exercice, mais l'operator "[]" permet de faire la même chose
* et en plus elle foction de operateur [] comme pour les "vector" (ex. v[i] cordonnée i de v)
* mais pour notre classe "Vector"
*
void Vector::set_coord(size_t i, double x){
if (i<components->size())
*components[i]=val;
//On vérifie si l'entrée i est plausible pour ensuite affecter la valeur à la coordonnée choisie
else
throw string("Vector: Out of bound error");
//Autrement on lance une erreur d'Out of bound
}
*/
void Vector::augmente(double x) {
components.push_back(x);
//On rajoute simplement la valeur à un nouveau paramètre, correspondant à une dimension de plus
}
void Vector::decale()
{
components.pop_back();
}
void Vector::clear(){
components.clear();
}
//Permet d'utiliser la fonction size pour une variale Vector, comme si celle si était un vector
size_t Vector::size() const {
return components.size();
}
/*Ici de suite on retrouve deux foctions "identiques"
* mais qui opèrent sur des objets constants ou non
*/
const double& Vector::operator[](size_t i) const {
return components[i];
//Retourne la valeur du vecteur à l'indice i(i commence à 0)
}
double& Vector::operator[](size_t i) {
return components[i];
//Retourne la valeur du vecteur à l'indice i(i commence à 0)
}
/*void Vector::affiche() const{
cout << "(";
for(auto a : *components){
cout << a << ", ";
}
//Par cette boucle on peut afficher un après l'autre tous les coordonnées du Vecteur
cout << ")"<< endl;
}
*
*
* Celle si à été substituée par un ostream implementé plus loins
*/
//Operateurs comparaisons
bool Vector::operator==(const Vector& v) const {
if(components.size() != v.size())
return false;
//Retourne déjà faux si les deux vecteurs n'ont pas la même dimension
for(size_t i(0); i < v.size(); ++i){
if (components[i] != v[i])
return false;
//Lors que le programme rencontre deux coordonnées différenentes respectivement pour la même dimension des deux vecteurs
//la fonction retourne false
}
return true;
}
bool Vector::operator!=(const Vector& w) const
{
return !(*this == w);
}
//Operateurs somme et produit
Vector& Vector::operator+=(const Vector& w)
{
size_t n = (this->size() <= w.size()) ? this->size() : w.size();
for (size_t i= 0; i < n; ++i)
(*this)[i] += w[i];
return *this;
}
Vector& Vector::operator-=(const Vector& w)
{
size_t n = (this->size() <= w.size()) ? this->size() : w.size();
for (size_t i = 0; i < n; ++i)
(*this)[i] -= w[i];
return *this;
}
Vector& Vector::operator*=(double k)
{
for (size_t i = 0; i < this->size(); ++i)
(*this)[i] *= k;
return *this;
}
Vector& Vector::operator/=(double k)
{
for (size_t i = 0; i < this->size(); ++i)
(*this)[i] /= k;
return *this;
}
Vector Vector::operator+(const Vector& w) const
{
Vector u (*this);
return u += w;
}
Vector Vector::operator-(const Vector& w) const
{
Vector u (*this);
return u -= w;
}
Vector Vector::operator*(double k) const
{
Vector u (*this);
return u *= k;
}
Vector Vector::operator/(double k) const
{
Vector u (*this);
return u /= k;
}
/* Cross product */
Vector Vector::operator^(const Vector& w) const
{
Vector u = {0, 0, 0};
if (this->size() != 3 || w.size() != 3)
throw string("dimension des vecteur incompatible");
u[0] = (*this)[1] * w[2] - (*this)[2] * w[1];
u[1] = (*this)[2] * w[0] - (*this)[0] * w[2];
u[2] = (*this)[0] * w[1] - (*this)[1] * w[0];
return u;
}
/* Dot product */
double Vector::operator*(const Vector& w) const
{
double x = 0;
if (this->size() != w.size())
throw string("dimension des vecteur incompatible");
for (size_t i = 0; i < this->size(); ++i)
x += (*this)[i] * w[i];
return x;
}
double Vector::sq_module() const
{
double x = 0;
for (size_t i = 0; i < this->size(); ++i)
x += (*this)[i] * (*this)[i];
return x;
}
double Vector::module() const
{
return sqrt(this->sq_module());
}
/* Implementation of ostream operator overloading */
/* Le prototype je le mets dans un header séparé parce que
* il ne fait pas partie de l'implementation d'un vecteur
* Une classe l'utilisant n'as pas besoin de l'imprimer
*/
// Flux de sortie permettant d'afficher les coordonnées du vecteur
ostream& operator<<(ostream& os, const Vector& v)
{
os << "(";
for (auto comp : v)
{
os << comp << ", ";
}
os << ")";
return os;
}
/* Cross product for static vector */
SVector<3> operator^(const SVector<3>& v, const SVector<3>& w)
{
SVector<3> u = {0, 0, 0};
u[0] = v[1] * w[2] - v[2] * w[1];
u[1] = v[2] * w[0] - v[0] * w[2];
u[2] = v[0] * w[1] - v[1] * w[0];
return u;
}
Event Timeline
Log In to Comment