typ.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. {
  2. $Id$
  3. This file is part of the Numlib package.
  4. Copyright (c) 1986-2000 by
  5. Kees van Ginneken, Wil Kortsmit and Loek van Reij of the
  6. Computational centre of the Eindhoven University of Technology
  7. FPC port Code by Marco van de Voort ([email protected])
  8. documentation by Michael van Canneyt ([email protected])
  9. This is the most basic unit from NumLib.
  10. The most important items this unit defines are matrix types and machine
  11. constants
  12. See the file COPYING.FPC, included in this distribution,
  13. for details about the copyright.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. **********************************************************************}
  18. {
  19. In the FPC revision, instead of picking a certain floating point type,
  20. a new type "ArbFloat" is defined, which is used as floating point type
  21. throughout the entire library. If you change the floating point type, you
  22. should only have to change ArbFloat, and the machineconstants belonging to
  23. the type you want.
  24. However for IEEE Double (64bit) and Extended(80bit) these constants are
  25. already defined, and autoselected by the library. (the library tests the
  26. size of the float type in bytes for 8 and 10 and picks the appropiate
  27. constants
  28. Also some stuff had to be added to get ipf running (vector object and
  29. complex.inp and scale methods)
  30. }
  31. unit typ;
  32. {$I DIRECT.INC} {Contains "global" compilerswitches which
  33. are imported into every unit of the library }
  34. {$DEFINE ArbExtended}
  35. interface
  36. CONST numlib_version=2; {used to detect version conflicts between
  37. header unit and dll}
  38. highestelement=20000; {Maximal n x m dimensions of matrix.
  39. +/- highestelement*SIZEOF(arbfloat) is
  40. minimal size of matrix.}
  41. type {Definition of base types}
  42. {$IFDEF ArbExtended}
  43. ArbFloat = extended;
  44. {$ELSE}
  45. ArbFloat = double;
  46. {$ENDIF}
  47. ArbInt = LONGINT;
  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. var h, r, i: ArbFloat;
  291. procedure Complex.Init(r, i: ArbFloat);
  292. begin
  293. xreal:= r;
  294. imag := i
  295. end;
  296. procedure Complex.Conjugate;
  297. begin
  298. imag := -imag
  299. end;
  300. function Complex.Inp(z:complex):ArbFloat;
  301. begin
  302. Inp := xreal*z.xreal + imag*z.imag
  303. end;
  304. procedure Complex.MinC(c: complex);
  305. begin if c.xreal<xreal then xreal := c.xreal;
  306. if c.imag<imag then imag := c.imag
  307. end;
  308. procedure Complex.Maxc(c: complex);
  309. begin if c.xreal>xreal then xreal := c.xreal;
  310. if c.imag>imag then imag := c.imag
  311. end;
  312. procedure Complex.Add(c: complex);
  313. begin
  314. xreal := xreal + c.xreal; imag := imag + c.imag
  315. end;
  316. procedure Complex.Sub(c: complex);
  317. begin
  318. xreal := xreal - c.xreal; imag := imag - c.imag
  319. end;
  320. Function Complex.Norm: ArbFloat;
  321. begin
  322. Norm := Sqr(xreal) + Sqr(imag)
  323. end;
  324. Function Complex.Size: ArbFloat;
  325. begin
  326. Size := Sqrt(Norm)
  327. end;
  328. Function Complex.Re: ArbFloat;
  329. begin
  330. Re := xreal;
  331. end;
  332. Function Complex.Im: ArbFloat;
  333. begin
  334. Im := imag
  335. end;
  336. Procedure Complex.TransF(var t: complex);
  337. var w: complex;
  338. tt: transformorg absolute t;
  339. begin
  340. w := Self; Conjugate;
  341. with tt do
  342. begin
  343. w.scale(ss);
  344. scale(sc);
  345. Add(offset)
  346. end;
  347. Add(w)
  348. end;
  349. procedure Complex.Unary;
  350. begin
  351. xreal := -xreal;
  352. imag := -imag
  353. end;
  354. procedure Complex.Scale(s:ArbFloat);
  355. begin
  356. xreal := xreal*s; imag := imag*s
  357. end;
  358. Function Complex.Arg: ArbFloat;
  359. begin
  360. if xreal=0 then
  361. if imag>0 then Arg := 0.5*pi else
  362. if imag=0 then Arg := 0 else Arg := -0.5*pi else
  363. if xReal>0 then Arg := ArcTan(imag/xReal)
  364. else if imag>=0 then Arg := ArcTan(imag/xReal) + pi
  365. else Arg := ArcTan(imag/xReal) - pi
  366. end;
  367. Function exp(x: ArbFloat): ArbFloat;
  368. begin
  369. if x<LnMidget then exp := 0 else exp := system.exp(x)
  370. end;
  371. { procedure berekent: v1 = v1 + r*v2 i1 en i2 geven de
  372. increments in bytes voor v1 en v2 }
  373. Function Inprod(var V1, V2; n, i1, i2: ArbInt): ArbFloat;
  374. VAR i: LONGINT;
  375. p1, p2: ^ArbFloat;
  376. s: ArbFloat;
  377. begin
  378. IF I1 <>SIZEOF(ArbFloat) THEN
  379. BEGIN
  380. WRITELN('1 Something went probably wrong while porting!');
  381. HALT;
  382. END;
  383. p1 := @v1; p2 := @v2; s := 0;
  384. for i:=1 to n do
  385. begin
  386. s := s + p1^*p2^;
  387. Inc(longint(p1), i1);
  388. Inc(longint(p2), i2)
  389. end;
  390. Inprod := s
  391. end;
  392. procedure Vector.Init(vii, vjj, vkk: ArbFloat);
  393. begin
  394. i := vii; j := vjj; k := vkk
  395. end;
  396. procedure Vector.Unary;
  397. begin i := -i; j := -j; k := -k end;
  398. procedure Vector.Add(c: vector);
  399. begin
  400. i := i + c.i; j := j + c.j; k := k + c.k
  401. end;
  402. procedure Vector.Sub(c: vector);
  403. begin
  404. i := i - c.i; j := j - c.j; k := k - c.k
  405. end;
  406. function Vector.Vi : ArbFloat; begin Vi := i end;
  407. function Vector.Vj : ArbFloat; begin Vj := j end;
  408. function Vector.Vk : ArbFloat; begin Vk := k end;
  409. function Vector.Norm:ArbFloat;
  410. begin
  411. Norm := Sqr(i) + Sqr(j) + Sqr(k)
  412. end;
  413. function Vector.Norm8:ArbFloat;
  414. var r: ArbFloat;
  415. begin
  416. r := abs(i);
  417. if abs(j)>r then r := abs(j);
  418. if abs(k)>r then r := abs(k);
  419. Norm8 := r
  420. end;
  421. function Vector.Size: ArbFloat;
  422. begin
  423. Size := Sqrt(Norm)
  424. end;
  425. function Vector.InProd(c: vector): ArbFloat;
  426. begin
  427. InProd := i*c.i + j*c.j + k*c.k
  428. end;
  429. procedure Vector.Uitprod(c: vector; var e: vector);
  430. begin
  431. e.i := j*c.k - k*c.j;
  432. e.j := k*c.i - i*c.k;
  433. e.k := i*c.j - j*c.i
  434. end;
  435. procedure Vector.Scale(s: ArbFloat);
  436. begin
  437. i := i*s; j := j*s; k := k*s
  438. end;
  439. procedure Vector.DScale(s: ArbFloat);
  440. begin
  441. i := i/s; j := j/s; k := k/s
  442. end;
  443. procedure Vector.Normalize;
  444. begin
  445. DScale(Size)
  446. end;
  447. procedure Vector.Show(p,q:ArbInt);
  448. begin writeln(i:p:q, 'I', j:p:q, 'J', k:p:q, 'K') end;
  449. procedure Vector.Rotate(calfa, salfa: arbfloat; axe: vector);
  450. var qv : vector;
  451. begin
  452. Uitprod(axe, qv); qv.scale(salfa);
  453. axe.scale((1-calfa)*Inprod(axe));
  454. scale(calfa); sub(qv); add(axe)
  455. end;
  456. function dllversion:LONGINT;
  457. BEGIN
  458. dllversion:=numlib_version;
  459. END;
  460. END.
  461. {
  462. $Log$
  463. Revision 1.2 2002-09-07 15:43:04 peter
  464. * old logs removed and tabs fixed
  465. Revision 1.1 2002/01/29 17:55:19 peter
  466. * splitted to base and extra
  467. Revision 1.2 2002/01/16 14:47:16 florian
  468. + Makefile.fpc added
  469. * several small changes to get things running with FPC 1.0.x
  470. }