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


/*given function f on domain [0,D-1], compute derivatives f^{(0)} until f^{(K)} and store them in df*/
void differentiate(double* f, double** df, int D, int K){
	df[0] = f;
	int 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(i=0; i<D-k; i++){
			df[k][i] = df[k-1][i+1] - df[k-1][i];
		}
	}
}

/*given function f on domain [0,D-1] compute pth root of integral of |f|^p*/
double integrate(double* f, double p, int D){
	double sum = 0.0;
	for(int i=0;i<D;i++){
		sum += pow(fabs(f[i]),p);
	}
	return pow(sum,1/p);
}

/*given integer valued function f on domain D, compute ||Mf^{(K)}||_p/||f^{(K)}||_p*/
/*All the other arguments are pointers to storage for intermediate variables. The purpose is that we do not have to allocate new storage with each invocation. Probably there's a more user friendly way but I don't know much about garbage collection.*/
double compute_derivatives(int* f, int** Sf, double** Af, double* Mf, double** df, double** dMf, double p, double* intdf, double* intdMf, int D, int K){

/*Convert integer valued f to float valued df[0]*/
for(int i=0;i<D;i++) df[0][i] = (double) f[i];

/*Sf[i][j] will be the integral of f on [min(i,j),max(i,j)]*/
/*Af[i][j] will be the average of f on [min(i,j),max(i,j)]*/
for(int i=0;i<D;i++) {
	Sf[i][i] = f[i];
	Af[i][i] = 1.0*Sf[i][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] = Sf[i][i+n-1]+f[i+n];
		Sf[i+n][i] = Sf[i][i+n];

		Af[i][i+n] = 1.0*Sf[i][i+n]/(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(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 ",Mf[i]);
//printf("\n");
//for(int i=0;i<D;i++){
	//for(int j=0;j<D;j++) printf("%0.1f ",Af[i][j]);
	//printf("\n");
//}

/*Compute 0th until Kth derivative of f and Mf*/
differentiate(df[0],df,D,K);
differentiate(dMf[0],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");
//}

//for(int k=0;k<=K;k++){

/*Compute L^p norm of derivatives*/
int k=K;
intdf[k] = integrate(df[k],p,D);
intdMf[k] = integrate(dMf[k],p,D);
//printf("%d: %f / %f = %f\n",k,intdMf[k],intdf[k],intdMf[k]/intdf[k]);
//}

/*Return ratio of L^p norms*/
return intdMf[k]/intdf[k];

}

int main() {

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

/*length of the domain*/
int D=5*N;

/*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;

/*exponent p of the L^p norm to consider*/
double p = 1.0;

/*allocate memory for f*/
int* f = malloc(D*sizeof(int));

/*allocate memory for temporary variables, such as averages*/
int** Sf = malloc(D*sizeof(int*));
double** Af = malloc(D*sizeof(double*));
double* Mf = malloc(D*sizeof(double));
for(int i=0;i<D;i++){
	Sf[i] = malloc(D*sizeof(int));
	Af[i] = malloc(D*sizeof(double));
}

/*Allocate memory for derivatives*/
double** df = malloc(D*sizeof(double*));
double** dMf = malloc(D*sizeof(double*));
for(int k=0;k<=K;k++){
	df[k] = malloc(D*sizeof(double));
	dMf[k] = malloc(D*sizeof(double));
	for(int i=0;i<D;i++){
		df[k][i] = 0;
		dMf[k][i] = 0;
	}
}
dMf[0] = Mf;

/*Allocate memory for integrals*/
double* intdf = malloc(K*sizeof(double));
double* intdMf = malloc(K*sizeof(double));

/*Allocate memory for ||Mf^{(k)}||_p/||f^{(k)}||_p.*/
double r = 0.0;

/*Iterate over all strings of 0s and 1s with length N. Those will represent f.*/
for(int t=1; t<=(1 << N)-1; t++){
	/*Initiate f to be zero everywhere.*/
	for(int i=0;i<D;i++) f[i]=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[2*N+i+K/2] = (t >> i) & 1;
		//if(i%3==0) f[2*N+i+K/2] = 1;
	}
	/*Compute ||Mf^{(k)}||_p/||f^{(k)}||_p.*/
	r = compute_derivatives(f, Sf, Af, Mf, df, dMf, p, intdf, intdMf, D, K);
	//printf("%.3d: %.3f  \n",t,r);
	/*Print f and ||Mf^{(k)}||_p/||f^{(k)}||_p if the latter is close to 1/2.*/
	if(r>.4997){
		printf("f: ");
		for(int i=0;i<D;i++) printf("%.1d ",f[i]);
		printf("\n");
		printf("%.4f\n",r);
	}
}

return 0;
}