My old project for strange attractors
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 

145 lines
3.5 KiB

//
// $filename
// $projectname
//
// Created by Joshua moerman on $TODAY.
// Copyright 2010 Joshua Moerman. All rights reserved.
//
#include "Polynomial.hpp"
using namespace std;
Polynomial::Polynomial():
myParameters(NULL), vectorNew(NULL), vectorOld(NULL) {
dimension = 3;
orde = 2;
init();
}
Polynomial::Polynomial(const unsigned int dimension, const unsigned int orde):
myParameters(NULL), vectorNew(NULL), vectorOld(NULL), dimension(dimension), orde(orde) {
init();
}
void Polynomial::init() {
calculateNumberOfParameters();
if ( myParameters != NULL ) {
delete myParameters;
}
myParameters = new double[numberOfParameters];
if ( vectorNew != NULL ) {
delete vectorNew;
}
vectorNew = new double[dimension];
if ( vectorOld != NULL ) {
delete vectorOld;
}
vectorOld = new double[dimension];
assert(myParameters != NULL);
assert(vectorNew != NULL);
assert(vectorOld != NULL);
for ( unsigned int i = 0; i < numberOfParameters; i++ ) {
myParameters[i] = 0.0;
}
for ( unsigned int i = 0; i < dimension; i++ ) {
vectorNew[i] = vectorOld[i] = 0.0;
}
}
void Polynomial::calculateNumberOfParameters() {
double n_coef = orde + 1;
for (unsigned int i = 2; i <= dimension; i++) {
n_coef = n_coef*(orde + i)/(i - 1);
}
numberOfParameters = (unsigned int) n_coef;
cout << numberOfParameters << endl;
}
void Polynomial::iterate() {
swap(vectorNew, vectorOld);
unsigned int m = 0;
for ( unsigned int i = 0; i < dimension; i++ ) {
#ifdef HARDDEBUG
cout << "Entering new dimension: " << i << " With m = " << m << endl;
#endif
vectorNew[i] = myParameters[m];
m++;
recur(i, 0, 1, m);
}
}
void Polynomial::recur(unsigned int curr_dimension, unsigned int prev_i, unsigned int n, unsigned int& m, double prev_product) {
double product;
for (unsigned int i = prev_i; i < dimension; i++) {
#ifdef HARDDEBUG
for ( unsigned int j = 0; j < n; j++ )
cout << " ";
cout << "Calculation in dimension: " << i << " With m = " << m << " And depth = " << n << endl;
#endif
product = prev_product * vectorOld[i];
vectorNew[curr_dimension] += myParameters[m] * product;
m++;
if (n < orde) {
recur(curr_dimension, i, n+1, m, product);
}
}
}
// setters, getters, all i/o to other classes/objects
void * Polynomial::getProperty(const string identifier) {
if ( identifier == "dimension" ) {
unsigned int * _return = new unsigned int;
*_return = dimension;
return _return;
} else if ( identifier == "orde" ) {
unsigned int * _return = new unsigned int;
*_return = orde;
return _return;
}
return NULL;
}
void Polynomial::setProperty(const string identifier, const void * _value) {
if ( identifier == "dimension" ) {
unsigned int * value = (unsigned int*) _value;
dimension = *value;
init();
} else if ( identifier == "orde" ) {
unsigned int * value = (unsigned int*) _value;
orde = *value;
init();
}
}
double * & Polynomial::parameters() {
return myParameters;
}
double & Polynomial::parameter(const unsigned int index) {
return myParameters[index];
}
unsigned int Polynomial::getNumberOfParameters() {
return numberOfParameters;
}
double * & Polynomial::vector() {
return vectorNew;
}
double * & Polynomial::previousVector() {
return vectorOld;
}