From 12fa75b7f6a1c9857930ae3eacd414c558115eaa Mon Sep 17 00:00:00 2001 From: Joshua Date: Fri, 2 Apr 2010 15:29:52 +0200 Subject: [PATCH] all kernels --- Attractor.cpp | 87 ++------------------------------------- Attractor.hpp | 7 +--- kernels/Logistic.cpp | 92 ++++++++++++++++++++++++++++++++++++++++++ kernels/Logistic.hpp | 42 +++++++++++++++++++ kernels/Polynomial.cpp | 1 + 5 files changed, 139 insertions(+), 90 deletions(-) create mode 100644 kernels/Logistic.cpp create mode 100644 kernels/Logistic.hpp diff --git a/Attractor.cpp b/Attractor.cpp index b4dba1f..518df6a 100644 --- a/Attractor.cpp +++ b/Attractor.cpp @@ -26,12 +26,6 @@ Attractor::Attractor(): } -Attractor::Attractor(unsigned int dimensions, FormulaChoice formula, unsigned int orde): - dim(dimensions), formula(formula), orde(orde), param(NULL), point(NULL), new_point(NULL) { - - init(dimensions, formula, orde); -} - Attractor::Attractor(const char* const filename) { ifstream file(filename); @@ -42,6 +36,8 @@ Attractor::Attractor(const char* const filename) { exit(2); } + // TODO : Use stfu + /* file.attr: lorenz @@ -95,46 +91,6 @@ Attractor::Attractor(const char* const filename) { void Attractor::init(unsigned int dim_in, FormulaChoice formula_in, unsigned int orde_in) { - dim = dim_in; - formula = formula_in; - orde = orde_in; - - switch (formula) { - case LOGISTIC: { - par = dim; - break; - } - default: { - cout << "Formula not recognized" << endl; - exit(1); - break; - } - } - - init_vector(); - init_param(); -} - -void Attractor::init_vector() { - point = new double[dim]; - new_point = new double[dim]; - - assert(point != NULL); - assert(new_point != NULL); - - for ( unsigned int i = 0; i < dim; i++ ) { - point[i] = new_point[i] = 0.9; - } -} - -void Attractor::init_param() { - param = new double[par]; - - assert(param != NULL); - - for ( unsigned int i = 0; i < dim; i++ ) { - param[i] = 0.0; - } } void Attractor::init_range() { @@ -201,46 +157,9 @@ bool Attractor::is_chaos() { Iteration & Math */ void Attractor::iterate() { - // pointer swap - double * temp = point; - point = new_point; - new_point = temp; - - // calculations - switch (formula) { - - case LOGISTIC: - logistic(); - break; - - default: - cout << "Formula not recognized" << endl; - exit(1); - break; - } - -#ifdef HARDDEBUG - cout << "Formula " << formula << " is done" << endl; - cout << "Old Vector: "; - for ( unsigned int i = 0; i < dim; i++ ) { - cout << point[i] << " "; - } - cout << endl << "Fresh Vector: "; - for ( unsigned int i = 0; i < dim; i++ ) { - cout << new_point[i] << " "; - } - cout << endl; -#endif + // attractorKernel->iterate() } - -void Attractor::logistic() { - for ( unsigned int i = 0; i < dim; i++ ) { - new_point[i] = param[i]*point[i]*(1.0 - point[i]); - } -} - - void Attractor::plot() { for ( vector::iterator it = projectors.begin(); it != projectors.end(); it++ ) { (*it)->plot(point); diff --git a/Attractor.hpp b/Attractor.hpp index d09d30c..d6da8c4 100644 --- a/Attractor.hpp +++ b/Attractor.hpp @@ -32,13 +32,11 @@ public: public: Attractor(); - Attractor(unsigned int dimensions, FormulaChoice formula, unsigned int orde = 3); Attractor(const char* const filename); void init(unsigned int dimensions, FormulaChoice formula, unsigned int orde); - void init_vector(); - void init_param(); + void init_range(); // TODO : lyapunov exponent uit rekenen @@ -47,9 +45,6 @@ public: // TODO : optimaliseren voor lage graads veeltermen void iterate(); - void poly_A(); - void poly_2(); - void logistic(); void plot(); void output(); diff --git a/kernels/Logistic.cpp b/kernels/Logistic.cpp new file mode 100644 index 0000000..79b5aec --- /dev/null +++ b/kernels/Logistic.cpp @@ -0,0 +1,92 @@ +// +// $filename +// $projectname +// +// Created by Joshua moerman on $TODAY. +// Copyright 2010 Joshua Moerman. All rights reserved. +// + +#include "Logistic.hpp" + +Logistic::Logistic() { + dimension = 3; + + init(); +} + +Logistic::Logistic(const unsigned int dimension): + dimension(dimension) { + init(); +} + +void Logistic::init() { + const unsigned int numberOfParameters = dimension; + + if ( myParameters != NULL ) { + delete myParameters; + } + myParameters = new double[numberOfParameters]; + + if ( vectorNew != NULL ) { + delete vectorNew; + } + vectorNew = new double[dimension]; + + if ( vectorOld != NULL ) { + delete vectorOld; + } + vectorOld = new double[dimension]; + + assert(myParameters != NULL); + assert(vectorNew != NULL); + assert(vectorOld != NULL); + for ( unsigned int i = 0; i < numberOfParameters; i++ ) { + myParameters[i] = 0.0; + } + for ( unsigned int i = 0; i < dimension; i++ ) { + vectorNew[i] = vectorOld[i] = 0.5; + } +} + +void Logistic::iterate() { + swap(vectorNew, vectorOld); + + for ( unsigned int i = 0; i < dim; i++ ) { + new_point[i] = param[i]*point[i]*(1.0 - point[i]); + } +} + + +// setters, getters, all i/o to other classes/objects +void * Logistic::getProperty(const string identifier) { + if ( identifier == "dimension" ) { + unsigned int * _return = new unsigned int; + *_return = dimension; + return _return; + } + return NULL; +} + +void Logistic::setProperty(const string identifier, const void * _value) { + if ( identifier == "dimension" ) { + unsigned int * value = (unsigned int*) _value; + dimension = *value; + init(); + } +} + +double * & Logistic::parameters() { + return myParameters; +} + +double & Logistic::parameter(const unsigned int index) { + return myParameters[index]; +} + +double * & Logistic::vector() { + return vectorNew; +} + +double * & Logistic::previousVector() { + return vectorOld; +} diff --git a/kernels/Logistic.hpp b/kernels/Logistic.hpp new file mode 100644 index 0000000..9235214 --- /dev/null +++ b/kernels/Logistic.hpp @@ -0,0 +1,42 @@ +#ifndef LOGISTIC_HPP +#define LOGISTIC_HPP + +#include "../AttractorKernel.hpp" + +class Logistic : AttractorKernel { + double * myParameters; + + double * vectorNew; + double * vectorOld; + + unsigned int dimension; + + void init(); + + public: + + Logistic(); + Logistic(const unsigned int dimension); + + // parameters are stored in a array of doubles + // if you want to use other types, use the properties + virtual double& parameter(const unsigned int index); + virtual double*& parameters(); + + // get properties of the attractor + // such as the dimension + // you should delete the void pointer if you used it + virtual void * getProperty(const string identifier); + virtual void setProperty(const string identifier, const void * value); + + // iterate his formula + // vector pointers will be swapped! so new remains new and old remains old + virtual void iterate(); + + // getter functions for teh resulta + virtual double * & vector(); + virtual double * & previousVector(); +}; + +#endif // LOGISTIC_HPP + diff --git a/kernels/Polynomial.cpp b/kernels/Polynomial.cpp index df09a14..61ae241 100644 --- a/kernels/Polynomial.cpp +++ b/kernels/Polynomial.cpp @@ -6,6 +6,7 @@ // Copyright 2010 Joshua Moerman. All rights reserved. // +#include "Polynomial.hpp" Polynomial::Polynomial() { dimension = 3;