2004-03-03 21:24:06 +01:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
#define DEBUG 0
|
|
|
|
|
|
|
|
static void gcd_expr_expr(void);
|
|
|
|
static void gcd_expr(U *);
|
|
|
|
static void gcd_term_term(void);
|
|
|
|
static void gcd_term_factor(void);
|
|
|
|
static void gcd_factor_term(void);
|
|
|
|
|
2006-04-25 20:31:30 +02:00
|
|
|
void
|
|
|
|
eval_gcd(void)
|
|
|
|
{
|
|
|
|
p1 = cdr(p1);
|
|
|
|
push(car(p1));
|
|
|
|
eval();
|
|
|
|
p1 = cdr(p1);
|
|
|
|
while (iscons(p1)) {
|
|
|
|
push(car(p1));
|
|
|
|
eval();
|
|
|
|
gcd();
|
|
|
|
p1 = cdr(p1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-03 21:24:06 +01:00
|
|
|
void
|
|
|
|
gcd(void)
|
|
|
|
{
|
|
|
|
int x = expanding;
|
|
|
|
save();
|
2005-10-27 19:39:15 +02:00
|
|
|
yygcd();
|
2004-03-03 21:24:06 +01:00
|
|
|
restore();
|
|
|
|
expanding = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-10-27 19:39:15 +02:00
|
|
|
yygcd(void)
|
2004-03-03 21:24:06 +01:00
|
|
|
{
|
|
|
|
expanding = 1;
|
|
|
|
|
|
|
|
p2 = pop();
|
|
|
|
p1 = pop();
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
printf("gcd: these are the two operands:\n");
|
|
|
|
print(stdout, p1);
|
|
|
|
print(stdout, p2);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (equal(p1, p2)) {
|
|
|
|
push(p1);
|
|
|
|
return;
|
|
|
|
}
|
2004-08-08 01:05:33 +02:00
|
|
|
|
2005-08-06 22:57:37 +02:00
|
|
|
if (isrational(p1) && isrational(p2)) {
|
2004-03-03 21:24:06 +01:00
|
|
|
push(p1);
|
|
|
|
push(p2);
|
|
|
|
gcd_numbers();
|
|
|
|
return;
|
|
|
|
}
|
2004-08-08 01:05:33 +02:00
|
|
|
|
2004-03-03 21:24:06 +01:00
|
|
|
if (car(p1) == symbol(ADD) && car(p2) == symbol(ADD)) {
|
|
|
|
gcd_expr_expr();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (car(p1) == symbol(ADD)) {
|
|
|
|
gcd_expr(p1);
|
|
|
|
p1 = pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (car(p2) == symbol(ADD)) {
|
|
|
|
gcd_expr(p2);
|
|
|
|
p2 = pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (car(p1) == symbol(MULTIPLY) && car(p2) == symbol(MULTIPLY)) {
|
|
|
|
gcd_term_term();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (car(p1) == symbol(MULTIPLY)) {
|
|
|
|
gcd_term_factor();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (car(p2) == symbol(MULTIPLY)) {
|
|
|
|
gcd_factor_term();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// gcd of factors
|
|
|
|
|
|
|
|
if (car(p1) == symbol(POWER)) {
|
|
|
|
p3 = caddr(p1);
|
|
|
|
p1 = cadr(p1);
|
|
|
|
} else
|
2004-06-25 22:45:15 +02:00
|
|
|
p3 = one;
|
2004-03-03 21:24:06 +01:00
|
|
|
|
|
|
|
if (car(p2) == symbol(POWER)) {
|
|
|
|
p4 = caddr(p2);
|
|
|
|
p2 = cadr(p2);
|
|
|
|
} else
|
2004-06-25 22:45:15 +02:00
|
|
|
p4 = one;
|
2004-03-03 21:24:06 +01:00
|
|
|
|
|
|
|
if (!equal(p1, p2)) {
|
2004-06-25 22:45:15 +02:00
|
|
|
push(one);
|
2004-03-03 21:24:06 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// are both exponents numerical?
|
|
|
|
|
|
|
|
if (isnum(p3) && isnum(p4)) {
|
|
|
|
push(p1);
|
|
|
|
if (lessp(p3, p4))
|
|
|
|
push(p3);
|
|
|
|
else
|
|
|
|
push(p4);
|
|
|
|
power();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// are the exponents multiples of eah other?
|
|
|
|
|
|
|
|
push(p3);
|
|
|
|
push(p4);
|
|
|
|
divide();
|
|
|
|
|
|
|
|
p5 = pop();
|
|
|
|
|
|
|
|
if (isnum(p5)) {
|
|
|
|
|
|
|
|
push(p1);
|
|
|
|
|
|
|
|
// choose the smallest exponent
|
|
|
|
|
|
|
|
if (car(p3) == symbol(MULTIPLY) && isnum(cadr(p3)))
|
|
|
|
p5 = cadr(p3);
|
|
|
|
else
|
2004-06-25 22:45:15 +02:00
|
|
|
p5 = one;
|
2004-03-03 21:24:06 +01:00
|
|
|
|
|
|
|
if (car(p4) == symbol(MULTIPLY) && isnum(cadr(p4)))
|
|
|
|
p6 = cadr(p4);
|
|
|
|
else
|
2004-06-25 22:45:15 +02:00
|
|
|
p6 = one;
|
2004-03-03 21:24:06 +01:00
|
|
|
|
|
|
|
if (lessp(p5, p6))
|
|
|
|
push(p3);
|
|
|
|
else
|
|
|
|
push(p4);
|
|
|
|
|
|
|
|
power();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
push(p3);
|
|
|
|
push(p4);
|
|
|
|
subtract();
|
|
|
|
|
|
|
|
p5 = pop();
|
|
|
|
|
|
|
|
if (!isnum(p5)) {
|
2004-06-25 22:45:15 +02:00
|
|
|
push(one);
|
2004-03-03 21:24:06 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// can't be equal because of test near beginning
|
|
|
|
|
|
|
|
push(p1);
|
|
|
|
|
|
|
|
if (isnegativenumber(p5))
|
|
|
|
push(p3);
|
|
|
|
else
|
|
|
|
push(p4);
|
|
|
|
|
|
|
|
power();
|
|
|
|
}
|
|
|
|
|
|
|
|
// in this case gcd is used as a composite function, i.e. gcd(gcd(gcd...
|
|
|
|
|
|
|
|
static void
|
|
|
|
gcd_expr_expr(void)
|
|
|
|
{
|
|
|
|
if (length(p1) != length(p2)) {
|
2004-06-25 22:45:15 +02:00
|
|
|
push(one);
|
2004-03-03 21:24:06 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p3 = cdr(p1);
|
|
|
|
push(car(p3));
|
|
|
|
p3 = cdr(p3);
|
|
|
|
while (iscons(p3)) {
|
|
|
|
push(car(p3));
|
|
|
|
gcd();
|
|
|
|
p3 = cdr(p3);
|
|
|
|
}
|
|
|
|
p3 = pop();
|
|
|
|
|
|
|
|
p4 = cdr(p2);
|
|
|
|
push(car(p4));
|
|
|
|
p4 = cdr(p4);
|
|
|
|
while (iscons(p4)) {
|
|
|
|
push(car(p4));
|
|
|
|
gcd();
|
|
|
|
p4 = cdr(p4);
|
|
|
|
}
|
|
|
|
p4 = pop();
|
|
|
|
|
|
|
|
push(p1);
|
|
|
|
push(p3);
|
|
|
|
divide();
|
|
|
|
p5 = pop();
|
|
|
|
|
|
|
|
push(p2);
|
|
|
|
push(p4);
|
|
|
|
divide();
|
|
|
|
p6 = pop();
|
|
|
|
|
|
|
|
if (equal(p5, p6)) {
|
|
|
|
push(p5);
|
|
|
|
push(p3);
|
|
|
|
push(p4);
|
|
|
|
gcd();
|
|
|
|
multiply();
|
|
|
|
} else
|
2004-06-25 22:45:15 +02:00
|
|
|
push(one);
|
2004-03-03 21:24:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gcd_expr(U *p)
|
|
|
|
{
|
|
|
|
p = cdr(p);
|
|
|
|
push(car(p));
|
|
|
|
p = cdr(p);
|
|
|
|
while (iscons(p)) {
|
|
|
|
push(car(p));
|
|
|
|
gcd();
|
|
|
|
p = cdr(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gcd_term_term(void)
|
|
|
|
{
|
2004-06-25 22:45:15 +02:00
|
|
|
push(one);
|
2004-03-03 21:24:06 +01:00
|
|
|
p3 = cdr(p1);
|
|
|
|
while (iscons(p3)) {
|
|
|
|
p4 = cdr(p2);
|
|
|
|
while (iscons(p4)) {
|
|
|
|
push(car(p3));
|
|
|
|
push(car(p4));
|
|
|
|
gcd();
|
|
|
|
multiply();
|
|
|
|
p4 = cdr(p4);
|
|
|
|
}
|
|
|
|
p3 = cdr(p3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gcd_term_factor(void)
|
|
|
|
{
|
2004-06-25 22:45:15 +02:00
|
|
|
push(one);
|
2004-03-03 21:24:06 +01:00
|
|
|
p3 = cdr(p1);
|
|
|
|
while (iscons(p3)) {
|
|
|
|
push(car(p3));
|
|
|
|
push(p2);
|
|
|
|
gcd();
|
|
|
|
multiply();
|
|
|
|
p3 = cdr(p3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gcd_factor_term(void)
|
|
|
|
{
|
2004-06-25 22:45:15 +02:00
|
|
|
push(one);
|
2004-03-03 21:24:06 +01:00
|
|
|
p4 = cdr(p2);
|
|
|
|
while (iscons(p4)) {
|
|
|
|
push(p1);
|
|
|
|
push(car(p4));
|
|
|
|
gcd();
|
|
|
|
multiply();
|
|
|
|
p4 = cdr(p4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-08 16:57:30 +02:00
|
|
|
#if SELFTEST
|
|
|
|
|
2004-03-03 21:24:06 +01:00
|
|
|
static char *s[] = {
|
|
|
|
|
|
|
|
"gcd(30,42)",
|
|
|
|
"6",
|
|
|
|
|
|
|
|
"gcd(42,30)",
|
|
|
|
"6",
|
|
|
|
|
|
|
|
"gcd(-30,42)",
|
|
|
|
"6",
|
|
|
|
|
|
|
|
"gcd(42,-30)",
|
|
|
|
"6",
|
|
|
|
|
|
|
|
"gcd(30,-42)",
|
|
|
|
"6",
|
|
|
|
|
|
|
|
"gcd(-42,30)",
|
|
|
|
"6",
|
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|
|
|
|
|
|
|
"gcd(2*a^2*x^2+a*x+a*b,a)",
|
|
|
|
"a",
|
|
|
|
|
|
|
|
"gcd(2*a^2*x^2+a*x+a*b,a^2)",
|
|
|
|
"a",
|
|
|
|
|
|
|
|
"gcd(2*a^2*x^2+2*a*x+2*a*b,a)",
|
|
|
|
"a",
|
|
|
|
|
|
|
|
// gcd expr term
|
|
|
|
|
|
|
|
"gcd(2*a^2*x^2+2*a*x+2*a*b,2*a)",
|
|
|
|
"2*a",
|
|
|
|
|
|
|
|
"gcd(2*a^2*x^2+2*a*x+2*a*b,3*a)",
|
|
|
|
"a",
|
|
|
|
|
|
|
|
"gcd(2*a^2*x^2+2*a*x+2*a*b,4*a)",
|
|
|
|
"2*a",
|
|
|
|
|
|
|
|
// gcd factor factor
|
|
|
|
|
|
|
|
"gcd(x,x^2)",
|
|
|
|
"x",
|
|
|
|
|
|
|
|
"gcd(x,x^a)",
|
|
|
|
"1",
|
2006-04-25 20:31:30 +02:00
|
|
|
|
|
|
|
// multiple arguments
|
|
|
|
|
|
|
|
"gcd(12,18,9)",
|
|
|
|
"3",
|
2004-03-03 21:24:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
test_gcd(void)
|
|
|
|
{
|
|
|
|
test(__FILE__, s, sizeof s / sizeof (char *));
|
|
|
|
}
|
2007-05-08 16:57:30 +02:00
|
|
|
|
|
|
|
#endif
|