numeric.red 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. module numeric; % Header module for the numeric package and
  2. % support of numerical evaluation of symbolic
  3. % expressions.
  4. % Author: Herbert Melenk.
  5. % Copyright (c) 1993 ZIB Berlin, RAND. All rights reserved.
  6. create!-package('(numeric numeval numsolve gauss newton steepstd
  7. bounds numint numfit chebysh rungeku),
  8. '(contrib numeric));
  9. fluid '(!*noequiv);
  10. fluid '(accuracy!*);
  11. global '(iterations!* !*trnumeric);
  12. switch trnumeric;
  13. % Create .. as infix operator.
  14. newtok '( (!. !.) !*interval!*);
  15. if null get('!*interval!*,'simpfn) then
  16. <<precedence .., or;
  17. algebraic operator ..;
  18. put('!*interval!*,'prtch,'! !.!.! );
  19. >>;
  20. % some common utilities
  21. fluid '(minus!-infinity!*);
  22. minus!-infinity!* := '(minus infinity);
  23. % intervals
  24. symbolic procedure adomainp u;
  25. numberp u or (pairp u and idp car u and get(car u,'dname));
  26. symbolic procedure revalnuminterval(u,num);
  27. % Evaluate u as interval; numeric bounds required if num=T.
  28. begin scalar l;
  29. if not eqcar(u,'!*interval!*) then typerr(u,"interval");
  30. l:={reval cadr u,reval caddr u};
  31. if adomainpx(car l,num) and adomainpx(cadr l,num)then return l;
  32. typerr(u,"numeric interval");
  33. end;
  34. symbolic procedure adomainpx(u,num);
  35. % extended algebraic domainp test:
  36. % num = t: u is a domain element;
  37. % num = inf: u is a domain element or inf or (minus inf)
  38. % num = nil: u is arbitrary.
  39. null num or adomainp u or num='infinity
  40. and member(u,'(infinity (minus infinity)));
  41. symbolic procedure evalgreaterpx(a,b);
  42. if a =minus!-infinity!* or b = 'infinity then nil else
  43. a='infinity or b=minus!-infinity!* or evalgreaterp(a,b);
  44. symbolic procedure mkinterval(u,v);
  45. list('!*interval!*,u,v);
  46. % Easy coding of numerical procedures with REDUCE:
  47. %
  48. % In statements or procedure bodies tagged with "dm:" all
  49. % arithmetic function calls are replaced by REDUCE domain
  50. % arithmetic.
  51. symbolic macro procedure dm!: u;
  52. subla('((plus2 . !:plus)(times2 . !:times)
  53. (plus . !:plusn)(times . !:timesn)
  54. (quotient . !:!:quotient)
  55. (difference . !:difference)
  56. (minus . !:minus)
  57. (minusp . !:minusp)
  58. (zerop . !:zerop)
  59. (lessp . (lambda(a b)(!:minusp (!:difference a b))))
  60. (greaterp . (lambda(a b)(!:minusp (!:difference b a))))
  61. (leq . (lambda(a b)(not (!:minusp (!:difference b a)))))
  62. (geq . (lambda(a b)(not (!:minusp (!:difference a b)))))
  63. (sqrt . num!-sqrtf)
  64. (abs . absf)
  65. (min2 . dm!:min)
  66. (max2 . dm!:max)
  67. (min . dm!:min)
  68. (max . dm!:max)
  69. ) , cadr u);
  70. %wrappers for n-ary plus and times
  71. symbolic macro procedure !:plusn u;
  72. if null cddr u then cadr u else
  73. list('!:plus,cadr u,'!:plusn . cddr u);
  74. symbolic macro procedure !:timesn u;
  75. if null cddr u then cadr u else
  76. list('!:times,cadr u,'!:timesn . cddr u);
  77. endmodule;
  78. end;