csolve.red 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. module csolve; % routines to do with the C constants.
  2. % Author: John P. Fitch.
  3. fluid '(!*trint ccount cmap cmatrix cval loglist neweqn);
  4. exports backsubst4cs,createcmap,findpivot,printvecsq, % printspreadc
  5. spreadc,subst4eliminateds;
  6. imports nth,interr,!*multf,printsf,printsq,quotf,putv,negf,invsq,
  7. negsq,addsq,multsq,mksp,addf,domainp,pnth;
  8. symbolic procedure findpivot cvec;
  9. % Finds first non-zero element in CVEC and returns its cell number.
  10. % If no such element exists, result is nil.
  11. begin scalar i,x;
  12. i:=1;
  13. x:=getv(cvec,i);
  14. while i<ccount and null x do
  15. << i:=i+1;
  16. x:=getv(cvec,i) >>;
  17. if null x then return nil;
  18. return i
  19. end;
  20. symbolic procedure subst4eliminatedcs(neweqn,substorder,ceqns);
  21. % Substitutes into NEWEQN for all the C's that have been eliminated so
  22. % far. These are given by CEQNS. SUBSTORDER gives the order of
  23. % substitution as well as the constant multipliers. Result is the
  24. % transformed NEWEQN.
  25. if null substorder then neweqn
  26. else begin scalar nxt,row,cvar,temp;
  27. row:=car ceqns;
  28. nxt:=car substorder;
  29. if null (cvar:=getv(neweqn,nxt)) then
  30. return subst4eliminatedcs(neweqn,cdr substorder,cdr ceqns);
  31. nxt:=getv(row,nxt);
  32. for i:=0 : ccount do
  33. << temp:=!*multf(nxt,getv(neweqn,i));
  34. temp:=addf(temp,negf !*multf(cvar,getv(row,i)));
  35. putv(neweqn,i,temp) >>;
  36. return subst4eliminatedcs(neweqn,cdr substorder,cdr ceqns)
  37. end;
  38. symbolic procedure backsubst4cs(cs2subst,cs2solve,cmatrix);
  39. % Solves the C-eqns and sets vector CVAL to the C-constant values
  40. % CMATRIX is a list of matrix rows for C-eqns after Gaussian
  41. % elimination has been performed. CS2SOLVE is a list of the remaining
  42. % C's to evaluate and CS2SUBST are the C's we have evaluated already.
  43. if null cmatrix then nil
  44. else begin scalar eqnn,cvar,already,substlist,temp,temp2;
  45. eqnn:=car cmatrix;
  46. cvar:=car cs2solve;
  47. already:=nil ./ 1; % The S.Q. nil.
  48. substlist:=cs2subst;
  49. % Now substitute for previously evaluated c's:
  50. while not null substlist do
  51. << temp:=car substlist;
  52. if not null getv(eqnn,temp) then
  53. already:=addsq(already,multsq(getv(eqnn,temp) ./ 1,
  54. getv(cval,temp)));
  55. substlist:=cdr substlist >>;
  56. % Now solve for the c given by cvar (any remaining c's assumed zero).
  57. temp:=negsq addsq(getv(eqnn,0) ./ 1,already);
  58. if not null (temp2:=quotf(numr temp,getv(eqnn,cvar))) then
  59. temp:=temp2 ./ denr temp
  60. else temp:=multsq(temp,invsq(getv(eqnn,cvar) ./ 1));
  61. if not null numr temp then putv(cval,cvar,
  62. resimp rootextractsq subs2q temp);
  63. backsubst4cs(reversip(cvar . reversip cs2subst),
  64. cdr cs2solve,cdr cmatrix)
  65. end;
  66. %**********************************************************************
  67. % Routines to deal with linear equations for the constants C.
  68. %**********************************************************************
  69. symbolic procedure createcmap;
  70. %Sets LOGLIST to list of things of form (LOG C-constant f), where f is
  71. % function linear in one of the z-variables and C-constant is in S.F.
  72. % When creating these C-constant names, the CMAP is also set up and
  73. % returned as the result.
  74. begin scalar i,l,c;
  75. l:=loglist;
  76. i:=1;
  77. while not null l do <<
  78. c:=(int!-gensym1('c) . i) . c;
  79. i:=i+1;
  80. rplacd(car l,((mksp(caar c,1) .* 1) .+ nil) . cdar l);
  81. l:=cdr l >>;
  82. if !*trint
  83. then printc ("Constants Created for log and tan terms:" . c);
  84. return c
  85. end;
  86. symbolic procedure spreadc(eqnn,cvec1,w);
  87. % Sets a vector 'cvec1' to coefficients of c<i> in eqnn.
  88. if domainp eqnn then putv(cvec1,0,addf(getv(cvec1,0),
  89. !*multf(eqnn,w)))
  90. else begin scalar mv,t1,t2;
  91. spreadc(red eqnn,cvec1,w);
  92. mv:=mvar eqnn;
  93. t1:=assoc(mv,cmap); %tests if it is a c var.
  94. if not null t1 then return <<
  95. t1:=cdr t1; %loc in vector for this c.
  96. if not (tdeg lt eqnn=1) then interr "Not linear in c eqn";
  97. t2:=addf(getv(cvec1,t1),!*multf(w,lc eqnn));
  98. putv(cvec1,t1,t2) >>;
  99. t1:=((lpow eqnn) .* 1) .+ nil; %this main var as sf.
  100. spreadc(lc eqnn,cvec1,!*multf(w,t1))
  101. end;
  102. % symbolic procedure printspreadc cvec1;
  103. % begin
  104. % for i:=0 : ccount do <<
  105. % prin2 i;
  106. % printc ":";
  107. % printsf(getv(cvec1,i)) >>;
  108. % printc "End of printspreadc output"
  109. % end;
  110. % symbolic procedure printvecsq cvec;
  111. % % Print contents of cvec which contains s.q.'s (not s.f.'s).
  112. % % Starts from cell 1 not 0 as above routine (printspreadc).
  113. % begin
  114. % for i:=1 : ccount do <<
  115. % prin2 i;
  116. % printc ":";
  117. % if null getv(cvec,i) then printc "0"
  118. % else printsq(getv(cvec,i)) >>;
  119. % printc "End of printvecsq output"
  120. % end;
  121. endmodule;
  122. end;