aboutsummaryrefslogtreecommitdiff
path: root/exercise-02.2.c
blob: dbeca8f6833eb339da3a41c0aed1a3f454ad3d9f (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * 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"

#define TOTAL 10000
#define NBLOCK 100
#define BLOCK_THROWS (TOTAL / NBLOCK)
#define NSTEPS 100
#define DISCRETE_NAME "DISCRETE"
#define CONTINUOUS_NAME "CONTINUOUS"

typedef struct Vector3 Vector3;
typedef struct Polar Polar;
struct Vector3 {
	double x, y, z;
};
struct Polar {
	double theta, phi;
};
enum direction {
	DIRECTION_MX = 0, /* (-1,  0,  0) */
	DIRECTION_MY,     /* ( 0, -1,  0) */
	DIRECTION_MZ,     /* ( 0,  0, -1) */
	DIRECTION_X,      /* ( 1,  0,  0) */
	DIRECTION_Y,      /* ( 0,  1,  0) */
	DIRECTION_Z,      /* ( 0,  0,  1) */
};
enum method {
	METHOD_UNKNOWN = 0,
	METHOD_DISCRETE,
	METHOD_CONTINUOUS,
};

static Vector3 unit_vectors[] = {
	{ -1, 0, 0 },
	{ 0, -1, 0 },
	{ 0, 0, -1 },
	{ 1, 0, 0 },
	{ 0, 1, 0 },
	{ 0, 0, 1 },
};

enum direction
random_direction(Random rnd[static 1])
{
	double x = random_rannyu(rnd);
	return (enum direction)(floor(x / 6.));
}

Polar
random_polar(Random rnd[static 1])
{
	return (Polar) {
		.phi = random_rannyu_range(rnd, 0, 2. * M_PI),
		.theta = acos(1 - 2 * random_rannyu(rnd)),
	};
}

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

static double avgs[NSTEPS] = { 0 }, avgs2[NSTEPS] = { 0 };

int
main(int argc, char *argv[])
{
	FILE *primes, *input;
	Random rnd = { 0 };
	enum method method = METHOD_UNKNOWN;
	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;
	}
	if (argc < 2) {
		usage(argv[0]);
	} else {
		if (strcmp(argv[1], DISCRETE_NAME) == 0) {
			method = METHOD_DISCRETE;
		} else if (strcmp(argv[1], CONTINUOUS_NAME) == 0) {
			method = METHOD_CONTINUOUS;
		} else {
			STD_ERROR("Unknown method: %s\n", argv[1]);
			usage(argv[0]);
		}
	}
	switch (method) {
	case METHOD_DISCRETE: {
		/* FIXME(mario): I did not understand this... */
		for (int steps = 0; steps < NSTEPS; steps++) {
			for (int block = 0; block < NBLOCK; block++) {
				double rmean_square = 0.0;
				Vector3 r = { 0 };
				for (int curr_throw = 0;
				        curr_throw < BLOCK_THROWS;
				        curr_throw++) {
					for (int step = 0; step < steps;
					        step++) {
						Vector3 dr = unit_vectors
						        [random_direction(
						                &rnd)];
						r.x += dr.x;
						r.y += dr.y;
						r.z += dr.z;
					}
					rmean_square += (r.x * r.x + r.y * r.y
					        + r.z * r.z);
				}
				rmean_square /= (double)(BLOCK_THROWS);
				avgs[steps] += rmean_square;
				avgs2[steps] += (rmean_square * rmean_square);
			}
			avgs[steps] /= (double)(NBLOCK);
			printf("%lf %lf\n", avgs[steps],
			        sqrt(avgs2[steps]
			                / (double)(NBLOCK)-avgs[steps]
			                * avgs[steps]));
		}
	} break;
	case METHOD_CONTINUOUS: {
		STD_TODO("Implement continuous RW.\n");
	} break;
	default:
		__builtin_unreachable();
	}
	random_save_seed(&rnd, "seed.out");
	return 0;
}