dfprin.red 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. module dfprin; % Printing for derivatives plus other options
  2. % suggested by the Twente group
  3. % Author: A. C. Norman, reconstructing ideas from Ben Hulshof,
  4. % Pim van den Heuvel and Hans van Hulzen.
  5. fluid '(!*fort !*nat depl!* posn!*);
  6. global '(!*dfprint
  7. !*noarg
  8. farglist!*);
  9. switch dfprint,noarg;
  10. !*dfprint := nil; % This is OFF by default because switching it on
  11. % changes Reduce output in a way that might upset
  12. % customers who have not found out about this switch.
  13. % Perhaps in later releases of the code (and when the
  14. % manual reflects this upgrade) it will be possible
  15. % to make 'on dfprint' the default. Some sites may of
  16. % course wish to arrange things otherwise...
  17. !*noarg := t; % If dfprint is enabled I am happy for noarg to be
  18. % the expected option.
  19. farglist!* := nil;
  20. symbolic procedure dfprintfn u;
  21. % Display derivatives - if suitable flags are set this uses
  22. % subscripts to denote differentiation and loses the arguments to
  23. % functions.
  24. if not !*nat or !*fort or not !*dfprint then 'failed
  25. else begin
  26. scalar w;
  27. w := layout!-formula('!!df!! . cdr u, 0, nil);
  28. if w = nil then return 'failed
  29. else putpline w
  30. end;
  31. put('df, 'prifn, 'dfprintfn);
  32. symbolic procedure dflayout u;
  33. % This is a prifn for !!df!!, which is used internally when I am
  34. % formatting derivatives, but which should only ever be seen in
  35. % testing!-width!* mode and never at all by the end-user.
  36. begin
  37. scalar op, args, w;
  38. w := car (u := cdr u);
  39. u := cdr u;
  40. if !*noarg then <<
  41. if atom w then <<
  42. op := w;
  43. args := assoc(op, depl!*); % Implicit args
  44. if args then args := cdr args >>
  45. else <<
  46. op := car w;
  47. args := cdr w >>; % Explicit args
  48. remember!-args(op, args);
  49. w := op >>;
  50. maprin w;
  51. if u then <<
  52. u := layout!-formula('!!dfsub!! . u, 0, nil); % subscript line
  53. if null u then return 'failed;
  54. w := 1 + cddr u;
  55. putpline((update!-pline(0, -w, caar u) . cdar u) .
  56. ((cadr u - w) . (cddr u - w))) >>
  57. end;
  58. symbolic procedure dfsublayout u;
  59. % This is a prifn for !!dfsub!!, which is used internally when I am
  60. % formatting derivatives, but which should only ever be seen in
  61. % testing!-width!* mode and never at all by the end-user.
  62. begin
  63. scalar dfcase, firstflag, w;
  64. % This is used as a prifn for both df and other things with
  65. % subscripts - dfcase remembers which.
  66. dfcase := (car u = '!!dfsub!!);
  67. u := cdr u;
  68. firstflag := t;
  69. while u do <<
  70. w := car u;
  71. u := cdr u;
  72. if firstflag then firstflag := nil
  73. else prin2!* ",";
  74. if dfcase and u and numberp car u then <<
  75. prin2!* car u;
  76. u := cdr u >>;
  77. maprin w >>
  78. end;
  79. put('!!df!!, 'prifn, 'dflayout);
  80. put('!!dfsub!!, 'prifn, 'dfsublayout);
  81. symbolic procedure remember!-args(op, args);
  82. % This records information that can be displayed by the user
  83. % issuing the command 'FARG'.
  84. begin
  85. scalar w;
  86. w := assoc(op, farglist!*);
  87. if null w then farglist!* := (op . args) . farglist!*
  88. end;
  89. symbolic procedure farg;
  90. % Implementation of FARG: display implicit argument data
  91. begin
  92. scalar newname;
  93. prin2!* "The operators have the following ";
  94. prin2!* "arguments or dependencies";
  95. terpri!* t;
  96. for each p in farglist!* do <<
  97. prin2!* car p;
  98. prin2!* "=";
  99. % To avoid clever pieces of code getting rid of argument displays
  100. % here I convert the name of the function into a string so that
  101. % maprin produces a simple but complete display. Since I expect
  102. % farg to be called but rarely this does not seem overexpensive
  103. newname := compress ('!" . append(explodec car p, '(!")));
  104. maprin(newname . cdr p);
  105. terpri!* t >>
  106. end;
  107. put('farg, 'stat, 'endstat);
  108. symbolic procedure clfarg;
  109. % Clear record of implicit args
  110. farglist!* := nil;
  111. put('clfarg, 'stat, 'endstat);
  112. symbolic procedure setprifn(u, fn);
  113. % Establish (or clear) prifn property for a list of symbols
  114. for each n in u do
  115. if idp n then <<
  116. % Things listed here will be declared operators now if they have
  117. % not been so declared earlier.
  118. if not operatorp n then mkop n;
  119. if fn then put(n, 'prifn, fn)
  120. else remprop(n, 'prifn) >>
  121. else lprim list(n, "not an identifier");
  122. symbolic procedure indexprin u;
  123. % Print helper-function when integer-valued arguments are to be shown as
  124. % subscripts
  125. if not !*nat or !*fort then 'failed
  126. else begin
  127. scalar w;
  128. w := layout!-formula('!!index!! . u, 0, nil);
  129. if w = nil then return 'failed
  130. else putpline w
  131. end;
  132. symbolic procedure indexpower(u, n);
  133. % Print helper-function when integer-valued arguments are to be shown as
  134. % subscripts with exponent n
  135. begin
  136. scalar w;
  137. w := layout!-formula('!!indexpower!! . n . u, 0, nil);
  138. if w = nil then return 'failed
  139. else putpline w
  140. end;
  141. symbolic procedure indexlayout u;
  142. % This is a prifn for !!index!!, which is used internally when I am
  143. % formatting index forms, but which should only ever be seen in
  144. % testing!-width!* mode and never at all by the end-user.
  145. begin
  146. scalar w;
  147. w := car (u := cdr u);
  148. u := cdr u;
  149. maprin w;
  150. if u then <<
  151. u := layout!-formula('!!indexsub!! . u, 0, nil);
  152. % subscript line
  153. if null u then return 'failed;
  154. w := 1 + cddr u;
  155. putpline((update!-pline(0, -w, caar u) . cdar u) .
  156. ((cadr u - w) . (cddr u - w))) >>
  157. end;
  158. symbolic procedure indexpowerlayout u;
  159. % Format a subscripted object raised to some power.
  160. begin
  161. scalar n, w, pos, maxpos;
  162. n := car (u := cdr u); % The exponent
  163. w := car (u := cdr u);
  164. u := cdr u;
  165. maprin w;
  166. w := layout!-formula(n, 0, nil);
  167. pos := posn!*;
  168. putpline((update!-pline(0, 1 - cadr w, caar w) . cdar w) .
  169. (1 . (1 + cddr w - cadr w)));
  170. maxpos := posn!*;
  171. posn!* := pos;
  172. if u then <<
  173. u := layout!-formula('!!indexsub!! . u, 0,nil);
  174. % subscript line
  175. if null u then return 'failed;
  176. w := 1 + cddr u;
  177. putpline((update!-pline(0, -w, caar u) . cdar u) .
  178. ((cadr u - w) . (cddr u - w))) >>;
  179. posn!* := max(posn!*, maxpos)
  180. end;
  181. put('!!index!!, 'prifn, 'indexlayout);
  182. put('!!indexpower!!, 'prifn, 'indexpowerlayout);
  183. put('!!indexsub!!, 'prifn, 'dfsublayout);
  184. symbolic procedure noargsprin u;
  185. % Print helper-function when arguments for a function are to be hidden,
  186. % but remembered for display via farg
  187. if not !*nat or !*fort then 'failed
  188. else <<
  189. remember!-args(car u, cdr u);
  190. maprin car u >>;
  191. symbolic procedure doindex u;
  192. % Establish some function names to have args treated as index values
  193. setprifn(u, 'indexprin);
  194. symbolic procedure offindex u;
  195. % Clear effect of doindex
  196. setprifn(u, nil);
  197. symbolic procedure donoargs u;
  198. % Identify functions where args are to be hidden
  199. setprifn(u, 'noargsprin);
  200. symbolic procedure offnoargs u;
  201. % Clear effect of donoargs
  202. setprifn(u, nil);
  203. put('doindex, 'stat, 'rlis);
  204. put('offindex, 'stat, 'rlis);
  205. put('donoargs, 'stat, 'rlis);
  206. put('offnoargs, 'stat, 'rlis);
  207. endmodule;
  208. end;