spl.pas 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. {
  2. This file is part of the Numlib package.
  3. Copyright (c) 1986-2000 by
  4. Kees van Ginneken, Wil Kortsmit and Loek van Reij of the
  5. Computational centre of the Eindhoven University of Technology
  6. FPC port Code by Marco van de Voort ([email protected])
  7. documentation by Michael van Canneyt ([email protected])
  8. Undocumented unit. B- and other Splines. Not imported by the other units
  9. afaik.
  10. See the file COPYING.FPC, included in this distribution,
  11. for details about the copyright.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. **********************************************************************}
  16. unit spl;
  17. {$I direct.inc}
  18. interface
  19. uses typ, sle;
  20. function spl1bspv(q: ArbInt; var kmin1, c1: ArbFloat; x: ArbFloat; var term: ArbInt): ArbFloat;
  21. function spl2bspv(qx, qy: ArbInt; var kxmin1, kymin1, c11: ArbFloat; x, y: ArbFloat; var term: ArbInt): ArbFloat;
  22. procedure spl1bspf(M, Q: ArbInt; var XYW1: ArbFloat;
  23. var Kmin1, C1, residu: ArbFloat;
  24. var term: ArbInt);
  25. procedure spl2bspf(M, Qx, Qy: ArbInt; var XYZW1: ArbFloat;
  26. var Kxmin1, Kymin1, C11, residu: ArbFloat;
  27. var term: ArbInt);
  28. procedure spl1nati(n: ArbInt; var xyc1: ArbFloat; var term: ArbInt);
  29. procedure spl1naki(n: ArbInt; var xyc1: ArbFloat; var term: ArbInt);
  30. procedure spl1cmpi(n: ArbInt; var xyc1: ArbFloat; dy1, dyn: ArbFloat;
  31. var term: ArbInt);
  32. procedure spl1peri(n: ArbInt; var xyc1: ArbFloat; var term: ArbInt);
  33. function spl1pprv(n: ArbInt; var xyc1: ArbFloat; t: ArbFloat; var term: ArbInt): ArbFloat;
  34. procedure spl1nalf(n: ArbInt; var xyw1: ArbFloat; lambda:ArbFloat;
  35. var xac1, residu: ArbFloat; var term: ArbInt);
  36. function spl2natv(n: ArbInt; var xyg0: ArbFloat; u, v: ArbFloat): ArbFloat;
  37. procedure spl2nalf(n: ArbInt; var xyzw1: ArbFloat; lambda:ArbFloat;
  38. var xyg0, residu: ArbFloat; var term: ArbInt);
  39. { term = 1: succes,
  40. term = 2: set linear equations is not "PD"
  41. term = 4: Approx. number of points? On a line.
  42. term = 3: wrong input n<3 or a weight turned out to be <=0 }
  43. implementation
  44. {$goto on}
  45. type
  46. Krec = record K1, K2, K3, K4, K5, K6 : ArbFloat end;
  47. function spl1bspv(q: ArbInt; var kmin1, c1: ArbFloat; x: ArbFloat; var term: ArbInt): ArbFloat;
  48. var c : arfloat1 absolute c1;
  49. k : arfloat_1 absolute kmin1;
  50. D1, D2, D3,
  51. E2, E3, E4, E5: ArbFloat;
  52. pk : ^Krec;
  53. l, r, m : ArbInt;
  54. begin
  55. spl1bspv := NaN;
  56. term := 3; { q >=4 ! }
  57. if q<4 then exit; { at least 1 interval }
  58. if (x<k[2]) or (x>k[q-1]) then exit; { x inside the interval }
  59. term := 1; { Let's hope the params are good :-)}
  60. l := 2; r := q-1;
  61. while l+1<r do { after this loop goes: }
  62. begin { k[l]<=x<=k[l+1] with }
  63. m := (l+r) div 2; { k[l] < k[l+1] }
  64. if x>=k[m] then l := m else r := m
  65. end;
  66. pk := @k[l-2]; { the (de) Boor algoritm .. }
  67. with pk^ do
  68. begin
  69. E2 := X - K2; E3 := X - K3; E4 := K4 - X; E5 := K5 - X;
  70. D2 := C[l]; D3 := C[l+1];
  71. D1 := ((X-K1)*D2+E4*C[l-1])/(K4-K1);
  72. D2 := (E2*D3+E5*D2)/(K5-K2);
  73. D3 := (E3*C[l+2]+(K6-X)*D3)/(K6-K3);
  74. D1 := (E2*D2+E4*D1)/(K4-K2);
  75. D2 := (E3*D3+E5*D2)/(K5-K3);
  76. spl1bspv := (E3*D2+E4*D1)/(K4-K3)
  77. end;
  78. end;
  79. function spl2bspv(qx, qy: ArbInt; var kxmin1, kymin1, c11: ArbFloat; x, y: ArbFloat; var term: ArbInt): ArbFloat;
  80. var pd: ^arfloat1;
  81. i, iy: ArbInt;
  82. c: arfloat1 absolute c11;
  83. begin
  84. GetMem(pd, qx*SizeOf(ArbFloat));
  85. i := 0;
  86. iy := 1;
  87. repeat
  88. i := i + 1;
  89. pd^[i] := spl1bspv(qy, kymin1, c[iy], y, term);
  90. Inc(iy, qy)
  91. until (i=qx) or (term<>1);
  92. if term=1
  93. then spl2bspv := spl1bspv(qx, kxmin1, pd^[1], x, term)
  94. else spl2bspv := NaN;
  95. FreeMem(pd, qx*SizeOf(ArbFloat));
  96. end;
  97. (* Bron: NAG LIBRARY SUBROUTINE E02BAF *)
  98. function Imin(x, y: ArbInt): ArbInt;
  99. begin if x<y then Imin := x else Imin := y end;
  100. type ar4 = array[1..$ffe0 div (4*SizeOf(ArbFloat)),1..4] of ArbFloat;
  101. ar3 = array[1..$ffe0 div (3*SizeOf(ArbFloat)),1..3] of ArbFloat;
  102. r_3 = record x, y, w: ArbFloat end;
  103. r3Ar= array[1..$ffe0 div SizeOf(r_3)] of r_3;
  104. r_4 = record x, y, z, w: ArbFloat end;
  105. r4Ar= array[1..$ffe0 div SizeOf(r_4)] of r_4;
  106. r4 = array[1..4] of ArbFloat;
  107. r2 = array[1..2] of ArbFloat;
  108. r4x = record xy: R2; alfa, d: ArbFloat end;
  109. r4xAr= array[1..$ffe0 div SizeOf(r4x)] of r4x;
  110. nsp2rec = array[0..$ff80 div (3*SizeOf(ArbFloat))] of
  111. record xy: R2; gamma: ArbFloat end;
  112. procedure spl1bspf(M, Q: ArbInt; var XYW1: ArbFloat;
  113. var Kmin1, C1, residu: ArbFloat;
  114. var term: ArbInt);
  115. var work1: ^arfloat1;
  116. work2: ^ar4;
  117. c : arfloat1 absolute c1;
  118. k : arfloat_1 absolute kmin1;
  119. xyw : r3Ar absolute XYW1;
  120. r, j, jmax, l, lplus1, i, iplusj, jold, jrev,
  121. jplusl, iu, lplusu : ArbInt;
  122. s, k0, k4, sigma,
  123. d, d4, d5, d6, d7, d8, d9,
  124. e2, e3, e4, e5,
  125. n1, n2, n3,
  126. relemt, dprime, cosine, sine,
  127. acol, arow, crow, ccol, ss : ArbFloat;
  128. pk : ^Krec;
  129. label einde;
  130. (*
  131. DOUBLE PRECISION C(NCAP7), K(NCAP7), W(M), WORK1(M),
  132. * WORK2(4,NCAP7), X(M), Y(M)
  133. .. Local Scalars ..
  134. DOUBLE PRECISION ACOL, AROW, CCOL, COSINE, CROW, D, D4, D5, D6,
  135. * D7, D8, D9, DPRIME, E2, E3, E4, E5, K0, K1, K2,
  136. * K3, K4, K5, K6, N1, N2, N3, RELEMT, S, SIGMA,
  137. * SINE, WI, XI
  138. INTEGER I, IERROR, IPLUSJ, IU, J, JOLD, JPLUSL, JREV, L,
  139. * L4, LPLUS1, LPLUSU, NCAP, NCAP3, NCAPM1, R
  140. *)
  141. begin
  142. term := 3;
  143. if q<4 then exit;
  144. if m<q then exit;
  145. (*
  146. CHECK THAT THE VALUES OF M AND NCAP7 ARE REASONABLE
  147. IF (NCAP7.LT.8 .OR. M.LT.NCAP7-4) GO TO 420
  148. NCAP = NCAP7 - 7
  149. NCAPM1 = NCAP - 1
  150. NCAP3 = NCAP + 3
  151. IN ORDER TO DEFINE THE FULL B-SPLINE BASIS, AUGMENT THE
  152. PRESCRIBED INTERIOR KNOTS BY KNOTS OF MULTIPLICITY FOUR
  153. AT EACH END OF THE DATA RANGE.
  154. *)
  155. for j:=-1 to 2 do k[j] := xyw[1].x;
  156. for j:=q-1 to q+2 do k[j] := xyw[m].x;
  157. if (k[3]<=xyw[1].x) or (k[q-2]>=xyw[m].x) then exit;
  158. (*
  159. CHECK THAT THE KNOTS ARE ORDERED AND ARE INTERIOR
  160. TO THE DATA INTERVAL.
  161. *)
  162. j := 3; while (k[j]<=k[j+1]) and (j<q-2) do Inc(j);
  163. if j<q-2 then exit;
  164. (*
  165. CHECK THAT THE WEIGHTS ARE STRICTLY POSITIVE.
  166. *)
  167. j := 1;
  168. while (xyw[j].w>0) and (j<m) do Inc(j);
  169. if xyw[j].w<=0 then exit;
  170. (*
  171. CHECK THAT THE DATA ABSCISSAE ARE ORDERED, THEN FORM THE
  172. ARRAY WORK1 FROM THE ARRAY X. THE ARRAY WORK1 CONTAINS
  173. THE
  174. SET OF DISTINCT DATA ABSCISSAE.
  175. *)
  176. GetMem(Work1, m*SizeOf(ArbFloat));
  177. GetMem(Work2, q*4*SizeOf(ArbFloat));
  178. r := 1; work1^[1] := xyw[1].x;
  179. j := 1;
  180. while (j<m) do
  181. begin
  182. Inc(j);
  183. if xyw[j].x>work1^[r]
  184. then begin Inc(r); work1^[r] := xyw[j].x end
  185. else if xyw[j].x<work1^[r] then goto einde;
  186. end;
  187. if r<q then goto einde;
  188. (*
  189. CHECK THE FIRST S AND THE LAST S SCHOENBERG-WHITNEY
  190. CONDITIONS ( S = MIN(NCAP - 1, 4) ).
  191. *)
  192. jmax := Imin(q-4,4);
  193. j := 1;
  194. while (j<=jmax) do
  195. begin
  196. if (work1^[j]>=k[j+2]) or (k[q-j-1]>=work1^[r-j+1]) then goto einde;
  197. Inc(j)
  198. end;
  199. (*
  200. CHECK ALL THE REMAINING SCHOENBERG-WHITNEY CONDITIONS.
  201. *)
  202. Dec(r, 4); i := 4; j := 5;
  203. while j<=q-4 do
  204. begin
  205. K0 := K[j+2]; K4 := K[J-2];
  206. repeat Inc(i) until (Work1^[i]>k4);
  207. if (I>R) or (WORK1^[I]>=K0) then goto einde;
  208. Inc(j)
  209. end;
  210. (*
  211. INITIALISE A BAND TRIANGULAR SYSTEM (I.E. A
  212. MATRIX AND A RIGHT HAND SIDE) TO ZERO. THE
  213. PROCESSING OF EACH DATA POINT IN TURN RESULTS
  214. IN AN UPDATING OF THIS SYSTEM. THE SUBSEQUENT
  215. SOLUTION OF THE RESULTING BAND TRIANGULAR SYSTEM
  216. YIELDS THE COEFFICIENTS OF THE B-SPLINES.
  217. *)
  218. FillChar(Work2^, q*4*SizeOf(ArbFloat), 0);
  219. FillChar(c, q*SizeOf(ArbFloat), 0);
  220. SIGMA := 0; j := 0; jold := 0;
  221. for i:=1 to m do
  222. with xyw[i] do
  223. begin
  224. (*
  225. FOR THE DATA POINT (X(I), Y(I)) DETERMINE AN INTERVAL
  226. K(J + 3) .LE. X .LT. K(J + 4) CONTAINING X(I). (IN THE
  227. CASE J + 4 .EQ. NCAP THE SECOND EQUALITY IS RELAXED TO
  228. INCLUDE
  229. EQUALITY).
  230. *)
  231. while (x>=k[j+2]) and (j<=q-4) do Inc(j);
  232. if j<>jold then
  233. begin
  234. pk := @k[j-1];
  235. with pk^ do
  236. begin
  237. D4 := 1/(K4-K1); D5 := 1/(K5-K2); D6 := 1/(K6-K3);
  238. D7 := 1/(K4-K2); D8 := 1/(K5-K3); D9 := 1/(K4-K3)
  239. end;
  240. JOLD := J;
  241. end;
  242. (*
  243. COMPUTE AND STORE IN WORK1(L) (L = 1, 2, 3, 4) THE VALUES
  244. OF
  245. THE FOUR NORMALIZED CUBIC B-SPLINES WHICH ARE NON-ZERO AT
  246. X=X(I).
  247. *) with pk^ do
  248. begin
  249. E5 := k5 - X;
  250. E4 := K4 - X;
  251. E3 := X - K3;
  252. E2 := X - K2;
  253. N1 := W*D9;
  254. N2 := E3*N1*D8;
  255. N1 := E4*N1*D7;
  256. N3 := E3*N2*D6;
  257. N2 := (E2*N1+E5*N2)*D5;
  258. N1 := E4*N1*D4;
  259. WORK1^[4] := E3*N3;
  260. WORK1^[3] := E2*N2 + (K6-X)*N3;
  261. WORK1^[2] := (X-K1)*N1 + E5*N2;
  262. WORK1^[1] := E4*N1;
  263. CROW := Y*W;
  264. end;
  265. (*
  266. ROTATE THIS ROW INTO THE BAND TRIANGULAR SYSTEM USING PLANE
  267. ROTATIONS.
  268. *)
  269. for lplus1:=1 to 4 do
  270. begin L := LPLUS1 - 1;
  271. RELEMT := WORK1^[LPLUS1];
  272. if relemt<>0 then
  273. begin JPLUSL := J + L;
  274. D := WORK2^[JPLUSL,1];
  275. IF (ABS(RELEMT)>=D)
  276. then DPRIME := ABS(RELEMT)*SQRT(1+sqr(D/RELEMT))
  277. else DPRIME := D*SQRT(1+sqr(RELEMT/D));
  278. WORK2^[JPLUSL,1] := DPRIME;
  279. COSINE := D/DPRIME; SINE := RELEMT/DPRIME;
  280. for iu :=2 to 4-l do
  281. begin
  282. LPLUSU := L + IU;
  283. ACOL := WORK2^[JPLUSL,iu];
  284. AROW := WORK1^[LPLUSU];
  285. WORK2^[JPLUSL,iu] := COSINE*ACOL + SINE*AROW;
  286. WORK1^[LPLUSU] := COSINE*AROW - SINE*ACOL
  287. end;
  288. CCOL := C[JPLUSL];
  289. C[JPLUSL] := COSINE*CCOL + SINE*CROW;
  290. CROW := COSINE*CROW - SINE*CCOL
  291. end;
  292. end;
  293. SIGMA := SIGMA + sqr(CROW)
  294. end;
  295. residu := SIGMA;
  296. (*
  297. SOLVE THE BAND TRIANGULAR SYSTEM FOR THE B-SPLINE
  298. COEFFICIENTS. IF A DIAGONAL ELEMENT IS ZERO, AND HENCE
  299. THE TRIANGULAR SYSTEM IS SINGULAR, THE IMPLICATION IS
  300. THAT THE SCHOENBERG-WHITNEY CONDITIONS ARE ONLY JUST
  301. SATISFIED. THUS IT IS APPROPRIATE TO EXIT IN THIS
  302. CASE WITH THE SAME VALUE (IFAIL=5) OF THE ERROR
  303. INDICATOR.
  304. *)
  305. term := 2;
  306. L := -1;
  307. for jrev:=1 to q do
  308. begin
  309. J := q - JREV + 1; D := WORK2^[J,1];
  310. if d=0 then goto einde;
  311. IF l<3 then L := L + 1;
  312. S := C[j];
  313. for i:=1 to l do
  314. begin
  315. IPLUSJ := I + J;
  316. S := S - WORK2^[j,i+1]*C[IPLUSJ];
  317. end;
  318. C[J] := S/D
  319. end;
  320. term:=1;
  321. einde:
  322. FreeMem(Work2, q*4*SizeOf(ArbFloat));
  323. FreeMem(Work1, m*SizeOf(ArbFloat))
  324. end;
  325. procedure spl2bspf(M, Qx, Qy: ArbInt; var XYZW1: ArbFloat;
  326. var Kxmin1, Kymin1, C11, residu: ArbFloat;
  327. var term: ArbInt);
  328. (* !!!!!!!! Test input !!!!!!!!!! *)
  329. (*
  330. cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
  331. c part 1: determination of the number of knots and their position. c
  332. c **************************************************************** c
  333. c given a set of knots we compute the least-squares spline sinf(x,y), c
  334. c and the corresponding weighted sum of squared residuals fp=f(p=inf). c
  335. c if iopt=-1 sinf(x,y) is the requested approximation. c
  336. c if iopt=0 or iopt=1 we check whether we can accept the knots: c
  337. c if fp <=s we will continue with the current set of knots. c
  338. c if fp > s we will increase the number of knots and compute the c
  339. c corresponding least-squares spline until finally fp<=s. c
  340. c the initial choice of knots depends on the value of s and iopt. c
  341. c if iopt=0 we first compute the least-squares polynomial of degree c
  342. c 3 in x and 3 in y; nx=nminx=2*3+2 and ny=nminy=2*3+2. c
  343. c fp0=f(0) denotes the corresponding weighted sum of squared c
  344. c residuals c
  345. c if iopt=1 we start with the knots found at the last call of the c
  346. c routine, except for the case that s>=fp0; then we can compute c
  347. c the least-squares polynomial directly. c
  348. c eventually the independent variables x and y (and the corresponding c
  349. c parameters) will be switched if this can reduce the bandwidth of the c
  350. c system to be solved. c
  351. cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc *)
  352. function Min(a, b:ArbInt): ArbInt;
  353. begin if a<b then Min := a else Min := b end;
  354. procedure WisselR(var x, y: ArbFloat);
  355. var h: ArbFloat; begin h := x; x := y; y := h end;
  356. procedure Wisseli(var x, y: ArbInt);
  357. var h: ArbInt; begin h := x; x := y; y := h end;
  358. procedure fprota(var cos1, sin1, a, b: ArbFloat);
  359. var store: ArbFloat;
  360. begin
  361. store := b; b := cos1*b+sin1*a; a := cos1*a-sin1*store
  362. end;
  363. procedure fpgivs(var piv, ww, cos1, sin1: ArbFloat);
  364. var store, dd: ArbFloat;
  365. begin
  366. store := abs(piv);
  367. if store>=ww
  368. then dd := store*sqrt(1+sqr(ww/piv))
  369. else dd := ww*sqrt(1+sqr(piv/ww));
  370. cos1 := ww/dd; sin1 := piv/dd; ww := dd
  371. end;
  372. procedure fpback(var a11, z1: ArbFloat; n, k: ArbInt; var c1: ArbFloat);
  373. (*
  374. subroutine fpback calculates the solution of the system of
  375. equations a*c = z with a a n x n upper triangular matrix
  376. of bandwidth k.
  377. ArbFloat a(.,k)
  378. *)
  379. var a: arfloat1 absolute a11;
  380. z: arfloat1 absolute z1;
  381. c: arfloat1 absolute c1;
  382. i, l: ArbInt;
  383. store : ArbFloat;
  384. begin
  385. for i:=n downto 1 do
  386. begin
  387. store := z[i];
  388. for l:=min(n+1-i,k)-1 downto 1 do store := store-c[i+l]*a[(i-1)*k+l+1];
  389. c[i] := store/a[(i-1)*k+1]
  390. end;
  391. end;
  392. procedure fpbspl(var kmin1: ArbFloat; x: ArbFloat; l: ArbInt; var h: r4);
  393. (*
  394. subroutine fpbspl evaluates the 4 non-zero b-splines of
  395. degree 3 at t(l) <= x < t(l+1) using the stable recurrence
  396. relation of de boor and cox.
  397. *)
  398. var k : arfloat_1 absolute kmin1;
  399. f : ArbFloat;
  400. hh: array[1..3] of ArbFloat;
  401. i, j, li, lj : ArbInt;
  402. begin
  403. h[1] := 1;
  404. for j:=1 to 3 do
  405. begin
  406. for i:=1 to j do hh[i] := h[i];
  407. h[1] := 0;
  408. for i:=1 to j do
  409. begin
  410. li := l+i; lj := li-j;
  411. f := hh[i]/(k[li]-k[lj]);
  412. h[i] := h[i]+f*(k[li]-x);
  413. h[i+1] := f*(x-k[lj])
  414. end;
  415. end;
  416. end;
  417. procedure fporde(m, qx, qy: ArbInt; var xyzw1, kxmin1, kymin1: ArbFloat;
  418. var nummer1, index1: ArbInt);
  419. var xi, yi : ArbFloat;
  420. i, im, num,
  421. k, l : ArbInt;
  422. xyzw : r4Ar absolute xyzw1;
  423. kx : arfloat_1 absolute kxmin1;
  424. ky : arfloat_1 absolute kymin1;
  425. nummer : arint1 absolute nummer1;
  426. index : arint1 absolute index1;
  427. begin
  428. for i:=1 to (qx-3)*(qy-3) do index[i] := 0;
  429. for im:=1 to m do
  430. with xyzw[im] do
  431. begin
  432. l := 2; while (x>=kx[l+1]) and (l<qx-2) do Inc(l);
  433. k := 2; while (y>=ky[k+1]) and (k<qy-2) do Inc(k);
  434. num := (l-2)*(qy-3)+k-1;
  435. nummer[im] := index[num]; index[num] := im
  436. end;
  437. end;
  438. label einde;
  439. var x0, x1, y0, y1, eps, cos1, sin1, dmax, sigma,
  440. wi, zi, hxi, piv : ArbFloat;
  441. i, j, l, l1, l2, lx, ly, nreg, ncof, jrot,
  442. inpanel, i1, j1,
  443. iband, num, irot : ArbInt;
  444. xyzw : r4Ar absolute xyzw1;
  445. kx, ky : ^arfloat_1;
  446. a, f, h : ^arfloat1;
  447. c : arfloat1 absolute c11;
  448. nummer, index : ^arint1;
  449. hx, hy : r4;
  450. ichang, fullrank : boolean;
  451. begin
  452. eps := 10*macheps;
  453. (* find the position of the additional knots which are needed for the
  454. b-spline representation of s(x,y) *)
  455. iband := 1+min(3*qy+3,3*qx+3);
  456. if qy>qx then
  457. begin
  458. ichang := true;
  459. kx := @kymin1; ky := @kxmin1;
  460. for i:=1 to m do with xyzw[i] do Wisselr(x, y);
  461. WisselI(qx, qy)
  462. end else
  463. begin
  464. ichang := false;
  465. kx := @kxmin1; ky := @kymin1;
  466. end;
  467. with xyzw[1] do begin x0 := x; x1 := x; y0 := y; y1 := y end;
  468. for i:=2 to m do with xyzw[i] do
  469. begin if x<x0 then x0 := x; if x>x1 then x1 := x;
  470. if y<y0 then y0 := y; if y>y1 then y1 := y
  471. end;
  472. for i:=-1 to 2 do kx^[i] := x0;
  473. for i:=-1 to 2 do ky^[i] := y0;
  474. for i:=qx-1 to qx+2 do kx^[i] := x1;
  475. for i:=qy-1 to qy+2 do ky^[i] := y1;
  476. (* arrange the data points according to the panel they belong to *)
  477. nreg := (qx-3)*(qy-3);
  478. ncof := qx*qy;
  479. GetMem(nummer, m*SizeOf(ArbInt));
  480. GetMem(index, nreg*SizeOf(ArbInt));
  481. GetMem(h, iband*SizeOf(ArbFloat));
  482. GetMem(a, iband*ncof*SizeOf(ArbFloat));
  483. GetMem(f, ncof*SizeOf(ArbFloat));
  484. fporde(m, qx, qy, xyzw1, kx^[-1], ky^[-1], nummer^[1], index^[1]);
  485. for i:=1 to ncof do f^[i] := 0;
  486. for j:=1 to ncof*iband do a^[j] := 0;
  487. residu := 0;
  488. (* fetch the data points in the new order. main loop for the different panels *)
  489. for num:=1 to nreg do
  490. begin
  491. lx := (num-1) div (qy-3); l1 := lx+2;
  492. ly := (num-1) mod (qy-3); l2 := ly+2;
  493. jrot := lx*qy+ly;
  494. inpanel := index^[num];
  495. while inpanel<>0 do
  496. with xyzw[inpanel] do
  497. begin
  498. wi := w; zi := z*wi;
  499. fpbspl(kx^[-1], x, l1, hx);
  500. fpbspl(ky^[-1], y, l2, hy);
  501. for i:=1 to iband do h^[i] := 0;
  502. i1 := 0;
  503. for i:=1 to 4 do
  504. begin
  505. hxi := hx[i]; j1 := i1;
  506. for j:=1 to 4 do begin Inc(j1); h^[j1] := hxi*hy[j]*wi end;
  507. Inc(i1, qy)
  508. end;
  509. irot := jrot;
  510. for i:=1 to iband do
  511. begin
  512. Inc(irot); piv := h^[i];
  513. if piv<>0 then
  514. begin
  515. fpgivs(piv, a^[(irot-1)*iband+1], cos1, sin1);
  516. fprota(cos1, sin1, zi, f^[irot]);
  517. for j:=i+1 to iband do
  518. fprota(cos1, sin1, h^[j], a^[(irot-1)*iband+j-i+1])
  519. end;
  520. end;
  521. residu := residu+sqr(zi);
  522. inpanel := nummer^[inpanel]
  523. end;
  524. end;
  525. dmax := 0;
  526. i := 1;
  527. while i<ncof*iband do
  528. begin
  529. if dmax<a^[i] then dmax:=a^[i]; Inc(i, iband)
  530. end;
  531. sigma := eps*dmax;
  532. i := 1; fullrank := true;
  533. while fullrank and (i<ncof*iband) do
  534. begin
  535. fullrank := a^[i]>sigma; Inc(i, iband)
  536. end;
  537. term := 2; if not fullrank then goto einde;
  538. term := 1;
  539. fpback(a^[1], f^[1], ncof, iband, c11);
  540. if ichang then
  541. begin
  542. l1 := 1;
  543. for i:=1 to qx do
  544. begin
  545. l2 := i;
  546. for j:=1 to qy do
  547. begin
  548. f^[l2] := c[l1]; Inc(l1); Inc(l2, qx)
  549. end;
  550. end;
  551. for i:=1 to ncof do c[i] := f^[i]
  552. end;
  553. einde:
  554. if ichang then for i:=1 to m do with xyzw[i] do Wisselr(x, y);
  555. FreeMem(f, ncof*SizeOf(ArbFloat));
  556. FreeMem(a, iband*ncof*SizeOf(ArbFloat));
  557. FreeMem(h, iband*SizeOf(ArbFloat));
  558. FreeMem(index, nreg*SizeOf(ArbInt));
  559. FreeMem(nummer, m*SizeOf(ArbInt))
  560. end;
  561. procedure spl1nati(n: ArbInt; var xyc1: ArbFloat; var term: ArbInt);
  562. var
  563. xyc : r3Ar absolute XYC1;
  564. l, b, d, u, c : ^arfloat1;
  565. h2, h3, s2, s3: ArbFloat;
  566. i, m : ArbInt; { afmeting van op te lossen stelsel }
  567. begin
  568. term:=3;
  569. if n < 2 then exit;
  570. for i:=2 to n do if xyc[i-1].x>=xyc[i].x then exit;
  571. term:=1;
  572. xyc[1].w := 0; xyc[n].w := 0; { c1=cn=0 }
  573. m := n-2;
  574. if m=0 then exit;
  575. getmem(u, n*SizeOf(ArbFloat));
  576. getmem(l, n*Sizeof(ArbFloat));
  577. getmem(d, n*SizeOf(ArbFloat));
  578. getmem(c, n*SizeOf(ArbFloat));
  579. getmem(b, n*SizeOf(ArbFloat));
  580. h3:=xyc[2].x-xyc[1].x;
  581. s3:=(xyc[2].y-xyc[1].y)/h3;
  582. for i:=2 to n-1 do
  583. begin
  584. h2:=h3; h3:=xyc[i+1].x-xyc[i].x;
  585. s2:=s3; s3:=(xyc[i+1].y-xyc[i].y)/h3;
  586. l^[i]:=h2/6;
  587. d^[i]:=(h2+h3)/3;
  588. u^[i]:=h3/6;
  589. b^[i]:=s3-s2
  590. end;
  591. sledtr(m, l^[3], d^[2], u^[2], b^[2], c^[2], term);
  592. for i:=2 to n-1 do xyc[i].w := c^[i];
  593. Freemem(b, n*SizeOf(ArbFloat));
  594. Freemem(c, n*SizeOf(ArbFloat));
  595. Freemem(d, n*SizeOf(ArbFloat));
  596. Freemem(l, n*Sizeof(ArbFloat));
  597. Freemem(u, n*SizeOf(ArbFloat));
  598. end; {spl1nati}
  599. procedure spl1naki(n: ArbInt; var xyc1: ArbFloat; var term: ArbInt);
  600. var
  601. xyc : r3Ar absolute XYC1;
  602. l, b, d, u, c : ^arfloat1;
  603. h2, h3, s2, s3: ArbFloat;
  604. i, m : ArbInt; { Dimensions of set lin eqs to solve}
  605. begin
  606. term:=3;
  607. if n < 4 then exit;
  608. for i:=2 to n do if xyc[i-1].x>=xyc[i].x then exit;
  609. term:=1;
  610. m := n-2;
  611. getmem(u, n*SizeOf(ArbFloat));
  612. getmem(l, n*Sizeof(ArbFloat));
  613. getmem(d, n*SizeOf(ArbFloat));
  614. getmem(c, n*SizeOf(ArbFloat));
  615. getmem(b, n*SizeOf(ArbFloat));
  616. h3:=xyc[2].x-xyc[1].x;
  617. s3:=(xyc[2].y-xyc[1].y)/h3;
  618. for i:=2 to n-1 do
  619. begin
  620. h2:=h3; h3:=xyc[i+1].x-xyc[i].x;
  621. s2:=s3; s3:=(xyc[i+1].y-xyc[i].y)/h3;
  622. l^[i]:=h2/6;
  623. d^[i]:=(h2+h3)/3;
  624. u^[i]:=h3/6;
  625. b^[i]:=s3-s2
  626. end;
  627. d^[n-1]:=d^[n-1]+h3/6*(1+h3/h2); l^[n-1]:=l^[n-1]-sqr(h3)/(6*h2);
  628. h2:=xyc[2].x-xyc[1].x; h3:=xyc[3].x-xyc[2].x;
  629. d^[2]:=d^[2]+h2/6*(1+h2/h3); u^[2]:=u^[2]-sqr(h2)/(6*h3);
  630. sledtr(m, l^[3], d^[2], u^[2], b^[2], c^[2], term);
  631. for i:=2 to n-1 do xyc[i].w := c^[i];
  632. xyc[1].w := xyc[2].w + (h2/h3)*(xyc[2].w-xyc[3].w);
  633. h2:=xyc[n-1].x-xyc[n-2].x; h3:=xyc[n].x-xyc[n-1].x;
  634. xyc[n].w := xyc[n-1].w + (h3/h2)*(xyc[n-1].w-xyc[n-2].w);
  635. Freemem(b, n*SizeOf(ArbFloat));
  636. Freemem(c, n*SizeOf(ArbFloat));
  637. Freemem(d, n*SizeOf(ArbFloat));
  638. Freemem(l, n*Sizeof(ArbFloat));
  639. Freemem(u, n*SizeOf(ArbFloat));
  640. end; {spl1naki}
  641. procedure spl1cmpi(n: ArbInt; var xyc1: ArbFloat; dy1, dyn: ArbFloat;
  642. var term: ArbInt);
  643. var
  644. xyc : r3Ar absolute XYC1;
  645. l, b, d, u, c : ^arfloat1;
  646. h2, h3, s2, s3: ArbFloat;
  647. i : ArbInt; { Dimensions of set lin eqs to solve}
  648. begin
  649. term:=3;
  650. if n < 2 then exit;
  651. for i:=2 to n do if xyc[i-1].x>=xyc[i].x then exit;
  652. term:=1;
  653. getmem(u, n*SizeOf(ArbFloat));
  654. getmem(l, n*Sizeof(ArbFloat));
  655. getmem(d, n*SizeOf(ArbFloat));
  656. getmem(c, n*SizeOf(ArbFloat));
  657. getmem(b, n*SizeOf(ArbFloat));
  658. h3:=xyc[2].x-xyc[1].x;
  659. s3:=(xyc[2].y-xyc[1].y)/h3;
  660. d^[1] := h3/3; u^[1] := h3/6; b^[1] := -dy1+s3;
  661. for i:=2 to n-1 do
  662. begin
  663. h2:=h3; h3:=xyc[i+1].x-xyc[i].x;
  664. s2:=s3; s3:=(xyc[i+1].y-xyc[i].y)/h3;
  665. l^[i]:=h2/6;
  666. d^[i]:=(h2+h3)/3;
  667. u^[i]:=h3/6;
  668. b^[i]:=s3-s2
  669. end;
  670. d^[n] := h3/3; l^[n] := h3/6; b^[n] := dyn-s3;
  671. sledtr(n, l^[2], d^[1], u^[1], b^[1], c^[1], term);
  672. for i:=1 to n do xyc[i].w := c^[i];
  673. Freemem(b, n*SizeOf(ArbFloat));
  674. Freemem(c, n*SizeOf(ArbFloat));
  675. Freemem(d, n*SizeOf(ArbFloat));
  676. Freemem(l, n*Sizeof(ArbFloat));
  677. Freemem(u, n*SizeOf(ArbFloat));
  678. end; {spl1cmpi}
  679. procedure spl1peri(n: ArbInt; var xyc1: ArbFloat; var term: ArbInt);
  680. var
  681. xyc : r3Ar absolute XYC1;
  682. l, b, d, u, c, k : ^arfloat1;
  683. k2, kn1, dy1, cn,
  684. h2, h3, s2, s3: ArbFloat;
  685. i, m : ArbInt; { Dimensions of set lin eqs to solve}
  686. begin
  687. term:=3;
  688. if n < 2 then exit;
  689. if xyc[1].y<>xyc[n].y then exit;
  690. for i:=2 to n do if xyc[i-1].x>=xyc[i].x then exit;
  691. term:=1;
  692. m := n-2;
  693. xyc[1].w := 0; xyc[n].w := 0; { c1=cn=0 }
  694. if m=0 then exit;
  695. if m=1 then
  696. begin
  697. h2:=xyc[2].x-xyc[1].x;
  698. s2:=(xyc[2].y-xyc[1].y)/h2;
  699. h3:=xyc[3].x-xyc[2].x;
  700. s3:=(xyc[3].y-xyc[2].y)/h3;
  701. xyc[2].w := 6*(s3-s2)/(h2+h3);
  702. xyc[3].w := -xyc[2].w;
  703. xyc[1].w := xyc[3].w;
  704. exit
  705. end;
  706. getmem(u, n*SizeOf(ArbFloat));
  707. getmem(l, n*Sizeof(ArbFloat));
  708. getmem(k, n*SizeOf(ArbFloat));
  709. getmem(d, n*SizeOf(ArbFloat));
  710. getmem(c, n*SizeOf(ArbFloat));
  711. getmem(b, n*SizeOf(ArbFloat));
  712. h3:=xyc[2].x-xyc[1].x;
  713. s3:=(xyc[2].y-xyc[1].y)/h3;
  714. k2 := h3/6; dy1 := s3;
  715. for i:=2 to n-1 do
  716. begin
  717. h2:=h3; h3:=xyc[i+1].x-xyc[i].x;
  718. s2:=s3; s3:=(xyc[i+1].y-xyc[i].y)/h3;
  719. l^[i]:=h2/6;
  720. d^[i]:=(h2+h3)/3;
  721. u^[i]:=h3/6;
  722. b^[i]:=s3-s2;
  723. k^[i]:=0
  724. end;
  725. kn1 := h3/6; k^[2] := k2; k^[n-1] := kn1;
  726. sledtr(m, l^[3], d^[2], u^[2], k^[2], k^[2], term);
  727. sledtr(m, l^[3], d^[2], u^[2], b^[2], c^[2], term);
  728. cn := (dy1-s3-k2*c^[2]-kn1*c^[n-1])/(2*(k2+kn1)-k2*k^[2]-kn1*k^[n-1]);
  729. for i:=2 to n-1 do xyc[i].w := c^[i] - cn*k^[i];
  730. xyc[1].w := cn; xyc[n].w := cn;
  731. Freemem(b, n*SizeOf(ArbFloat));
  732. Freemem(c, n*SizeOf(ArbFloat));
  733. Freemem(d, n*SizeOf(ArbFloat));
  734. Freemem(l, n*Sizeof(ArbFloat));
  735. Freemem(k, n*SizeOf(ArbFloat));
  736. Freemem(u, n*SizeOf(ArbFloat));
  737. end; {spl1peri}
  738. function spl1pprv(n: ArbInt; var xyc1: ArbFloat; t: ArbFloat; var term: ArbInt): ArbFloat;
  739. var
  740. xyc : r3Ar absolute XYC1;
  741. i, j, m : ArbInt;
  742. d, d3, h, dy : ArbFloat;
  743. begin { Assumption : x[i]<x[i+1] i=1..n-1 }
  744. spl1pprv := NaN;
  745. term:=3; if n<2 then exit;
  746. if (t<xyc[1].x) or (t>xyc[n].x) then exit;
  747. term:=1;
  748. i:=1; j:=n;
  749. while j <> i+1 do
  750. begin
  751. m:=(i+j) div 2;
  752. if t>=xyc[m].x then i:=m else j:=m
  753. end; { x[i]<= t <=x[i+1] }
  754. h := xyc[i+1].x-xyc[i].x;
  755. d := t-xyc[i].x;
  756. d3 :=(xyc[i+1].w-xyc[i].w)/h;
  757. dy :=(xyc[i+1].y-xyc[i].y)/h-h*(2*xyc[i].w+xyc[i+1].w)/6;
  758. spl1pprv:= xyc[i].y+d*(dy+d*(xyc[i].w/2+d*d3/6))
  759. end; {spl1pprv}
  760. procedure spl1nalf(n: ArbInt; var xyw1: ArbFloat; lambda:ArbFloat;
  761. var xac1, residu: ArbFloat; var term: ArbInt);
  762. var
  763. xyw : r3Ar absolute xyw1;
  764. xac : r3Ar absolute xac1;
  765. i, j, ncd : ArbInt;
  766. ca, crow : ArbFloat;
  767. h, qty : ^arfloat1;
  768. ch : ^arfloat0;
  769. qtdq : ^arfloat1;
  770. begin
  771. term := 3; { testing input}
  772. if n<2 then exit;
  773. for i:=2 to n do if xyw[i-1].x>=xyw[i].x then exit;
  774. for i:=1 to n do if xyw[i].w<=0 then exit;
  775. if lambda<0 then exit;
  776. term := 1;
  777. Move(xyw, xac, n*SizeOf(r_3));
  778. if n=2 then begin xac[1].w := 0; xac[2].w := 0; exit end;
  779. Getmem(ch, (n+2)*SizeOf(ArbFloat)); FillChar(ch^, (n+2)*SizeOf(ArbFloat), 0);
  780. Getmem(h, n*SizeOf(ArbFloat));
  781. Getmem(qty, n*SizeOf(ArbFloat));
  782. ncd := n-3; if ncd>2 then ncd := 2;
  783. Getmem(qtdq, ((n-2)*(ncd+1)-(ncd*(ncd+1)) div 2)*SizeOf(ArbFloat));
  784. for i:=2 to n do h^[i] := 1/(xyw[i].x-xyw[i-1].x); h^[1] := 0;
  785. for i:=1 to n-2
  786. do qty^[i] := (h^[i+1]*xyw[i].y -
  787. (h^[i+1]+h^[i+2])*xyw[i+1].y +
  788. h^[i+2]*xyw[i+2].y);
  789. j := 1; i := 1;
  790. qtdq^[j] := sqr(h^[i+1])/xyw[i].w +
  791. sqr(h^[i+1]+h^[i+2])/xyw[i+1].w +
  792. sqr(h^[i+2])/xyw[i+2].w +
  793. lambda*(1/h^[i+1]+1/h^[i+2])/3;
  794. Inc(j);
  795. if ncd>0 then
  796. begin i := 2;
  797. qtdq^[j] := -h^[i+1]*(h^[i]+h^[i+1])/xyw[i].w
  798. -h^[i+1]*(h^[i+1]+h^[i+2])/xyw[i+1].w +
  799. lambda/h^[i+1]/6;
  800. Inc(j);
  801. qtdq^[j] := sqr(h^[i+1])/xyw[i].w +
  802. sqr(h^[i+1]+h^[i+2])/xyw[i+1].w +
  803. sqr(h^[i+2])/xyw[i+2].w +
  804. lambda*(1/h^[i+1]+1/h^[i+2])/3;
  805. Inc(j)
  806. end;
  807. for i:=3 to n-2
  808. do begin
  809. qtdq^[j] := h^[i]*h^[i+1]/xyw[i].w;
  810. Inc(j);
  811. qtdq^[j] := -h^[i+1]*(h^[i]+h^[i+1])/xyw[i].w
  812. -h^[i+1]*(h^[i+1]+h^[i+2])/xyw[i+1].w +
  813. lambda/h^[i+1]/6;
  814. Inc(j);
  815. qtdq^[j] := sqr(h^[i+1])/xyw[i].w +
  816. sqr(h^[i+1]+h^[i+2])/xyw[i+1].w +
  817. sqr(h^[i+2])/xyw[i+2].w +
  818. lambda*(1/h^[i+1]+1/h^[i+2])/3;
  819. Inc(j)
  820. end;
  821. { Solving for c/lambda }
  822. Slegpb(n-2, ncd, qtdq^[1], qty^[1], ch^[2], ca, term);
  823. if term=1 then
  824. begin
  825. residu := 0;
  826. for i:=1 to n do
  827. begin
  828. crow := (h^[i]*ch^[i-1] - (h^[i]+h^[i+1])*ch^[i]+h^[i+1]*ch^[i+1])
  829. /xyw[i].w;
  830. xac[i].y := xyw[i].y - crow;
  831. residu := residu + sqr(crow)*xyw[i].w
  832. end;
  833. xac[1].w := 0;
  834. for i:=2 to n-1 do xac[i].w := lambda*ch^[i];
  835. xac[n].w := 0;
  836. end;
  837. Freemem(qtdq, ((n-2)*(ncd+1)-(ncd*(ncd+1)) div 2)*SizeOf(ArbFloat));
  838. Freemem(qty, n*SizeOf(ArbFloat));
  839. Freemem(h, n*SizeOf(ArbFloat));
  840. Freemem(ch, (n+2)*SizeOf(ArbFloat));
  841. end;
  842. procedure spl2nalf(n: ArbInt; var xyzw1: ArbFloat; lambda:ArbFloat;
  843. var xyg0, residu: ArbFloat; var term: ArbInt);
  844. type R3 = array[1..3] of ArbFloat;
  845. R33= array[1..3] of R3;
  846. Rn3= array[1..$ffe0 div SizeOf(R3)] of R3;
  847. var b,e21t,ht :^Rn3;
  848. pfac :par2dr1;
  849. e22 :R33;
  850. i,j,l,i1,i2,n3 :ArbInt;
  851. s,s1,px,py,hr,ca,
  852. x,absdet,x1,x2,
  853. absdetmax :ArbFloat;
  854. vr :R4x;
  855. wr :R2;
  856. w,u :R3;
  857. a_alfa_d :R4xAr absolute xyzw1;
  858. a_gamma :nsp2rec absolute xyg0;
  859. gamma :^arfloat1;
  860. function e(var x,y:R2):ArbFloat;
  861. const c1:ArbFloat=1/(16*pi);
  862. var s:ArbFloat;
  863. begin s:=sqr(x[1]-y[1]) +sqr(x[2]-y[2]);
  864. if s=0 then e:=0 else e:=c1*s*ln(s)
  865. end {e};
  866. procedure pfxpfy(var a,b,c:R2;var f:r3; var pfx,pfy:ArbFloat);
  867. var det:ArbFloat;
  868. begin det:=(b[1]-a[1])*(c[2]-a[2]) - (b[2]-a[2])*(c[1]-a[1]);
  869. pfx:=((f[2]-f[1])*(c[2]-a[2]) - (f[3]-f[1])*(b[2]-a[2]))/det;
  870. pfy:=(-(f[2]-f[1])*(c[1]-a[1]) + (f[3]-f[1])*(b[1]-a[1]))/det
  871. end {pfxpfy};
  872. procedure pxpy(var a,b,c:R2; var px,py:ArbFloat);
  873. var det : ArbFloat;
  874. begin det:=(b[1]-a[1])*(c[2]-a[2]) - (b[2]-a[2])*(c[1]-a[1]);
  875. px:=(b[2]-c[2])/det; py:=(c[1]-b[1])/det
  876. end {pxpy};
  877. function p(var x,a:R2; var px,py:ArbFloat):ArbFloat;
  878. begin p:=1 + (x[1]-a[1])*px +(x[2]-a[2])*py end {p};
  879. procedure slegpdlown(n: ArbInt; var a1; var bx1: ArbFloat;
  880. var term: ArbInt);
  881. var i, j, k, kmin1 : ArbInt;
  882. h, lkk : ArbFloat;
  883. a : ar2dr1 absolute a1;
  884. x : arfloat1 absolute bx1;
  885. begin
  886. k:=0; term := 2;
  887. while (k<n) do
  888. begin
  889. kmin1:=k; k:=k+1; lkk:=a[k]^[k];
  890. for j:=1 to kmin1 do lkk:=lkk-sqr(a[k]^[j]);
  891. if lkk<=0 then exit else
  892. begin
  893. a[k]^[k]:=sqrt(lkk); lkk:=a[k]^[k];
  894. for i:=k+1 to n do
  895. begin
  896. h:=a[i]^[k];
  897. for j:=1 to kmin1 do h:=h-a[k]^[j]*a[i]^[j];
  898. a[i]^[k]:=h/lkk
  899. end; {i}
  900. h:=x[k];
  901. for j:=1 to kmin1 do h:=h-a[k]^[j]*x[j];
  902. x[k]:=h/lkk
  903. end {lkk > 0}
  904. end; {k}
  905. for i:=n downto 1 do
  906. begin
  907. h:=x[i];
  908. for j:=i+1 to n do h:=h-a[j]^[i]*x[j];
  909. x[i]:=h/a[i]^[i];
  910. end; {i}
  911. term := 1
  912. end;
  913. begin
  914. term := 3; if n<3 then exit;
  915. n3 := n - 3;
  916. i1:=1; x1:=a_alfa_d[1].xy[1]; i2:=1; x2:=x1;
  917. for i:= 2 to n do
  918. begin hr:=a_alfa_d[i].xy[1];
  919. if hr < x1 then begin i1:=i; x1:=hr end else
  920. if hr > x2 then begin i2:=i; x2:=hr end;
  921. end;
  922. vr:=a_alfa_d[n-2]; a_alfa_d[n-2]:=a_alfa_d[i1]; a_alfa_d[i1]:=vr;
  923. vr:=a_alfa_d[n-1]; a_alfa_d[n-1]:=a_alfa_d[i2]; a_alfa_d[i2]:=vr;
  924. for i:=1 to 2 do vr.xy[i]:=a_alfa_d[n-2].xy[i]-a_alfa_d[n-1].xy[i];
  925. absdetmax:=-1; i1:=0;
  926. for i:=1 to n do
  927. begin for j:=1 to 2 do wr[j]:=a_alfa_d[i].xy[j]-a_alfa_d[n-2].xy[j];
  928. if a_alfa_d[i].d<=0 then exit;
  929. absdet:=abs(wr[1]*vr.xy[2]-wr[2]*vr.xy[1]);
  930. if absdet > absdetmax then begin i1:=i; absdetmax:=absdet end;
  931. end;
  932. term := 4;
  933. if absdetmax<=macheps*abs(x2-x1) then exit;
  934. term := 1;
  935. vr:=a_alfa_d[n]; a_alfa_d[n]:=a_alfa_d[i1]; a_alfa_d[i1]:=vr;
  936. GetMem(e21t, n3*SizeOf(r3));
  937. GetMem(b, n3*SizeOf(r3));
  938. GetMem(gamma, n*SizeOf(ArbFloat));
  939. pxpy(a_alfa_d[n-2].xy,a_alfa_d[n-1].xy,a_alfa_d[n].xy,px,py);
  940. for i:=1 to n3 do b^[i][1]:=p(a_alfa_d[i].xy,a_alfa_d[n-2].xy,px,py);
  941. pxpy(a_alfa_d[n-1].xy,a_alfa_d[n].xy,a_alfa_d[n-2].xy,px,py);
  942. for i:=1 to n3 do b^[i][2]:=p(a_alfa_d[i].xy,a_alfa_d[n-1].xy,px,py);
  943. pxpy(a_alfa_d[n].xy,a_alfa_d[n-2].xy,a_alfa_d[n-1].xy,px,py);
  944. for i:=1 to n3 do b^[i][3]:=p(a_alfa_d[i].xy,a_alfa_d[n].xy,px,py);
  945. e22[1,1]:=0; e22[2,2]:=0; e22[3,3]:=0;
  946. e22[2,1]:=e(a_alfa_d[n-1].xy,a_alfa_d[n-2].xy); e22[1,2]:=e22[2,1];
  947. e22[3,1]:=e(a_alfa_d[n].xy,a_alfa_d[n-2].xy); e22[1,3]:=e22[3,1];
  948. e22[3,2]:=e(a_alfa_d[n].xy,a_alfa_d[n-1].xy); e22[2,3]:=e22[3,2];
  949. for i:=1 to 3 do
  950. for j:=1 to n3 do e21t^[j,i]:=e(a_alfa_d[n3+i].xy,a_alfa_d[j].xy);
  951. GetMem(ht, n3*SizeOf(r3));
  952. for i:=1 to 3 do
  953. for j:=1 to n3 do
  954. begin s:=0;
  955. for l:= 1 to 3 do s:=s+e22[i,l]*b^[j][l]; ht^[j][i]:=s
  956. end;
  957. AllocateL2dr(n3,pfac);
  958. for i:= 1 to n3 do
  959. for j:= 1 to i do
  960. begin if j=i then s1:=0 else s1:=e(a_alfa_d[i].xy,a_alfa_d[j].xy);
  961. for l:= 1 to 3 do s1:=s1+b^[i][l]*(ht^[j][l]-e21t^[j][l])-e21t^[i][l]*b^[j][l];
  962. if j=i then s:=1/a_alfa_d[i].d else s:=0;
  963. for l:= 1 to 3 do s:=s+b^[i][l]*b^[j][l]/a_alfa_d[n3+l].d;
  964. pfac^[i]^[j] := s1+s/lambda
  965. end;
  966. for i:= 1 to n3 do
  967. gamma^[i]:=a_alfa_d[i].alfa-b^[i][1]*a_alfa_d[n-2].alfa-b^[i][2]*a_alfa_d[n-1].alfa-b^[i][3]*a_alfa_d[n].alfa;
  968. slegpdlown(n3,pfac^[1],gamma^[1],term);
  969. DeAllocateL2dr(n3,pfac);
  970. FreeMem(ht, n3*SizeOf(r3));
  971. if term=1 then
  972. begin
  973. for i:= 1 to 3 do
  974. begin s:= 0;
  975. for j:= 1 to n3 do
  976. s:=s+b^[j][i]*gamma^[j]; w[i]:=s;
  977. gamma^[n3+i]:=-w[i]
  978. end;{w=btgamma}
  979. for i:=1 to 3 do
  980. begin s:=0;
  981. for l:=1 to n3 do s:=s+e21t^[l][i]*gamma^[l];
  982. s1:=0;
  983. for l:=1 to 3 do s1:=s1+e22[i,l]*w[l];
  984. u[i]:=a_alfa_d[n3+i].alfa+w[i]/(lambda*a_alfa_d[n3+i].d)+s1-s
  985. end;
  986. with a_gamma[0] do
  987. pfxpfy(a_alfa_d[n-2].xy,a_alfa_d[n-1].xy,a_alfa_d[n].xy,u,xy[1],xy[2]);
  988. residu:=0;for i:=1 to n3 do residu:=residu+sqr(gamma^[i])/a_alfa_d[i].d;
  989. for i:= 1 to 3 do residu:=residu+sqr(w[i])/a_alfa_d[n3+i].d;
  990. residu:=residu/sqr(lambda);
  991. a_gamma[0].gamma := u[1];
  992. for i:=1 to n do
  993. begin
  994. a_gamma[i].xy := a_alfa_d[i].xy;
  995. a_gamma[i].gamma := gamma^[i]
  996. end;
  997. end;
  998. FreeMem(gamma, n*SizeOf(ArbFloat));
  999. FreeMem(b, n3*SizeOf(r3));
  1000. FreeMem(e21t, n3*SizeOf(r3))
  1001. end;
  1002. function spl2natv(n: ArbInt; var xyg0: ArbFloat; u, v: ArbFloat): ArbFloat;
  1003. const c1: ArbFloat=1/(16*pi);
  1004. var i : ArbInt;
  1005. s : ArbFloat;
  1006. a_gamma : nsp2rec absolute xyg0;
  1007. z : R2;
  1008. function e(var x,y:R2):ArbFloat;
  1009. var s:ArbFloat;
  1010. begin
  1011. s:=sqr(x[1]-y[1]) + sqr(x[2]-y[2]);
  1012. if s=0 then
  1013. e:= 0
  1014. else
  1015. e:=s*ln(s)
  1016. end {e};
  1017. function pf(var x,a:R2;fa,pfx,pfy:ArbFloat):ArbFloat;
  1018. begin
  1019. pf:=fa + (x[1]-a[1])*pfx + (x[2]-a[2])*pfy
  1020. end {pf};
  1021. begin
  1022. s:=0;
  1023. z[1] := u;
  1024. z[2] := v;
  1025. for i:=1 to n do
  1026. s:=s+a_gamma[i].gamma*e(z, a_gamma[i].xy);
  1027. with a_gamma[0] do
  1028. spl2natv :=s*c1+pf(z,a_gamma[n-2].xy, gamma, xy[1], xy[2])
  1029. end;
  1030. begin
  1031. end.