|
|
@ -1,6 +1,8 @@ |
|
|
|
#ifndef ATTRACTORKERNEL_HPP |
|
|
|
#define ATTRACTORKERNEL_HPP |
|
|
|
|
|
|
|
#include "Logger.hpp" |
|
|
|
#include <algorithm> |
|
|
|
#include <cstdlib> |
|
|
|
|
|
|
|
#include "stfu/stf.hpp" |
|
|
@ -8,13 +10,24 @@ |
|
|
|
class AttractorKernel { |
|
|
|
private: |
|
|
|
|
|
|
|
void allocate(); |
|
|
|
void dealloc(); |
|
|
|
#pragma mark - |
|
|
|
#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)
|
|
|
|
// size is now 40 (when the unsigned int are in front, it was 48)
|
|
|
|
protected: |
|
|
|
double* parameters; |
|
|
|
double* vectorNew; |
|
|
|
double* vectorOld; |
|
|
@ -22,15 +35,37 @@ protected: |
|
|
|
unsigned int numberOfParameters; |
|
|
|
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: |
|
|
|
virtual ~AttractorKernel() { |
|
|
|
dealloc(); |
|
|
|
} |
|
|
|
|
|
|
|
// parameters are stored in a array of doubles
|
|
|
|
double& operator[](const unsigned int index); |
|
|
|
double const& operator[](const unsigned int index) const; |
|
|
|
unsigned int getNumberOfParameters() const; |
|
|
|
#pragma mark - |
|
|
|
#pragma mark parameters |
|
|
|
double& operator[](const unsigned int index) { |
|
|
|
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() { |
|
|
|
for(unsigned int i = 0; i < numberOfParameters; ++i) |
|
|
@ -39,7 +74,8 @@ public: |
|
|
|
vectorNew[i] = vectorOld[i] = 6.0 * rand() / double(RAND_MAX) - 3.0; |
|
|
|
} |
|
|
|
|
|
|
|
// tests
|
|
|
|
#pragma mark - |
|
|
|
#pragma mark tests |
|
|
|
bool convergent() { |
|
|
|
double sum = 0.0; |
|
|
|
for(unsigned int i = 0; i < dimension; ++i) { |
|
|
@ -56,18 +92,27 @@ public: |
|
|
|
return sum > 1.0e3; |
|
|
|
} |
|
|
|
|
|
|
|
#pragma mark - |
|
|
|
#pragma mark main |
|
|
|
// iterate his formula, implemented by subclasses
|
|
|
|
virtual void operator()() = 0; |
|
|
|
|
|
|
|
// getter functions for teh resulta (can't be used as setters)
|
|
|
|
double const* vector() const; |
|
|
|
double const* previousVector() const; |
|
|
|
unsigned int getDimension() const; |
|
|
|
#pragma mark - |
|
|
|
#pragma mark vector |
|
|
|
double const* vector() const { |
|
|
|
return vectorNew; |
|
|
|
} |
|
|
|
|
|
|
|
double const* previousVector() const { |
|
|
|
return vectorOld; |
|
|
|
} |
|
|
|
|
|
|
|
// dtor, should be virtual for subclasses to be deleted
|
|
|
|
virtual ~AttractorKernel(); |
|
|
|
unsigned int getDimension() const { |
|
|
|
return dimension; |
|
|
|
} |
|
|
|
|
|
|
|
// factory function
|
|
|
|
#pragma mark - |
|
|
|
#pragma mark factory function |
|
|
|
static AttractorKernel* createAttractorKernel(stfu::node& attractorKernel); |
|
|
|
|
|
|
|
}; |
|
|
|