-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathclenshaw.c
executable file
·392 lines (364 loc) · 11 KB
/
clenshaw.c
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/* clenshaw.c */
/* S. Engblom 2007-04-19 (Revision) */
/* S. Engblom 2005-07-28 */
#include <string.h>
#include "mex.h"
#include "matrix.h"
/* forward declarations */
void SizesAndType(const mxArray **A,
int *dimM,int *dimN,
mxComplexity *type);
void recurrence(double *prY,double *piY,int M,int N,
const mxArray *A,const mxArray *B);
void clenshaw(double *prY,double *piY,int M,int N,int KC,int NC,
const mxArray *A,const mxArray *B,
const double *prC,const double *piC);
#define ISDOUBLEMATRIX(A) (mxIsDouble(A) && !mxIsSparse(A) && \
mxGetNumberOfDimensions(A) == 2)
#define ISDOUBLETENSOR(A) (mxIsDouble(A) && !mxIsSparse(A))
/*------------------------------------------------------------------------*/
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
/* syntax */
if (nrhs < 2 || 3 < nrhs || nlhs > 1)
mexErrMsgIdAndTxt("clenshaw:e1","Expecting 2 or 3 inputs "
"and one output.");
/* 'recurrence'-syntax */
if (nrhs == 2) {
/* chech input and allocate output */
int dimM,dimN;
mxComplexity type;
SizesAndType(prhs,&dimM,&dimN,&type);
/* allocate and evaluate recurrence */
plhs[0] = mxCreateDoubleMatrix(dimM,dimN,type);
recurrence(mxGetPr(plhs[0]),mxGetPi(plhs[0]),
dimM,dimN,prhs[0],prhs[1]);
}
/* 'coefficient'-syntax (Clenshaw summation) */
else {
/* check input and allocate output */
int dimM,dimN,KC,NC;
mxComplexity type;
SizesAndType(prhs,&dimM,&dimN,&type);
/* coefficient matrix */
if (!ISDOUBLETENSOR(prhs[2]))
mexErrMsgIdAndTxt("clenshaw:e2",
"Expecting a double, non-sparse matrix.");
if (mxGetM(prhs[2]) != dimM)
mexErrMsgIdAndTxt("clenshaw:e4",
"The dimensions of the coefficients must "
"match the length of each recursion.");
if (mxGetNumberOfDimensions(prhs[2]) > 3)
mexErrMsgIdAndTxt("clenshaw:e5","Coefficient array can have at "
"most three dimensions.");
else if (mxGetNumberOfDimensions(prhs[2]) == 3) {
KC = mxGetDimensions(prhs[2])[1];
NC = mxGetDimensions(prhs[2])[2];
}
else {
KC = mxGetN(prhs[2]);
NC = 1;
}
if (dimN == 1)
dimN = NC;
else if (dimN != NC && NC != 1)
mexErrMsgIdAndTxt("clenshaw:e6","The number of recursions "
"must agree.");
if (mxIsComplex(prhs[2])) type = mxCOMPLEX;
/* allocate and evaluate sum */
plhs[0] = mxCreateDoubleMatrix(KC,dimN,type);
clenshaw(mxGetPr(plhs[0]),mxGetPi(plhs[0]),
dimM,dimN,KC,NC,
prhs[0],prhs[1],mxGetPr(prhs[2]),mxGetPi(prhs[2]));
}
}
/*------------------------------------------------------------------------*/
void SizesAndType(const mxArray **A,
int *dimM,int *dimN,
mxComplexity *type)
/* Given the arrays A[0] and A[1], this routine determines the
dimensions dimM and dimN of the recursions defined by this
input. The common type (mxREAL/mxCOMPLEX) is returned in type. */
{
*dimM = *dimN = 1;
*type = mxREAL;
for (int j = 0; j < 2; j++) {
const mxArray *Aj = A[j];
if (!ISDOUBLEMATRIX(Aj))
mexErrMsgIdAndTxt("clenshaw:e2",
"Expecting a double, non-sparse matrix.");
if (*dimM == 1)
*dimM = mxGetM(Aj);
else if (*dimM != mxGetM(Aj) && mxGetM(Aj) != 1)
mexErrMsgIdAndTxt("clenshaw:e3",
"Dimensions must either match or be singletons.");
if (*dimN == 1)
*dimN = mxGetN(Aj);
else if (*dimN != mxGetN(Aj) && mxGetN(Aj) != 1)
mexErrMsgIdAndTxt("clenshaw:e3",
"Dimensions must either match or be singletons.");
if (mxIsComplex(Aj)) *type = mxCOMPLEX;
}
}
/*------------------------------------------------------------------------*/
void recurrence(double *prY,double *piY,int M,int N,
const mxArray *A,const mxArray *B)
/* Evaluates the recurrence as defined by the matrices A and B and
stores the result in (prY,piY), which must be allocated prior to
call. [M,N] = max(size(A),size(B)). All dimensions must either
match or be singletons. */
{
const double *prA = mxGetPr(A),*piA = mxGetPi(A);
const double *prB = mxGetPr(B),*piB = mxGetPi(B);
/* increments for columns and rows */
const int ia = mxGetM(A) == M;
const int ja = (ia ^ mxGetN(A) == N)*((mxGetN(A) == N)-mxGetM(A));
const int ib = mxGetM(B) == M;
const int jb = (ib ^ mxGetN(B) == N)*((mxGetN(B) == N)-mxGetM(B));
/* evaluate recurrence */
if (piY == NULL)
for (int j = 0; j < N; j++) {
double r1 = 1.0,r2 = 1.0,r3;
for (int i = 0; i < M; i++) {
r3 = r2;
r2 = r1;
r1 = *prA*r2+*prB*r3;
*prY++ = r1;
prA += ia;
prB += ib;
}
prA += ja;
prB += jb;
}
/* the remaining 3 complex case */
else if (piB == NULL)
for (int j = 0; j < N; j++) {
double r1 = 1.0,r2 = 1.0,r3;
double i1 = 0.0,i2 = 0.0,i3;
for (int i = 0; i < M; i++) {
r3 = r2; i3 = i2;
r2 = r1; i2 = i1;
r1 = *prA*r2-*piA*i2+*prB*r3;
i1 = *prA*i2+*piA*r2+*prB*i3;
*prY++ = r1; *piY++ = i1;
prA += ia; piA += ia;
prB += ib;
}
prA += ja; piA += ja;
prB += jb;
}
else if (piA == NULL)
for (int j = 0; j < N; j++) {
double r1 = 1.0,r2 = 1.0,r3;
double i1 = 0.0,i2 = 0.0,i3;
for (int i = 0; i < M; i++) {
r3 = r2; i3 = i2;
r2 = r1; i2 = i1;
r1 = *prA*r2+*prB*r3-*piB*i3;
i1 = *prA*i2+*prB*i3+*piB*r3;
*prY++ = r1; *piY++ = i1;
prA += ia;
prB += ib; piB += ib;
}
prA += ja;
prB += jb; piB += jb;
}
else
for (int j = 0; j < N; j++) {
double r1 = 1.0,r2 = 1.0,r3;
double i1 = 0.0,i2 = 0.0,i3;
for (int i = 0; i < M; i++) {
r3 = r2; i3 = i2;
r2 = r1; i2 = i1;
r1 = *prA*r2-*piA*i2+*prB*r3-*piB*i3;
i1 = *prA*i2+*piA*r2+*prB*i3+*piB*r3;
*prY++ = r1; *piY++ = i1;
prA += ia; piA += ia;
prB += ib; piB += ib;
}
prA += ja; piA += ja;
prB += jb; piB += jb;
}
}
/*------------------------------------------------------------------------*/
void clenshaw(double *prY,double *piY,int M,int N,int KC,int NC,
const mxArray *A,const mxArray *B,
const double *prC,const double *piC)
/* Evaluates the recurrence as defined by the matrices A and B,
multiplies it by the coefficients C and stores the sum of the
result in (prY,piY), which must be allocated prior to call. [M,N] =
max(size(A),size(B),size(C,[1 3])) and C is M-by-KC-by-NC (where NC
is either 1 or N). The result has the dimensions KC-by-N. All
dimensions must either match or be singletons, except for C's first
dimension which must be M. */
{
const double *prA = mxGetPr(A),*piA = mxGetPi(A);
const double *prB = mxGetPr(B),*piB = mxGetPi(B);
/* start at the end since Clenshaws algorithm runs backwards */
if (M > 2) {
prA += mxGetNumberOfElements(A)-1;
prB += mxGetNumberOfElements(B)-1;
prC += M*KC*NC-1;
prY += KC*N-1;
}
/* increments for columns and rows */
const int ia = mxGetM(A) == M;
const int ja = mxGetM(A)*(mxGetN(A) == N);
const int ka = ia-ia*M;
const int ib = mxGetM(B) == M;
const int jb = mxGetM(B)*(mxGetN(B) == N);
const int kb = ib-ib*M;
const int jc = -(NC != N)*M*KC;
/* all real case */
if (piY == NULL) {
/* two special cases: sum includes boundary values only */
if (M == 1)
for (int j = 0; j < N; j++) {
for (int k = 0; k < KC; k++) {
const double R1 = prA[0]+prB[0];
*prY++ = prC[0]*R1; prC++;
}
prA += ka+ja;
prB += kb+jb;
prC += jc;
}
else if (M == 2)
for (int j = 0; j < N; j++) {
for (int k = 0; k < KC; k++) {
const double R2 = prA[0]+prB[0];
const double R1 = prA[ia]*R2+prB[ib];
*prY++ = prC[0]*R2+prC[1]*R1; prC += 2;
}
prA += ia+ka+ja;
prB += ib+kb+jb;
prC += jc;
}
/* evaluate using Clenshaw summation */
else if (M >= 3)
for (int j = 0; j < N; j++) {
for (int k = 0; k < KC; k++) {
double r1 = *prC,r2,r3;
prC--;
/* initial boundary */
r2 = r1;
r1 = *prA*r2+*prC;
prA -= ia;
prC--;
/* internal formula */
for (int i = 0; i < M-3; i++) {
r3 = r2;
r2 = r1;
r1 = *prA*r2+*prB*r3+*prC;
prA -= ia;
prB -= ib;
prC--;
}
/* final boundary */
r3 = r2;
r2 = r1;
r1 = *prB*r3+*prC;
/* initial values of the recurrence */
prA -= ia;
prB -= 2*ib;
const double R2 = prA[0]+prB[0];
const double R1 = prA[ia]*R2+prB[ib];
/* accumulated sum */
*prY-- = R2*r1+R1*r2;
prA -= ka;
prB -= kb;
prC--;
}
prA -= ja;
prB -= jb;
prC -= jc;
}
}
else {
/* in order to avoid a messy code the remaining 7 complex cases
are handled in the same way as the pure complex case */
const int ia2 = piA == NULL ? 0 : ia;
const int ja2 = piA == NULL ? 0 : ja;
const int ka2 = piA == NULL ? 0 : ka;
const int ib2 = piB == NULL ? 0 : ib;
const int jb2 = piB == NULL ? 0 : jb;
const int kb2 = piB == NULL ? 0 : kb;
const int ic2 = piC == NULL ? 0 : 1;
const int jc2 = piC == NULL ? 0 : jc;
if (M > 2) {
if (piA != NULL) piA += mxGetNumberOfElements(A)-1;
if (piB != NULL) piB += mxGetNumberOfElements(B)-1;
if (piC != NULL) piC += M*KC*NC-1;
piY += KC*N-1;
}
const double zero = 0.0;
if (piA == NULL) piA = &zero;
if (piB == NULL) piB = &zero;
if (piC == NULL) piC = &zero;
if (M == 1)
for (int j = 0; j < N; j++) {
for (int k = 0; k < KC; k++) {
const double R1 = prA[0]+prB[0],I1 = piA[0]+piB[0];
*prY++ = prC[0]*R1-piC[0]*I1;
*piY++ = prC[0]*I1+piC[0]*R1;
prC++; piC += ic2;
}
prA += ka+ja; piA += ka2+ja2;
prB += kb+jb; piB += kb2+jb2;
prC += jc; piC += jc2;
}
else if (M == 2)
for (int j = 0; j < N; j++) {
for (int k = 0; k < KC; k++) {
const double R2 = prA[0]+prB[0],I2 = piA[0]+piB[0];
const double R1 = prA[ia]*R2-piA[ia2]*I2+prB[ib],
I1 = prA[ia]*I2+piA[ia2]*R2+piB[ib2];
*prY++ = prC[0]*R2-piC[0]*I2+prC[1]*R1-piC[ic2]*I1;
*piY++ = prC[0]*I2+piC[0]*R2+prC[1]*I1+piC[ic2]*R1;
prC += 2; piC += 2*ic2;
}
prA += ia+ka+ja; piA += ia2+ka2+ja2;
prB += ib+kb+jb; piB += ib2+kb2+jb2;
prC += jc; piC += jc2;
}
else if (M >= 3)
for (int j = 0; j < N; j++) {
for (int k = 0; k < KC; k++) {
double r1 = *prC,r2,r3;
double i1 = *piC,i2,i3;
prC--; piC -= ic2;
r2 = r1; i2 = i1;
r1 = *prA*r2-*piA*i2+*prC;
i1 = *prA*i2+*piA*r2+*piC;
prA -= ia; piA -= ia2;
prC--; piC -= ic2;
for (int i = 0; i < M-3; i++) {
r3 = r2; i3 = i2;
r2 = r1; i2 = i1;
r1 = *prA*r2-*piA*i2+*prB*r3-*piB*i3+*prC;
i1 = *prA*i2+*piA*r2+*prB*i3+*piB*r3+*piC;
prA -= ia; piA -= ia2;
prB -= ib; piB -= ib2;
prC--; piC -= ic2;
}
r3 = r2; i3 = i2;
r2 = r1; i2 = i1;
r1 = *prB*r3-*piB*i3+*prC;
i1 = *prB*i3+*piB*r3+*piC;
prA -= ia; piA -= ia2;
prB -= 2*ib; piB -= 2*ib2;
const double R2 = prA[0]+prB[0],I2 = piA[0]+piB[0];
const double R1 = prA[ia]*R2-piA[ia2]*I2+prB[ib],
I1 = prA[ia]*I2+piA[ia2]*R2+piB[ib2];
*prY-- = R2*r1-I2*i1+R1*r2-I1*i2;
*piY-- = R2*i1+I2*r1+I1*r2+R1*i2;
prA -= ka; piA -= ka2;
prB -= kb; piB -= kb2;
prC--; piC -= ic2;
}
prA -= ja; piA -= ja2;
prB -= jb; piB -= jb2;
prC -= jc; piC -= jc2;
}
}
}
/*------------------------------------------------------------------------*/