imag.cpp 659 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* Returns the coefficient of the imaginary part of complex z
  2. z imag(z)
  3. - -------
  4. a + i b b
  5. exp(i a) sin(a)
  6. */
  7. #include "stdafx.h"
  8. #include "defs.h"
  9. void
  10. eval_imag(void)
  11. {
  12. push(cadr(p1));
  13. eval();
  14. imag();
  15. }
  16. void
  17. imag(void)
  18. {
  19. save();
  20. rect();
  21. p1 = pop();
  22. push(p1);
  23. push(p1);
  24. conjugate();
  25. subtract();
  26. push_integer(2);
  27. divide();
  28. push(imaginaryunit);
  29. divide();
  30. restore();
  31. }
  32. #if SELFTEST
  33. static char *s[] = {
  34. "imag(a+i*b)",
  35. "b",
  36. "imag(1+exp(i*pi/3))",
  37. "1/2*3^(1/2)",
  38. "imag(i)",
  39. "1",
  40. "imag((-1)^(1/3))",
  41. "1/2*3^(1/2)",
  42. "imag(-i)",
  43. "-1",
  44. };
  45. void
  46. test_imag(void)
  47. {
  48. test(__FILE__, s, sizeof s / sizeof (char *));
  49. }
  50. #endif