Browse Source

better code

master
Joshua Moerman 13 years ago
parent
commit
21a873e909
  1. 32
      aatester.cpp
  2. 29
      analyse.hpp

32
aatester.cpp

@ -20,24 +20,21 @@
int verbose = 0; int verbose = 0;
Logger logger(std::cout, LOG_VERBOSE); Logger logger(std::cout, LOG_VERBOSE);
inline double test(const int dimension, const double doubling, const double percentage){ inline double test(const unsigned int dimension){
LOG("making vector", LOG("making vector",
srand(time(0)); std::vector<double *> buffer(10000000);
std::vector<double *> buffer(1000000);
); );
LOG("making random data", LOG("making random data",
for(auto & p : buffer){ for(auto & p : buffer){
p = new double[dimension]; p = new double[dimension];
for(unsigned int i = 2;i < dimension; ++i){ for(unsigned int i = 0;i < dimension; ++i){
p[i] = rand() / (double) RAND_MAX - 0.5; p[i] = rand() / (double) RAND_MAX - 0.5;
} }
p[0] = p[1]*p[1];
p[1] = std::sin(5.0*p[2]);
}); });
LOG("calculate fractal dimension", LOG("calculate fractal dimension",
double f = fractal_dimension(buffer.begin(), buffer.end(), dimension, doubling, percentage); double f = fractal_dimension(buffer.begin(), buffer.end(), dimension);
); );
LOG("freeing data", LOG("freeing data",
@ -50,22 +47,11 @@ inline double test(const int dimension, const double doubling, const double perc
} }
int main(){ int main(){
double percentages[] = {0.00015, 0.00010, 0.00006, 0.00004, 0.000015}; srand(time(0));
std::cout << "... |"; for (unsigned int i = 1; i <= 6; ++i) {
for(auto p : percentages) std::cout << "\t" << p; std::cout << i << " =>";
std::cout << std::endl << "----------------------------------------------" << std::endl; for(unsigned int j = 0; j < 10; ++j) {
std::cout << "\t" << test(i);
for (double doubling = 2.0; doubling <= 3.0; doubling += 0.1){
std::cout << doubling << " |";
for (auto p : percentages){
double sse = 0;
for (int i = 3; i <= 6; ++i) {
for(int j = 0; j < 20; ++j) {
double e = (i-2) - test(i, doubling, p);
sse += e*e;
}
}
std::cout << "\t" << sse;
} }
std::cout << std::endl; std::cout << std::endl;
} }

29
analyse.hpp

@ -19,29 +19,42 @@ inline double distance_squared(double * p1, double * p2, size_t dimension){
return s; return s;
} }
struct fractal_dimension_settings{
double doubling;
double size;
size_t chunk_size;
fractal_dimension_settings()
: doubling(2.4)
, size(0.035)
, chunk_size(200)
{}
};
template <typename Iterator> template <typename Iterator>
double fractal_dimension(Iterator begin, Iterator end, size_t const dimension, const double doubling = 2.4, const double percentage = 0.00004) { double fractal_dimension(Iterator begin, Iterator end, size_t const dimension, const fractal_dimension_settings settings = fractal_dimension_settings() ) {
const double size = std::pow(percentage, 1.0 / 3.0); const double size = settings.size;
//const double size = std::pow(percentage, 1.0 / dimension);
const double totalSize = std::sqrt(dimension); const double totalSize = std::sqrt(dimension);
const double r1 = size * totalSize * size * totalSize;
const double r2 = r1 * settings.doubling * settings.doubling;
unsigned int counts[2] = {}; unsigned int counts[2] = {};
double r = size * totalSize;
while(begin != end){ while(begin != end){
auto x0 = *begin++; auto x0 = *begin++;
for(unsigned int i = 0; i < 200 && begin != end; ++i){ for(unsigned int i = 0; i < settings.chunk_size && begin != end; ++i){
auto x = *begin++; auto x = *begin++;
auto d = distance_squared(x, x0, dimension); auto d = distance_squared(x, x0, dimension);
if( d <= r*r ) if( d <= r1 )
++counts[0]; ++counts[0];
if( d <= doubling*doubling*r*r ) if( d <= r2 )
++counts[1]; ++counts[1];
} }
} }
return std::log((double) counts[1] / (double) counts[0]) / std::log(doubling); return std::log((double) counts[1] / (double) counts[0]) / std::log(settings.doubling);
} }
#endif #endif