spl.pas 35 KB

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