Browse Source

simplified design

master
Joshua Moerman 15 years ago
parent
commit
e44e7adc71
  1. 6
      Attractor.cpp
  2. 89
      AttractorKernel.cpp
  3. 43
      AttractorKernel.hpp
  4. 2
      defines.hpp
  5. 58
      kernels/KernelBasicParameter.cpp
  6. 46
      kernels/KernelBasicParameter.hpp
  7. 55
      kernels/KernelBasicVector.cpp
  8. 45
      kernels/KernelBasicVector.hpp
  9. 10
      kernels/Logistic.cpp
  10. 5
      kernels/Logistic.hpp
  11. 5
      kernels/Lorenz3D.cpp
  12. 5
      kernels/Lorenz3D.hpp
  13. 19
      kernels/Polynomial.cpp
  14. 5
      kernels/Polynomial.hpp
  15. 3
      kernels/PolynomialA3D.cpp
  16. 5
      kernels/PolynomialA3D.hpp
  17. 3
      kernels/Unravel3D.cpp
  18. 5
      kernels/Unravel3D.hpp

6
Attractor.cpp

@ -79,7 +79,7 @@ Attractor::Attractor(const char* const fileName) {
// read parameters // read parameters
const unsigned int numberOfParameters = myAttractor->numberOfParameters(); const unsigned int numberOfParameters = myAttractor->getNumberOfParameters();
for ( unsigned int i = 0; i < numberOfParameters; i++ ) { for ( unsigned int i = 0; i < numberOfParameters; i++ ) {
stfu::node attractorParameters = attractor.getChild("parameters"); stfu::node attractorParameters = attractor.getChild("parameters");
@ -103,7 +103,7 @@ void Attractor::init_range() {
} }
// initialize projectors with dimension and first point // initialize projectors with dimension and first point
const unsigned int dimension = myAttractor->dimension(); const unsigned int dimension = myAttractor->getDimension();
const double * point = myAttractor->vector(); const double * point = myAttractor->vector();
for ( std::vector<Projector*>::iterator it = projectors.begin(); it != projectors.end(); it++ ) { for ( std::vector<Projector*>::iterator it = projectors.begin(); it != projectors.end(); it++ ) {
(*it)->extern_dim = dimension; (*it)->extern_dim = dimension;
@ -167,7 +167,7 @@ void Attractor::plot() {
IO & control IO & control
*/ */
void Attractor::output() { void Attractor::output() {
const unsigned int dimension = myAttractor->dimension(); const unsigned int dimension = myAttractor->getDimension();
const double * point = myAttractor->vector(); const double * point = myAttractor->vector();
for ( unsigned int i = 0; i < dimension; i++ ) { for ( unsigned int i = 0; i < dimension; i++ ) {

89
AttractorKernel.cpp

@ -0,0 +1,89 @@
/*
* AttractorKernel.cpp
* AwesomeAttractor
*
* Created by Joshua Moerman on 07-08-10.
* Copyright 2010 Rodo. All rights reserved.
*
*/
#include "AttractorKernel.hpp"
#include <algorithm>
#include <iostream>
#pragma mark -
#pragma mark memory
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);
}
std::fill_n(parameters, numberOfParameters, 0.0);
std::fill_n(vectorNew, dimension, 0.0);
std::fill_n(vectorOld, dimension, 0.0);
}
AttractorKernel::~AttractorKernel(){
delete[] vectorOld;
delete[] vectorNew;
delete[] parameters;
}
void AttractorKernel::reallocParameters(const unsigned int newNumberOfParameters){
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);
}
#pragma mark -
#pragma mark parameters
double & AttractorKernel::operator[](const unsigned int index){
return parameters[index];
}
double const & AttractorKernel::operator[](const unsigned int index) const{
return parameters[index];
}
unsigned int AttractorKernel::getNumberOfParameters() const{
return numberOfParameters;
}
#pragma mark -
#pragma mark vector
double const * AttractorKernel::vector() const{
return vectorNew;
}
double const * AttractorKernel::previousVector() const{
return vectorOld;
}
unsigned int AttractorKernel::getDimension() const{
return dimension;
}

43
AttractorKernel.hpp

@ -3,30 +3,43 @@
/* /*
Protocol/interface, what you want ABC
*/ */
class AttractorKernel { class AttractorKernel {
private:
protected:
// stuff used by subclasses
AttractorKernel(const unsigned int dimension, const unsigned int numberOfParameters);
void reallocParameters(const unsigned int numberOfParameters);
unsigned int numberOfParameters;
double * parameters;
unsigned int dimension;
double * vectorNew;
double * vectorOld;
public: public:
// parameters are stored in a array of doubles // parameters are stored in a array of doubles
// if you want to use other types, use the properties double & operator[](const unsigned int index);
virtual double & operator[](const unsigned int index) = 0; double const & operator[](const unsigned int index) const;
virtual double const & operator[](const unsigned int index) const = 0; unsigned int getNumberOfParameters() const;
virtual unsigned int numberOfParameters() const = 0; // iterate his formula, implemented by subclasses
// iterate his formula
// vector pointers will be swapped! so new remains new and old remains old
virtual void operator()() = 0; virtual void operator()() = 0;
// getter functions for teh resulta (can be used as setters, and it may be used as such) // getter functions for teh resulta (can't be used as setters)
virtual double * & vector() = 0; double const * vector() const;
virtual double * & previousVector() = 0; double const * previousVector() const;
unsigned int getDimension() const;
virtual unsigned int dimension() const = 0;
// dtor, default is generally good // dtor, should be virtual for subclasses to be deleted
virtual ~AttractorKernel() {}; virtual ~AttractorKernel();
}; };

2
defines.hpp

@ -1,7 +1,7 @@
//TODO: do this with files //TODO: do this with files
#define ATTRACTOR_FILE "attractors/testPolynomial.stf" #define ATTRACTOR_FILE "attractors/testLorenz.stf"
#ifdef UNI_BUILD #ifdef UNI_BUILD
#warning Building for the RU, are you sure? #warning Building for the RU, are you sure?

58
kernels/KernelBasicParameter.cpp

@ -1,58 +0,0 @@
/*
* KernelBasicParameter.cpp
* AwesomeAttractor
*
* Created by Joshua Moerman on 18-07-10.
* Copyright 2010 Rodo. All rights reserved.
*
*/
#include "KernelBasicParameter.hpp"
#include <iostream>
#pragma mark -
#pragma mark Memory managment
KernelBasicParameter::KernelBasicParameter(const unsigned int numberOfParameters):
myNumberOfParameters(numberOfParameters){
init();
}
void KernelBasicParameter::resetNumberOfParameters(const unsigned int numberOfParameters){
dealloc();
myNumberOfParameters = numberOfParameters;
init();
}
KernelBasicParameter::~KernelBasicParameter(){
dealloc();
}
void KernelBasicParameter::init(){
parameters = new double[myNumberOfParameters];
for ( unsigned int i = 0; i < myNumberOfParameters; i++ ) {
parameters[i] = 0.0;
}
}
void KernelBasicParameter::dealloc(){
delete[] parameters;
}
#pragma mark -
#pragma mark Implementation
double & KernelBasicParameter::operator[](const unsigned int index){
return parameters[index];
}
double const & KernelBasicParameter::operator[](const unsigned int index) const{
return parameters[index];
}
unsigned int KernelBasicParameter::numberOfParameters() const{
return myNumberOfParameters;
}

46
kernels/KernelBasicParameter.hpp

@ -1,46 +0,0 @@
/*
* KernelBasicParameter.hpp
* AwesomeAttractor
*
* Created by Joshua Moerman on 18-07-10.
* Copyright 2010 Rodo. All rights reserved.
*
*/
#ifndef KERNELBASICPARAMETER_HPP
#define KERNELBASICPARAMETER_HPP
#include "../AttractorKernel.hpp"
/*
This is a partial default implementation of the abstract AttractorKernel class
you can easily use the constructor to initialise the parameters and stuff...
*/
class KernelBasicParameter : virtual public AttractorKernel {
private:
void init();
void dealloc();
protected:
unsigned int myNumberOfParameters;
double * parameters;
KernelBasicParameter(const unsigned int numberOfParameters);
virtual ~KernelBasicParameter();
void resetNumberOfParameters(const unsigned int numberOfParameters);
public:
// parameters are stored in a array of doubles
// if you want to use other types, use the properties
virtual double & operator[](const unsigned int index);
virtual double const & operator[](const unsigned int index) const;
virtual unsigned int numberOfParameters() const;
};
#endif

55
kernels/KernelBasicVector.cpp

@ -1,55 +0,0 @@
/*
* KernelBasicVector.cpp
* AwesomeAttractor
*
* Created by Joshua Moerman on 18-07-10.
* Copyright 2010 Rodo. All rights reserved.
*
*/
#include "KernelBasicVector.hpp"
#include <iostream>
#pragma mark -
#pragma mark Memory Managment
KernelBasicVector::KernelBasicVector(const unsigned int dimension):
myDimension(dimension){
init();
}
KernelBasicVector::~KernelBasicVector(){
dealloc();
}
void KernelBasicVector::init(){
vectorNew = new double[myDimension];
vectorOld = new double[myDimension];
for ( unsigned int i = 0; i < myDimension; i++ ) {
vectorNew[i] = vectorOld[i] = 0.0;
}
}
void KernelBasicVector::dealloc(){
delete[] vectorOld;
delete[] vectorNew;
}
#pragma mark -
#pragma mark Implementation
double * & KernelBasicVector::vector(){
return vectorNew;
}
double * & KernelBasicVector::previousVector(){
return vectorOld;
}
unsigned int KernelBasicVector::dimension() const{
return myDimension;
}

45
kernels/KernelBasicVector.hpp

@ -1,45 +0,0 @@
/*
* KernelBasicVector.hpp
* AwesomeAttractor
*
* Created by Joshua Moerman on 18-07-10.
* Copyright 2010 Rodo. All rights reserved.
*
*/
#ifndef KERNELBASICVECTOR_HPP
#define KERNELBASICVECTOR_HPP
#include "../AttractorKernel.hpp"
/*
This is a partial default implementation of the abstract AttractorKernel class
you can easily use the constructor to initialise the vectors and stuff...
*/
class KernelBasicVector : virtual public AttractorKernel {
private:
void init();
void dealloc();
protected:
unsigned int myDimension;
double * vectorNew;
double * vectorOld;
KernelBasicVector(const unsigned int dimension);
virtual ~KernelBasicVector();
public:
// getter functions for teh resulta (can be used as setters, and it may be used as such)
virtual double * & vector();
virtual double * & previousVector();
virtual unsigned int dimension() const;
};
#endif

10
kernels/Logistic.cpp

@ -10,16 +10,14 @@
#include <algorithm> #include <algorithm>
Logistic::Logistic(): Logistic::Logistic():
KernelBasicParameter(3), AttractorKernel(3, 3) {
KernelBasicVector(3) {
init(); init();
} }
Logistic::Logistic(const unsigned int dimension): Logistic::Logistic(const unsigned int dimension):
KernelBasicParameter(dimension), AttractorKernel(dimension, dimension) {
KernelBasicVector(dimension) {
init(); init();
@ -28,7 +26,7 @@ Logistic::Logistic(const unsigned int dimension):
void Logistic::init() { void Logistic::init() {
// setting some starting values // setting some starting values
for ( unsigned int i = 0; i < myDimension; i++ ) { for ( unsigned int i = 0; i < dimension; i++ ) {
vectorNew[i] = vectorOld[i] = 0.5; vectorNew[i] = vectorOld[i] = 0.5;
} }
@ -38,7 +36,7 @@ void Logistic::operator()() {
std::swap(vectorNew, vectorOld); std::swap(vectorNew, vectorOld);
for ( unsigned int i = 0; i < myDimension; i++ ) { for ( unsigned int i = 0; i < dimension; i++ ) {
vectorNew[i] = parameters[i]*vectorOld[i]*(1.0 - vectorOld[i]); vectorNew[i] = parameters[i]*vectorOld[i]*(1.0 - vectorOld[i]);
} }

5
kernels/Logistic.hpp

@ -1,10 +1,9 @@
#ifndef LOGISTIC_HPP #ifndef LOGISTIC_HPP
#define LOGISTIC_HPP #define LOGISTIC_HPP
#include "KernelBasicParameter.hpp" #include "../AttractorKernel.hpp"
#include "KernelBasicVector.hpp"
class Logistic : public KernelBasicParameter, public KernelBasicVector { class Logistic : public AttractorKernel {
private: private:
void init(); void init();

5
kernels/Lorenz3D.cpp

@ -2,8 +2,7 @@
#include <algorithm> #include <algorithm>
Lorenz3D::Lorenz3D(): Lorenz3D::Lorenz3D():
KernelBasicParameter(4), AttractorKernel(3, 4){
KernelBasicVector(3){
init(); init();
@ -12,7 +11,7 @@ Lorenz3D::Lorenz3D():
void Lorenz3D::init() { void Lorenz3D::init() {
// setting some starting values // setting some starting values
for ( unsigned int i = 0; i < myDimension; i++ ) { for ( unsigned int i = 0; i < dimension; i++ ) {
vectorNew[i] = vectorOld[i] = 1.0; vectorNew[i] = vectorOld[i] = 1.0;
} }

5
kernels/Lorenz3D.hpp

@ -1,10 +1,9 @@
#ifndef LORENZ_HPP #ifndef LORENZ_HPP
#define LORENZ_HPP #define LORENZ_HPP
#include "KernelBasicParameter.hpp" #include "../AttractorKernel.hpp"
#include "KernelBasicVector.hpp"
class Lorenz3D : public KernelBasicParameter, public KernelBasicVector { class Lorenz3D : public AttractorKernel {
private: private:
void init(); void init();

19
kernels/Polynomial.cpp

@ -11,29 +11,27 @@
//#include <iostream> //#include <iostream>
Polynomial::Polynomial(): Polynomial::Polynomial():
KernelBasicParameter(0), AttractorKernel(3, 0),
KernelBasicVector(3),
orde(2){ orde(2){
init(); init();
} }
Polynomial::Polynomial(const unsigned int dimension, const unsigned int orde): Polynomial::Polynomial(const unsigned int dimension, const unsigned int orde):
KernelBasicParameter(0), AttractorKernel(dimension, 0),
KernelBasicVector(dimension),
orde(orde) { orde(orde) {
init(); init();
} }
void Polynomial::init() { void Polynomial::init() {
const unsigned int number = calculateNumberOfParameters(); const unsigned int number = calculateNumberOfParameters();
KernelBasicParameter::resetNumberOfParameters(number); reallocParameters(number);
} }
// i want to has constexpr, so the ctor can be clean :] // i want to has constexpr, so the ctor can be clean :]
unsigned int Polynomial::calculateNumberOfParameters() { unsigned int Polynomial::calculateNumberOfParameters() {
double n_coef = orde + 1; double n_coef = orde + 1;
for (unsigned int i = 2; i <= myDimension; i++) { for (unsigned int i = 2; i <= dimension; i++) {
n_coef = n_coef*(orde + i)/(i - 1); n_coef = n_coef*(orde + i)/(i - 1);
} }
@ -45,7 +43,7 @@ void Polynomial::operator()() {
std::swap(vectorNew, vectorOld); std::swap(vectorNew, vectorOld);
unsigned int m = 0; unsigned int m = 0;
for ( unsigned int i = 0; i < myDimension; i++ ) { for ( unsigned int i = 0; i < dimension; i++ ) {
vectorNew[i] = parameters[m]; vectorNew[i] = parameters[m];
m++; m++;
@ -56,7 +54,7 @@ void Polynomial::operator()() {
void Polynomial::recur(unsigned int curr_dimension, unsigned int prev_i, unsigned int n, unsigned int& m, double prev_product) { void Polynomial::recur(unsigned int curr_dimension, unsigned int prev_i, unsigned int n, unsigned int& m, double prev_product) {
double product; double product;
for (unsigned int i = prev_i; i < myDimension; i++) { for (unsigned int i = prev_i; i < dimension; i++) {
product = prev_product * vectorOld[i]; product = prev_product * vectorOld[i];
vectorNew[curr_dimension] += parameters[m] * product; vectorNew[curr_dimension] += parameters[m] * product;
@ -67,3 +65,8 @@ void Polynomial::recur(unsigned int curr_dimension, unsigned int prev_i, unsigne
} }
} }

5
kernels/Polynomial.hpp

@ -1,10 +1,9 @@
#ifndef POLYNOMIAL_HPP #ifndef POLYNOMIAL_HPP
#define POLYNOMIAL_HPP #define POLYNOMIAL_HPP
#include "KernelBasicParameter.hpp" #include "../AttractorKernel.hpp"
#include "KernelBasicVector.hpp"
class Polynomial : public KernelBasicParameter, public KernelBasicVector { class Polynomial : public AttractorKernel {
private: private:
unsigned int orde; unsigned int orde;

3
kernels/PolynomialA3D.cpp

@ -10,8 +10,7 @@
#include <algorithm> #include <algorithm>
PolynomialA3D::PolynomialA3D(): PolynomialA3D::PolynomialA3D():
KernelBasicParameter(3), AttractorKernel(3, 3){
KernelBasicVector(3){
init(); init();
} }

5
kernels/PolynomialA3D.hpp

@ -1,10 +1,9 @@
#ifndef POLYNOMIALA3D_HPP #ifndef POLYNOMIALA3D_HPP
#define POLYNOMIALA3D_HPP #define POLYNOMIALA3D_HPP
#include "KernelBasicParameter.hpp" #include "../AttractorKernel.hpp"
#include "KernelBasicVector.hpp"
class PolynomialA3D : public KernelBasicParameter, public KernelBasicVector { class PolynomialA3D : public AttractorKernel {
private: private:
void init(); void init();

3
kernels/Unravel3D.cpp

@ -3,8 +3,7 @@
#include <cmath> #include <cmath>
Unravel3D::Unravel3D(): Unravel3D::Unravel3D():
KernelBasicParameter(7), AttractorKernel(3, 7){
KernelBasicVector(3){
} }

5
kernels/Unravel3D.hpp

@ -1,10 +1,9 @@
#ifndef UNRAVEL_HPP #ifndef UNRAVEL_HPP
#define UNRAVEL_HPP #define UNRAVEL_HPP
#include "KernelBasicParameter.hpp" #include "../AttractorKernel.hpp"
#include "KernelBasicVector.hpp"
class Unravel3D : public KernelBasicParameter, public KernelBasicVector { class Unravel3D : public AttractorKernel {
public: public:
Unravel3D(); Unravel3D();