vectorop.red 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. module vectorop;
  2. % This small module makes basic operation between EXPLICIT
  3. % vectors available. They are assumed to be represented by
  4. % BAGS or LISTS.
  5. % Mixed product is restricted to 3-space vectors.
  6. ;
  7. symbolic procedure depthl1!: u;
  8. if null u then t else (caar u neq 'list) and depthl1!: cdr u;
  9. symbolic procedure depthl1 u;
  10. not null getrtype u and depthl1!: cdr u;
  11. symbolic procedure !:vect(u,v,bool);
  12. %returns a list whose elements are the sum of each list elements.
  13. % null v check not necessary;
  14. if null u then nil
  15. else addsq(car u,if null bool then car v else negsq car v)
  16. . !:vect(cdr u,cdr v,bool);
  17. symbolic procedure rsumvect(u);
  18. begin scalar x,y,prf;
  19. x:=reval car u;y:=reval cadr u; prf:=car x;
  20. if (rdepth list x = 0) or (rdepth list y = 0) then
  21. rederr " both arguments must be of depth 1 " else
  22. x:=cdr x; y:=cdr y;
  23. if length x neq length y then rederr "vector mismatch";
  24. x:=for each j in x collect simp!* j;
  25. y:=for each j in y collect simp!* j;
  26. return prf . (for each j in !:vect(x,y,nil) collect mk!*sq j) end;
  27. put('sumvect,'psopfn,'rsumvect);
  28. symbolic procedure rminvect(u);
  29. begin scalar x,y,prf;
  30. x:=reval car u;y:=reval cadr u; prf:=car x;
  31. if (rdepth list x = 0) or (rdepth list y = 0) then
  32. rederr " both arguments must be of depth 1 " else
  33. x:=cdr x; y:=cdr y;
  34. if length x neq length y then rederr "vector mismatch";
  35. x:=for each j in x collect simp!* j;
  36. y:=for each j in y collect simp!* j;
  37. return prf . (for each j in !:vect(x,y,'minus) collect mk!*sq j) end;
  38. put('minvect,'psopfn,'rminvect);
  39. symbolic procedure !:scalprd(u,v);
  40. %returns scalar product of two lists;
  41. if null u and null v then nil ./ 1
  42. else addsq(multsq(car u,car v),!:scalprd(cdr u,cdr v));
  43. symbolic procedure sscalvect(u);
  44. begin scalar x,y;
  45. x:=reval car u;y:=reval cadr u;
  46. if (rdepth list x = 0) or (rdepth list y = 0) then
  47. rederr " both arguments must be of depth 1 " else
  48. if length x neq length y then rederr "vector mismatch";
  49. x:=cdr x; y:=cdr y;
  50. x:=for each j in x collect simp!* j;
  51. y:=for each j in y collect simp!* j;
  52. return mk!*sq !:scalprd(x,y)
  53. end;
  54. put('scalvect,'psopfn,'sscalvect);
  55. symbolic procedure !:pvect3 u;
  56. begin scalar x,y; integer xl;
  57. if (rdepth list car u = 0) or (rdepth cdr u = 0) then
  58. rederr " both arguments must be of depth 1 " else
  59. x:=reval car u;y:=reval cadr u;
  60. if (xl:=length x) neq 4 then rederr "not 3-space vectors" else
  61. if xl neq length y then rederr "vector mismatch" ;
  62. x:=cdr x; y:=cdr y;
  63. x:=for each j in x collect simp!* j;
  64. y:=for each j in y collect simp!* j;
  65. return
  66. list( addsq(multsq(cadr x,caddr y),negsq multsq(caddr x,cadr y)),
  67. addsq(multsq(caddr x,car y),negsq multsq(car x,caddr y)),
  68. addsq(multsq(car x,cadr y),negsq multsq(cadr x,car y)))
  69. end;
  70. symbolic procedure rcrossvect u;
  71. % implemented only with LIST prefix;
  72. 'list . (for each j in !:pvect3 u collect mk!*sq j);
  73. put ('crossvect,'psopfn,'rcrossvect);
  74. symbolic procedure smpvect u;
  75. begin scalar x;
  76. if (rdepth list car u =0) then
  77. rederr " arguments must be of depth 1 " else
  78. x:=reval car u; u:=cdr u;
  79. x:=cdr x;
  80. if length x neq 3 then rederr " not 3-space vector";
  81. x:=for each j in x collect simp!* j;
  82. return mk!*sq !:scalprd(x,!:pvect3 u) end;
  83. put('mpvect,'psopfn,'smpvect);
  84. endmodule;
  85. end;