squiggle.c

Self-contained Monte Carlo estimation in C99
Log | Files | Refs | README

example.c (933B)


      1 #include "../../../squiggle.h"
      2 #include "../../../squiggle_more.h"
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 
      6 double sample_0(uint64_t* seed)
      7 {
      8     UNUSED(seed);
      9     return 0;
     10 }
     11 
     12 double sample_1(uint64_t* seed)
     13 {
     14     UNUSED(seed);
     15     return 1;
     16 }
     17 
     18 double sample_normal_mean_1_std_2(uint64_t* seed)
     19 {
     20     return sample_normal(1, 2, seed);
     21 }
     22 
     23 double sample_1_to_3(uint64_t* seed)
     24 {
     25     return sample_to(1, 3, seed);
     26 }
     27 
     28 int main()
     29 {
     30     // set randomness seed
     31     uint64_t* seed = malloc(sizeof(uint64_t));
     32     *seed = 1000; // xorshift can't start with 0
     33 
     34     int n_dists = 4;
     35     double weights[] = { 1, 2, 3, 4 };
     36     double (*samplers[])(uint64_t*) = {
     37         sample_0,
     38         sample_1,
     39         sample_normal_mean_1_std_2,
     40         sample_1_to_3
     41     };
     42 
     43     int n_samples = 10;
     44     for (int i = 0; i < n_samples; i++) {
     45         printf("Sample #%d: %f\n", i, sample_mixture(samplers, weights, n_dists, seed));
     46     }
     47 
     48     free(seed);
     49 }