summaryrefslogtreecommitdiff
path: root/charf.c
blob: a2d1ad8fefe460b99442cacd0e1c2479a3e56013 (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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>

#ifndef EXACT
#define EXACT false
#endif

#if EXACT
#include "ratio.h"
#define VALUETYPE rational
#define EXPTYPE unsigned int
#else
#include "double.h"
#define VALUETYPE double
#define EXPTYPE double
#endif


/*given function df[0] on domain [0,D-1], compute derivatives f' until f^{(K)} and store them in df[1] to df[K]*/
void differentiate(VALUETYPE* f, VALUETYPE* df, int D, int K){
	/*Set zeroth derivative to be f.*/
	for(int i=0; i<D; i++){
		df[i] = f[i];
	}
	for(int k=1; k<=K; k++){
		/*Compute kth derivative of f from (k-1)th.*/
		/*Only compute derivatives at arguments i < D-k, because for larger i we would need data from outside the domain of f.*/
		for(int i=0; i<D-k; i++){
			df[i] = difference(df[i+1], df[i]);
		}
		/*Make them zero for arguments where their dependence reaches outside the domain.*/
		for(int i=D-k; i<D; i++){
			df[i] = convert_int(0);
		}
	}
}

/*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++){
		integralp = sum(integralp,power(absolute(f[i]),p));
	}
	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 = malloc(D*sizeof(VALUETYPE*));
	/*Af[i][j] will be the average of f on [min(i,j),max(i,j)]*/
	VALUETYPE** Af = malloc(D*sizeof(VALUETYPE*));
	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+n<D;i++){
			Sf[i][i+n] = sum(Sf[i][i+n-1], f[i+n]);
			Sf[i+n][i] = Sf[i][i+n];
	
			Af[i][i+n] = ratio(Sf[i][i+n],convert_int(n+1));
			Af[i+n][i] = Af[i][i+n];
		}
	}
	
	/*Compute maximal function by picking the largest average*/
	for(int i=0;i<D;i++){
		Mf[i] = Af[i][i];
		for(int j=0;j<D;j++){
			if(is_greater(Af[i][j], Mf[i])) Mf[i] = Af[i][j];
		}
	}
	
	/*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]);
	}
	free(Sf);
	free(Af);
}

void compute(int N, int K, int D, EXPTYPE p, int t){
	/*allocate memory for f*/
	VALUETYPE* f = malloc(D*sizeof(VALUETYPE));
	/*Initiate f to be zero everywhere.*/
	for(int i=0;i<D;i++) f[i] = convert_int(0);
	//for(int i=0;i<N;i++) f[N+i] = rand() %2;
	/*In the middle of the domain set f to the values encoded in bit string t*/
	for(int i=0;i<N;i++){
		/*Since we care about the Kth derivative, which in i depends on f on [i,i+K], shift f to the right by K/2 so that the most interesting part of f^{(K)} and Mf^{(K)} will be around the center of the domain*/
		f[(D-N)/2+i+K/2] = convert_int((t >> i) & 1);
		//if(i%3==0) f[2*N+i+K/2] = 1;
	}

	VALUETYPE* Mf = malloc(D*sizeof(VALUETYPE));
	compute_maximalfunction(f, Mf, D);

	/*Allocate memory for derivatives.*/
	VALUETYPE* df = malloc(D*sizeof(VALUETYPE));
	VALUETYPE* dMf = malloc(D*sizeof(VALUETYPE));

	/*Compute Kth derivative of f and Mf*/
	differentiate(f,df,D,K);
	differentiate(Mf,dMf,D,K);
	
	/*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);

	free(df);
	free(dMf);

	//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)>.7)
		printf("f: ");
		for(int i=0;i<D;i++) printf("%1.0f ",to_double(f[i]));
		printf("\n");
		printf("%.4f\n",to_double(r));
	}
	free(f);
	free(Mf);
}

int main() {
	/*length of the support of f*/
	int N=12;

	/*order of the derivative to consider. Should not be larger than (D-N)/2 because then the support of f^{(K)} reaches outside of our domain.*/
	int K=3;

	/*length of the domain*/
	int D=N+K+1;

	/*exponent p of the L^p norm to consider*/
	EXPTYPE p = 1;

	/*Iterate over all strings of 0s and 1s with length N. Those will represent f.*/
	for(int t=1; t<=(1 << N)-1; t++) compute(N,K,D,p,t);

	return 0;
}