~ubuntu-branches/ubuntu/raring/python-scipy/raring-proposed

« back to all changes in this revision

Viewing changes to Lib/sandbox/spline/fitpack/fpperi.f

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-07 14:12:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070107141212-mm0ebkh5b37hcpzn
* Remove build dependency on python-numpy-dev.
* python-scipy: Depend on python-numpy instead of python-numpy-dev.
* Package builds on other archs than i386. Closes: #402783.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
      subroutine fpperi(iopt,x,y,w,m,k,s,nest,tol,maxit,k1,k2,n,t,c,
 
2
     * fp,fpint,z,a1,a2,b,g1,g2,q,nrdata,ier)
 
3
c  ..
 
4
c  ..scalar arguments..
 
5
      real*8 s,tol,fp
 
6
      integer iopt,m,k,nest,maxit,k1,k2,n,ier
 
7
c  ..array arguments..
 
8
      real*8 x(m),y(m),w(m),t(nest),c(nest),fpint(nest),z(nest),
 
9
     * a1(nest,k1),a2(nest,k),b(nest,k2),g1(nest,k2),g2(nest,k1),
 
10
     * q(m,k1)
 
11
      integer nrdata(nest)
 
12
c  ..local scalars..
 
13
      real*8 acc,cos,c1,d1,fpart,fpms,fpold,fp0,f1,f2,f3,p,per,pinv,piv,
 
14
     *
 
15
     * p1,p2,p3,sin,store,term,wi,xi,yi,rn,one,con1,con4,con9,half
 
16
      integer i,ich1,ich3,ij,ik,it,iter,i1,i2,i3,j,jk,jper,j1,j2,kk,
 
17
     * kk1,k3,l,l0,l1,l5,mm,m1,new,nk1,nk2,nmax,nmin,nplus,npl1,
 
18
     * nrint,n10,n11,n7,n8
 
19
c  ..local arrays..
 
20
      real*8 h(6),h1(7),h2(6)
 
21
c  ..function references..
 
22
      real*8 abs,fprati
 
23
      integer max0,min0
 
24
c  ..subroutine references..
 
25
c    fpbacp,fpbspl,fpgivs,fpdisc,fpknot,fprota
 
26
c  ..
 
27
c  set constants
 
28
      one = 0.1e+01
 
29
      con1 = 0.1e0
 
30
      con9 = 0.9e0
 
31
      con4 = 0.4e-01
 
32
      half = 0.5e0
 
33
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
 
34
c  part 1: determination of the number of knots and their position     c
 
35
c  **************************************************************      c
 
36
c  given a set of knots we compute the least-squares periodic spline   c
 
37
c  sinf(x). if the sum f(p=inf) <= s we accept the choice of knots.    c
 
38
c  the initial choice of knots depends on the value of s and iopt.     c
 
39
c    if s=0 we have spline interpolation; in that case the number of   c
 
40
c    knots equals nmax = m+2*k.                                        c
 
41
c    if s > 0 and                                                      c
 
42
c      iopt=0 we first compute the least-squares polynomial of         c
 
43
c      degree k; n = nmin = 2*k+2. since s(x) must be periodic we      c
 
44
c      find that s(x) is a constant function.                          c
 
45
c      iopt=1 we start with the set of knots found at the last         c
 
46
c      call of the routine, except for the case that s > fp0; then     c
 
47
c      we compute directly the least-squares periodic polynomial.      c
 
48
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
 
49
      m1 = m-1
 
50
      kk = k
 
51
      kk1 = k1
 
52
      k3 = 3*k+1
 
53
      nmin = 2*k1
 
54
c  determine the length of the period of s(x).
 
55
      per = x(m)-x(1)
 
56
      if(iopt.lt.0) go to 50
 
57
c  calculation of acc, the absolute tolerance for the root of f(p)=s.
 
58
      acc = tol*s
 
59
c  determine nmax, the number of knots for periodic spline interpolation
 
60
      nmax = m+2*k
 
61
      if(s.gt.0. .or. nmax.eq.nmin) go to 30
 
62
c  if s=0, s(x) is an interpolating spline.
 
63
      n = nmax
 
64
c  test whether the required storage space exceeds the available one.
 
65
      if(n.gt.nest) go to 620
 
66
c  find the position of the interior knots in case of interpolation.
 
67
   5  if((k/2)*2 .eq. k) go to 20
 
68
      do 10 i=2,m1
 
69
        j = i+k
 
70
        t(j) = x(i)
 
71
  10  continue
 
72
      if(s.gt.0.) go to 50
 
73
      kk = k-1
 
74
      kk1 = k
 
75
      if(kk.gt.0) go to 50
 
76
      t(1) = t(m)-per
 
77
      t(2) = x(1)
 
78
      t(m+1) = x(m)
 
79
      t(m+2) = t(3)+per
 
80
      do 15 i=1,m1
 
81
        c(i) = y(i)
 
82
  15  continue
 
83
      c(m) = c(1)
 
84
      fp = 0.
 
85
      fpint(n) = fp0
 
86
      fpint(n-1) = 0.
 
87
      nrdata(n) = 0
 
88
      go to 630
 
89
  20  do 25 i=2,m1
 
90
        j = i+k
 
91
        t(j) = (x(i)+x(i-1))*half
 
92
  25  continue
 
93
      go to 50
 
94
c  if s > 0 our initial choice depends on the value of iopt.
 
95
c  if iopt=0 or iopt=1 and s>=fp0, we start computing the least-squares
 
96
c  periodic polynomial. (i.e. a constant function).
 
97
c  if iopt=1 and fp0>s we start computing the least-squares periodic
 
98
c  spline according the set of knots found at the last call of the
 
99
c  routine.
 
100
  30  if(iopt.eq.0) go to 35
 
101
      if(n.eq.nmin) go to 35
 
102
      fp0 = fpint(n)
 
103
      fpold = fpint(n-1)
 
104
      nplus = nrdata(n)
 
105
      if(fp0.gt.s) go to 50
 
106
c  the case that s(x) is a constant function is treated separetely.
 
107
c  find the least-squares constant c1 and compute fp0 at the same time.
 
108
  35  fp0 = 0.
 
109
      d1 = 0.
 
110
      c1 = 0.
 
111
      do 40 it=1,m1
 
112
        wi = w(it)
 
113
        yi = y(it)*wi
 
114
        call fpgivs(wi,d1,cos,sin)
 
115
        call fprota(cos,sin,yi,c1)
 
116
        fp0 = fp0+yi**2
 
117
  40  continue
 
118
      c1 = c1/d1
 
119
c  test whether that constant function is a solution of our problem.
 
120
      fpms = fp0-s
 
121
      if(fpms.lt.acc .or. nmax.eq.nmin) go to 640
 
122
      fpold = fp0
 
123
c  test whether the required storage space exceeds the available one.
 
124
      if(nmin.ge.nest) go to 620
 
125
c  start computing the least-squares periodic spline with one
 
126
c  interior knot.
 
127
      nplus = 1
 
128
      n = nmin+1
 
129
      mm = (m+1)/2
 
130
      t(k2) = x(mm)
 
131
      nrdata(1) = mm-2
 
132
      nrdata(2) = m1-mm
 
133
c  main loop for the different sets of knots. m is a save upper
 
134
c  bound for the number of trials.
 
135
  50  do 340 iter=1,m
 
136
c  find nrint, the number of knot intervals.
 
137
        nrint = n-nmin+1
 
138
c  find the position of the additional knots which are needed for
 
139
c  the b-spline representation of s(x). if we take
 
140
c      t(k+1) = x(1), t(n-k) = x(m)
 
141
c      t(k+1-j) = t(n-k-j) - per, j=1,2,...k
 
142
c      t(n-k+j) = t(k+1+j) + per, j=1,2,...k
 
143
c  then s(x) is a periodic spline with period per if the b-spline
 
144
c  coefficients satisfy the following conditions
 
145
c      c(n7+j) = c(j), j=1,...k   (**)   with n7=n-2*k-1.
 
146
        t(k1) = x(1)
 
147
        nk1 = n-k1
 
148
        nk2 = nk1+1
 
149
        t(nk2) = x(m)
 
150
        do 60 j=1,k
 
151
          i1 = nk2+j
 
152
          i2 = nk2-j
 
153
          j1 = k1+j
 
154
          j2 = k1-j
 
155
          t(i1) = t(j1)+per
 
156
          t(j2) = t(i2)-per
 
157
  60    continue
 
158
c  compute the b-spline coefficients c(j),j=1,...n7 of the least-squares
 
159
c  periodic spline sinf(x). the observation matrix a is built up row
 
160
c  by row while taking into account condition (**) and is reduced to
 
161
c  triangular form by givens transformations .
 
162
c  at the same time fp=f(p=inf) is computed.
 
163
c  the n7 x n7 triangularised upper matrix a has the form
 
164
c            ! a1 '    !
 
165
c        a = !    ' a2 !
 
166
c            ! 0  '    !
 
167
c  with a2 a n7 x k matrix and a1 a n10 x n10 upper triangular
 
168
c  matrix of bandwith k+1 ( n10 = n7-k).
 
169
c  initialization.
 
170
        do 70 i=1,nk1
 
171
          z(i) = 0.
 
172
          do 70 j=1,kk1
 
173
            a1(i,j) = 0.
 
174
  70    continue
 
175
        n7 = nk1-k
 
176
        n10 = n7-kk
 
177
        jper = 0
 
178
        fp = 0.
 
179
        l = k1
 
180
        do 290 it=1,m1
 
181
c  fetch the current data point x(it),y(it)
 
182
          xi = x(it)
 
183
          wi = w(it)
 
184
          yi = y(it)*wi
 
185
c  search for knot interval t(l) <= xi < t(l+1).
 
186
  80      if(xi.lt.t(l+1)) go to 85
 
187
          l = l+1
 
188
          go to 80
 
189
c  evaluate the (k+1) non-zero b-splines at xi and store them in q.
 
190
  85      call fpbspl(t,n,k,xi,l,h)
 
191
          do 90 i=1,k1
 
192
            q(it,i) = h(i)
 
193
            h(i) = h(i)*wi
 
194
  90      continue
 
195
          l5 = l-k1
 
196
c  test whether the b-splines nj,k+1(x),j=1+n7,...nk1 are all zero at xi
 
197
          if(l5.lt.n10) go to 285
 
198
          if(jper.ne.0) go to 160
 
199
c  initialize the matrix a2.
 
200
          do 95 i=1,n7
 
201
          do 95 j=1,kk
 
202
              a2(i,j) = 0.
 
203
  95      continue
 
204
          jk = n10+1
 
205
          do 110 i=1,kk
 
206
            ik = jk
 
207
            do 100 j=1,kk1
 
208
              if(ik.le.0) go to 105
 
209
              a2(ik,i) = a1(ik,j)
 
210
              ik = ik-1
 
211
 100        continue
 
212
 105        jk = jk+1
 
213
 110      continue
 
214
          jper = 1
 
215
c  if one of the b-splines nj,k+1(x),j=n7+1,...nk1 is not zero at xi
 
216
c  we take account of condition (**) for setting up the new row
 
217
c  of the observation matrix a. this row is stored in the arrays h1
 
218
c  (the part with respect to a1) and h2 (the part with
 
219
c  respect to a2).
 
220
 160      do 170 i=1,kk
 
221
            h1(i) = 0.
 
222
            h2(i) = 0.
 
223
 170      continue
 
224
          h1(kk1) = 0.
 
225
          j = l5-n10
 
226
          do 210 i=1,kk1
 
227
            j = j+1
 
228
            l0 = j
 
229
 180        l1 = l0-kk
 
230
            if(l1.le.0) go to 200
 
231
            if(l1.le.n10) go to 190
 
232
            l0 = l1-n10
 
233
            go to 180
 
234
 190        h1(l1) = h(i)
 
235
            go to 210
 
236
 200        h2(l0) = h2(l0)+h(i)
 
237
 210      continue
 
238
c  rotate the new row of the observation matrix into triangle
 
239
c  by givens transformations.
 
240
          if(n10.le.0) go to 250
 
241
c  rotation with the rows 1,2,...n10 of matrix a.
 
242
          do 240 j=1,n10
 
243
            piv = h1(1)
 
244
            if(piv.ne.0.) go to 214
 
245
            do 212 i=1,kk
 
246
              h1(i) = h1(i+1)
 
247
 212        continue
 
248
            h1(kk1) = 0.
 
249
            go to 240
 
250
c  calculate the parameters of the givens transformation.
 
251
 214        call fpgivs(piv,a1(j,1),cos,sin)
 
252
c  transformation to the right hand side.
 
253
            call fprota(cos,sin,yi,z(j))
 
254
c  transformations to the left hand side with respect to a2.
 
255
            do 220 i=1,kk
 
256
              call fprota(cos,sin,h2(i),a2(j,i))
 
257
 220        continue
 
258
            if(j.eq.n10) go to 250
 
259
            i2 = min0(n10-j,kk)
 
260
c  transformations to the left hand side with respect to a1.
 
261
            do 230 i=1,i2
 
262
              i1 = i+1
 
263
              call fprota(cos,sin,h1(i1),a1(j,i1))
 
264
              h1(i) = h1(i1)
 
265
 230        continue
 
266
            h1(i1) = 0.
 
267
 240      continue
 
268
c  rotation with the rows n10+1,...n7 of matrix a.
 
269
 250      do 270 j=1,kk
 
270
            ij = n10+j
 
271
            if(ij.le.0) go to 270
 
272
            piv = h2(j)
 
273
            if(piv.eq.0.) go to 270
 
274
c  calculate the parameters of the givens transformation.
 
275
            call fpgivs(piv,a2(ij,j),cos,sin)
 
276
c  transformations to right hand side.
 
277
            call fprota(cos,sin,yi,z(ij))
 
278
            if(j.eq.kk) go to 280
 
279
            j1 = j+1
 
280
c  transformations to left hand side.
 
281
            do 260 i=j1,kk
 
282
              call fprota(cos,sin,h2(i),a2(ij,i))
 
283
 260        continue
 
284
 270      continue
 
285
c  add contribution of this row to the sum of squares of residual
 
286
c  right hand sides.
 
287
 280      fp = fp+yi**2
 
288
          go to 290
 
289
c  rotation of the new row of the observation matrix into
 
290
c  triangle in case the b-splines nj,k+1(x),j=n7+1,...n-k-1 are all zero
 
291
c  at xi.
 
292
 285      j = l5
 
293
          do 140 i=1,kk1
 
294
            j = j+1
 
295
            piv = h(i)
 
296
            if(piv.eq.0.) go to 140
 
297
c  calculate the parameters of the givens transformation.
 
298
            call fpgivs(piv,a1(j,1),cos,sin)
 
299
c  transformations to right hand side.
 
300
            call fprota(cos,sin,yi,z(j))
 
301
            if(i.eq.kk1) go to 150
 
302
            i2 = 1
 
303
            i3 = i+1
 
304
c  transformations to left hand side.
 
305
            do 130 i1=i3,kk1
 
306
              i2 = i2+1
 
307
              call fprota(cos,sin,h(i1),a1(j,i2))
 
308
 130        continue
 
309
 140      continue
 
310
c  add contribution of this row to the sum of squares of residual
 
311
c  right hand sides.
 
312
 150      fp = fp+yi**2
 
313
 290    continue
 
314
        fpint(n) = fp0
 
315
        fpint(n-1) = fpold
 
316
        nrdata(n) = nplus
 
317
c  backward substitution to obtain the b-spline coefficients c(j),j=1,.n
 
318
        call fpbacp(a1,a2,z,n7,kk,c,kk1,nest)
 
319
c  calculate from condition (**) the coefficients c(j+n7),j=1,2,...k.
 
320
        do 295 i=1,k
 
321
          j = i+n7
 
322
          c(j) = c(i)
 
323
 295    continue
 
324
        if(iopt.lt.0) go to 660
 
325
c  test whether the approximation sinf(x) is an acceptable solution.
 
326
        fpms = fp-s
 
327
        if(abs(fpms).lt.acc) go to 660
 
328
c  if f(p=inf) < s accept the choice of knots.
 
329
        if(fpms.lt.0.) go to 350
 
330
c  if n=nmax, sinf(x) is an interpolating spline.
 
331
        if(n.eq.nmax) go to 630
 
332
c  increase the number of knots.
 
333
c  if n=nest we cannot increase the number of knots because of the
 
334
c  storage capacity limitation.
 
335
        if(n.eq.nest) go to 620
 
336
c  determine the number of knots nplus we are going to add.
 
337
        npl1 = nplus*2
 
338
        rn = nplus
 
339
        if(fpold-fp.gt.acc) npl1 = rn*fpms/(fpold-fp)
 
340
        nplus = min0(nplus*2,max0(npl1,nplus/2,1))
 
341
        fpold = fp
 
342
c  compute the sum(wi*(yi-s(xi))**2) for each knot interval
 
343
c  t(j+k) <= xi <= t(j+k+1) and store it in fpint(j),j=1,2,...nrint.
 
344
        fpart = 0.
 
345
        i = 1
 
346
        l = k1
 
347
        do 320 it=1,m1
 
348
          if(x(it).lt.t(l)) go to 300
 
349
          new = 1
 
350
          l = l+1
 
351
 300      term = 0.
 
352
          l0 = l-k2
 
353
          do 310 j=1,k1
 
354
            l0 = l0+1
 
355
            term = term+c(l0)*q(it,j)
 
356
 310      continue
 
357
          term = (w(it)*(term-y(it)))**2
 
358
          fpart = fpart+term
 
359
          if(new.eq.0) go to 320
 
360
          if(l.gt.k2) go to 315
 
361
          fpint(nrint) = term
 
362
          new = 0
 
363
          go to 320
 
364
 315      store = term*half
 
365
          fpint(i) = fpart-store
 
366
          i = i+1
 
367
          fpart = store
 
368
          new = 0
 
369
 320    continue
 
370
        fpint(nrint) = fpint(nrint)+fpart
 
371
        do 330 l=1,nplus
 
372
c  add a new knot
 
373
          call fpknot(x,m,t,n,fpint,nrdata,nrint,nest,1)
 
374
c  if n=nmax we locate the knots as for interpolation.
 
375
          if(n.eq.nmax) go to 5
 
376
c  test whether we cannot further increase the number of knots.
 
377
          if(n.eq.nest) go to 340
 
378
 330    continue
 
379
c  restart the computations with the new set of knots.
 
380
 340  continue
 
381
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
 
382
c  part 2: determination of the smoothing periodic spline sp(x).       c
 
383
c  *************************************************************       c
 
384
c  we have determined the number of knots and their position.          c
 
385
c  we now compute the b-spline coefficients of the smoothing spline    c
 
386
c  sp(x). the observation matrix a is extended by the rows of matrix   c
 
387
c  b expressing that the kth derivative discontinuities of sp(x) at    c
 
388
c  the interior knots t(k+2),...t(n-k-1) must be zero. the corres-     c
 
389
c  ponding weights of these additional rows are set to 1/sqrt(p).      c
 
390
c  iteratively we then have to determine the value of p such that      c
 
391
c  f(p)=sum(w(i)*(y(i)-sp(x(i)))**2) be = s. we already know that      c
 
392
c  the least-squares constant function corresponds to p=0, and that    c
 
393
c  the least-squares periodic spline corresponds to p=infinity. the    c
 
394
c  iteration process which is proposed here, makes use of rational     c
 
395
c  interpolation. since f(p) is a convex and strictly decreasing       c
 
396
c  function of p, it can be approximated by a rational function        c
 
397
c  r(p) = (u*p+v)/(p+w). three values of p(p1,p2,p3) with correspond-  c
 
398
c  ing values of f(p) (f1=f(p1)-s,f2=f(p2)-s,f3=f(p3)-s) are used      c
 
399
c  to calculate the new value of p such that r(p)=s. convergence is    c
 
400
c  guaranteed by taking f1>0 and f3<0.                                 c
 
401
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
 
402
c  evaluate the discontinuity jump of the kth derivative of the
 
403
c  b-splines at the knots t(l),l=k+2,...n-k-1 and store in b.
 
404
 350  call fpdisc(t,n,k2,b,nest)
 
405
c  initial value for p.
 
406
      p1 = 0.
 
407
      f1 = fp0-s
 
408
      p3 = -one
 
409
      f3 = fpms
 
410
      n11 = n10-1
 
411
      n8 = n7-1
 
412
      p = 0.
 
413
      l = n7
 
414
      do 352 i=1,k
 
415
         j = k+1-i
 
416
         p = p+a2(l,j)
 
417
         l = l-1
 
418
         if(l.eq.0) go to 356
 
419
 352  continue
 
420
      do 354 i=1,n10
 
421
         p = p+a1(i,1)
 
422
 354  continue
 
423
 356  rn = n7
 
424
      p = rn/p
 
425
      ich1 = 0
 
426
      ich3 = 0
 
427
c  iteration process to find the root of f(p) = s.
 
428
      do 595 iter=1,maxit
 
429
c  form the matrix g  as the matrix a extended by the rows of matrix b.
 
430
c  the rows of matrix b with weight 1/p are rotated into
 
431
c  the triangularised observation matrix a.
 
432
c  after triangularisation our n7 x n7 matrix g takes the form
 
433
c            ! g1 '    !
 
434
c        g = !    ' g2 !
 
435
c            ! 0  '    !
 
436
c  with g2 a n7 x (k+1) matrix and g1 a n11 x n11 upper triangular
 
437
c  matrix of bandwidth k+2. ( n11 = n7-k-1)
 
438
        pinv = one/p
 
439
c  store matrix a into g
 
440
        do 360 i=1,n7
 
441
          c(i) = z(i)
 
442
          g1(i,k1) = a1(i,k1)
 
443
          g1(i,k2) = 0.
 
444
          g2(i,1) = 0.
 
445
          do 360 j=1,k
 
446
            g1(i,j) = a1(i,j)
 
447
            g2(i,j+1) = a2(i,j)
 
448
 360    continue
 
449
        l = n10
 
450
        do 370 j=1,k1
 
451
          if(l.le.0) go to 375
 
452
          g2(l,1) = a1(l,j)
 
453
          l = l-1
 
454
 370    continue
 
455
 375    do 540 it=1,n8
 
456
c  fetch a new row of matrix b and store it in the arrays h1 (the part
 
457
c  with respect to g1) and h2 (the part with respect to g2).
 
458
          yi = 0.
 
459
          do 380 i=1,k1
 
460
            h1(i) = 0.
 
461
            h2(i) = 0.
 
462
 380      continue
 
463
          h1(k2) = 0.
 
464
          if(it.gt.n11) go to 420
 
465
          l = it
 
466
          l0 = it
 
467
          do 390 j=1,k2
 
468
            if(l0.eq.n10) go to 400
 
469
            h1(j) = b(it,j)*pinv
 
470
            l0 = l0+1
 
471
 390      continue
 
472
          go to 470
 
473
 400      l0 = 1
 
474
          do 410 l1=j,k2
 
475
            h2(l0) = b(it,l1)*pinv
 
476
            l0 = l0+1
 
477
 410      continue
 
478
          go to 470
 
479
 420      l = 1
 
480
          i = it-n10
 
481
          do 460 j=1,k2
 
482
            i = i+1
 
483
            l0 = i
 
484
 430        l1 = l0-k1
 
485
            if(l1.le.0) go to 450
 
486
            if(l1.le.n11) go to 440
 
487
            l0 = l1-n11
 
488
            go to 430
 
489
 440        h1(l1) = b(it,j)*pinv
 
490
            go to 460
 
491
 450        h2(l0) = h2(l0)+b(it,j)*pinv
 
492
 460      continue
 
493
          if(n11.le.0) go to 510
 
494
c  rotate this row into triangle by givens transformations without
 
495
c  square roots.
 
496
c  rotation with the rows l,l+1,...n11.
 
497
 470      do 500 j=l,n11
 
498
            piv = h1(1)
 
499
c  calculate the parameters of the givens transformation.
 
500
            call fpgivs(piv,g1(j,1),cos,sin)
 
501
c  transformation to right hand side.
 
502
            call fprota(cos,sin,yi,c(j))
 
503
c  transformation to the left hand side with respect to g2.
 
504
            do 480 i=1,k1
 
505
              call fprota(cos,sin,h2(i),g2(j,i))
 
506
 480        continue
 
507
            if(j.eq.n11) go to 510
 
508
            i2 = min0(n11-j,k1)
 
509
c  transformation to the left hand side with respect to g1.
 
510
            do 490 i=1,i2
 
511
              i1 = i+1
 
512
              call fprota(cos,sin,h1(i1),g1(j,i1))
 
513
              h1(i) = h1(i1)
 
514
 490        continue
 
515
            h1(i1) = 0.
 
516
 500      continue
 
517
c  rotation with the rows n11+1,...n7
 
518
 510      do 530 j=1,k1
 
519
            ij = n11+j
 
520
            if(ij.le.0) go to 530
 
521
            piv = h2(j)
 
522
c  calculate the parameters of the givens transformation
 
523
            call fpgivs(piv,g2(ij,j),cos,sin)
 
524
c  transformation to the right hand side.
 
525
            call fprota(cos,sin,yi,c(ij))
 
526
            if(j.eq.k1) go to 540
 
527
            j1 = j+1
 
528
c  transformation to the left hand side.
 
529
            do 520 i=j1,k1
 
530
              call fprota(cos,sin,h2(i),g2(ij,i))
 
531
 520        continue
 
532
 530      continue
 
533
 540    continue
 
534
c  backward substitution to obtain the b-spline coefficients
 
535
c  c(j),j=1,2,...n7 of sp(x).
 
536
        call fpbacp(g1,g2,c,n7,k1,c,k2,nest)
 
537
c  calculate from condition (**) the b-spline coefficients c(n7+j),j=1,.
 
538
        do 545 i=1,k
 
539
          j = i+n7
 
540
          c(j) = c(i)
 
541
 545    continue
 
542
c  computation of f(p).
 
543
        fp = 0.
 
544
        l = k1
 
545
        do 570 it=1,m1
 
546
          if(x(it).lt.t(l)) go to 550
 
547
          l = l+1
 
548
 550      l0 = l-k2
 
549
          term = 0.
 
550
          do 560 j=1,k1
 
551
            l0 = l0+1
 
552
            term = term+c(l0)*q(it,j)
 
553
 560      continue
 
554
          fp = fp+(w(it)*(term-y(it)))**2
 
555
 570    continue
 
556
c  test whether the approximation sp(x) is an acceptable solution.
 
557
        fpms = fp-s
 
558
        if(abs(fpms).lt.acc) go to 660
 
559
c  test whether the maximal number of iterations is reached.
 
560
        if(iter.eq.maxit) go to 600
 
561
c  carry out one more step of the iteration process.
 
562
        p2 = p
 
563
        f2 = fpms
 
564
        if(ich3.ne.0) go to 580
 
565
        if((f2-f3) .gt. acc) go to 575
 
566
c  our initial choice of p is too large.
 
567
        p3 = p2
 
568
        f3 = f2
 
569
        p = p*con4
 
570
        if(p.le.p1) p = p1*con9 +p2*con1
 
571
        go to 595
 
572
 575    if(f2.lt.0.) ich3 = 1
 
573
 580    if(ich1.ne.0) go to 590
 
574
        if((f1-f2) .gt. acc) go to 585
 
575
c  our initial choice of p is too small
 
576
        p1 = p2
 
577
        f1 = f2
 
578
        p = p/con4
 
579
        if(p3.lt.0.) go to 595
 
580
        if(p.ge.p3) p = p2*con1 +p3*con9
 
581
        go to 595
 
582
 585    if(f2.gt.0.) ich1 = 1
 
583
c  test whether the iteration process proceeds as theoretically
 
584
c  expected.
 
585
 590    if(f2.ge.f1 .or. f2.le.f3) go to 610
 
586
c  find the new value for p.
 
587
        p = fprati(p1,f1,p2,f2,p3,f3)
 
588
 595  continue
 
589
c  error codes and messages.
 
590
 600  ier = 3
 
591
      go to 660
 
592
 610  ier = 2
 
593
      go to 660
 
594
 620  ier = 1
 
595
      go to 660
 
596
 630  ier = -1
 
597
      go to 660
 
598
 640  ier = -2
 
599
c  the least-squares constant function c1 is a solution of our problem.
 
600
c  a constant function is a spline of degree k with all b-spline
 
601
c  coefficients equal to that constant c1.
 
602
      do 650 i=1,k1
 
603
        rn = k1-i
 
604
        t(i) = x(1)-rn*per
 
605
        c(i) = c1
 
606
        j = i+k1
 
607
        rn = i-1
 
608
        t(j) = x(m)+rn*per
 
609
 650  continue
 
610
      n = nmin
 
611
      fp = fp0
 
612
      fpint(n) = fp0
 
613
      fpint(n-1) = 0.
 
614
      nrdata(n) = 0
 
615
 660  return
 
616
      end