Browse Source

more to headers, some cleanup

master
Joshua Moerman (joshua-desktop) 14 years ago
parent
commit
4a2a273b50
  1. 3
      Attractor.cpp
  2. 84
      AttractorKernel.hpp
  3. 52
      Projector.cpp
  4. 64
      Projector.hpp
  5. 5
      projectors/Normalizer.cpp

3
Attractor.cpp

@ -22,7 +22,8 @@ Attractor::Attractor(const std::string& filename) : kernel(0), projector(0) {
projector = Projector::createProjector(system.getChild(system.getValue("Projector")), kernel->getDimension()); projector = Projector::createProjector(system.getChild(system.getValue("Projector")), kernel->getDimension());
} }
Attractor::Attractor(){ Attractor::Attractor() :
kernel(0), projector(0) {
stfu::node kernel_node; stfu::node kernel_node;
switch(rand()%3){ switch(rand()%3){
case 0: case 0:

84
AttractorKernel.hpp

@ -8,48 +8,6 @@
#include "stfu/stf.hpp" #include "stfu/stf.hpp"
class AttractorKernel { class AttractorKernel {
private:
#pragma mark -
#pragma mark memory
void allocate() {
parameters = new double[numberOfParameters];
vectorNew = new double[dimension];
vectorOld = new double[dimension];
}
void dealloc() {
delete[] vectorOld;
vectorOld = NULL;
delete[] vectorNew;
vectorNew = NULL;
delete[] parameters;
parameters = NULL;
}
protected:
double* parameters;
double* vectorNew;
double* vectorOld;
unsigned int numberOfParameters;
unsigned int dimension;
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() { virtual ~AttractorKernel() {
dealloc(); dealloc();
@ -112,9 +70,49 @@ public:
} }
#pragma mark - #pragma mark -
#pragma mark factory function #pragma mark factory functions
static AttractorKernel* createAttractorKernel(stfu::node& attractorKernel); static AttractorKernel* createAttractorKernel(stfu::node& attractorKernel);
static AttractorKernel* randomAttractorKernel();
protected:
double* parameters;
double* vectorNew;
double* vectorOld;
unsigned int numberOfParameters;
unsigned int dimension;
AttractorKernel(const unsigned int dimension, const unsigned int numberOfParameters) :
parameters(0), vectorNew(0), vectorOld(0),
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);
}
private:
void allocate() {
parameters = new double[numberOfParameters];
vectorNew = new double[dimension];
vectorOld = new double[dimension];
}
void dealloc() {
delete[] vectorOld;
vectorOld = NULL;
delete[] vectorNew;
vectorNew = NULL;
delete[] parameters;
parameters = NULL;
}
}; };

52
Projector.cpp

@ -1,62 +1,10 @@
#include "Logger.hpp" #include "Logger.hpp"
#include "Projector.hpp" #include "Projector.hpp"
#include <algorithm>
#include "Canvas.hpp"
#pragma mark -
#pragma mark memory
Projector::Projector(unsigned int inputDimension, unsigned int outputDimension) : canvas(0), projector(0), projectedPoint(0), inputDimension(inputDimension), outputDimension(outputDimension), ready(true) {
try {
allocate();
} catch(std::exception& e) {
LogError("Couldn't construct Projector: %s\n", e.what());
deallocate();
}
std::fill_n(projectedPoint, outputDimension, 0.0);
}
Projector::~Projector() {
deallocate();
}
void Projector::allocate() {
projectedPoint = new double[outputDimension];
}
void Projector::deallocate() {
delete[] projectedPoint;
projectedPoint = NULL;
}
#pragma mark -
#pragma mark plot
void Projector::plot(const double* point) {
project(point);
if(ready) {
if(canvas != NULL) {
canvas->plot(projectedPoint);
}
if(projector != NULL) {
projector->plot(projectedPoint);
}
}
}
#pragma mark -
#pragma mark factory function
#include "projectors/Normalizer.hpp" #include "projectors/Normalizer.hpp"
Projector* Projector::createProjector(stfu::node& projector, unsigned int input_dimension) { Projector* Projector::createProjector(stfu::node& projector, unsigned int input_dimension) {
Projector* output = new Normalizer(input_dimension); Projector* output = new Normalizer(input_dimension);
return output; return output;
} }

64
Projector.hpp

@ -1,14 +1,49 @@
#ifndef PROJECTOR_HPP #ifndef PROJECTOR_HPP
#define PROJECTOR_HPP #define PROJECTOR_HPP
#include "stfu/stf.hpp" #include "Logger.hpp"
#include <algorithm>
class Canvas; #include "stfu/stf.hpp"
#include "Canvas.hpp"
class Projector { class Projector {
private: public:
void allocate(); // SHOULD NOT BE HERE
void deallocate(); Canvas* canvas;
Projector(unsigned int inputDimension, unsigned int outputDimension) :
canvas(0), projector(0), projectedPoint(0),
inputDimension(inputDimension), outputDimension(outputDimension),
ready(true) {
try {
allocate();
} catch(std::exception& e) {
LogError("Couldn't construct Projector: %s\n", e.what());
deallocate();
}
std::fill_n(projectedPoint, outputDimension, 0.0);
}
virtual ~Projector() {
deallocate();
}
void plot(const double* point) {
project(point);
if(ready) {
if(canvas != NULL) {
canvas->plot(projectedPoint);
}
if(projector != NULL) {
projector->plot(projectedPoint);
}
}
}
static Projector* createProjector(stfu::node& projector, unsigned int input_dimension);
protected: protected:
Projector* projector; Projector* projector;
@ -22,18 +57,15 @@ protected:
virtual void project(const double* point) = 0; virtual void project(const double* point) = 0;
public: private:
// SHOULD NOT BE HERE void allocate() {
Canvas* canvas; projectedPoint = new double[outputDimension];
}
Projector(unsigned int inputDimension, unsigned int outputDimension);
virtual ~Projector();
// delegates forward trough the chain, know wha i'm sayin'?
void plot(const double* point);
// factory function void deallocate() {
static Projector* createProjector(stfu::node& projector, unsigned int input_dimension); delete[] projectedPoint;
projectedPoint = NULL;
}
}; };
#endif // PROJECTOR_HPP #endif // PROJECTOR_HPP

5
projectors/Normalizer.cpp

@ -4,7 +4,10 @@
#pragma mark - #pragma mark -
#pragma mark memory #pragma mark memory
Normalizer::Normalizer(unsigned int dimension) : Projector(dimension, dimension), factor(1) { Normalizer::Normalizer(unsigned int dimension) :
Projector(dimension, dimension),
range_min(0), range_max(0), offset(0),
factor(1) {
ready = false; ready = false;
try { try {