3 //-----------------------------------------------------------------------------
5 // Generate all divisors of a term
7 // Input: Term on stack (factor * factor * ...)
9 // Output: Divisors on stack
11 //-----------------------------------------------------------------------------
15 static void gen(int, int);
16 static void __factor_add(void);
17 static int __cmp(const void *, const void *);
27 qsort(stack
+ h
, n
, sizeof (U
*), __cmp
);
29 p1
->u
.tensor
->ndim
= 1;
30 p1
->u
.tensor
->dim
[0] = n
;
31 for (i
= 0; i
< n
; i
++)
32 p1
->u
.tensor
->elem
[i
] = stack
[h
+ i
];
39 divisors_onstack(void)
49 // push all of the term's factors
53 factor_small_number();
54 } else if (car(p1
) == symbol(ADD
)) {
58 //for (i = h; i < tos; i++)
59 //print(stdout, stack[i]);
61 } else if (car(p1
) == symbol(MULTIPLY
)) {
65 factor_small_number();
70 if (car(p2
) == symbol(POWER
)) {
79 } else if (car(p1
) == symbol(POWER
)) {
89 // contruct divisors by recursive descent
99 for (i
= 0; i
< n
; i
++)
100 stack
[h
+ i
] = stack
[k
+ i
];
107 //-----------------------------------------------------------------------------
111 // Input: Base-exponent pairs on stack
115 // k just past last pair
117 // Output: Divisors on stack
119 // For example, factor list 2 2 3 1 results in 6 divisors,
128 //-----------------------------------------------------------------------------
153 expo
= pop_integer();
155 for (i
= 0; i
<= abs(expo
); i
++) {
158 push_integer(sign(expo
) * i
);
167 //-----------------------------------------------------------------------------
169 // Factor ADD expression
171 // Input: Expression on stack
173 // Output: Factors on stack
175 // Each factor consists of two expressions, the factor itself followed
178 //-----------------------------------------------------------------------------
187 // get gcd of all terms
212 factor_small_number();
213 } else if (car(p2
) == symbol(MULTIPLY
)) {
215 if (isnum(car(p3
))) {
217 factor_small_number();
233 // divide each term by gcd
255 __cmp(const void *p1
, const void *p2
)
257 return cmp_expr(*((U
**) p1
), *((U
**) p2
));
292 test(__FILE__
, s
, sizeof s
/ sizeof (char *));