NFFT Logo 3.2.3
fastsum_matlab.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002, 2012 Jens Keiner, Stefan Kunis, Daniel Potts
3  *
4  * This program is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 51
16  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 /* $Id: fastsum_matlab.c 3775 2012-06-02 16:39:48Z keiner $ */
20 
27 #include "config.h"
28 
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <math.h>
33 #ifdef HAVE_COMPLEX_H
34  #include <complex.h>
35 #endif
36 
37 #include "fastsum.h"
38 #include "kernels.h"
39 #include "infft.h"
40 
47 int main(int argc, char **argv)
48 {
49  int j,k,t;
50  int d;
51  int N;
52  int M;
53  int n;
54  int m;
55  int p;
56  char *s;
57  double _Complex (*kernel)(double , int , const double *);
58  double c;
59  fastsum_plan my_fastsum_plan;
60  double _Complex *direct;
61  ticks t0, t1;
62  double time;
63  double error=0.0;
64  double eps_I;
65  double eps_B;
66  FILE *fid1, *fid2;
67  double temp;
68 
69  if (argc!=11)
70  {
71  printf("\nfastsum_test d N M n m p kernel c\n\n");
72  printf(" d dimension \n");
73  printf(" N number of source nodes \n");
74  printf(" M number of target nodes \n");
75  printf(" n expansion degree \n");
76  printf(" m cut-off parameter \n");
77  printf(" p degree of smoothness \n");
78  printf(" kernel kernel function (e.g., gaussian)\n");
79  printf(" c kernel parameter \n");
80  printf(" eps_I inner boundary \n");
81  printf(" eps_B outer boundary \n\n");
82  exit(-1);
83  }
84  else
85  {
86  d=atoi(argv[1]);
87  N=atoi(argv[2]); c=1.0/pow((double)N,1.0/(double)d);
88  M=atoi(argv[3]);
89  n=atoi(argv[4]);
90  m=atoi(argv[5]);
91  p=atoi(argv[6]);
92  s=argv[7];
93  c=atof(argv[8]);
94  eps_I=atof(argv[9]);
95  eps_B=atof(argv[10]);
96  if (strcmp(s,"gaussian")==0)
97  kernel = gaussian;
98  else if (strcmp(s,"multiquadric")==0)
99  kernel = multiquadric;
100  else if (strcmp(s,"inverse_multiquadric")==0)
101  kernel = inverse_multiquadric;
102  else if (strcmp(s,"logarithm")==0)
103  kernel = logarithm;
104  else if (strcmp(s,"thinplate_spline")==0)
105  kernel = thinplate_spline;
106  else if (strcmp(s,"one_over_square")==0)
107  kernel = one_over_square;
108  else if (strcmp(s,"one_over_modulus")==0)
109  kernel = one_over_modulus;
110  else if (strcmp(s,"one_over_x")==0)
111  kernel = one_over_x;
112  else if (strcmp(s,"inverse_multiquadric3")==0)
113  kernel = inverse_multiquadric3;
114  else if (strcmp(s,"sinc_kernel")==0)
115  kernel = sinc_kernel;
116  else if (strcmp(s,"cosc")==0)
117  kernel = cosc;
118  else if (strcmp(s,"cot")==0)
119  kernel = kcot;
120  else
121  {
122  s="multiquadric";
123  kernel = multiquadric;
124  }
125  }
126  printf("d=%d, N=%d, M=%d, n=%d, m=%d, p=%d, kernel=%s, c=%g, eps_I=%g, eps_B=%g \n",d,N,M,n,m,p,s,c,eps_I,eps_B);
127 
129  fastsum_init_guru(&my_fastsum_plan, d, N, M, kernel, &c, 0, n, m, p, eps_I, eps_B);
130  /*fastsum_init_guru(&my_fastsum_plan, d, N, M, kernel, &c, EXACT_NEARFIELD, n, m, p);*/
131 
133  fid1=fopen("x.dat","r");
134  fid2=fopen("alpha.dat","r");
135  for (k=0; k<N; k++)
136  {
137  for (t=0; t<d; t++)
138  {
139  fscanf(fid1,"%le",&my_fastsum_plan.x[k*d+t]);
140  }
141  fscanf(fid2,"%le",&temp); my_fastsum_plan.alpha[k] = temp;
142  fscanf(fid2,"%le",&temp); my_fastsum_plan.alpha[k] += temp*_Complex_I;
143  }
144  fclose(fid1);
145  fclose(fid2);
146 
148  fid1=fopen("y.dat","r");
149  for (j=0; j<M; j++)
150  {
151  for (t=0; t<d; t++)
152  {
153  fscanf(fid1,"%le",&my_fastsum_plan.y[j*d+t]);
154  }
155  }
156  fclose(fid1);
157 
159  printf("direct computation: "); fflush(NULL);
160  t0 = getticks();
161  fastsum_exact(&my_fastsum_plan);
162  t1 = getticks();
163  time=nfft_elapsed_seconds(t1,t0);
164  printf("%fsec\n",time);
165 
167  direct = (double _Complex *)nfft_malloc(my_fastsum_plan.M_total*(sizeof(double _Complex)));
168  for (j=0; j<my_fastsum_plan.M_total; j++)
169  direct[j]=my_fastsum_plan.f[j];
170 
172  printf("pre-computation: "); fflush(NULL);
173  t0 = getticks();
174  fastsum_precompute(&my_fastsum_plan);
175  t1 = getticks();
176  time=nfft_elapsed_seconds(t1,t0);
177  printf("%fsec\n",time);
178 
180  printf("fast computation: "); fflush(NULL);
181  t0 = getticks();
182  fastsum_trafo(&my_fastsum_plan);
183  t1 = getticks();
184  time=nfft_elapsed_seconds(t1,t0);
185  printf("%fsec\n",time);
186 
188  error=0.0;
189  for (j=0; j<my_fastsum_plan.M_total; j++)
190  {
191  if (cabs(direct[j]-my_fastsum_plan.f[j])/cabs(direct[j])>error)
192  error=cabs(direct[j]-my_fastsum_plan.f[j])/cabs(direct[j]);
193  }
194  printf("max relative error: %e\n",error);
195 
197  fid1=fopen("f.dat","w+");
198  fid2=fopen("f_direct.dat","w+");
199  if (fid1==NULL)
200  {
201  printf("Fehler!\n");
202  exit(-1);
203  }
204  for (j=0; j<M; j++)
205  {
206  temp=creal(my_fastsum_plan.f[j]);
207  fprintf(fid1," % .16e",temp);
208  temp=cimag(my_fastsum_plan.f[j]);
209  fprintf(fid1," % .16e\n",temp);
210 
211  temp=creal(direct[j]);
212  fprintf(fid2," % .16e",temp);
213  temp=cimag(direct[j]);
214  fprintf(fid2," % .16e\n",temp);
215  }
216  fclose(fid1);
217  fclose(fid2);
218 
220  fastsum_finalize(&my_fastsum_plan);
221 
222  return 0;
223 }
224 /* \} */

Generated on Tue Apr 30 2013 by Doxygen 1.8.1