typ.pas 15 KB

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