Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeweigt committed Jun 14, 2024
1 parent 285fc01 commit 38ea1f6
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 73 deletions.
33 changes: 18 additions & 15 deletions eigenmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -728,8 +728,8 @@ void simplify(void);
void simplify_nib(void);
void simplify_trig(void);
int simpler(struct atom *p1, struct atom *p2);
int powdep(struct atom *p);
int complexity(struct atom *p);
int diameter(struct atom *p);
int mass(struct atom *p);
void eval_sin(struct atom *p1);
void sinfunc(void);
void sinfunc_sum(struct atom *p1);
Expand Down Expand Up @@ -12797,6 +12797,7 @@ simplify_nib(void)
push(DEN);
push(NUM);
divide();
rationalize();
reciprocate();
p2 = pop();
if (simpler(p2, p1)) {
Expand Down Expand Up @@ -12837,29 +12838,31 @@ simplify_trig(void)
int
simpler(struct atom *p1, struct atom *p2)
{
int n1, n2;
int d1, d2;

n1 = powdep(p1);
n2 = powdep(p2);
d1 = diameter(p1);
d2 = diameter(p2);

if (n1 == n2)
return complexity(p1) < complexity(p2);
else
return n1 < n2;
if (d1 == d2) {
d1 = mass(p1);
d2 = mass(p2);
}

return d1 < d2;
}

// for example, 1 / (x + y^2 / x) has powdep of 2
// for example, 1 / (x + y^2 / x) has diameter of 2

int
powdep(struct atom *p)
diameter(struct atom *p)
{
int max = 0, n;

if (car(p) == symbol(POWER) && isnegativenumber(caddr(p)))
return 1 + powdep(cadr(p));
return 1 + diameter(cadr(p));

while (iscons(p)) {
n = powdep(car(p));
n = diameter(car(p));
if (n > max)
max = n;
p = cdr(p);
Expand All @@ -12869,11 +12872,11 @@ powdep(struct atom *p)
}

int
complexity(struct atom *p)
mass(struct atom *p)
{
int n = 1;
while (iscons(p)) {
n += complexity(car(p));
n += mass(car(p));
p = cdr(p);
}
return n;
Expand Down
29 changes: 16 additions & 13 deletions js/eigenmath.js
Original file line number Diff line number Diff line change
Expand Up @@ -11751,6 +11751,7 @@ simplify_nib()
push(DEN);
push(NUM);
divide();
rationalize();
reciprocate();
p2 = pop();
if (simpler(p2, p1)) {
Expand Down Expand Up @@ -11791,29 +11792,31 @@ simplify_trig()
function
simpler(p1, p2)
{
var n1, n2;
var d1, d2;

n1 = powdep(p1);
n2 = powdep(p2);
d1 = diameter(p1);
d2 = diameter(p2);

if (n1 == n2)
return complexity(p1) < complexity(p2);
else
return n1 < n2;
if (d1 == d2) {
d1 = mass(p1);
d2 = mass(p2);
}

return d1 < d2;
}

// for example, 1 / (x + y^2 / x) has powdep of 2
// for example, 1 / (x + y^2 / x) has diameter of 2

function
powdep(p)
diameter(p)
{
var max = 0, n;

if (car(p) == symbol(POWER) && isnegativenumber(caddr(p)))
return 1 + powdep(cadr(p));
return 1 + diameter(cadr(p));

while (iscons(p)) {
n = powdep(car(p));
n = diameter(car(p));
if (n > max)
max = n;
p = cdr(p);
Expand All @@ -11823,11 +11826,11 @@ powdep(p)
}

function
complexity(p)
mass(p)
{
var n = 1;
while (iscons(p)) {
n += complexity(car(p));
n += mass(car(p));
p = cdr(p);
}
return n;
Expand Down
29 changes: 16 additions & 13 deletions js/src/eval_simplify.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ simplify_nib()
push(DEN);
push(NUM);
divide();
rationalize();
reciprocate();
p2 = pop();
if (simpler(p2, p1)) {
Expand Down Expand Up @@ -180,29 +181,31 @@ simplify_trig()
function
simpler(p1, p2)
{
var n1, n2;
var d1, d2;

n1 = powdep(p1);
n2 = powdep(p2);
d1 = diameter(p1);
d2 = diameter(p2);

if (n1 == n2)
return complexity(p1) < complexity(p2);
else
return n1 < n2;
if (d1 == d2) {
d1 = mass(p1);
d2 = mass(p2);
}

return d1 < d2;
}

// for example, 1 / (x + y^2 / x) has powdep of 2
// for example, 1 / (x + y^2 / x) has diameter of 2

function
powdep(p)
diameter(p)
{
var max = 0, n;

if (car(p) == symbol(POWER) && isnegativenumber(caddr(p)))
return 1 + powdep(cadr(p));
return 1 + diameter(cadr(p));

while (iscons(p)) {
n = powdep(car(p));
n = diameter(car(p));
if (n > max)
max = n;
p = cdr(p);
Expand All @@ -212,11 +215,11 @@ powdep(p)
}

function
complexity(p)
mass(p)
{
var n = 1;
while (iscons(p)) {
n += complexity(car(p));
n += mass(car(p));
p = cdr(p);
}
return n;
Expand Down
29 changes: 16 additions & 13 deletions src/eval_simplify.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ simplify_nib(void)
push(DEN);
push(NUM);
divide();
rationalize();
reciprocate();
p2 = pop();
if (simpler(p2, p1)) {
Expand Down Expand Up @@ -180,29 +181,31 @@ simplify_trig(void)
int
simpler(struct atom *p1, struct atom *p2)
{
int n1, n2;
int d1, d2;

n1 = powdep(p1);
n2 = powdep(p2);
d1 = diameter(p1);
d2 = diameter(p2);

if (n1 == n2)
return complexity(p1) < complexity(p2);
else
return n1 < n2;
if (d1 == d2) {
d1 = mass(p1);
d2 = mass(p2);
}

return d1 < d2;
}

// for example, 1 / (x + y^2 / x) has powdep of 2
// for example, 1 / (x + y^2 / x) has diameter of 2

int
powdep(struct atom *p)
diameter(struct atom *p)
{
int max = 0, n;

if (car(p) == symbol(POWER) && isnegativenumber(caddr(p)))
return 1 + powdep(cadr(p));
return 1 + diameter(cadr(p));

while (iscons(p)) {
n = powdep(car(p));
n = diameter(car(p));
if (n > max)
max = n;
p = cdr(p);
Expand All @@ -212,11 +215,11 @@ powdep(struct atom *p)
}

int
complexity(struct atom *p)
mass(struct atom *p)
{
int n = 1;
while (iscons(p)) {
n += complexity(car(p));
n += mass(car(p));
p = cdr(p);
}
return n;
Expand Down
4 changes: 2 additions & 2 deletions src/prototypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ void simplify(void);
void simplify_nib(void);
void simplify_trig(void);
int simpler(struct atom *p1, struct atom *p2);
int powdep(struct atom *p);
int complexity(struct atom *p);
int diameter(struct atom *p);
int mass(struct atom *p);
void eval_sin(struct atom *p1);
void sinfunc(void);
void sinfunc_sum(struct atom *p1);
Expand Down
4 changes: 4 additions & 0 deletions test/selftest1
Original file line number Diff line number Diff line change
Expand Up @@ -8662,6 +8662,10 @@ check(sqrt(N) == 46349) -- bug fix

clear

check(infixform(simplify(2 a / (2 b + 2 c))) == "a / (b + c)")
check(infixform(simplify(4 a / (2 b + 2 c))) == "2 a / (b + c)")
check(infixform(simplify(2 a / (4 b + 4 c))) == "a / (2 b + 2 c)")

-- ratio of expressions
check(infixform(simplify((a x + a) / (x + 1))) == "a")

Expand Down
4 changes: 4 additions & 0 deletions test/src1/test-simplify
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

clear

check(infixform(simplify(2 a / (2 b + 2 c))) == "a / (b + c)")
check(infixform(simplify(4 a / (2 b + 2 c))) == "2 a / (b + c)")
check(infixform(simplify(2 a / (4 b + 4 c))) == "a / (2 b + 2 c)")

-- ratio of expressions
check(infixform(simplify((a x + a) / (x + 1))) == "a")

Expand Down
33 changes: 18 additions & 15 deletions xcode/src/eigenmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -728,8 +728,8 @@ void simplify(void);
void simplify_nib(void);
void simplify_trig(void);
int simpler(struct atom *p1, struct atom *p2);
int powdep(struct atom *p);
int complexity(struct atom *p);
int diameter(struct atom *p);
int mass(struct atom *p);
void eval_sin(struct atom *p1);
void sinfunc(void);
void sinfunc_sum(struct atom *p1);
Expand Down Expand Up @@ -12797,6 +12797,7 @@ simplify_nib(void)
push(DEN);
push(NUM);
divide();
rationalize();
reciprocate();
p2 = pop();
if (simpler(p2, p1)) {
Expand Down Expand Up @@ -12837,29 +12838,31 @@ simplify_trig(void)
int
simpler(struct atom *p1, struct atom *p2)
{
int n1, n2;
int d1, d2;

n1 = powdep(p1);
n2 = powdep(p2);
d1 = diameter(p1);
d2 = diameter(p2);

if (n1 == n2)
return complexity(p1) < complexity(p2);
else
return n1 < n2;
if (d1 == d2) {
d1 = mass(p1);
d2 = mass(p2);
}

return d1 < d2;
}

// for example, 1 / (x + y^2 / x) has powdep of 2
// for example, 1 / (x + y^2 / x) has diameter of 2

int
powdep(struct atom *p)
diameter(struct atom *p)
{
int max = 0, n;

if (car(p) == symbol(POWER) && isnegativenumber(caddr(p)))
return 1 + powdep(cadr(p));
return 1 + diameter(cadr(p));

while (iscons(p)) {
n = powdep(car(p));
n = diameter(car(p));
if (n > max)
max = n;
p = cdr(p);
Expand All @@ -12869,11 +12872,11 @@ powdep(struct atom *p)
}

int
complexity(struct atom *p)
mass(struct atom *p)
{
int n = 1;
while (iscons(p)) {
n += complexity(car(p));
n += mass(car(p));
p = cdr(p);
}
return n;
Expand Down
4 changes: 2 additions & 2 deletions xcode/src/prototypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,8 @@ void simplify(void);
void simplify_nib(void);
void simplify_trig(void);
int simpler(struct atom *p1, struct atom *p2);
int powdep(struct atom *p);
int complexity(struct atom *p);
int diameter(struct atom *p);
int mass(struct atom *p);
void eval_sin(struct atom *p1);
void sinfunc(void);
void sinfunc_sum(struct atom *p1);
Expand Down

0 comments on commit 38ea1f6

Please sign in to comment.