matrix.red 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. module matrix; % Header for matrix package.
  2. % Author: Anthony C. Hearn.
  3. % Copyright (c) 1998 Anthony C. Hearn. All rights reserved.
  4. % This module has one reference to rplaca.
  5. create!-package('(matrix matsm matpri extops bareiss det glmat nullsp
  6. rank nestdom resultnt cofactor),nil);
  7. fluid '(!*sub2 subfg!*);
  8. global '(nxtsym!*);
  9. symbolic procedure matrix u;
  10. % Declares list U as matrices.
  11. begin scalar w,x,y;
  12. for each j in u do
  13. if atom j then if null (x := gettype j)
  14. then put(j,'rtype,'matrix)
  15. else if x eq 'matrix
  16. then <<lprim list(x,j,"redefined");
  17. put(j,'rtype,'matrix)>>
  18. else typerr(list(x,j),"matrix")
  19. else if not idp car j then errpri2(j,'hold)
  20. else if not (x := gettype car j) or x eq 'matrix
  21. then <<if length j neq 3 then typerr(j,'matrix);
  22. x := reval_without_mod cadr j;
  23. if not fixp x or x<=0 then typerr(x,"positive integer");
  24. y := reval_without_mod caddr j;
  25. if not fixp y or y<=0 then typerr(y,"positive integer");
  26. w := nil; for n := 1:x do w := nzero y . w;
  27. put(car j,'rtype,'matrix);
  28. put(car j,'avalue,list('matrix,'mat . w))>>
  29. else typerr(list(x,car j),"matrix")
  30. end;
  31. rlistat '(matrix);
  32. symbolic procedure nzero n;
  33. % Returns a list of N zeros.
  34. if n=0 then nil else 0 . nzero(n-1);
  35. % Parsing interface.
  36. symbolic procedure matstat;
  37. % Read a matrix.
  38. begin scalar x,y;
  39. if not (nxtsym!* eq '!() then symerr("Syntax error",nil);
  40. a: scan();
  41. if not (scan() eq '!*lpar!*) then symerr("Syntax error",nil);
  42. y := xread 'paren;
  43. if not eqcar(y,'!*comma!*) then y := list y else y := remcomma y;
  44. x := y . x;
  45. if nxtsym!* eq '!)
  46. then return <<scan(); scan(); 'mat . reversip x>>
  47. else if not(nxtsym!* eq '!,) then symerr("Syntax error",nil);
  48. go to a
  49. end;
  50. put('mat,'stat,'matstat);
  51. symbolic procedure formmat(u,vars,mode);
  52. 'list . mkquote car u
  53. . for each x in cdr u collect('list . formlis(x,vars,mode));
  54. put('mat,'formfn,'formmat);
  55. put('mat,'i2d,'mkscalmat);
  56. put('mat,'inversefn,'matinverse);
  57. put('mat,'lnrsolvefn,'lnrsolve);
  58. put('mat,'rtypefn,'quotematrix);
  59. symbolic procedure quotematrix u; 'matrix;
  60. flag('(mat tp),'matflg);
  61. flag('(mat),'noncommuting);
  62. put('mat,'prifn,'matpri);
  63. flag('(mat),'struct); % for parsing
  64. put('matrix,'fn,'matflg);
  65. put('matrix,'evfn,'matsm!*);
  66. flag('(matrix),'sprifn);
  67. put('matrix,'tag,'mat);
  68. put('matrix,'lengthfn,'matlength);
  69. put('matrix,'getelemfn,'getmatelem);
  70. put('matrix,'setelemfn,'setmatelem);
  71. symbolic procedure mkscalmat u;
  72. % Converts id u to 1 by 1 matrix.
  73. list('mat,list u);
  74. symbolic procedure getmatelem u;
  75. % This differs from setmatelem in that let x=y, where y is a
  76. % matrix, should work.
  77. begin scalar x,y;
  78. if length u neq 3 then typerr(u,"matrix element");
  79. x := get(car u,'avalue);
  80. if null x or not(car x eq 'matrix) then typerr(car u,"matrix")
  81. else if not eqcar(x := cadr x,'mat)
  82. then if idp x then return getmatelem (x . cdr u)
  83. else rerror(matrix,1,list("Matrix",car u,"not set"));
  84. y := reval_without_mod cadr u;
  85. if not fixp y or y<=0 then typerr(y,"positive integer");
  86. x := nth(cdr x,y);
  87. y := reval_without_mod caddr u;
  88. if not fixp y or y<=0 then typerr(y,"positive integer");
  89. return nth(x,y)
  90. end;
  91. symbolic procedure setmatelem(u,v);
  92. begin scalar x,y;
  93. if length u neq 3 then typerr(u,"matrix element");
  94. x := get(car u,'avalue);
  95. if null x or not(car x eq 'matrix) then typerr(car u,"matrix")
  96. else if not eqcar(x := cadr x,'mat)
  97. then rerror(matrix,10,list("Matrix",car u,"not set"));
  98. y := reval_without_mod cadr u;
  99. if not fixp y or y<=0 then typerr(y,"positive integer");
  100. x := nth(cdr x,y);
  101. y := reval_without_mod caddr u;
  102. if not fixp y or y<=0 then typerr(y,"positive integer");
  103. return rplaca(pnth(x,y),v)
  104. end;
  105. symbolic procedure matlength u;
  106. if not eqcar(u,'mat) then rerror(matrix,2,list("Matrix",u,"not set"))
  107. else list('list,length cdr u,length cadr u);
  108. % Aggregate Property. Commented out for now.
  109. % symbolic procedure matrixmap(u,v);
  110. % if flagp(car u,'matmapfn)
  111. % then matsm!*1 for each j in matsm cadr u collect
  112. % for each k in j collect simp!*(car u . mk!*sq k . cddr u)
  113. % else if flagp(car u,'matfn) then reval2(u,v)
  114. % else typerr(car u,"matrix operator");
  115. % put('matrix,'aggregatefn,'matrixmap);
  116. % flag('(int df),'matmapfn);
  117. % flag('(det trace),'matfn);
  118. % symbolic procedure mk!*sq2 u;
  119. % begin scalar x;
  120. % x := !*sub2; % Since we need value for each element.
  121. % u := subs2 u;
  122. % !*sub2 := x;
  123. % return mk!*sq u
  124. % end;
  125. endmodule;
  126. end;