home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / prolog / pdprolog / matrix.pro < prev    next >
Text File  |  1986-05-05  |  1KB  |  43 lines

  1.  
  2.  
  3. con_num_list( [H|T] ) :- rnum( H ), con_num_list( T ).
  4. con_num_list( [] ).
  5.  
  6.  
  7. /* To multiply a row by a column: */
  8.  
  9. mat_mult( [H1|T1], [H2|T2], res ) :-
  10.     mat_mult1( [H1|T1], [H2|T2, 0, res ).
  11.  
  12. mat_mult1( [H1|T1], [H2|T2], sum, res ) :-
  13.     sum is sum + H1 * H2,
  14.     mat_mult1( T1, T2, sum, res ).
  15.  
  16. mat_mult1( [], [], sum, res ) :- res = sum.
  17.  
  18.  
  19. /* To get the nth element of a list: 
  20.    Let us assume a matrix in the form of a list of columns: 
  21.  
  22. [ L1, L2.....]
  23.  
  24. listel( N, [H|T], X ) :- 
  25.     N > 0, 
  26.     N is N - 1,
  27.     listel( N, T, X ).
  28.  
  29. listel( N, [H|_], X ) :- X = H.
  30.  
  31. /* Below, X, Y are the "coordinates".
  32.    L is the complex list representing the array.
  33.    element is the returned value. */
  34.  
  35. matrix_el( X, Y, L, element ) :-
  36.  
  37. /* First get the row, represented as an element "rowel" in list L */
  38.  
  39.     listel( X, L, rowel ),
  40.  
  41. /* And now the value contained within the row. */
  42.     listel( Y, rowel, element ).
  43.