-
Notifications
You must be signed in to change notification settings - Fork 4
/
legepw.f90
1541 lines (1242 loc) · 39.2 KB
/
legepw.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
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! This module contains code for constructing and manipulating piecewise Legendre
! expansions. By a piecewise Legendre expansion of order n on the subintervals
!
! (a_1, b_1), (a_2,b_2), ... (a_m,b_m),
!
! we mean a sum of the form
!
! m n-1
! f(x) = \sum \chi (x) \sum c \tilde{P} (x), (2)
! j=1 [a_j, b_j) i=0 i,j i,j
!
!
! where \chi_[a_j,b_j) is the characteristic function of the half-open interval
! [a_j, b_j) and \tilde{P}_i,j(x) denotes the L^2 normalized Legendre polynomial of
! degree i on the interval (a_j,b_j). That is,
!
!
! ( 2n+1 ) ( 2 a_j+b_j )
! \tilde{P}_i,j(x) = sqrt( ----- ) P_i ( ------- x + ------- ) ,
! ( b-a ) ( b_j-a_j a_j-b_j )
!
!
! with P_i(x) the Legendre polynomial of degree i. This module provides routines
! for handling both real-valued and complex-values expansions of this type.
!
! Expansions are represented via either the vector
!
! ( a_0,0 )
! ( a_1,0 )
! ( ... )
! ( a_{n-1},0 )
! ( a_0,1 )
! ( a_1,1 )
! ( ... ) (3)
! ( a_{n-1},1 )
! ( ... )
! ( a_{0},{m-1} )
! ( ... )
! ( a_{n-1},{m-1} )
!
! of expansion coefficients or via the vector
!
! ( f(x_1) \sqrt{w_1} )
! ( f(x_n) \sqrt{w_2} )
! ( ... ) (4)
! ( f(x_mn) \sqrt{w_mn} )
!
! of the *scaled* values of the expansion at the nodes of the mn-point quadrature
! rule constructing by amalgamating the n-point Gauss-Legendre rules on each
! of the intervals (a_j,b_j]. We refer to this mechanism for representing
! functions as a ``piecewise Legendre discretization scheme'' or simply a
! ``discretization scheme'' and the amalgamated quadrature as a piecewise Legendre
! quadrature or the discretization quadrature.
!
! The following subroutines should be regarded as publicly callable:
!
! legepw_init - initialize the data structure describing a piecewise
! Legendre discretization scheme; the user must supply an initial
! interval or list of intervals for the scheme
!
! legepw_uniform - refine a discretization scheme until the length of every
! subinterval is at most equal to a specified quantity
!
! legepw_adap - refine a discretization scheme until it suffices to represent a
! user-specified collection of functions to a specified precision
!
! legepw_quad - return the piecewise Legendre quadrature rule associated with
! a discretization scheme
!
! legepw_coefs - given the *scaled* values of one or more piecewise Legendre
! expansions at the nodes of the discretization quadrature, compute their
! piecewise Legendre coefficient expansions
!
! legepw_diff - given the *scaled* values of one or more piecewise Legendre
! expansions at the nodes of the discretization quadrature, compute the
! *scaled* values of their first derivatives at the nodes of the
! discretization quadrature
!
! **WARNING** THE CONDITION NUMBER OF SPECTRAL DIFFERENTIATION
! DETERIORATES WITH INCREASING ORDER
!
! legepw_eval - given the vector of coefficients for one or more expansions
! of the form (2), compute their values at a specified point
!
! legepw_evalder - given the vector of coefficients for one or more expansions
! of the form (2), compute their values and their first derivative
! at a specified point
!
! legepw_interp - given the *scaled* values of one or more piecewise Legendre
! expansions at the nodes of the discretization quadrature, evaluate them at a
! specified point
!
! legepw_order - increase the order of the Legendre expansions used to represent
! functions on each subinterval; this routine is principally useful for
! representing products of the form
!
! f_i(x) g_j(x) i = 1,....,n, j = 1,...,m
!
! by discretizing the collections { f_i } and { g_j } separately
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
module legepw
use utils
use legendre
use iso_c_binding
type legepw_disc
integer :: nlege ! the length of the Gauss-Legendre quadrature
double precision, allocatable :: xslege(:) ! the Gauss-Legendre quadrature nodes
double precision, allocatable :: whtslege(:) ! the Gauss-Legendre quadrature weights
double precision, allocatable :: u(:,:) ! the weighted values to coefs matrix
double precision, allocatable :: adiff(:,:) ! the spectral differentiation matrix
! second quadrature for adaptive integration
integer :: kextra
integer :: nlege2 ! the length of the Gauss-Legendre quadrature
double precision, allocatable :: xslege2(:) ! the Gauss-Legendre quadrature nodes
double precision, allocatable :: whtslege2(:) ! the Gauss-Legendre quadrature weights
double precision, allocatable :: u2(:,:) ! the weighted values to coefs matrix
integer :: maxints ! the maximum number of subintervals
integer :: nints ! the number of subintervals
double precision, allocatable :: ab(:,:) ! the (2,nints) list of subintervals
end type legepw_disc
interface
subroutine legepw_adapfun(nfuns,n,xs,whts,vals,userptr)
import c_ptr
double precision :: xs(n)
double precision :: whts(n)
double precision :: vals(:,:)
type(c_ptr) :: userptr
end subroutine
subroutine legepw_cadapfun(nfuns,n,xs,whts,vals,userptr)
import c_ptr
double precision :: xs(n)
double precision :: whts(n)
double complex :: vals(:,:)
type(c_ptr) :: userptr
end subroutine
end interface
interface legepw_init
module procedure legepw_init1
module procedure legepw_init2
end interface legepw_init
interface legepw_coefs
module procedure legepw_coefs1
module procedure legepw_coefs2
module procedure legepw_coefs3
module procedure legepw_coefs4
end interface legepw_coefs
interface legepw_diff
module procedure legepw_diff1
module procedure legepw_diff2
module procedure legepw_diff3
module procedure legepw_diff4
end interface legepw_diff
interface legepw_eval
module procedure legepw_eval1
module procedure legepw_eval2
module procedure legepw_eval3
module procedure legepw_eval4
end interface legepw_eval
interface legepw_evalder
module procedure legepw_evalder1
module procedure legepw_evalder2
module procedure legepw_evalder3
module procedure legepw_evalder4
end interface legepw_evalder
interface legepw_interp
module procedure legepw_interp1
module procedure legepw_interp2
module procedure legepw_interp3
module procedure legepw_interp4
end interface legepw_interp
contains
subroutine legepw_init1(disc,nlege,a,b)
implicit double precision (a-h,o-z)
type(legepw_disc) :: disc
!
! Initalize the structure which stores a piecewise Legendre discretization
! scheme. The initial scheme will consist of the single interval (a,b).
!
! Input parameters:
! nlege - the number of Legendre nodes to use on each subinterval
! (a,b) - the initial interval in the discretization scheme
!
! Output parameters:
! disc - the data structure which specifies a discretization scheme on
! the interval (a,b) will be initialized
!
maxints = 10000
kextra = 8
if ( allocated(disc%ab) ) deallocate(disc%ab)
if ( allocated(disc%xslege) ) deallocate(disc%xslege)
if ( allocated(disc%whtslege) ) deallocate(disc%whtslege)
if ( allocated(disc%u) ) deallocate(disc%u)
if ( allocated(disc%adiff) ) deallocate(disc%adiff)
if ( allocated(disc%xslege2) ) deallocate(disc%xslege2)
if ( allocated(disc%whtslege2) ) deallocate(disc%whtslege2)
if ( allocated(disc%u2) ) deallocate(disc%u2)
call legendre_quad(nlege,disc%xslege,disc%whtslege)
call legendre_coefsmatrix(nlege,disc%xslege,disc%whtslege,disc%u)
call legendre_diffmatrix(nlege,disc%xslege,disc%whtslege,disc%adiff)
nlege2 = nlege+kextra
call legendre_quad(nlege2,disc%xslege2,disc%whtslege2)
call legendre_coefsmatrix(nlege2,disc%xslege2,disc%whtslege2,disc%u2)
disc%nints = 1
disc%nlege = nlege
disc%maxints = maxints
disc%kextra = kextra
disc%nlege2 = nlege2
allocate( disc%ab(2,1) )
disc%ab(1,1) = a
disc%ab(2,1) = b
end subroutine
subroutine legepw_init2(disc,nlege,nints,ab)
implicit double precision (a-h,o-z)
type(legepw_disc) :: disc
double precision :: ab(:,:)
!
! Initialize the data structure describing a piecewise Legendre expansion.
! The initial scheme will consist of a collection of intervals specified
! by the user.
!
! Input parameters:
! nlege - the number of Legendre nodes to use on each subinterval
! nints - the number of intervals in the initial
! ab - a (2,nints) array whose (1,j) and (2,j) entries give the left-
! hand and right-hand endpoints of the jth interval
!
! NOTE: THE INTERVALS MUST BE ORDERED FROM LEFT TO RIGHT
!
! Output parameters:
! disc - the data structure which specifies a discretization scheme on
! the interval (a,b) will be initialized
!
maxints = 10000
kextra = 8
if ( allocated(disc%ab) ) deallocate(disc%ab)
if ( allocated(disc%xslege) ) deallocate(disc%xslege)
if ( allocated(disc%whtslege) ) deallocate(disc%whtslege)
if ( allocated(disc%u) ) deallocate(disc%u)
if ( allocated(disc%adiff) ) deallocate(disc%adiff)
if ( allocated(disc%xslege2) ) deallocate(disc%xslege2)
if ( allocated(disc%whtslege2) ) deallocate(disc%whtslege2)
if ( allocated(disc%u2) ) deallocate(disc%u2)
call legendre_quad(nlege,disc%xslege,disc%whtslege)
call legendre_coefsmatrix(nlege,disc%xslege,disc%whtslege,disc%u)
call legendre_diffmatrix(nlege,disc%xslege,disc%whtslege,disc%adiff)
nlege2 = nlege+kextra
call legendre_quad(nlege2,disc%xslege2,disc%whtslege2)
call legendre_coefsmatrix(nlege2,disc%xslege2,disc%whtslege2,disc%u2)
disc%nints = nints
disc%nlege = nlege
disc%maxints = maxints
disc%kextra = kextra
disc%nlege2 = nlege2
allocate( disc%ab(2,nints) )
disc%ab = ab(:,1:nints)
end subroutine
subroutine legepw_uniform(disc,dlen)
implicit double precision (a-h,o-z)
type(legepw_disc) :: disc
!
! Refine a discretization scheme until each subinterval is of length at most
! dlen.
!
! Input parameters:
! disc - the data structure describing an existing discretization scheme
! dlen - the maximum length of an interval in the scheme.
!
! Ouput parameters:
! disc - the discretization described by this data structure is refined
! as specified
!
double precision, allocatable :: stack(:,:), ab0(:,:)
nstack = 0
nints0 = 0
maxints = disc%maxints
maxstack = maxints
nlege = disc%nlege
nints = disc%nints
allocate(stack(2,maxstack), ab0(2,maxints) )
nints0 = 0
nstack = nints
stack(:,1:nints) = disc%ab
do while (nstack .gt. 0)
a = stack(1,nstack)
b = stack(2,nstack)
nstack = nstack-1
ifsplit = 0
if (b-a .gt. dlen) ifsplit = 1
if (ifsplit .eq. 1) then
if (nstack .ge. maxstack) then
call prina("in legepw_uniform, stack overflowed")
stop
endif
nstack = nstack+1
stack(1,nstack) = a
stack(2,nstack) = (a+b)/2
nstack = nstack+1
stack(1,nstack) = (a+b)/2
stack(2,nstack) = b
else
if (nints0 .eq. maxints) then
call prina("in legepw_uniform, maximum number of intervals exceeded")
stop
endif
nints0 = nints0+1
ab0(1,nints0) = a
ab0(2,nints0) = b
endif
end do
call quicksort(nints0*2,ab0)
deallocate(disc%ab)
allocate(disc%ab(2,nints0))
disc%nints = nints0
disc%ab = ab0(:,1:nints0)
end subroutine
subroutine legepw_gradedmesh(disc)
implicit double precision (a-h,o-z)
type(legepw_disc) :: disc
!
! Refine a discretization scheme so that every interval is at a distance
! from 0 which is less than or equal to its length.
!
! Input parameters:
! disc - the data structure describing an existing discretization scheme
!
! Ouput parameters:
! disc - the discretization described by this data structure is refined
! as specified
!
double precision, allocatable :: stack(:,:), ab0(:,:)
nstack = 0
nints0 = 0
maxints = disc%maxints
maxstack = maxints
nlege = disc%nlege
nints = disc%nints
allocate(stack(2,maxstack), ab0(2,maxints) )
nints0 = 0
nstack = nints
stack(:,1:nints) = disc%ab
do while (nstack .gt. 0)
a = stack(1,nstack)
b = stack(2,nstack)
nstack = nstack-1
dlen = b-a
ifsplit = 0
if (dlen .gt. abs(a)) ifsplit=1
if (ifsplit .eq. 1) then
if (nstack .ge. maxstack) then
call prina("in legepw_uniform, stack overflowed")
stop
endif
nstack = nstack+1
stack(1,nstack) = a
stack(2,nstack) = (a+b)/2
nstack = nstack+1
stack(1,nstack) = (a+b)/2
stack(2,nstack) = b
else
if (nints0 .eq. maxints) then
call prina("in legepw_uniform, maximum number of intervals exceeded")
stop
endif
nints0 = nints0+1
ab0(1,nints0) = a
ab0(2,nints0) = b
endif
end do
call quicksort(nints0*2,ab0)
deallocate(disc%ab)
allocate(disc%ab(2,nints0))
disc%nints = nints0
disc%ab = ab0(:,1:nints0)
end subroutine
subroutine legepw_adap(eps,nfuns,fun,disc,userptr)
implicit double precision (a-h,o-z)
type(legepw_disc) :: disc
procedure(legepw_adapfun) :: fun
type(c_ptr) :: userptr
!
! Adaptively refine a discretization scheme until a user-specified collection
! of square integrable functions can be represented to a specified precision
! using it.
!
! Input parameters:
! eps - the desired precision for the discretization
! nfuns - the number of input functions
! fun - an external subroutine conforming to the legepw_adapfun interface
! which returns the matrix giving the values of the collection of input
! functions at a specified collection of input points
! userptr - a "void *" pointer which is passed to the user-supplied function
! disc - the data structure describing an existing discretization scheme
!
! Input/Output parameters:
! disc - the discretization described by this data structure is refined
! until is suffices to represent the input functions
!
double precision, allocatable :: vals(:,:), sums1(:), sums2(:)
double precision, allocatable :: stack(:,:), ab0(:,:), coefs0(:)
double precision, allocatable :: coefs(:,:), ratios(:)
double precision, allocatable :: xs0(:), whts0(:)
nstack = 0
nints0 = 0
maxints = disc%maxints
maxstack = maxints
nlege = disc%nlege
nints = disc%nints
kk = 8
epssq = eps**2
!allocate(stack(2,maxstack), vals(nlege2,nfuns), coefs0(kextra) )
! allocate(xs0(nlege2), whts0(nlege2), ab0(2,maxints), sums1(nfuns), sums2(nfuns) )
allocate(stack(2,maxstack), vals(nlege,nfuns), coefs0(nlege), coefs(nlege,nfuns) )
allocate(xs0(nlege), whts0(nlege), ab0(2,maxints), sums1(nfuns), sums2(nfuns), ratios(nfuns) )
nstack = nints
do i=1,nstack
stack(:,nints-i+1) = disc%ab(:,i)
end do
do while(nstack .gt. 0)
a = stack(1,nstack)
b = stack(2,nstack)
nstack = nstack-1
xs0 = (b-a)/2 * disc%xslege + (a+b)/2
whts0 = (b-a)/2 * disc%whtslege
call fun(nfuns,nlege,xs0,whts0,vals,userptr)
coefs = matmul(disc%u, vals)
coefs = coefs**2
sums1 = sum(coefs(1:nlege-kk,:),2)
sums2 = sum(coefs(nlege-kk+1:nlege,:),2)
sums1 = sums1+sums2+1.0d0
ratios = sums2 / sums1
ifsplit = 0
if (maxval(ratios) .gt. epssq) ifsplit = 1
if (ifsplit .eq. 1) then
if (nstack .ge. maxstack) then
call prina("in legepw_adap, stack overflowed")
stop
endif
nstack = nstack+1
stack(1,nstack) = (a+b)/2
stack(2,nstack) = b
nstack = nstack+1
stack(1,nstack) = a
stack(2,nstack) = (a+b)/2
else
if (nints0 .eq. maxints) then
call prina("in legepw_adap, maximum number of intervals exceeded")
stop
endif
nints0 = nints0+1
ab0(1,nints0) = a
ab0(2,nints0) = b
endif
end do
! call quicksort(nints0*2,ab0)
deallocate(disc%ab)
allocate(disc%ab(2,nints0))
disc%nints = nints0
disc%ab = ab0(:,1:nints0)
end subroutine
subroutine legepw_cadap(eps,nfuns,fun,disc,userptr)
implicit double precision (a-h,o-z)
type(legepw_disc) :: disc
procedure(legepw_cadapfun) :: fun
type(c_ptr) :: userptr
!
! Adaptively refine a discretization scheme until a user-specified collection
! of square integrable functions can be represented to a specified precision
! using it.
!
! Input parameters:
! eps - the desired precision for the discretization
! nfuns - the number of input functions
! fun - an external subroutine conforming to the legepw_adapfun interface
! which returns the matrix giving the values of the collection of input
! functions at a specified collection of input points
! userptr - a "void *" pointer which is passed to the user-supplied function
! disc - the data structure describing an existing discretization scheme
!
! Input/Output parameters:
! disc - the discretization described by this data structure is refined
! until is suffices to represent the input functions
!
double complex, allocatable :: vals(:,:), coefs(:,:), sums1(:), sums2(:)
double precision, allocatable :: stack(:,:), ab0(:,:)
double precision, allocatable :: xs0(:), whts0(:)
nstack = 0
nints0 = 0
maxints = disc%maxints
maxstack = maxints
nlege = disc%nlege
nints = disc%nints
allocate(stack(2,maxstack), vals(nlege,nfuns), coefs(nlege,nfuns) )
allocate(xs0(nlege), whts0(nlege), ab0(2,maxints), sums1(nfuns), sums2(nfuns) )
nstack = nints
stack(:,1:nints) = disc%ab
do while(nstack .gt. 0)
a = stack(1,nstack)
b = stack(2,nstack)
nstack = nstack-1
xs0 = (b-a)/2 * disc%xslege + (a+b)/2
whts0 = (b-a)/2 * disc%whtslege
call fun(nfuns,nlege,xs0,whts0,vals,userptr)
ifsplit = 0
coefs = matmul(disc%u,vals)
coefs = coefs**2
sums1 = sum(coefs(1:nlege/2,:),1)
sums2 = sum(coefs(nlege/2+1:nlege,:),1)
sums1 = sqrt(sums2/(sums1+sums2))
dd1 = maxval(abs(sums1))
if (dd1 .gt. eps) ifsplit = 1
if (ifsplit .eq. 1) then
if (nstack .ge. maxstack) then
call prina("in legepw_cadap, stack overflowed")
stop
endif
nstack = nstack+1
stack(1,nstack) = a
stack(2,nstack) = (a+b)/2
nstack = nstack+1
stack(1,nstack) = (a+b)/2
stack(2,nstack) = b
else
if (nints0 .eq. maxints) then
call prina("in legepw_cadap, maximum number of intervals exceeded")
stop
endif
nints0 = nints0+1
ab0(1,nints0) = a
ab0(2,nints0) = b
endif
end do
call quicksort(nints0*2,ab0)
deallocate(disc%ab)
allocate(disc%ab(2,nints0))
disc%nints = nints0
disc%ab = ab0(:,1:nints0)
end subroutine
subroutine legepw_quad(disc,nquad,xs,whts)
implicit double precision (a-h,o-z)
type(legepw_disc) :: disc
double precision, allocatable, intent(out) :: xs(:), whts(:)
!
! Return the discretization quadrature rule.
!
! Input parameters:
! disc - the data structure describing the discretization scheme
!
! Output parameters:
! nquad - the number of nodes in the quadrature rule
! xs - the nodes in the quadruature rule
! whts - the weights in the quadrature rule
!
nints = disc%nints
nlege = disc%nlege
nquad = nints*nlege
allocate(xs(nquad), whts(nquad) )
i2 = 0
do int = 1,nints
a = disc%ab(1,int)
b = disc%ab(2,int)
i1 = i2+1
i2 = i1+nlege-1
xs(i1:i2) = disc%xslege*(b-a)/2 + (b+a)/2
whts(i1:i2) = disc%whtslege*(b-a)/2
end do
end subroutine
subroutine legepw_coefs1(disc,vals,coefs)
implicit double precision (a-h,o-z)
type(legepw_disc) :: disc
double precision :: vals(:), coefs(:)
!
! Given the vector (4) of *scaled* values of a real-valued expansion of the form
! (2) at the nodes of the discretization quadrature, compute the vector (3) of
! expansion coefficients.
!
! Input parameters:
! disc - the data structure describing the discretization scheme
! vals - the array specifying the scaled values of the expansion
!
! Output parameters:
! coefs - the array specifying the vecotr of coefficients
!
!
nlege = disc%nlege
nints = disc%nints
i2 = 0
do int=1,nints
a = disc%ab(1,int)
b = disc%ab(2,int)
i1 = i2+1
i2 = i1+nlege-1
coefs(i1:i2) = matmul(disc%u,vals(i1:i2))
end do
end subroutine
subroutine legepw_coefs2(disc,vals,coefs)
implicit double precision (a-h,o-z)
type(legepw_disc) :: disc
double complex :: vals(:), coefs(:)
!
! Given the vector (4) of *scaled* values of a complex-valued expansion of the form
! (2) at the nodes of the discretization quadrature, compute the vector (3) of
! expansion coefficients.
!
! Input parameters:
! disc - the data structure describing the discretization scheme
! vals - the array specifying the scaled values of the expansion
!
! Output parameters:
! coefs - the array specifying the vecotr of coefficients
!
nlege = disc%nlege
nints = disc%nints
i2 = 0
do int=1,nints
a = disc%ab(1,int)
b = disc%ab(2,int)
i1 = i2+1
i2 = i1+nlege-1
coefs(i1:i2) = matmul(disc%u,vals(i1:i2))
end do
end subroutine
subroutine legepw_coefs3(disc,vals,coefs)
implicit double precision (a-h,o-z)
type(legepw_disc) :: disc
double complex :: vals(:,:), coefs(:,:)
!
! Given the vector (4) of *scaled* values of a collection of complex-valued expansions
! of the form (2) at the nodes of the discretization quadrature, their compute the vectors
! (3) of expansion coefficients.
!
! Input parameters:
! disc - the data structure describing the discretization scheme
! vals - the matrix whose jth column gives the scaled values of the jth
! input expansion
!
! Output parameters:
! coefs - the matrix whose jth column gives the coefficinets of the jth
! input expansion
!
nlege = disc%nlege
nints = disc%nints
i2 = 0
do int=1,nints
a = disc%ab(1,int)
b = disc%ab(2,int)
i1 = i2+1
i2 = i1+nlege-1
coefs(i1:i2,:) = matmul(disc%u,vals(i1:i2,:))
end do
end subroutine
subroutine legepw_coefs4(disc,vals,coefs)
implicit double precision (a-h,o-z)
type(legepw_disc) :: disc
double precision :: vals(:,:), coefs(:,:)
!
! Given the vector (4) of *scaled* values of a collection of real-valued expansions
! of the form (2) at the nodes of the discretization quadrature, their compute the vectors
! (3) of expansion coefficients.
!
! Input parameters:
! disc - the data structure describing the discretization scheme
! vals - the matrix whose jth column gives the scaled values of the jth
! input expansion
!
! Output parameters:
! coefs - the matrix whose jth column gives the coefficinets of the jth
! input expansion
!
nlege = disc%nlege
nints = disc%nints
i2 = 0
do int=1,nints
a = disc%ab(1,int)
b = disc%ab(2,int)
i1 = i2+1
i2 = i1+nlege-1
coefs(i1:i2,:) = matmul(disc%u,vals(i1:i2,:))
end do
end subroutine
subroutine legepw_diff1(disc,vals,ders)
implicit double precision (a-h,o-z)
type(legepw_disc) :: disc
double precision :: vals(:), ders(:)
!
! Given the vector (4) of *scaled* values of a real-valued expansion of the form
! (2) at the nodes of the discretization quadrature, compute the vector (3) of the scaled
! values of its derivative at the same.
!
! Input parameters:
! disc - the data structure describing the discretization scheme
! vals - the array specifying the scaled values of the expansion
!
! Output parameters:
! ders - the array specifying the scaled values of the expansion's derviative
!
!
nlege = disc%nlege
nints = disc%nints
i2 = 0
do int=1,nints
a = disc%ab(1,int)
b = disc%ab(2,int)
i1 = i2+1
i2 = i1+nlege-1
ders(i1:i2) = matmul(disc%adiff,vals(i1:i2))
ders(i1:i2) = ders(i1:i2)*2/(b-a)
end do
end subroutine
subroutine legepw_diff2(disc,vals,ders)
implicit double precision (a-h,o-z)
type(legepw_disc) :: disc
double complex :: vals(:), ders(:)
!
! Given the vector (4) of *scaled* values of a real-valued expansion of the form
! (2) at the nodes of the discretization quadrature, compute the vector (3) of the scaled
! values of its derivative at the same.
!
! Input parameters:
! disc - the data structure describing the discretization scheme
! vals - the array specifying the scaled values of the expansion
!
! Output parameters:
! ders - the array specifying the scaled values of the expansion's derviative
!
!
nlege = disc%nlege
nints = disc%nints
i2 = 0
do int=1,nints
a = disc%ab(1,int)
b = disc%ab(2,int)
i1 = i2+1
i2 = i1+nlege-1
ders(i1:i2) = matmul(disc%adiff,vals(i1:i2))
ders(i1:i2) = ders(i1:i2)*2/(b-a)
end do
end subroutine
subroutine legepw_diff3(disc,vals,ders)
implicit double precision (a-h,o-z)
type(legepw_disc) :: disc
double precision :: vals(:,:), ders(:,:)
!
! Given the vector (4) of *scaled* values of a collection of real-valued expansions of the form
! (2) at the nodes of the discretization quadrature, compute the (3) scaled values
! of their derivative at the same.
!
! Input parameters:
! disc - the data structure describing the discretization scheme
! vals - the array specifying the scaled values of the expansion
!
! Output parameters:
! ders - the array specifying the scaled values of the expansion's derviative
!
!
nlege = disc%nlege
nints = disc%nints
i2 = 0
do int=1,nints
a = disc%ab(1,int)
b = disc%ab(2,int)
i1 = i2+1
i2 = i1+nlege-1
ders(i1:i2,:) = matmul(disc%adiff,vals(i1:i2,:))
ders(i1:i2,:) = ders(i1:i2,:)*2/(b-a)
end do
end subroutine
subroutine legepw_diff4(disc,vals,ders)
implicit double precision (a-h,o-z)
type(legepw_disc) :: disc
double complex :: vals(:,:), ders(:,:)
!
! Given the vector (4) of *scaled* values of a collection of complex-valued expansions of the form
! (2) at the nodes of the discretization quadrature, compute the (3) scaled values
! of their derivative at the same.
!
! Input parameters:
! disc - the data structure describing the discretization scheme
! vals - the array specifying the scaled values of the expansion
!
! Output parameters:
! ders - the array specifying the scaled values of the expansion's derviative
!
!
nlege = disc%nlege
nints = disc%nints
i2 = 0
do int=1,nints
a = disc%ab(1,int)
b = disc%ab(2,int)
i1 = i2+1
i2 = i1+nlege-1
ders(i1:i2,:) = matmul(disc%adiff,vals(i1:i2,:))
ders(i1:i2,:) = ders(i1:i2,:)*2/(b-a)
end do
end subroutine
subroutine legepw_eval1(disc,coefs,x,valout)
implicit double precision (a-h,o-z)
type(legepw_disc) :: disc
double precision :: coefs(:)
double precision :: valout
!
! Evaluate a real-valued expansion of the form (2) at a specified point
! given its vector (3) of expansion coefficients.
!
! Input parameters:
! disc - the data structure describing the discretization scheme
! coefs - the vector (3) of expansion coefficients
! x - the point at which to evaluate the input expansion
!
! Output parameters: