Browse Source

moved to header

master
Joshua Moerman 14 years ago
parent
commit
4d811dabb9
  1. 76
      AttractorKernel.cpp
  2. 91
      AttractorKernel.hpp

76
AttractorKernel.cpp

@ -8,82 +8,6 @@
*/ */
#include "AttractorKernel.hpp" #include "AttractorKernel.hpp"
#include "Logger.hpp"
#include <algorithm>
#pragma mark -
#pragma mark memory
AttractorKernel::AttractorKernel(const unsigned int dimension, const unsigned int numberOfParameters) :
numberOfParameters(numberOfParameters), dimension(dimension) {
try {
allocate();
} catch(std::exception& e) {
LogError("Couldn't construct Attractorkernel: %s\n", e.what());
dealloc();
}
std::fill_n(parameters, numberOfParameters, 0.0);
std::fill_n(vectorNew, dimension, 0.0);
std::fill_n(vectorOld, dimension, 0.0);
}
AttractorKernel::~AttractorKernel() {
dealloc();
}
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;
parameters = NULL;
}
#pragma mark -
#pragma mark parameters
// NOTE: inlining these functions (with the keyword inline) improves performance by at most 1% (tested)
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;
}
#pragma mark -
#pragma mark factory function
#include "kernels/Logistic.hpp" #include "kernels/Logistic.hpp"
#include "kernels/Lorenz3D.hpp" #include "kernels/Lorenz3D.hpp"
#include "kernels/Polynomial.hpp" #include "kernels/Polynomial.hpp"

91
AttractorKernel.hpp

@ -1,6 +1,8 @@
#ifndef ATTRACTORKERNEL_HPP #ifndef ATTRACTORKERNEL_HPP
#define ATTRACTORKERNEL_HPP #define ATTRACTORKERNEL_HPP
#include "Logger.hpp"
#include <algorithm>
#include <cstdlib> #include <cstdlib>
#include "stfu/stf.hpp" #include "stfu/stf.hpp"
@ -8,13 +10,24 @@
class AttractorKernel { class AttractorKernel {
private: private:
void allocate(); #pragma mark -
void dealloc(); #pragma mark memory
void allocate() {
parameters = new double[numberOfParameters];
vectorNew = new double[dimension];
vectorOld = new double[dimension];
}
protected: void dealloc() {
delete[] vectorOld;
vectorOld = NULL;
delete[] vectorNew;
vectorNew = NULL;
delete[] parameters;
parameters = NULL;
}
// biggest type first, this will reduce sizeof(AttractorKernel) protected:
// size is now 40 (when the unsigned int are in front, it was 48)
double* parameters; double* parameters;
double* vectorNew; double* vectorNew;
double* vectorOld; double* vectorOld;
@ -22,15 +35,37 @@ protected:
unsigned int numberOfParameters; unsigned int numberOfParameters;
unsigned int dimension; unsigned int dimension;
// stuff used by subclasses AttractorKernel(const unsigned int dimension, const unsigned int numberOfParameters) :
AttractorKernel(const unsigned int dimension, const unsigned int numberOfParameters); numberOfParameters(numberOfParameters), dimension(dimension) {
try {
allocate();
} catch(std::exception& e) {
LogError("Couldn't construct Attractorkernel: %s\n", e.what());
dealloc();
}
std::fill_n(parameters, numberOfParameters, 0.0);
std::fill_n(vectorNew, dimension, 0.0);
std::fill_n(vectorOld, dimension, 0.0);
}
public: public:
virtual ~AttractorKernel() {
dealloc();
}
// parameters are stored in a array of doubles #pragma mark -
double& operator[](const unsigned int index); #pragma mark parameters
double const& operator[](const unsigned int index) const; double& operator[](const unsigned int index) {
unsigned int getNumberOfParameters() const; return parameters[index];
}
double const& operator[](const unsigned int index) const {
return parameters[index];
}
unsigned int getNumberOfParameters() const {
return numberOfParameters;
}
virtual void generate_random_parameters() { virtual void generate_random_parameters() {
for(unsigned int i = 0; i < numberOfParameters; ++i) for(unsigned int i = 0; i < numberOfParameters; ++i)
@ -39,35 +74,45 @@ public:
vectorNew[i] = vectorOld[i] = 6.0 * rand() / double(RAND_MAX) - 3.0; vectorNew[i] = vectorOld[i] = 6.0 * rand() / double(RAND_MAX) - 3.0;
} }
// tests #pragma mark -
bool convergent(){ #pragma mark tests
bool convergent() {
double sum = 0.0; double sum = 0.0;
for(unsigned int i = 0; i < dimension; ++i){ for(unsigned int i = 0; i < dimension; ++i) {
sum += (vectorNew[i] - vectorOld[i])*(vectorNew[i] - vectorOld[i]); sum += (vectorNew[i] - vectorOld[i])*(vectorNew[i] - vectorOld[i]);
} }
return sum < 1.0e-6; return sum < 1.0e-6;
} }
bool divergent(){ bool divergent() {
double sum = 0.0; double sum = 0.0;
for(unsigned int i = 0; i < dimension; ++i){ for(unsigned int i = 0; i < dimension; ++i) {
sum += (vectorNew[i] - vectorOld[i])*(vectorNew[i] - vectorOld[i]); sum += (vectorNew[i] - vectorOld[i])*(vectorNew[i] - vectorOld[i]);
} }
return sum > 1.0e3; return sum > 1.0e3;
} }
#pragma mark -
#pragma mark main
// iterate his formula, implemented by subclasses // iterate his formula, implemented by subclasses
virtual void operator()() = 0; virtual void operator()() = 0;
// getter functions for teh resulta (can't be used as setters) #pragma mark -
double const* vector() const; #pragma mark vector
double const* previousVector() const; double const* vector() const {
unsigned int getDimension() const; return vectorNew;
}
double const* previousVector() const {
return vectorOld;
}
// dtor, should be virtual for subclasses to be deleted unsigned int getDimension() const {
virtual ~AttractorKernel(); return dimension;
}
// factory function #pragma mark -
#pragma mark factory function
static AttractorKernel* createAttractorKernel(stfu::node& attractorKernel); static AttractorKernel* createAttractorKernel(stfu::node& attractorKernel);
}; };