dirac.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //-----------------------------------------------------------------------------
  2. //
  3. // Author : philippe.billet@noos.fr
  4. //
  5. // Dirac function dirac(x)
  6. // dirac(-x)=dirac(x)
  7. // dirac(b-a)=dirac(a-b)
  8. //-----------------------------------------------------------------------------
  9. #include "stdafx.h"
  10. #include "defs.h"
  11. static void ydirac(void);
  12. void
  13. eval_dirac(void)
  14. {
  15. push(cadr(p1));
  16. eval();
  17. dirac();
  18. }
  19. void
  20. dirac(void)
  21. {
  22. save();
  23. ydirac();
  24. restore();
  25. }
  26. #define X p1
  27. static void
  28. ydirac(void)
  29. {
  30. X = pop();
  31. if (isdouble(X)) {
  32. if (X->u.d == 0)
  33. {push_integer(1);
  34. return;}
  35. else
  36. {push_integer(0);
  37. return;}
  38. }
  39. if (isrational(X)) {
  40. if (MZERO(mmul(X->u.q.a,X->u.q.b)))
  41. {push_integer(1);
  42. return;}
  43. else
  44. {push_integer(0);
  45. return;}
  46. }
  47. if (car(X) == symbol(POWER)) {
  48. push_symbol(DIRAC);
  49. push(cadr(X));
  50. list(2);
  51. return;
  52. }
  53. if (isnegativeterm(X)) {
  54. push_symbol(DIRAC);
  55. push(X);
  56. negate();
  57. list(2);
  58. return;
  59. }
  60. if (isnegativeterm(p1) || (car(p1) == symbol(ADD) && isnegativeterm(cadr(p1)))) {
  61. push(p1);
  62. negate();
  63. p1 = pop();
  64. }
  65. push_symbol(DIRAC);
  66. push(X);
  67. list(2);
  68. }
  69. #if SELFTEST
  70. static char *s[] = {
  71. "dirac(-x)",
  72. "dirac(x)",
  73. };
  74. void
  75. test_dirac(void)
  76. {
  77. test(__FILE__, s, sizeof s / sizeof (char *));
  78. }
  79. #endif