-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpppack.f90
6824 lines (6059 loc) · 177 KB
/
pppack.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
! ROMSPath Version: 1.0.1
subroutine banfac ( w, nroww, nrow, nbandl, nbandu, iflag )
!*****************************************************************************80
!
!! BANFAC factors a banded matrix without pivoting.
!
! Discussion:
!
! BANFAC returns in W the LU-factorization, without pivoting, of
! the banded matrix A of order NROW with (NBANDL+1+NBANDU) bands
! or diagonals in the work array W.
!
! Gauss elimination without pivoting is used. The routine is
! intended for use with matrices A which do not require row
! interchanges during factorization, especially for the totally
! positive matrices which occur in spline calculations.
!
! The matrix storage mode used is the same one used by LINPACK
! and LAPACK, and results in efficient innermost loops.
!
! Explicitly, A has
!
! NBANDL bands below the diagonal
! 1 main diagonal
! NBANDU bands above the diagonal
!
! and thus, with MIDDLE=NBANDU+1,
! A(I+J,J) is in W(I+MIDDLE,J) for I=-NBANDU,...,NBANDL, J=1,...,NROW.
!
! For example, the interesting entries of a banded matrix
! matrix of order 9, with NBANDL=1, NBANDU=2:
!
! 11 12 13 0 0 0 0 0 0
! 21 22 23 24 0 0 0 0 0
! 0 32 33 34 35 0 0 0 0
! 0 0 43 44 45 46 0 0 0
! 0 0 0 54 55 56 57 0 0
! 0 0 0 0 65 66 67 68 0
! 0 0 0 0 0 76 77 78 79
! 0 0 0 0 0 0 87 88 89
! 0 0 0 0 0 0 0 98 99
!
! would appear in the first 1+1+2=4 rows of W as follows:
!
! 0 0 13 24 35 46 57 68 79
! 0 12 23 34 45 56 67 78 89
! 11 22 33 44 55 66 77 88 99
! 21 32 43 54 65 76 87 98 0
!
! All other entries of W not identified in this way with an
! entry of A are never referenced.
!
! This routine makes it possible to solve any particular linear system
! A*X=B for X by the call
!
! call banslv ( w, nroww, nrow, nbandl, nbandu, b )
!
! with the solution X contained in B on return.
!
! If IFLAG=2, then one of NROW-1, NBANDL, NBANDU failed to be nonnegative,
! or else one of the potential pivots was found to be zero
! indicating that A does not have an LU-factorization. This
! implies that A is singular in case it is totally positive.
!
! Modified:
!
! 14 February 2007
!
! Author:
!
! Original FORTRAN77 version by Carl de Boor.
! FORTRAN90 version by John Burkardt.
!
! Reference:
!
! Carl de Boor,
! A Practical Guide to Splines,
! Springer, 2001,
! ISBN: 0387953663,
! LC: QA1.A647.v27.
!
! Parameters:
!
! Input/output, real ( kind = 8 ) W(NROWW,NROW).
! On input, W contains the "interesting" part of a banded
! matrix A, with the diagonals or bands of A stored in the
! rows of W, while columns of A correspond to columns of W.
! On output, W contains the LU-factorization of A into a unit
! lower triangular matrix L and an upper triangular matrix U
! (both banded) and stored in customary fashion over the
! corresponding entries of A.
!
! Input, integer ( kind = 4 ) NROWW, the row dimension of the work array W.
! NROWW must be at least NBANDL+1 + NBANDU.
!
! Input, integer ( kind = 4 ) NROW, the number of rows in A.
!
! Input, integer ( kind = 4 ) NBANDL, the number of bands of A below
! the main diagonal.
!
! Input, integer ( kind = 4 ) NBANDU, the number of bands of A above
! the main diagonal.
!
! Output, integer ( kind = 4 ) IFLAG, error flag.
! 1, success.
! 2, failure, the matrix was not factored.
!
implicit none
integer ( kind = 4 ) nrow
integer ( kind = 4 ) nroww
real ( kind = 8 ) factor
integer ( kind = 4 ) i
integer ( kind = 4 ) iflag
integer ( kind = 4 ) j
integer ( kind = 4 ) k
integer ( kind = 4 ) middle
integer ( kind = 4 ) nbandl
integer ( kind = 4 ) nbandu
real ( kind = 8 ) pivot
real ( kind = 8 ) w(nroww,nrow)
iflag = 1
if ( nrow < 1 ) then
iflag = 2
return
end if
!
! W(MIDDLE,*) contains the main diagonal of A.
!
middle = nbandu + 1
if ( nrow == 1 ) then
if ( w(middle,nrow) == 0.0D+00 ) then
iflag = 2
end if
return
end if
!
! A is upper triangular. Check that the diagonal is nonzero.
!
if ( nbandl <= 0 ) then
do i = 1, nrow-1
if ( w(middle,i) == 0.0D+00 ) then
iflag = 2
return
end if
end do
if ( w(middle,nrow) == 0.0D+00 ) then
iflag = 2
end if
return
!
! A is lower triangular. Check that the diagonal is nonzero and
! divide each column by its diagonal.
!
else if ( nbandu <= 0 ) then
do i = 1, nrow - 1
pivot = w(middle,i)
if ( pivot == 0.0D+00 ) then
iflag = 2
return
end if
do j = 1, min ( nbandl, nrow-i )
w(middle+j,i) = w(middle+j,i) / pivot
end do
end do
return
end if
!
! A is not just a triangular matrix.
! Construct the LU factorization.
!
do i = 1, nrow - 1
!
! W(MIDDLE,I) is the pivot for the I-th step.
!
if ( w(middle,i) == 0.0D+00 ) then
iflag = 2
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'BANFAC - Fatal error!'
write ( *, '(a,i8)' ) ' Zero pivot encountered in column ', i
stop 1
end if
!
! Divide each entry in column I below the diagonal by PIVOT.
!
do j = 1, min ( nbandl, nrow-i )
w(middle+j,i) = w(middle+j,i) / w(middle,i)
end do
!
! Subtract A(I,I+K)*(I-th column) from (I+K)-th column (below row I).
!
do k = 1, min ( nbandu, nrow-i )
factor = w(middle-k,i+k)
do j = 1, min ( nbandl, nrow-i )
w(middle-k+j,i+k) = w(middle-k+j,i+k) - w(middle+j,i) * factor
end do
end do
end do
!
! Check the last diagonal entry.
!
if ( w(middle,nrow) == 0.0D+00 ) then
iflag = 2
end if
return
end
subroutine banslv ( w, nroww, nrow, nbandl, nbandu, b )
!*****************************************************************************80
!
!! BANSLV solves a banded linear system A * X = B factored by BANFAC.
!
! Modified:
!
! 14 February 2007
!
! Author:
!
! Original FORTRAN77 version by Carl de Boor.
! FORTRAN90 version by John Burkardt.
!
! Reference:
!
! Carl de Boor,
! A Practical Guide to Splines,
! Springer, 2001,
! ISBN: 0387953663,
! LC: QA1.A647.v27.
!
! Parameters:
!
! Input, real ( kind = 8 ) W(NROWW,NROW). W contains the banded matrix,
! after it has been factored by BANFAC.
!
! Input, integer ( kind = 4 ) NROWW, the row dimension of the work array W.
! NROWW must be at least NBANDL+1 + NBANDU.
!
! Input, integer ( kind = 4 ) NROW, the number of rows in A.
!
! Input, integer ( kind = 4 ) NBANDL, the number of bands of A below the
! main diagonal.
!
! Input, integer ( kind = 4 ) NBANDU, the number of bands of A above the
! main diagonal.
!
! Input/output, real ( kind = 8 ) B(NROW).
! On input, B contains the right hand side of the system to be solved.
! On output, B contains the solution, X.
!
implicit none
integer ( kind = 4 ) nrow
integer ( kind = 4 ) nroww
real ( kind = 8 ) b(nrow)
integer ( kind = 4 ) i
integer ( kind = 4 ) j
integer ( kind = 4 ) jmax
integer ( kind = 4 ) middle
integer ( kind = 4 ) nbandl
integer ( kind = 4 ) nbandu
real ( kind = 8 ) w(nroww,nrow)
middle = nbandu + 1
if ( nrow == 1 ) then
b(1) = b(1) / w(middle,1)
return
end if
!
! Forward pass:
!
! For I = 1, 2, ..., NROW-1, subtract RHS(I)*(I-th column of L)
! from the right hand side, below the I-th row.
!
if ( 0 < nbandl ) then
do i = 1, nrow - 1
jmax = min ( nbandl, nrow-i )
do j = 1, jmax
b(i+j) = b(i+j) - b(i) * w(middle+j,i)
end do
end do
end if
!
! Backward pass:
!
! For I=NROW, NROW-1,...,1, divide RHS(I) by
! the I-th diagonal entry of U, then subtract
! RHS(I)*(I-th column of U) from right hand side, above the I-th row.
!
do i = nrow, 2, -1
b(i) = b(i) / w(middle,i)
do j = 1, min ( nbandu, i - 1 )
b(i-j) = b(i-j) - b(i) * w(middle-j,i)
end do
end do
b(1) = b(1) / w(middle,1)
return
end
subroutine bchfac ( w, nbands, nrow, diag )
!*****************************************************************************80
!
!! BCHFAC constructs a Cholesky factorization of a matrix.
!
! Discussion:
!
! The factorization has the form
!
! C = L * D * L'
!
! with L unit lower triangular and D diagonal, for a given matrix C of
! order NROW, where C is symmetric positive semidefinite and banded,
! having NBANDS diagonals at and below the main diagonal.
!
! Gauss elimination is used, adapted to the symmetry and bandedness of C.
!
! Near-zero pivots are handled in a special way. The diagonal
! element C(N,N) = W(1,N) is saved initially in DIAG(N), all N.
!
! At the N-th elimination step, the current pivot element, W(1,N),
! is compared with its original value, DIAG(N). If, as the result
! of prior elimination steps, this element has been reduced by about
! a word length, that is, if W(1,N) + DIAG(N) <= DIAG(N), then the pivot
! is declared to be zero, and the entire N-th row is declared to
! be linearly dependent on the preceding rows. This has the effect
! of producing X(N) = 0 when solving C * X = B for X, regardless of B.
!
! Justification for this is as follows. In contemplated applications
! of this program, the given equations are the normal equations for
! some least-squares approximation problem, DIAG(N) = C(N,N) gives
! the norm-square of the N-th basis function, and, at this point,
! W(1,N) contains the norm-square of the error in the least-squares
! approximation to the N-th basis function by linear combinations
! of the first N-1.
!
! Having W(1,N)+DIAG(N) <= DIAG(N) signifies that the N-th function
! is linearly dependent to machine accuracy on the first N-1
! functions, therefore can safely be left out from the basis of
! approximating functions.
!
! The solution of a linear system C * X = B is effected by the
! succession of the following two calls:
!
! call bchfac ( w, nbands, nrow, diag )
!
! call bchslv ( w, nbands, nrow, b, x )
!
! Modified:
!
! 14 February 2007
!
! Author:
!
! Original FORTRAN77 version by Carl de Boor.
! FORTRAN90 version by John Burkardt.
!
! Reference:
!
! Carl de Boor,
! A Practical Guide to Splines,
! Springer, 2001,
! ISBN: 0387953663,
! LC: QA1.A647.v27.
!
! Parameters:
!
! Input/output, real ( kind = 8 ) W(NBANDS,NROW).
! On input, W contains the NBANDS diagonals in its rows,
! with the main diagonal in row 1. Precisely, W(I,J)
! contains C(I+J-1,J), I=1,...,NBANDS, J=1,...,NROW.
! For example, the interesting entries of a seven diagonal
! symmetric matrix C of order 9 would be stored in W as
! 11 22 33 44 55 66 77 88 99
! 21 32 43 54 65 76 87 98 *
! 31 42 53 64 75 86 97 * *
! 41 52 63 74 85 96 * * *
! Entries of the array not associated with an
! entry of C are never referenced.
! On output, W contains the Cholesky factorization
! C = L*D*L', with W(1,I) containing 1/D(I,I) and W(I,J)
! containing L(I-1+J,J), I=2,...,NBANDS.
!
! Input, integer ( kind = 4 ) NBANDS, indicates the bandwidth of the
! matrix C, that is, C(I,J) = 0 for NBANDS < abs(I-J).
!
! Input, integer ( kind = 4 ) NROW, is the order of the matrix C.
!
! Work array, real ( kind = 8 ) DIAG(NROW).
!
implicit none
integer ( kind = 4 ) nbands
integer ( kind = 4 ) nrow
real ( kind = 8 ) diag(nrow)
integer ( kind = 4 ) i
integer ( kind = 4 ) imax
integer ( kind = 4 ) j
integer ( kind = 4 ) jmax
integer ( kind = 4 ) n
real ( kind = 8 ) ratio
real ( kind = 8 ) w(nbands,nrow)
if ( nrow <= 1 ) then
if ( 0.0D+00 < w(1,1) ) then
w(1,1) = 1.0D+00 / w(1,1)
end if
return
end if
!
! Store the diagonal.
!
diag(1:nrow) = w(1,1:nrow)
!
! Factorization.
!
do n = 1, nrow
if ( w(1,n) + diag(n) <= diag(n) ) then
w(1:nbands,n) = 0.0D+00
else
w(1,n) = 1.0D+00 / w(1,n)
imax = min ( nbands - 1, nrow - n )
jmax = imax
do i = 1, imax
ratio = w(i+1,n) * w(1,n)
do j = 1, jmax
w(j,n+i) = w(j,n+i) - w(j+i,n) * ratio
end do
jmax = jmax - 1
w(i+1,n) = ratio
end do
end if
end do
return
end
subroutine bchslv ( w, nbands, nrow, b )
!*****************************************************************************80
!
!! BCHSLV solves a banded symmetric positive definite system.
!
! Discussion:
!
! The system is of the form:
!
! C * X = B
!
! and the Cholesky factorization of C has been constructed
! by BCHFAC.
!
! With the factorization
!
! C = L * D * L'
!
! available, where L is unit lower triangular and D is diagonal,
! the triangular system
!
! L * Y = B
!
! is solved for Y (forward substitution), Y is stored in B, the
! vector D^(-1)*Y is computed and stored in B, then the
! triangular system L'*X = D^(-1)*Y is solved for X
! (back substitution).
!
! Modified:
!
! 14 February 2007
!
! Author:
!
! Original FORTRAN77 version by Carl de Boor.
! FORTRAN90 version by John Burkardt.
!
! Reference:
!
! Carl de Boor,
! A Practical Guide to Splines,
! Springer, 2001,
! ISBN: 0387953663,
! LC: QA1.A647.v27.
!
! Parameters:
!
! Input, real ( kind = 8 ) W(NBANDS,NROW), the Cholesky factorization for C,
! as computed by BCHFAC.
!
! Input, integer ( kind = 4 ) NBANDS, the bandwidth of C.
!
! Input, integer ( kind = 4 ) NROW, the order of the matrix C.
!
! Input/output, real ( kind = 8 ) B(NROW).
! On input, the right hand side.
! On output, the solution.
!
implicit none
integer ( kind = 4 ) nbands
integer ( kind = 4 ) nrow
real ( kind = 8 ) b(nrow)
integer ( kind = 4 ) j
integer ( kind = 4 ) n
real ( kind = 8 ) w(nbands,nrow)
if ( nrow <= 1 ) then
b(1) = b(1) * w(1,1)
return
end if
!
! Forward substitution.
! Solve L*Y = B.
!
do n = 1, nrow
do j = 1, min ( nbands - 1, nrow - n )
b(j+n) = b(j+n) - w(j+1,n) * b(n)
end do
end do
!
! Back substitution.
! Solve L'*X = D^(-1)*Y.
!
do n = nrow, 1, -1
b(n) = b(n) * w(1,n)
do j = 1, min ( nbands - 1, nrow - n )
b(n) = b(n) - w(j+1,n) * b(j+n)
end do
end do
return
end
subroutine bsplpp ( t, bcoef, n, k, scrtch, break, coef, l )
!*****************************************************************************80
!
!! BSPLPP converts from B-spline to piecewise polynomial form.
!
! Discussion:
!
! The B-spline representation of a spline is
! ( T, BCOEF, N, K ),
! while the piecewise polynomial representation is
! ( BREAK, COEF, L, K ).
!
! For each breakpoint interval, the K relevant B-spline coefficients
! of the spline are found and then differenced repeatedly to get the
! B-spline coefficients of all the derivatives of the spline on that
! interval.
!
! The spline and its first K-1 derivatives are then evaluated at the
! left end point of that interval, using BSPLVB repeatedly to obtain
! the values of all B-splines of the appropriate order at that point.
!
! Modified:
!
! 14 February 2007
!
! Author:
!
! Original FORTRAN77 version by Carl de Boor.
! FORTRAN90 version by John Burkardt.
!
! Reference:
!
! Carl de Boor,
! A Practical Guide to Splines,
! Springer, 2001,
! ISBN: 0387953663,
! LC: QA1.A647.v27.
!
! Parameters:
!
! Input, real ( kind = 8 ) T(N+K), the knot sequence.
!
! Input, real ( kind = 8 ) BCOEF(N), the B spline coefficient sequence.
!
! Input, integer ( kind = 4 ) N, the number of B spline coefficients.
!
! Input, integer ( kind = 4 ) K, the order of the spline.
!
! Work array, real ( kind = 8 ) SCRTCH(K,K).
!
! Output, real ( kind = 8 ) BREAK(L+1), the piecewise polynomial breakpoint
! sequence. BREAK contains the distinct points in the sequence T(K:N+1)
!
! Output, real ( kind = 8 ) COEF(K,N), with COEF(I,J) = (I-1)st derivative
! of the spline at BREAK(J) from the right.
!
! Output, integer ( kind = 4 ) L, the number of polynomial pieces which
! make up the spline in the interval ( T(K), T(N+1) ).
!
implicit none
integer ( kind = 4 ) k
integer ( kind = 4 ) l
integer ( kind = 4 ) n
real ( kind = 8 ) bcoef(n)
real ( kind = 8 ) biatx(k)
real ( kind = 8 ) break(*)
real ( kind = 8 ) coef(k,n)
real ( kind = 8 ) diff
integer ( kind = 4 ) i
integer ( kind = 4 ) j
integer ( kind = 4 ) jp1
integer ( kind = 4 ) left
integer ( kind = 4 ) lsofar
real ( kind = 8 ) scrtch(k,k)
real ( kind = 8 ) sum1
real ( kind = 8 ) t(n+k)
lsofar = 0
break(1) = t(k)
do left = k, n
!
! Find the next nontrivial knot interval.
!
if ( t(left+1) == t(left) ) then
cycle
end if
lsofar = lsofar + 1
break(lsofar+1) = t(left+1)
if ( k <= 1 ) then
coef(1,lsofar) = bcoef(left)
cycle
end if
!
! Store the K B-spline coefficients relevant to current knot
! interval in SCRTCH(*,1).
!
do i = 1, k
scrtch(i,1) = bcoef(left-k+i)
end do
!
! For J=1,...,K-1, compute the K-J B-spline coefficients relevant to
! the current knot interval for the J-th derivative by differencing
! those for the (J-1)st derivative, and store in SCRTCH(.,J+1).
!
do jp1 = 2, k
j = jp1 - 1
do i = 1, k - j
diff = t(left+i) - t(left+i-(k-j))
if ( 0.0D+00 < diff ) then
scrtch(i,jp1) = ( ( scrtch(i+1,j) - scrtch(i,j) ) / diff ) &
* real ( k - j, kind = 8 )
end if
end do
end do
!
! For J=0, ..., K-1, find the values at T(left) of the J+1
! B-splines of order J+1 whose support contains the current
! knot interval from those of order J (in BIATX ), then combine
! with the B-spline coefficients (in SCRTCH(.,K-J) ) found earlier
! to compute the (K-J-1)st derivative at T(LEFT) of the given
! spline.
!
call bsplvb ( t, 1, 1, t(left), left, biatx )
coef(k,lsofar) = scrtch(1,k)
do jp1 = 2, k
call bsplvb ( t, jp1, 2, t(left), left, biatx )
coef(k+1-jp1,lsofar) = dot_product ( biatx(1:jp1), scrtch(1:jp1,k+1-jp1) )
end do
end do
l = lsofar
return
end
subroutine bsplvb ( t, jhigh, index, x, left, biatx )
!*****************************************************************************80
!
!! BSPLVB evaluates B-splines at a point X with a given knot sequence.
!
! Discusion:
!
! BSPLVB evaluates all possibly nonzero B-splines at X of order
!
! JOUT = MAX ( JHIGH, (J+1)*(INDEX-1) )
!
! with knot sequence T.
!
! The recurrence relation
!
! X - T(I) T(I+J+1) - X
! B(I,J+1)(X) = ----------- * B(I,J)(X) + --------------- * B(I+1,J)(X)
! T(I+J)-T(I) T(I+J+1)-T(I+1)
!
! is used to generate B(LEFT-J:LEFT,J+1)(X) from B(LEFT-J+1:LEFT,J)(X)
! storing the new values in BIATX over the old.
!
! The facts that
!
! B(I,1)(X) = 1 if T(I) <= X < T(I+1)
!
! and that
!
! B(I,J)(X) = 0 unless T(I) <= X < T(I+J)
!
! are used.
!
! The particular organization of the calculations follows
! algorithm 8 in chapter X of the text.
!
! Modified:
!
! 14 February 2007
!
! Author:
!
! Original FORTRAN77 version by Carl de Boor.
! FORTRAN90 version by John Burkardt.
!
! Reference:
!
! Carl de Boor,
! A Practical Guide to Splines,
! Springer, 2001,
! ISBN: 0387953663,
! LC: QA1.A647.v27.
!
! Parameters:
!
! Input, real ( kind = 8 ) T(LEFT+JOUT), the knot sequence. T is assumed to
! be nondecreasing, and also, T(LEFT) must be strictly less than
! T(LEFT+1).
!
! Input, integer ( kind = 4 ) JHIGH, INDEX, determine the order
! JOUT = max ( JHIGH, (J+1)*(INDEX-1) )
! of the B-splines whose values at X are to be returned.
! INDEX is used to avoid recalculations when several
! columns of the triangular array of B-spline values are
! needed, for example, in BVALUE or in BSPLVD.
! If INDEX = 1, the calculation starts from scratch and the entire
! triangular array of B-spline values of orders
! 1, 2, ...,JHIGH is generated order by order, that is,
! column by column.
! If INDEX = 2, only the B-spline values of order J+1, J+2, ..., JOUT
! are generated, the assumption being that BIATX, J,
! DELTAL, DELTAR are, on entry, as they were on exit
! at the previous call. In particular, if JHIGH = 0,
! then JOUT = J+1, that is, just the next column of B-spline
! values is generated.
! Warning: the restriction JOUT <= JMAX (= 20) is
! imposed arbitrarily by the dimension statement for DELTAL
! and DELTAR, but is nowhere checked for.
!
! Input, real ( kind = 8 ) X, the point at which the B-splines
! are to be evaluated.
!
! Input, integer ( kind = 4 ) LEFT, an integer chosen so that
! T(LEFT) <= X <= T(LEFT+1).
!
! Output, real ( kind = 8 ) BIATX(JOUT), with BIATX(I) containing the
! value at X of the polynomial of order JOUT which agrees
! with the B-spline B(LEFT-JOUT+I,JOUT,T) on the interval
! (T(LEFT),T(LEFT+1)).
!
implicit none
integer ( kind = 4 ), parameter :: jmax = 20
integer ( kind = 4 ) jhigh
real ( kind = 8 ) biatx(jhigh)
real ( kind = 8 ), save, dimension ( jmax ) :: deltal
real ( kind = 8 ), save, dimension ( jmax ) :: deltar
integer ( kind = 4 ) i
integer ( kind = 4 ) index
integer ( kind = 4 ), save :: j = 1
integer ( kind = 4 ) left
real ( kind = 8 ) saved
real ( kind = 8 ) t(left+jhigh)
real ( kind = 8 ) term
real ( kind = 8 ) x
if ( index == 1 ) then
j = 1
biatx(1) = 1.0D+00
if ( jhigh <= j ) then
return
end if
end if
if ( t(left+1) <= t(left) ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'BSPLVB - Fatal error!'
write ( *, '(a)' ) ' It is required that T(LEFT) < T(LEFT+1).'
write ( *, '(a,i8)' ) ' But LEFT = ', left
write ( *, '(a,g14.6)' ) ' T(LEFT) = ', t(left)
write ( *, '(a,g14.6)' ) ' T(LEFT+1) = ', t(left+1)
stop 1
end if
do
deltar(j) = t(left+j) - x
deltal(j) = x - t(left+1-j)
saved = 0.0D+00
do i = 1, j
term = biatx(i) / ( deltar(i) + deltal(j+1-i) )
biatx(i) = saved + deltar(i) * term
saved = deltal(j+1-i) * term
end do
biatx(j+1) = saved
j = j + 1
if ( jhigh <= j ) then
exit
end if
end do
return
end
subroutine bsplvd ( t, k, x, left, a, dbiatx, nderiv )
!*****************************************************************************80
!
!! BSPLVD calculates the nonvanishing B-splines and derivatives at X.
!
! Discussion:
!
! Values at X of all the relevant B-splines of order K:K+1-NDERIV
! are generated via BSPLVB and stored temporarily in DBIATX.
!
! Then the B-spline coefficients of the required derivatives
! of the B-splines of interest are generated by differencing,
! each from the preceding one of lower order, and combined with
! the values of B-splines of corresponding order in DBIATX
! to produce the desired values.
!
! Modified:
!
! 14 February 2007
!
! Author:
!
! Original FORTRAN77 version by Carl de Boor.
! FORTRAN90 version by John Burkardt.
!
! Reference:
!
! Carl de Boor,
! A Practical Guide to Splines,
! Springer, 2001,
! ISBN: 0387953663,
! LC: QA1.A647.v27.
!
! Parameters:
!
! Input, real ( kind = 8 ) T(LEFT+K), the knot sequence. It is assumed that
! T(LEFT) < T(LEFT+1). Also, the output is correct only if
! T(LEFT) <= X <= T(LEFT+1).
!
! Input, integer ( kind = 4 ) K, the order of the B-splines to be evaluated.
!
! Input, real ( kind = 8 ) X, the point at which these values are sought.
!
! Input, integer ( kind = 4 ) LEFT, indicates the left endpoint of the
! interval of interest. The K B-splines whose support contains the interval
! ( T(LEFT), T(LEFT+1) ) are to be considered.
!
! Workspace, real ( kind = 8 ) A(K,K).
!
! Output, real ( kind = 8 ) DBIATX(K,NDERIV). DBIATX(I,M) contains
! the value of the (M-1)st derivative of the (LEFT-K+I)-th B-spline
! of order K for knot sequence T, I=M,...,K, M=1,...,NDERIV.
!
! Input, integer ( kind = 4 ) NDERIV, indicates that values of B-splines and
! their derivatives up to but not including the NDERIV-th are asked for.
!
implicit none
integer ( kind = 4 ) k
integer ( kind = 4 ) left
integer ( kind = 4 ) nderiv
real ( kind = 8 ) a(k,k)
real ( kind = 8 ) dbiatx(k,nderiv)
real ( kind = 8 ) factor
real ( kind = 8 ) fkp1mm
integer ( kind = 4 ) i
integer ( kind = 4 ) ideriv
integer ( kind = 4 ) il
integer ( kind = 4 ) j
integer ( kind = 4 ) jlow
integer ( kind = 4 ) jp1mid
integer ( kind = 4 ) ldummy
integer ( kind = 4 ) m
integer ( kind = 4 ) mhigh
real ( kind = 8 ) sum1
real ( kind = 8 ) t(left+k)
real ( kind = 8 ) x
mhigh = max ( min ( nderiv, k ), 1 )
!
! MHIGH is usually equal to NDERIV.
!
call bsplvb ( t, k+1-mhigh, 1, x, left, dbiatx )
if ( mhigh == 1 ) then
return
end if
!
! The first column of DBIATX always contains the B-spline values
! for the current order. These are stored in column K+1-current
! order before BSPLVB is called to put values for the next
! higher order on top of it.
!
ideriv = mhigh
do m = 2, mhigh
jp1mid = 1
do j = ideriv, k
dbiatx(j,ideriv) = dbiatx(jp1mid,1)
jp1mid = jp1mid + 1
end do
ideriv = ideriv - 1
call bsplvb ( t, k+1-ideriv, 2, x, left, dbiatx )
end do
!
! At this point, B(LEFT-K+I, K+1-J)(X) is in DBIATX(I,J) for
! I=J,...,K and J=1,...,MHIGH ('=' NDERIV).
!
! In particular, the first column of DBIATX is already in final form.
!
! To obtain corresponding derivatives of B-splines in subsequent columns,
! generate their B-representation by differencing, then evaluate at X.
!
jlow = 1
do i = 1, k
a(jlow:k,i) = 0.0D+00
jlow = i
a(i,i) = 1.0D+00
end do
!
! At this point, A(.,J) contains the B-coefficients for the J-th of the
! K B-splines of interest here.
!
do m = 2, mhigh
fkp1mm = real ( k + 1 - m, kind = 8 )
il = left
i = k
!
! For J = 1,...,K, construct B-coefficients of (M-1)st derivative of
! B-splines from those for preceding derivative by differencing
! and store again in A(.,J). The fact that A(I,J) = 0 for