nestdom.red 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. module nestdom; % nested domain: domain elements are standard quotients
  2. % coefficients are taken from the integers or another
  3. % dnest.
  4. % Original version by Herbert Melenk, 1993(?)
  5. % Improved version with Rainer mod.
  6. % Changes to nestlevel, nestdmode and nestsq by Winfried Neun, 1998.
  7. %%%%%%%%%
  8. % Adaption to allow convertion between arnum and nested.
  9. %%%%%%%%%
  10. symbolic procedure ident(x);x;
  11. put('!:ar!:,'!:nest!:,'ident);
  12. %%%%%%%%%
  13. % data structure:
  14. % a domain element is a list
  15. % ('!:nest!: level# dmode* . sq)
  16. smacro procedure nestlevel u; if fixp u then 0 else cadr u;
  17. smacro procedure nestdmode u; if fixp u then nil else caddr u;
  18. smacro procedure nestsq u; if fixp u then simp u else cdddr u;
  19. global '(domainlist!*);
  20. fluid '(alglist!* nestlevel!*);
  21. nestlevel!* := 0;
  22. switch nested;
  23. domainlist!* := union('(!:nest!:),domainlist!*);
  24. put('nested,'tag,'!:nest!:);
  25. put('!:nest!:,'dname,'nested);
  26. flag('(!:nest!:),'field);
  27. flag('(!:nest!:),'convert);
  28. put('!:nest!:,'i2d,'!*i2nest);
  29. %put('!:nest!:,'!:bf!:,'nestcnv);
  30. %put('!:nest!:,'!:ft!:,'nestcnv);
  31. %put('!:nest!:,'!:rn!:,'nestcnv);
  32. put('!:nest!:,'!:bf!:,mkdmoderr('!:nest!:,'!:bf!:));
  33. put('!:nest!:,'!:ft!:,mkdmoderr('!:nest!:,'!:ft!:));
  34. put('!:nest!:,'!:rn!:,mkdmoderr('!:nest!:,'!:rn!:));
  35. put('!:nest!:,'minusp,'nestminusp!:);
  36. put('!:nest!:,'plus,'nestplus!:);
  37. put('!:nest!:,'times,'nesttimes!:);
  38. put('!:nest!:,'difference,'nestdifference!:);
  39. put('!:nest!:,'quotient,'nestquotient!:);
  40. put('!:nest!:,'divide,'nestdivide!:);
  41. % put('!:nest!:,'gcd,'nestgcd!:);
  42. put('!:nest!:,'zerop,'nestzerop!:);
  43. put('!:nest!:,'onep,'nestonep!:);
  44. % put('!:nest!:,'factorfn,'factornest!:);
  45. put('!:nest!:,'prepfn,'nestprep!:);
  46. put('!:nest!:,'prifn,'prin2);
  47. put('!:rn!:,'!:nest!:,'rn2nest);
  48. symbolic procedure !*i2nest u;
  49. %converts integer u to nested form;
  50. if domainp u then u else
  51. '!:nest!: . 0 . dmode!* . (u ./ 1);
  52. symbolic procedure rn2nest u;
  53. %converts integer u to nested form;
  54. if domainp u then u else
  55. '!:nest!: . 0 . dmode!* . (cdr u);
  56. symbolic procedure nestcnv u;
  57. rederr list("Conversion between `nested' and",
  58. get(car u,'dname),"not defined");
  59. symbolic procedure nestminusp!: u;
  60. nestlevel u = 0 and minusf car nestsq u;
  61. symbolic procedure sq2nestedf sq;
  62. '!:nest!: . nestlevel!* . dmode!* . sq;
  63. symbolic procedure nest2op!:(u,v,op);
  64. (begin scalar r,nlu,nlv,nlr,dm,nestlevel!*;
  65. nlu := if not eqcar (u,'!:nest!:) then 0 else nestlevel u;
  66. nlv := if not eqcar (v,'!:nest!:) then 0 else nestlevel v;
  67. if nlu = nlv then goto case1
  68. else if nlu #> nlv then goto case2
  69. else goto case3;
  70. case1: % same level for u and v
  71. dm := nestdmode u;
  72. if dm then setdmode(dm,t);
  73. nlr := nlu;
  74. nestlevel!* := nlu - 1;
  75. r := apply(op,list(nestsq u,nestsq v));
  76. goto ready;
  77. case2: % v below u
  78. dm := nestdmode u;
  79. if dm then setdmode(dm,t);
  80. nlr := nlu;
  81. nestlevel!* := nlv;
  82. r := apply(op,list (nestsq u, v ./ 1));
  83. goto ready;
  84. case3: % u below v
  85. dm := nestdmode v;
  86. if dm then setdmode(dm,t);
  87. nlr := nlv;
  88. nestlevel!* := nlu;
  89. r := apply(op,list (u ./ 1,nestsq v));
  90. ready:
  91. r := if null numr r then nil
  92. else if domainp numr r and denr r = 1 then numr r
  93. else '!:nest!: . nlr . dm . r;
  94. if dm then setdmode (dm,nil);
  95. return r;
  96. end ) where dmode!* = nil;
  97. symbolic procedure nestplus!:(u,v); nest2op!:(u,v,'addsq);
  98. symbolic procedure nesttimes!:(u,v); nest2op!:(u,v,'multsq);
  99. symbolic procedure nestdifference!:(u,v);
  100. nest2op!:(u,v,function (lambda(x,y); addsq(x,negsq y)));
  101. symbolic procedure nestdivide!:(u,v); nest2op!:(u,v,'quotsq) . 1;
  102. % symbolic procedure nestgcd!:(u,v); !*i2nest 1;
  103. symbolic procedure nestquotient!:(u,v); nest2op!:(u,v,'quotsq);
  104. symbolic procedure nestzerop!: u; null numr nestsq u;
  105. symbolic procedure nestonep!: u;
  106. (car v = 1 and cdr v = 1) where v = nestsq u;
  107. initdmode 'nested;
  108. % nested routines are defined in the gennest nestule with the exception
  109. % of the following:
  110. symbolic procedure setnest u;
  111. begin
  112. u := reval u;
  113. if not fixp u then typerr(u,"nestulus");
  114. nestlevel!* := u;
  115. end;
  116. flag('(setnest),'opfn); %to make it a symbolic operator;
  117. flag('(setnest),'noval);
  118. algebraic operator co;
  119. symbolic procedure simpco u;
  120. % conmvert an expression to a nested coefficient
  121. begin scalar sq,lev;
  122. if not (length u = 2 and fixp car u) then
  123. typerr(u,"nested coefficient");
  124. sq := simp cadr u;
  125. lev := car u;
  126. return (if null numr sq then nil else ('!:nest!: . lev . dmode!* .
  127. sq)) ./ 1;
  128. end;
  129. put('co,'simpfn,'simpco);
  130. symbolic procedure nestprep!: u; list('co,nestlevel u,prepsq nestsq u);
  131. endmodule;
  132. end;