home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume8 / gnuplot1.10A / part04 / standard.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-09  |  5.8 KB  |  360 lines

  1. /*
  2.  *
  3.  *    G N U P L O T  --  standard.c
  4.  *
  5.  *  Copyright (C) 1986, 1987  Thomas Williams, Colin Kelley
  6.  *
  7.  *  You may use this code as you wish if credit is given and this message
  8.  *  is retained.
  9.  *
  10.  *  Please e-mail any useful additions to vu-vlsi!plot so they may be
  11.  *  included in later releases.
  12.  *
  13.  *  This file should be edited with 4-column tabs!  (:set ts=4 sw=4 in vi)
  14.  */
  15.  
  16. #include <math.h>
  17. #include <stdio.h>
  18. #include "plot.h"
  19.  
  20. extern BOOLEAN undefined;
  21.  
  22. #ifdef vms
  23. #include <errno.h>
  24. #else
  25. extern int errno;
  26. #endif /* vms */
  27.  
  28.  
  29. extern struct value stack[STACK_DEPTH];
  30. extern int s_p;
  31.  
  32. struct value *pop(), *complex(), *integer();
  33.  
  34. double magnitude(), angle(), real(), imag();
  35.  
  36.  
  37. f_real()
  38. {
  39. struct value a;
  40.     push( complex(&a,real(pop(&a)), 0.0) );
  41. }
  42.  
  43. f_imag()
  44. {
  45. struct value a;
  46.     push( complex(&a,imag(pop(&a)), 0.0) );
  47. }
  48.  
  49. f_arg()
  50. {
  51. struct value a;
  52.     push( complex(&a,angle(pop(&a)), 0.0) );
  53. }
  54.  
  55. f_conjg()
  56. {
  57. struct value a;
  58.     (void) pop(&a);
  59.     push( complex(&a,real(&a),-imag(&a) ));
  60. }
  61.  
  62. f_sin()
  63. {
  64. struct value a;
  65.     (void) pop(&a);
  66.     push( complex(&a,sin(real(&a))*cosh(imag(&a)), cos(real(&a))*sinh(imag(&a))) );
  67. }
  68.  
  69. f_cos()
  70. {
  71. struct value a;
  72.     (void) pop(&a);
  73.     push( complex(&a,cos(real(&a))*cosh(imag(&a)), -sin(real(&a))*sinh(imag(&a))));
  74. }
  75.  
  76. f_tan()
  77. {
  78. struct value a;
  79. register double den;
  80.     (void) pop(&a);
  81.     if (imag(&a) == 0.0)
  82.         push( complex(&a,tan(real(&a)),0.0) );
  83.     else {
  84.         den = cos(2*real(&a))+cosh(2*imag(&a));
  85.         if (den == 0.0) {
  86.             undefined = TRUE;
  87.             push( &a );
  88.         }
  89.         else
  90.             push( complex(&a,sin(2*real(&a))/den, sinh(2*imag(&a))/den) );
  91.     }
  92. }
  93.  
  94. f_asin()
  95. {
  96. struct value a;
  97. register double alpha, beta, x, y;
  98.     (void) pop(&a);
  99.     x = real(&a); y = imag(&a);
  100.     if (y == 0.0) {
  101.         if (fabs(x) > 1.0) {
  102.             undefined = TRUE;
  103.             push(complex(&a,0.0, 0.0));
  104.         } else
  105.             push( complex(&a,asin(x),0.0) );
  106.     } else {
  107.         beta  = sqrt((x + 1)*(x + 1) + y*y)/2 - sqrt((x - 1)*(x - 1) + y*y)/2;
  108.         alpha = sqrt((x + 1)*(x + 1) + y*y)/2 + sqrt((x - 1)*(x - 1) + y*y)/2;
  109.         push( complex(&a,asin(beta), log(alpha + sqrt(alpha*alpha-1))) );
  110.     }
  111. }
  112.  
  113. f_acos()
  114. {
  115. struct value a;
  116. register double alpha, beta, x, y;
  117.     (void) pop(&a);
  118.     x = real(&a); y = imag(&a);
  119.     if (y == 0.0) {
  120.         if (fabs(x) > 1.0) {
  121.             undefined = TRUE;
  122.             push(complex(&a,0.0, 0.0));
  123.         } else
  124.             push( complex(&a,acos(x),0.0) );
  125.     } else {
  126.         alpha = sqrt((x + 1)*(x + 1) + y*y)/2 + sqrt((x - 1)*(x - 1) + y*y)/2;
  127.         beta  = sqrt((x + 1)*(x + 1) + y*y)/2 - sqrt((x - 1)*(x - 1) + y*y)/2;
  128.         push( complex(&a,acos(beta), log(alpha + sqrt(alpha*alpha-1))) );
  129.     }
  130. }
  131.  
  132. f_atan()
  133. {
  134. struct value a;
  135. register double x, y;
  136.     (void) pop(&a);
  137.     x = real(&a); y = imag(&a);
  138.     if (y == 0.0)
  139.         push( complex(&a,atan(x), 0.0) );
  140.     else if (x == 0.0 && fabs(y) == 1.0) {
  141.         undefined = TRUE;
  142.         push(complex(&a,0.0, 0.0));
  143.     } else
  144.         push( complex(&a,atan(2*x/(1-x*x-y*y)),
  145.                 log((x*x+(y+1)*(y+1))/(x*x+(y-1)*(y-1)))/4) );
  146. }
  147.  
  148. f_sinh()
  149. {
  150. struct value a;
  151.     (void) pop(&a);
  152.     push( complex(&a,sinh(real(&a))*cos(imag(&a)), cosh(real(&a))*sin(imag(&a))) );
  153. }
  154.  
  155. f_cosh()
  156. {
  157. struct value a;
  158.     (void) pop(&a);
  159.     push( complex(&a,cosh(real(&a))*cos(imag(&a)), sinh(real(&a))*sin(imag(&a))) );
  160. }
  161.  
  162. f_tanh()
  163. {
  164. struct value a;
  165. register double den;
  166.     (void) pop(&a);
  167.     den = cosh(2*real(&a)) + cos(2*imag(&a));
  168.     push( complex(&a,sinh(2*real(&a))/den, sin(2*imag(&a))/den) );
  169. }
  170.  
  171. f_int()
  172. {
  173. struct value a;
  174.     push( integer(&a,(int)real(pop(&a))) );
  175. }
  176.  
  177.  
  178. f_abs()
  179. {
  180. struct value a;
  181.     (void) pop(&a);
  182.     switch (a.type) {
  183.         case INT:
  184.             push( integer(&a,abs(a.v.int_val)) );            
  185.             break;
  186.         case CMPLX:
  187.             push( complex(&a,magnitude(&a), 0.0) );
  188.     }
  189. }
  190.  
  191. f_sgn()
  192. {
  193. struct value a;
  194.     (void) pop(&a);
  195.     switch(a.type) {
  196.         case INT:
  197.             push( integer(&a,(a.v.int_val > 0) ? 1 : 
  198.                     (a.v.int_val < 0) ? -1 : 0) );
  199.             break;
  200.         case CMPLX:
  201.             push( integer(&a,(a.v.cmplx_val.real > 0.0) ? 1 : 
  202.                     (a.v.cmplx_val.real < 0.0) ? -1 : 0) );
  203.             break;
  204.     }
  205. }
  206.  
  207.  
  208. f_sqrt()
  209. {
  210. struct value a;
  211. register double mag, ang;
  212.     (void) pop(&a);
  213.     mag = sqrt(magnitude(&a));
  214.     if (imag(&a) == 0.0 && real(&a) < 0.0)
  215.         push( complex(&a,0.0,mag) );
  216.     else
  217.     {
  218.         if ( (ang = angle(&a)) < 0.0)
  219.             ang += 2*Pi;
  220.         ang /= 2;
  221.         push( complex(&a,mag*cos(ang), mag*sin(ang)) );
  222.     }
  223. }
  224.  
  225.  
  226. f_exp()
  227. {
  228. struct value a;
  229. register double mag, ang;
  230.     (void) pop(&a);
  231.     mag = exp(real(&a));
  232.     ang = imag(&a);
  233.     push( complex(&a,mag*cos(ang), mag*sin(ang)) );
  234. }
  235.  
  236.  
  237. f_log10()
  238. {
  239. struct value a;
  240. register double l10;;
  241.     (void) pop(&a);
  242.     l10 = log(10.0);    /***** replace with a constant! ******/
  243.     push( complex(&a,log(magnitude(&a))/l10, angle(&a)/l10) );
  244. }
  245.  
  246.  
  247. f_log()
  248. {
  249. struct value a;
  250.     (void) pop(&a);
  251.     push( complex(&a,log(magnitude(&a)), angle(&a)) );
  252. }
  253.  
  254.  
  255. f_besj0()    /* j0(a) = sin(a)/a */
  256. {
  257. struct value a;
  258.     a = top_of_stack;
  259.     f_sin();
  260.     push(&a);
  261.     f_div();
  262. }
  263.  
  264.  
  265. f_besj1()    /* j1(a) = sin(a)/(a**2) - cos(a)/a */
  266. {
  267. struct value a;
  268.     a = top_of_stack;
  269.     f_sin();
  270.     push(&a);
  271.     push(&a);
  272.     f_mult();
  273.     f_div();
  274.     push(&a);
  275.     f_cos();
  276.     push(&a);
  277.     f_div();
  278.     f_minus();
  279. }
  280.  
  281.  
  282. f_besy0()    /* y0(a) = -cos(a)/a */
  283. {
  284. struct value a;
  285.     a = top_of_stack;
  286.     f_cos();
  287.     push(&a);
  288.     f_div();
  289.     f_uminus();
  290. }
  291.  
  292.  
  293. f_besy1()    /* y1(a) = -cos(a)/(a**2) - sin(a)/a */
  294. {
  295. struct value a;
  296.  
  297.     a = top_of_stack;
  298.     f_cos();
  299.     push(&a);
  300.     push(&a);
  301.     f_mult();
  302.     f_div();
  303.     push(&a);
  304.     f_sin();
  305.     push(&a);
  306.     f_div();
  307.     f_plus();
  308.     f_uminus();
  309. }
  310.  
  311.  
  312. f_floor()
  313. {
  314. struct value a;
  315.  
  316.     (void) pop(&a);
  317.     switch (a.type) {
  318.         case INT:
  319.             push( integer(&a,(int)floor((double)a.v.int_val)));            
  320.             break;
  321.         case CMPLX:
  322.             push( complex(&a,floor(a.v.cmplx_val.real),
  323.                 floor(a.v.cmplx_val.imag)) );
  324.     }
  325. }
  326.  
  327.  
  328. f_ceil()
  329. {
  330. struct value a;
  331.  
  332.     (void) pop(&a);
  333.     switch (a.type) {
  334.         case INT:
  335.             push( integer(&a,(int)ceil((double)a.v.int_val)));            
  336.             break;
  337.         case CMPLX:
  338.             push( complex(&a,ceil(a.v.cmplx_val.real), ceil(a.v.cmplx_val.imag)) );
  339.     }
  340. }
  341.  
  342. #ifdef GAMMA
  343.  
  344. f_gamma()
  345. {
  346. extern int signgam;
  347. register double y;
  348. struct value a;
  349.  
  350.     y = gamma(real(pop(&a)));
  351.     if (y > 88.0) {
  352.         undefined = TRUE;
  353.         push( integer(&a,0) );
  354.     }
  355.     else
  356.         push( complex(&a,signgam * exp(y),0.0) );
  357. }
  358.  
  359. #endif /* GAMMA */
  360.