nssimp.red 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. module nssimp; % Simplification functions for non-scalar quantities.
  2. % Author: Anthony C. Hearn.
  3. % Copyright (c) 1987 The RAND Corporation. All rights reserved.
  4. fluid '(!*div frlis!* subfg!*);
  5. % Several inessential uses of ACONC, NCONC, and MAPping "JOIN". Latter
  6. % not yet changed.
  7. symbolic procedure nssimp(u,v);
  8. %U is a prefix expression involving non-commuting quantities.
  9. %V is the type of U. Result is an expression of the form
  10. % SUM R(I)*PRODUCT M(I,J) where the R(I) are standard
  11. %quotients and the M(I,J) non-commuting expressions;
  12. %N. B: the products in M(I,J) are returned in reverse order
  13. %(to facilitate, e.g., matrix augmentation);
  14. begin scalar r,s,w,x,y,z;
  15. u := dsimp(u,v);
  16. a: if null u then return z;
  17. w := car u;
  18. c: if null w then go to d
  19. else if numberp(r := car w)
  20. or not(eqcar(r,'!*div) or
  21. (if (s := getrtype r) eq 'yetunknowntype
  22. then getrtype(r :=
  23. eval!-yetunknowntypeexpr(r,nil))
  24. else s) eq v)
  25. then x := aconc!*(x,r)
  26. else y := aconc!*(y,r);
  27. w := cdr w;
  28. go to c;
  29. d: if null y then go to er;
  30. e: z := addns(((if null x then 1 ./ 1 else simptimes x) . y),z);
  31. u := cdr u;
  32. x := y:= nil;
  33. go to a;
  34. er: y := v;
  35. if idp car x
  36. then if not flagp(car x,get(y,'fn)) then redmsg(car x,y)
  37. else rerror(alg,30,list(y,x,"not set"))
  38. else if w := get(get(y,'tag),'i2d)
  39. then <<y := list apply1(w,1); go to e>>
  40. %to allow a scalar to be a 1 by 1 matrix;
  41. else msgpri(list("Missing",y,"in"),car x,nil,nil,t);
  42. put(car x,'rtype,y);
  43. y := list car x;
  44. x := cdr x;
  45. go to e
  46. end;
  47. symbolic procedure dsimp(u,v);
  48. %result is a list of lists representing a sum of products;
  49. %N. B: symbols are in reverse order in product list;
  50. if numberp u then list list u
  51. else if atom u
  52. then (if x and subfg!* then dsimp(cadr x,v)
  53. else if flagp(u,'share) then dsimp(lispeval u,v)
  54. else <<flag(list u,'used!*); list list u>>)
  55. where x= get(u,'avalue)
  56. else if car u eq 'plus
  57. then for each j in cdr u join dsimp(j,v)
  58. else if car u eq 'difference
  59. then nconc!*(dsimp(cadr u,v),
  60. dsimp('minus . cddr u,v))
  61. else if car u eq 'minus
  62. then dsimptimes(list(-1,carx(cdr u,'dsimp)),v)
  63. else if car u eq 'times then dsimptimes(cdr u,v)
  64. else if car u eq 'quotient
  65. then dsimptimes(list(cadr u,list('recip,carx(cddr u,'dsimp))),v)
  66. else if not(getrtype u eq v) then list list u
  67. else if car u eq 'recip
  68. then list list list('!*div,carx(cdr u,'dsimp))
  69. else if car u eq 'expt then (lambda z;
  70. if not numberp z then errpri2(u,t)
  71. else if z<0
  72. then list list list('!*div,'times . nlist(cadr u,-z))
  73. else if z=0 then list list list('!*div,cadr u,1)
  74. else dsimptimes(nlist(cadr u,z),v))
  75. reval_without_mod caddr u
  76. else if flagp(car u,'noncommuting) then list list u
  77. else if arrayp car u
  78. then dsimp(getelv u,v)
  79. else (if x then dsimp(x,v)
  80. else ((if z then dsimp(z,v) else {{y}})
  81. where z=opmtch y) where y=revop1 u)
  82. where x=opmtch u;
  83. symbolic procedure dsimptimes(u,v);
  84. if null u then errach 'dsimptimes
  85. else if null cdr u then dsimp(car u,v)
  86. else (lambda j; for each k in dsimptimes(cdr u,v) join mappend(j,k))
  87. dsimp(car u,v);
  88. symbolic procedure addns(u,v);
  89. if null v then list u
  90. else if cdr u=cdar v
  91. then (lambda x; % if null car x then cdr v else;
  92. (x . cdr u) . cdr v)
  93. addsq(car u,caar v)
  94. else if ordp(cdr u,cdar v) then u . v
  95. else car v . addns(u,cdr v);
  96. symbolic procedure getelx u;
  97. %to take care of free variables in LET statements;
  98. if smemqlp(frlis!*,cdr u) then nil
  99. else if null(u := getelv u) then 0
  100. else reval u;
  101. endmodule;
  102. end;