qpow.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // Rational power function
  2. #include "stdafx.h"
  3. #include "defs.h"
  4. static void qpowf(void);
  5. static void normalize_angle(void);
  6. static int is_small_integer(U *);
  7. void
  8. qpow()
  9. {
  10. save();
  11. qpowf();
  12. restore();
  13. }
  14. #define BASE p1
  15. #define EXPO p2
  16. static void
  17. qpowf(void)
  18. {
  19. int expo;
  20. unsigned int a, b, *t, *x, *y;
  21. EXPO = pop();
  22. BASE = pop();
  23. // if base is 1 or exponent is 0 then return 1
  24. if (isplusone(BASE) || iszero(EXPO)) {
  25. push_integer(1);
  26. return;
  27. }
  28. // if base is zero then return 0
  29. if (iszero(BASE)) {
  30. if (isnegativenumber(EXPO))
  31. stop("divide by zero");
  32. push(zero);
  33. return;
  34. }
  35. // if exponent is 1 then return base
  36. if (isplusone(EXPO)) {
  37. push(BASE);
  38. return;
  39. }
  40. // if exponent is integer then power
  41. if (isinteger(EXPO)) {
  42. push(EXPO);
  43. expo = pop_integer();
  44. if (expo == (int) 0x80000000) {
  45. // expo greater than 32 bits
  46. push_symbol(POWER);
  47. push(BASE);
  48. push(EXPO);
  49. list(3);
  50. return;
  51. }
  52. x = mpow(BASE->u.q.a, abs(expo));
  53. y = mpow(BASE->u.q.b, abs(expo));
  54. if (expo < 0) {
  55. t = x;
  56. x = y;
  57. y = t;
  58. MSIGN(x) = MSIGN(y);
  59. MSIGN(y) = 1;
  60. }
  61. p3 = alloc();
  62. p3->k = NUM;
  63. p3->u.q.a = x;
  64. p3->u.q.b = y;
  65. push(p3);
  66. return;
  67. }
  68. // from here on out the exponent is NOT an integer
  69. // if base is -1 then normalize polar angle
  70. if (isminusone(BASE)) {
  71. push(EXPO);
  72. normalize_angle();
  73. return;
  74. }
  75. // if base is negative then (-N)^M -> N^M * (-1)^M
  76. if (isnegativenumber(BASE)) {
  77. push(BASE);
  78. negate();
  79. push(EXPO);
  80. qpow();
  81. push_integer(-1);
  82. push(EXPO);
  83. qpow();
  84. multiply();
  85. return;
  86. }
  87. // if BASE is not an integer then power numerator and denominator
  88. if (!isinteger(BASE)) {
  89. push(BASE);
  90. mp_numerator();
  91. push(EXPO);
  92. qpow();
  93. push(BASE);
  94. mp_denominator();
  95. push(EXPO);
  96. negate();
  97. qpow();
  98. multiply();
  99. return;
  100. }
  101. // At this point BASE is a positive integer.
  102. // If BASE is small then factor it.
  103. if (is_small_integer(BASE)) {
  104. push(BASE);
  105. push(EXPO);
  106. quickfactor();
  107. return;
  108. }
  109. // At this point BASE is a positive integer and EXPO is not an integer.
  110. if (MLENGTH(EXPO->u.q.a) > 1 || MLENGTH(EXPO->u.q.b) > 1) {
  111. push_symbol(POWER);
  112. push(BASE);
  113. push(EXPO);
  114. list(3);
  115. return;
  116. }
  117. a = EXPO->u.q.a[0];
  118. b = EXPO->u.q.b[0];
  119. x = mroot(BASE->u.q.a, b);
  120. if (x == 0) {
  121. push_symbol(POWER);
  122. push(BASE);
  123. push(EXPO);
  124. list(3);
  125. return;
  126. }
  127. y = mpow(x, a);
  128. mfree(x);
  129. p3 = alloc();
  130. p3->k = NUM;
  131. if (MSIGN(EXPO->u.q.a) == -1) {
  132. p3->u.q.a = mint(1);
  133. p3->u.q.b = y;
  134. } else {
  135. p3->u.q.a = y;
  136. p3->u.q.b = mint(1);
  137. }
  138. push(p3);
  139. }
  140. //-----------------------------------------------------------------------------
  141. //
  142. // Normalize the angle of unit imaginary, i.e. (-1) ^ N
  143. //
  144. // Input: N on stack (must be rational, not float)
  145. //
  146. // Output: Result on stack
  147. //
  148. // Note:
  149. //
  150. // n = q * d + r
  151. //
  152. // Example:
  153. // n d q r
  154. //
  155. // (-1)^(8/3) -> (-1)^(2/3) 8 3 2 2
  156. // (-1)^(7/3) -> (-1)^(1/3) 7 3 2 1
  157. // (-1)^(5/3) -> -(-1)^(2/3) 5 3 1 2
  158. // (-1)^(4/3) -> -(-1)^(1/3) 4 3 1 1
  159. // (-1)^(2/3) -> (-1)^(2/3) 2 3 0 2
  160. // (-1)^(1/3) -> (-1)^(1/3) 1 3 0 1
  161. //
  162. // (-1)^(-1/3) -> -(-1)^(2/3) -1 3 -1 2
  163. // (-1)^(-2/3) -> -(-1)^(1/3) -2 3 -1 1
  164. // (-1)^(-4/3) -> (-1)^(2/3) -4 3 -2 2
  165. // (-1)^(-5/3) -> (-1)^(1/3) -5 3 -2 1
  166. // (-1)^(-7/3) -> -(-1)^(2/3) -7 3 -3 2
  167. // (-1)^(-8/3) -> -(-1)^(1/3) -8 3 -3 1
  168. //
  169. //-----------------------------------------------------------------------------
  170. #define A p1
  171. #define Q p2
  172. #define R p3
  173. static void
  174. normalize_angle(void)
  175. {
  176. save();
  177. A = pop();
  178. // integer exponent?
  179. if (isinteger(A)) {
  180. if (A->u.q.a[0] & 1)
  181. push_integer(-1); // odd exponent
  182. else
  183. push_integer(1); // even exponent
  184. restore();
  185. return;
  186. }
  187. // floor
  188. push(A);
  189. bignum_truncate();
  190. Q = pop();
  191. if (isnegativenumber(A)) {
  192. push(Q);
  193. push_integer(-1);
  194. add();
  195. Q = pop();
  196. }
  197. // remainder (always positive)
  198. push(A);
  199. push(Q);
  200. subtract();
  201. R = pop();
  202. // remainder becomes new angle
  203. push_symbol(POWER);
  204. push_integer(-1);
  205. push(R);
  206. list(3);
  207. // negate if quotient is odd
  208. if (Q->u.q.a[0] & 1)
  209. negate();
  210. restore();
  211. }
  212. static int
  213. is_small_integer(U *p)
  214. {
  215. if (isinteger(p) && MLENGTH(p->u.q.a) == 1 && (p->u.q.a[0] & 0x80000000) == 0)
  216. return 1;
  217. else
  218. return 0;
  219. }