NFFT  3.3.0
fastsum_matlab.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002, 2015 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$ */
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  const char *s;
57  C (*kernel)(R, int, const R *);
58  R c;
59  fastsum_plan my_fastsum_plan;
60  C *direct;
61  ticks t0, t1;
62  R time;
63  R error = K(0.0);
64  R eps_I;
65  R eps_B;
66  FILE *fid1, *fid2;
67  R 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]);
88  c = K(1.0) / POW((R)(N), K(1.0) / ((R)(d)));
89  M = atoi(argv[3]);
90  n = atoi(argv[4]);
91  m = atoi(argv[5]);
92  p = atoi(argv[6]);
93  s = argv[7];
94  c = (R)(atof(argv[8]));
95  eps_I = (R)(atof(argv[9]));
96  eps_B = (R)(atof(argv[10]));
97  if (strcmp(s, "gaussian") == 0)
98  kernel = gaussian;
99  else if (strcmp(s, "multiquadric") == 0)
100  kernel = multiquadric;
101  else if (strcmp(s, "inverse_multiquadric") == 0)
102  kernel = inverse_multiquadric;
103  else if (strcmp(s, "logarithm") == 0)
104  kernel = logarithm;
105  else if (strcmp(s, "thinplate_spline") == 0)
106  kernel = thinplate_spline;
107  else if (strcmp(s, "one_over_square") == 0)
108  kernel = one_over_square;
109  else if (strcmp(s, "one_over_modulus") == 0)
110  kernel = one_over_modulus;
111  else if (strcmp(s, "one_over_x") == 0)
112  kernel = one_over_x;
113  else if (strcmp(s, "inverse_multiquadric3") == 0)
114  kernel = inverse_multiquadric3;
115  else if (strcmp(s, "sinc_kernel") == 0)
116  kernel = sinc_kernel;
117  else if (strcmp(s, "cosc") == 0)
118  kernel = cosc;
119  else if (strcmp(s, "cot") == 0)
120  kernel = kcot;
121  else
122  {
123  s = "multiquadric";
124  kernel = multiquadric;
125  }
126  }
127  printf(
128  "d=%d, N=%d, M=%d, n=%d, m=%d, p=%d, kernel=%s, c=%" __FGS__ ", eps_I=%" __FGS__ ", eps_B=%" __FGS__ " \n",
129  d, N, M, n, m, p, s, c, eps_I, eps_B);
130 
132  fastsum_init_guru(&my_fastsum_plan, d, N, M, kernel, &c, 0, n, m, p, eps_I,
133  eps_B);
134  /*fastsum_init_guru(&my_fastsum_plan, d, N, M, kernel, &c, EXACT_NEARFIELD, n, m, p);*/
135 
137  fid1 = fopen("x.dat", "r");
138  fid2 = fopen("alpha.dat", "r");
139  for (k = 0; k < N; k++)
140  {
141  for (t = 0; t < d; t++)
142  {
143  fscanf(fid1, __FR__, &my_fastsum_plan.x[k * d + t]);
144  }
145  fscanf(fid2, __FR__, &temp);
146  my_fastsum_plan.alpha[k] = temp;
147  fscanf(fid2, __FR__, &temp);
148  my_fastsum_plan.alpha[k] += temp * II;
149  }
150  fclose(fid1);
151  fclose(fid2);
152 
154  fid1 = fopen("y.dat", "r");
155  for (j = 0; j < M; j++)
156  {
157  for (t = 0; t < d; t++)
158  {
159  fscanf(fid1, __FR__, &my_fastsum_plan.y[j * d + t]);
160  }
161  }
162  fclose(fid1);
163 
165  printf("direct computation: ");
166  fflush(NULL);
167  t0 = getticks();
168  fastsum_exact(&my_fastsum_plan);
169  t1 = getticks();
170  time = NFFT(elapsed_seconds)(t1, t0);
171  printf(__FI__ "sec\n", time);
172 
174  direct = (C *) NFFT(malloc)((size_t)(my_fastsum_plan.M_total) * (sizeof(C)));
175  for (j = 0; j < my_fastsum_plan.M_total; j++)
176  direct[j] = my_fastsum_plan.f[j];
177 
179  printf("pre-computation: ");
180  fflush(NULL);
181  t0 = getticks();
182  fastsum_precompute(&my_fastsum_plan);
183  t1 = getticks();
184  time = NFFT(elapsed_seconds)(t1, t0);
185  printf(__FI__ "sec\n", time);
186 
188  printf("fast computation: ");
189  fflush(NULL);
190  t0 = getticks();
191  fastsum_trafo(&my_fastsum_plan);
192  t1 = getticks();
193  time = NFFT(elapsed_seconds)(t1, t0);
194  printf(__FI__ "sec\n", time);
195 
197  error = K(0.0);
198  for (j = 0; j < my_fastsum_plan.M_total; j++)
199  {
200  if (CABS(direct[j] - my_fastsum_plan.f[j]) / CABS(direct[j]) > error)
201  error = CABS(direct[j] - my_fastsum_plan.f[j]) / CABS(direct[j]);
202  }
203  printf("max relative error: " __FE__ "\n", error);
204 
206  fid1 = fopen("f.dat", "w+");
207  fid2 = fopen("f_direct.dat", "w+");
208  if (fid1 == NULL)
209  {
210  printf("Fehler!\n");
211  exit(EXIT_FAILURE);
212  }
213  for (j = 0; j < M; j++)
214  {
215  temp = CREAL(my_fastsum_plan.f[j]);
216  fprintf(fid1, " % .16" __FES__ "", temp);
217  temp = CIMAG(my_fastsum_plan.f[j]);
218  fprintf(fid1, " % .16" __FES__ "\n", temp);
219 
220  temp = CREAL(direct[j]);
221  fprintf(fid2, " % .16" __FES__ "", temp);
222  temp = CIMAG(direct[j]);
223  fprintf(fid2, " % .16" __FES__ "\n", temp);
224  }
225  fclose(fid1);
226  fclose(fid2);
227 
229  fastsum_finalize(&my_fastsum_plan);
230 
231  return EXIT_SUCCESS;
232 }
233 /* \} */
int M_total
number of target knots
Definition: fastsum.h:81
Header file with predefined kernels for the fast summation algorithm.
plan for fast summation algorithm
Definition: fastsum.h:74
Header file for the fast NFFT-based summation algorithm.
C * alpha
source coefficients
Definition: fastsum.h:83
void fastsum_trafo(fastsum_plan *ths)
fast NFFT-based summation
Definition: fastsum.c:1057
void fastsum_precompute(fastsum_plan *ths)
precomputation for fastsum
Definition: fastsum.c:909
void fastsum_init_guru(fastsum_plan *ths, int d, int N_total, int M_total, kernel k, R *param, unsigned flags, int nn, int m, int p, R eps_I, R eps_B)
initialization of fastsum plan
Definition: fastsum.c:693
C * f
target evaluations
Definition: fastsum.h:84
R * y
target knots in d-ball with radius 1/4-eps_b/2
Definition: fastsum.h:87
void fastsum_finalize(fastsum_plan *ths)
finalization of fastsum plan
Definition: fastsum.c:846
void fastsum_exact(fastsum_plan *ths)
direct computation of sums
Definition: fastsum.c:879
R * x
source knots in d-ball with radius 1/4-eps_b/2
Definition: fastsum.h:86