home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 15 / CDACTUAL15.iso / cdactual / program / pascal / SPARSE.ZIP / SPARSE.LIB next >
Encoding:
Text File  |  1986-03-27  |  4.3 KB  |  188 lines

  1. { Sparse Matrix Library }
  2. { David Myers           }
  3. { Biochemistry Dept     }
  4. { Rice University       }
  5. { Houston TX 77251      }
  6.  
  7. { this code requires that you define the following structures and types }
  8. { this code is probably useless if you have a matrix smaller than }
  9. { 100 by 100 }
  10. { STORAGE CONSUMED: }
  11. { 20 bytes per Node, 4kb per 100 elements in an empty square array }
  12. { The storage consumed for a 400 x 400 tridiagonal array =
  13.  
  14.                20 x (399 + 399 + 400) = 23960 bytes
  15.                            20 x 801   = 16020 bytes
  16.                            ------------------------
  17.                                       = 39980 bytes               }
  18.  
  19.   CONST
  20.     Maxcolms = 300;
  21.     Maxrows  = 300;
  22.  
  23.   TYPE
  24.     rownum = 1 .. Maxrows;
  25.     colnum = 1 .. Maxcolms;
  26.     NodePtr = ^NodeType;
  27.     NodeType = RECORD
  28.       row : rownum;
  29.       col : colnum;
  30.       val : real;
  31.       nextrow : NodePtr;
  32.       nextcol : NodePtr;
  33.     end;
  34.  
  35. { conventions : A is the pointer to the sparse matrix }
  36.  
  37. function FindRowPos(A : NodePtr; r: rownum;c : colnum) : NodePtr;
  38.  
  39. { this function returns a pointer to a node in row r immediately
  40.   preceeding the position of interest, if it exists              }
  41.  
  42. VAR
  43.   P,Q,Z : NodePtr;
  44.  
  45. BEGIN
  46.   P := a;
  47.   While (p^.row < r) do
  48.     p := p^.nextrow;
  49.   Q := p;
  50.   z := p^.nextCOL;
  51.   While (p^.COL < c) and (z^.COL <> 0) do
  52.     BEGIN
  53.       q := p;
  54.       p := p^.nextCOL;
  55.       z := p^.nextCOL;
  56.     END;
  57.   If ( p^.COL >= c )
  58.     then FindRowPos := q
  59.   else FindRowPos := p;
  60. END; { FindRowPos }
  61.  
  62. function FindColmPos(A : NodePtr; r: rownum;c : colnum) : NodePtr;
  63.  
  64. { this function returns a pointer to a node in column c immediately
  65.   preceeding the position of interest, if it exists              }
  66.  
  67. VAR
  68.   P,Q,Z : NodePtr;
  69.  
  70. BEGIN
  71.   P := a;
  72.   While (p^.col < c) do
  73.     p := p^.nextcol;
  74.   Q := p;
  75.   z := p^.nextrow;
  76.   While (p^.row < r) and (z^.row <> 0) do
  77.     BEGIN
  78.       q := p;
  79.       p := p^.nextrow;
  80.       z := p^.nextrow;
  81.     END;
  82.   If ( p^.row >= r )
  83.     then FindColmPos := q
  84.   else FindColmPos := p;
  85. END; { FindColmPos }
  86.  
  87. PROCEDURE InsertAfter(rowptr,colptr : NodePtr; R : real);
  88. { This procedure inserts a new node after rowptr^ and colptr^,
  89.   in the same row as rowptr^ and the same col as colptr^      }
  90.  
  91. Var
  92.   Node : NodePtr;
  93.  
  94. BEGIN
  95.   if (rowptr = NIL) or (colptr = NIL)
  96.     then writeln('*******VOID INSERTION******')
  97.   else begin
  98.     new(Node);
  99.     Node^.val := R;
  100.     Node^.nextcol := RowPtr^.nextcol;
  101.     RowPtr^.nextcol := Node;
  102.     Node^.nextrow := ColPtr^.nextrow;
  103.     ColPtr^.nextrow := Node;
  104.   end
  105. END;
  106.  
  107. FUNCTION CreateArray(numrows : rownum; numcolms : colnum) : NodePtr;
  108.  
  109. { creates an empty sparse matrix, and returns a pointer to the Matrix }
  110. { Note that any area where Node does not exist, the Matrix Value = 0
  111.          by definition                                                }
  112.  
  113. VAR
  114.   A,temp,old : NodePtr;
  115.   i : integer;
  116.  
  117. BEGIN
  118.   new(A);
  119.   A^.row := 0;
  120.   A^.col := 0;
  121.   A^.val := 0;
  122.   A^.nextrow := A;
  123.   A^.nextcol := A;
  124.   old := A;
  125.   for i := 1 to numcolms do
  126.     BEGIN
  127.       new(temp);
  128.       temp^.row := 0;
  129.       temp^.col := i;
  130.       temp^.val := 0;
  131.       temp^.nextrow := temp;
  132.       temp^.nextcol := old^.nextcol;
  133.       old^.nextcol := temp;
  134.       old := temp;
  135.     END;
  136.   old := A;
  137.   for i := 1 to numrows do
  138.     BEGIN
  139.       new(temp);
  140.       temp^.col := 0;
  141.       temp^.row := i;
  142.       temp^.val := 0;
  143.       temp^.nextcol := temp;
  144.       temp^.nextrow := old^.nextrow;
  145.       old^.nextrow := temp;
  146.       old := temp;
  147.     END;
  148.   CreateArray := A;
  149. END; { CreateArray }
  150.  
  151. PROCEDURE EnterElement(A: NodePtr; r : rownum; c : colnum ; X : real);
  152.  
  153. { Enters the value X into element A[r,c] }
  154.  
  155. VAR
  156.   rptr,cptr : NodePtr;
  157.  
  158. BEGIN
  159.   cptr := FindColmPos(A,r,c);
  160.   if (cptr^.nextrow^.row <> r) or (cptr^.nextrow^.col <> c) then
  161.     BEGIN
  162.       rptr := FindRowPos(A,r,c);
  163.       InsertAfter(rptr,cptr,X);
  164.       cptr^.nextrow^.row := r;
  165.       cptr^.nextrow^.col := c;
  166.     END
  167.   ELSE cptr^.nextrow^.val := X
  168. END;
  169.  
  170. FUNCTION ReadElement(A: NodePtr; r : rownum; c : colnum) : real;
  171.  
  172. { Reads the Value of A[r,c] }
  173.  
  174. VAR
  175.   ColPtr : NodePtr;
  176. BEGIN
  177.   ColPtr := FindColmPos(A,r,c);
  178.   if (ColPtr^.Nextrow^.row <> r) or (ColPtr^.Nextrow^.col <> c)
  179.     THEN ReadElement := 0
  180.   ELSE ReadElement := ColPtr^.Nextrow^.val;
  181. END;
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.