aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Weigt <juw@posteo.de>2026-02-09 18:51:46 +0100
committerJulian Weigt <juw@posteo.de>2026-02-09 19:13:37 +0100
commit15cb5ccbbee7abb8ce253632bc2c4febdfaedca5 (patch)
tree6c335dac9be90fed6e57e6b0e471f928872fb094
parent3397d3040a63d3afd9392fa465c8b6e1380dce71 (diff)
Add more comments to code.
-rw-r--r--charf.c15
-rw-r--r--double-error.c3
-rw-r--r--makefile9
-rw-r--r--ratio.c2
4 files changed, 25 insertions, 4 deletions
diff --git a/charf.c b/charf.c
index fad2938..e9daaf3 100644
--- a/charf.c
+++ b/charf.c
@@ -110,7 +110,7 @@ void compute_maximalfunction(VALUETYPE* f, VALUETYPE* Mf, int D){
/*Because it simplifies code later on we also assign the average on twice the circle, starting in 0.*/
Af[0][2*D] = Af[0][D];
- /*Finding the maximal average actually looks like the most costly computation, in the whole algorithm, being of order D^3.
+ /*Finding the maximal average actually looks like the most costly computation, in the whole algorithm, being of order D^2.
Hence we put effort into making it efficient.
The strategy is to go trough all possible intervals, starting with those of largest length.
For each [i,i+l) that we encounter we check for each n∈[i,i+l) if the average on [i,i+l) beats the current best average that we have so far computed for intervals containing n.
@@ -273,6 +273,7 @@ int generate_function(VALUETYPE* f, index_t i){
#define FORMAT_TEXT 0
#define FORMAT_LATEX 1
+/*Write into pointer s the function corresponding to an index, and that the ratio of the L^p norms of the kth derivative of the maximal function and the function equals r.*/
void format_result(char* s, index_t index, int k, EXPTYPE p, VALUETYPE r, int format){
VALUETYPE f[N];
int d = generate_function(f,index);
@@ -340,6 +341,8 @@ void format_result(char* s, index_t index, int k, EXPTYPE p, VALUETYPE r, int fo
}
}
+/*Given an index compute the ratio of the L^p norms of derivatives up to order k of the maximal function and the function for a given range of exponents p.
+If for any order of derivative or exponent p the latest record for that ratio is broken, update the value of the record and the index of the witnessing function.*/
int compute(index_t index, EXPTYPE exponents[P], VALUETYPE (*records_ratio)[K+1][P], index_t (*records_index)[K+1][P]){
VALUETYPE f[N];
int D = generate_function(f,index);
@@ -407,6 +410,7 @@ int compute(index_t index, EXPTYPE exponents[P], VALUETYPE (*records_ratio)[K+1]
return D;
}
+/*Save formatted string of current records into pointer text. Each entry is formatted using format_result.*/
void format_results(char* text, EXPTYPE exponents[P], VALUETYPE ratios[K+1][P], index_t indeces[K+1][P], int format){
char beginning[1024];
char end[1024];
@@ -481,9 +485,10 @@ void print_latex_records_to_file(EXPTYPE exponents[P], VALUETYPE ratios[K+1][P],
fclose(fptr);
}
+/*information given to each thread*/
typedef struct { EXPTYPE exponents[P]; VALUETYPE (*records_ratio)[K+1][P]; index_t (*records_index)[K+1][P]; int num_thread; int* domain_current; int* cont; } Args;
-/*Goes through all functions indexed by those numbers which equal the thread number module NUM_THREADS.*/
+/*Goes through all functions indexed by those numbers which equal the thread number mod NUM_THREADS.*/
void* compute_chunk(void* arguments){
Args* args = arguments;
int i = args -> num_thread;
@@ -518,6 +523,7 @@ int main() {
int domain_current = 0;
int cont = 0;
+ /*Start threads that do the computation.*/
for (int i = 0; i < NUM_THREADS; i++) {
printf("In main: Creating thread %d.\n", i);
memcpy(args[i].exponents,exponents,sizeof exponents);
@@ -529,6 +535,7 @@ int main() {
pthread_create(&threads[i], NULL, compute_chunk, args+i);
}
+ /*Capture user input.*/
size_t sz = 1024;
char prmpt[] = "To exit enter q, quit or exit.\nTo print current records to terminal enter r.\nTo print current records to a file enter p.\nTo do so with tex formatting enter t.\n";
char buff[sz];
@@ -543,9 +550,9 @@ int main() {
getLine("Enter file name:\n", buff, sz);
print_latex_records_to_file(exponents,records_ratio,records_index,buff);
}
- } while(cont < NUM_THREADS);
+ } while(cont < NUM_THREADS); //Stop once all threads have finished.
- // wait for each thread to complete
+ /*Wait for each thread to complete.*/
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
printf("In main: Thread %d has ended.\n", i);
diff --git a/double-error.c b/double-error.c
index 24b4526..dfbb34e 100644
--- a/double-error.c
+++ b/double-error.c
@@ -102,6 +102,7 @@ double_error power_int(double_error de, double_error p){
double_error power(double_error de, double_error p) {
//if(p.e == 0.0 && ((vtype) ((int) p.v) == p.v)) return power_int(de,p);
+ /*Avoid pow function if exponent equals one because I don't know certain error bounds for pow function.*/
if(p.e == 0.0 && ((vtype) ((int) p.v) == p.v) && (int) p.v == 1) return de;
double_error dep;
dep.v = pow(de.v,p.v);
@@ -131,6 +132,7 @@ double exptype_to_double(double_error de){ return de.v; }
int valuetype_to_string(char* s, double_error de){
if(de.e == 0.0){
+ /*If number really is an int, print accordingly to give cleaner output for example when printing the function itself, which often will be integer valued without errors.*/
if((vtype) ((int) de.v) == de.v) sprintf(s,"%d", (int)de.v);
else sprintf(s,"%4.3f…",de.v);
}
@@ -145,6 +147,7 @@ int exptype_to_string(char* s, double_error de){
int valuetype_to_latex(char* s, double_error de){
if(de.e == 0.0){
+ /*If number really is an int, print accordingly to give cleaner output for example when printing the function itself, which often will be integer valued without errors.*/
if((vtype) ((int) de.v) == de.v) sprintf(s,"%d", (int)de.v);
else sprintf(s,"%4.3f\\ldots",de.v);
}
diff --git a/makefile b/makefile
index a55b47f..fcdc6fa 100644
--- a/makefile
+++ b/makefile
@@ -3,14 +3,23 @@
CFILES = charf.c misc.h misc.c
CDEPS = $(CFILES) misc.h
CFLAGS = -lm -pthread -O3 -march=native -pipe -pedantic -Wextra#-Wall
+# -lm: link math
+# -pthread: use multithreading
+# -O3: most aggressive optimization of binary
+# -pipe: use RAM instead of disk for temporary files
+# -pedantic:
+# -Wextra: more warnings
all: charf_approx charf_exact charf_error
+# use floating point computations
charf_approx: $(CDEPS) double.h double.c
gcc -o $@ -D MODE=0 $(CFILES) double.c $(CFLAGS)
+# use floating point computations with error bounds
charf_error: $(CDEPS) double-error.h double-error.c
gcc -o $@ -D MODE=1 $(CFILES) double-error.c $(CFLAGS)
+# use fractions
charf_exact: $(CDEPS) ratio.h ratio.c
gcc -o $@ -D MODE=2 $(CFILES) ratio.c $(CFLAGS)
diff --git a/ratio.c b/ratio.c
index 6d0b59d..f8bfeb0 100644
--- a/ratio.c
+++ b/ratio.c
@@ -9,6 +9,7 @@ typedef unsigned long long num;
typedef struct {bool s; num n; num d;} rational;
+/*safe_sum and safe_product abort if computations would exceed maximal value allowd for unsigned long long and overflow.*/
num safe_sum(num n1, num n2){
if(n2 > ULLONG_MAX-n1){
printf("Sum overflow: Adding %llu and %llu\n",n1,n2);
@@ -107,6 +108,7 @@ rational product(rational r1, rational r2){
rational r;
rational s1 = {r1.s, r1.n, r2.d};
rational s2 = {r2.s, r2.n, r1.d};
+ /*Cancel as early as possible to avoid exceeding maximal value.*/
rational t1 = cancel(s1);
rational t2 = cancel(s2);
r.s = t1.s^t2.s;