-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_quadrature.F90
1483 lines (1200 loc) · 43.3 KB
/
mod_quadrature.F90
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
module quadrature
use precision
implicit none
private
real(kind=real2), parameter :: pi = 4d0*atan(1d0)
public :: quad
public :: gaussj_nodes,gaussj_weights,gauss_Jacobi
public :: gaussl_nodes,gaussl_weights,gauss_Legendre
public :: lobatto_nodes,lobatto_weights
public :: kronrod_nodes,kronrod_weights
public :: christoffel_nodes,christoffel_weights,gauss_Christoffel
public :: clenshaw_curtis
contains
!**********************************************************************
!> @brief Calculate Gauss-Legendre Nodes
!> @par Calculates the Gauss-Legendre nodes using Gauss-Jacobi
!> @param[in] n order (number of points)
!> @param[out] x nodes
!**********************************************************************
function gaussl_nodes(n) result(x)
use precision
implicit none
integer(kind=int1), intent(in) :: n
real(kind=real2) :: x(n)
real(kind=real2) :: w(n)
call quad('legendre',n,x,w,-1d0,1d0)
return
end function gaussl_nodes
!**********************************************************************
!> @brief Calculate Gauss-Legendre Weights
!> @par Calculates the Gauss-Legendre weights using Gauss-Jacobi
!> @param[in] n order (number of points)
!> @param[out] w weights
!**********************************************************************
function gaussl_weights(n) result(w)
use precision
implicit none
integer(kind=int1), intent(in) :: n
real(kind=real2) :: w(n)
real(kind=real2) :: x(n)
call quad('legendre',n,x,w,-1d0,1d0)
return
end function gaussl_weights
!**********************************************************************
!> @brief Calculate Gauss-Legendre Quadrature
!> @par Calculates the Gauss-Legendre nodes and weights using Gauss-Jacobi
!> @param[in] n order (number of points)
!> @param[out] x nodes
!> @param[out] w weights
!**********************************************************************
subroutine gauss_Legendre(n,x,w)
use precision
implicit none
integer(kind=int1), intent(in) :: n
real(kind=real2), intent(out) :: x(n),w(n)
call quad('legendre',n,x,w,-1d0,1d0)
return
end subroutine gauss_Legendre
!**********************************************************************
!> @brief Calculate Gauss-Jacobi Nodes
!> @par Calculates the Gauss-Jacobi nodes using Golub-Welsch
!> @param[in] n order (number of points)
!> @param[in] alpha Jacobi weight function alpha parameter
!> @param[in] beta Jacobi weight function beta parameter
!> @param[out] x nodes
!**********************************************************************
function gaussj_nodes(n,alpha,beta) result(x)
use precision
implicit none
integer(kind=int1), intent(in) :: n
real(kind=real2), intent(in) :: alpha,beta
real(kind=real2) :: x(n)
real(kind=real2) :: w(n)
call gauss_Jacobi(n,alpha,beta,x,w)
return
end function gaussj_nodes
!**********************************************************************
!> @brief Calculate Gauss-Jacobi Weights
!> @par Calculates the Gauss-Jacobi weights using Golub-Welsch
!> @param[in] n order (number of points)
!> @param[in] alpha Jacobi weight function alpha parameter
!> @param[in] beta Jacobi weight function beta parameter
!> @param[out] w weights
!**********************************************************************
function gaussj_weights(n,alpha,beta) result(w)
use precision
implicit none
integer(kind=int1), intent(in) :: n
real(kind=real2), intent(in) :: alpha,beta
real(kind=real2) :: w(n)
real(kind=real2) :: x(n)
call gauss_Jacobi(n,alpha,beta,x,w)
return
end function gaussj_weights
!**********************************************************************
!> @brief Calculate Gauss-Lobatto Nodes
!> @par Calculates the Gauss-Lobatto nodes using Gauss-Legendre
!> @param[in] n order (number of points)
!> @param[out] x nodes
!**********************************************************************
function lobatto_nodes(n) result(x)
use precision
implicit none
integer(kind=int1), intent(in) :: n
real(kind=real2) :: x(n)
if(n .ge. 3)then
x(1) = -1d0
x(2:n-1) = gaussl_nodes(n-2)
x(n) = 1d0
elseif(n .eq. 2)then
x(1) = -1d0
x(n) = 1d0
else
print *,'ERROR :: INVALID LOBATTO QUAD ORDER'
stop
endif
return
end function lobatto_nodes
!**********************************************************************
!> @brief Calculate Gauss-Lobatto Weights
!> @par Calculates the Gauss-Lobatto nodes using Gauss-Legendre
!> @param[in] n order (number of points)
!> @param[out] w weight
!**********************************************************************
function lobatto_weights(n) result(w)
use precision
use polynomial, only : legendre
implicit none
integer(kind=int1), intent(in) :: n
real(kind=real2) :: w(n)
integer(kind=int2) :: in
real(kind=real2) :: x(n),rnnm1
x = lobatto_nodes(n)
rnnm1 = 2d0/dble(n*(n-1d0))
do in=1,n
w(in) = 1d0/(legendre(x(in),n-1))
w(in) = rnnm1*w(in)*w(in)
enddo
return
end function lobatto_weights
!**********************************************************************
function kronrod_nodes(n) result(x)
use precision
implicit none
integer(kind=int1), intent(in) :: n
real(kind=real2) :: x(n)
integer(kind=int1) :: m,in,i
real(kind=real2), allocatable :: x_in(:),w1_in(:),w2_in(:)
m = n/2
allocate(x_in(m+1)); allocate(w1_in(m+1)); allocate(w2_in(m+1))
call kronrod(m,x_in,w1_in,w2_in)
i = 0
do in=1,m+mod(m,2)
i = i + 1
x(i) = x_in(in)
enddo
do in=m,1,-1
i = i + 1
x(i) = -x_in(in)
enddo
return
end function kronrod_nodes
!**********************************************************************
function kronrod_weights(n) result(w)
use precision
implicit none
integer(kind=int1), intent(in) :: n
real(kind=real2) :: w(n)
integer(kind=int1) :: m,in,i
real(kind=real2), allocatable :: x_in(:),w1_in(:),w2_in(:)
m = n/2
allocate(x_in(m+1)); allocate(w1_in(m+1)); allocate(w2_in(m+1))
call kronrod(m,x_in,w1_in,w2_in)
i = 0
do in=1,m+mod(m,2)
i = i + 1
w(i) = w1_in(in)
enddo
do in=m,1,-1
i = i + 1
w(i) = w1_in(in)
enddo
return
end function kronrod_weights
!**********************************************************************
function christoffel_nodes(n,r) result(x)
use precision
implicit none
integer(kind=int1), intent(in) :: n
real(kind=real2), intent(in) :: r(:)
real(kind=real2) :: x(n)
real(kind=real2) :: w(n)
call gauss_Christoffel(n,r,x,w)
return
end function christoffel_nodes
!**********************************************************************
function christoffel_weights(n,r) result(w)
use precision
implicit none
integer(kind=int1), intent(in) :: n
real(kind=real2), intent(in) :: r(:)
real(kind=real2) :: w(n)
real(kind=real2) :: x(n)
call gauss_Christoffel(n,r,x,w)
return
end function christoffel_weights
!**********************************************************************
!> @breif computes Gauss-Christoffel quadrature for measure desrcibed in r
!
!> @par this computes the nodes and weights according to gauss-christoffel
!! for a polynomial measure set out in r. The form or r are the monomial
!! coefficents of the measure. Such that:
!! measure = r(1)*x^0 + r(2)*x^1 + r(3)*x^2 + ...
!! This assumes the range of integration is [-1,1]
!! Complexity is O(n^3). Could be better, but ok for n<~500.
!
!> @param[in] n order
!> @param[in] r array of monomial coefficents of measure
!> @param[out] x nodes
!> @param[out] w weights
!**********************************************************************
subroutine gauss_Christoffel(n,r,x,w)
use precision
use maths, only : eigen_stdd,eigen_std
implicit none
integer(kind=int1), intent(in) :: n
real(kind=real2), intent(in) :: r(:)
real(kind=real2), intent(out) :: x(n),w(n)
integer(kind=int1) :: nq,i,lwork,liwork
real(kind=real2), allocatable :: xg(:),wg(:)
real(kind=real2) :: a(n),b(n),mu0,v(n,n)
nq = ceiling(dble(2*(n-1)+size(r))/2d0) + 1
allocate(wg(nq)); allocate(xg(nq))
call gauss_Legendre(nq,xg,wg)
call christoffel_ab(n,r,xg,wg,a,b,mu0)
if(n .le. 500)then
call sgqf(n,a,b,mu0,x,w)
else
lwork = 1 + 4*n + n*n
liwork = 3 + 5*n
call eigen_stdd(n,lwork,liwork,a,b,x,v)
do i=1,n
w(i) = mu0*v(1,i)*v(1,i)
enddo
endif
return
end subroutine gauss_Christoffel
!**********************************************************************
!> @brief calculates the normalised polynomial coefficents of polynomial
!! recurrsion
!
!> @param[in] n order
!> @param[in] w quadrature measure
!> @param[in] xg quadrature points used to integrate
!> @param[in] wg quadrature weights used to integrate
!> @param[out] a first recurrsion coefficient
!> @param[out] b second recurrsion coefficient
!> @param[out] mu0 zeroth order moment of measure
!**********************************************************************
subroutine christoffel_ab(n,w,xg,wg,a,b,mu0)
use precision
use polynomial, only : inproduct,polyval,poly_recursion_single
implicit none
integer(kind=int1), intent(in) :: n
real(kind=real2), intent(in) :: w(:),xg(:),wg(:)
real(kind=real2), intent(out) :: a(n),b(n),mu0
integer(kind=int1) :: i,nx
real(kind=real2) :: at(n+1),bt(n+1)
real(kind=real2) :: int,xint,intm1
real(kind=real2) :: wx(size(xg)),ww(size(w)+1),wxx(size(xg))
real(kind=real2) :: pm1(size(xg)),p(size(xg)),pp1(size(xg))
real(kind=real2), parameter :: tol = sqrt(epsilon(int))
nx = size(xg)
ww(1) = 0d0
ww(2:) = w(1:)
wx = polyval(w,xg)
wxx = polyval(ww,xg)
pm1(:) = 0d0
p(:) = 1d0
int = inproduct(nx,p,wx,xg,wg); xint = inproduct(nx,p,wxx,xg,wg)
mu0 = int
at(1) = xint/int
bt(1) = 1d0
do i=1,n
intm1 = int
pp1 = poly_recursion_single(at(i),bt(i),xg,p,pm1)
pm1 = p
p = pp1
int = inproduct(nx,p,wx ,xg,wg)
xint = inproduct(nx,p,wxx,xg,wg)
at(i+1) = xint/int
bt(i+1) = int/intm1
enddo
a(1:n) = at(1:n)
do i=1,n
if(bt(i+1) .gt. tol) then
b(i) = sqrt(abs(bt(i+1)))
else
b(i) = 0d0
endif
enddo
return
end subroutine christoffel_ab
!**********************************************************************
!> @breif computes a Clenshaw Curtis quadrature rule.
!
!> @par Computes a Clenshaw Curtis quadrature rule with the convention
!! that the abscissas are numbered from left to right
!
!> @author John Burkardt, Will Trojak
!
!> @param[in] order the order of the 1D quadrature
!> @param[out] x the abscissas
!> @param[out] w the weights
!**********************************************************************
subroutine clenshaw_curtis(order,x,w)
use precision
implicit none
integer(kind=int1), intent(in) :: order
real(kind=real2), intent(out) :: x(order),w(order)
real(kind=real2), parameter :: pi = 4d0*atan(1d0)
integer(kind=int1) :: i,j
real(kind=real2) :: theta,b,rom1
if(order .lt. 1) then
print *,'ERROR: CLENSHAW QUAD ORDER TOO SMALL'
stop
endif
if(order .eq. 1) then
x(1) = 0d0
w(1) = 2d0
return
endif
rom1 = 1d0/real(order - 1)
do i=1,order
x(i) = cos(real(order - i)*pi*rom1)
theta = real(i - 1)*pi*rom1
w(i) = 1d0
b = 2d0
do j=1,(order - 1)/2-1
w(i) = w(i) - b*cos(2d0*real(j)*theta)/real(4*j*j - 1)
enddo
b = 1d0
j = (order - 1)/2
w(i) = w(i) - b*cos(2d0*real(j)*theta)/real(4*j*j - 1)
w(i) = 2d0*w(i)*rom1
enddo
x(1) = -1d0
if(mod(order,2) .eq. 1)then
x((order+1)/2) = 0d0
endif
x(order) = 1d0
w(1) = 0.5d0*w(1)
!w(2:order-1) = 2d0*w(2:order-1)/real(order - 1)
w(order) = 0.5d0*w(order)
return
end subroutine clenshaw_curtis
!**********************************************************************
subroutine gauss_Jacobi(n,alpha,beta,x,w)
use precision
implicit none
integer(kind=int1), intent(in) :: n
real(kind=real2), intent(in) :: alpha,beta
real(kind=real2), intent(out) :: x(n),w(n)
integer(kind=int1) :: qtype
real(kind=real2) :: a,b
a = -1d0; b = 1d0
qtype = 4
if(alpha .le. -1d0) then
print *,'ERROR: JACOBI POLY \alpha < -1'
stop
endif
if(beta .le. -1d0) then
print *,'ERROR: JACOBI POLY \beta < -1'
stop
endif
call cgqf(n,qtype,alpha,beta,a,b,x,w)
return
end subroutine gauss_Jacobi
!**********************************************************************
subroutine quad(qtype_str,n,x,w,a0,b0,c0,d0)
use precision
implicit none
character(*), intent(in) :: qtype_str
integer(kind=int1), intent(in) :: n
real(kind=real2), intent(out) :: x(n),w(n)
real(kind=real2), optional, intent(in) :: a0,b0,c0,d0
integer(kind=int1) :: qtype
real(kind=real2) :: a,b,c,d
a = 0d0
b = 0d0
if(present(a0)) a = a0
if(present(b0)) b = b0
select case(qtype_str)
case('legendre') ! x = (c,d), w = 1.0
qtype = 1
c = -1d0; d = 1d0
if(present(c0)) c = c0
if(present(d0)) d = d0
case('chebyshev') ! x = (c,d), w = ((d-x)*(x-c))^(-0.5)
qtype = 2
c = -1d0; d = 1d0
if(present(c0)) c = c0
if(present(d0)) d = d0
case('gegenbauer') ! x = (c,d), w = ((d-x)*(x-c))^a
qtype = 3
c = -1d0; d = 1d0
if(present(c0)) c = c0
if(present(d0)) d = d0
case('jacobi') ! x = (c,d), w = (d-x)^a*(x-c)^b
qtype = 4
c = -1d0; d = 1d0
if(present(c0)) c = c0
if(present(d0)) d = d0
case('laguerre') ! x = (c,inf), w = (x-c)^a*exp(-d*(x-c))
qtype = 5
c = 0d0; d = 0d0
if(present(c0)) c = c0
if(present(d0)) d = d0
case('hermite') ! x = (-inf,inf), w = |x-c|^a*exp(-d*(x-c)^2)
qtype = 6
c = 0d0; d = 0d0
if(present(c0)) c = c0
if(present(d0)) d = d0
case('exponential') ! x = (c,d), w = |x-(c+d)/2.0|^a
qtype = 7
c = -1d0; d = 1d0
if(present(c0)) c = c0
if(present(d0)) d = d0
case('rational') ! x = (c,inf), w = (x-c)^a*(x+d)^b
qtype = 8
c = 0d0; d = 0d0
if(present(c0)) c = c0
if(present(d0)) d = d0
case default
qtype = 1
!print *,'defualting to Guass-Legendre'
c = -1d0; d = 1d0
if(present(c0)) c = c0
if(present(d0)) d = d0
end select
call cgqf(n,qtype,a,b,c,d,x,w)
return
end subroutine quad
!**********************************************************************
!! 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: Sylvan Elhay, Jaroslav Kautsky and, John Burkardt (F90)
!
! Parameters:
!
! Input, integer(kind=int1) :: NT, the number of knots.
! Input, integer(kind=int1) :: 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, real(kind=real2) :: ALPHA, the value of Alpha, if needed.
! Input, real(kind=real2) :: BETA, the value of Beta, if needed.
! Output, real(kind=real2) :: T(NT), the knots.
! Output, real(kind=real2) :: WTS(NT), the weights.
!**********************************************************************
subroutine cdgqf(nt,qtype,alpha,beta,t,wts)
use precision
implicit none
integer(kind=int1), intent(in) :: nt,qtype
real(kind=real2), intent(in) :: alpha,beta
real(kind=real2), intent(out) :: t(nt),wts(nt)
real(kind=real2) :: zemu,aj(nt),bj(nt)
! Get the Jacobi matrix and zero-th moment.
call class_matrix(qtype,nt,alpha,beta,aj,bj,zemu)
! Compute the knots and weights.
call sgqf(nt,aj,bj,zemu,t,wts)
return
end subroutine cdgqf
!**********************************************************************
!! CGQF computes knots and weights of a Gauss quadrature formula.
!
! Licensing:
! This code is distributed under the GNU LGPL license.
!
! Author: Sylvan Elhay, Jaroslav Kautsky and, John Burkardt (F90)
!
! 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, integer(kind=int1) :: NT, the number of knots.
! Input, real(kind=real2) :: ALPHA, the value of Alpha, if needed.
! Input, real(kind=real2) :: BETA, the value of Beta, if needed.
! Input, real(kind=real2) :: A, B, the interval endpoints, or
! other parameters.
! Output, real(kind=real2) :: T(NT), the knots.
! Output, real(kind=real2) :: WTS(NT), the weights.
!**********************************************************************
subroutine cgqf(nt,qtype,alpha,beta,a,b,t,wts)
use precision
implicit none
integer(kind=int1), intent(in) :: nt,qtype
real(kind=real2), intent(in) :: alpha,beta,a,b
real(kind=real2), intent(out) :: t(nt),wts(nt)
integer(kind=int1) :: i
integer(kind=int1), allocatable :: mlt(:),ndx(:)
! Compute the Gauss quadrature for default a & b
call cdgqf(nt,qtype,alpha,beta,t,wts)
! Prepare to scale the quadrature to other weight function a & b
allocate(mlt(nt))
mlt = 1
allocate(ndx(nt))
do i=1,nt
ndx(i) = i
enddo
call scqf(nt,t,mlt,wts,nt,ndx,wts,t,qtype,alpha,beta,a,b)
deallocate(mlt)
deallocate(ndx)
return
end subroutine cgqf
!**********************************************************************
!! 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: Sylvan Elhay, Jaroslav Kautsky and, John Burkardt (F90)
!
! 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, integer(kind=int1) :: M, the order of the Jacobi matrix.
! Input, real(kind=real2) :: ALPHA, the value of Alpha, if needed.
! Input, real(kind=real2) :: BETA, the value of Beta, if needed.
! Output, real(kind=real2) :: AJ(M), BJ(M), the diagonal and subdiagonal
! of the Jacobi matrix.
! Output, real(kind=real2) :: ZEMU, the zero-th moment.
!**********************************************************************
subroutine class_matrix(qtype,m,alpha,beta,aj,bj,zemu)
use precision
implicit none
integer(kind=int1), intent(in) :: m,qtype
real(kind=real2), intent(in) :: alpha,beta
real(kind=real2), intent(out) :: aj(m),bj(m),zemu
integer(kind=int1) :: i
real(kind=real2) :: a2b2,ab,aba,abi,abj,abti,apone
real(kind=real2) :: temp,temp2
real(kind=real2), parameter :: pi = 4d0*atan(1d0)
temp = epsilon(temp)
temp2 = 0.5d0
if(500d0*temp .lt. abs(gamma(temp2)**2 - pi)) then
print *,'ERROR: GAMMA FUNCTION INACCURATE'
stop
endif
select case(qtype)
case(1) ! Legendre
ab = 0d0
zemu = 2d0/(ab + 1d0)
aj = 0d0
do i=1,m
abi = i + ab*mod(i,2)
abj = 2*i + ab
bj(i) = abi*abi/(abj*abj - 1d0)
enddo
bj(:) = sqrt(bj(:))
case(2) ! Chebyshev
zemu = pi
aj = 0d0
bj(1) = sqrt(0.5d0)
bj(2:m) = 0.5d0
case(3) ! Gegenbauer
ab = alpha*2d0
zemu = 2d0**(ab + 1d0)*gamma(alpha + 1d0)**2/gamma(ab + 2d0)
aj = 0d0
bj(1) = 1d0/(2d0*alpha + 3d0)
do i=2,m
bj(i) = i*(i + ab)/(4d0*(i + alpha)**2 - 1d0)
enddo
bj(:) = sqrt(bj(:))
case(4) ! Jacobi
ab = alpha + beta
abi = 2d0 + ab
zemu = 2d0**(ab + 1d0)*gamma(alpha + 1d0)*gamma(beta + 1d0)/gamma(abi)
aj(1) = (beta - alpha)/abi
bj(1) = 4d0*(1d0 + alpha)*(1d0 + beta)/((abi + 1d0)*abi*abi)
a2b2 = beta*beta - alpha*alpha
do i=2,m
abi = 2d0*i + ab
aj(i) = a2b2/((abi - 2d0)*abi)
abi = abi**2
bj(i) = 4d0*i*(i + alpha)*(i + beta)*(i + ab)/((abi - 1d0)*abi)
enddo
bj(:) = sqrt(bj(:))
case(5) ! Generalized Laguerre
zemu = gamma(alpha + 1d0)
do i=1,m
aj(i) = 2d0*i - 1d0 + alpha
bj(i) = i*(i + alpha)
enddo
bj(:) = sqrt(bj(:))
case(6) ! Generalized Hermite
zemu = gamma((alpha + 1d0)/2d0)
aj = 0d0
do i=1,m
bj(i) = 0.5d0*(i + alpha*mod(i,2))
enddo
bj(:) = sqrt(bj(:))
case(7) ! Exponential
ab = alpha
zemu = 2d0/(ab + 1d0)
aj = 0d0
do i=1,m
abi = i + ab*mod(i,2)
abj = 2*i + ab
bj(i) = abi*abi/(abj*abj - 1d0)
enddo
bj(:) = sqrt(bj(:))
case(8) ! Rational
ab = alpha + beta
zemu = gamma(alpha + 1d0)*gamma(-(ab + 1d0))/gamma(-beta)
apone = alpha + 1d0
aba = ab*apone
aj(1) = -apone/(ab + 2d0)
bj(1) = -aj(1)*(beta + 1d0)/(ab + 2d0)/(ab + 3d0)
do i=2,m
abti = ab + 2d0*i
aj(i) = aba + 2d0*(ab + i)*(i - 1)
aj(i) = -aj(i)/abti/(abti - 2d0)
enddo
do i=2,m-1
abti = ab + 2d0*i
bj(i) = i*(alpha + i)/(abti - 1d0)*(beta + i)/(abti**2)*(ab + i)/(abti + 1d0)
enddo
bj(m) = 0d0
bj(:) = sqrt(bj(:))
end select
return
end subroutine class_matrix
!**********************************************************************
!! IMTQLX diagonalizes a symmetric tridiagonal matrix.
!
! Discussion:
!
! This routine is a modified version of EISPACK implicit QL algorithm for
! symmetric tridiagonal matrix.
!
! It is 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 (essentially) of applying the orthogonal
! transformations directly to Z as they are generated.
!
! Author: Sylvan Elhay, Jaroslav Kautsky and, John Burkardt (F90)
!
! 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, integer(kind=int1) :: N, the order of the matrix.
! Input/output, real(kind=real2) :: D(N), the diagonal entries of the matrix.
! On output, the information in D has been overwritten.
!
! Input/output, real(kind=real2) :: 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, real(kind=real2) :: 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.
!**********************************************************************
subroutine imtqlx(n,d,e,z)
use precision
implicit none
integer(kind=int1), intent(in) :: n
real(kind=real2), intent(inout) :: d(n),e(n),z(n)
integer(kind=int1) :: i,ii,j,k,l,m,mml
integer(kind=int1), parameter :: itn = 30
real(kind=real2) :: b,c,f,g,p,prec,r,s
prec = epsilon(prec)
if(n .eq. 1) return
e(n) = 0d0
do l=1,n
j = 0
do
do m=l,n-1
!if(m .eq. n) exit
if(abs(e(m)) .le. prec*(abs(d(m)) + abs(d(m+1)))) exit
enddo
p = d(l)
if(m .eq. l) exit
if(itn .le. j) then
print *,'ERROR: IMTQLX CONV FAIL'
stop
endif
j = j + 1
g = (d(l+1) - p)/(2d0*e(l))
r = sqrt(g*g + 1d0)
g = d(m) - p + e(l)/(g + sign(r,g))
s = 1d0; c = 1d0; p = 0d0
mml = m - l
do ii=1,mml
i = m - ii
f = s*e(i); b = c*e(i)
if(abs(g) .le. abs(f)) then
c = g/f
r = sqrt(c*c + 1d0)
e(i+1) = f*r
s = 1d0/r
c = c*s
else
s = f/g
r = sqrt(s*s + 1d0)
e(i+1) = g*r
c = 1d0/r
s = s*c
end if
g = d(i+1) - p
r = (d(i) - g)*s + 2d0*c*b
p = s*r
d(i+1) = g + p
g = c*r - b
f = z(i+1)
z(i+1) = s*z(i) + c*f
z(i) = c*z(i) - s*f
enddo
d(l) = d(l) - p; e(l) = g; e(m) = 0d0
enddo
enddo
! Sorting.
do ii=2,n
i = ii - 1; k = i
p = d(i)
do j=ii,n
if(d(j) .lt. p) then
k = j; p = d(j)
endif
enddo
if(k .ne. i) then
d(k) = d(i); d(i) = p
p = z(i)
z(i) = z(k); z(k) = p
endif
enddo
return
end subroutine imtqlx
!**********************************************************************
!! 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: Sylvan Elhay, Jaroslav Kautsky and, John Burkardt (F90)
!
! 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, integer(kind=int1) :: NT, the number of knots.
! Input, real(kind=real2) :: T(NT), the original knots.
! Input, integer(kind=int1) :: MLT(NT), the multiplicity of the knots.
! Input, real(kind=real2) :: WTS(NWTS), the weights.