crintfix.red 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. module intfix$ % Further fixes to the integration package.
  2. if lisp !*comp then apply1('load!-package, 'int)$
  3. fluid '(!*depend !*nolnr !*failhard)$
  4. % die folgende Aenderung verhindert das Erzeugen von int* ...
  5. remd('simpint!*)$
  6. symbolic procedure simpint!* u$
  7. begin scalar x$
  8. return if (x := opmtch('int . u)) then simp x
  9. else simpiden('int . u)
  10. % statt else simpiden('int!* . u)
  11. end$
  12. % ein Patch fuer das REDUCE 3.5 EZGCD
  13. %symbolic procedure simpexpt u$
  14. % % We suppress reordering during exponent evaluation, otherwise
  15. % % internal parts (as in e^(a*b)) can have wrong order.
  16. % begin scalar expon;
  17. % expon := simpexpon carx(cdr u,'expt) where kord!*=nil;
  18. % expon := resimp expon; % We still need right order. <--- change.
  19. % return simpexpt1(car u,expon,nil)
  20. % end$
  21. % Zum Integrieren
  22. % put('int, 'simpfn, 'SimpIntPatch)$
  23. %algebraic <<
  24. % % fuer reelle Rechnungen:
  25. % let {abs(~r)**(~n) => r**n when (fixp(n) and evenp(n))}$
  26. % let {
  27. % int(1/~x^(~n),~x) => -x/(x^n*(n-1)) when numberp n,
  28. % ~x^(~m/~n)*~x => x**((m+n)/n) when (numberp n and numberp m),
  29. % int(~z/~y,~x) => log(y) when z = df(y,x)}$
  30. %
  31. % if sin(!%x)**2+cos(!%x)**2 neq 1 then
  32. % let {sin(~x)**2 => 1-cos(x)**2}$
  33. %
  34. % if cosh(!%x)**2 neq (sinh(!%x)**2 + 1) then
  35. % let {cosh(~x)**2 => (sinh(x)**2 + 1)}$
  36. %
  37. % if sin(!%x)*tan(!%x/2)+cos(!%x) neq 1 then
  38. % let {tan(~x/2) => (1-cos(x))/sin(x)}$
  39. %
  40. % if sin(!%x)*cot(!%x/2)-cos(!%x) neq 1 then
  41. % let {cot(~x/2) => (1+cos(x))/sin(x)}$
  42. %
  43. % if sqrt(!%x**2-!%y**2)-sqrt(!%x-!%y)*sqrt(!%x+!%y) neq 0 then
  44. % let {sqrt(~x)*sqrt(~y) => sqrt(x*y)}
  45. %>>$
  46. endmodule$
  47. module dfint$
  48. % Patch to improve differentiation, mainly of integrals.
  49. % This version specifically for use by the crack package.
  50. % Francis J. Wright <F.J.Wright@QMW.ac.uk>, 27 December 1997
  51. fluid '(!*fjwflag)$ !*fjwflag := t$
  52. switch allowdfint, dfint$ % dfint OFF by default
  53. deflist('((dfint ((t (rmsubs))))
  54. (allowdfint ((t (progn (put 'int 'dfform 'dfform_int) (rmsubs)))
  55. (nil (remprop 'int 'dfform))))), 'simpfg)$
  56. % There is no code to reverse the df-int commutation,
  57. % so no reason to call rmsubs when the switch is turned off.
  58. !*allowdfint := t$ % allowdfint ON by default
  59. put('int, 'dfform, 'dfform_int)$
  60. % The switch allowdfint ALLOWS differentiation under the integral sign
  61. % provided the result simplies, and should normally be on.
  62. % The switch dfint FORCES differentiation under the integral sign,
  63. % PROVIDED ALLOWDFINT IS ALSO ON, and should normally be turned on
  64. % only when required.
  65. symbolic procedure diffp(u,v);
  66. % U is a standard power, V a kernel.
  67. % Value is the standard quotient derivative of U wrt V.
  68. begin scalar n,w,x,y,z; integer m;
  69. n := cdr u; % integer power.
  70. u := car u; % main variable.
  71. if u eq v and (w := 1 ./ 1) then go to e
  72. else if atom u then go to f
  73. %else if (x := assoc(u,dsubl!*)) and (x := atsoc(v,cdr x))
  74. % and (w := cdr x) then go to e % deriv known.
  75. % DSUBL!* not used for now.
  76. else if (not atom car u and (w:= difff(u,v)))
  77. or (car u eq '!*sq and (w:= diffsq(cadr u,v)))
  78. then go to c % extended kernel found.
  79. else if x := get(car u,'dfform) then return apply3(x,u,v,n)
  80. else if x:= get(car u,dfn_prop u) then nil
  81. else if car u eq 'plus and (w := diffsq(simp u,v))
  82. then go to c
  83. else go to h; % unknown derivative.
  84. y := x;
  85. z := cdr u;
  86. a: w := diffsq(simp car z,v) . w;
  87. if caar w and null car y then go to h; % unknown deriv.
  88. y := cdr y;
  89. z := cdr z;
  90. if z and y then go to a
  91. else if z or y then go to h; % arguments do not match.
  92. y := reverse w;
  93. z := cdr u;
  94. w := nil ./ 1;
  95. b: % computation of kernel derivative.
  96. if caar y
  97. then w := addsq(multsq(car y,simp subla(pair(caar x,z),
  98. cdar x)),
  99. w);
  100. x := cdr x;
  101. y := cdr y;
  102. if y then go to b;
  103. c: % save calculated deriv in case it is used again.
  104. % if x := atsoc(u,dsubl!*) then go to d
  105. % else x := u . nil;
  106. % dsubl!* := x . dsubl!*;
  107. % d: rplacd(x,xadd(v . w,cdr x,t));
  108. e: % allowance for power.
  109. % first check to see if kernel has weight.
  110. if (x := atsoc(u,wtl!*))
  111. then w := multpq('k!* .** (-cdr x),w);
  112. m := n-1;
  113. % Evaluation is far more efficient if results are rationalized.
  114. return rationalizesq if n=1 then w
  115. else if flagp(dmode!*,'convert)
  116. and null(n := int!-equiv!-chk
  117. apply1(get(dmode!*,'i2d),n))
  118. then nil ./ 1
  119. else multsq(!*t2q((u .** m) .* n),w);
  120. f: % Check for possible unused substitution rule.
  121. if not depends(u,v)
  122. and (not (x:= atsoc(u,powlis!*))
  123. or not depends(cadddr x,v))
  124. and null !*depend
  125. then return nil ./ 1;
  126. w := list('df,u,v);
  127. w := if x := opmtch w then simp x else mksq(w,1);
  128. go to e;
  129. h: % Final check for possible kernel deriv.
  130. if car u eq 'df % multiple derivative
  131. then if depends(cadr u,v)
  132. % FJW - my version of above test was simply as follows. Surely, inner
  133. % derivative will already have simplied to 0 unless v depends on A!
  134. and not(cadr u eq v)
  135. % (df (df v A) v) ==> 0
  136. %% and not(cadr u eq v and not depends(v,caddr u))
  137. %% % (df (df v A) v) ==> 0 unless v depends on A.
  138. then
  139. <<if !*fjwflag and eqcar(cadr u, 'int) then
  140. % (df (df (int F x) A) v) ==> (df (df (int F x) v) A) ?
  141. % Commute the derivatives to differentiate the integral?
  142. if caddr cadr u eq v then
  143. % Evaluating (df u v) where u = (df (int F v) A)
  144. % Just return (df F A) - derivative absorbed
  145. << w := 'df . cadr cadr u . cddr u; go to j >>
  146. else if !*allowdfint and
  147. % Evaluating (df u v) where u = (df (int F x) A)
  148. % (If dfint is also on then this will not arise!)
  149. % Commute only if the result simplifies:
  150. not_df_p(w := diffsq(simp!* cadr cadr u, v))
  151. then <<
  152. % Generally must re-evaluate the integral (carefully!)
  153. % FJW. Bug fix!
  154. % w := aeval{'int, mk!*sq w, caddr cadr u} . cddr u;
  155. w := 'df . reval{'int, mk!*sq w, caddr cadr u} . cddr u;
  156. go to j >>; % derivative absorbed
  157. if (x := find_sub_df(w:= cadr u . derad(v,cddr u),
  158. get('df,'kvalue)))
  159. then <<w := simp car x;
  160. for each el in cdr x do
  161. for i := 1:cdr el do
  162. w := diffsq(w,car el);
  163. go to e>>
  164. else w := 'df . w
  165. >>
  166. else if null !*depend then return nil ./ 1
  167. else w := {'df,u,v}
  168. else w := {'df,u,v};
  169. j: if (x := opmtch w) then w := simp x
  170. else if not depends(u,v) and null !*depend then return nil ./ 1
  171. else w := mksq(w,1);
  172. go to e
  173. end$
  174. % Author: Francis J. Wright <F.J.Wright@QMW.ac.uk>
  175. % Last revised: 27 December 1997
  176. symbolic procedure dfform_int(u, v, n);
  177. % Simplify a SINGLE derivative of an integral.
  178. % u = '(int y x) [as main variable of SQ form]
  179. % v = kernel
  180. % n = integer power
  181. % Return SQ form of df(u**n, v) = n*u**(n-1)*df(u, v)
  182. % This routine is called by diffp via the hook
  183. % "if x := get(car u,'dfform) then return apply3(x,u,v,n)".
  184. % It does not necessarily need to use this hook, but it needs to be
  185. % called as an alternative to diffp so that the linearity of
  186. % differentiation has already been applied.
  187. begin scalar result, x, y;
  188. y := simp!* cadr u; % SQ form integrand
  189. x := caddr u; % kernel
  190. result :=
  191. if v eq x then y
  192. % df(int(y,x), x) -> y replacing the let rule in INT.RED
  193. else if not !*intflag!* and % not in the integrator
  194. % If used in the integrator it can cause infinite loops,
  195. % e.g. in df(int(int(f,x),y),x) and df(int(int(f,x),y),y)
  196. !*allowdfint and % must be on for dfint to work
  197. << y := diffsq(y, v); !*dfint or not_df_p y >>
  198. % it has simplified
  199. then simp{'int, mk!*sq y, x} % MUST re-simplify it!!!
  200. % i.e. differentiate under the integral sign
  201. % df(int(y, x), v) -> int(df(y, v), x).
  202. % (Perhaps I should use prepsq - kernels are normally true prefix?)
  203. else !*kk2q{'df, u, v}; % remain unchanged
  204. if not(n eq 1) then
  205. result := multsq( (((u .** (n-1)) .* n) .+ nil) ./ 1, result);
  206. return result
  207. end$
  208. symbolic procedure not_df_p y;
  209. % True if the SQ form y is not a df kernel.
  210. not(denr y eq 1 and
  211. not domainp (y := numr y) and eqcar(mvar y, 'df))$
  212. endmodule$
  213. module intdf$
  214. % Patch to simpint1 in src/int/trans/driver.red to provide better
  215. % simplification of integrals of derivatives. (I think -- hope --
  216. % this is the right place to hook this patch into the integrator!)
  217. % This patch was motivated by the needs of crack.
  218. % F.J.Wright@Maths.QMW.ac.uk, 31 December 1997
  219. %% load_package int$
  220. %apply1('load!-package, 'int)$ % not at compile time!
  221. switch PartialIntDf$ % off by default
  222. deflist('((PartialIntDf ((t (rmsubs))))), 'simpfg)$
  223. % If the switch PartialIntDf is turned on then integration by parts is
  224. % performed if the result simplifies in the sense that it integrates a
  225. % symbolic derivative and does not introduce new symbolic derivatives.
  226. % However, because the initial integral contains an unevaluated
  227. % derivative then the result must still contain an unevaluated
  228. % integral.
  229. symbolic procedure simpint1 u;
  230. % Varstack* rebound, since FORMLNR use can create recursive
  231. % evaluations. (E.g., with int(cos(x)/x**2,x)).
  232. begin scalar !*keepsqrts,v,varstack!*;
  233. u := 'int . prepsq car u . cdr u;
  234. if (v := formlnr u) neq u
  235. then if !*nolnr
  236. then <<v := simp subst('int!*,'int,v);
  237. return remakesf numr v ./ remakesf denr v>>
  238. else <<!*nolnr := nil . !*nolnr;
  239. v:=errorset!*(list('simp,mkquote v),!*backtrace);
  240. if pairp v then v := car v else v := simp u;
  241. !*nolnr := cdr !*nolnr;
  242. return v>>;
  243. % FJW: At this point linearity has been applied.
  244. return if (v := opmtch u) then simp v
  245. % FJW: Check for a directly integrable derivative:
  246. else if (v := NestedIntDf(cadr u, caddr u)) then mksq(v,1)
  247. else if !*failhard then rerror(int,4,"FAILHARD switch set")
  248. % FJW: Integrate by parts if the result simplifies:
  249. else if !*PartialIntDf and
  250. (v := PartialIntDf(cadr u, caddr u)) then mksq(v,1)
  251. else mksq(u,1)
  252. end$
  253. symbolic procedure NestedIntDf(y, x);
  254. %% int( ... df(f,A,x,B) ..., x) -> ... df(f,A,B) ...
  255. %% Find a df(f,A,x,B) among possibly nested int's and df's within
  256. %% the integrand y in int(y,x), and return the whole structure y
  257. %% but with the derivative integrated; otherwise return nil.
  258. %% [A,B are arbitrary sequences of kernels.]
  259. not atom y and
  260. begin scalar car_y, nested;
  261. return
  262. if (car_y := car y) eq 'df and memq(x, cddr y) then
  263. %% int( df(f, A, x, B), x ) -> df(f, A, B)
  264. 'df . cadr y . delete(x, cddr y)
  265. %% use delete for portability!
  266. %% deleq is defined in CSL, delq in PSL -- oops!
  267. else if memq(car_y, '(df int)) and
  268. (nested := NestedIntDf(cadr y, x)) then
  269. %% int( df(int(df(f, A, x, B), c), C), x ) ->
  270. %% df(int(df(f, A, B), c), C)
  271. %% int( int(df(f, A, x, B), c), x ) ->
  272. %% int(df(f, A, B), c)
  273. car_y . nested . cddr y
  274. end$
  275. symbolic procedure PartialIntDf(y, x);
  276. %% int(u(x)*df(v(x),x), x) -> u(x)*v(x) - int(df(u(x),x)*v(x), x)
  277. %% Integrate by parts if the resulting integral simplifies [to
  278. %% avoid infinite loops], which means that df(u(x),x) may not
  279. %% contain any unevaluated derivatives; otherwise return nil.
  280. not atom y and
  281. begin scalar denlist, facs, df, u, v;
  282. if car y eq 'quotient then <<
  283. denlist := cddr y;
  284. % y := numerator:
  285. if atom(y := cadr y) then return % no derivative
  286. >>;
  287. % y := list of factors:
  288. if car y eq 'times then y := cdr y
  289. else if denlist then y := y . nil
  290. else return;
  291. % Find an integrable derivative among the factors:
  292. facs := y;
  293. while facs and not
  294. (eqcar(df := car facs, 'df) and memq(x, cddr df)) do
  295. facs := cdr facs;
  296. if null facs then return; % no integrable derivative
  297. % Construct u(x) and v(x) [v(x) may still be a derivative]:
  298. u := delete(df, y); % list of factors
  299. u := if null u then 1 else if cdr u then 'times . u else car u;
  300. if denlist then u := 'quotient . u . denlist;
  301. v := cadr df; % kernel being differentiated
  302. if (df := delete(x, cddr df)) then v := 'df . v . df;
  303. % Check that df(u(x),x) simplifies:
  304. if smemq('df, df := reval {'df,u,x}) then return;
  305. return reval {'difference,
  306. {'times,u,v}, {'int, {'times, df, v}, x}}
  307. end$
  308. endmodule$
  309. end$