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 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
|
// Purpose:
//
// Compute nodes and weights for the Gauss-Hermite quadrature rule.
// Print them as code for use in a C/C++ program.
//
// Discussion:
//
// The integral to be approximated has the form
//
// C * Integral ( -oo < x < +oo ) f(x) rho(x) dx
//
// where the weight rho(x) is:
//
// rho(x) = exp ( - b * x^2 ) * sqrt ( b / pi ) dx
//
// The user specifies:
// * N, the number of points in the rule
// * b, a scale factor;
// * SCALE, is 1 if the weights are to be normalized;
// * FILENAME, the root name of the output files.
//
// If SCALE = 0, then the factor C in front of the integrand is 1.
// If SCALE is nonzero, then the factor C is sqrt ( B ) / sqrt ( PI ).
// which means that the function f(x)=1 will integrate to 1.
//
// Usage is demonstrated in companion program GaussHermite_Demo.cpp.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Authors:
//
// Sylvan Elhay, Jaroslav Kautsky
// - Algorithm 655: IQPACK, FORTRAN Subroutines for the Weights of Interpolatory Quadrature,
// ACM Transactions on Mathematical Software,
// Volume 13, Number 4, December 1987, pages 399-415.
//
// Roger Martin, James Wilkinson,
// - The Implicit QL Algorithm,
// Numerische Mathematik,
// Volume 12, Number 5, December 1968, pages 377-383.
//
// John Burkardt:
// - rewrote Fortran code in C++
// - last edit 06 February 2014
// - published at https://people.sc.fsu.edu/~jburkardt/cpp_src/hermite_rule/hermite_rule.cpp
//
// Joachim Wuttke
// - 3feb23, put under version control as file GaussHermite_NodesWeights.cpp at
// https://jugit.fz-juelich.de/mlz/bornagain/-/tree/main/devtools/tabulate
// - modified to generate code
#include <cfloat>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
using namespace std;
void r8mat_write(string output_filename, int n, double table[]);
void cdgqf(int nt, int kind, double alpha, double beta, double t[], double wts[]);
void cgqf(int nt, int kind, double alpha, double beta, double a, double b, double t[],
double wts[]);
double class_matrix(int kind, int m, double alpha, double beta, double aj[], double bj[]);
void imtqlx(int n, double d[], double e[], double z[]);
void parchk(int kind, int m, double alpha, double beta);
void scqf(int nt, double t[], int mlt[], double wts[], int nwts, int ndx[], double swts[],
double st[], int kind, double alpha, double beta, double a, double b);
void sgqf(int nt, double aj[], double bj[], double zemu, double t[], double wts[]);
template<int SIGN>
void symmetrize(int n, double x[]);
const double pi = 3.14159265358979323846264338327950;
int main(int argc, char* argv[])
{
int kind = 6; // Hermite-Gauss
double b = 1.;
int scale = 1; // normalization off/on = 0/1
int nmax = 25;
double* r;
double* w;
double* x;
cout << "/* Nodes and weight for Gauss-Hermite quadrature.\n"
<< " Computed using the program GaussHermite_NodesWeights.cpp\n"
<< " by John Burkardt and Joachim Wuttke [1], based on algorithms by\n"
<< " Roger Martin and James Wilkinson [2] and Sylvan Elhay, Jaroslav Kautsky [3]\n"
<< "\n"
<< " [1] https://jugit.fz-juelich.de/mlz/bornagain/-/tree/main/devtools/tabulate/GaussHermite_NodesWeights.cpp\n"
<< " [2] The Implicit QL Algorithm, Numerische Mathematik, Volume 12, Number 5, December 1968, pages 377-383.\n"
<< " [3] Algorithm 655: IQPACK, FORTRAN Subroutines for the Weights of Interpolatory Quadrature, ACM Transactions on Mathematical Software, Volume 13, Number 4, December 1987, pages 399-415.\n"
<< "*/\n";
cout << "static const int maxOrderGaussHermite = " << nmax << ";\n";
cout << "static const double GaussHermiteNodesWeights[" << nmax*(nmax+1) << "] = {\n";
cout << setw(24) << setprecision(15);
for (int n=1; n<=nmax; ++n) {
//
// Construct the rule.
//
w = new double[n];
x = new double[n];
cgqf(n, kind, /*alpha*/0., /*beta*/0., /*center*/0., /*scale*/b, x, w);
if (kind == 6) {
symmetrize<-1>(n, x);
symmetrize<+1>(n, w);
}
//
// Normalize the rule.
// This way, the weights add up to 1.
//
if (scale == 1)
for (int i = 0; i < n; i++)
w[i] = w[i] * sqrt(b) / sqrt(pi);
//
// Print results.
//
cout << "/* order " << n << " */ ";
for (int i = 0; i < n; i++)
cout << x[i] << ", " << w[i] << ", ";
cout << "\n";
//
// Free memory.
//
delete[] w;
delete[] x;
//
// Terminate.
//
cout << "\n";
}
cout << "};\n";
return 0;
}
//****************************************************************************80
void cdgqf(int nt, int kind, double alpha, double beta, double t[], double wts[])
//****************************************************************************80
//
// Purpose:
//
// CDGQF computes a Gauss quadrature formula with default A, B and simple knots.
//
// Discussion:
//
// This routine computes all the knots and weights of a Gauss quadrature
// formula with a classical weight function with default values for A and B,
// and only simple knots.
//
// There are no moments checks and no printing is done.
//
// Use routine EIQFS to evaluate a quadrature computed by CGQFS.
//
// Author:
//
// Original FORTRAN77 version by Sylvan Elhay, Jaroslav Kautsky.
// C++ version by John Burkardt.
//
// Reference:
//
// Sylvan Elhay, Jaroslav Kautsky,
// Algorithm 655: IQPACK, FORTRAN Subroutines for the Weights of
// Interpolatory Quadrature,
// ACM Transactions on Mathematical Software,
// Volume 13, Number 4, December 1987, pages 399-415.
//
// Parameters:
//
// Input, int NT, the number of knots.
//
// Input, int KIND, the rule.
// 1, Legendre, (a,b) 1.0
// 2, Chebyshev, (a,b) ((b-x)*(x-a))^(-0.5)
// 3, Gegenbauer, (a,b) ((b-x)*(x-a))^alpha
// 4, Jacobi, (a,b) (b-x)^alpha*(x-a)^beta
// 5, Generalized Laguerre, (a,inf) (x-a)^alpha*exp(-b*(x-a))
// 6, Generalized Hermite, (-inf,inf) |x-a|^alpha*exp(-b*(x-a)^2)
// 7, Exponential, (a,b) |x-(a+b)/2.0|^alpha
// 8, Rational, (a,inf) (x-a)^alpha*(x+b)^beta
//
// Input, double ALPHA, the value of Alpha, if needed.
//
// Input, double BETA, the value of Beta, if needed.
//
// Output, double T[NT], the knots.
//
// Output, double WTS[NT], the weights.
//
{
double* aj;
double* bj;
double zemu;
parchk(kind, 2 * nt, alpha, beta);
//
// Get the Jacobi matrix and zero-th moment.
//
aj = new double[nt];
bj = new double[nt];
zemu = class_matrix(kind, nt, alpha, beta, aj, bj);
//
// Compute the knots and weights.
//
sgqf(nt, aj, bj, zemu, t, wts);
delete[] aj;
delete[] bj;
return;
}
//****************************************************************************80
void cgqf(int nt, int kind, double alpha, double beta, double a, double b, double t[], double wts[])
//****************************************************************************80
//
// Purpose:
//
// CGQF computes knots and weights of a Gauss quadrature formula.
//
// Discussion:
//
// The user may specify the interval (A,B).
//
// Only simple knots are produced.
//
// Use routine EIQFS to evaluate this quadrature formula.
//
// Author:
//
// Original FORTRAN77 version by Sylvan Elhay, Jaroslav Kautsky.
// C++ version by John Burkardt.
//
// Reference:
//
// Sylvan Elhay, Jaroslav Kautsky,
// Algorithm 655: IQPACK, FORTRAN Subroutines for the Weights of
// Interpolatory Quadrature,
// ACM Transactions on Mathematical Software,
// Volume 13, Number 4, December 1987, pages 399-415.
//
// Parameters:
//
// Input, int NT, the number of knots.
//
// Input, int KIND, the rule.
// 1, Legendre, (a,b) 1.0
// 2, Chebyshev Type 1, (a,b) ((b-x)*(x-a))^-0.5)
// 3, Gegenbauer, (a,b) ((b-x)*(x-a))^alpha
// 4, Jacobi, (a,b) (b-x)^alpha*(x-a)^beta
// 5, Generalized Laguerre, (a,+oo) (x-a)^alpha*exp(-b*(x-a))
// 6, Generalized Hermite, (-oo,+oo) |x-a|^alpha*exp(-b*(x-a)^2)
// 7, Exponential, (a,b) |x-(a+b)/2.0|^alpha
// 8, Rational, (a,+oo) (x-a)^alpha*(x+b)^beta
// 9, Chebyshev Type 2, (a,b) ((b-x)*(x-a))^(+0.5)
//
// Input, double ALPHA, the value of Alpha, if needed.
//
// Input, double BETA, the value of Beta, if needed.
//
// Input, double A, B, the interval endpoints, or
// other parameters.
//
// Output, double T[NT], the knots.
//
// Output, double WTS[NT], the weights.
//
{
int i;
int* mlt;
int* ndx;
//
// Compute the Gauss quadrature formula for default values of A and B.
//
cdgqf(nt, kind, alpha, beta, t, wts);
//
// Prepare to scale the quadrature formula to other weight function with
// valid A and B.
//
mlt = new int[nt];
for (i = 0; i < nt; i++)
mlt[i] = 1;
ndx = new int[nt];
for (i = 0; i < nt; i++)
ndx[i] = i + 1;
scqf(nt, t, mlt, wts, nt, ndx, wts, t, kind, alpha, beta, a, b);
delete[] mlt;
delete[] ndx;
return;
}
//****************************************************************************80
double class_matrix(int kind, int m, double alpha, double beta, double aj[], double bj[])
//****************************************************************************80
//
// Purpose:
//
// CLASS_MATRIX computes the Jacobi matrix for a quadrature rule.
//
// Discussion:
//
// This routine computes the diagonal AJ and sub-diagonal BJ
// elements of the order M tridiagonal symmetric Jacobi matrix
// associated with the polynomials orthogonal with respect to
// the weight function specified by KIND.
//
// For weight functions 1-7, M elements are defined in BJ even
// though only M-1 are needed. For weight function 8, BJ(M) is
// set to zero.
//
// The zero-th moment of the weight function is returned in ZEMU.
//
// Author:
//
// Original FORTRAN77 version by Sylvan Elhay, Jaroslav Kautsky.
// C++ version by John Burkardt.
//
// Reference:
//
// Sylvan Elhay, Jaroslav Kautsky,
// Algorithm 655: IQPACK, FORTRAN Subroutines for the Weights of
// Interpolatory Quadrature,
// ACM Transactions on Mathematical Software,
// Volume 13, Number 4, December 1987, pages 399-415.
//
// Parameters:
//
// Input, int KIND, the rule.
// 1, Legendre, (a,b) 1.0
// 2, Chebyshev, (a,b) ((b-x)*(x-a))^(-0.5)
// 3, Gegenbauer, (a,b) ((b-x)*(x-a))^alpha
// 4, Jacobi, (a,b) (b-x)^alpha*(x-a)^beta
// 5, Generalized Laguerre, (a,inf) (x-a)^alpha*exp(-b*(x-a))
// 6, Generalized Hermite, (-inf,inf) |x-a|^alpha*exp(-b*(x-a)^2)
// 7, Exponential, (a,b) |x-(a+b)/2.0|^alpha
// 8, Rational, (a,inf) (x-a)^alpha*(x+b)^beta
//
// Input, int M, the order of the Jacobi matrix.
//
// Input, double ALPHA, the value of Alpha, if needed.
//
// Input, double BETA, the value of Beta, if needed.
//
// Output, double AJ[M], BJ[M], the diagonal and subdiagonal
// of the Jacobi matrix.
//
// Output, double CLASS_MATRIX, the zero-th moment.
//
{
double a2b2;
double ab;
double aba;
double abi;
double abj;
double abti;
double apone;
int i;
double temp;
double temp2;
double zemu;
temp = DBL_EPSILON;
parchk(kind, 2 * m - 1, alpha, beta);
temp2 = 0.5;
if (500.0 * temp < fabs(pow(tgamma(temp2), 2) - pi)) {
cout << "\n";
cout << "CLASS_MATRIX - Fatal error!\n";
cout << " Gamma function does not match machine parameters.\n";
exit(1);
}
if (kind == 1) {
ab = 0.0;
zemu = 2.0 / (ab + 1.0);
for (i = 0; i < m; i++)
aj[i] = 0.0;
for (i = 1; i <= m; i++) {
abi = i + ab * (i % 2);
abj = 2 * i + ab;
bj[i - 1] = sqrt(abi * abi / (abj * abj - 1.0));
}
} else if (kind == 2) {
zemu = pi;
for (i = 0; i < m; i++)
aj[i] = 0.0;
bj[0] = sqrt(0.5);
for (i = 1; i < m; i++)
bj[i] = 0.5;
} else if (kind == 3) {
ab = alpha * 2.0;
zemu = pow(2.0, ab + 1.0) * pow(tgamma(alpha + 1.0), 2) / tgamma(ab + 2.0);
for (i = 0; i < m; i++)
aj[i] = 0.0;
bj[0] = sqrt(1.0 / (2.0 * alpha + 3.0));
for (i = 2; i <= m; i++)
bj[i - 1] = sqrt(i * (i + ab) / (4.0 * pow(i + alpha, 2) - 1.0));
} else if (kind == 4) {
ab = alpha + beta;
abi = 2.0 + ab;
zemu = pow(2.0, ab + 1.0) * tgamma(alpha + 1.0) * tgamma(beta + 1.0) / tgamma(abi);
aj[0] = (beta - alpha) / abi;
bj[0] = sqrt(4.0 * (1.0 + alpha) * (1.0 + beta) / ((abi + 1.0) * abi * abi));
a2b2 = beta * beta - alpha * alpha;
for (i = 2; i <= m; i++) {
abi = 2.0 * i + ab;
aj[i - 1] = a2b2 / ((abi - 2.0) * abi);
abi = abi * abi;
bj[i - 1] = sqrt(4.0 * i * (i + alpha) * (i + beta) * (i + ab) / ((abi - 1.0) * abi));
}
} else if (kind == 5) {
zemu = tgamma(alpha + 1.0);
for (i = 1; i <= m; i++) {
aj[i - 1] = 2.0 * i - 1.0 + alpha;
bj[i - 1] = sqrt(i * (i + alpha));
}
} else if (kind == 6) {
zemu = tgamma((alpha + 1.0) / 2.0);
for (i = 0; i < m; i++)
aj[i] = 0.0;
for (i = 1; i <= m; i++)
bj[i - 1] = sqrt((i + alpha * (i % 2)) / 2.0);
} else if (kind == 7) {
ab = alpha;
zemu = 2.0 / (ab + 1.0);
for (i = 0; i < m; i++)
aj[i] = 0.0;
for (i = 1; i <= m; i++) {
abi = i + ab * (i % 2);
abj = 2 * i + ab;
bj[i - 1] = sqrt(abi * abi / (abj * abj - 1.0));
}
} else if (kind == 8) {
ab = alpha + beta;
zemu = tgamma(alpha + 1.0) * tgamma(-(ab + 1.0)) / tgamma(-beta);
apone = alpha + 1.0;
aba = ab * apone;
aj[0] = -apone / (ab + 2.0);
bj[0] = -aj[0] * (beta + 1.0) / (ab + 2.0) / (ab + 3.0);
for (i = 2; i <= m; i++) {
abti = ab + 2.0 * i;
aj[i - 1] = aba + 2.0 * (ab + i) * (i - 1);
aj[i - 1] = -aj[i - 1] / abti / (abti - 2.0);
}
for (i = 2; i <= m - 1; i++) {
abti = ab + 2.0 * i;
bj[i - 1] = i * (alpha + i) / (abti - 1.0) * (beta + i) / (abti * abti) * (ab + i)
/ (abti + 1.0);
}
bj[m - 1] = 0.0;
for (i = 0; i < m; i++)
bj[i] = sqrt(bj[i]);
}
return zemu;
}
//****************************************************************************80
void imtqlx(int n, double d[], double e[], double z[])
//****************************************************************************80
//
// Purpose:
//
// IMTQLX diagonalizes a symmetric tridiagonal matrix.
//
// Discussion:
//
// This routine is a slightly modified version of the EISPACK routine to
// perform the implicit QL algorithm on a symmetric tridiagonal matrix.
//
// The authors thank the authors of EISPACK for permission to use this
// routine.
//
// It has been modified to produce the product Q' * Z, where Z is an input
// vector and Q is the orthogonal matrix diagonalizing the input matrix.
// The changes consist (essentialy) of applying the orthogonal transformations
// directly to Z as they are generated.
//
// Author:
//
// Original FORTRAN77 version by Sylvan Elhay, Jaroslav Kautsky.
// C++ version by John Burkardt.
//
// Reference:
//
// Sylvan Elhay, Jaroslav Kautsky,
// Algorithm 655: IQPACK, FORTRAN Subroutines for the Weights of
// Interpolatory Quadrature,
// ACM Transactions on Mathematical Software,
// Volume 13, Number 4, December 1987, pages 399-415.
//
// Roger Martin, James Wilkinson,
// The Implicit QL Algorithm,
// Numerische Mathematik,
// Volume 12, Number 5, December 1968, pages 377-383.
//
// Parameters:
//
// Input, int N, the order of the matrix.
//
// Input/output, double D(N), the diagonal entries of the matrix.
// On output, the information in D has been overwritten.
//
// Input/output, double E(N), the subdiagonal entries of the
// matrix, in entries E(1) through E(N-1). On output, the information in
// E has been overwritten.
//
// Input/output, double Z(N). On input, a vector. On output,
// the value of Q' * Z, where Q is the matrix that diagonalizes the
// input symmetric tridiagonal matrix.
//
{
double b;
double c;
double f;
double g;
int i;
int ii;
int itn = 30;
int j;
int k;
int l;
int m;
int mml;
double p;
double prec;
double r;
double s;
prec = DBL_EPSILON;
if (n == 1)
return;
e[n - 1] = 0.0;
for (l = 1; l <= n; l++) {
j = 0;
for (;;) {
for (m = l; m <= n; m++) {
if (m == n)
break;
if (fabs(e[m - 1]) <= prec * (fabs(d[m - 1]) + fabs(d[m])))
break;
}
p = d[l - 1];
if (m == l)
break;
if (itn <= j) {
cout << "\n";
cout << "IMTQLX - Fatal error!\n";
cout << " Iteration limit exceeded\n";
exit(1);
}
j = j + 1;
g = (d[l] - p) / (2.0 * e[l - 1]);
r = sqrt(g * g + 1.0);
g = d[m - 1] - p + e[l - 1] / (g + copysign(r, g));
s = 1.0;
c = 1.0;
p = 0.0;
mml = m - l;
for (ii = 1; ii <= mml; ii++) {
i = m - ii;
f = s * e[i - 1];
b = c * e[i - 1];
if (fabs(g) <= fabs(f)) {
c = g / f;
r = sqrt(c * c + 1.0);
e[i] = f * r;
s = 1.0 / r;
c = c * s;
} else {
s = f / g;
r = sqrt(s * s + 1.0);
e[i] = g * r;
c = 1.0 / r;
s = s * c;
}
g = d[i] - p;
r = (d[i - 1] - g) * s + 2.0 * c * b;
p = s * r;
d[i] = g + p;
g = c * r - b;
f = z[i];
z[i] = s * z[i - 1] + c * f;
z[i - 1] = c * z[i - 1] - s * f;
}
d[l - 1] = d[l - 1] - p;
e[l - 1] = g;
e[m - 1] = 0.0;
}
}
//
// Sorting.
//
for (ii = 2; ii <= m; ii++) {
i = ii - 1;
k = i;
p = d[i - 1];
for (j = ii; j <= n; j++) {
if (d[j - 1] < p) {
k = j;
p = d[j - 1];
}
}
if (k != i) {
d[k - 1] = d[i - 1];
d[i - 1] = p;
p = z[i - 1];
z[i - 1] = z[k - 1];
z[k - 1] = p;
}
}
return;
}
//****************************************************************************80
void parchk(int kind, int m, double alpha, double beta)
//****************************************************************************80
//
// Purpose:
//
// PARCHK checks parameters ALPHA and BETA for classical weight functions.
//
// Author:
//
// Original FORTRAN77 version by Sylvan Elhay, Jaroslav Kautsky.
// C++ version by John Burkardt.
//
// Reference:
//
// Sylvan Elhay, Jaroslav Kautsky,
// Algorithm 655: IQPACK, FORTRAN Subroutines for the Weights of
// Interpolatory Quadrature,
// ACM Transactions on Mathematical Software,
// Volume 13, Number 4, December 1987, pages 399-415.
//
// Parameters:
//
// Input, int KIND, the rule.
// 1, Legendre, (a,b) 1.0
// 2, Chebyshev, (a,b) ((b-x)*(x-a))^(-0.5)
// 3, Gegenbauer, (a,b) ((b-x)*(x-a))^alpha
// 4, Jacobi, (a,b) (b-x)^alpha*(x-a)^beta
// 5, Generalized Laguerre, (a,inf) (x-a)^alpha*exp(-b*(x-a))
// 6, Generalized Hermite, (-inf,inf) |x-a|^alpha*exp(-b*(x-a)^2)
// 7, Exponential, (a,b) |x-(a+b)/2.0|^alpha
// 8, Rational, (a,inf) (x-a)^alpha*(x+b)^beta
//
// Input, int M, the order of the highest moment to
// be calculated. This value is only needed when KIND = 8.
//
// Input, double ALPHA, BETA, the parameters, if required
// by the value of KIND.
//
{
double tmp;
if (kind <= 0) {
cout << "\n";
cout << "PARCHK - Fatal error!\n";
cout << " KIND <= 0.\n";
exit(1);
}
//
// Check ALPHA for Gegenbauer, Jacobi, Laguerre, Hermite, Exponential.
//
if (3 <= kind && alpha <= -1.0) {
cout << "\n";
cout << "PARCHK - Fatal error!\n";
cout << " 3 <= KIND and ALPHA <= -1.\n";
exit(1);
}
//
// Check BETA for Jacobi.
//
if (kind == 4 && beta <= -1.0) {
cout << "\n";
cout << "PARCHK - Fatal error!\n";
cout << " KIND == 4 and BETA <= -1.0.\n";
exit(1);
}
//
// Check ALPHA and BETA for rational.
//
if (kind == 8) {
tmp = alpha + beta + m + 1.0;
if (0.0 <= tmp || tmp <= beta) {
cout << "\n";
cout << "PARCHK - Fatal error!\n";
cout << " KIND == 8 but condition on ALPHA and BETA fails.\n";
exit(1);
}
}
return;
}
//****************************************************************************80
void scqf(int nt, double t[], int mlt[], double wts[], int nwts, int ndx[], double swts[],
double st[], int kind, double alpha, double beta, double a, double b)
//****************************************************************************80
//
// Purpose:
//
// SCQF scales a quadrature formula to a nonstandard interval.
//
// Discussion:
//
// The arrays WTS and SWTS may coincide.
//
// The arrays T and ST may coincide.
//
// Author:
//
// Original FORTRAN77 version by Sylvan Elhay, Jaroslav Kautsky.
// C++ version by John Burkardt.
//
// Reference:
//
// Sylvan Elhay, Jaroslav Kautsky,
// Algorithm 655: IQPACK, FORTRAN Subroutines for the Weights of
// Interpolatory Quadrature,
// ACM Transactions on Mathematical Software,
// Volume 13, Number 4, December 1987, pages 399-415.
//
// Parameters:
//
// Input, int NT, the number of knots.
//
// Input, double T[NT], the original knots.
//
// Input, int MLT[NT], the multiplicity of the knots.
//
// Input, double WTS[NWTS], the weights.
//
// Input, int NWTS, the number of weights.
//
// Input, int NDX[NT], used to index the array WTS.
// For more details see the comments in CAWIQ.
//
// Output, double SWTS[NWTS], the scaled weights.
//
// Output, double ST[NT], the scaled knots.
//
// Input, int KIND, the rule.
// 1, Legendre, (a,b) 1.0
// 2, Chebyshev Type 1, (a,b) ((b-x)*(x-a))^(-0.5)
// 3, Gegenbauer, (a,b) ((b-x)*(x-a))^alpha
// 4, Jacobi, (a,b) (b-x)^alpha*(x-a)^beta
// 5, Generalized Laguerre, (a,+oo) (x-a)^alpha*exp(-b*(x-a))
// 6, Generalized Hermite, (-oo,+oo) |x-a|^alpha*exp(-b*(x-a)^2)
// 7, Exponential, (a,b) |x-(a+b)/2.0|^alpha
// 8, Rational, (a,+oo) (x-a)^alpha*(x+b)^beta
// 9, Chebyshev Type 2, (a,b) ((b-x)*(x-a))^(+0.5)
//
// Input, double ALPHA, the value of Alpha, if needed.
//
// Input, double BETA, the value of Beta, if needed.
//
// Input, double A, B, the interval endpoints.
//
{
double al;
double be;
int i;
int k;
int l;
double p;
double shft;
double slp;
double temp;
double tmp;
temp = DBL_EPSILON;
parchk(kind, 1, alpha, beta);
if (kind == 1) {
al = 0.0;
be = 0.0;
if (fabs(b - a) <= temp) {
cout << "\n";
cout << "SCQF - Fatal error!\n";
cout << " |B - A| too small.\n";
exit(1);
}
shft = (a + b) / 2.0;
slp = (b - a) / 2.0;
} else if (kind == 2) {
al = -0.5;
be = -0.5;
if (fabs(b - a) <= temp) {
cout << "\n";
cout << "SCQF - Fatal error!\n";
cout << " |B - A| too small.\n";
exit(1);
}
shft = (a + b) / 2.0;
slp = (b - a) / 2.0;
} else if (kind == 3) {
al = alpha;
be = alpha;
if (fabs(b - a) <= temp) {
cout << "\n";
cout << "SCQF - Fatal error!\n";
cout << " |B - A| too small.\n";
exit(1);
}
shft = (a + b) / 2.0;
slp = (b - a) / 2.0;
} else if (kind == 4) {
al = alpha;
be = beta;
if (fabs(b - a) <= temp) {
cout << "\n";
cout << "SCQF - Fatal error!\n";
cout << " |B - A| too small.\n";
exit(1);
}
shft = (a + b) / 2.0;
slp = (b - a) / 2.0;
} else if (kind == 5) {
if (b <= 0.0) {
cout << "\n";
cout << "SCQF - Fatal error!\n";
cout << " B <= 0\n";
exit(1);
}
shft = a;
slp = 1.0 / b;
al = alpha;
be = 0.0;
} else if (kind == 6) {
if (b <= 0.0) {
cout << "\n";
cout << "SCQF - Fatal error!\n";
cout << " B <= 0.\n";
exit(1);
}
shft = a;
slp = 1.0 / sqrt(b);
al = alpha;
be = 0.0;
} else if (kind == 7) {
al = alpha;
be = 0.0;
if (fabs(b - a) <= temp) {
cout << "\n";
cout << "SCQF - Fatal error!\n";
cout << " |B - A| too small.\n";
exit(1);
}
shft = (a + b) / 2.0;
slp = (b - a) / 2.0;
} else if (kind == 8) {
if (a + b <= 0.0) {
cout << "\n";
cout << "SCQF - Fatal error!\n";
cout << " A + B <= 0.\n";
exit(1);
}
shft = a;
slp = a + b;
al = alpha;
be = beta;
} else if (kind == 9) {
al = 0.5;
be = 0.5;
if (fabs(b - a) <= temp) {
cout << "\n";
cout << "SCQF - Fatal error!\n";
cout << " |B - A| too small.\n";
exit(1);
}
shft = (a + b) / 2.0;
slp = (b - a) / 2.0;
}
p = pow(slp, al + be + 1.0);
for (k = 0; k < nt; k++) {
st[k] = shft + slp * t[k];
l = abs(ndx[k]);
if (l != 0) {
tmp = p;
for (i = l - 1; i <= l - 1 + mlt[k] - 1; i++) {
swts[i] = wts[i] * tmp;
tmp = tmp * slp;
}
}
}
return;
}
//****************************************************************************80
void sgqf(int nt, double aj[], double bj[], double zemu, double t[], double wts[])
//****************************************************************************80
//
// Purpose:
//
// SGQF computes knots and weights of a Gauss Quadrature formula.
//
// Discussion:
//
// This routine computes all the knots and weights of a Gauss quadrature
// formula with simple knots from the Jacobi matrix and the zero-th
// moment of the weight function, using the Golub-Welsch technique.
//
// Author:
//
// Original FORTRAN77 version by Sylvan Elhay, Jaroslav Kautsky.
// C++ version by John Burkardt.
//
// Reference:
//
// Sylvan Elhay, Jaroslav Kautsky,
// Algorithm 655: IQPACK, FORTRAN Subroutines for the Weights of
// Interpolatory Quadrature,
// ACM Transactions on Mathematical Software,
// Volume 13, Number 4, December 1987, pages 399-415.
//
// Parameters:
//
// Input, int NT, the number of knots.
//
// Input, double AJ[NT], the diagonal of the Jacobi matrix.
//
// Input/output, double BJ[NT], the subdiagonal of the Jacobi
// matrix, in entries 1 through NT-1. On output, BJ has been overwritten.
//
// Input, double ZEMU, the zero-th moment of the weight function.
//
// Output, double T[NT], the knots.
//
// Output, double WTS[NT], the weights.
//
{
int i;
//
// Exit if the zero-th moment is not positive.
//
if (zemu <= 0.0) {
cout << "\n";
cout << "SGQF - Fatal error!\n";
cout << " ZEMU <= 0.\n";
exit(1);
}
//
// Set up vectors for IMTQLX.
//
for (i = 0; i < nt; i++)
t[i] = aj[i];
wts[0] = sqrt(zemu);
for (i = 1; i < nt; i++)
wts[i] = 0.0;
//
// Diagonalize the Jacobi matrix.
//
imtqlx(nt, t, bj, wts);
for (i = 0; i < nt; i++)
wts[i] = wts[i] * wts[i];
return;
}
//****************************************************************************80
template<int SIGN>
void symmetrize(int n, double x[])
//****************************************************************************80
//
// Purpose:
//
// correct the slight assymmetry introduced by the handling of 0 in the
// call to copysign in function imtqlx by averaging f(x) and f(-x) or
// f(x) and -f(-x) (odd vs even case).
//
// Author:
//
// Joachim Wuttke
//
{
for(int i=0; i<=n/2; ++i) {
double tmp = (x[i] + SIGN * x[n-1-i]) / 2;
x[n-1-i] = SIGN * tmp;
x[i] = tmp;
}
}
|