DEFINITION MODULE Mtrx;

CONST
  MAXVECT = 8;

TYPE
 MATRIX = ARRAY[1..MAXVECT,1..MAXVECT] OF REAL;
 VECTOR = ARRAY[1..MAXVECT] OF REAL;

VAR
  Imatrix:MATRIX;
  Ovector:vector;

                  {global subroutines}

PROCEDURE displaymatrix(    xmin,ymin      :byte;
                            rows,cols      :integer;
                        VAR M1             :TNMATRIX;
                        VAR xmax,ymax      :byte);

PROCEDURE displayvector(    xmin,ymin      :byte;
                            rows           :integer;
                        VAR v1             :TNvector;
                        VAR xmax,ymax      :byte);

PROCEDURE multiplymatrix(cols1,rows1,cols2,rows2:integer;
                         M1,M2                  :ARRAY OF REAL;
                         VAR ans                :ARRAY OF REAL;
                         VAR error              :byte );

PROCEDURE transformvector(cols1,rows1           :integer;
                          VAR M1                :TNMATRIX;
                          v1                    :TNvector;
                          VAR ans               :TNvector;
                          VAR error             :byte );

implementation

                  {local declarations}
VAR
  i,j:integer;

                  {subroutine definitions}

PROCEDURE displaymatrix(    xmin,ymin      :byte;
                            rows,cols      :integer;
                        VAR M1             :TNMATRIX;
                        VAR xmax,ymax      :byte);
{----------------------------------------------------------------------------}
{-                                                                          -}
{-     Input: xmin,ymin,rows,cols,M1                                        -}
{-     Output: xmax,ymax                                                    -}
{-                                                                          -}
{-             Purpose : display a matrix with its top left corner at       -}
{-                       (xmin,ymin), and return the position of its        -}
{-                       bottom right corner in xmax and ymax.              -}
{-                                                                          -}
{-  User-defined TYPEs : TNvector = array[1..TNArraySize] of real;          -}
{-                       TNmatrix = array[1..TNArraySize] of TNvector;      -}
{-                                                                          -}
{-        Version Date : 24 May 1988                                        -}
{-                                                                          -}
{----------------------------------------------------------------------------}
                   {declarations}
VAR
  i,j,maxrow:integer;

                   {the PROCEDURE}
begin{displaymatrix }
  xmax:=xmin+3+7*cols;
  ymax:=ymin+rows;
  gotoXY(xmin,ymin);
  writeln(outfile,' _',' ':7*cols,'_ ');
  for i:=1 to rows-1 do
    begin
      gotoXY(xmin,wherey);
      write(outfile,'| ');
      for j:=1 to cols do
        write(outfile,M1[i,j]:7:3);
      writeln(outfile,' |');
    end;
  gotoXY(xmin,wherey);
  write(outfile,'|_');
  for j:=1 to cols do
    write(outfile,M1[rows,j]:7:3);
  writeln(outfile,'_|');
end; {displaymatrix }

PROCEDURE displayvector(    xmin,ymin      :byte;
                            rows           :integer;
                        VAR v1             :TNvector;
                        VAR xmax,ymax      :byte);
{----------------------------------------------------------------------------}
{-                                                                          -}
{-     Input: xmin,ymin,rows,v1                                             -}
{-     Output: xmax,ymax                                                    -}
{-                                                                          -}
{-             Purpose : display a vector with its top left corner at       -}
{-                       (xmin,ymin), and return the position of its        -}
{-                       bottom right corner in xmax and ymax.              -}
{-                                                                          -}
{-  User-defined TYPEs : TNvector = array[1..TNArraySize] of real;          -}
{-                       TNMATRIX = array[1..TNArraySize] of TNvector;      -}
{-                                                                          -}
{-        Version Date : 7 June 1988                                        -}
{-                                                                          -}
{----------------------------------------------------------------------------}
                   {declarations}
VAR
  m1:TNMATRIX;
  i :integer;

                   {the PROCEDURE}
begin{displayvector }
  for i:=1 to rows do
    m1[i,1]:=v1[i];
  displaymatrix(xmin,ymin,rows,1,m1,xmax,ymax);
end; {displayvector }



PROCEDURE multiplymatrix(cols1,rows1,cols2,rows2:integer;
                         M1,M2                  :TNMATRIX;
                         VAR ans                :TNMATRIX;
                         VAR error              :byte );
{----------------------------------------------------------------------------}
{-                                                                          -}
{-     Input: Cols1,rows1,cols2,M1,M2                                       -}
{-     Output: Ans, Error                                                   -}
{-                                                                          -}
{-             Purpose : Calculate the product of two matrixes              -}
{-                                                                          -}
{-  User-defined TYPEs : TNvector = array[1..TNArraySize] of real;          -}
{-                       TNmatrix = array[1..TNArraySize] of TNvector;      -}
{-                                                                          -}
{-              Errors : 0: No errors;                                      -}
{-                       1: cols1<>rows2                                    -}
{-                                                                          -}
{-        Version Date : 24 May 1988                                        -}
{-                                                                          -}
{----------------------------------------------------------------------------}
                   {declarations}
VAR
  i,j,k:integer;
                   {the PROCEDURE}
begin{multiplymatrix }
  if cols1<>rows2 then
    error:=1
  else
    begin
      error:=0;
      for i:=1 to rows1 do
        for j:=1 to cols2 do
          begin
            ans[i,j]:=0;
            for k:=1 to cols1 do
              ans[i,j]:=ans[i,j]+M1[i,k]*M2[k,j];
          end;
    end;
end; {multiplymatrix }


PROCEDURE transformvector(cols1,rows1           :integer;
                          VAR M1                :TNmatrix;
                          v1                    :TNvector;
                          VAR ans               :TNvector;
                          VAR error             :byte );
{----------------------------------------------------------------------------}
{-                                                                          -}
{-     Input: Cols1,rows1,M1,v1                                             -}
{-     Output: Ans, Error                                                   -}
{-                                                                          -}
{-             Purpose : Calculate the product of a matrixe and a vector    -}
{-                                                                          -}
{-  User-defined TYPEs : TNvector = array[1..TNArraySize] of real;          -}
{-                       TNmatrix = array[1..TNArraySize] of TNvector;      -}
{-                                                                          -}
{-              Errors : 0: No errors;                                      -}
{-                                                                          -}
{-        Version Date : 7 June 1988                                        -}
{-                                                                          -}
{----------------------------------------------------------------------------}
                   {declarations}
VAR
  i,j:integer;
                   {the PROCEDURE}
begin{transformvector }
  error:=0;
  for i:=1 to rows1 do
    begin
      ans[i]:=0;
      for j:=1 to cols1 do
          ans[i]:=ans[i]+M1[i,j]*v1[j];
      end;
end; {transformvector }


                  {the initialisation bit}
begin{initialisation}
  for i:=1to TNarraysize do
    begin
      for j:= 1 to TNarraysize do
        Imatrix[i,j]:=0;
      Ovector[i]:=0;
      Imatrix[i,i]:=1;
    end;
end. {initialisation}