home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 334_02 / standar2.c < prev    next >
Text File  |  1991-02-05  |  6KB  |  327 lines

  1. /*
  2.  *
  3.  *    G N U P L O T  --  header.c
  4.  *
  5.  *  Copyright (C) 1986 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.     den = cos(2*real(&a))+cosh(2*imag(&a));
  82.     push( complex(&a,sin(2*real(&a))/den, sinh(2*imag(&a))/den) );
  83. }
  84.  
  85. f_asin()
  86. {
  87. struct value a;
  88. register double alpha, beta, x, y;
  89.     (void) pop(&a);
  90.     x = real(&a); y = imag(&a);
  91.     if (y == 0.0) {
  92.         if (fabs(x) > 1.0) {
  93.             undefined = TRUE;
  94.             push(complex(&a,0.0, 0.0));
  95.         } else
  96.             push( complex(&a,asin(x),0.0) );
  97.     } else {
  98.         beta  = sqrt((x + 1)*(x + 1) + y*y)/2 - sqrt((x - 1)*(x - 1) + y*y)/2;
  99.         alpha = sqrt((x + 1)*(x + 1) + y*y)/2 + sqrt((x - 1)*(x - 1) + y*y)/2;
  100.         push( complex(&a,asin(beta), log(alpha + sqrt(alpha*alpha-1))) );
  101.     }
  102. }
  103.  
  104. f_acos()
  105. {
  106. struct value a;
  107. register double alpha, beta, x, y;
  108.     (void) pop(&a);
  109.     x = real(&a); y = imag(&a);
  110.     if (y == 0.0) {
  111.         if (fabs(x) > 1.0) {
  112.             undefined = TRUE;
  113.             push(complex(&a,0.0, 0.0));
  114.         } else
  115.             push( complex(&a,acos(x),0.0) );
  116.     } else {
  117.         alpha = sqrt((x + 1)*(x + 1) + y*y)/2 + sqrt((x - 1)*(x - 1) + y*y)/2;
  118.         beta  = sqrt((x + 1)*(x + 1) + y*y)/2 - sqrt((x - 1)*(x - 1) + y*y)/2;
  119.         push( complex(&a,acos(beta), log(alpha + sqrt(alpha*alpha-1))) );
  120.     }
  121. }
  122.  
  123. f_atan()
  124. {
  125. struct value a;
  126. register double x, y;
  127.     (void) pop(&a);
  128.     x = real(&a); y = imag(&a);
  129.     if (y == 0.0)
  130.         push( complex(&a,atan(x), 0.0) );
  131.     else if (x == 0.0 && fabs(y) == 1.0) {
  132.         undefined = TRUE;
  133.         push(complex(&a,0.0, 0.0));
  134.     } else
  135.         push( complex(&a,atan(2*x/(1-x*x-y*y)),
  136.                 log((x*x+(y+1)*(y+1))/(x*x+(y-1)*(y-1)))/4) );
  137. }
  138.  
  139. f_sinh()
  140. {
  141. struct value a;
  142.     (void) pop(&a);
  143.     push( complex(&a,sinh(real(&a))*cos(imag(&a)), cosh(real(&a))*sin(imag(&a))) );
  144. }
  145.  
  146. f_cosh()
  147. {
  148. struct value a;
  149.     (void) pop(&a);
  150.     push( complex(&a,cosh(real(&a))*cos(imag(&a)), sinh(real(&a))*sin(imag(&a))) );
  151. }
  152.  
  153. f_tanh()
  154. {
  155. struct value a;
  156. register double den;
  157.     (void) pop(&a);
  158.     den = cosh(2*real(&a)) + cos(2*imag(&a));
  159.     push( complex(&a,sinh(2*real(&a))/den, sin(2*imag(&a))/den) );
  160. }
  161.  
  162. f_int()
  163. {
  164. struct value a;
  165.     push( integer(&a,(int)real(pop(&a))) );
  166. }
  167.  
  168.  
  169. f_abs()
  170. {
  171. struct value a;
  172.     (void) pop(&a);
  173.     switch (a.type) {
  174.         case INT:
  175.             push( integer(&a,abs(a.v.int_val)) );            
  176.             break;
  177.         case CMPLX:
  178.             push( complex(&a,magnitude(&a), 0.0) );
  179.     }
  180. }
  181.  
  182. f_sgn()
  183. {
  184. struct value a;
  185.     (void) pop(&a);
  186.     switch(a.type) {
  187.         case INT:
  188.             push( integer(&a,(a.v.int_val > 0) ? 1 : 
  189.                     (a.v.int_val < 0) ? -1 : 0) );
  190.             break;
  191.         case CMPLX:
  192.             push( integer(&a,(a.v.cmplx_val.real > 0.0) ? 1 : 
  193.                     (a.v.cmplx_val.real < 0.0) ? -1 : 0) );
  194.             break;
  195.     }
  196. }
  197.  
  198.  
  199. f_sqrt()
  200. {
  201. struct value a;
  202.     double mag, ang;
  203.     (void) pop(&a);
  204.     mag = sqrt(magnitude(&a));
  205.     if ( (ang = angle(&a)) < 0.0)
  206.         ang += 2*Pi;
  207.     ang /= 2;
  208.     push( complex(&a,mag*cos(ang), mag*sin(ang)) );
  209. }
  210.  
  211.  
  212. f_exp()
  213. {
  214. register double mag, ang;
  215. struct value a;
  216.     (void) pop(&a);
  217.     mag = exp(real(&a));
  218.     ang = imag(&a);
  219.     push( complex(&a,mag*cos(ang), mag*sin(ang)) );
  220. }
  221.  
  222.  
  223. f_log10()
  224. {
  225. struct value a;
  226. register double l10;;
  227.     (void) pop(&a);
  228.     l10 = log(10.0);    /***** replace with a constant! ******/
  229.     push( complex(&a,log(magnitude(&a))/l10, angle(&a)/l10) );
  230. }
  231.  
  232.  
  233. f_log()
  234. {
  235. struct value a;
  236.     (void) pop(&a);
  237.     push( complex(&a,log(magnitude(&a)), angle(&a)) );
  238. }
  239.  
  240. f_besj0()    /* j0(a) = sin(a)/a */
  241. {
  242. struct value a;
  243.     a = top_of_stack;
  244.     f_sin();
  245.     push(&a);
  246.     f_div();
  247. }
  248.  
  249.  
  250. f_besj1()    /* j1(a) = sin(a)/(a**2) - cos(a)/a */
  251. {
  252. struct value a;
  253.     a = top_of_stack;
  254.     f_sin();
  255.     push(&a);
  256.     push(&a);
  257.     f_mult();
  258.     f_div();
  259.     push(&a);
  260.     f_cos();
  261.     push(&a);
  262.     f_div();
  263.     f_minus();
  264. }
  265.  
  266.  
  267. f_besy0()    /* y0(a) = -cos(a)/a */
  268. {
  269. struct value a;
  270.     a = top_of_stack;
  271.     f_cos();
  272.     push(&a);
  273.     f_div();
  274.     f_uminus();
  275. }
  276.  
  277.  
  278. f_besy1()    /* y1(a) = -cos(a)/(a**2) - sin(a)/a */
  279. {
  280. struct value a;
  281.  
  282.     a = top_of_stack;
  283.     f_cos();
  284.     push(&a);
  285.     push(&a);
  286.     f_mult();
  287.     f_div();
  288.     push(&a);
  289.     f_sin();
  290.     push(&a);
  291.     f_div();
  292.     f_plus();
  293.     f_uminus();
  294. }
  295.  
  296.  
  297. f_floor()
  298. {
  299. struct value a;
  300.  
  301.     (void) pop(&a);
  302.     switch (a.type) {
  303.         case INT:
  304.             push( integer(&a,(int)floor((double)a.v.int_val)));            
  305.             break;
  306.         case CMPLX:
  307.             push( complex(&a,floor(a.v.cmplx_val.real),
  308.                 floor(a.v.cmplx_val.imag)) );
  309.     }
  310. }
  311.  
  312.  
  313.  
  314. f_ceil()
  315. {
  316. struct value a;
  317.  
  318.     (void) pop(&a);
  319.     switch (a.type) {
  320.         case INT:
  321.             push( integer(&a,(int)ceil((double)a.v.int_val)));            
  322.             break;
  323.         case CMPLX:
  324.             push( complex(&a,ceil(a.v.cmplx_val.real), ceil(a.v.cmplx_val.imag)) );
  325.     }
  326. }
  327.