lamatrix.red 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. module lmatrix;
  2. %**********************************************************************%
  3. % %
  4. % This module forms the ability for matrices to be passed locally. %
  5. % %
  6. % Authors: W. Neun (customised by Matt Rebbeck). %
  7. % %
  8. %**********************************************************************%
  9. switch mod_was_on; % Used internally to keep track of the modular
  10. % switch.
  11. symbolic procedure mkmatrix(n,m);
  12. %
  13. % Create an nXm matrix.
  14. %
  15. 'mat . (for i:=1:n collect
  16. for j:=1:m collect 0);
  17. symbolic procedure setmat(matri,i,j,val);
  18. %
  19. % Set matrix element (i,j) to val.
  20. %
  21. << if !*modular then << off modular; on mod_was_on; >>;
  22. i := my_reval i;
  23. j := my_reval j;
  24. my_letmtr(list(matri,i,j),val,matri);
  25. if !*mod_was_on then << on modular; off mod_was_on; >>;
  26. matri>>;
  27. symbolic procedure getmat(matri,i,j);
  28. %
  29. % Get matrix element (i,j).
  30. %
  31. << if !*modular then << off modular; on mod_was_on; >>;
  32. i := my_reval i;
  33. j := my_reval j;
  34. if !*mod_was_on then << on modular; off mod_was_on; >>;
  35. unchecked_getmatelem list(matri,i,j)>>;
  36. symbolic procedure unchecked_getmatelem u;
  37. begin scalar x;
  38. if not eqcar(x := car u,'mat)
  39. then rerror(matrix,1,list("Matrix",car u,"not set"))
  40. else return nth(nth(cdr x,cadr u),caddr u);
  41. end;
  42. symbolic procedure my_letmtr(u,v,y);
  43. %
  44. % Substitution for matrix elements with reval only when necessary.
  45. %
  46. begin
  47. scalar z;
  48. if not eqcar(y,'mat) then
  49. rerror(matrix,10,list("Matrix",car u,"not set"))
  50. else if not numlis (z := my_revlis cdr u) or length z neq 2
  51. then return errpri2(u,'hold);
  52. rplaca(pnth(nth(cdr y,car z),cadr z),v);
  53. end;
  54. endmodule; % lmatrix.
  55. end;