home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l042 / 1.ddi / CHAP6.ARC / MATRIX.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1987-12-30  |  18.7 KB  |  294 lines

  1. unit Matrix;
  2.  
  3. {----------------------------------------------------------------------------}
  4. {-                                                                          -}
  5. {-     Turbo Pascal Numerical Methods Toolbox                               -}
  6. {-     Copyright (c) 1986, 87 by Borland International, Inc.                -}
  7. {-                                                                          -}
  8. {-  This unit provides procedures for dealing with systems of linear        -}
  9. {-  equations.                                                              -}
  10. {-                                                                          -}
  11. {----------------------------------------------------------------------------}
  12.  
  13. {$I Float.inc} { Determines the setting of the $N compiler directive }
  14.  
  15. interface
  16.  
  17. {$IFOPT N+}
  18. type
  19.   Float = Double; { 8 byte real, requires 8087 math chip }
  20.  
  21. const
  22.   TNNearlyZero = 1E-015;
  23. {$ELSE}
  24. type
  25.   Float = real;   { 6 byte real, no math chip required }
  26.  
  27. const
  28.   TNNearlyZero = 1E-07;
  29. {$ENDIF}
  30.  
  31.   TNArraySize = 30;               { Size of the matrix }
  32.  
  33. type
  34.   TNvector = array[1..TNArraySize] of Float;
  35.   TNmatrix = array[1..TNArraySize] of TNvector;
  36.  
  37. procedure Determinant(Dimen : integer;
  38.                       Data  : TNmatrix;
  39.                   var Det   : Float;
  40.                   var Error : byte);
  41.  
  42. {----------------------------------------------------------------------------}
  43. {-                                                                          -}
  44. {-     Input: Dimen, Data                                                   -}
  45. {-     Output: Det, Error                                                   -}
  46. {-                                                                          -}
  47. {-             Purpose : Calculate the determinant of a matrix by           -}
  48. {-                       making it upper-triangular and then                -}
  49. {-                       taking the product of the diagonal elements.       -}
  50. {-                                                                          -}
  51. {-  User-defined Types : TNvector = array[1..TNArraySize] of real;          -}
  52. {-                       TNmatrix = array[1..TNArraySize] of TNvector       -}
  53. {-                                                                          -}
  54. {-    Global Variables : Dimen : integer;  Dimension of the square matrix   -}
  55. {-                       Data  : TNmatrix; Square matrix                    -}
  56. {-                       Det   : real;     Determinant of Data              -}
  57. {-                       Error : integer;  Flags if something goes wrong    -}
  58. {-                                                                          -}
  59. {-              Errors : 0: No errors;                                      -}
  60. {-                       1: Dimen < 1                                       -}
  61. {-                                                                          -}
  62. {----------------------------------------------------------------------------}
  63.  
  64. procedure Inverse(Dimen : integer;
  65.                   Data  : TNmatrix;
  66.               var Inv   : TNmatrix;
  67.               var Error : byte);
  68.  
  69. {----------------------------------------------------------------------------}
  70. {-                                                                          -}
  71. {-                Input: Dimen, Data                                        -}
  72. {-               Output: Inv, Error                                         -}
  73. {-                                                                          -}
  74. {-             Purpose : calculate the inverse of a matrix with             -}
  75. {-                       Gauss-Jordan elimination.                          -}
  76. {-                                                                          -}
  77. {-  User-defined Types : TNvector = array[1..TNArraySize] of real;          -}
  78. {-                       TNmatrix = array[1..TNArraySize] of TNvector       -}
  79. {-                                                                          -}
  80. {-    Global Variables : Dimen : integer;   Dimension of the square matrix  -}
  81. {-                       Data  : TNmatrix;  Square matrix                   -}
  82. {-                       Inv   : TNmatrix;  Inverse of Data                 -}
  83. {-                       Error : integer;   Flags if something goes wrong   -}
  84. {-                                                                          -}
  85. {-              Errors : 0: No errors;                                      -}
  86. {-                       1: Dimen < 1                                       -}
  87. {-                       2: no inverse exists                               -}
  88. {-                                                                          -}
  89. {----------------------------------------------------------------------------}
  90.  
  91. procedure Gaussian_Elimination(Dimen        : integer;
  92.                                Coefficients : TNmatrix;
  93.                                Constants    : TNvector;
  94.                            var Solution     : TNvector;
  95.                            var Error        : byte);
  96.  
  97. {----------------------------------------------------------------------------}
  98. {-                                                                          -}
  99. {-                Input: Dimen, Coefficients, Constants                     -}
  100. {-               Output: Solution, Error                                    -}
  101. {-                                                                          -}
  102. {-             Purpose : Calculate the solution of a linear set of          -}
  103. {-                       equations using Gaussian elimination and           -}
  104. {-                       backwards substitution.                            -}
  105. {-                                                                          -}
  106. {-  User-defined Types : TNvector = array[1..TNArraySize] of real           -}
  107. {-                       TNmatrix = array[1..TNArraySize] of TNvector       -}
  108. {-                                                                          -}
  109. {-    Global Variables : Dimen : integer;         Dimension of the square   -}
  110. {-                                                matrix                    -}
  111. {-                       Coefficients : TNmatrix; Square matrix             -}
  112. {-                       Constants    : TNvector; Constants of each equation-}
  113. {-                       Solution     : TNvector; Unique solution to the    -}
  114. {-                                                set of equations          -}
  115. {-                       Error        : integer;  Flags if something goes   -}
  116. {-                                                wrong.                    -}
  117. {-                                                                          -}
  118. {-              Errors:  0: No errors;                                      -}
  119. {-                       1: Dimen < 1                                       -}
  120. {-                       2: no solution exists                              -}
  121. {-                                                                          -}
  122. {-                                                                          -}
  123. {----------------------------------------------------------------------------}
  124.  
  125. procedure Partial_Pivoting(Dimen        : integer;
  126.                            Coefficients : TNmatrix;
  127.                            Constants    : TNvector;
  128.                        var Solution     : TNvector;
  129.                        var Error        : byte);
  130.  
  131. {----------------------------------------------------------------------------}
  132. {-                                                                          -}
  133. {-                Input: Dimen, Coefficients, Constants                     -}
  134. {-               Output: Solution, Error                                    -}
  135. {-                                                                          -}
  136. {-             Purpose : Calculate the solution of a linear set of          -}
  137. {-                       equations using Gaussian elimination, maximal      -}
  138. {-                       pivoting and backwards substitution.               -}
  139. {-                                                                          -}
  140. {-  User-defined Types : TNvector = array[1..TNArraySize] of real;          -}
  141. {-                       TNmatrix = array[1..TNArraySize] of TNvector       -}
  142. {-                                                                          -}
  143. {-    Global Variables : Dimen        : integer;  Dimen of the square       -}
  144. {-                                                matrix                    -}
  145. {-                       Coefficients : TNmatrix; Square matrix             -}
  146. {-                       Constants    : TNvector; Constants of each equation-}
  147. {-                       Solution     : TNvector; Unique solution to the    -}
  148. {-                                                set of equations          -}
  149. {-                       Error        : integer;  Flags if something goes   -}
  150. {-                                                wrong.                    -}
  151. {-                                                                          -}
  152. {-              Errors : 0: No errors;                                      -}
  153. {-                       1: Dimen < 2                                       -}
  154. {-                       2: no solution exists                              -}
  155. {-                                                                          -}
  156. {----------------------------------------------------------------------------}
  157.  
  158. procedure LU_Decompose(Dimen        : integer;
  159.                        Coefficients : TNmatrix;
  160.                    var Decomp       : TNmatrix;
  161.                    var Permute      : TNmatrix;
  162.                    var Error        : byte);
  163.  
  164. {----------------------------------------------------------------------------}
  165. {-                                                                          -}
  166. {-                Input: Dimen, Coefficients                                -}
  167. {-               Output: Decomp, Permute, Error                             -}
  168. {-                                                                          -}
  169. {-             Purpose : Decompose a square matrix into an upper            -}
  170. {-                       triangular and lower triangular matrix such that   -}
  171. {-                       the product of the two triangular matrices is      -}
  172. {-                       the original matrix. This procedure also returns   -}
  173. {-                       a permutation matrix which records the             -}
  174. {-                       permutations resulting from partial pivoting.      -}
  175. {-                                                                          -}
  176. {-  User-defined Types : TNvector = array[1..TNArraySize] of real           -}
  177. {-                       TNmatrix = array[1..TNArraySize] of TNvector       -}
  178. {-                                                                          -}
  179. {-    Global Variables : Dimen        : integer;  Dimen of the coefficients -}
  180. {-                                                Matrix                    -}
  181. {-                       Coefficients : TNmatrix; Coefficients matrix       -}
  182. {-                       Decomp       : TNmatrix; Decomposition of          -}
  183. {-                                                Coefficients matrix       -}
  184. {-                       Permute      : TNmatrix; Record of partial         -}
  185. {-                                                Pivoting                  -}
  186. {-                       Error        : integer;  Flags if something goes   -}
  187. {-                                                wrong.                    -}
  188. {-                                                                          -}
  189. {-              Errors : 0: No errors;                                      -}
  190. {-                       1: Dimen < 1                                       -}
  191. {-                       2: No decomposition possible; singular matrix      -}
  192. {-                                                                          -}
  193. {----------------------------------------------------------------------------}
  194.  
  195. procedure LU_Solve(Dimen     : integer;
  196.                var Decomp    : TNmatrix;
  197.                    Constants : TNvector;
  198.                var Permute   : TNmatrix;
  199.                var Solution  : TNvector;
  200.                var Error     : byte);
  201. {----------------------------------------------------------------------------}
  202. {-                                                                          -}
  203. {-                Input: Dimen, Decomp, Constants, Permute                  -}
  204. {-               Output: Solution, Error                                    -}
  205. {-                                                                          -}
  206. {-             Purpose : Calculate the solution of a linear set of          -}
  207. {-                       equations using an LU decomposed matrix, a         -}
  208. {-                       permutation matrix and backwards and forward       -}
  209. {-                       substitution.                                      -}
  210. {-                                                                          -}
  211. {-  User_defined Types : TNvector = array[1..TNArraySize] of real           -}
  212. {-                       TNmatrix = array[1..TNArraySize] of TNvector       -}
  213. {-                                                                          -}
  214. {-    Global Variables : Dimen     : integer;  Dimen of the square          -}
  215. {-                                             matrix                       -}
  216. {-                       Decomp    : TNmatrix; Decomposition of             -}
  217. {-                                             coefficient matrix           -}
  218. {-                       Constants : TNvector; Constants of each equation   -}
  219. {-                       Permute   : TNmatrix; Permutation matrix from      -}
  220. {-                                             partial pivoting             -}
  221. {-                       Solution  : TNvector; Unique solution to the       -}
  222. {-                                             set of equations             -}
  223. {-                       Error     : integer;  Flags if something goes      -}
  224. {-                                             wrong.                       -}
  225. {-                                                                          -}
  226. {-              Errors : 0: No errors;                                      -}
  227. {-                       1: Dimen < 1                                       -}
  228. {-                                                                          -}
  229. {----------------------------------------------------------------------------}
  230.  
  231. procedure Gauss_Seidel(Dimen       : integer;
  232.                       Coefficients : TNmatrix;
  233.                       Constants    : TNvector;
  234.                       Tol          : Float;
  235.                       MaxIter      : integer;
  236.                   var Solution     : TNvector;
  237.                   var Iter         : integer;
  238.                   var Error        : byte);
  239.  
  240. {----------------------------------------------------------------------------}
  241. {-                                                                          -}
  242. {-                Input: Dimen, Coefficients, Constants, Tol, MaxIter       -}
  243. {-               Output: Solution, Iter, Error                              -}
  244. {-                                                                          -}
  245. {-             Purpose : Calculate the solution of a linear set of          -}
  246. {-                       equations using Gauss - Seidel iteration.          -}
  247. {-                                                                          -}
  248. {-  User-defined Types : TNvector = array[1..TNArraySize] of real           -}
  249. {-                       TNmatrix = array[1..TNArraySize] of TNvector       -}
  250. {-                                                                          -}
  251. {-    Global Variables : Dimen : integer;         Dimen of the square       -}
  252. {-                                                matrix                    -}
  253. {-                       Coefficients : TNmatrix; Square matrix             -}
  254. {-                       Constants    : TNvector; Constants of each equation-}
  255. {-                       Tol          : real;     Tolerance in answer       -}
  256. {-                       MaxIter      : integer;  Maximum number of         -}
  257. {-                                                iterations allowed        -}
  258. {-                       Solution     : TNvector; Unique solution to the    -}
  259. {-                                                set of equations          -}
  260. {-                       Iter         : integer;  Number of iterations      -}
  261. {-                       Error        : integer;  Flags if something goes   -}
  262. {-                                                wrong.                    -}
  263. {-                                                                          -}
  264. {-              Errors : 0: No errors;                                      -}
  265. {-                       1: Iter >= MaxIter and matrix not                  -}
  266. {-                          diagonally dominant                             -}
  267. {-                       2: Iter >= MaxIter                                 -}
  268. {-                       3: Dimen < 1                                       -}
  269. {-                       4: Tol <= 0                                        -}
  270. {-                       5: MaxIter < 0                                     -}
  271. {-                       6: Zero on the diagonal of                         -}
  272. {-                          the Coefficients matrix                         -}
  273. {-                       7: Diverging                                       -}
  274. {-                                                                          -}
  275. {-                Note:  If the Gauss-Seidel iterative method is            -}
  276. {-                       applied to an underdetermined system of equations  -}
  277. {-                       (i.e. one of the equations is a linear             -}
  278. {-                       superposition of the others) it will converge      -}
  279. {-                       to a (non-unique) solution. The Gauss-Seidel       -}
  280. {-                       method is unable to distinguish between unique     -}
  281. {-                       and non-unique solutions.                          -}
  282. {-                       If you are concerned that  your equations          -}
  283. {-                       may be underdetermined, solve them with            -}
  284. {-                       Gaussian elimination (GAUSELIM.INC or              -}
  285. {-                       PARTPIVT.INC                                       -}
  286. {-                                                                          -}
  287. {----------------------------------------------------------------------------}
  288.  
  289. implementation
  290.  
  291. {$I Matrix.inc}
  292.  
  293. end. { Matrix }
  294.