My old project for strange attractors
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

127 lines
2.7 KiB

#include <iostream>
using namespace std;
#include "Vector.hpp"
Vector::Vector():
dimension(0) {
coordinates = new (nothrow) float[0];
assert(coordinates != NULL);
#ifdef HARDDEBUG
cout << "New vector (without elements)" << endl;
#endif
}
Vector::Vector(unsigned int d):
dimension(d) {
coordinates = new (nothrow) float[dimension];
assert(coordinates != NULL);
#ifdef HARDDEBUG
cout << "New vector:" << endl << *this << endl;
#endif
}
Vector::Vector(unsigned int d, float default_val):
dimension(d) {
coordinates = new (nothrow) float[dimension];
assert(coordinates != NULL);
for (unsigned int i = 0; i < dimension; i++) {
coordinates[i] = default_val;
}
#ifdef HARDDEBUG
cout << "New vector with default values:" << endl << *this << endl;
#endif
}
Vector::~Vector() {
delete[] coordinates;
#ifdef HARDDEBUG
cout << "coordinates deleted" << endl;
#endif
}
Vector& Vector::operator=(const Vector& a) {
if ( dimension != a.dimension ) {
dimension = a.dimension;
delete[] coordinates;
coordinates = new float[dimension];
#ifdef HARDDEBUG
cout << "Dimensions were not equal, made new vector" << endl;
#endif
}
for ( unsigned int i = 0; i < dimension; i++ ) {
coordinates[i] = a.coordinates[i];
}
#ifdef HARDDEBUG
cout << "operator= result" << endl << *this << endl;
#endif
return *this;
}
ostream& operator<<(ostream& os, const Vector& a) {
os << a.dimension << endl;
for ( unsigned int i = 0; i < a.dimension; i++ ) {
os << a.coordinates[i] << " ";
}
os << endl;
return os;
}
float& Vector::operator[](const unsigned int index) {
assert(index < dimension);
return coordinates[index];
}
// matig werkende optelling en scalaire vermenigvuldiging van vectoren
/*
Vector Vector::operator+(const Vector a) const {
if ( dimension != a.dimension ) {
cout << "WARNING: dimensions not equal in vector addition" << endl;
exit(1);
} else {
static Vector ret(dimension);
for ( unsigned int i = 0; i < dimension; i++ ) {
ret.coordinates[i] = coordinates[i] + a.coordinates[i];
}
#ifdef HARDDEBUG
cout << "operator+ result" << endl << ret << endl;
#endif
return ret;
}
}
Vector Vector::operator*(const float a) const {
static Vector ret(dimension);
for ( unsigned int i = 0; i < dimension; i++ ) {
ret.coordinates[i] = coordinates[i] * a;
}
#ifdef HARDDEBUG
cout << "operator* result" << endl << ret << endl;
#endif
return ret;
}
*/