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