spl.pas 35 KB

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