expr.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* This file is part of the GNU plotutils package. */
  2. /*
  3. *
  4. * Copyright (C) 1982-1994, Nicholas B. Tufillaro. All rights reserved.
  5. *
  6. * GNU enhancements Copyright (C) 1996, 1997, 1998, 1999, 2005, 2008, Free
  7. * Software Foundation, Inc.
  8. */
  9. /*
  10. * expression tree evaluator and expression space management routines
  11. */
  12. #include "sys-defines.h"
  13. #include "ode.h"
  14. #include "extern.h"
  15. #define NSTACK 30
  16. double stack[NSTACK];
  17. double
  18. eval (const struct expr *ep)
  19. {
  20. double *sp;
  21. double tmp, tmp2;
  22. for (sp = &stack[NSTACK]; ep != NULL; ep = ep->ex_next)
  23. {
  24. #ifdef DEBUG
  25. if (sp > &stack[NSTACK])
  26. panic ("expression stack underflow");
  27. #endif
  28. if (sp <= stack)
  29. panic ("stack overflow -- bump NSTACK and recompile");
  30. switch (ep->ex_oper)
  31. {
  32. case O_CONST:
  33. *--sp = ep->ex_value;
  34. break;
  35. case O_IDENT:
  36. *--sp = ep->ex_sym->sy_value;
  37. break;
  38. case O_PLUS:
  39. tmp = *sp++;
  40. *sp += tmp;
  41. break;
  42. case O_MINUS:
  43. tmp = *sp++;
  44. *sp -= tmp;
  45. break;
  46. case O_MULT:
  47. tmp = *sp++;
  48. *sp *= tmp;
  49. break;
  50. case O_DIV:
  51. tmp = *sp++;
  52. *sp /= tmp;
  53. break;
  54. case O_POWER:
  55. tmp = *sp++;
  56. if ((tmp != (int)tmp) && (*sp < 0))
  57. rterror("negative number to non-integer power");
  58. *sp = pow(*sp,tmp);
  59. break;
  60. case O_SQAR:
  61. *sp *= *sp;
  62. break;
  63. case O_CUBE:
  64. *sp *= *sp * *sp;
  65. break;
  66. case O_INV:
  67. *sp = 1. / *sp;
  68. break;
  69. case O_SQRT:
  70. if (*sp < 0)
  71. rterror("square root of a negative number");
  72. *sp = sqrt(*sp);
  73. break;
  74. case O_SIN:
  75. *sp = sin(*sp);
  76. break;
  77. case O_COS:
  78. *sp = cos(*sp);
  79. break;
  80. case O_TAN:
  81. *sp = tan(*sp);
  82. break;
  83. case O_ASIN:
  84. *sp = asin(*sp);
  85. break;
  86. case O_ACOS:
  87. *sp = acos(*sp);
  88. break;
  89. case O_ATAN:
  90. *sp = atan(*sp);
  91. break;
  92. case O_ABS:
  93. if (*sp < 0)
  94. *sp = -*sp;
  95. break;
  96. case O_EXP:
  97. *sp = exp(*sp);
  98. break;
  99. case O_LOG:
  100. if (*sp <= 0)
  101. rterror("logarithm of non-positive number");
  102. *sp = log(*sp);
  103. break;
  104. case O_LOG10:
  105. if (*sp <= 0)
  106. rterror("logarithm of non-positive number");
  107. *sp = log10(*sp);
  108. break;
  109. case O_SINH:
  110. *sp = sinh(*sp);
  111. break;
  112. case O_COSH:
  113. *sp = cosh(*sp);
  114. break;
  115. case O_TANH:
  116. *sp = tanh(*sp);
  117. break;
  118. case O_ASINH:
  119. *sp = asinh(*sp);
  120. break;
  121. case O_ACOSH:
  122. *sp = acosh(*sp);
  123. break;
  124. case O_ATANH:
  125. *sp = atanh(*sp);
  126. break;
  127. case O_FLOOR:
  128. *sp = floor(*sp);
  129. break;
  130. case O_CEIL:
  131. *sp = ceil(*sp);
  132. break;
  133. case O_J0:
  134. *sp = j0(*sp);
  135. break;
  136. case O_J1:
  137. *sp = j1(*sp);
  138. break;
  139. case O_Y0:
  140. *sp = y0(*sp);
  141. break;
  142. case O_GAMMA:
  143. *sp = f_gamma(*sp);
  144. break;
  145. case O_LGAMMA:
  146. *sp = F_LGAMMA(*sp);
  147. break;
  148. case O_ERFC:
  149. *sp = erfc(*sp);
  150. break;
  151. case O_ERF:
  152. *sp = erf(*sp);
  153. break;
  154. case O_INVERF:
  155. *sp = inverf(*sp);
  156. break;
  157. case O_NORM:
  158. *sp = norm(*sp);
  159. break;
  160. case O_INVNORM:
  161. *sp = invnorm(*sp);
  162. break;
  163. case O_NEG:
  164. *sp = -*sp;
  165. break;
  166. case O_IGAMMA:
  167. tmp = *sp++;
  168. *sp = igamma(*sp, tmp);
  169. break;
  170. case O_IBETA:
  171. tmp2 = *sp++;
  172. tmp = *sp++;
  173. *sp = ibeta(*sp, tmp, tmp2);
  174. break;
  175. default:
  176. panicn ("bad op spec (%d) in eval()", (int)(ep->ex_oper));
  177. }
  178. }
  179. return *sp;
  180. }
  181. struct expr *
  182. ealloc (void)
  183. {
  184. struct expr *ep;
  185. ep = (struct expr *)xmalloc (sizeof(struct expr));
  186. ep->ex_next = NULL;
  187. ep->ex_sym = NULL;
  188. ep->ex_oper = O_NOOP; /* default */
  189. return ep;
  190. }
  191. void
  192. efree (struct expr *ep)
  193. {
  194. if (ep == NULL || ep == &exprzero || ep == &exprone)
  195. return;
  196. efree (ep->ex_next);
  197. free ((void *)ep);
  198. }