home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume08 / qhwc < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  13.1 KB

  1. From decwrl!ucbvax!ucsd!swrinde!cs.utexas.edu!uunet!allbery Sun Oct  1 15:40:14 PDT 1989
  2. Article 1132 of comp.sources.misc:
  3. Path: decwrl!ucbvax!ucsd!swrinde!cs.utexas.edu!uunet!allbery
  4. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  5. Newsgroups: comp.sources.misc
  6. Subject: v08i088: qhwc: a version of Kernighan/Pike's hoc(1) calculator for quaterions
  7. Message-ID: <68946@uunet.UU.NET>
  8. Date: 1 Oct 89 14:48:55 GMT
  9. Sender: allbery@uunet.UU.NET
  10. Reply-To: eugene@eos.arc.nasa.gov
  11. Organization: NASA Ames Research Center, Calif.
  12. Lines: 373
  13. Approved: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  14.  
  15. Posting-number: Volume 8, Issue 88
  16. Submitted-by: eugene@eos.arc.nasa.gov
  17. Archive-name: qhwc
  18.  
  19. Bill Burke, Lick Observatory, Astronomy/Physics Dept, UC Santa Cruz
  20. has asked me to post this.  Qhwc (silent Q, pronounced "hawk," for K/P's hoc
  21. calculator, yet another pun, and dedicated to physicist Stephen Hawking.)
  22. The code can be in the public domain, but he asks that users
  23. of quaterions send him a copy of papers on sample research areas
  24. they are using quaterions requiring this calcuator).  You can either mail
  25. directly to him (no email, but reachable at UCSC) or me.
  26.  
  27. He said to say, "Yes, some physicists do program using lex and yacc.
  28. And like Unix.  Way to go."
  29.  
  30. Another gross generalization from
  31.  
  32. --eugene miya, NASA Ames Research Center, eugene@aurora.arc.nasa.gov
  33.   resident cynic at the Rock of Ages Home for Retired Hackers:
  34.   "You trust the `reply' command with all those different mailers out there?"
  35.   "If my mail does not reach you, please accept my apology."
  36.   {ncar,decwrl,hplabs,uunet}!ames!eugene
  37.                   Live free or die.
  38.  
  39. --------snip here and run thru sh-----------
  40. echo x - Makefile
  41. sed 's/^X//' >Makefile <<'*-*-END-of-Makefile-*-*'
  42. XFILES= Makefile qhwc.c qhwc.l qhwc.y
  43. XOBJECTS= y.tab.o lex.yy.o qhwc.o
  44. XLIBES= -lm -ll
  45. XCFLAGS= -O
  46. X
  47. Xqhwk: $(OBJECTS)
  48. X    cc $(CFLAGS) $(OBJECTS) $(LIBES) -o qhwk
  49. X
  50. Xy.tab.c: qhwc.y
  51. X    yacc -d qhwc.y
  52. X
  53. Xlex.yy.c: qhwc.l
  54. X    lex qhwc.l
  55. X
  56. Xclean:
  57. X    rm *.o y.tab.[hc] lex.yy.c
  58. *-*-END-of-Makefile-*-*
  59. echo x - qhwc.c
  60. sed 's/^X//' >qhwc.c <<'*-*-END-of-qhwc.c-*-*'
  61. X#include <stdio.h>
  62. X#include <math.h>
  63. X
  64. Xtypedef struct 
  65. X{
  66. X  double real, imag, jmag, kmag;
  67. X} QRT;
  68. X
  69. X#include "y.tab.h"
  70. X
  71. Xdouble obj[8][4];
  72. Xint j;
  73. X
  74. Xint  main()
  75. X{
  76. X  double u;
  77. X
  78. X  printf("QHWC: William's Quaternionic Hand Calculator \n\n");
  79. X  printf("Last expression is H0, then H1, up to H7     \n");
  80. X  printf("Use h as a shorthand for H0, the previous one\n");
  81. X  printf("Operators: + - * % (u minus) (h*=CCG) exp |h| \n");
  82. X  printf("        pi if(,,) Re Pu (Re(h)+Pu(h)=h)      \n");
  83. X  printf("Enter expression to calculate (^D to quit):  \n\n");
  84. X  for ( j=0; j<8; ++j) {
  85. X  obj[j][0] = 0;
  86. X  obj[j][1] = 0;
  87. X  obj[j][2] = 0;
  88. X  obj[j][3] = 0;
  89. X  }
  90. X
  91. X  return (yyparse());
  92. X}
  93. *-*-END-of-qhwc.c-*-*
  94. echo x - qhwc.l
  95. sed 's/^X//' >qhwc.l <<'*-*-END-of-qhwc.l-*-*'
  96. X%{
  97. Xdouble pi=3.141592654;
  98. Xtypedef struct qrt
  99. X{
  100. X  double real, imag, jmag, kmag;
  101. X} QRT;
  102. X#include "y.tab.h"
  103. X
  104. Xdouble rbuff;
  105. X
  106. X%}
  107. X
  108. X%%
  109. X
  110. X[0-9]+(\.[0-9]+)?  {
  111. X                   sscanf(yytext,"%lf",&rbuff);
  112. X                   yylval.hval.real = rbuff;
  113. X                   yylval.hval.imag = 0;
  114. X                   yylval.hval.jmag = 0;
  115. X                   yylval.hval.kmag = 0;    
  116. X                   return H; 
  117. X                   }
  118. X[0-9]+(\.[0-9]+)?i {
  119. X                   yytext[yyleng-1] = '\0';
  120. X                   sscanf(yytext,"%lf",&rbuff);
  121. X                   yylval.hval.real = 0;
  122. X                   yylval.hval.imag = rbuff;
  123. X                   yylval.hval.jmag = 0;
  124. X                   yylval.hval.kmag = 0;    
  125. X                   return H;
  126. X                   }
  127. X[0-9]+(\.[0-9]+)?j {
  128. X                   yytext[yyleng-1] = '\0';
  129. X                   sscanf(yytext,"%lf",&rbuff);
  130. X                   yylval.hval.real = 0;
  131. X                   yylval.hval.imag = 0;
  132. X                   yylval.hval.jmag = rbuff;
  133. X                   yylval.hval.kmag = 0;    
  134. X                   return H;
  135. X                   }
  136. X[0-9]+(\.[0-9]+)?k {
  137. X                   yytext[yyleng-1] = '\0';
  138. X                   sscanf(yytext,"%lf",&rbuff);
  139. X                   yylval.hval.real = 0;
  140. X                   yylval.hval.imag = 0;
  141. X                   yylval.hval.jmag = 0;
  142. X                   yylval.hval.kmag = rbuff;    
  143. X                   return H;
  144. X                   }
  145. X[hH][0-7]          {     /* recalling previous from stack */
  146. X                   yylval.intval = (int) (yytext[1]-'0');
  147. X                   return OBJECT;
  148. X                   }
  149. Xh                  {
  150. X                   yylval.intval = 0;
  151. X                   return OBJECT;
  152. X                   }
  153. XRe                 {
  154. X                   return RE;
  155. X                   }
  156. X[sS][qQ][rR][tT]   {
  157. X                   return SQRT;
  158. X                   }
  159. X[sS][qQ]           {
  160. X                   return SQ;
  161. X                   }
  162. X[eE][xX][pP]       {
  163. X                   return EXP;
  164. X                   }
  165. X[iI][fF]           {
  166. X                   return IF;
  167. X                   }
  168. XPu                 {
  169. X                   return PU;
  170. X                   }
  171. X[pP][iI]           {
  172. X                   yylval.hval.real = pi;
  173. X                   yylval.hval.imag = 0;
  174. X                   yylval.hval.jmag = 0;
  175. X                   yylval.hval.kmag = 0;
  176. X                   return H;
  177. X                   }
  178. Xi                  {
  179. X                   yylval.hval.real = 0;
  180. X                   yylval.hval.imag = 1.0;
  181. X                   yylval.hval.jmag = 0;
  182. X                   yylval.hval.kmag = 0;
  183. X                   return H;
  184. X                   }
  185. Xj                  {
  186. X                   yylval.hval.real = 0;
  187. X                   yylval.hval.imag = 0;
  188. X                   yylval.hval.jmag = 1.0;
  189. X                   yylval.hval.kmag = 0;
  190. X                   return H;
  191. X                   }
  192. Xk                  {
  193. X                   yylval.hval.real = 0;
  194. X                   yylval.hval.imag = 0;
  195. X                   yylval.hval.jmag = 0;
  196. X                   yylval.hval.kmag = 1.0;
  197. X                   return H;
  198. X                   }
  199. X[-()|+/*,\n]       return *yytext;
  200. X[ \t]+               ;
  201. X.                  {yyerror("Unrecognized input: %s\n",yytext);}
  202. X
  203. X%%
  204. X
  205. *-*-END-of-qhwc.l-*-*
  206. echo x - qhwc.y
  207. sed 's/^X//' >qhwc.y <<'*-*-END-of-qhwc.y-*-*'
  208. X%{
  209. X
  210. X#include <math.h>
  211. X
  212. Xtypedef struct qrt
  213. X{
  214. X  double real, imag, jmag, kmag;
  215. X} QRT;
  216. X
  217. Xdouble sqrt(), sin(), cos(), exp();
  218. Xdouble x,r,rsqd,theta;
  219. XQRT hh, hhh, hcc;
  220. Xextern double obj[8][4];
  221. Xextern double pi;
  222. Xint i;
  223. X
  224. X%}
  225. X
  226. X%union {
  227. X    int intval;
  228. X        double realval;
  229. X        QRT hval;
  230. X        }
  231. X
  232. X%token <hval> H
  233. X%token <intval>  OBJECT
  234. X%left '+' '-'
  235. X%left '*' '/'
  236. X%right SQ SQRT EXP IF
  237. X%right RE PU
  238. X%left UMINUS CCG
  239. X
  240. X%type <hval> expression program
  241. X
  242. X%%
  243. X
  244. Xprogram:
  245. X    program expression '\n'    = { printf("%lf + %lfi + %lfj + %lfk \n",
  246. X                                      $2.real, $2.imag, $2.jmag, $2.kmag);
  247. X                                    for ( i=7; i>0; --i) {
  248. X                                      obj[i][0] = obj[i-1][0];
  249. X                                      obj[i][1] = obj[i-1][1];
  250. X                                      obj[i][2] = obj[i-1][2];
  251. X                                      obj[i][3] = obj[i-1][3];
  252. X                                    }
  253. X                                    obj[0][0] = $2.real;
  254. X                                    obj[0][1] = $2.imag;
  255. X                                    obj[0][2] = $2.jmag;
  256. X                                    obj[0][3] = $2.kmag;
  257. X                                  }
  258. X|       program error '\n'      = { yyerrok; }
  259. X|    /* NULL */              = {}
  260. X;
  261. X
  262. Xexpression:
  263. X    H                               = {  $$.real = $1.real;
  264. X                                             $$.imag = $1.imag;
  265. X                                             $$.jmag = $1.jmag;
  266. X                                             $$.kmag = $1.kmag;
  267. X                                          }
  268. X|       OBJECT                          = {  $$.real = obj[$1][0];
  269. X                                             $$.imag = obj[$1][1];
  270. X                                             $$.jmag = obj[$1][2];
  271. X                                             $$.kmag = obj[$1][3];
  272. X                                          }
  273. X|    expression '+' expression    = {  $$.real = $1.real + $3.real;
  274. X                                             $$.imag = $1.imag + $3.imag;
  275. X                                             $$.jmag = $1.jmag + $3.jmag;
  276. X                                             $$.kmag = $1.kmag + $3.kmag;
  277. X                                          }
  278. X|    expression '-' expression    = {  $$.real = $1.real - $3.real;
  279. X                                             $$.imag = $1.imag - $3.imag;
  280. X                                             $$.jmag = $1.jmag - $3.jmag;
  281. X                                             $$.kmag = $1.kmag - $3.kmag;
  282. X                                          }
  283. X|    expression '*' expression    = {  
  284. X                                            Multiply(&$1,&$3,&$$);
  285. X                                          }
  286. X|       expression '/' expression =       {
  287. X                                            Conjugate(&$3,&hcc);
  288. X                                            Multiply(&$3,&hcc,&hh);
  289. X                                            hh.real = 1/(hh.real);
  290. X                                            Multiply(&$1,&hcc,&hhh);
  291. X                                            Multiply(&hh,&hhh,&$$);
  292. X                                          }
  293. X|   RE expression                       = {  $$.real = $2.real;
  294. X                                             $$.imag = 0;
  295. X                                             $$.jmag = 0;
  296. X                                             $$.kmag = 0;
  297. X                                          }
  298. X|   PU expression                       = {  $$.real = 0;
  299. X                                             $$.imag = $2.imag;
  300. X                                             $$.jmag = $2.jmag;
  301. X                                             $$.kmag = $2.kmag;
  302. X                                          }
  303. X|   EXP '('  expression ')' = { 
  304. X    r = sqrt($3.imag*$3.imag+$3.jmag*$3.jmag+$3.kmag*$3.kmag);
  305. X    if ( r > 0.0) {
  306. X    $$.real = exp($3.real)*cos(r);
  307. X    $$.imag = exp($3.real)*sin(r)*$3.imag/r;
  308. X    $$.jmag = exp($3.real)*sin(r)*$3.jmag/r;
  309. X    $$.kmag = exp($3.real)*sin(r)*$3.kmag/r;
  310. X    } else {
  311. X    $$.real = exp($3.real);
  312. X    $$.imag = 0.0;
  313. X    $$.jmag = 0.0;
  314. X    $$.kmag = 0.0;
  315. X    }
  316. X  }
  317. X|   IF '(' expression ',' expression',' expression ')' = {
  318. X                               if ( $3.real > 0) {
  319. X                                 $$.real = $5.real;
  320. X                                 $$.imag = $5.imag;
  321. X                                 $$.jmag = $5.jmag;
  322. X                                 $$.kmag = $5.kmag;
  323. X                               }
  324. X                               else {
  325. X                                 $$.real = $7.real;
  326. X                                 $$.imag = $7.imag;
  327. X                                 $$.jmag = $7.jmag;
  328. X                                 $$.kmag = $7.kmag;
  329. X                               }
  330. X                              }
  331. X|   '-' expression  %prec UMINUS        = {  $$.real = -$2.real;
  332. X                                             $$.imag = -$2.imag;
  333. X                                             $$.jmag = -$2.jmag;
  334. X                                             $$.kmag = -$2.kmag;
  335. X                                          }
  336. X|    expression '*'   %prec CCG         = {  $$.real =  $1.real;
  337. X                                             $$.imag = -$1.imag;
  338. X                                             $$.jmag = -$1.jmag;
  339. X                                             $$.kmag = -$1.kmag;
  340. X                                          }
  341. X|    '(' expression ')'            = {  $$.real = $2.real;
  342. X                                             $$.imag = $2.imag;
  343. X                                             $$.jmag = $2.jmag;
  344. X                                             $$.kmag = $2.kmag;
  345. X                                          }
  346. X|    '|' expression '|'            = {   
  347. X                                             Conjugate(&$2,&hcc);
  348. X                                             Multiply(&$2,&hcc,&hh);
  349. X                                             $$.real = sqrt(hh.real);
  350. X                                             $$.imag = 0;
  351. X                                             $$.jmag = 0;
  352. X                                             $$.kmag = 0;
  353. X                                          }
  354. X;
  355. X
  356. X%%
  357. X
  358. X
  359. Xvoid Multiply (hh1, hh2, hh3)
  360. XQRT *hh1, *hh2, *hh3;
  361. X{
  362. X  hh3->real = hh1->real * hh2->real - hh1->imag * hh2->imag
  363. X            -hh1->jmag * hh2->jmag - hh1->kmag * hh2->kmag;
  364. X  hh3->imag = hh1->real * hh2->imag + hh1->imag * hh2->real
  365. X            +hh1->jmag * hh2->kmag - hh1->kmag * hh2->jmag;
  366. X  hh3->jmag = hh1->real * hh2->jmag + hh1->jmag * hh2->real
  367. X            +hh1->kmag * hh2->imag - hh1->imag * hh2->kmag;
  368. X  hh3->kmag = hh1->real * hh2->kmag + hh1->kmag * hh2->real
  369. X            +hh1->imag * hh2->jmag - hh1->jmag * hh2->imag;
  370. X}
  371. X
  372. Xvoid Conjugate (hh1, hh2)
  373. XQRT *hh1, *hh2;
  374. X{
  375. X  hh2->real =  hh1->real;
  376. X  hh2->imag = -hh1->imag;
  377. X  hh2->jmag = -hh1->jmag;
  378. X  hh2->kmag = -hh1->kmag;
  379. X}
  380. X
  381. Xyyerror(s)
  382. Xchar *s;
  383. X{
  384. X    printf("%s\n",s);
  385. X}
  386. *-*-END-of-qhwc.y-*-*
  387. exit
  388.  
  389.  
  390.