-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhpp_prb.cpp
547 lines (469 loc) · 9.83 KB
/
hpp_prb.cpp
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# include <cstdlib>
# include <iostream>
# include <sstream>
# include <iomanip>
# include <cmath>
# include <ctime>
using namespace std;
# include "hpp.hpp"
int main ( );
void hpp_test01 ( );
void hpp_test015 ( );
void hpp_test02 ( );
void hpp_test03 ( );
void hpp_test04 ( );
//****************************************************************************80
int main ( )
//****************************************************************************80
//
// Purpose:
//
// MAIN is the main program for HPP_PRB.
//
// Discussion:
//
// HPP_PRB tests the HERMITE_PRODUCT_POLYNOMIAL library.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 22 October 2014
//
// Author:
//
// John Burkardt
//
{
timestamp ( );
cout << "\n";
cout << "HPP_PRB:\n";
cout << " C++ version\n";
cout << " Test the HERMITE_PRODUCT_POLYNOMIAL library.\n";
hpp_test01 ( );
hpp_test015 ( );
hpp_test02 ( );
hpp_test03 ( );
hpp_test04 ( );
//
// Terminate.
//
cout << "\n";
cout << "HPP_PRB:\n";
cout << " Normal end of execution.\n";
cout << "\n";
timestamp ( );
return 0;
}
//****************************************************************************80
void hpp_test01 ( )
//****************************************************************************80
//
// Purpose:
//
// HPP_TEST01 tests routines for the GRLEX ordering of compositions.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 11 September 2014
//
// Author:
//
// John Burkardt
//
{
int i;
int k = 2;
int rank;
int rank1;
int rank2;
int seed;
int test;
int *x;
int x_sum;
int x_sum_old;
x = new int[k];
cout << "\n";
cout << "HPP_TEST01:\n";
cout << " COMP_NEXT_GRLEX is given a composition, and computes the \n";
cout << " next composition in grlex order.\n";
cout << "\n";
cout << " Rank Sum Components\n";
for ( i = 0; i < k; i++ )
{
x[i] = 0;
}
x_sum_old = -1;
rank = 1;
for ( ; ; )
{
x_sum = i4vec_sum ( k, x );
if ( x_sum_old < x_sum )
{
x_sum_old = x_sum;
cout << "\n";
}
cout << setw(6) << rank << " "
<< setw(6) << x_sum;
for ( i = 0; i < k; i++ )
{
cout << setw(4) << x[i];
}
cout << "\n";
if ( 20 <= rank )
{
break;
}
comp_next_grlex ( k, x );
rank = rank + 1;
}
delete [] x;
cout << "\n";
cout << " COMP_UNRANK_GRLEX is given a rank and returns the\n";
cout << " corresponding set of multinomial exponents.\n";
cout << "\n";
cout << " Rank Sum Components\n";
cout << "\n";
seed = 123456789;
for ( test = 1; test <= 5; test++ )
{
rank = i4_uniform_ab ( 1, 20, seed );
x = comp_unrank_grlex ( k, rank );
x_sum = i4vec_sum ( k, x );
cout << setw(6) << rank << " "
<< setw(6) << x_sum;
for ( i = 0; i < k; i++ )
{
cout << setw(4) << x[i];
}
cout << "\n";
delete [] x;
}
cout << "\n";
cout << " COMP_RANDOM_GRLEX randomly selects a composition\n";
cout << " between given lower and upper ranks.\n";
cout << "\n";
cout << " Rank Sum Components\n";
cout << "\n";
seed = 123456789;
rank1 = 5;
rank2 = 20;
for ( test = 1; test <= 5; test++ )
{
x = comp_random_grlex ( k, rank1, rank2, seed, rank );
x_sum = i4vec_sum ( k, x );
cout << setw(6) << rank << " "
<< setw(6) << x_sum;
for ( i = 0; i < k; i++ )
{
cout << setw(4) << x[i];
}
cout << "\n";
delete [] x;
}
cout << "\n";
cout << " COMP_RANK_GRLEX returns the rank of a given composition.\n";
cout << "\n";
cout << " Rank Sum Components\n";
cout << "\n";
x = new int[k];
x[0] = 4;
x[1] = 0;
rank = comp_rank_grlex ( k, x );
x_sum = i4vec_sum ( k, x );
cout << setw(6) << rank << " "
<< setw(6) << x_sum;
for ( i = 0; i < k; i++ )
{
cout << setw(4) << x[i];
}
cout << "\n";;
x[0] = 11;
x[1] = 5;
rank = comp_rank_grlex ( k, x );
x_sum = i4vec_sum ( k, x );
cout << setw(6) << rank << " "
<< setw(6) << x_sum;
for ( i = 0; i < k; i++ )
{
cout << setw(4) << x[i];
}
cout << "\n";
delete [] x;
return;
}
//****************************************************************************80
void hpp_test015 ( )
//****************************************************************************80
//
// Purpose:
//
// HPP_TEST015 tests HEP_COEFFICIENTS.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 22 October 2014
//
// Author:
//
// John Burkardt
//
{
double *c;
int *e;
int *f;
int l[1];
int m;
int n;
int o;
int o_max;
ostringstream title;
m = 1;
cout << "\n";
cout << "HPP_TEST015:\n";
cout << " HEP_COEFFICIENTS computes the coefficients and\n";
cout << " exponents of the Hermite polynomial He(n,x).\n";
for ( n = 1; n <= 5; n++ )
{
o = ( n + 2 ) / 2;
c = new double[o];
e = new int[o];
f = new int[o];
hep_coefficients ( n, o, c, f );
l[0] = n;
o_max = o;
hepp_to_polynomial ( m, l, o_max, o, c, e );
cout << "\n";
title << " He(" << n << ",x) =";
polynomial_print ( m, o, c, e, title.str ( ) );
title.str ( "" );
delete [] c;
delete [] e;
delete [] f;
}
return;
}
//****************************************************************************80
void hpp_test02 ( )
//****************************************************************************80
//
// Purpose:
//
// HPP_TEST02 tests HEP_VALUE.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 22 October 2014
//
// Author:
//
// John Burkardt
//
{
double e;
int n;
int n_data;
int o;
double x;
double xvec[1];
double fx1;
double *fx2;
n = 1;
cout << "\n";
cout << "HPP_TEST02:\n";
cout << " HEP_VALUES stores values of\n";
cout << " the Hermite polynomial He(o,x).\n";
cout << " HEP_VALUE evaluates a Hermite polynomial.\n";
cout << "\n";
cout << " Tabulated Computed\n";
cout << " O X He(O,X) He(O,X)";
cout << " Error\n";
cout << "\n";
n_data = 0;
for ( ; ; )
{
hep_values ( n_data, o, x, fx1 );
if ( n_data == 0 )
{
break;
}
xvec[0] = x;
fx2 = hep_value ( n, o, xvec );
e = fx1 - fx2[0];
cout << setw(6) << o << " "
<< setw(12) << x << " "
<< setw(24) << fx1 << " "
<< setw(24) << fx2[0] << " "
<< setw(8) << e << "\n";
delete [] fx2;
}
return;
}
//****************************************************************************80
void hpp_test03 ( )
//****************************************************************************80
//
// Purpose:
//
// HPP_TEST03 tests HEPP_VALUE.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 22 October 2014
//
// Author:
//
// John Burkardt
//
{
double *c;
int *e;
int i;
int *l;
int m = 3;
int n = 1;
int o;
int o_max;
int rank;
int seed;
double *v1;
double *v2;
double *x;
double xhi;
double xlo;
cout << "\n";
cout << "HPP_TEST03:\n";
cout << " HEPP_VALUE evaluates a Hermite product polynomial.\n";
cout << " POLYNOMIAL_VALUE evaluates a polynomial.\n";
xlo = -1.0;
xhi = +1.0;
seed = 123456789;
x = r8vec_uniform_ab_new ( m, xlo, xhi, seed );
cout << "\n";
cout << " Evaluate at X = ";
for ( i = 0; i < m; i++ )
{
cout << " " << x[i+0*m];
}
cout << "\n";
cout << "\n";
cout << " Rank I1 I2 I3: He(I1,X1)*He(I2,X2)*He(I3,X3) P(X1,X2,X3)\n";
cout << "\n";
for ( rank = 1; rank <= 20; rank++ )
{
l = comp_unrank_grlex ( m, rank );
//
// Evaluate the HePP directly.
//
v1 = hepp_value ( m, n, l, x );
//
// Convert the HePP to a polynomial.
//
o_max = 1;
for ( i = 0; i < m; i++ )
{
o_max = o_max * ( l[i] + 2 ) / 2;
}
c = new double[o_max];
e = new int[o_max];
hepp_to_polynomial ( m, l, o_max, o, c, e );
//
// Evaluate the polynomial.
//
v2 = polynomial_value ( m, o, c, e, n, x );
//
// Compare results.
//
cout << setw(6) << rank << " "
<< setw(2) << l[0] << " "
<< setw(2) << l[1] << " "
<< setw(2) << l[2] << " "
<< setw(14) << v1[0] << " "
<< setw(14) << v2[0] << "\n";
delete [] c;
delete [] e;
delete [] l;
delete [] v1;
delete [] v2;
}
delete [] x;
return;
}
//****************************************************************************80
void hpp_test04 ( )
//****************************************************************************80
//
// Purpose:
//
// HPP_TEST04 tests HEPP_TO_POLYNOMIAL.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 22 October 2014
//
// Author:
//
// John Burkardt
//
{
double *c;
int *e;
int i;
int *l;
ostringstream label;
int m = 2;
int o;
int o_max;
int rank;
cout << "\n";
cout << "HPP_TEST04:\n";
cout << " HEPP_TO_POLYNOMIAL is given a Hermite product polynomial\n";
cout << " and determines its polynomial representation.\n";
cout << "\n";
cout << " Using spatial dimension M = " << m << "\n";
for ( rank = 1; rank <= 11; rank++ )
{
l = comp_unrank_grlex ( m, rank );
o_max = 1;
for ( i = 0; i < m; i++ )
{
o_max = o_max * ( l[i] + 2 ) / 2;
}
c = new double[o_max];
e = new int[o_max];
hepp_to_polynomial ( m, l, o_max, o, c, e );
label << " HePP #" << rank
<< " = He(" << l[0]
<< ",X)*He(" << l[1]
<< ",Y) =";
cout << "\n";
polynomial_print ( m, o, c, e, label.str ( ) );
label.str ( "" );
delete [] c;
delete [] e;
delete [] l;
}
return;
}