diff options
| author | Tyge Løvset <[email protected]> | 2020-08-31 23:43:32 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2020-08-31 23:43:32 +0200 |
| commit | 8c4daa7f91d9a6e7a30e0f6dfdafaf2664d68f2d (patch) | |
| tree | dc3e7a19038f29ac905bbc938cacda2f9b5b8bb6 /examples | |
| parent | 55c84dee6cce33e31a4ca3e0cdcbdbe85fcccac6 (diff) | |
| download | STC-modified-8c4daa7f91d9a6e7a30e0f6dfdafaf2664d68f2d.tar.gz STC-modified-8c4daa7f91d9a6e7a30e0f6dfdafaf2664d68f2d.zip | |
Added example gaussian. Some cleanup.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/ex_gaussian.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/examples/ex_gaussian.c b/examples/ex_gaussian.c new file mode 100644 index 00000000..62ab7994 --- /dev/null +++ b/examples/ex_gaussian.c @@ -0,0 +1,58 @@ +#include <stdio.h>
+#include <time.h>
+#include <math.h>
+#include <stc/crandom.h>
+#include <stc/cstr.h>
+#include <stc/cmap.h>
+#include <stc/cvec.h>
+
+// Declare int -> int hashmap. Uses typetag 'i' for ints.
+declare_cmap(i, int, size_t);
+
+// Declare int vector with map entries that can be sorted by map keys.
+static int compare(cmap_i_entry_t *a, cmap_i_entry_t *b) {
+ return c_default_compare(&a->key, &b->key);
+}
+// Vector: typetag 'e' for (map) entry
+declare_cvec(e, cmap_i_entry_t, c_default_destroy, compare);
+
+int main()
+{
+ enum {Rows = 40, N = 5000000};
+ const double StdDev = 6.0, Mag = 35.0, Mean = 12;
+
+ printf("Demo of gaussian / normal distribution of %d random samples\n", N);
+
+ // Setup random engine with normal distribution.
+ uint64_t seed = time(NULL);
+ crand_rng64_t rng = crand_rng64_init(seed);
+ crand_normal_f64_t dist = crand_normal_f64_init(rng, Mean, Rows / StdDev);
+
+ // Create histogram map
+ cmap_i mhist = cmap_init;
+ for (size_t i = 0; i < N; ++i) {
+ int index = round( crand_normal_f64(&dist) );
+ ++ cmap_i_insert(&mhist, index, 0)->value;
+ }
+
+ // Transfer map to vec and sort it by map keys.
+ cvec_e vhist = cvec_init;
+ c_foreach (i, cmap_i, mhist)
+ cvec_e_push_back(&vhist, *i.item);
+ cvec_e_sort(&vhist);
+
+ // Print the gaussian bar chart
+ cstr_t bar = cstr_init;
+ c_foreach (i, cvec_e, vhist) {
+ size_t n = (size_t) (i.item->value * Mag * Rows / N);
+ if (n > 0) {
+ // bar string: take ownership in new str after freeing current.
+ cstr_take(&bar, cstr_with_size(n, '*'));
+ printf("%4d %s\n", i.item->key, bar.str);
+ }
+ }
+ // Cleanup
+ cstr_destroy(&bar);
+ cmap_i_destroy(&mhist);
+ cvec_e_destroy(&vhist);
+}
\ No newline at end of file |
