aboutsummaryrefslogtreecommitdiff
path: root/exercise-02.1.c
blob: 585357ef168137d1782045f8349400e6544c9150 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright ©️ 2024 Mario Forzanini <mf@marioforzanini.com>
 *
 * This file is part of my exercises for LSN.
 *
 * My exercises for LSN are free software: you can redistribute them
 * and/or modify them under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * My exercises for LSN are distributed in the hope that they will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with my exercises for LSN. If not, see
 * <https://www.gnu.org/licenses/>.
 *
 */

#include <errno.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

#include "random.h"
#include "std.h"

/* Total number of "throws" of the MonteCarlo algorithm. */
#define TOTAL 10000
/* Argument to select uniform sampling */
#define UNIFORM_NAME "UNIFORM"
/* Argument to select importance sampling */
#define IMPORTANCE_NAME "IMPORTANCE"

enum method {
	METHOD_UNKNOWN,
	METHOD_UNIFORM,
	METHOD_IMPORTANCE,
};

const double M_PI_SQUARE = M_PI * M_PI;

#define M_PI_2 (M_PI / 2.0)

double
integrand(double x)
{
	return M_PI_2 * cos(M_PI_2 * x);
}

double
d(double x)
{
	return 3. / 2. * (1 - x * x);
}

double
sample(Random rnd[static 1])
{
	double x, r;
	do {
	        x = random_rannyu(rnd);
	        r = random_rannyu(rnd);
	} while (r >= 1 - x * x);
	return r;
}

void
usage(char *argv0)
{
	fprintf(stderr, "USAGE: %s [UNIFORM|IMPORTANCE]\n", argv0);
	exit(1);
}

/* Numbers of blocks to be tested. */
static int Ns[] = { 5, 10, 20, 50, 100, 200 };

int
main(int argc, char *argv[])
{
	FILE *primes, *input;
	Random rnd = { 0 };
	if (!(primes = fopen("Primes", "r"))) {
		STD_ERROR("Error opening Primes: %s\n", strerror(errno));
		return 1;
	} else if (!(input = fopen("seed.in", "r"))) {
		STD_ERROR("Error opening seed.in: %s\n", strerror(errno));
		return 1;
	}
	if (!random_init(&rnd, primes, input)) {
		return 1;
	}
	enum method method = METHOD_UNKNOWN;
	if (argc < 2) {
		usage(argv[0]);
	} else {
		if (strcmp(argv[1], UNIFORM_NAME) == 0) {
			method = METHOD_UNIFORM;
		} else if (strcmp(argv[1], IMPORTANCE_NAME) == 0) {
			method = METHOD_IMPORTANCE;
		}
	}
	for (size_t i = 0; i < LEN(Ns); i++) {
		int nblock = Ns[i];
		int block_steps = TOTAL / nblock;
		double avg = 0.0, avg2 = 0.0, sum = 0.0;
		if (method == METHOD_IMPORTANCE) {
			for (int block = 0; block < nblock; block++) {
				for (int step = 0; step < block_steps;
				        step++) {
					double x = sample(&rnd);
					sum += integrand(x) / d(x);
				}
				sum /= (double)(block_steps);
				avg += sum;
				avg2 += sum * sum;
			}
		} else if (method == METHOD_UNIFORM) {
			for (int block = 0; block < nblock; block++) {
				for (int step = 0; step < block_steps;
				        step++) {
					sum += integrand(random_rannyu(&rnd));
				}
				sum /= (double)(block_steps);
				avg += sum;
				avg2 += sum * sum;
			}
		}
		avg /= (double)(nblock);
		printf("%d %lf %lf\n", nblock, avg,
		        sqrt(avg2 / (double)(nblock)-avg * avg));
	}
	random_save_seed(&rnd, "seed.out");
	return 0;
}