pade.red 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. module pade; % Pade' Approximations.
  2. % Author: Lisa Temme
  3. % Date: 15/6/95.
  4. algebraic;
  5. load taylor;
  6. load solve;
  7. %**************
  8. %%Include a boolean function to check for taylor expression
  9. procedure taylorp(x);
  10. lisp eqcar(x,'taylor);
  11. %% Input my code for the Pade Function
  12. procedure pade(f, x, h, n, d);
  13. % f is function to be approximated
  14. % x is function variable
  15. % h is point at which approximation is evaluated
  16. % n is degree (wanted) of numerator of rational function approximation
  17. % d is degree (wanted) of denominator of rational function approximation
  18. begin
  19. scalar y,g,numer,denom, num_var_list, den_var_list, variable_list,
  20. tay_expsn,tay_output,poly_taylor,coeff_list,j, k, kk, a, b,
  21. new_list,answer,part_answer,count,zero_check_list,p,q,r;
  22. %check to see if input is rational
  23. %if so larger degrees of n & d will return input
  24. if type_ratpoly(f,x) AND deg(num f,x)<=n AND deg(den f,x)<=d
  25. then return f
  26. else
  27. << y := lisp gensym(); %declare y as local variable
  28. lisp(a:= gensym()); %\ declare
  29. lisp(b:= gensym()); % | a and b
  30. lisp eval list ('operator,mkquote list a); % | as local
  31. lisp eval list ('operator,mkquote list b); %/ operators
  32. g := sub(x=y+h,f); %rewrite f in terms of y at 0
  33. numer := for k:=0:n sum a(k)*y^k;
  34. denom := for j:=0:d sum b(j)*y^j;
  35. num_var_list := for k:=0:n collect a(k);
  36. den_var_list := for j:=0:d collect b(j);
  37. variable_list := append(num_var_list,den_var_list);
  38. tay_expsn := taylor(g, y,0,n+d);
  39. tay_output := taylortostandard(tay_expsn);
  40. if NOT(freeof(tay_output,df)) then rederr "not yet implemented"
  41. %Some Taylor Expansions do not exist at present.
  42. else
  43. << poly_taylor :=
  44. denom*(num tay_output) - numer*(den tay_output);
  45. coeff_list := COEFF(poly_taylor,y);
  46. if (n+d+1)>length(coeff_list)
  47. %Only consider first n+d+1 coefficients at most.
  48. then new_list := coeff_list
  49. else new_list :=
  50. for kk:= 1:n+d+1 collect part(coeff_list,kk);
  51. part_answer := solve(new_list,variable_list);
  52. count :=0;
  53. zero_check_list :=
  54. for each r in
  55. (for each q in
  56. (for p:=n+2:n+d+2 collect part(first part_answer,p))
  57. collect part(q,2))
  58. do <<if r=0 then count:=count+1>>;
  59. %if all the coefficients of the denominator are zero
  60. if count=d+1
  61. then rederr "Pade Approximation of this order does not exist"
  62. else
  63. << answer:= sub(part_answer, numer/denom);
  64. %if Pade would be returned as a Taylor expansion
  65. if taylorp answer
  66. then rederr "no Pade Approximation exists"
  67. %following commented out as not sure it is necessary
  68. % else
  69. % << if length answer=0
  70. % then
  71. % rederr "Pade Approximation of this order does not exist"
  72. else return sub(y=x,answer)
  73. % >>;
  74. >>;
  75. >>;
  76. >>;
  77. end;
  78. endmodule;
  79. end;