7 Output: Result on stack
39 // both base and exponent are rational numbers?
41 if (isrational(p1
) && isrational(p2
)) {
48 // both base and exponent are either rational or double?
50 if (isnum(p1
) && isnum(p2
)) {
62 if (p1
== symbol(E
) && car(p2
) == symbol(LOG
)) {
67 if (p1
== symbol(E
) && isdouble(p2
)) {
68 push_double(exp(p2
->u
.d
));
76 if (equal(p1
, one
) || iszero(p2
)) {
88 // (a * b) ^ c -> (a ^ c) * (b ^ c)
90 if (car(p1
) == symbol(MULTIPLY
)) {
106 // (a ^ b) ^ c -> a ^ (b * c)
108 if (car(p1
) == symbol(POWER
)) {
117 // (a + b) ^ n -> (a + b) * (a + b) ...
119 if (expanding
&& isadd(p1
) && isnum(p2
)) {
128 // sin(x) ^ 2n -> (1 - cos(x) ^ 2) ^ n
130 if (trigmode
== 1 && car(p1
) == symbol(SIN
) && iseveninteger(p2
)) {
144 // cos(x) ^ 2n -> (1 - sin(x) ^ 2) ^ n
146 if (trigmode
== 2 && car(p1
) == symbol(COS
) && iseveninteger(p2
)) {
160 // complex number? (just number, not expression)
162 if (iscomplexnumber(p1
)) {
166 // n will be negative here, positive n already handled
172 // (a + ib) = | -------- |
190 // noninteger or floating power?
194 #if 1 // use polar form
209 #else // use exponential form
228 if (simplify_polar())
237 //-----------------------------------------------------------------------------
239 // Compute the power of a sum
245 // Output: Result on stack
249 // Uses the multinomial series (see Math World)
252 // (a1 + a2 + ... + ak) = sum (--------------- a1 a2 ... ak )
255 // The sum is over all n1 ... nk such that n1 + n2 + ... + nk = n.
257 //-----------------------------------------------------------------------------
259 // first index is the term number 0..k-1, second index is the exponent 0..n
261 #define A(i, j) frame[(i) * (n + 1) + (j)]
268 // number of terms in the sum
274 push_frame(k
* (n
+ 1));
279 for (i
= 0; i
< k
; i
++) {
280 for (j
= 0; j
<= n
; j
++) {
293 a
= (int *) malloc(k
* sizeof (int));
296 stop("malloc failure");
300 multinomial_sum(k
, n
, a
, 0, n
);
304 pop_frame(k
* (n
+ 1));
307 //-----------------------------------------------------------------------------
309 // Compute multinomial sum
311 // Input: k number of factors
313 // n overall exponent
317 // i partition array index
319 // m partition remainder
325 // Output: Result on stack
329 // Uses recursive descent to fill the partition array.
331 //-----------------------------------------------------------------------------
334 multinomial_sum(int k
, int n
, int *a
, int i
, int m
)
339 for (j
= 0; j
<= m
; j
++) {
341 multinomial_sum(k
, n
, a
, i
+ 1, m
- j
);
352 for (j
= 0; j
< k
; j
++) {
360 for (j
= 0; j
< k
; j
++) {
370 // p2 is the exponent expression
379 n
= isquarterturn(p2
);
399 if (car(p2
) == symbol(ADD
)) {
402 n
= isquarterturn(car(p3
));
479 "2^(1/2)*3^(1/2)*17^(1/2)",
485 "3*11^(1/2)*101^(1/2)",
488 "10*2^(1/3)*5^(1/3)",
499 // this is why we factor irrationals
504 // inverse of complex numbers
532 // more complex number cases
541 "(-1)^(1/8)*2^(1/4)",
544 "-(-1)^(7/8)/(2^(1/4))",
550 "0.776887-0.321797*i",
552 // test cases for simplification of polar forms, counterclockwise
602 // test cases for simplification of polar forms, clockwise
656 test(__FILE__
, s
, sizeof s
/ sizeof (char *));