inv.tex 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. %
  2. % part of numlib docs. In time this won't be a standalone pdf anymore, but part of a larger part.
  3. % for now I keep it directly compliable. Uses fpc.sty from fpc-docs pkg.
  4. %
  5. \documentclass{report}
  6. \usepackage{fpc}
  7. \lstset{%
  8. basicstyle=\small,
  9. language=delphi,
  10. commentstyle=\itshape,
  11. keywordstyle=\bfseries,
  12. showstringspaces=false,
  13. frame=
  14. }
  15. \makeindex
  16. \newcommand{\FunctionDescription}{\item[Description]\rmfamily}
  17. \newcommand{\Dataorganisation}{\item[Data Struct]\rmfamily}
  18. \newcommand{\DeclarationandParams}{\item[Declaration]\rmfamily}
  19. \newcommand{\References}{\item[References]\rmfamily}
  20. \newcommand{\Method}{\item[Method]\rmfamily}
  21. \newcommand{\Precision}{\item[Precision]\rmfamily}
  22. \newcommand{\Remarks}{\item[Remarks]\rmfamily}
  23. \newcommand{\Example}{\item[Example]\rmfamily}
  24. \newcommand{\ProgramData}{\item[Example Data]\rmfamily}
  25. \newcommand{\ProgramResults}{\item[Example Result]\rmfamily}
  26. \begin{document}
  27. \section{Unit inv}
  28. \textbf{Original comments:} \\
  29. The used floating point type \textbf{real} depends on the used version,
  30. see the general introduction for more information. You'll need to USE
  31. units typ an inv to use these routines.
  32. \textbf{MvdV notes:} \\
  33. Integers used for parameters are of type "ArbInt" to avoid problems with
  34. systems that define integer differently depending on mode.
  35. Floating point values are of type "ArbFloat" to allow writing code that is
  36. independant of the exact real type. (Contrary to directly specifying single,
  37. real, double or extended in library and examples). Typ.pas and the central
  38. includefile have some conditional code to switch between floating point
  39. types.
  40. These changes were already prepared somewhat when I got the lib, but weren't
  41. consequently applied. I did that while porting to FPC.
  42. \begin{procedure}{invgen}
  43. \FunctionDescription
  44. Procedure to calculate the inverse of a matrix.
  45. \Dataorganisation
  46. The procedure assumes that the calling program has declared a two dimensional
  47. matrix containing the maxtrixelements in a square partial block.
  48. \DeclarationandParams
  49. \lstinline|procedure invgen(n, rwidth: ArbInt; var ai: ArbFloat;var term: ArbInt);|
  50. \begin{description}
  51. \item[n: integer] \mbox{ } \\
  52. The parameter {\bf n} describes the size of the matrix
  53. \item[rwidth: integer] \mbox{} \\
  54. The parameter {\bf rwidth} describes the declared rowlength of the twodimensional
  55. matrix.
  56. \item[var ai: real] \mbox{} \\
  57. The parameter {\bf ai} must contain to the twodimensional arrayelement
  58. that is top-left in the matrix.
  59. After the function has ended succesfully (\textbf{term=1}) then
  60. the input matrix has been changed into its inverse, otherwise the contents
  61. of the input matrix are undefined.
  62. ongedefinieerd.
  63. \item[var term: integer] \mbox{} \\
  64. After the procedure has run, this variable contains the reason for
  65. the termination of the procedure:\\
  66. {\bf term}=1, succesfull termination, the inverse has been calculated
  67. {\bf term}=2, inverse matrix could not be calculated because the matrix
  68. is (very close to) being singular.
  69. {\bf term}=3, wrong input n$<$1
  70. \end{description}
  71. \Remarks
  72. This procedure does not do array range checks. When called with invalid
  73. parameters, invalid/nonsense responses or even crashes may be the result.
  74. \Example
  75. Calculate the inverse of
  76. \[
  77. A=
  78. \left(
  79. \begin{array}{rrrr}
  80. 4 & 2 & 4 & 1 \\
  81. 30 & 20 & 45 & 12 \\
  82. 20 & 15 & 36 & 10 \\
  83. 35 & 28 & 70 & 20
  84. \end{array}
  85. \right)
  86. .
  87. \]
  88. Below is the source of the invgenex demo that demontrates invgenex, some
  89. routines of iom were used to construct matrices.
  90. \begin{lstlisting}
  91. program invgenex;
  92. uses typ, iom, inv;
  93. const n = 4;
  94. var term : ArbInt;
  95. A : array[1..n,1..n] of ArbFloat;
  96. begin
  97. assign(input, paramstr(1)); reset(input);
  98. assign(output, paramstr(2)); rewrite(output);
  99. writeln('program results invgenex');
  100. { Read matrix A }
  101. iomrem(input, A[1,1], n, n, n);
  102. { Print matrix A }
  103. writeln; writeln('A =');
  104. iomwrm(output, A[1,1], n, n, n, numdig);
  105. { Calculation of the inverse of A }
  106. invgen(n, n, A[1,1], term);
  107. writeln; writeln('term=', term:2);
  108. if term=1 then
  109. { print inverse inverse of A }
  110. begin
  111. writeln; writeln('inverse of A =');
  112. iomwrm(output, A[1,1], n, n, n, numdig);
  113. end; {term=1}
  114. close(input); close(output)
  115. end.
  116. \end{lstlisting}
  117. \ProgramData
  118. The input datafile looks like:
  119. \begin{verbatim}
  120. 4 2 4 1
  121. 30 20 45 12
  122. 20 15 36 10
  123. 35 28 70 20
  124. \end{verbatim}
  125. \ProgramResults
  126. \begin{verbatim}
  127. program results invgenex
  128. A =
  129. 4.0000000000000000E+0000 2.0000000000000000E+0000
  130. 3.0000000000000000E+0001 2.0000000000000000E+0001
  131. 2.0000000000000000E+0001 1.5000000000000000E+0001
  132. 3.5000000000000000E+0001 2.8000000000000000E+0001
  133. 4.0000000000000000E+0000 1.0000000000000000E+0000
  134. 4.5000000000000000E+0001 1.2000000000000000E+0001
  135. 3.6000000000000000E+0001 1.0000000000000000E+0001
  136. 7.0000000000000000E+0001 2.0000000000000000E+0001
  137. term= 1
  138. inverse of A =
  139. 4.0000000000000000E+0000 -2.0000000000000000E+0000
  140. -3.0000000000000000E+0001 2.0000000000000000E+0001
  141. 2.0000000000000000E+0001 -1.5000000000000000E+0001
  142. -3.4999999999999999E+0001 2.7999999999999999E+0001
  143. 3.9999999999999999E+0000 -1.0000000000000000E+0000
  144. -4.4999999999999999E+0001 1.2000000000000000E+0001
  145. 3.5999999999999999E+0001 -1.0000000000000000E+0001
  146. -6.9999999999999999E+0001 2.0000000000000000E+0001
  147. \end{verbatim}
  148. \Precision
  149. The procedure doesn't supply information about the precision of the
  150. operation after termination.
  151. \Method
  152. The calculation of the inverse is based on LU decomposition with partial
  153. pivoting.
  154. \References
  155. Wilkinson, J.H. and Reinsch, C.\\
  156. Handbook for Automatic Computation.\\
  157. Volume II, Linear Algebra.\\
  158. Springer Verlag, Contribution I/7, 1971.
  159. \end{procedure}
  160. \begin{procedure}{invgpd}
  161. \FunctionDescription
  162. Procedure to calculate the inverse of a symmetrical, postivive definite
  163. %van een symmetrische,positief definiete matrix.
  164. \Dataorganisation
  165. The procedure assumes that the calling program has declared a two dimensional
  166. matrix containing the maxtrixelements in a square partial block.
  167. \DeclarationandParams
  168. \lstinline|procedure invgpd(n, rwidth: ArbInt; var ai: ArbFloat; var term: ArbInt);|
  169. \begin{description}
  170. \item[n: integer] \mbox{ } \\
  171. The parameter {\bf n} describes the size of the matrix
  172. \item[rwidth: integer] \mbox{} \\
  173. The parameter {\bf rwidth} describes the declared rowlength of the twodimensional
  174. matrix.
  175. \item[var ai: real] \mbox{} \\
  176. The parameter {\bf ai} must contain to the twodimensional arrayelement
  177. that is top-left in the matrix.
  178. After the function has ended succesfully (\textbf{term=1}) then
  179. the input matrix has been changed into its inverse, otherwise the contents
  180. of the input matrix are undefined.
  181. ongedefinieerd.
  182. \item[var term: integer] \mbox{} \\
  183. After the procedure has run, this variable contains the reason for
  184. the termination of the procedure:\\
  185. {\bf term}=1, succesfull termination, the inverse has been calculated
  186. {\bf term}=2, inverse matrix could not be calculated because the matrix
  187. is (very close to) being singular.
  188. {\bf term}=3, wrong input n$<$1
  189. \end{description}
  190. \Remarks
  191. \begin{itemize}
  192. \item Only the bottomleft part of the matrix $A$ needs to be passed.
  193. \item \textbf{Warning} This procedure does not do array range checks. When called with invalid
  194. parameters, invalid/nonsense responses or even crashes may be the result.
  195. \end{itemize}
  196. \Example
  197. Calculate the inverse of
  198. \[
  199. A=
  200. \left(
  201. \begin{array}{rrrr}
  202. 5 & 7 & 6 & 5 \\
  203. 7 & 10 & 8 & 7 \\
  204. 6 & 8 & 10 & 9 \\
  205. 5 & 7 & 9 & 10
  206. \end{array}
  207. \right)
  208. .
  209. \]
  210. \begin{lstlisting}
  211. program invgpdex;
  212. uses typ, iom, inv;
  213. const n = 4;
  214. var i, j, term : ArbInt;
  215. A : array[1..n,1..n] of ArbFloat;
  216. begin
  217. assign(input, paramstr(1)); reset(input);
  218. assign(output, paramstr(2)); rewrite(output);
  219. writeln('program results invgpdex');
  220. { Read bottom leftpart of matrix A}
  221. for i:=1 to n do iomrev(input, A[i,1], i);
  222. { print matrix A }
  223. writeln; writeln('A =');
  224. for i:=1 to n do for j:=1 to i-1 do A[j,i]:=A[i,j];
  225. iomwrm(output, A[1,1], n, n, n, numdig);
  226. { Calculate inverse of matrix A}
  227. invgpd(n, n, A[1,1], term);
  228. writeln; writeln('term=', term:2);
  229. if term=1 then
  230. { Print inverse of matrix A}
  231. begin
  232. writeln; writeln('inverse of A =');
  233. iomwrm(output, A[1,1], n, n, n, numdig);
  234. end; {term=1}
  235. close(input); close(output)
  236. end.
  237. \end{lstlisting}
  238. \ProgramData
  239. \begin{verbatim}
  240. 5
  241. 7 10
  242. 6 8 10
  243. 5 7 9 10
  244. \end{verbatim}
  245. \ProgramResults
  246. \begin{verbatim}
  247. program results invgpdex
  248. A =
  249. 5.0000000000000000E+0000 7.0000000000000000E+0000
  250. 7.0000000000000000E+0000 1.0000000000000000E+0001
  251. 6.0000000000000000E+0000 8.0000000000000000E+0000
  252. 5.0000000000000000E+0000 7.0000000000000000E+0000
  253. 6.0000000000000000E+0000 5.0000000000000000E+0000
  254. 8.0000000000000000E+0000 7.0000000000000000E+0000
  255. 1.0000000000000000E+0001 9.0000000000000000E+0000
  256. 9.0000000000000000E+0000 1.0000000000000000E+0001
  257. term= 1
  258. inverse of A =
  259. 6.8000000000000000E+0001 -4.1000000000000000E+0001
  260. -4.1000000000000000E+0001 2.5000000000000000E+0001
  261. -1.7000000000000000E+0001 1.0000000000000000E+0001
  262. 1.0000000000000000E+0001 -6.0000000000000000E+0000
  263. -1.7000000000000000E+0001 1.0000000000000000E+0001
  264. 1.0000000000000000E+0001 -6.0000000000000000E+0000
  265. 5.0000000000000000E+0000 -3.0000000000000000E+0000
  266. -3.0000000000000000E+0000 2.0000000000000000E+0000
  267. \end{verbatim}
  268. \Precision
  269. The procedure doesn't supply information about the precision of the
  270. operation after termination.
  271. \Method
  272. The calculation of the inverse is based on Cholesky decomposition for a
  273. symmetrical positive definitie matrix.
  274. \References
  275. Wilkinson, J.H. and Reinsch, C.\\ Handbook for Automatic Computation.\\
  276. Volume II, Linear Algebra.\\ Springer Verlag, Contribution I/7, 1971.
  277. \end{procedure}
  278. \begin{procedure}{invgsy}
  279. \FunctionDescription
  280. Procedure to calculate the inverse of a symmetrical matrix.
  281. \Dataorganisation
  282. The procedure assumes that the calling program has declared a two
  283. dimensional matrix containing the maxtrixelements in (the bottomleft part
  284. of) a square partial block.
  285. \DeclarationandParams
  286. \lstinline|procedure invgsy(n, rwidth: ArbInt; var ai: ArbFloat;var term:ArbInt);|
  287. \begin{description}
  288. \item[n: integer] \mbox{ } \\
  289. The parameter {\bf n} describes the size of the matrix
  290. \item[rwidth: integer] \mbox{} \\
  291. The parameter {\bf rwidth} describes the declared rowlength of the twodimensional
  292. matrix.
  293. \item[var ai: real] \mbox{} \\
  294. The parameter {\bf ai} must contain to the twodimensional arrayelement
  295. that is top-left in the matrix.
  296. After the function has ended succesfully (\textbf{term=1}) then
  297. the input matrix has been changed into its inverse, otherwise the contents
  298. of the input matrix are undefined.
  299. ongedefinieerd.
  300. \item[var term: integer] \mbox{} \\
  301. After the procedure has run, this variable contains the reason for
  302. the termination of the procedure:\\
  303. {\bf term}=1, succesfull termination, the inverse has been calculated
  304. {\bf term}=2, inverse matrix could not be calculated because the matrix
  305. is (very close to) being singular.
  306. {\bf term}=3, wrong input n$<$1
  307. \end{description}
  308. \Remarks
  309. \begin{itemize}
  310. \item Only the bottomleft part of the matrix $A$ needs to be passed.
  311. \item \textbf{Warning} This procedure does not do array range checks. When called with invalid
  312. parameters, invalid/nonsense responses or even crashes may be the result.
  313. \end{itemize}
  314. \Example
  315. Het berekenen van de inverse van
  316. \[
  317. A=
  318. \left(
  319. \begin{array}{rrrr}
  320. 5 & 7 & 6 & 5 \\
  321. 7 & 10 & 8 & 7 \\
  322. 6 & 8 & 10 & 9 \\
  323. 5 & 7 & 9 & 10
  324. \end{array}
  325. \right)
  326. .
  327. \]
  328. \begin{lstlisting}
  329. program invgsyex;
  330. uses typ, iom, inv;
  331. const n = 4;
  332. var i, j, term : ArbInt;
  333. A : array[1..n,1..n] of ArbFloat;
  334. begin
  335. assign(input, paramstr(1)); reset(input);
  336. assign(output, paramstr(2)); rewrite(output);
  337. writeln('program results invgsyex');
  338. { Read bottomleft part of matrix A}
  339. for i:=1 to n do iomrev(input, A[i,1], i);
  340. { print matrix A}
  341. writeln; writeln('A =');
  342. for i:=1 to n do for j:=1 to i-1 do A[j,i]:=A[i,j];
  343. iomwrm(output, A[1,1], n, n, n, numdig);
  344. { calculate inverse of matrix A}
  345. invgsy(n, n, A[1,1], term);
  346. writeln; writeln('term=', term:2);
  347. if term=1 then
  348. { print inverse of matrix A}
  349. begin
  350. writeln; writeln('inverse of A =');
  351. iomwrm(output, A[1,1], n, n, n, numdig);
  352. end; {term=1}
  353. close(input); close(output)
  354. end.
  355. \end{lstlisting}
  356. \ProgramData
  357. \begin{verbatim}
  358. 5
  359. 7 10
  360. 6 8 10
  361. 5 7 9 10
  362. \end{verbatim}
  363. \ProgramResults
  364. \begin{verbatim}
  365. program results invgsyex
  366. A =
  367. 5.0000000000000000E+0000 7.0000000000000000E+0000
  368. 7.0000000000000000E+0000 1.0000000000000000E+0001
  369. 6.0000000000000000E+0000 8.0000000000000000E+0000
  370. 5.0000000000000000E+0000 7.0000000000000000E+0000
  371. 6.0000000000000000E+0000 5.0000000000000000E+0000
  372. 8.0000000000000000E+0000 7.0000000000000000E+0000
  373. 1.0000000000000000E+0001 9.0000000000000000E+0000
  374. 9.0000000000000000E+0000 1.0000000000000000E+0001
  375. term= 1
  376. inverse of A =
  377. 6.8000000000000001E+0001 -4.1000000000000001E+0001
  378. -4.1000000000000001E+0001 2.5000000000000000E+0001
  379. -1.7000000000000000E+0001 1.0000000000000000E+0001
  380. 1.0000000000000000E+0001 -6.0000000000000001E+0000
  381. -1.7000000000000000E+0001 1.0000000000000000E+0001
  382. 1.0000000000000000E+0001 -6.0000000000000001E+0000
  383. 5.0000000000000001E+0000 -3.0000000000000000E+0000
  384. -3.0000000000000000E+0000 2.0000000000000000E+0000
  385. \end{verbatim}
  386. \Precision
  387. The procedure doesn't supply information about the precision of the
  388. operation after termination.
  389. \Method
  390. The calculation of the inverse is based on reduction of a symmetrical
  391. matrix to a tridiagonal form.
  392. \References
  393. Aasen, J. O. \\
  394. On the reduction of a symmetric matrix to tridiagonal form. \\
  395. BIT, 11, (1971), pag. 223-242.
  396. \end{procedure}
  397. \end{document}