typ.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. This is the most basic unit from NumLib.
  9. The most important items this unit defines are matrix types and machine
  10. constants
  11. See the file COPYING.FPC, included in this distribution,
  12. for details about the copyright.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. **********************************************************************}
  17. {
  18. In the FPC revision, instead of picking a certain floating point type,
  19. a new type "ArbFloat" is defined, which is used as floating point type
  20. throughout the entire library. If you change the floating point type, you
  21. should only have to change ArbFloat, and the machineconstants belonging to
  22. the type you want.
  23. However for IEEE Double (64bit) and Extended(80bit) these constants are
  24. already defined, and autoselected by the library. (the library tests the
  25. size of the float type in bytes for 8 and 10 and picks the appropiate
  26. constants
  27. Also some stuff had to be added to get ipf running (vector object and
  28. complex.inp and scale methods)
  29. }
  30. {$mode objfpc}{$H+}
  31. {$modeswitch nestedprocvars}
  32. unit typ;
  33. {$I DIRECT.INC} {Contains "global" compilerswitches which
  34. are imported into every unit of the library }
  35. {$DEFINE ArbExtended}
  36. interface
  37. uses
  38. Math;
  39. CONST numlib_version=2; {used to detect version conflicts between
  40. header unit and dll}
  41. highestelement=20000; {Maximal n x m dimensions of matrix.
  42. +/- highestelement*SIZEOF(arbfloat) is
  43. minimal size of matrix.}
  44. type {Definition of base types}
  45. {$IFDEF ArbExtended}
  46. ArbFloat = extended;
  47. {$ELSE}
  48. ArbFloat = double;
  49. {$ENDIF}
  50. ArbInt = LONGINT;
  51. ArbString = AnsiString;
  52. Float8Arb =ARRAY[0..7] OF BYTE;
  53. Float10Arb =ARRAY[0..9] OF BYTE;
  54. CONST {Some constants for the variables below, in binary formats.}
  55. {$IFNDEF ArbExtended}
  56. {First for REAL/Double}
  57. TC1 : Float8Arb = ($00,$00,$00,$00,$00,$00,$B0,$3C);
  58. TC2 : Float8Arb = ($FF,$FF,$FF,$FF,$FF,$FF,$EF,$7F);
  59. TC3 : Float8Arb = ($00,$00,$00,$00,$01,$00,$10,$00);
  60. TC5 : Float8Arb = ($EF,$39,$FA,$FE,$42,$2E,$86,$40);
  61. TC6 : Float8Arb = ($D6,$BC,$FA,$BC,$2B,$23,$86,$C0);
  62. {$ENDIF}
  63. {For Extended}
  64. {$IFDEF ArbExtended}
  65. TC1 : Float10Arb = (0,0,$00,$00,$00,$00,0,128,192,63); {Eps}
  66. TC2 : Float10Arb = ($FF,$FF,$FF,$FF,$FF,$FF,$FF,$D6,$FE,127); {9.99188560553925115E+4931}
  67. TC3 : Float10Arb = (1,0,0,0,0,0,0,0,0,0); {3.64519953188247460E-4951}
  68. TC5 : Float10Arb = (18,25,219,91,61,101,113,177,12,64); {1.13563488668777920E+0004}
  69. TC6 : Float10Arb = (108,115,3,170,182,56,27,178,12,192); {-1.13988053843083006E+0004}
  70. {$ENDIF}
  71. { numdig is the number of useful (safe) decimal places of an "ArbFloat"
  72. for display.
  73. minform is the number of decimal places shown by the rtls
  74. write(x:ArbFloat)
  75. maxform is the maximal number of decimal positions
  76. }
  77. numdig = 25;
  78. minform = 10;
  79. maxform = 26;
  80. var
  81. macheps : ArbFloat absolute TC1; { macheps = r - 1, with r
  82. the smallest ArbFloat > 1}
  83. giant : ArbFloat absolute TC2; { the largest ArbFloat}
  84. midget : ArbFloat absolute TC3; { the smallest positive ArbFloat}
  85. LnGiant : ArbFloat absolute TC5; {ln of giant}
  86. LnMidget : ArbFloat absolute TC6; {ln of midget}
  87. {Copied from Det. Needs ArbExtended conditional}
  88. const { og = 8^-maxexp, ogý>=midget,
  89. bg = 8^maxexp, bgý<=giant
  90. midget and giant are defined in typ.pas}
  91. {$IFDEF ArbExtended}
  92. ogx: Float10Arb = (51,158,223,249,51,243,4,181,224,31);
  93. bgx: Float10Arb = (108,119,117,92,70,38,155,234,254,95);
  94. maxexpx : ArbInt = 2740;
  95. {$ELSE}
  96. ogx: Float8Arb= (84, 254, 32, 128, 32, 0, 0, 32);
  97. bgx: Float8Arb= (149, 255, 255, 255, 255, 255, 239, 95);
  98. maxexpx : ArbInt = 170;
  99. {$ENDIF}
  100. var
  101. og : ArbFloat absolute ogx;
  102. bg : ArbFloat absolute bgx;
  103. MaxExp : ArbInt absolute maxexpx;
  104. {Like standard EXP(), but for very small values (near lowest possible
  105. ArbFloat this version returns 0}
  106. Function exp(x: ArbFloat): ArbFloat;
  107. type
  108. Complex = object
  109. { Crude complex record. For me an example of
  110. useless OOP, specially if you have operator overloading
  111. }
  112. xreal, imag : ArbFloat;
  113. procedure Init (r, i: ArbFloat);
  114. procedure Add (c: complex);
  115. procedure Sub (c: complex);
  116. function Inp(z:complex):ArbFloat;
  117. procedure Conjugate;
  118. procedure Scale(s: ArbFloat);
  119. Function Norm : ArbFloat;
  120. Function Size : ArbFloat;
  121. Function Re : ArbFloat;
  122. procedure Unary;
  123. Function Im : ArbFloat;
  124. Function Arg : ArbFloat;
  125. procedure MinC(c: complex);
  126. procedure MaxC(c: complex);
  127. Procedure TransF(var t: complex);
  128. end;
  129. vector = object
  130. i, j, k: ArbFloat;
  131. procedure Init (vii, vjj, vkk: ArbFloat);
  132. procedure Unary;
  133. procedure Add (c: vector);
  134. procedure Sub (c: vector);
  135. function Vi : ArbFloat;
  136. function Vj : ArbFloat;
  137. function Vk : ArbFloat;
  138. function Norm : ArbFloat;
  139. Function Norm8 : ArbFloat;
  140. function Size : ArbFloat;
  141. function InProd(c: vector): ArbFloat;
  142. procedure Uitprod(c: vector; var e: vector);
  143. procedure Scale(s: ArbFloat);
  144. procedure DScale(s: ArbFloat);
  145. procedure Normalize;
  146. procedure Rotate(calfa, salfa: ArbFloat; axe: vector);
  147. procedure Show(p,q: ArbInt);
  148. end;
  149. transformorg = record offset: complex; ss, sc: real end;
  150. transform = record
  151. offsetx, offsety, scalex, scaley: ArbFloat
  152. end;
  153. {Standard Functions used in NumLib}
  154. rfunc1r = Function(x : ArbFloat): ArbFloat;
  155. rfunc1rn = Function(x : ArbFloat): ArbFloat is nested;
  156. rfunc2r = Function(x, y : ArbFloat): ArbFloat;
  157. {Complex version}
  158. rfunc1z = Function(z: complex): ArbFloat;
  159. {Special Functions}
  160. oderk1n = procedure(x: ArbFloat; var y, f: ArbFloat);
  161. roofnrfunc = procedure(var x, fx: ArbFloat; var deff: boolean);
  162. {Definition of matrix types in NumLib. First some vectors.
  163. The high boundery is a maximal number only. Vectors can be smaller, but
  164. not bigger. The difference is the starting number}
  165. arfloat0 = array[0..highestelement] of ArbFloat;
  166. arfloat1 = array[1..highestelement] of ArbFloat;
  167. arfloat2 = array[2..highestelement] of ArbFloat;
  168. arfloat_1 = array[-1..highestelement] of ArbFloat;
  169. {A matrix is an array of floats}
  170. ar2dr = array[0..highestelement] of ^arfloat0;
  171. ar2dr1 = array[1..highestelement] of ^arfloat1;
  172. {Matrices can get big, so we mosttimes allocate them on the heap.}
  173. par2dr1 = ^ar2dr1;
  174. {Integer vectors}
  175. arint0 = array[0..highestelement] of ArbInt;
  176. arint1 = array[1..highestelement] of ArbInt;
  177. {Boolean (true/false) vectors}
  178. arbool1 = array[1..highestelement] of boolean;
  179. {Complex vectors}
  180. arcomp0 = array[0..highestelement] of complex;
  181. arcomp1 = array[1..highestelement] of complex;
  182. arvect0 = array[0..highestelement] of vector;
  183. vectors = array[1..highestelement] of vector;
  184. parcomp = ^arcomp1;
  185. {(de) Allocate mxn matrix to A}
  186. procedure AllocateAr2dr(m, n: integer; var a: par2dr1);
  187. procedure DeAllocateAr2dr(m, n: integer; var a: par2dr1);
  188. {(de) allocate below-left triangle matrix for (de)convolution
  189. (a 3x3 matrix looks like this
  190. x
  191. x x
  192. x x x)
  193. }
  194. procedure AllocateL2dr(n: integer; var a: par2dr1);
  195. procedure DeAllocateL2dr(n: integer; var a: par2dr1);
  196. {Get the Re and Im parts of a complex type}
  197. Function Re(z: complex): ArbFloat;
  198. Function Im(z: complex): ArbFloat;
  199. { Creates a string from a floatingpoint value}
  200. Function R2S(x: ArbFloat; p, q: integer): string;
  201. {Calculate inproduct of V1 and V2, which are vectors with N elements;
  202. I1 and I2 are the SIZEOF the datatypes of V1 and V2
  203. MvdV: Change this to "V1,V2:array of ArbFloat and forget the i1 and i2
  204. parameters?}
  205. Function Inprod(var V1, V2; n, i1, i2: ArbInt): ArbFloat;
  206. {Return certain special machine constants.(macheps=1, Nan=7)}
  207. Function MachCnst(n: ArbInt): ArbFloat;
  208. function dllversion:LONGINT;
  209. implementation
  210. Function MachCnst(n: ArbInt): ArbFloat;
  211. begin
  212. case n of
  213. 1: MachCnst := macheps;
  214. 2: MachCnst := giant;
  215. 3: MachCnst := midget;
  216. 4: MachCnst := infinity;
  217. 5: MachCnst := LnGiant;
  218. 6: MachCnst := LnMidget;
  219. 7: MachCnst := Nan;
  220. end
  221. end;
  222. { Are used in many of the example programs}
  223. Function Re(z: complex): ArbFloat;
  224. begin
  225. Re := z.xreal
  226. end;
  227. Function Im(z: complex): ArbFloat;
  228. begin
  229. Im := z.imag
  230. end;
  231. {Kind of Sysutils.TrimRight and TrimLeft called after eachother}
  232. procedure Compress(var s: string);
  233. var i, j: LONGINT;
  234. begin
  235. j := length(s);
  236. while (j>0) and (s[j]=' ') do dec(j);
  237. i := 1;
  238. while (i<=j) and (s[i]=' ') do Inc(i);
  239. s := copy(s, i, j+1-i)
  240. end;
  241. Function R2S(x: ArbFloat; p, q: integer): string;
  242. var s: string;
  243. i, j, k: integer;
  244. begin
  245. if q=-1 then
  246. begin
  247. Str(x:p, s);
  248. i := Pos('E', s)-1; k := i+1;
  249. j := i+3; while (j<length(s)) and (s[j]='0') do inc(j);
  250. while s[i]='0' do dec(i); if s[i]='.' then dec(i);
  251. if s[j]='0' then s := copy(s,1,i) else
  252. if s[k]='-' then
  253. s := copy(s, 1, i)+'E-'+Copy(s, j, length(s)+1-j)
  254. else
  255. s := copy(s, 1, i)+'E'+Copy(s, j, length(s)+1-j)
  256. end
  257. else
  258. Str(x:p:q, s);
  259. Compress(s);
  260. R2S := s
  261. end;
  262. procedure AllocateAr2dr(m, n: integer; var a: par2dr1);
  263. var i: integer;
  264. begin
  265. GetMem(a, m*SizeOf(pointer));
  266. for i:=1 to m do GetMem(a^[i], n*SizeOf(ArbFloat))
  267. end;
  268. procedure DeAllocateAr2dr(m, n: integer; var a: par2dr1);
  269. var i: integer;
  270. begin
  271. for i:=m downto 1 do FreeMem(a^[i], n*SizeOf(ArbFloat));
  272. FreeMem(a, m*SizeOf(pointer));
  273. a := Nil
  274. end;
  275. procedure AllocateL2dr(n: integer; var a: par2dr1);
  276. var i: integer;
  277. begin
  278. GetMem(a, n*SizeOf(pointer));
  279. for i:=1 to n do GetMem(a^[i], i*SizeOf(ArbFloat))
  280. end;
  281. procedure DeAllocateL2dr(n: integer; var a: par2dr1);
  282. var i: integer;
  283. begin
  284. for i:=n downto 1 do FreeMem(a^[i], i*SizeOf(ArbFloat));
  285. FreeMem(a, n*SizeOf(pointer));
  286. a := Nil
  287. end;
  288. procedure Complex.Init(r, i: ArbFloat);
  289. begin
  290. xreal:= r;
  291. imag := i
  292. end;
  293. procedure Complex.Conjugate;
  294. begin
  295. imag := -imag
  296. end;
  297. function Complex.Inp(z:complex):ArbFloat;
  298. begin
  299. Inp := xreal*z.xreal + imag*z.imag
  300. end;
  301. procedure Complex.MinC(c: complex);
  302. begin if c.xreal<xreal then xreal := c.xreal;
  303. if c.imag<imag then imag := c.imag
  304. end;
  305. procedure Complex.Maxc(c: complex);
  306. begin if c.xreal>xreal then xreal := c.xreal;
  307. if c.imag>imag then imag := c.imag
  308. end;
  309. procedure Complex.Add(c: complex);
  310. begin
  311. xreal := xreal + c.xreal; imag := imag + c.imag
  312. end;
  313. procedure Complex.Sub(c: complex);
  314. begin
  315. xreal := xreal - c.xreal; imag := imag - c.imag
  316. end;
  317. Function Complex.Norm: ArbFloat;
  318. begin
  319. Norm := Sqr(xreal) + Sqr(imag)
  320. end;
  321. Function Complex.Size: ArbFloat;
  322. begin
  323. Size := Sqrt(Norm)
  324. end;
  325. Function Complex.Re: ArbFloat;
  326. begin
  327. Re := xreal;
  328. end;
  329. Function Complex.Im: ArbFloat;
  330. begin
  331. Im := imag
  332. end;
  333. Procedure Complex.TransF(var t: complex);
  334. var w: complex;
  335. tt: transformorg absolute t;
  336. begin
  337. w := Self; Conjugate;
  338. with tt do
  339. begin
  340. w.scale(ss);
  341. scale(sc);
  342. Add(offset)
  343. end;
  344. Add(w)
  345. end;
  346. procedure Complex.Unary;
  347. begin
  348. xreal := -xreal;
  349. imag := -imag
  350. end;
  351. procedure Complex.Scale(s:ArbFloat);
  352. begin
  353. xreal := xreal*s; imag := imag*s
  354. end;
  355. Function Complex.Arg: ArbFloat;
  356. begin
  357. if xreal=0 then
  358. if imag>0 then Arg := 0.5*pi else
  359. if imag=0 then Arg := 0 else Arg := -0.5*pi else
  360. if xReal>0 then Arg := ArcTan(imag/xReal)
  361. else if imag>=0 then Arg := ArcTan(imag/xReal) + pi
  362. else Arg := ArcTan(imag/xReal) - pi
  363. end;
  364. Function exp(x: ArbFloat): ArbFloat;
  365. begin
  366. if x<LnMidget then exp := 0 else exp := system.exp(x)
  367. end;
  368. { procedure berekent: v1 = v1 + r*v2 i1 en i2 geven de
  369. increments in bytes voor v1 en v2 }
  370. Function Inprod(var V1, V2; n, i1, i2: ArbInt): ArbFloat;
  371. VAR i: LONGINT;
  372. p1, p2: ^ArbFloat;
  373. s: ArbFloat;
  374. begin
  375. IF I1 <>SIZEOF(ArbFloat) THEN
  376. BEGIN
  377. WRITELN('1 Something went probably wrong while porting!');
  378. HALT;
  379. END;
  380. p1 := @v1; p2 := @v2; s := 0;
  381. for i:=1 to n do
  382. begin
  383. s := s + p1^*p2^;
  384. Inc(ptrint(p1), i1);
  385. Inc(ptrint(p2), i2)
  386. end;
  387. Inprod := s
  388. end;
  389. procedure Vector.Init(vii, vjj, vkk: ArbFloat);
  390. begin
  391. i := vii; j := vjj; k := vkk
  392. end;
  393. procedure Vector.Unary;
  394. begin i := -i; j := -j; k := -k end;
  395. procedure Vector.Add(c: vector);
  396. begin
  397. i := i + c.i; j := j + c.j; k := k + c.k
  398. end;
  399. procedure Vector.Sub(c: vector);
  400. begin
  401. i := i - c.i; j := j - c.j; k := k - c.k
  402. end;
  403. function Vector.Vi : ArbFloat; begin Vi := i end;
  404. function Vector.Vj : ArbFloat; begin Vj := j end;
  405. function Vector.Vk : ArbFloat; begin Vk := k end;
  406. function Vector.Norm:ArbFloat;
  407. begin
  408. Norm := Sqr(i) + Sqr(j) + Sqr(k)
  409. end;
  410. function Vector.Norm8:ArbFloat;
  411. var r: ArbFloat;
  412. begin
  413. r := abs(i);
  414. if abs(j)>r then r := abs(j);
  415. if abs(k)>r then r := abs(k);
  416. Norm8 := r
  417. end;
  418. function Vector.Size: ArbFloat;
  419. begin
  420. Size := Sqrt(Norm)
  421. end;
  422. function Vector.InProd(c: vector): ArbFloat;
  423. begin
  424. InProd := i*c.i + j*c.j + k*c.k
  425. end;
  426. procedure Vector.Uitprod(c: vector; var e: vector);
  427. begin
  428. e.i := j*c.k - k*c.j;
  429. e.j := k*c.i - i*c.k;
  430. e.k := i*c.j - j*c.i
  431. end;
  432. procedure Vector.Scale(s: ArbFloat);
  433. begin
  434. i := i*s; j := j*s; k := k*s
  435. end;
  436. procedure Vector.DScale(s: ArbFloat);
  437. begin
  438. i := i/s; j := j/s; k := k/s
  439. end;
  440. procedure Vector.Normalize;
  441. begin
  442. DScale(Size)
  443. end;
  444. procedure Vector.Show(p,q:ArbInt);
  445. begin writeln(i:p:q, 'I', j:p:q, 'J', k:p:q, 'K') end;
  446. procedure Vector.Rotate(calfa, salfa: arbfloat; axe: vector);
  447. var qv : vector;
  448. begin
  449. Uitprod(axe, qv); qv.scale(salfa);
  450. axe.scale((1-calfa)*Inprod(axe));
  451. scale(calfa); sub(qv); add(axe)
  452. end;
  453. function dllversion:LONGINT;
  454. BEGIN
  455. dllversion:=numlib_version;
  456. END;
  457. END.