diff --git a/AttractorKernel.cpp b/AttractorKernel.cpp index fb3ad1a..302e216 100644 --- a/AttractorKernel.cpp +++ b/AttractorKernel.cpp @@ -14,18 +14,15 @@ #pragma mark - #pragma mark memory -AttractorKernel::AttractorKernel(const unsigned int dimension, const unsigned int numberOfParameters): -numberOfParameters(numberOfParameters), dimension(dimension) { +AttractorKernel::AttractorKernel(const unsigned int dimension, const unsigned int numberOfParameters) : +numberOfParameters(numberOfParameters), dimension(dimension){ try { - parameters = new double[numberOfParameters]; - - vectorNew = new double[dimension]; - vectorOld = new double[dimension]; - } catch (std::bad_alloc& e) { - std::cerr << "Exception: " << e.what() << std::endl; - std::cerr << "Quiting because nothing to do... " << std::endl; - exit(1); + allocate(); + } + catch (std::exception& e) { + std::cout << "Couldn't construct AttractorKernel: " << e.what() << std::endl; + dealloc(); } std::fill_n(parameters, numberOfParameters, 0.0); @@ -35,26 +32,22 @@ numberOfParameters(numberOfParameters), dimension(dimension) { } AttractorKernel::~AttractorKernel(){ - delete[] vectorOld; - delete[] vectorNew; - delete[] parameters; + dealloc(); } -void AttractorKernel::reallocParameters(const unsigned int newNumberOfParameters){ +void AttractorKernel::allocate(){ + parameters = new double[numberOfParameters]; + vectorNew = new double[dimension]; + vectorOld = new double[dimension]; +} + +void AttractorKernel::dealloc(){ + delete[] vectorOld; + vectorOld = NULL; + delete[] vectorNew; + vectorNew = NULL; delete[] parameters; - - numberOfParameters = newNumberOfParameters; - - try { - parameters = new double[numberOfParameters]; - } - catch (std::bad_alloc& e) { - std::cerr << "Exception: " << e.what() << std::endl; - std::cerr << "Quiting because nothing to do... " << std::endl; - exit(1); - } - - std::fill_n(parameters, numberOfParameters, 0.0); + parameters = NULL; } #pragma mark - diff --git a/AttractorKernel.hpp b/AttractorKernel.hpp index 2b5f18f..6d0b03c 100644 --- a/AttractorKernel.hpp +++ b/AttractorKernel.hpp @@ -8,6 +8,8 @@ class AttractorKernel { private: + void allocate(); + void dealloc(); protected: diff --git a/kernels/Logistic.cpp b/kernels/Logistic.cpp index 696a238..3350872 100644 --- a/kernels/Logistic.cpp +++ b/kernels/Logistic.cpp @@ -9,36 +9,35 @@ #include "Logistic.hpp" #include -Logistic::Logistic(): - AttractorKernel(3, 3) { +#pragma mark - +#pragma mark ctors + +Logistic::Logistic(): + AttractorKernel(3, 3) { init(); - } Logistic::Logistic(const unsigned int dimension): - AttractorKernel(dimension, dimension) { - + AttractorKernel(dimension, dimension) { init(); - } void Logistic::init() { - // setting some starting values - for ( unsigned int i = 0; i < dimension; i++ ) { - vectorNew[i] = vectorOld[i] = 0.5; - } - + std::fill_n(vectorOld, dimension, 0.5); + std::fill_n(vectorNew, dimension, 0.5); } + +#pragma mark - +#pragma mark attractor + void Logistic::operator()() { - std::swap(vectorNew, vectorOld); for ( unsigned int i = 0; i < dimension; i++ ) { vectorNew[i] = parameters[i]*vectorOld[i]*(1.0 - vectorOld[i]); } - } diff --git a/kernels/Lorenz3D.cpp b/kernels/Lorenz3D.cpp index 6a35fc1..c8a9dbe 100644 --- a/kernels/Lorenz3D.cpp +++ b/kernels/Lorenz3D.cpp @@ -1,31 +1,31 @@ #include "Lorenz3D.hpp" #include + +#pragma mark - +#pragma mark ctors + Lorenz3D::Lorenz3D(): - AttractorKernel(3, 4){ - + AttractorKernel(3, 4){ init(); - } void Lorenz3D::init() { - // setting some starting values - for ( unsigned int i = 0; i < dimension; i++ ) { - vectorNew[i] = vectorOld[i] = 1.0; - } - + std::fill_n(vectorOld, dimension, 1.0); + std::fill_n(vectorNew, dimension, 1.0); } -// the main function + +#pragma mark - +#pragma mark attractor + void Lorenz3D::operator()() { - std::swap(vectorNew, vectorOld); vectorNew[0] = vectorOld[0] + parameters[0] * parameters[1] * (vectorOld[1] - vectorOld[0]); vectorNew[1] = vectorOld[1] + parameters[0] * (vectorOld[0] * (parameters[2] - vectorOld[2]) - vectorOld[1]); vectorNew[2] = vectorOld[2] + parameters[0] * (vectorOld[0] * vectorOld[1] - parameters[3] * vectorOld[2]); - } /* diff --git a/kernels/Polynomial.cpp b/kernels/Polynomial.cpp index 445627a..4d3f79c 100644 --- a/kernels/Polynomial.cpp +++ b/kernels/Polynomial.cpp @@ -8,37 +8,32 @@ #include "Polynomial.hpp" #include -//#include -Polynomial::Polynomial(): - AttractorKernel(3, 0), - orde(2){ - - init(); -} -Polynomial::Polynomial(const unsigned int dimension, const unsigned int orde): - AttractorKernel(dimension, 0), - orde(orde) { - init(); -} - -void Polynomial::init() { - const unsigned int number = calculateNumberOfParameters(); - reallocParameters(number); -} - -// i want to has constexpr, so the ctor can be clean :] -unsigned int Polynomial::calculateNumberOfParameters() { +unsigned int calculateNumberOfParameters(const unsigned int dimension, const unsigned int orde) { double n_coef = orde + 1; for (unsigned int i = 2; i <= dimension; i++) { n_coef = n_coef*(orde + i)/(i - 1); } - + const unsigned int output = (unsigned int) n_coef; return output; } + +#pragma mark - +#pragma mark ctors + +Polynomial::Polynomial(): + AttractorKernel(3, calculateNumberOfParameters(3, 2)), orde(2){} + +Polynomial::Polynomial(const unsigned int dimension, const unsigned int orde): + AttractorKernel(dimension, calculateNumberOfParameters(dimension, orde)), orde(orde){} + + +#pragma mark - +#pragma mark attractor + void Polynomial::operator()() { std::swap(vectorNew, vectorOld); @@ -49,7 +44,6 @@ void Polynomial::operator()() { m++; recur(i, 0, 1, m); } - } void Polynomial::recur(unsigned int curr_dimension, unsigned int prev_i, unsigned int n, unsigned int& m, double prev_product) { diff --git a/kernels/Polynomial.hpp b/kernels/Polynomial.hpp index 3572258..394745e 100644 --- a/kernels/Polynomial.hpp +++ b/kernels/Polynomial.hpp @@ -8,8 +8,6 @@ private: unsigned int orde; - void init(); - unsigned int calculateNumberOfParameters(); void recur(unsigned int curr_dimension, unsigned int prev_i, unsigned int n, unsigned int& m, double prev_product=1.0); public: diff --git a/kernels/PolynomialA3D.cpp b/kernels/PolynomialA3D.cpp index ac664d5..cd6bd60 100644 --- a/kernels/PolynomialA3D.cpp +++ b/kernels/PolynomialA3D.cpp @@ -9,14 +9,16 @@ #include "PolynomialA3D.hpp" #include + +#pragma mark - +#pragma mark ctors + PolynomialA3D::PolynomialA3D(): - AttractorKernel(3, 3){ - init(); -} + AttractorKernel(3, 3){} -void PolynomialA3D::init() { -} +#pragma mark - +#pragma mark attractor void PolynomialA3D::operator()() { std::swap(vectorNew, vectorOld); @@ -25,6 +27,7 @@ void PolynomialA3D::operator()() { vectorNew[1] = parameters[1] + vectorOld[2] - vectorOld[2]*vectorOld[0]; vectorNew[2] = parameters[2] + vectorOld[0] - vectorOld[0]*vectorOld[1]; } + /* N dimensional: for ( unsigned int i = 0; i < dim; i++ ) { diff --git a/kernels/PolynomialA3D.hpp b/kernels/PolynomialA3D.hpp index 74de519..e53be0d 100644 --- a/kernels/PolynomialA3D.hpp +++ b/kernels/PolynomialA3D.hpp @@ -6,8 +6,6 @@ class PolynomialA3D : public AttractorKernel { private: - void init(); - public: PolynomialA3D(); diff --git a/kernels/Unravel3D.cpp b/kernels/Unravel3D.cpp index aebe7f5..6218840 100644 --- a/kernels/Unravel3D.cpp +++ b/kernels/Unravel3D.cpp @@ -2,10 +2,16 @@ #include #include + +#pragma mark - +#pragma mark ctors + Unravel3D::Unravel3D(): - AttractorKernel(3, 7){ + AttractorKernel(3, 7){} -} + +#pragma mark - +#pragma mark attractor void Unravel3D::operator()() { std::swap(vectorNew, vectorOld); @@ -17,7 +23,7 @@ void Unravel3D::operator()() { const double dist = vectorNew[0]*vectorNew[0] + vectorNew[1]*vectorNew[1] + vectorNew[2]*vectorNew[2]; if ( dist > parameters[6]*parameters[6] ) { - const double sqrtDist = sqrt(dist); + const double sqrtDist = std::sqrt(dist); const double p = 1.0 - parameters[6] * ( static_cast ( sqrtDist / parameters[6] ) + 1.0 ) / sqrtDist; vectorNew[0] *= p; vectorNew[1] *= p;