Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeweigt committed Apr 28, 2024
1 parent 76ff250 commit 330e6b9
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 72 deletions.
30 changes: 14 additions & 16 deletions eigenmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,8 @@ void flatten_terms(int h);
struct atom * combine_tensors(int h);
void add_tensors(void);
void combine_terms(int h);
int combine_terms_nib(int i, int j);
void sort_terms(int n);
int combine_terms_nib(int i);
void sort_terms(int h);
int sort_terms_func(const void *q1, const void *q2);
int cmp_terms(struct atom *p1, struct atom *p2);
int simplify_terms(int h);
Expand Down Expand Up @@ -2207,37 +2207,35 @@ void
combine_terms(int h)
{
int i;
sort_terms(tos - h);
for (i = h; i < tos - 1; i++) {
if (combine_terms_nib(i, i + 1)) {
sort_terms(h);
for (i = h; i < tos; i++) {
if (iszero(stack[i])) {
slice(i, 1); // remove
i--; // use same index again
continue;
}
if (i + 1 < tos && combine_terms_nib(i)) {
if (iszero(stack[i]))
slice(i, 2); // remove 2 terms
else
slice(i + 1, 1); // remove 1 term
i--; // use same index again
}
}
if (h < tos && iszero(stack[tos - 1]))
tos--;
}

int
combine_terms_nib(int i, int j)
combine_terms_nib(int i)
{
int denorm;
struct atom *coeff1, *coeff2, *p1, *p2;

p1 = stack[i];
p2 = stack[j];
p2 = stack[i + 1];

if (iszero(p2))
return 1;

if (iszero(p1)) {
stack[i] = p2;
return 1;
}

if (isnum(p1) && isnum(p2)) {
add_numbers(p1, p2);
stack[i] = pop();
Expand Down Expand Up @@ -2315,9 +2313,9 @@ combine_terms_nib(int i, int j)
}

void
sort_terms(int n)
sort_terms(int h)
{
qsort(stack + tos - n, n, sizeof (struct atom *), sort_terms_func);
qsort(stack + h, tos - h, sizeof (struct atom *), sort_terms_func);
}

int
Expand Down
20 changes: 9 additions & 11 deletions js/eigenmath.js
Original file line number Diff line number Diff line change
Expand Up @@ -3301,35 +3301,33 @@ combine_terms(h)
{
var i;
sort_terms(h);
for (i = h; i < stack.length - 1; i++) {
if (combine_terms_nib(i, i + 1)) {
for (i = h; i < stack.length; i++) {
if (iszero(stack[i])) {
stack.splice(i, 1); // remove
i--; // use same index again
continue;
}
if (i + 1 < stack.length && combine_terms_nib(i)) {
if (iszero(stack[i]))
stack.splice(i, 2); // remove 2 terms
else
stack.splice(i + 1, 1); // remove 1 term
i--; // use same index again
}
}
if (h < stack.length && iszero(stack[stack.length - 1]))
stack.pop();
}

function
combine_terms_nib(i, j)
combine_terms_nib(i)
{
var coeff1, coeff2, denorm, p1, p2;

p1 = stack[i];
p2 = stack[j];
p2 = stack[i + 1];

if (iszero(p2))
return 1;

if (iszero(p1)) {
stack[i] = p2;
return 1;
}

if (isnum(p1) && isnum(p2)) {
add_numbers(p1, p2);
stack[i] = pop();
Expand Down
20 changes: 9 additions & 11 deletions js/src/eval_add.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,35 +144,33 @@ combine_terms(h)
{
var i;
sort_terms(h);
for (i = h; i < stack.length - 1; i++) {
if (combine_terms_nib(i, i + 1)) {
for (i = h; i < stack.length; i++) {
if (iszero(stack[i])) {
stack.splice(i, 1); // remove
i--; // use same index again
continue;
}
if (i + 1 < stack.length && combine_terms_nib(i)) {
if (iszero(stack[i]))
stack.splice(i, 2); // remove 2 terms
else
stack.splice(i + 1, 1); // remove 1 term
i--; // use same index again
}
}
if (h < stack.length && iszero(stack[stack.length - 1]))
stack.pop();
}

function
combine_terms_nib(i, j)
combine_terms_nib(i)
{
var coeff1, coeff2, denorm, p1, p2;

p1 = stack[i];
p2 = stack[j];
p2 = stack[i + 1];

if (iszero(p2))
return 1;

if (iszero(p1)) {
stack[i] = p2;
return 1;
}

if (isnum(p1) && isnum(p2)) {
add_numbers(p1, p2);
stack[i] = pop();
Expand Down
26 changes: 12 additions & 14 deletions src/eval_add.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,37 +147,35 @@ void
combine_terms(int h)
{
int i;
sort_terms(tos - h);
for (i = h; i < tos - 1; i++) {
if (combine_terms_nib(i, i + 1)) {
sort_terms(h);
for (i = h; i < tos; i++) {
if (iszero(stack[i])) {
slice(i, 1); // remove
i--; // use same index again
continue;
}
if (i + 1 < tos && combine_terms_nib(i)) {
if (iszero(stack[i]))
slice(i, 2); // remove 2 terms
else
slice(i + 1, 1); // remove 1 term
i--; // use same index again
}
}
if (h < tos && iszero(stack[tos - 1]))
tos--;
}

int
combine_terms_nib(int i, int j)
combine_terms_nib(int i)
{
int denorm;
struct atom *coeff1, *coeff2, *p1, *p2;

p1 = stack[i];
p2 = stack[j];
p2 = stack[i + 1];

if (iszero(p2))
return 1;

if (iszero(p1)) {
stack[i] = p2;
return 1;
}

if (isnum(p1) && isnum(p2)) {
add_numbers(p1, p2);
stack[i] = pop();
Expand Down Expand Up @@ -255,9 +253,9 @@ combine_terms_nib(int i, int j)
}

void
sort_terms(int n)
sort_terms(int h)
{
qsort(stack + tos - n, n, sizeof (struct atom *), sort_terms_func);
qsort(stack + h, tos - h, sizeof (struct atom *), sort_terms_func);
}

int
Expand Down
4 changes: 2 additions & 2 deletions src/prototypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ void flatten_terms(int h);
struct atom * combine_tensors(int h);
void add_tensors(void);
void combine_terms(int h);
int combine_terms_nib(int i, int j);
void sort_terms(int n);
int combine_terms_nib(int i);
void sort_terms(int h);
int sort_terms_func(const void *q1, const void *q2);
int cmp_terms(struct atom *p1, struct atom *p2);
int simplify_terms(int h);
Expand Down
30 changes: 14 additions & 16 deletions xcode/src/eigenmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,8 @@ void flatten_terms(int h);
struct atom * combine_tensors(int h);
void add_tensors(void);
void combine_terms(int h);
int combine_terms_nib(int i, int j);
void sort_terms(int n);
int combine_terms_nib(int i);
void sort_terms(int h);
int sort_terms_func(const void *q1, const void *q2);
int cmp_terms(struct atom *p1, struct atom *p2);
int simplify_terms(int h);
Expand Down Expand Up @@ -2207,37 +2207,35 @@ void
combine_terms(int h)
{
int i;
sort_terms(tos - h);
for (i = h; i < tos - 1; i++) {
if (combine_terms_nib(i, i + 1)) {
sort_terms(h);
for (i = h; i < tos; i++) {
if (iszero(stack[i])) {
slice(i, 1); // remove
i--; // use same index again
continue;
}
if (i + 1 < tos && combine_terms_nib(i)) {
if (iszero(stack[i]))
slice(i, 2); // remove 2 terms
else
slice(i + 1, 1); // remove 1 term
i--; // use same index again
}
}
if (h < tos && iszero(stack[tos - 1]))
tos--;
}

int
combine_terms_nib(int i, int j)
combine_terms_nib(int i)
{
int denorm;
struct atom *coeff1, *coeff2, *p1, *p2;

p1 = stack[i];
p2 = stack[j];
p2 = stack[i + 1];

if (iszero(p2))
return 1;

if (iszero(p1)) {
stack[i] = p2;
return 1;
}

if (isnum(p1) && isnum(p2)) {
add_numbers(p1, p2);
stack[i] = pop();
Expand Down Expand Up @@ -2315,9 +2313,9 @@ combine_terms_nib(int i, int j)
}

void
sort_terms(int n)
sort_terms(int h)
{
qsort(stack + tos - n, n, sizeof (struct atom *), sort_terms_func);
qsort(stack + h, tos - h, sizeof (struct atom *), sort_terms_func);
}

int
Expand Down
4 changes: 2 additions & 2 deletions xcode/src/prototypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ void flatten_terms(int h);
struct atom * combine_tensors(int h);
void add_tensors(void);
void combine_terms(int h);
int combine_terms_nib(int i, int j);
void sort_terms(int n);
int combine_terms_nib(int i);
void sort_terms(int h);
int sort_terms_func(const void *q1, const void *q2);
int cmp_terms(struct atom *p1, struct atom *p2);
int simplify_terms(int h);
Expand Down

0 comments on commit 330e6b9

Please sign in to comment.