iom.pas 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. Basic In and output of matrix and vector types. Maybe too simple for
  9. your application, but still handy for logging and debugging.
  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 iom;
  17. interface
  18. {$I direct.inc}
  19. uses typ;
  20. const
  21. npos : ArbInt = 78;
  22. {Read a n-dimensional vector v from textfile}
  23. procedure iomrev(var inp: text; var v: ArbFloat; n: ArbInt);
  24. {Read a m x n-dimensional matrix a from textfile}
  25. procedure iomrem(var inp: text; var a: ArbFloat; m, n, rwidth: ArbInt);
  26. {Write a n-dimensional vectorv v to textfile}
  27. procedure iomwrv(var out: text; var v: ArbFloat; n, form: ArbInt);
  28. {Write a m x n-dimensional matrix a to textfile}
  29. procedure iomwrm(var out: text; var a: ArbFloat; m, n, rwidth, form: ArbInt);
  30. {Read a m x n-dimensional matrix a from string}
  31. procedure iomrems(inp: ArbString; var a: ArbFloat; var m, n: ArbInt; c: ArbInt);
  32. {Write a m x n-dimensional matrix a to string}
  33. procedure iomwrms(var out: ArbString; var a: ArbFloat; m, n, form, c: ArbInt);
  34. implementation
  35. procedure iomrev(var inp: text; var v: ArbFloat; n: ArbInt);
  36. var pv : ^arfloat1;
  37. i : ArbInt;
  38. BEGIN
  39. pv:=@v; for i:=1 to n do read(inp, pv^[i])
  40. END {iomrev};
  41. procedure iomrem(var inp: text; var a: ArbFloat; m, n, rwidth: ArbInt);
  42. var pa : ^arfloat1;
  43. i, k : ArbInt;
  44. BEGIN
  45. pa:=@a; k:=1;
  46. for i:=1 to m do
  47. BEGIN
  48. iomrev(inp, pa^[k], n); Inc(k, rwidth)
  49. END
  50. END {iomrem};
  51. procedure iomwrv(var out: text; var v: ArbFloat; n, form: ArbInt);
  52. var pv : arfloat1 absolute v;
  53. i, i1 : ArbInt;
  54. BEGIN
  55. if form>maxform then form:=maxform else
  56. if form<minform then form:=minform;
  57. i1:=npos div (form+2);
  58. for i:=1 to n do
  59. if ((i mod i1)=0) or (i=n) then writeln(out, pv[i]:form)
  60. else write(out, pv[i]:form, '':2)
  61. END {iomwrv};
  62. procedure iomwrm(var out: text; var a: ArbFloat; m, n, rwidth, form: ArbInt);
  63. var pa : ^arfloat1;
  64. i, k, nb, i1, l, j, r, l1, kk : ArbInt;
  65. BEGIN
  66. if (n<1) or (m<1) then exit;
  67. pa:=@a;
  68. if form>maxform then form:=maxform else
  69. if form<minform then form:=minform;
  70. i1:=npos div (form+2); l1:=0;
  71. nb:=n div i1; r:=n mod i1;
  72. if r>0 then Inc(nb);
  73. for l:=1 to nb do
  74. BEGIN
  75. k:=l1+1; if (r>0) and (l=nb) then i1:=r;
  76. for i:=1 to m do
  77. BEGIN
  78. kk:=k;
  79. for j:=1 to i1-1 do
  80. BEGIN
  81. write(out, pa^[kk]:form, '':2); Inc(kk)
  82. END;
  83. writeln(out, pa^[kk]:form); Inc(k, rwidth)
  84. END;
  85. Inc(l1, i1); if l<nb then writeln(out)
  86. END;
  87. END {iomwrm};
  88. procedure iomrems(inp: ArbString; var a: ArbFloat; var m, n: ArbInt; c: ArbInt);
  89. var
  90. pa: ^arfloat1;
  91. i, j, k: ArbInt;
  92. err: ArbInt;
  93. s: ArbString;
  94. ni: ArbInt;
  95. ci: ArbInt;
  96. begin
  97. pa:=@a;
  98. k:=1;
  99. m:=0;
  100. n:=0;
  101. //parse the text
  102. i:= 1;
  103. while i < Length(inp) do
  104. begin
  105. ni := 1;
  106. ci := 1;
  107. //parse row
  108. while not (inp[i] in ['}']) do
  109. begin
  110. //go to beginning of row values
  111. while inp[i] in ['{',' '] do
  112. begin
  113. //increase row counter
  114. if inp[i] = '{' then
  115. Inc(m);
  116. Inc(i);
  117. end;
  118. //get value from string
  119. s := '';
  120. while inp[i] in ['0'..'9','E','e','+','-'] do
  121. begin
  122. s := s + inp[i];
  123. Inc(i);
  124. end;
  125. //assign value to element
  126. val(s, pa^[k], err);
  127. Inc(k);
  128. if err <> 0 then
  129. writeln('Val(',s,') failed at position ', err);
  130. Inc(ci);
  131. end;
  132. k := ((k div c) + 1) * c + 1;
  133. Inc(ni);
  134. if ni > n then n := ni;
  135. Inc(i);
  136. end;
  137. end;
  138. procedure iomwrms(var out: ArbString; var a: ArbFloat; m, n, form, c: ArbInt);
  139. var
  140. pa: ^arfloat1;
  141. i, l, kk: ArbInt;
  142. s: string;
  143. BEGIN
  144. if (n<1) or (m<1) then
  145. exit;
  146. pa:=@a;
  147. if form>maxform then
  148. form:=maxform
  149. else
  150. if form<minform then
  151. form:=minform;
  152. kk := 1;
  153. for l:=1 to m do
  154. BEGIN
  155. out := out + '{';
  156. for i:=1 to n do
  157. BEGIN
  158. str(pa^[kk]:form, s);
  159. Inc(kk);
  160. if i <> n then
  161. out := out + s + ' '
  162. else
  163. out := out + s;
  164. END;
  165. kk := ((kk div c) + 1) * c + 1;
  166. out := out + ' }';
  167. end;
  168. end;
  169. END.