summaryrefslogtreecommitdiff
path: root/charf.c
blob: 0508cc4d918adc2433225f4b1937d88b7a1d93c9 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//for multithreading
#include <pthread.h>

#define DOUBLEMODE 0
#define DOUBLEERRORMODE 1
#define RATIOMODE 2

#ifndef MODE
#define MODE DOUBLEMODE
#endif

#define NUM_THREADS 6

#if MODE == DOUBLEERRORMODE
#include "double-error.h"
#define VALUETYPE double_error
#define EXPTYPE double_error
#elif MODE == RATIOMODE
#include "ratio.h"
#define VALUETYPE rational
#define EXPTYPE unsigned int
#else
#include "double.h"
#define VALUETYPE double
#define EXPTYPE double
#endif

/*length of the support of f*/
static const int N=16;

/*order of the derivative to consider.*/
static const int K=3;

/*given function df[0] on domain [0,M-1], compute derivatives f' until f^{(K)} and store them in df[1] to df[K]*/
void differentiate(VALUETYPE* f, VALUETYPE* df, int D){
	VALUETYPE df0[D];
	/*Set zeroth derivative to be f.*/
	for(int i=0; i<D; i++){
		df0[i] = f[i];
		df[i] = f[i];
	}
	for(int k=1; k<=K; k++){
		/*Compute kth derivative of f from (k-1)th.*/
		for(int i=0; i<D; i++) df[i] = difference(df0[(i+1)%D], df0[i]);
		for(int i=0; i<D; i++) df0[i] = df[i];
	}

	/*
	printf("df    ");
	for(int i=0;i<D;i++) printf("%+6.1f ",to_double(df[i]));
	printf("\n");
	*/
}

/*given function f on domain [0,D-1] compute pth root of integral of |f|^p*/
VALUETYPE integratep(VALUETYPE* f, EXPTYPE p, int D){
	VALUETYPE integralp = convert_int(0);
	for(int i=0;i<D;i++){
		VALUETYPE padd = power(absolute(f[i]),p);
		integralp = sum(integralp,padd);
	}
	return integralp;
}

void compute_maximalfunction(VALUETYPE* f, VALUETYPE* Mf, int D){
	/*Sf[i][j] will be the integral of f on [min(i,j),max(i,j)]*/
	//VALUETYPE Sf[N][N];
	/*Af[i][j] will be the average of f on [min(i,j),max(i,j)]*/
	//VALUETYPE Af[N][N];
	/*Apparently may become too big for stack or something.*/
	VALUETYPE* Sf[N];
	VALUETYPE* Af[N];
	for(int i=0; i<D;i++){
		Sf[i] = malloc(N*sizeof(VALUETYPE));
		Af[i] = malloc(N*sizeof(VALUETYPE));
	}

	for(int i=0; i<D; i++) {
		Sf[i][i] = f[i];
		Af[i][i] = f[i];
	}
	
	for(int n=1; n<D; n++){
		/*Recursively compute all integrals and averages over intervals of increasing length*/
		for(int i=0; i<D; i++){
			Sf[i][(i+n)%D] = sum(Sf[i][(i+n-1)%D], f[(i+n)%D]);
			Af[i][(i+n)%D] = ratio(Sf[i][(i+n)%D],convert_int(n+1));
		}
	}
	
	/*Compute maximal function by picking the largest average*/
	for(int i=0; i<D; i++){
		Mf[i] = Af[i][i];
		for(int j=1; j<=D; j++){
			Mf[i] = maximum(Af[i][(i+j)%D], Mf[i]);
			Mf[i] = maximum(Af[(i-j+D)%D][i], Mf[i]);
		}
	}

	/*Print computed functions and averages*/
	/*
	printf("Mf    ");
	for(int i=0;i<D;i++) printf("%+0.1f ",to_double(Mf[i]));
	printf("\n");
	for(int i=0;i<D;i++){
		for(int j=0;j<D;j++) printf("%0.1f ",to_double(Af[i][j]));
		printf("\n");
	}
	*/

	for(int i=0; i<D; i++){
		free(Sf[i]);
		free(Af[i]);
	}
}

void compute(EXPTYPE p, int D, VALUETYPE* f){
	VALUETYPE Mf[N];
	compute_maximalfunction(f,Mf,D);

	/*Allocate memory for derivatives.*/
	VALUETYPE df[N];
	VALUETYPE dMf[N];

	/*Compute Kth derivative of f and Mf*/
	differentiate(f,df,D);
	differentiate(Mf,dMf,D);

	/*Print derivatives*/
	/*
	for(int k=0; k<=K; k++){
		printf("f  %d: ",k);
		for(int i=0; i<D; i++) printf("%+0.1f ",df[k][i]);
		printf("\n");
		printf("Mf %d: ",k);
		for(int i=0; i<D; i++) printf("%+0.1f ",dMf[k][i]);
		printf("\n");
	}
	*/

	/*Compute L^p norm of derivatives*/
	VALUETYPE intdfp = integratep(df,p,D);
	VALUETYPE intdMfp = integratep(dMf,p,D);

	//printf("%d: %f / %f = %f\n",k,intdMfp[k],intdfp[k],intdMfp[k]/intdfp[k]);

	/*Compute ||Mf^{(k)}||_p/||f^{(k)}||_p.*/

	VALUETYPE r = ratio(intdMfp, intdfp);

	//printf("%.3d: %.3f  \n",t,r);
	/*Print f and ||Mf^{(k)}||_p/||f^{(k)}||_p if the latter is close to 1/2.*/
	//if(to_double(r)>.4997)
	//if(to_double(r)>=.58)
	if(to_double(r)>.53)
	{
		printf("f: ");
		for(int i=0; i<D; i++) printf("%1.0f ",to_double(f[i]));
		printf("\n");
		char s[128];
		root_to_string(s,r,p);
		printf("%s\n",s);
	}
}

/*Writes into f the values of the function indexed by i. Returns the size of the support of f, or -1 if there is no function with that index.*/
int generate_function(VALUETYPE* f, int i){
	int s=0;
	int d=1;
	while(d <= N){
		/*number of strings of length d that are not all 0s or all 1s*/
		int powd = (1<<d)-2;
		if(i-s >= powd){
			s += powd;
			d++;
		}
		else {
			int t = i-s+1;
			/*Set f to the values encoded in bit string t which is a value between 1 and powd = (1<<d)-2.*/
			for(int n=0; n<d; n++) f[n] = convert_int((t >> n) & 1);
			return d;
		}
	}
	return -1;

}

typedef struct {
	EXPTYPE p;
	VALUETYPE** f;
	int i; 
}Args;

/*Goes through all functions indexed by those numbers which equal the thread number module NUM_THREADS.*/
void* compute_chunk(void* arguments){
	Args *args = arguments;
	EXPTYPE p = args->p;
	int i = args->i;

	VALUETYPE f[N];
	int d = generate_function(f,i);
	while(d >= 0){
		compute(p,d,f);
		i += NUM_THREADS;
		d = generate_function(f,i);
	}	
}

int main() {
	/*exponent p of the L^p norm to consider*/
	EXPTYPE p = to_exptype(1);

	pthread_t threads[NUM_THREADS];
	Args args[NUM_THREADS];
	int result_code;

	for (int i = 0; i < NUM_THREADS; i++) {
		printf("In main: Creating thread %d.\n", i);
		args[i] = (Args){.p=p, .i=i};
		result_code = pthread_create(&threads[i], NULL, compute_chunk, &(args[i]));
	}

	// wait for each thread to complete
	for (int i = 0; i < NUM_THREADS; i++) {
		result_code = pthread_join(threads[i], NULL);
		printf("In main: Thread %d has ended.\n", i);
	}

	return 0;
}