home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TURBOPAS / PAS_ENG.ZIP / SOLVGV.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1985-07-19  |  5.3 KB  |  270 lines

  1. program solvgv;  { -> 96 }
  2. { pascal program to perform simultaneous solution by gauss-jordan elimination }
  3. { with multiple constant vectors }
  4.  
  5. const maxr = 7;
  6.  maxc = 7;
  7.  
  8. type ary2s = array[1..maxr,1..maxc] of real;
  9.  
  10. var dummy : char;
  11.  a,y : ary2s;
  12.  n,nvec : integer;
  13.  first,
  14.  error : boolean;
  15.  determ : real;
  16.  
  17.  
  18. procedure get_data(var a: ary2s;
  19.      var y: ary2s;
  20.      var n,nvec: integer);
  21. { get values for n,nvec and arrays a,y }
  22.  
  23. var i,j : integer;
  24.  
  25. begin
  26.   if not first then clrscr else first:=false;
  27.   writeln;
  28.   repeat
  29.     write('How many equations? ');
  30.     readln(n)
  31.   until n<maxr;
  32.   if n>1 then
  33.     begin
  34.       write('How many constant vectors? ');
  35.       readln(nvec);
  36.       for i:=1 to n do
  37.  begin
  38.    for j:=1 to n do
  39.      begin
  40.        write(j:3,': ');
  41.        read(a[i,j]);
  42.        if (j mod n+nvec)=0 then writeln
  43.      end;
  44.         if nvec>0 then
  45.    begin
  46.      for j:=1 to nvec do
  47.        begin
  48.   write('        C:');
  49.   read(y[i,j])
  50.        end;
  51.      readln
  52.    end
  53.      end;  { i-loop }
  54.  
  55.   writeln;
  56.   write('      Matrix');
  57.   if nvec>0 then write('      Constants');
  58.   writeln;
  59.   for i:=1 to n do
  60.     begin
  61.       for j:=1 to n do
  62.  write(a[i,j]:7:4,' ');
  63.       for j:=1 to nvec do
  64.  write(':',y[i,j]:7:4);
  65.       writeln
  66.   end;   { i-loop }
  67.   writeln
  68.   end  { if n>1 }
  69.  end; { procedure get_data }
  70.  
  71. procedure write_data;
  72.  { print out answers }
  73.  
  74. var i,j : integer;
  75.  
  76. begin
  77.   if nvec>0 then
  78.     begin
  79.       writeln('Solution ');
  80.       for i:=1 to n do
  81.  begin
  82.    for j:=1 to nvec do
  83.      write(y[i,j]:9:5);
  84.    writeln
  85.  end
  86.   end { if }
  87.  else
  88.   begin
  89.     writeln('      Inverse');
  90.     for i:=1 to n do
  91.       begin
  92.  for j:=1 to n do
  93.    write(a[i,j]:9:5);
  94.  writeln
  95.       end;
  96.     writeln;
  97.     write('Determinant is ',determ:10:5)
  98.   end;  { else }
  99.  writeln
  100. end; { write_data }
  101.  
  102.  
  103. procedure gausjv
  104.  (var b : ary2s; { square matrix of coefficients }
  105.   var w : ary2s; { constant vector matrix }
  106.   var determ : real;  { the determinant }
  107.   ncol : integer; { order of matrix }
  108.   nv : integer; { number of constants }
  109.  var error : boolean); { true if matrix is singular }
  110.  
  111. { Gauss Jordan matrix inversion and solution }
  112. {  B(n,n) coefficients matrix becomes inverse }
  113. {  W(n,m) constant vector(s) become solution vector }
  114. {  determ is the determinant }
  115. {  error=1 if singular }
  116. {  INDEX(n,3) }
  117. {  NV is the number of vectors }
  118.  
  119. label 99;
  120.  
  121. var
  122.  index : array[1..maxc,1..3] of integer;
  123.  i,j,k,l,
  124.  irow,icol,
  125.  n,l1 : integer;
  126.  pivot,hold,
  127.  sum,ab,
  128.  t,big : real;
  129.  
  130. procedure swap(var a,b: real);
  131. var hold : real;
  132.  
  133. begin
  134.  hold:=a;
  135.  a:=b;
  136.  b:=hold
  137. end; { procedure swap }
  138.  
  139.  
  140. procedure gausj2;
  141.   label 98;
  142.   var i,j,k,l,l1 : integer;
  143.  
  144.  
  145. procedure gausj3;
  146.  
  147. var l : integer;
  148.  
  149. begin  { procedure gausj3 }
  150.  { interchange rows to put pivot on diagonal }
  151.   if irow<>icol then
  152.     begin
  153.       determ:=-determ;
  154.       for l:=1 to n do
  155.  swap(b[irow,l],b[icol,l]);
  156.       if nv>0 then
  157.  for l:=1 to nv do
  158.    swap(w[irow,l],w[icol,l])
  159.    end { if irow<>icol }
  160. end; { gausj3 }
  161.  
  162. begin  { procedure gausj2 }
  163.  { actual start of gaussj }
  164.   error:=false;
  165.   n:=ncol;
  166.   for i:=1 to n do
  167.     index[i,3]:=0;
  168.   determ:=1.0;
  169.   for i:=1 to n do
  170.     begin
  171.     { search for the largest element }
  172.     big:=0.0;
  173.     for j:=1 to n do
  174.       begin
  175.  if index[j,3]<>1 then
  176.    begin
  177.      for k:=1 to n do
  178.       begin
  179.       if index[k,3]>1 then
  180.        begin
  181.   writeln(chr(7),'ERROR: matrix is singular');
  182.   error:=true;
  183.   goto 98  { abort }
  184.        end;
  185.  if index[k,3]<1 then
  186.    if abs(b[j,k])>big then
  187.      begin
  188.        irow:=j;
  189.        icol:=k;
  190.        big:=abs(B[j,k])
  191.       end
  192.     end  { k-loop }
  193.         end  { if }
  194.     end; { j-loop }
  195.   index[icol,3]:=index[icol,3]+1;
  196.   index[i,1]:=irow;
  197.   index[i,2]:=icol;
  198.     gausj3;  { further subdivision of gaussj }
  199.    { divide pivot row by pivot column }
  200.   pivot:=b[icol,icol];
  201.   determ:=determ*pivot;
  202.   b[icol,icol]:=1.0;
  203.   for l:=1 to n do
  204.     b[icol,l]:=b[icol,l]/pivot;
  205.   if nv>0 then
  206.     for l:=1 to nv do
  207.       w[icol,l]:=w[icol,l]/pivot;
  208.   { reduce nonpivot rows }
  209.   for l1:=1 to n do
  210.     begin
  211.       if l1<>icol then
  212.  begin
  213.    t:=b[l1,icol];
  214.    b[l1,icol]:=0;
  215.    for l:=1 to n do
  216.      b[l1,l]:=b[l1,l]-b[icol,l]*t;
  217.    if nv>0 then
  218.      for l:=1 to nv do
  219.        w[l1,l]:=w[l1,l]-w[icol,l]*t
  220.       end { if l1<>icol }
  221.    end  { for l1 }
  222.   end; { i-loop }
  223. 98:
  224. end;  { gausj2 }
  225.  
  226. begin  { GAUS-JORDAN main program }
  227.   gausj2; { first half of gaussj }
  228.   if error then goto 99;
  229.   { interchange columns }
  230.   for i:=1 to n do
  231.     begin
  232.       l:=n-i+1;
  233.       if index[l,1]<>index[l,2] then
  234.  begin
  235.    irow:=index[l,1];
  236.    icol:=index[l,2];
  237.    for k:=1 to n do
  238.      swap(b[k,irow],b[k,icol])
  239.  end { if index }
  240.   end;  { i-loop }
  241. for k:=1 to n do
  242.   if index[k,3]<>1 then
  243.     begin
  244.       writeln(chr(7),'ERROR: matrix is singular');
  245.       error:=true;
  246.       goto 99 { abort }
  247.     end;
  248. 99:
  249. end;  { procedure gaussj }
  250.  
  251.  
  252. begin { main program }
  253.   first:=true;
  254.   clrscr;
  255.   writeln;
  256.   lowvideo;
  257.   writeln('Simultaneous solution by Gauss-Jordan');
  258.   writeln('Multiple constant vectors, or matrix inverse');
  259.   normvideo;
  260.   repeat
  261.     get_data(a,y,n,nvec);
  262.     if n>1 then
  263.       begin
  264.  gausjv(a,y,determ,n,nvec,error);
  265.  if not error then write_data;
  266.  read(dummy)
  267.  end
  268.   until n<2
  269. end.
  270.