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 "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/Lorenz3D.hpp"
#include "kernels/Polynomial.hpp"

91
AttractorKernel.hpp

@ -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,35 +74,45 @@ public:
vectorNew[i] = vectorOld[i] = 6.0 * rand() / double(RAND_MAX) - 3.0;
}
// tests
bool convergent(){
#pragma mark -
#pragma mark tests
bool convergent() {
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]);
}
return sum < 1.0e-6;
}
bool divergent(){
bool divergent() {
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]);
}
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);
};