1
subroutine polar(iopt,m,x,y,z,w,rad,s,nuest,nvest,eps,nu,tu,
2
* nv,tv,u,v,c,fp,wrk1,lwrk1,wrk2,lwrk2,iwrk,kwrk,ier)
3
c subroutine polar fits a smooth function f(x,y) to a set of data
4
c points (x(i),y(i),z(i)) scattered arbitrarily over an approximation
5
c domain x**2+y**2 <= rad(atan(y/x))**2. through the transformation
6
c x = u*rad(v)*cos(v) , y = u*rad(v)*sin(v)
7
c the approximation problem is reduced to the determination of a bi-
8
c cubic spline s(u,v) fitting a corresponding set of data points
9
c (u(i),v(i),z(i)) on the rectangle 0<=u<=1,-pi<=v<=pi.
10
c in order to have continuous partial derivatives
17
c s(u,v)=f(x,y) must satisfy the following conditions
19
c (1) s(0,v) = g(0,0) -pi <=v<= pi.
22
c (2) -------- = rad(v)*(cos(v)*g(1,0)+sin(v)*g(0,1))
27
c (3) -------- = rad(v)*(cos(v)*g(2,0)+sin(v)*g(0,2)+sin(2*v)*g(1,1))
31
c moreover, s(u,v) must be periodic in the variable v, i.e.
34
c d s(u,-pi) d s(u,pi)
35
c (4) ---------- = --------- 0 <=u<= 1, j=0,1,2
39
c if iopt(1) < 0 circle calculates a weighted least-squares spline
40
c according to a given set of knots in u- and v- direction.
41
c if iopt(1) >=0, the number of knots in each direction and their pos-
42
c ition tu(j),j=1,2,...,nu ; tv(j),j=1,2,...,nv are chosen automatical-
43
c ly by the routine. the smoothness of s(u,v) is then achieved by mini-
44
c malizing the discontinuity jumps of the derivatives of the spline
45
c at the knots. the amount of smoothness of s(u,v) is determined by
46
c the condition that fp = sum((w(i)*(z(i)-s(u(i),v(i))))**2) be <= s,
47
c with s a given non-negative constant.
48
c the bicubic spline is given in its standard b-spline representation
49
c and the corresponding function f(x,y) can be evaluated by means of
50
c function program evapol.
53
c call polar(iopt,m,x,y,z,w,rad,s,nuest,nvest,eps,nu,tu,
54
c * nv,tv,u,v,wrk1,lwrk1,wrk2,lwrk2,iwrk,kwrk,ier)
57
c iopt : integer array of dimension 3, specifying different options.
59
c iopt(1):on entry iopt(1) must specify whether a weighted
60
c least-squares polar spline (iopt(1)=-1) or a smoothing
61
c polar spline (iopt(1)=0 or 1) must be determined.
62
c if iopt(1)=0 the routine will start with an initial set of
63
c knots tu(i)=0,tu(i+4)=1,i=1,...,4;tv(i)=(2*i-9)*pi,i=1,...,8.
64
c if iopt(1)=1 the routine will continue with the set of knots
65
c found at the last call of the routine.
66
c attention: a call with iopt(1)=1 must always be immediately
67
c preceded by another call with iopt(1) = 1 or iopt(1) = 0.
68
c iopt(2):on entry iopt(2) must specify the requested order of conti-
69
c nuity for f(x,y) at the origin.
70
c if iopt(2)=0 only condition (1) must be fulfilled,
71
c if iopt(2)=1 conditions (1)+(2) must be fulfilled and
72
c if iopt(2)=2 conditions (1)+(2)+(3) must be fulfilled.
73
c iopt(3):on entry iopt(3) must specify whether (iopt(3)=1) or not
74
c (iopt(3)=0) the approximation f(x,y) must vanish at the
75
c boundary of the approximation domain.
76
c m : integer. on entry m must specify the number of data points.
77
c m >= 4-iopt(2)-iopt(3) unchanged on exit.
78
c x : real array of dimension at least (m).
79
c y : real array of dimension at least (m).
80
c z : real array of dimension at least (m).
81
c before entry, x(i),y(i),z(i) must be set to the co-ordinates
82
c of the i-th data point, for i=1,...,m. the order of the data
83
c points is immaterial. unchanged on exit.
84
c w : real array of dimension at least (m). before entry, w(i) must
85
c be set to the i-th value in the set of weights. the w(i) must
86
c be strictly positive. unchanged on exit.
87
c rad : real function subprogram defining the boundary of the approx-
88
c imation domain, i.e x = rad(v)*cos(v) , y = rad(v)*sin(v),
90
c must be declared external in the calling (sub)program.
91
c s : real. on entry (in case iopt(1) >=0) s must specify the
92
c smoothing factor. s >=0. unchanged on exit.
93
c for advice on the choice of s see further comments
94
c nuest : integer. unchanged on exit.
95
c nvest : integer. unchanged on exit.
96
c on entry, nuest and nvest must specify an upper bound for the
97
c number of knots required in the u- and v-directions resp.
98
c these numbers will also determine the storage space needed by
99
c the routine. nuest >= 8, nvest >= 8.
100
c in most practical situation nuest = nvest = 8+sqrt(m/2) will
101
c be sufficient. see also further comments.
103
c on entry, eps must specify a threshold for determining the
104
c effective rank of an over-determined linear system of equat-
105
c ions. 0 < eps < 1. if the number of decimal digits in the
106
c computer representation of a real number is q, then 10**(-q)
107
c is a suitable value for eps in most practical applications.
110
c unless ier=10 (in case iopt(1) >=0),nu will contain the total
111
c number of knots with respect to the u-variable, of the spline
112
c approximation returned. if the computation mode iopt(1)=1
113
c is used, the value of nu should be left unchanged between
115
c in case iopt(1)=-1,the value of nu must be specified on entry
116
c tu : real array of dimension at least nuest.
117
c on succesful exit, this array will contain the knots of the
118
c spline with respect to the u-variable, i.e. the position
119
c of the interior knots tu(5),...,tu(nu-4) as well as the
120
c position of the additional knots tu(1)=...=tu(4)=0 and
121
c tu(nu-3)=...=tu(nu)=1 needed for the b-spline representation
122
c if the computation mode iopt(1)=1 is used,the values of
123
c tu(1),...,tu(nu) should be left unchanged between subsequent
124
c calls. if the computation mode iopt(1)=-1 is used,the values
125
c tu(5),...tu(nu-4) must be supplied by the user, before entry.
126
c see also the restrictions (ier=10).
128
c unless ier=10 (in case iopt(1)>=0), nv will contain the total
129
c number of knots with respect to the v-variable, of the spline
130
c approximation returned. if the computation mode iopt(1)=1
131
c is used, the value of nv should be left unchanged between
132
c subsequent calls. in case iopt(1)=-1, the value of nv should
133
c be specified on entry.
134
c tv : real array of dimension at least nvest.
135
c on succesful exit, this array will contain the knots of the
136
c spline with respect to the v-variable, i.e. the position of
137
c the interior knots tv(5),...,tv(nv-4) as well as the position
138
c of the additional knots tv(1),...,tv(4) and tv(nv-3),...,
139
c tv(nv) needed for the b-spline representation.
140
c if the computation mode iopt(1)=1 is used, the values of
141
c tv(1),...,tv(nv) should be left unchanged between subsequent
142
c calls. if the computation mode iopt(1)=-1 is used,the values
143
c tv(5),...tv(nv-4) must be supplied by the user, before entry.
144
c see also the restrictions (ier=10).
145
c u : real array of dimension at least (m).
146
c v : real array of dimension at least (m).
147
c on succesful exit, u(i),v(i) contains the co-ordinates of
148
c the i-th data point with respect to the transformed rectan-
149
c gular approximation domain, for i=1,2,...,m.
150
c if the computation mode iopt(1)=1 is used the values of
151
c u(i),v(i) should be left unchanged between subsequent calls.
152
c c : real array of dimension at least (nuest-4)*(nvest-4).
153
c on succesful exit, c contains the coefficients of the spline
154
c approximation s(u,v).
155
c fp : real. unless ier=10, fp contains the weighted sum of
156
c squared residuals of the spline approximation returned.
157
c wrk1 : real array of dimension (lwrk1). used as workspace.
158
c if the computation mode iopt(1)=1 is used the value of
159
c wrk1(1) should be left unchanged between subsequent calls.
160
c on exit wrk1(2),wrk1(3),...,wrk1(1+ncof) will contain the
161
c values d(i)/max(d(i)),i=1,...,ncof=1+iopt(2)*(iopt(2)+3)/2+
162
c (nv-7)*(nu-5-iopt(2)-iopt(3)) with d(i) the i-th diagonal el-
163
c ement of the triangular matrix for calculating the b-spline
164
c coefficients.it includes those elements whose square is < eps
165
c which are treated as 0 in the case of rank deficiency(ier=-2)
166
c lwrk1 : integer. on entry lwrk1 must specify the actual dimension of
167
c the array wrk1 as declared in the calling (sub)program.
168
c lwrk1 must not be too small. let
169
c k = nuest-7, l = nvest-7, p = 1+iopt(2)*(iopt(2)+3)/2,
170
c q = k+2-iopt(2)-iopt(3) then
171
c lwrk1 >= 129+10*k+21*l+k*l+(p+l*q)*(1+8*l+p)+8*m
172
c wrk2 : real array of dimension (lwrk2). used as workspace, but
173
c only in the case a rank deficient system is encountered.
174
c lwrk2 : integer. on entry lwrk2 must specify the actual dimension of
175
c the array wrk2 as declared in the calling (sub)program.
176
c lwrk2 > 0 . a save upper bound for lwrk2 = (p+l*q+1)*(4*l+p)
177
c +p+l*q where p,l,q are as above. if there are enough data
178
c points, scattered uniformly over the approximation domain
179
c and if the smoothing factor s is not too small, there is a
180
c good chance that this extra workspace is not needed. a lot
181
c of memory might therefore be saved by setting lwrk2=1.
182
c (see also ier > 10)
183
c iwrk : integer array of dimension (kwrk). used as workspace.
184
c kwrk : integer. on entry kwrk must specify the actual dimension of
185
c the array iwrk as declared in the calling (sub)program.
186
c kwrk >= m+(nuest-7)*(nvest-7).
187
c ier : integer. unless the routine detects an error, ier contains a
188
c non-positive value on exit, i.e.
189
c ier=0 : normal return. the spline returned has a residual sum of
190
c squares fp such that abs(fp-s)/s <= tol with tol a relat-
191
c ive tolerance set to 0.001 by the program.
192
c ier=-1 : normal return. the spline returned is an interpolating
194
c ier=-2 : normal return. the spline returned is the weighted least-
195
c squares constrained polynomial . in this extreme case
196
c fp gives the upper bound for the smoothing factor s.
197
c ier<-2 : warning. the coefficients of the spline returned have been
198
c computed as the minimal norm least-squares solution of a
199
c (numerically) rank deficient system. (-ier) gives the rank.
200
c especially if the rank deficiency which can be computed as
201
c 1+iopt(2)*(iopt(2)+3)/2+(nv-7)*(nu-5-iopt(2)-iopt(3))+ier
202
c is large the results may be inaccurate.
203
c they could also seriously depend on the value of eps.
204
c ier=1 : error. the required storage space exceeds the available
205
c storage space, as specified by the parameters nuest and
207
c probably causes : nuest or nvest too small. if these param-
208
c eters are already large, it may also indicate that s is
210
c the approximation returned is the weighted least-squares
211
c polar spline according to the current set of knots.
212
c the parameter fp gives the corresponding weighted sum of
213
c squared residuals (fp>s).
214
c ier=2 : error. a theoretically impossible result was found during
215
c the iteration proces for finding a smoothing spline with
216
c fp = s. probably causes : s too small or badly chosen eps.
217
c there is an approximation returned but the corresponding
218
c weighted sum of squared residuals does not satisfy the
219
c condition abs(fp-s)/s < tol.
220
c ier=3 : error. the maximal number of iterations maxit (set to 20
221
c by the program) allowed for finding a smoothing spline
222
c with fp=s has been reached. probably causes : s too small
223
c there is an approximation returned but the corresponding
224
c weighted sum of squared residuals does not satisfy the
225
c condition abs(fp-s)/s < tol.
226
c ier=4 : error. no more knots can be added because the dimension
227
c of the spline 1+iopt(2)*(iopt(2)+3)/2+(nv-7)*(nu-5-iopt(2)
228
c -iopt(3)) already exceeds the number of data points m.
229
c probably causes : either s or m too small.
230
c the approximation returned is the weighted least-squares
231
c polar spline according to the current set of knots.
232
c the parameter fp gives the corresponding weighted sum of
233
c squared residuals (fp>s).
234
c ier=5 : error. no more knots can be added because the additional
235
c knot would (quasi) coincide with an old one.
236
c probably causes : s too small or too large a weight to an
237
c inaccurate data point.
238
c the approximation returned is the weighted least-squares
239
c polar spline according to the current set of knots.
240
c the parameter fp gives the corresponding weighted sum of
241
c squared residuals (fp>s).
242
c ier=10 : error. on entry, the input data are controlled on validity
243
c the following restrictions must be satisfied.
244
c -1<=iopt(1)<=1 , 0<=iopt(2)<=2 , 0<=iopt(3)<=1 ,
245
c m>=4-iopt(2)-iopt(3) , nuest>=8 ,nvest >=8, 0<eps<1,
246
c 0<=teta(i)<=pi, 0<=phi(i)<=2*pi, w(i)>0, i=1,...,m
247
c lwrk1 >= 129+10*k+21*l+k*l+(p+l*q)*(1+8*l+p)+8*m
248
c kwrk >= m+(nuest-7)*(nvest-7)
249
c if iopt(1)=-1:9<=nu<=nuest,9+iopt(2)*(iopt(2)+1)<=nv<=nvest
250
c 0<tu(5)<tu(6)<...<tu(nu-4)<1
251
c -pi<tv(5)<tv(6)<...<tv(nv-4)<pi
252
c if iopt(1)>=0: s>=0
253
c if one of these conditions is found to be violated,control
254
c is immediately repassed to the calling program. in that
255
c case there is no approximation returned.
256
c ier>10 : error. lwrk2 is too small, i.e. there is not enough work-
257
c space for computing the minimal least-squares solution of
258
c a rank deficient system of linear equations. ier gives the
259
c requested value for lwrk2. there is no approximation re-
260
c turned but, having saved the information contained in nu,
261
c nv,tu,tv,wrk1,u,v and having adjusted the value of lwrk2
262
c and the dimension of the array wrk2 accordingly, the user
263
c can continue at the point the program was left, by calling
264
c polar with iopt(1)=1.
267
c by means of the parameter s, the user can control the tradeoff
268
c between closeness of fit and smoothness of fit of the approximation.
269
c if s is too large, the spline will be too smooth and signal will be
270
c lost ; if s is too small the spline will pick up too much noise. in
271
c the extreme cases the program will return an interpolating spline if
272
c s=0 and the constrained weighted least-squares polynomial if s is
273
c very large. between these extremes, a properly chosen s will result
274
c in a good compromise between closeness of fit and smoothness of fit.
275
c to decide whether an approximation, corresponding to a certain s is
276
c satisfactory the user is highly recommended to inspect the fits
278
c recommended values for s depend on the weights w(i). if these are
279
c taken as 1/d(i) with d(i) an estimate of the standard deviation of
280
c z(i), a good s-value should be found in the range (m-sqrt(2*m),m+
281
c sqrt(2*m)). if nothing is known about the statistical error in z(i)
282
c each w(i) can be set equal to one and s determined by trial and
283
c error, taking account of the comments above. the best is then to
284
c start with a very large value of s ( to determine the least-squares
285
c polynomial and the corresponding upper bound fp0 for s) and then to
286
c progressively decrease the value of s ( say by a factor 10 in the
287
c beginning, i.e. s=fp0/10, fp0/100,...and more carefully as the
288
c approximation shows more detail) to obtain closer fits.
289
c to choose s very small is strongly discouraged. this considerably
290
c increases computation time and memory requirements. it may also
291
c cause rank-deficiency (ier<-2) and endager numerical stability.
292
c to economize the search for a good s-value the program provides with
293
c different modes of computation. at the first call of the routine, or
294
c whenever he wants to restart with the initial set of knots the user
295
c must set iopt(1)=0.
296
c if iopt(1)=1 the program will continue with the set of knots found
297
c at the last call of the routine. this will save a lot of computation
298
c time if polar is called repeatedly for different values of s.
299
c the number of knots of the spline returned and their location will
300
c depend on the value of s and on the complexity of the shape of the
301
c function underlying the data. if the computation mode iopt(1)=1
302
c is used, the knots returned may also depend on the s-values at
303
c previous calls (if these were smaller). therefore, if after a number
304
c of trials with different s-values and iopt(1)=1,the user can finally
305
c accept a fit as satisfactory, it may be worthwhile for him to call
306
c polar once more with the selected value for s but now with iopt(1)=0
307
c indeed, polar may then return an approximation of the same quality
308
c of fit but with fewer knots and therefore better if data reduction
309
c is also an important objective for the user.
310
c the number of knots may also depend on the upper bounds nuest and
311
c nvest. indeed, if at a certain stage in polar the number of knots
312
c in one direction (say nu) has reached the value of its upper bound
313
c (nuest), then from that moment on all subsequent knots are added
314
c in the other (v) direction. this may indicate that the value of
315
c nuest is too small. on the other hand, it gives the user the option
316
c of limiting the number of knots the routine locates in any direction
318
c other subroutines required:
319
c fpback,fpbspl,fppola,fpdisc,fpgivs,fprank,fprati,fprota,fporde,
323
c dierckx p.: an algorithm for fitting data over a circle using tensor
324
c product splines,j.comp.appl.maths 15 (1986) 161-173.
325
c dierckx p.: an algorithm for fitting data on a circle using tensor
326
c product splines, report tw68, dept. computer science,
328
c dierckx p.: curve and surface fitting with splines, monographs on
329
c numerical analysis, oxford university press, 1993.
333
c dept. computer science, k.u. leuven
334
c celestijnenlaan 200a, b-3001 heverlee, belgium.
335
c e-mail : Paul.Dierckx@cs.kuleuven.ac.be
337
c creation date : june 1984
338
c latest update : march 1989
341
c ..scalar arguments..
343
integer m,nuest,nvest,nu,nv,lwrk1,lwrk2,kwrk,ier
344
c ..array arguments..
345
real*8 x(m),y(m),z(m),w(m),tu(nuest),tv(nvest),u(m),v(m),
346
* c((nuest-4)*(nvest-4)),wrk1(lwrk1),wrk2(lwrk2)
347
integer iopt(3),iwrk(kwrk)
348
c ..user specified function
351
real*8 tol,pi,dist,r,one
352
integer i,ib1,ib3,ki,kn,kwest,la,lbu,lcc,lcs,lro,j
353
* lbv,lco,lf,lff,lfp,lh,lq,lsu,lsv,lwest,maxit,ncest,ncc,nuu,
354
* nvv,nreg,nrint,nu4,nv4,iopt1,iopt2,iopt3,ipar,nvmin
355
c ..function references..
358
c ..subroutine references..
363
c we set up the parameters tol and maxit.
366
c before starting computations a data check is made. if the input data
367
c are invalid,control is immediately repassed to the calling program.
369
if(eps.le.0. .or. eps.ge.1.) go to 60
371
if(iopt1.lt.(-1) .or. iopt1.gt.1) go to 60
373
if(iopt2.lt.0 .or. iopt2.gt.2) go to 60
375
if(iopt3.lt.0 .or. iopt3.gt.1) go to 60
376
if(m.lt.(4-iopt2-iopt3)) go to 60
377
if(nuest.lt.8 .or. nvest.lt.8) go to 60
383
ipar = 1+iopt2*(iopt2+3)/2
384
ncc = ipar+nvv*(nuest-5-iopt2-iopt3)
389
lwest = ncc*(1+ib1+ib3)+2*nrint+ncest+m*8+ib3+5*nuest+12*nvest
391
if(lwrk1.lt.lwest .or. kwrk.lt.kwest) go to 60
392
if(iopt1.gt.0) go to 40
394
if(w(i).le.0.) go to 60
395
dist = x(i)**2+y(i)**2
398
if(dist.le.0.) go to 10
399
v(i) = datan2(y(i),x(i))
403
if(u(i).gt.one) go to 60
405
if(iopt1.eq.0) go to 40
407
if(nuu.lt.1 .or. nu.gt.nuest) go to 60
411
if(tu(j).le.tu(j-1) .or. tu(j).ge.one) go to 60
414
nvmin = 9+iopt2*(iopt2+1)
415
if(nv.lt.nvmin .or. nv.gt.nvest) go to 60
416
pi = datan2(0d0,-one)
420
if(tv(j).le.tv(j-1) .or. tv(j).ge.pi) go to 60
423
40 if(s.lt.0.) go to 60
425
c we partition the working space and determine the spline approximation
442
call fppola(iopt1,iopt2,iopt3,m,u,v,z,w,rad,s,nuest,nvest,eps,tol,
444
* maxit,ib1,ib3,ncest,ncc,nrint,nreg,nu,tu,nv,tv,c,fp,wrk1(1),
445
* wrk1(lfp),wrk1(lco),wrk1(lf),wrk1(lff),wrk1(lro),wrk1(lcc),
446
* wrk1(lcs),wrk1(la),wrk1(lq),wrk1(lbu),wrk1(lbv),wrk1(lsu),
447
* wrk1(lsv),wrk1(lh),iwrk(ki),iwrk(kn),wrk2,lwrk2,ier)