diff options
| -rw-r--r-- | CMakeLists.txt | 3 | ||||
| -rw-r--r-- | exp_0702.cpp | 34 | ||||
| -rw-r--r-- | exp_3.cpp | 44 |
3 files changed, 62 insertions, 19 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e3ea0b..c1d5471 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,3 +67,6 @@ set_target_properties(normal_distribution PROPERTIES LINKER_LANGUAGE CXX) add_executable(normal_distribution_ex3 exp_3.cpp) set_target_properties(normal_distribution PROPERTIES LINKER_LANGUAGE CXX) + +add_executable(normal_distribution_ex0702 exp_0702.cpp) +set_target_properties(normal_distribution PROPERTIES LINKER_LANGUAGE CXX) diff --git a/exp_0702.cpp b/exp_0702.cpp new file mode 100644 index 0000000..1c74d0a --- /dev/null +++ b/exp_0702.cpp @@ -0,0 +1,34 @@ +#include <algorithm> +#include <cassert> +#include <cmath> +#include <cstdio> +#include <vector> + +constexpr size_t length = 1048576; +std::vector<unsigned long long> original_vector; + +long double finite_population_cdf(long double x) { + auto it = upper_bound(original_vector.begin(), original_vector.end(), x); + return (long double)(it - original_vector.begin()) / original_vector.size(); +} + +int main() { + + assert((length & 1) == 0); + assert(length > 1024); + + FILE *file = fopen("normal_distribution.txt", "r"); + assert(file); + for (long long i; fscanf(file, "%lld ", &i) != EOF; + original_vector.push_back(i)) + ; + fclose(file); + + assert(original_vector.size() == length); + + std::sort(original_vector.begin(), original_vector.end()); + + for (int i = length / 1024 - 1; i < length; i += (length / 1024)) { + printf("%Lf\n", finite_population_cdf(original_vector[i])); + } +}
\ No newline at end of file @@ -10,13 +10,14 @@ constexpr size_t length = 1048576; constexpr size_t sample_length = 1000; #define CALC_HEIGHT -#define CALC_CDF_DIFF +//#define CALC_CDF_DIFF +//#define INSERT_LIMIT_VALUE #ifdef CALC_HEIGHT constexpr int block_size = 2; unsigned trailing_zeroes(size_t index) { unsigned bits = 0; - unsigned x = index / block_size; + unsigned x = index; if (x) { while (x % block_size == 0) { @@ -28,12 +29,12 @@ unsigned trailing_zeroes(size_t index) { } unsigned int getHeight(long loc) { - if (loc % block_size == 0) { - auto level = trailing_zeroes(loc) + 1; - return level - 1; - } + // if (loc % block_size == 0) { + auto level = trailing_zeroes(loc) + 1; + return level; + //} - return 0; + // return 1; } #else unsigned int getHeight(long loc) { return loc; } @@ -47,6 +48,10 @@ void initialize(const std::vector<long long> &input_population, std::sort(population_vector.begin(), population_vector.end()); sample_vector = input_sample; std::sort(sample_vector.begin(), sample_vector.end()); +#ifdef INSERT_LIMIT_VALUE + sample_vector.insert(sample_vector.cbegin(), *population_vector.begin()); + sample_vector.push_back(*(population_vector.end() - 1)); +#endif } long double sample_cdf(long double x) { @@ -64,7 +69,7 @@ long double sample_cdf(long double x) { } int infer_offset(long double x) { - return int(sample_cdf(x) * double(population_vector.size())); + return (int)std::round(sample_cdf(x) * double(population_vector.size())); } #ifdef CALC_CDF_DIFF int infer_offset_from_cdf(long double cdf) { @@ -89,19 +94,15 @@ int main() { std::vector<long long> original_vector; - std::set<long long> set1, set2; - - typedef std::tuple<int, int, long double> element_type; - - std::vector<element_type> location; + std::set<long long> set2; FILE *file = fopen("normal_distribution.txt", "r"); + assert(file); for (long long i; fscanf(file, "%lld ", &i) != EOF; - original_vector.push_back(i), set1.insert(i)) + original_vector.push_back(i)) ; fclose(file); - assert(original_vector.size() == set1.size()); assert(original_vector.size() >= length); auto it = original_vector.begin(); @@ -127,11 +128,16 @@ int main() { getHeight(iter - population_vector.begin()), std::abs(cdf - stat_cdf(*iter))); #else - printf("%d,%d ", getHeight(infer_offset(*iter)), - getHeight(iter - population_vector.begin())); + auto index = infer_offset(*iter); + printf("%d,%d,%d,%ld\n", getHeight(index), index, + getHeight(iter - population_vector.begin() + 1), + iter - population_vector.begin() + 1); #endif - fflush(stdout); + // fflush(stdout); } - puts(""); + // puts(""); + // printf("%lld, %lld\n", sample_vector[1], *(sample_vector.end() - 2)); + // printf("%lld, %lld\n", population_vector[0], *(population_vector.end() - + // 1)); return 0; }
\ No newline at end of file |
