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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
//for multithreading
#include <pthread.h>
#include <stdbool.h>
#include <string.h>
//getLine
#include "misc.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
/*maximum length of the support of f*/
#define N 24
/*maximal order of derivative*/
#define K 24
/*maximal number of exponents*/
#define P 1
/*given function df[0] on domain [0,M-1], compute derivatives f' until f^{(K)} and store f^{(K)} in df*/
void differentiate(VALUETYPE* f, VALUETYPE* df, int D, int k){
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 l=1; l<=k; l++){
/*Compute lth derivative of f from (l-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 = int_to_valuetype(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 so have to use malloc instead.*/
VALUETYPE* Sf[N];
VALUETYPE* Af[N];
for(int i=0; i<D; i++){
Sf[i] = malloc(D*sizeof(VALUETYPE));
Af[i] = malloc(D*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],int_to_valuetype(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(Af[i]);
free(Sf[i]);
}
}
/*Generates all characteristic functions of length up N.*/
int generate_each_charf(VALUETYPE* f, int i){
int s=0;
int d=2;
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] = int_to_valuetype((t >> n) & 1);
return d;
}
}
return -1;
}
int generate_triangle(VALUETYPE* f, int i){
int j = i+1;
if(j <= N/2){
for(int n=0; n < N; n++) f[n] = int_to_valuetype(0);
for(int n=0; n < j; n++) f[n] = int_to_valuetype(j-n);
for(int n=1; n < j; n++) f[N-n] = int_to_valuetype(j-n);
return N;
}
else return -1;
}
/*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){
return generate_each_charf(f,i);
//return generate_triangle(f,i);
}
void format_result(char* s, int index, int k, EXPTYPE p, VALUETYPE r){
VALUETYPE f[N];
int d = generate_function(f,index);
sprintf(s,"f: ");
int l = 3;
for(int i=0; i<d; i++) l += sprintf(s+l,"%2.0f ",to_double(f[i]));
for(int i=d; i<N; i++) l += sprintf(s+l," ");
char rts[128];
root_to_string(rts,r,p);
l += sprintf(s+l,"%dth der.: %s",k,rts);
}
bool over_threshold_charf(double t, int k){
return (k==1 && t>=1) || (k==2 && t>=.5) || (k==3 && t>.53) || (k==4 && t>=.5) || (k==5 && t>=.58) || (k==6 && t>=.58) || (k==7 && t>=.69) || (k==8 && t>=.83) || (k==9 && t>=.8699) || (k==10 && t>=.919) || (k==11 && t>=.97) || (k==12 && t>=.97) || (k==13 && t>=.98) || (k==14 && t>=.98) || (k==15 && t>=.9817) || (k==16 && t>=.9817) || (k==17 && t>=.987) || (k==18 && t>=.991) || (k==19 && t>=.994) || (k==20 && t>=1.001) || (k==21 && t>=1.009) || (k==22 && t>=1.009) || (k==23 && t>=1.003) || (k==24 && t>=1.174) ;
}
int compute(int index, int num_exponents[K+1], EXPTYPE exponents[K+1][P], VALUETYPE (*records_ratio)[K+1][P], int (*records_index)[K+1][P]){
VALUETYPE f[N];
int D = generate_function(f,index);
/*Immediately abort if index is out of bounds.*/
if(D <= 0) return D;
VALUETYPE Mf[N];
/*This is the only O(D^2) operation in here so makes a lot of sense to only compute once and avoid repeating it.*/
compute_maximalfunction(f,Mf,D);
/*Allocate memory for derivatives.*/
VALUETYPE df[2][N];
VALUETYPE dMf[2][N];
for(int i=0; i<=1; i++){
for(int n=0; n<D; n++){
df[i][n] = f[n];
dMf[i][n] = Mf[n];
}
}
for(int k=0; k<=K; k++){
if(k>=1){
/*Compute kth derivative of f and Mf from (k-1)th derivative*/
differentiate(df[(k+1)%2],df[k%2],D,1);
differentiate(dMf[(k+1)%2],dMf[k%2],D,1);
}
for(int p=0; p<num_exponents[k]; p++){
/*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[k%2],exponents[k][p],D);
VALUETYPE intdMfp = integratep(dMf[k%2],exponents[k][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);
if(
//over_threshold_charf(to_double(r),k) &&
is_greater_possibly(r,(*records_ratio)[k][p])){
/*extra check for printing only because in error mode for some reason floats randomly seem to increase by tiny amounts*/
if(is_greater_certainly(r,(*records_ratio)[k][p])){
(*records_index)[k][p] = index;
char s[1024];
format_result(s, index, k, exponents[k][p], r);
printf("%s\n",s);
}
(*records_ratio)[k][p] = maximum((*records_ratio)[k][p],r);
}
}
}
return D;
}
void print_records(int num_exponents[K+1], EXPTYPE exponents[K+1][P], VALUETYPE ratios[K+1][P], int indeces[K+1][P]){
char s[1024];
printf("Current records:\n");
for(int k=0; k<=K; k++) for(int p=0; p<num_exponents[k]; p++){
format_result(s, indeces[k][p], k, exponents[k][p], ratios[k][p]);
printf("%s\n",s);
}
}
typedef struct { int num_exponents[K+1]; EXPTYPE exponents[K+1][P]; VALUETYPE (*records_ratio)[K+1][P]; int (*records_index)[K+1][P]; int num_thread; int* domain_current; } 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;
int i = args -> num_thread;
int* domain_current = args -> domain_current;
VALUETYPE f[N];
int d = 0;
while(d >= 0){
d = compute(i, args -> num_exponents, args -> exponents, args -> records_ratio, args -> records_index);
if(d > *domain_current){
*domain_current = d;
printf("Start considering length: %d\n",d);
}
i += NUM_THREADS;
}
}
int main() {
int num_exponents[K+1];
EXPTYPE exponents[K+1][P];
for(int k=0; k<=K; k++){
num_exponents[k] = 1;
exponents[k][0] = int_to_exptype(1);
}
pthread_t threads[NUM_THREADS];
Args args[NUM_THREADS];
VALUETYPE records_ratio[K+1][P];
int records_index[K+1][P];
for(int k=0; k<=K; k++) for(int p=0; p<num_exponents[k]; p++) records_ratio[k][p] = int_to_valuetype(0);
int domain_current = 0;
int result_code;
for (int i = 0; i < NUM_THREADS; i++) {
printf("In main: Creating thread %d.\n", i);
memcpy(args[i].num_exponents,num_exponents,sizeof num_exponents);
memcpy(args[i].exponents,exponents,sizeof exponents);
args[i].records_ratio = &records_ratio;
args[i].records_index = &records_index;
args[i].num_thread = i;
args[i].domain_current = &domain_current;
result_code = pthread_create(&threads[i], NULL, compute_chunk, args+i);
}
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];
bool cont = true;
do {
getLine(prmpt, buff, sz);
if( 0 == strcmp(buff,"q") || 0 == strcmp(buff,"quit") || 0 == strcmp(buff,"exit") ){
print_records(num_exponents,exponents,records_ratio,records_index);
exit(EXIT_SUCCESS);
}
else if( 0 == strcmp(buff,"r")) print_records(num_exponents,exponents,records_ratio,records_index);
} while(cont);
// 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);
}
print_records(num_exponents,exponents,records_ratio,records_index);
return 0;
}
|