mv.red 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. module mv; % Operations on multivariate forms.
  2. % Author: Anthony C. Hearn.
  3. % Copyright (c) 1989 The RAND Corporation. All Rights Reserved.
  4. % These smacros are local to this module.
  5. symbolic smacro procedure mv!-term!-coeff u; cdr u;
  6. symbolic smacro procedure mv!-term!-pow u; car u;
  7. symbolic smacro procedure mv!-tpow u; car u;
  8. symbolic smacro procedure mv!-tc u; cdr u;
  9. symbolic procedure mv!-!+(u,v);
  10. if null u then v
  11. else if null v then u
  12. else if mv!-lpow u= mv!-lpow v
  13. then (lambda x;
  14. if x=0 then mv!-!+(mv!-red u,mv!-red v)
  15. else mv!-!.!+(mv!-!.!*(mv!-lpow u,x),
  16. mv!-!+(mv!-red u,mv!-red v)))
  17. (mv!-lc u + mv!-lc v)
  18. else if mv!-pow!-!>(mv!-lpow u,mv!-lpow v)
  19. then mv!-!.!+(mv!-lt u,mv!-!+(mv!-red u,v))
  20. else mv!-!.!+(mv!-lt v,mv!-!+(u,mv!-red v));
  21. symbolic smacro procedure domain!-!*(u,v); u*v;
  22. symbolic smacro procedure domain!-!/(u,v); u/v;
  23. symbolic procedure mv!-term!-!*(u,v);
  24. % U is a (non-zero) term and v a multivariate form. Result is
  25. % product of u and v.
  26. if null v then nil
  27. else mv!-!.!+(mv!-!.!*(mv!-pow!-!+(mv!-tpow u,mv!-lpow v),
  28. domain!-!*(mv!-tc u,mv!-lc v)),
  29. mv!-term!-!*(u,mv!-red v));
  30. symbolic procedure mv!-term!-!/(u,v);
  31. % Returns the result of the (exact) division of u by term v.
  32. if null u then nil
  33. else mv!-!.!+(mv!-!.!*(mv!-pow!-!-(mv!-lpow u,mv!-tpow v),
  34. domain!-!/(mv!-lc u,mv!-tc v)),
  35. mv!-term!-!/(mv!-red u,v));
  36. symbolic procedure mv!-domainlist u;
  37. if null u then nil
  38. else mv!-lc u . mv!-domainlist mv!-red u;
  39. symbolic procedure mv!-pow!-mv!-!+(u,v);
  40. if null v then nil
  41. else mv!-!.!+(mv!-pow!-mv!-term!-!+(u,mv!-lt v),
  42. mv!-pow!-mv!-!+(u,mv!-red v));
  43. symbolic procedure mv!-pow!-mv!-term!-!+(u,v);
  44. mv!-!.!*(mv!-pow!-!+(u,mv!-term!-pow v), mv!-term!-coeff v);
  45. symbolic procedure mv!-pow!-!+(u,v);
  46. if null u then nil
  47. else (car u+car v) . mv!-pow!-!+(cdr u,cdr v);
  48. symbolic procedure mv!-pow!-!-(u,v);
  49. if null u then nil
  50. else (car u-car v) . mv!-pow!-!-(cdr u,cdr v);
  51. symbolic procedure mv!-pow!-!*(u,v);
  52. if null v then nil
  53. else (u*car v) . mv!-pow!-!*(u,cdr v);
  54. symbolic procedure mv!-pow!-minusp u;
  55. if null u then nil
  56. else car u<0 or mv!-pow!-minusp cdr u;
  57. symbolic procedure mv!-pow!-!>(u,v);
  58. if null u then nil
  59. else if car u=car v then mv!-pow!-!>(cdr u,cdr v)
  60. else car u>car v;
  61. symbolic procedure mv!-reduced!-coeffs u;
  62. % reduce coefficients of u to lowest terms.
  63. begin scalar x,y;
  64. x := mv!-lc u;
  65. y := mv!-red u;
  66. while y and x neq 1 do <<x := gcdn(x,mv!-lc y); y := mv!-red y>>;
  67. return if x=1 then u else mv!-!/(u,x)
  68. end;
  69. symbolic procedure mv!-!/(u,v);
  70. if null u then nil
  71. else mv!-!.!+(mv!-!.!*(mv!-lpow u,mv!-lc u/v),mv!-!/(mv!-red u,v));
  72. % Functions that convert between standard forms and multivariate forms.
  73. symbolic procedure sf2mv(u,varlist);
  74. % Converts the standard form u to a multivariate form wrt varlist.
  75. sf2mv1(u,nil,varlist);
  76. symbolic procedure sf2mv1(u,powers,varlist);
  77. if null u then nil
  78. else if domainp u
  79. then list(append(powers,nzeros length varlist) . u)
  80. else if mvar u = car varlist % This should be eq, but seems to
  81. % need equal.
  82. then append(sf2mv1(lc u,append(powers,list ldeg u),cdr varlist),
  83. sf2mv1(red u,powers,varlist))
  84. else sf2mv1(u,append(powers,list 0),cdr varlist);
  85. symbolic procedure nzeros n; if n=0 then nil else 0 . nzeros(n-1);
  86. symbolic procedure mv2sf(u,varlist);
  87. % converts the multivariate form u to a standard form wrt varlist.
  88. % This version uses addf to fold terms - there is probably a more
  89. % direct method.
  90. if null u then nil
  91. else addf(mv2sf1(mv!-lpow u,cdar u,varlist),mv2sf(cdr u,varlist));
  92. symbolic procedure mv2sf1(powers,cf,varlist);
  93. if null powers then cf
  94. else if car powers=0 then mv2sf1(cdr powers,cf,cdr varlist)
  95. else !*t2f((car varlist .** car powers)
  96. .* mv2sf1(cdr powers,cf,cdr varlist));
  97. endmodule;
  98. end;